~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to handler/texthandler.cpp

Support accounts not based on phone numbers.
Approved by: PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
        
78
78
        if (mPendingMessages.contains(accountId)) {
79
79
            // create text channels to send the pending messages
80
 
            Q_FOREACH(const QStringList& phoneNumbers, mPendingMessages[accountId].keys()) {
81
 
                startChat(phoneNumbers, accountId);
 
80
            Q_FOREACH(const QStringList& recipients, mPendingMessages[accountId].keys()) {
 
81
                startChat(recipients, accountId);
82
82
            }
83
83
        }
84
84
        if (mPendingMMSs.contains(accountId)) {
85
85
            // create text channels to send the pending MMSs
86
 
            Q_FOREACH(const QStringList& phoneNumbers, mPendingMMSs[accountId].keys()) {
87
 
                startChat(phoneNumbers, accountId);
 
86
            Q_FOREACH(const QStringList& recipients, mPendingMMSs[accountId].keys()) {
 
87
                startChat(recipients, accountId);
88
88
            }
89
89
        }
90
90
        if (mPendingSilentMessages.contains(accountId)) {
91
91
            // create text channels to send the pending silent messages
92
 
            Q_FOREACH(const QStringList& phoneNumbers, mPendingSilentMessages[accountId].keys()) {
93
 
                startChat(phoneNumbers, accountId);
 
92
            Q_FOREACH(const QStringList& recipients, mPendingSilentMessages[accountId].keys()) {
 
93
                startChat(recipients, accountId);
94
94
            }
95
95
        }
96
96
 
103
103
    return handler;
104
104
}
105
105
 
106
 
void TextHandler::startChat(const QStringList &phoneNumbers, const QString &accountId)
 
106
void TextHandler::startChat(const QStringList &recipients, const QString &accountId)
107
107
{
108
108
    // Request the contact to start chatting to
109
109
    // FIXME: make it possible to select which account to use, for now, pick the first one
113
113
        return;
114
114
    }
115
115
 
116
 
    connect(account->account()->connection()->contactManager()->contactsForIdentifiers(phoneNumbers),
 
116
    connect(account->account()->connection()->contactManager()->contactsForIdentifiers(recipients),
117
117
            SIGNAL(finished(Tp::PendingOperation*)),
118
118
            SLOT(onContactsAvailable(Tp::PendingOperation*)));
119
119
}
205
205
    return message;
206
206
}
207
207
 
208
 
void TextHandler::sendMMS(const QStringList &phoneNumbers, const AttachmentList &attachments, const QString &accountId) {
 
208
void TextHandler::sendMMS(const QStringList &recipients, const AttachmentList &attachments, const QString &accountId) {
209
209
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
210
210
    if (!account) {
211
211
        // account does not exist
212
212
        return;
213
213
    }
214
214
    if (!account->connected()) {
215
 
        mPendingMMSs[accountId][phoneNumbers].append(attachments);
 
215
        mPendingMMSs[accountId][recipients].append(attachments);
216
216
        return;
217
217
    }
218
218
 
219
 
    Tp::TextChannelPtr channel = existingChat(phoneNumbers, accountId);
 
219
    Tp::TextChannelPtr channel = existingChat(recipients, accountId);
220
220
    if (channel.isNull()) {
221
 
        mPendingMMSs[accountId][phoneNumbers].append(attachments);
222
 
        startChat(phoneNumbers, accountId);
 
221
        mPendingMMSs[accountId][recipients].append(attachments);
 
222
        startChat(recipients, accountId);
223
223
        return;
224
224
    }
225
225
 
230
230
            SLOT(onMessageSent(Tp::PendingOperation*)));
231
231
}
232
232
 
233
 
void TextHandler::sendSilentMessage(const QStringList &phoneNumbers, const QString &message, const QString &accountId)
 
233
void TextHandler::sendSilentMessage(const QStringList &recipients, const QString &message, const QString &accountId)
234
234
{
235
235
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
236
236
    if (!account) {
238
238
        return;
239
239
    }
240
240
    if (!account->connected()) {
241
 
        mPendingSilentMessages[accountId][phoneNumbers].append(message);
 
241
        mPendingSilentMessages[accountId][recipients].append(message);
242
242
        return;
243
243
    }
244
 
    Tp::TextChannelPtr channel = existingChat(phoneNumbers, accountId);
 
244
    Tp::TextChannelPtr channel = existingChat(recipients, accountId);
245
245
    if (channel.isNull()) {
246
 
        mPendingSilentMessages[accountId][phoneNumbers].append(message);
247
 
        startChat(phoneNumbers, accountId);
 
246
        mPendingSilentMessages[accountId][recipients].append(message);
 
247
        startChat(recipients, accountId);
248
248
        return;
249
249
    }
250
250
 
263
263
            SLOT(onMessageSent(Tp::PendingOperation*)));
264
264
}
265
265
 
