~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to indicator/messagingmenu.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:
27
27
#include "messagingmenu.h"
28
28
#include "telepathyhelper.h"
29
29
#include "accountentry.h"
 
30
#include "ofonoaccountentry.h"
30
31
#include <QContactAvatar>
31
32
#include <QContactFetchRequest>
32
33
#include <QContactFilter>
110
111
    QUrl iconPath = QUrl::fromLocalFile(telephonyServiceDir() + "/assets/avatar-default@18.png");
111
112
    QString contactAlias = senderId;
112
113
 
 
114
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
 
115
    if (!account) {
 
116
        return;
 
117
    }
 
118
 
 
119
    // FIXME: for accounts not based on phone number, we need to match other fields.
 
120
    // Right now we don't even bother trying to match contact data
 
121
 
113
122
    // try to match the contact info
114
123
    QContactFetchRequest *request = new QContactFetchRequest(this);
115
124
    request->setFilter(QContactPhoneNumber::match(senderId));
182
191
        g_object_unref(message);
183
192
    });
184
193
 
185
 
    request->setManager(ContactUtils::sharedManager());
186
 
    request->start();
 
194
    // FIXME: For accounts not based on phone numbers, don't try to match contacts for now
 
195
    if (account->type() == AccountEntry::PhoneAccount) {
 
196
        request->setManager(ContactUtils::sharedManager());
 
197
        request->start();
 
198
    } else {
 
199
        // just emit the signal to pretend we did a contact search
 
200
        Q_EMIT request->stateChanged(QContactAbstractRequest::FinishedState);
 
201
    }
187
202
}
188
203
 
189
204
void MessagingMenu::removeMessage(const QString &messageId)
201
216
    GVariant *messages = NULL;
202
217
    GFile *file = g_file_new_for_uri(call.contactIcon.toString().toUtf8().data());
203
218
    GIcon *icon = g_file_icon_new(file);
