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;
114
AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
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
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);
185
request->setManager(ContactUtils::sharedManager());
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());
199
// just emit the signal to pretend we did a contact search
200
Q_EMIT request->stateChanged(QContactAbstractRequest::FinishedState);
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(),
206
221
call.contactAlias.toUtf8().data(),
209
224
call.timestamp.toMSecsSinceEpoch() * 1000); // the value is expected to be in microseconds
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,
215
230
C::gettext("Call back"), // label
242
257
g_object_unref(message);
245
void MessagingMenu::addCall(const QString &phoneNumber, const QString &accountId, const QDateTime ×tamp)
260
void MessagingMenu::addCall(const QString &targetId, const QString &accountId, const QDateTime ×tamp)
248
263
bool found = false;
264
AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
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;
253
274
mCalls.removeOne(callMessage);
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;
273
294
text = QString::fromUtf8(C::ngettext("%1 missed call", "%1 missed calls", call.count)).arg(call.count);
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);
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);
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
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));
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);
313
request->setManager(ContactUtils::sharedManager());
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());
342
// just emit the signal to pretend we did a contact search
343
Q_EMIT request->stateChanged(QContactAbstractRequest::FinishedState);
317
347
void MessagingMenu::showVoicemailEntry(AccountEntry *account)
349
OfonoAccountEntry *ofonoAccount = qobject_cast<OfonoAccountEntry*>(account);
319
354
messaging_menu_app_remove_message_by_id(mCallsApp, account->accountId().toUtf8().data());
320
355
mVoicemailIds.removeAll(account->accountId());
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);
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());
345
380
g_object_unref(icon);
346
381
g_object_unref(message);
411
446
void MessagingMenu::saveFlashMessage(const QString &messageId)
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,
419
457
History::TextEvent textEvent(details["accountId"].toString(),
420
458
thread.threadId(),
448
486
void MessagingMenu::callBack(const QString &messageId)
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);
491
qWarning() << "Could not find the account originating the call";
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))));
455
500
void MessagingMenu::replyWithMessage(const QString &messageId, const QString &reply)
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);
462
507
void MessagingMenu::callVoicemail(const QString &messageId)
464
509
QString voicemailNumber;
465
Q_FOREACH(AccountEntry *accountEntry, TelepathyHelper::instance()->accounts()) {
466
if (!accountEntry->voicemailNumber().isEmpty() && messageId == accountEntry->accountId()) {
467
voicemailNumber = accountEntry->voicemailNumber();
510
// get the corresponding account
511
OfonoAccountEntry *ofonoAccount = qobject_cast<OfonoAccountEntry*>(TelepathyHelper::instance()->accountForId(messageId));
513
voicemailNumber = ofonoAccount->voicemailNumber();
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)));