266
 
void TextHandler::sendMessage(const QStringList &phoneNumbers, const QString &message, const QString &accountId)
 
266
void TextHandler::sendMessage(const QStringList &recipients, const QString &message, const QString &accountId)
267
267
{
268
268
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
269
269
    if (!account) {
271
271
        return;
272
272
    }
273
273
    if (!account->connected()) {
274
 
        mPendingMessages[accountId][phoneNumbers].append(message);
 
274
        mPendingMessages[accountId][recipients].append(message);
275
275
        return;
276
276
    }
277
277
 
278
 
    Tp::TextChannelPtr channel = existingChat(phoneNumbers, accountId);
 
278
    Tp::TextChannelPtr channel = existingChat(recipients, accountId);
279
279
    if (channel.isNull()) {
280
 
        mPendingMessages[accountId][phoneNumbers].append(message);
281
 
        startChat(phoneNumbers, accountId);
 
280
        mPendingMessages[accountId][recipients].append(message);
 
281
        startChat(recipients, accountId);
282
282
        return;
283
283
    }
284
284
 
287
287
            SLOT(onMessageSent(Tp::PendingOperation*)));
288
288
}
289
289
 
290
 
void TextHandler::acknowledgeMessages(const QStringList &phoneNumbers, const QStringList &messageIds, const QString &accountId)
 
290
void TextHandler::acknowledgeMessages(const QStringList &recipients, const QStringList &messageIds, const QString &accountId)
291
291
{
292
 
    Tp::TextChannelPtr channel = existingChat(phoneNumbers, accountId);
 
292
    Tp::TextChannelPtr channel = existingChat(recipients, accountId);
293
293
    if (channel.isNull()) {
294
294
        return;
295
295
    }
321
321
        return;
322
322
    }
323
323
 
324
 
    Q_FOREACH(const Tp::ContactPtr &phoneNumberOld, channel->groupContacts(false)) {
325
 
        recipients << phoneNumberOld->id();
 
324
    Q_FOREACH(const Tp::ContactPtr &channelContact, channel->groupContacts(false)) {
 
325
        recipients << channelContact->id();
326
326
    }
327
327
 
328
328
    QMap<QStringList, QStringList> &pendingMessages = mPendingMessages[accountId];
377
377
    }
378
378
}
379
379
 
380
 
Tp::TextChannelPtr TextHandler::existingChat(const QStringList &phoneNumbers, const QString &accountId)
 
380
Tp::TextChannelPtr TextHandler::existingChat(const QStringList &targetIds, const QString &accountId)
381
381
{
382
382
    Tp::TextChannelPtr channel;
383
383
 
384
384
    Q_FOREACH(const Tp::TextChannelPtr &channel, mChannels) {
385
385
        int count = 0;
386
386
        AccountEntry *channelAccount = TelepathyHelper::instance()->accountForConnection(channel->connection());
387
 
        if (!channelAccount || channel->groupContacts(false).size() != phoneNumbers.size()
 
387
        if (!channelAccount || channel->groupContacts(false).size() != targetIds.size()
388
388
            || channelAccount->accountId() != accountId) {
389
389
            continue;
390
390
        }
391
 
        Q_FOREACH(const QString &phoneNumberNew, phoneNumbers) {
392
 
            Q_FOREACH(const Tp::ContactPtr &phoneNumberOld, channel->groupContacts(false)) {
393
 
                if (PhoneUtils::comparePhoneNumbers(phoneNumberOld->id(), phoneNumberNew)) {
 
391
        Q_FOREACH(const QString &targetId, targetIds) {
 
392
            Q_FOREACH(const Tp::ContactPtr &channelContact, channel->groupContacts(false)) {
 
393
                if (channelAccount->compareIds(channelContact->id(), targetId)) {
394
394
                    count++;
395
395
                }
396
396
            }
397
397
        }
398
 
        if (count == phoneNumbers.size()) {
 
398
        if (count == targetIds.size()) {
399
399
            return channel;
400
400
        }
401
401
    }