204
 
    MessagingMenuMessage *message = messaging_menu_message_new(call.number.toUtf8().data(),
 
219
    MessagingMenuMessage *message = messaging_menu_message_new(call.targetId.toUtf8().data(),
205
220
                                                               icon,
206
221
                                                               call.contactAlias.toUtf8().data(),
207
222
                                                               NULL,
209
224
                                                               call.timestamp.toMSecsSinceEpoch() * 1000);  // the value is expected to be in microseconds
210
225
 
211
226
    call.messageId = messaging_menu_message_get_id(message);
212
 
    if (call.number != "x-ofono-private" && call.number != "x-ofono-unknown") {
 
227
    if (call.targetId != "x-ofono-private" && call.targetId != "x-ofono-unknown") {
213
228
        messaging_menu_message_add_action(message,
214
229
                                          "callBack",
215
230
                                          C::gettext("Call back"), // label
242
257
    g_object_unref(message);
243
258
}
244
259
 
245
 
void MessagingMenu::addCall(const QString &phoneNumber, const QString &accountId, const QDateTime &timestamp)
 
260
void MessagingMenu::addCall(const QString &targetId, const QString &accountId, const QDateTime &timestamp)
246
261
{
247
262
    Call call;
248
263
    bool found = false;
 
264
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
 
265
    if (!account) {
 
266
        return;
 
267
    }
 
268
 
249
269
    Q_FOREACH(Call callMessage, mCalls) {
250
 
        if (PhoneUtils::comparePhoneNumbers(callMessage.number, phoneNumber)) {
 
270
        // FIXME: we need a better strategy to group calls from different accounts
 
271
        if (account->compareIds(callMessage.targetId, targetId)) {
251
272
            call = callMessage;
252
273
            found = true;
253
274
            mCalls.removeOne(callMessage);
259
280
    }
260
281
 
261
282
    if (!found) {
262
 
        call.contactAlias = phoneNumber;
 
283
        call.contactAlias = targetId;
263
284
        call.accountId = accountId;
264
285
        call.contactIcon = QUrl::fromLocalFile(telephonyServiceDir() + "/assets/avatar-default@18.png");
265
 
        call.number = phoneNumber;
 
286
        call.targetId = targetId;
266
287
        call.count = 0;
267
288
    }
268
289
 
272
293
    QString text;
273
294
    text = QString::fromUtf8(C::ngettext("%1 missed call", "%1 missed calls", call.count)).arg(call.count);
274
295
 
275
 
    if (phoneNumber.startsWith("x-ofono-private")) {
 
296
    if (targetId.startsWith("x-ofono-private")) {
276
297
        call.contactAlias = C::gettext("Private number");
277
298
        addCallToMessagingMenu(call, text);
278
299
        return;
279
 
    } else if (phoneNumber.startsWith("x-ofono-unknown")) {
 
300
    } else if (targetId.startsWith("x-ofono-unknown")) {
280
301
        call.contactAlias = C::gettext("Unknown number");
281
302
        addCallToMessagingMenu(call, text);
282
303
        return;
283
304
    }
284
305
 
 
306
    // FIXME: we need to match other fields for accounts not based on phone numbers.
 
307
    // For now we are not even trying to match contact data
 
308
 
285
309
    // try to match the contact info
286
310
    QContactFetchRequest *request = new QContactFetchRequest(this);
287
 
    request->setFilter(QContactPhoneNumber::match(phoneNumber));
 
311
    request->setFilter(QContactPhoneNumber::match(targetId));
288
312
 
289
313
    // place the messaging-menu item only after the contact fetch request is finished, as we can´t simply update
290
314
    QObject::connect(request, &QContactAbstractRequest::stateChanged, [request, call, text, this]() {
310
334
        addCallToMessagingMenu(newCall, text);
311
335
    });
312
336
 
313
 
    request->setManager(ContactUtils::sharedManager());
314
 
    request->start();
 
337
    // FIXME: For accounts not based on phone numbers, don't try to match contacts for now
 
338
    if (account->type() == AccountEntry::PhoneAccount) {
 
339
        request->setManager(ContactUtils::sharedManager());
 
340
        request->start();
 
341
    } else {
 
342
        // just emit the signal to pretend we did a contact search
 
343
        Q_EMIT request->stateChanged(QContactAbstractRequest::FinishedState);
 
344
    }
315
345
}
316
346
 
317
347
void MessagingMenu::showVoicemailEntry(AccountEntry *account)
318
348
{
 
349
    OfonoAccountEntry *ofonoAccount = qobject_cast<OfonoAccountEntry*>(account);
 
350
    if (!ofonoAccount) {
 
351
        return;
 
352
    }
 
353
 
319
354
    messaging_menu_app_remove_message_by_id(mCallsApp, account->accountId().toUtf8().data());
320
355
    mVoicemailIds.removeAll(account->accountId());
321
356
 
322
357
    QString messageBody = C::gettext("Voicemail messages");
323
 
    uint count = account->voicemailCount();
 
358
    uint count = ofonoAccount->voicemailCount();
324
359
    if (count != 0) {
325
360
        messageBody = QString::fromUtf8(C::ngettext("%1 voicemail message", "%1 voicemail messages", count)).arg(count);
326
361
    }
340
375
                                                               QDateTime::currentDateTime().toMSecsSinceEpoch() * 1000); // the value is expected to be in microseconds
341
376
    g_signal_connect(message, "activate", G_CALLBACK(&MessagingMenu::callsActivateCallback), this);
342
377
    messaging_menu_app_append_message(mCallsApp, message, SOURCE_ID, true);
343
 
    mVoicemailIds.append(account->accountId());
 
378
    mVoicemailIds.append(ofonoAccount->accountId());
344
379
 
345
380
    g_object_unref(icon);
346
381
    g_object_unref(message);
411
446
void MessagingMenu::saveFlashMessage(const QString &messageId)
412
447
{
413
448
    QVariantMap details = mMessages[messageId];
 
449
    AccountEntry *account = TelepathyHelper::instance()->accountForId(details["accountId"].toString());
 
450
    bool phoneNumberBased = account && (account->type() == AccountEntry::PhoneAccount);
414
451
    History::Thread thread = History::Manager::instance()->threadForParticipants(details["accountId"].toString(),
415
452
                                                                                 History::EventTypeText,
416
453
                                                                                 QStringList() << details["senderId"].toString(),
417
 
                                                                                 History::MatchPhoneNumber,
 
454
                                                                                 phoneNumberBased ? History::MatchPhoneNumber :
 
455
                                                                                                    History::MatchCaseSensitive,
418
456
                                                                                 true);
419
457
    History::TextEvent textEvent(details["accountId"].toString(),
420
458
                                 thread.threadId(), 
447
485
 
448
486
void MessagingMenu::callBack(const QString &messageId)
449
487
{
450
 
    QString phoneNumber = callFromMessageId(messageId).number;
451
 
    qDebug() << "TelephonyService/MessagingMenu: Calling back" << phoneNumber;
452
 
    ApplicationUtils::openUrl(QString("tel:///%1").arg(QString(QUrl::toPercentEncoding(phoneNumber))));
 
488
    Call call = callFromMessageId(messageId);
 
489
    AccountEntry *account = TelepathyHelper::instance()->accountForId(call.accountId);
 
490
    if (!account) {
 
491
        qWarning() << "Could not find the account originating the call";
 
492
    }
 
493
    qDebug() << "TelephonyService/MessagingMenu: Calling back" << call.targetId;
 
494
    // FIXME: support accounts not based on phone numbers
 
495
    if (account->type() == AccountEntry::PhoneAccount) {
 
496
        ApplicationUtils::openUrl(QString("tel:///%1").arg(QString(QUrl::toPercentEncoding(call.targetId))));
 
497
    }
453
498
}
454
499
 
455
500
void MessagingMenu::replyWithMessage(const QString &messageId, const QString &reply)
456
501
{
457
502
    Call call = callFromMessageId(messageId);
458
 
    qDebug() << "TelephonyService/MessagingMenu: Replying to call" << call.number << "with text" << reply;
459
 
    Q_EMIT replyReceived(QStringList() << call.number, call.accountId, reply);
 
503
    qDebug() << "TelephonyService/MessagingMenu: Replying to call" << call.targetId << "with text" << reply;
 
504
    Q_EMIT replyReceived(QStringList() << call.targetId, call.accountId, reply);
460
505
}
461
506
 
462
507
void MessagingMenu::callVoicemail(const QString &messageId)
463
508
{
464
509
    QString voicemailNumber;
465
 
    Q_FOREACH(AccountEntry *accountEntry, TelepathyHelper::instance()->accounts()) {
466
 
        if (!accountEntry->voicemailNumber().isEmpty() && messageId == accountEntry->accountId()) {
467
 
            voicemailNumber = accountEntry->voicemailNumber();
468
 
            break;
469
 
        }
 
510
    // get the corresponding account
 
511
    OfonoAccountEntry *ofonoAccount = qobject_cast<OfonoAccountEntry*>(TelepathyHelper::instance()->accountForId(messageId));
 
512
    if (ofonoAccount) {
 
513
        voicemailNumber = ofonoAccount->voicemailNumber();
470
514
    }
471
515
 
472
516
    qDebug() << "TelephonyService/MessagingMenu: Calling voicemail for messageId" << messageId;
473
517
    if (!voicemailNumber.isEmpty()) {
 
518
        // FIXME: we need to specify which account to use
474
519
        ApplicationUtils::openUrl(QUrl(QString("tel:///%1").arg(voicemailNumber)));
475
520
    }
476
521
}