~phablet-team/messaging-app/disable-attachments

« back to all changes in this revision

Viewing changes to src/qml/Messages.qml

  • Committer: Gustavo Pichorim Boiko
  • Date: 2016-11-21 16:26:32 UTC
  • mfrom: (631.5.7 send_message_validators)
  • Revision ID: gustavo.boiko@canonical.com-20161121162632-5wu2zgwgslc9rfea
Change the way validation is done when sending messages to allow adding more validation in the future if needed.

Also when MMS is disabled in settings and required to send a message, make it possible to enable it from a dialog displayed when sending the message.

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
 
105
105
    property bool isBroadcast: chatType != ChatEntry.ChatTypeRoom && (participantIds.length  > 1 || multiRecipient.recipientCount > 1)
106
106
 
 
107
    property alias validator: sendMessageValidator
 
108
 
107
109
    signal ready
108
110
    signal cancel
109
111
 
138
140
 
139
141
    function getSelectedIndex() {
140
142
        if (newMessage) {
141
 
            // if this is a new message, just pre select the the 
 
143
            // if this is a new message, just pre select the the
142
144
            // default phone account for messages if available
143
145
            if (multiplePhoneAccounts && telepathyHelper.defaultMessagingAccount) {
144
146
                for (var i in messages.accountsModel) {
216
218
                                           true)
217
219
        if (thread.length == 0) {
218
220
            return thread
219
 
        } 
 
221
        }
220
222
        var threadId = thread.threadId
221
223
 
222
224
        // dont change the participants list
301
303
        multiRecipient.forceActiveFocus()
302
304
    }
303
305
 
304
 
    function sendMessageSanityCheck(text, participantIds, attachments, properties) {
305
 
        // if MMS is enabled, we don't have to check for anything here
306
 
        if (telepathyHelper.mmsEnabled ) {
307
 
            return true
308
 
        }
309
 
 
310
 
        // if the account is not a phone one, we can also send the message
311
 
        if (messages.account.type != AccountEntry.PhoneAccount) {
312
 
            return true
313
 
        }
314
 
 
315
 
        // we need to check if there will be an overload for sending the message
316
 
        var accounts = telepathyHelper.accountOverload(messages.account)
317
 
        for (var i in accounts) {
318
 
            var account = accounts[i]
319
 
            if (account.active) {
320
 
                return true
321
 
            }
322
 
        }
323
 
 
324
 
        // now we are here with a phone account that doesn't support MMS
325
 
        // we check if MMS is required or not
326
 
        // for now it is only required in two cases: attachments and MMS groups
327
 
        // so if chatType is not Room and the attachment list is empty, we can send
328
 
        if (messages.chatType != ChatEntry.ChatTypeRoom && attachments.length == 0) {
329
 
            return true
330
 
        }
331
 
 
332
 
        // last but not least, show a warning to the user saying he needs to enable MMS to send the message
333
 
        var props = {}
334
 
        props["title"] = i18n.tr("MMS support required")
335
 
        props["text"] = i18n.tr("MMS support is required to send this message.\nPlease enable it in Settings->Enable MMS messages")
336
 
        PopupUtils.open(Qt.createComponent("Dialogs/InformationDialog.qml").createObject(messages), messages, props)
337
 
        return false
338
 
    }
339
 
 
340
306
    function sendMessage(text, participantIds, attachments, properties) {
341
307
        if (typeof(properties) === 'undefined') {
342
308
            properties = {}
343
309
        }
344
310
 
345
 
        // check if at least one account is selected
346
 
        if (!messages.account) {
347
 
            Qt.inputMethod.hide()
348
 
            // workaround for bug #1461861
349
 
            messages.focus = false
350
 
            var properties = {}
351
 
 
352
 
            if (telepathyHelper.flightMode) {
353
 
                properties["title"] = i18n.tr("You have to disable flight mode")
354
 
                properties["text"] = i18n.tr("It is not possible to send messages in flight mode")
355
 
            } else if (multiplePhoneAccounts) {
356
 
                properties["title"] = i18n.tr("No SIM card selected")
357
 
                properties["text"] = i18n.tr("You need to select a SIM card")
358
 
            } else if (telepathyHelper.phoneAccounts.all.length > 0 && telepathyHelper.phoneAccounts.active.length == 0) {
359
 
                properties["title"] = i18n.tr("No SIM card")
360
 
                properties["text"] = i18n.tr("Please insert a SIM card and try again.")
361
 
            } else {
362
 
                properties["text"] = i18n.tr("It is not possible to send the message")
363
 
                properties["title"] = i18n.tr("Failed to send the message")
364
 
            }
365
 
            PopupUtils.open(Qt.createComponent("Dialogs/InformationDialog.qml").createObject(messages), messages, properties)
366
 
            return false
367
 
        }
368
 
 
369
 
        if (!sendMessageSanityCheck(text, participantIds, attachments, properties)) {
370
 
            return false
371
 
        }
372
 
 
373
311
        if (messages.threads.length > 0) {
374
312
            properties["chatType"] = messages.chatType
375
313
            properties["threadId"] = messages.threadId
404
342
                }
405
343
                // if the last outgoing message used a different accountId, add an
406
344
                // information event and quit the loop
 
345
                var thread = eventModel.threadForProperties(messages.account.accountId,
 
346
                                                            HistoryEventModel.EventTypeText,
 
347
                                                            properties,
 
348
                                                            messages.account.usePhoneNumbers ? HistoryEventModel.MatchPhoneNumber : HistoryEventModel.MatchCaseSensitive,
 
349
                                                            true);
 
350
                if (!checkThreadInFilters(thread.accountId, thread.threadId)) {
 
351
                    addNewThreadToFilter(thread.accountId, thread)
 
352
                }
 
353
 
 
354
                messages.threadId = thread.threadId
407
355
                eventModel.writeTextInformationEvent(messages.account.accountId,
408
356
                                                     messages.threadId,
409
357
                                                     newParticipantsIds,
422
370
            // and insert it into the history service
423
371
 
424
372
            // FIXME: we need to review this case. In case of account overload, this will be saved in the wrong thread
425
 
            //        also, we probably need to create the thread here
 
373
 
 
374
            // create the thread
 
375
            var thread = eventModel.threadForProperties(messages.account.accountId,
 
376
                                                        HistoryEventModel.EventTypeText,
 
377
                                                        properties,
 
378
                                                        messages.account.usePhoneNumbers ? HistoryEventModel.MatchPhoneNumber : HistoryEventModel.MatchCaseSensitive,
 
379
                                                        true);
 
380
            if (!checkThreadInFilters(thread.accountId, thread.threadId)) {
 
381
                addNewThreadToFilter(thread.accountId, thread)
 
382
            }
 
383
            messages.threadId = thread.threadId
 
384
 
426
385
            var event = {}
427
386
            var timestamp = new Date()
428
387
            var tmpEventId = timestamp.toISOString()
705
664
                        } else if (roomInfo.RoomName != "") {
706
665
                            return roomInfo.RoomName
707
666
                        }
708
 
                        // include the "Me" participant to be consistent with 
 
667
                        // include the "Me" participant to be consistent with
709
668
                        // group info page
710
669
                        if (roomInfo.Joined) {
711
670
                            finalParticipants++
918
877
        processPendingEvents()
919
878
    }
920
879
 
921
 
    Item {
922
 
        id: mmsBroadcastChecker
923
 
 
924
 
        property var props: null
925
 
 
926
 
        function isMMSBroadcast(text, participantIds, attachments, properties) {
927
 
            var account = messages.account
928
 
            // if we don't have the account or if it is not a phone one, it is
929
 
            // not an MMS broadcast
930
 
            if (!account || account.type != AccountEntry.PhoneAccount) {
931
 
                return false
932
 
            }
933
 
 
934
 
            // if chatType is Room, this is not a broadcast
935
 
            if (chatType == ChatEntry.ChatTypeRoom) {
936
 
                return false
937
 
            }
938
 
 
939
 
            // if there is only one participant, it is also not a broadcast
940
 
            if (participantIds.length == 1) {
941
 
                return false
942
 
            }
943
 
 
944
 
            // if there is no attachments, that's not going via MMS
945
 
            if (attachments.length == 0) {
946
 
                return false
947
 
            }
948
 
 
949
 
            // if there is an active account overload, assume it is going to be used
950
 
            // and thus this won't be an MMS broadcast
951
 
            var accounts = telepathyHelper.accountOverload(account).length
952
 
            for (var i in accounts) {
953
 
                if (accounts[i].active) {
954
 
                    return false
955
 
                }
956
 
            }
957
 
 
958
 
            // if none of the cases above match, this is an MMS broadcast
959
 
            return true
960
 
        }
961
 
 
962
 
        function checkForBroadcastAndSend(text, participantIds, attachments, properties) {
963
 
            props = {"text" : text,
964
 
                     "participantIds" : participantIds,
965
 
                     "attachments" : attachments,
966
 
                     "properties" : properties
967
 
                    }
968
 
 
969
 
            if (isMMSBroadcast(text, participantIds, attachments, properties)) {
970
 
                var popup = PopupUtils.open(Qt.resolvedUrl("MMSBroadcastDialog.qml"), mmsBroadcastChecker, {})
971
 
                popup.accepted.connect(onPopupAccepted)
972
 
                return false
973
 
            }
974
 
 
975
 
            // if this is not an MMS broadcast, just send the message
976
 
            messages.sendMessage(text, participantIds, attachments, properties)
977
 
            return true
978
 
        }
979
 
 
980
 
 
981
 
        function onPopupAccepted() {
982
 
            if (messages.sendMessage(props["text"],
983
 
                                     props["participantIds"],
984
 
                                     props["attachments"],
985
 
                                     props["properties"])) {
986
 
                composeBar.reset()
987
 
            }
988
 
        }
989
 
 
990
 
    }
991
 
 
992
880
    // These fake items are used to track if there are instances loaded
993
881
    // on the second column because we have no access to the page stack
994
882
    Item {
1293
1181
 
1294
1182
    HistoryUnionFilter {
1295
1183
        id: filters
1296
 
        HistoryIntersectionFilter { 
 
1184
        HistoryIntersectionFilter {
1297
1185
            HistoryFilter { filterProperty: "accountId"; filterValue: messages.accountId }
1298
1186
            HistoryFilter { filterProperty: "threadId"; filterValue: messages.threadId }
1299
1187
        }
1528
1416
            }
1529
1417
 
1530
1418
            var newAttachments = []
1531
 
            var videoSize = 0;
1532
1419
            for (var i = 0; i < attachments.count; i++) {
1533
1420
                var attachment = []
1534
1421
                var item = attachments.get(i)
1536
1423
                if (item.contentType.toLowerCase() === "application/smil") {
1537
1424
                    continue
1538
1425
                }
1539
 
                if (startsWith(item.contentType.toLowerCase(),"video/")) {
1540
 
                    videoSize += FileOperations.size(item.filePath)
1541
 
                }
1542
1426
                attachment.push(item.name)
1543
1427
                attachment.push(item.contentType)
1544
1428
                attachment.push(item.filePath)
1545
1429
                newAttachments.push(attachment)
1546
1430
            }
1547
 
            if (videoSize > 307200 && !settings.messagesDontShowFileSizeWarning) {
1548
 
                // FIXME we are guessing here if the handler will try to send it over an overloaded account
1549
 
                // FIXME: this should be revisited when changing the MMS group implementation
1550
 
                var isPhone = (account && account.type == AccountEntry.PhoneAccount)
1551
 
                if (isPhone) {
1552
 
                    // check if an account overload might be used
1553
 
                    var accounts = telepathyHelper.accountOverload(account)
1554
 
                    for (var i in accounts) {
1555
 
                        var tmpAccount = accounts[i]
1556
 
                        if (tmpAccount.active) {
1557
 
                            isPhone = false
1558
 
                        }
1559
 
                    }
1560
 
                }
1561
 
 
1562
 
                if (isPhone) {
1563
 
                    PopupUtils.open(Qt.createComponent("Dialogs/FileSizeWarningDialog.qml").createObject(messages))
1564
 
                }
1565
 
            }
1566
1431
 
1567
1432
            var recipients = participantIds.length > 0 ? participantIds :
1568
1433
                                                         multiRecipient.recipients
1571
1436
                properties["x-canonical-tmp-files"] = true
1572
1437
            }
1573
1438
 
1574
 
            // if sendMessage succeeds it means the message was either sent or
1575
 
            // injected into the history service so the user can retry later
1576
 
            if (mmsBroadcastChecker.checkForBroadcastAndSend(text, recipients, newAttachments, properties)) {
1577
 
                composeBar.reset()
1578
 
            }
 
1439
            sendMessageValidator.validateMessageAndSend(text, recipients, newAttachments, properties)
 
1440
 
1579
1441
            if (eventModel.filter == null) {
1580
1442
                reloadFilters = !reloadFilters
1581
1443
            }
1582
1444
        }
1583
1445
    }
1584
1446
 
 
1447
    SendMessageValidator {
 
1448
        id: sendMessageValidator
 
1449
 
 
1450
        onMessageSent: composeBar.reset()
 
1451
    }
 
1452
 
1585
1453
    KeyboardRectangle {
1586
1454
        id: keyboard
1587
1455
    }