~ubuntu-branches/ubuntu/vivid/messaging-app/vivid

« back to all changes in this revision

Viewing changes to src/qml/Messages.qml

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, CI Train Bot, Omer Akram, Tiago Salem Herrmann
  • Date: 2015-02-11 18:09:01 UTC
  • mfrom: (1.1.66)
  • Revision ID: package-import@ubuntu.com-20150211180901-w6c8jiwsvzl4ojyw
Tags: 0.1+15.04.20150211-0ubuntu1
[ CI Train Bot ]
* Resync trunk

[ Omer Akram ]
* Provide a pre-populated database for search test so that we don't
  conflict with the On Screen Display Notification (LP: #1418074)

[ Tiago Salem Herrmann ]
* Inject messages into the history service when it is impossible to
  send them. (LP: #1417353)

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
        }
76
76
    }
77
77
 
78
 
    function sendMessageSanityCheck() {
79
 
        // check if at least one account is selected
80
 
        if (!messages.account) {
81
 
            Qt.inputMethod.hide()
82
 
            PopupUtils.open(Qt.createComponent("Dialogs/NoSIMCardSelectedDialog.qml").createObject(messages))
83
 
            return false
84
 
        }
85
 
 
 
78
    function sendMessageNetworkCheck() {
86
79
        if (messages.account.simLocked) {
87
80
            Qt.inputMethod.hide()
88
81
            PopupUtils.open(Qt.createComponent("Dialogs/SimLockedDialog.qml").createObject(messages))
104
97
        multiRecipient.forceActiveFocus()
105
98
    }
106
99
 
 
100
    function sendMessage(text, participants, attachments) {
 
101
        // check if at least one account is selected
 
102
        if (!messages.account) {
 
103
            Qt.inputMethod.hide()
 
104
            PopupUtils.open(Qt.createComponent("Dialogs/NoSIMCardSelectedDialog.qml").createObject(messages))
 
105
            return false
 
106
        }
 
107
 
 
108
        // create the new thread and update the threadId list
 
109
        var threadId = eventModel.threadIdForParticipants(messages.account.accountId,
 
110
                                           HistoryThreadModel.EventTypeText,
 
111
                                           participants,
 
112
                                           HistoryThreadModel.MatchPhoneNumber,
 
113
                                           true)
 
114
        for (var i=0; i < eventModel.count; i++) {
 
115
            var event = eventModel.get(i)
 
116
            if (event.senderId == "self" && event.accountId != messages.account.accountId) {
 
117
                // if the last outgoing message used a different accountId, add an
 
118
                // information event and quit the loop
 
119
                eventModel.writeTextInformationEvent(messages.account.accountId,
 
120
                                                     threadId,
 
121
                                                     participants,
 
122
                                                     "")
 
123
                break;
 
124
            } else if (event.senderId == "self" && event.accountId == messages.account.accountId) {
 
125
                // in case last ougoing event used the same accountId, just skip
 
126
                break;
 
127
            }
 
128
        }
 
129
 
 
130
        if (!sendMessageNetworkCheck()) {
 
131
            // we can't simply send the message as the handler checks for
 
132
            // connection state. while this is not fixed, we generate the event here
 
133
            // and insert it into the history service
 
134
            var event = {}
 
135
            var timestamp = new Date()
 
136
            var tmpEventId = timestamp.toISOString()
 
137
            event["accountId"] = messages.account.accountId
 
138
            event["threadId"] = threadId
 
139
            event["eventId"] =  tmpEventId
 
140
            event["type"] = HistoryEventModel.MessageTypeText
 
141
            event["participants"] = participants
 
142
            event["senderId"] = "self"
 
143
            event["timestamp"] = timestamp
 
144
            event["newEvent"] = false
 
145
            event["message"] = text
 
146
            event["messageStatus"] = HistoryEventModel.MessageStatusPermanentlyFailed
 
147
            event["readTimestamp"] = timestamp;
 
148
            event["subject"] = ""; // we dont support subject yet
 
149
            if (attachments.length > 0) {
 
150
                event["messageType"] = HistoryEventModel.MessageTypeMultiPart
 
151
                var newAttachments = []
 
152
                for (var i = 0; i < attachments.length; i++) {
 
153
                    var attachment = {}
 
154
                    var item = attachments[i]
 
155
                    attachment["accountId"] = messages.account.accountId
 
156
                    attachment["threadId"] = threadId
 
157
                    attachment["eventId"] = tmpEventId
 
158
                    attachment["attachmentId"] = item[0]
 
159
                    attachment["contentType"] = item[1]
 
160
                    attachment["filePath"] = item[2]
 
161
                    attachment["status"] = HistoryEventModel.AttachmentDownloaded
 
162
                    newAttachments.push(attachment)
 
163
                }
 
164
                event["attachments"] = newAttachments
 
165
            } else {
 
166
                event["messageType"] = HistoryEventModel.MessageTypeText
 
167
            }
 
168
            eventModel.writeEvents([event]);
 
169
        } else {
 
170
            var isMMS = attachments.length > 0
 
171
            var isMmsGroupChat = participants.length > 1 && telepathyHelper.mmsGroupChat
 
172
            // mms group chat only works if we know our own phone number
 
173
            var isSelfContactKnown = account.selfContactId != ""
 
174
            if (isMMS || (isMmsGroupChat && isSelfContactKnown)) {
 
175
                chatManager.sendMMS(participants, text, attachments, messages.account.accountId)
 
176
            } else {
 
177
                chatManager.sendMessage(participants, text, messages.account.accountId)
 
178
            }
 
179
        }
 
180
 
 
181
        if (multipleAccounts && !telepathyHelper.defaultMessagingAccount && !settings.messagesDontAsk) {
 
182
            Qt.inputMethod.hide()
 
183
            PopupUtils.open(Qt.createComponent("Dialogs/SetDefaultSIMCardDialog.qml").createObject(messages))
 
184
        }
 
185
        return true
 
186
    }
 
187
 
107
188
    // this is necessary to automatically update the view when the
108
189
    // default account changes in system settings
109
190
    Connections {
905
986
                return false
906
987
            }
907
988
            onClicked: {
908
 
                if (!sendMessageSanityCheck()) {
909
 
                    return
910
 
                }
911
 
 
912
 
                if (multipleAccounts && !telepathyHelper.defaultMessagingAccount && !settings.messagesDontAsk) {
913
 
                    Qt.inputMethod.hide()
914
 
                    PopupUtils.open(Qt.createComponent("Dialogs/SetDefaultSIMCardDialog.qml").createObject(messages))
915
 
                }
916
 
 
917
989
                // make sure we flush everything we have prepared in the OSK preedit
918
990
                Qt.inputMethod.commit();
919
991
                if (textEntry.text == "" && attachments.count == 0) {
925
997
                if (participants.length == 0) {
926
998
                    participants = multiRecipient.recipients
927
999
                }
928
 
                // create the new thread and update the threadId list
929
 
                var threadId = eventModel.threadIdForParticipants(messages.account.accountId,
930
 
                                                   HistoryThreadModel.EventTypeText,
931
 
                                                   participants,
932
 
                                                   HistoryThreadModel.MatchPhoneNumber,
933
 
                                                   true)
934
 
                for (var i=0; i < eventModel.count; i++) {
935
 
                    var event = eventModel.get(i)
936
 
                    if (event.senderId == "self" && event.accountId != messages.account.accountId) {
937
 
                        // if the last outgoing message used a different accountId, add an
938
 
                        // information event and quit the loop
939
 
                        eventModel.writeTextInformationEvent(messages.account.accountId,
940
 
                                                             threadId,
941
 
                                                             participants,
942
 
                                                             "")
943
 
                        break;
944
 
                    } else if (event.senderId == "self" && event.accountId == messages.account.accountId) {
945
 
                        // in case last ougoing event used the same accountId, just skip
946
 
                        break;
 
1000
 
 
1001
                var newAttachments = []
 
1002
                for (var i = 0; i < attachments.count; i++) {
 
1003
                    var attachment = []
 
1004
                    var item = attachments.get(i)
 
1005
                    // we dont include smil files. they will be auto generated 
 
1006
                    if (item.contentType.toLowerCase() === "application/smil") {
 
1007
                        continue
947
1008
                    }
 
1009
                    attachment.push(item.name)
 
1010
                    attachment.push(item.contentType)
 
1011
                    attachment.push(item.filePath)
 
1012
                    newAttachments.push(attachment)
948
1013
                }
949
 
                updateFilters()
950
 
                var isMMS = attachments.count > 0
951
 
                var isMmsGroupChat = participants.length > 1 && telepathyHelper.mmsGroupChat
952
 
                // mms group chat only works if we known our own phone number
953
 
                var isSelfContactKnown = account.selfContactId != ""
954
 
                if (isMMS || (isMmsGroupChat && isSelfContactKnown)) {
955
 
                    var newAttachments = []
956
 
                    for (var i = 0; i < attachments.count; i++) {
957
 
                        var attachment = []
958
 
                        var item = attachments.get(i)
959
 
                        attachment.push(item.name)
960
 
                        attachment.push(item.contentType)
961
 
                        attachment.push(item.filePath)
962
 
                        newAttachments.push(attachment)
963
 
                    }
964
 
                    chatManager.sendMMS(participants, textEntry.text, newAttachments, messages.account.accountId)
 
1014
 
 
1015
                // if sendMessage succeeds it means the message was either sent or
 
1016
                // injected into the history service so the user can retry later
 
1017
                if (sendMessage(textEntry.text, participants, newAttachments)) {
965
1018
                    textEntry.text = ""
966
1019
                    attachments.clear()
967
 
                    return
968
1020
                }
969
 
 
970
 
                chatManager.sendMessage(participants, textEntry.text, messages.account.accountId)
971
 
                textEntry.text = ""
 
1021
                updateFilters()
972
1022
            }
973
1023
        }
974
1024
    }