/
notificationmanager.cpp
876 lines (747 loc) · 32.8 KB
/
notificationmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/******************************************************************************
**
** This file is part of commhistory-daemon.
**
** Copyright (C) 2020 Open Mobile Platform LLC.
** Copyright (C) 2013-2016 Jolla Ltd.
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Contact: John Brooks <john.brooks@jolla.com>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/
// Qt includes
#include <QCoreApplication>
#include <QDBusReply>
#include <QDir>
// CommHistory includes
#include <CommHistory/commonutils.h>
#include <CommHistory/GroupModel>
#include <CommHistory/Group>
// Telepathy includes
#include <TelepathyQt/Constants>
// NGF-Qt includes
#include <NgfClient>
// nemo notifications
#include <notification.h>
// mce
#include <mce/dbus-names.h>
// Our includes
#include "qofonomanager.h"
#include "notificationmanager.h"
#include "locstrings.h"
#include "constants.h"
#include "debug.h"
using namespace RTComLogger;
using namespace CommHistory;
NotificationManager* NotificationManager::m_pInstance = 0;
static const QString NgfdEventSms("sms");
static const QString NgfdEventChat("chat");
// constructor
//
NotificationManager::NotificationManager(QObject* parent)
: QObject(parent)
, m_Initialised(false)
, m_contactResolver(0)
, m_GroupModel(0)
, m_ngfClient(0)
, m_ngfEvent(0)
{
}
NotificationManager::~NotificationManager()
{
qDeleteAll(interfaces.values());
qDeleteAll(m_Groups);
qDeleteAll(m_unresolvedNotifications);
}
void NotificationManager::addModem(QString path)
{
DEBUG() << "NotificationManager::addModem" << path;
QOfonoMessageWaiting *mw = new QOfonoMessageWaiting(this);
interfaces.insert(path, mw);
mw->setModemPath(path);
connect(mw, SIGNAL(voicemailWaitingChanged(bool)), SLOT(slotVoicemailWaitingChanged()));
connect(mw, SIGNAL(voicemailMessageCountChanged(int)), SLOT(slotVoicemailWaitingChanged()));
connect(mw, SIGNAL(validChanged(bool)), this, SLOT(slotValidChanged(bool)));
if (mw->isValid()) {
DEBUG() << "NotificationManager::addModem, mwi interface already valid";
slotVoicemailWaitingChanged();
}
}
void NotificationManager::init()
{
if (m_Initialised) {
return;
}
m_contactResolver = new ContactResolver(this);
connect(m_contactResolver, SIGNAL(finished()),
SLOT(slotContactResolveFinished()));
m_contactListener = ContactListener::instance();
connect(m_contactListener.data(), SIGNAL(contactChanged(RecipientList)),
SLOT(slotContactChanged(RecipientList)));
connect(m_contactListener.data(), SIGNAL(contactInfoChanged(RecipientList)),
SLOT(slotContactInfoChanged(RecipientList)));
m_ngfClient = new Ngf::Client(this);
connect(m_ngfClient, SIGNAL(eventFailed(quint32)), SLOT(slotNgfEventFinished(quint32)));
connect(m_ngfClient, SIGNAL(eventCompleted(quint32)), SLOT(slotNgfEventFinished(quint32)));
ofonoManager = QOfonoManager::instance();
QOfonoManager* ofono = ofonoManager.data();
connect(ofono, SIGNAL(modemsChanged(QStringList)), this, SLOT(slotModemsChanged(QStringList)));
connect(ofono, SIGNAL(modemAdded(QString)), this, SLOT(slotModemAdded(QString)));
connect(ofono, SIGNAL(modemRemoved(QString)), this, SLOT(slotModemRemoved(QString)));
QStringList modems = ofono->modems();
DEBUG() << "Created modem manager";
foreach (QString path, modems) {
addModem(path);
}
// Loads old state
syncNotifications();
CommHistoryService *service = CommHistoryService::instance();
connect(service, SIGNAL(inboxObservedChanged(bool,QString)), SLOT(slotInboxObservedChanged()));
connect(service, SIGNAL(callHistoryObservedChanged(bool)), SLOT(slotCallHistoryObservedChanged(bool)));
connect(service, SIGNAL(observedConversationsChanged(QList<CommHistoryService::Conversation>)),
SLOT(slotObservedConversationsChanged(QList<CommHistoryService::Conversation>)));
groupModel();
m_Initialised = true;
}
void NotificationManager::syncNotifications()
{
QList<PersonalNotification*> pnList;
QMap<int,int> typeCounts;
QList<QObject*> notifications = Notification::notifications();
foreach (QObject *o, notifications) {
Notification *n = static_cast<Notification*>(o);
if (n->hintValue("x-commhistoryd-data").isNull()) {
// This was a group notification, which will be recreated if required
n->close();
delete n;
} else {
PersonalNotification *pn = new PersonalNotification(this);
if (!pn->restore(n)) {
delete pn;
n->close();
delete n;
continue;
}
typeCounts[pn->eventType()]++;
pnList.append(pn);
}
}
foreach (PersonalNotification *pn, pnList)
resolveNotification(pn);
}
NotificationManager* NotificationManager::instance()
{
if (!m_pInstance) {
m_pInstance = new NotificationManager(QCoreApplication::instance());
m_pInstance->init();
}
return m_pInstance;
}
bool NotificationManager::updateEditedEvent(const CommHistory::Event& event, const QString &text)
{
if (event.messageToken().isEmpty())
return false;
foreach (PersonalNotification *notification, m_unresolvedNotifications) {
if (notification->eventToken() == event.messageToken()) {
notification->setNotificationText(text);
return true;
}
}
EventGroupProperties groupProperties(eventGroup(PersonalNotification::collection(event.type()), event.recipients().value(0)));
NotificationGroup *eventGroup = m_Groups.value(groupProperties);
if (!eventGroup)
return false;
foreach (PersonalNotification *pn, eventGroup->notifications()) {
if (pn->eventToken() == event.messageToken()) {
pn->setNotificationText(text);
return true;
}
}
return false;
}
void NotificationManager::showNotification(const CommHistory::Event& event,
const QString& channelTargetId,
CommHistory::Group::ChatType chatType,
const QString &details)
{
DEBUG() << Q_FUNC_INFO << event.id() << channelTargetId << chatType;
if (event.type() == CommHistory::Event::SMSEvent
|| event.type() == CommHistory::Event::MMSEvent
|| event.type() == CommHistory::Event::IMEvent)
{
bool inboxObserved = CommHistoryService::instance()->inboxObserved();
if (inboxObserved || isCurrentlyObservedByUI(event, channelTargetId, chatType)) {
if (!m_ngfClient->isConnected())
m_ngfClient->connect();
if (!m_ngfEvent) {
QMap<QString, QVariant> properties;
properties.insert("play.mode", "foreground");
const QString *ngfEvent;
if (event.type() == CommHistory::Event::SMSEvent || event.type() == CommHistory::Event::MMSEvent) {
ngfEvent = &NgfdEventSms;
} else {
ngfEvent = &NgfdEventChat;
}
DEBUG() << Q_FUNC_INFO << "play ngf event: " << ngfEvent;
m_ngfEvent = m_ngfClient->play(*ngfEvent, properties);
}
return;
}
}
// try to update notifications for existing event
QString text(notificationText(event, details));
if (event.isValid() && updateEditedEvent(event, text))
return;
// Get MUC topic from group
QString chatName;
if (m_GroupModel && (chatType == CommHistory::Group::ChatTypeUnnamed ||
chatType == CommHistory::Group::ChatTypeRoom)) {
for (int i = 0; i < m_GroupModel->rowCount(); i++) {
QModelIndex row = m_GroupModel->index(i, 0);
CommHistory::Group group = m_GroupModel->group(row);
if (group.isValid() && group.id() == event.groupId()) {
chatName = group.chatName();
if (chatName.isEmpty())
chatName = txt_qtn_msg_group_chat;
DEBUG() << Q_FUNC_INFO << "Using chatName:" << chatName;
break;
}
}
}
PersonalNotification *notification = new PersonalNotification(event.recipients().value(0).remoteUid(),
event.localUid(), event.type(), channelTargetId, chatType);
notification->setNotificationText(text);
notification->setSmsReplaceNumber(event.headers().value(REPLACE_TYPE));
if (!chatName.isEmpty())
notification->setChatName(chatName);
notification->setEventToken(event.messageToken());
resolveNotification(notification);
}
void NotificationManager::resolveNotification(PersonalNotification *pn)
{
if (pn->remoteUid() == QLatin1String("<hidden>") ||
!pn->chatName().isEmpty() ||
pn->recipient().isContactResolved()) {
// Add notification immediately
addNotification(pn);
} else {
DEBUG() << Q_FUNC_INFO << "Trying to resolve contact for" << pn->account() << pn->remoteUid();
m_unresolvedNotifications.append(pn);
m_contactResolver->add(pn->recipient());
}
}
void NotificationManager::playClass0SMSAlert()
{
if (!m_ngfClient->isConnected())
m_ngfClient->connect();
m_ngfEvent = m_ngfClient->play(QLatin1Literal("sms"));
// ask mce to undim the screen
QString mceMethod = QString::fromLatin1(MCE_DISPLAY_ON_REQ);
QDBusMessage msg = QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF, mceMethod);
QDBusConnection::systemBus().call(msg, QDBus::NoBlock);
}
void NotificationManager::requestClass0Notification(const CommHistory::Event &event)
{
QDBusMessage msg = QDBusMessage::createMethodCall(QLatin1String("org.nemomobile.ClassZeroSmsNotification"),
QLatin1String("/org/nemomobile/ClassZeroSmsNotification"),
QLatin1String("org.nemomobile.ClassZeroSmsNotification"),
QLatin1String("showNotification"));
QList<QVariant> arguments;
arguments << event.freeText();
msg.setArguments(arguments);
if (!QDBusConnection::sessionBus().callWithCallback(msg, this, 0, SLOT(slotClassZeroError(QDBusError)))) {
qWarning() << "Unable to create class 0 SMS notification request";
}
}
bool NotificationManager::isCurrentlyObservedByUI(const CommHistory::Event& event,
const QString &channelTargetId,
CommHistory::Group::ChatType chatType)
{
// Return false if it's not message event (IM or SMS/MMS)
CommHistory::Event::EventType eventType = event.type();
if (eventType != CommHistory::Event::IMEvent
&& eventType != CommHistory::Event::SMSEvent
&& eventType != CommHistory::Event::MMSEvent)
{
return false;
}
QString remoteMatch;
if (chatType == CommHistory::Group::ChatTypeP2P)
remoteMatch = event.recipients().value(0).remoteUid();
else
remoteMatch = channelTargetId;
const Recipient messageRecipient(event.localUid(), remoteMatch);
foreach (const CommHistoryService::Conversation &conversation, CommHistoryService::instance()->observedConversations()) {
if (conversation.first.matches(messageRecipient) && conversation.second == chatType)
return true;
}
return false;
}
void NotificationManager::removeNotifications(const QString &accountPath, const QList<int> &removeTypes)
{
DEBUG() << Q_FUNC_INFO << "Removing notifications of account " << accountPath;
QSet<NotificationGroup> updatedGroups;
// remove matched notifications and update group
foreach (NotificationGroup *group, m_Groups) {
if (group->localUid() != accountPath) {
continue;
}
foreach (PersonalNotification *notification, group->notifications()) {
if (removeTypes.isEmpty() || removeTypes.contains(notification->eventType())) {
DEBUG() << Q_FUNC_INFO << "Removing notification: accountPath: " << notification->account() << " remoteUid: " << notification->remoteUid();
group->removeNotification(notification);
}
}
}
for (QList<PersonalNotification*>::iterator it = m_unresolvedNotifications.begin();
it != m_unresolvedNotifications.end(); ) {
if ((*it)->account() == accountPath) {
delete *it;
it = m_unresolvedNotifications.erase(it);
} else
it++;
}
}
void NotificationManager::removeConversationNotifications(const CommHistory::Recipient &recipient,
CommHistory::Group::ChatType chatType)
{
QHash<EventGroupProperties, NotificationGroup *>::const_iterator it = m_Groups.constBegin(), end = m_Groups.constEnd();
for ( ; it != end; ++it) {
NotificationGroup *group(it.value());
foreach (PersonalNotification *notification, group->notifications()) {
if (notification->collection() != PersonalNotification::Messaging ||
notification->chatType() != chatType)
continue;
// For p-to-p chat we use remote uid for comparison and for MUC we use target (channel) id:
bool match(false);
if (chatType == CommHistory::Group::ChatTypeP2P)
match = recipient.matches(notification->recipient());
else
match = recipient.matches(Recipient(notification->account(), notification->targetId()));
if (match)
group->removeNotification(notification);
}
}
}
void NotificationManager::slotObservedConversationsChanged(const QList<CommHistoryService::Conversation> &conversations)
{
foreach (const CommHistoryService::Conversation &conversation, conversations) {
removeConversationNotifications(conversation.first, static_cast<CommHistory::Group::ChatType>(conversation.second));
}
}
void NotificationManager::slotInboxObservedChanged()
{
DEBUG() << Q_FUNC_INFO;
// Cannot be passed as a parameter, because this slot is also used for m_notificationTimer
bool observed = CommHistoryService::instance()->inboxObserved();
if (observed) {
QList<int> removeTypes;
removeTypes << CommHistory::Event::IMEvent << CommHistory::Event::SMSEvent << CommHistory::Event::MMSEvent << VOICEMAIL_SMS_EVENT_TYPE;
if (!isFilteredInbox()) {
// remove sms, mms and im notifications
removeNotificationTypes(removeTypes);
} else {
// Filtering is in use, remove only notifications of that account whose threads are visible in inbox:
QString filteredAccountPath = filteredInboxAccountPath();
DEBUG() << Q_FUNC_INFO << "Removing only notifications belonging to account " << filteredAccountPath;
if (!filteredAccountPath.isEmpty())
removeNotifications(filteredAccountPath, removeTypes);
}
}
}
void NotificationManager::slotCallHistoryObservedChanged(bool observed)
{
if (observed) {
removeNotificationTypes(QList<int>() << CommHistory::Event::CallEvent);
}
}
bool NotificationManager::isFilteredInbox()
{
return !CommHistoryService::instance()->inboxFilterAccount().isEmpty();
}
QString NotificationManager::filteredInboxAccountPath()
{
return CommHistoryService::instance()->inboxFilterAccount();
}
void NotificationManager::removeNotificationTypes(const QList<int> &types)
{
DEBUG() << Q_FUNC_INFO << types;
foreach (NotificationGroup *group, m_Groups) {
foreach (PersonalNotification *notification, group->notifications()) {
if (types.contains(notification->eventType())) {
group->removeNotification(notification);
}
}
}
}
void NotificationManager::addNotification(PersonalNotification *notification)
{
EventGroupProperties groupProperties(eventGroup(notification->collection(), CommHistory::Recipient(notification->account(), notification->remoteUid())));
NotificationGroup *group = m_Groups.value(groupProperties);
if (!group) {
group = new NotificationGroup(groupProperties.collection, notification->account(), notification->remoteUid(), this);
m_Groups.insert(groupProperties, group);
}
group->addNotification(notification);
}
int NotificationManager::pendingEventCount()
{
return m_unresolvedNotifications.size();
}
QString NotificationManager::notificationText(const CommHistory::Event& event, const QString &details)
{
QString text;
switch(event.type())
{
case CommHistory::Event::IMEvent:
case CommHistory::Event::SMSEvent:
{
text = event.fromVCardLabel().isEmpty()
? event.freeText()
: txt_qtn_msg_notification_new_vcard(event.fromVCardLabel());
break;
}
case CommHistory::Event::MMSEvent:
{
if (event.status() == Event::ManualNotificationStatus) {
text = txt_qtn_mms_notification_manual_download;
} else if (event.status() >= Event::TemporarilyFailedStatus) {
QString trimmedDetails(details.trimmed());
if (trimmedDetails.isEmpty()) {
if (event.direction() == Event::Inbound)
text = txt_qtn_mms_notification_download_failed;
else
text = txt_qtn_mms_notification_send_failed;
} else {
text = trimmedDetails;
}
} else {
if (!event.subject().isEmpty())
text = event.subject();
else
text = event.freeText();
int attachmentCount = 0;
foreach (const MessagePart &part, event.messageParts()) {
if (!part.contentType().startsWith("text/plain") &&
!part.contentType().startsWith("application/smil"))
{
attachmentCount++;
}
}
if (attachmentCount > 0) {
if (!text.isEmpty())
text = txt_qtn_mms_notification_with_text(attachmentCount, text);
else
text = txt_qtn_mms_notification_attachment(attachmentCount);
}
}
break;
}
case CommHistory::Event::CallEvent:
{
text = txt_qtn_call_missed(1);
break;
}
case CommHistory::Event::VoicemailEvent:
{
// freeText() returns the amount of new / not listened voicemails
// e.g. 3 Voicemails
text = event.freeText();
break;
}
default:
break;
}
return text;
}
static QVariant dbusAction(const QString &name, const QString &displayName, const QString &service, const QString &path, const QString &iface,
const QString &method, const QVariantList &arguments = QVariantList())
{
return Notification::remoteAction(name, displayName, service, path, iface, method, arguments);
}
void NotificationManager::setNotificationProperties(Notification *notification, PersonalNotification *pn, bool grouped)
{
QVariantList remoteActions;
switch (pn->collection()) {
case PersonalNotification::Messaging:
if (pn->eventType() != VOICEMAIL_SMS_EVENT_TYPE && grouped) {
remoteActions.append(dbusAction("default",
txt_qtn_msg_notification_show_messages,
MESSAGING_SERVICE_NAME,
OBJECT_PATH,
MESSAGING_INTERFACE,
SHOW_INBOX_METHOD));
} else {
remoteActions.append(dbusAction("default",
txt_qtn_msg_notification_reply,
MESSAGING_SERVICE_NAME,
OBJECT_PATH,
MESSAGING_INTERFACE,
START_CONVERSATION_METHOD,
QVariantList() << pn->account()
<< pn->targetId()
<< static_cast<uint>(pn->chatType())));
}
remoteActions.append(dbusAction("app",
QString(),
MESSAGING_SERVICE_NAME,
OBJECT_PATH,
MESSAGING_INTERFACE,
SHOW_INBOX_METHOD));
break;
case PersonalNotification::Voice:
remoteActions.append(dbusAction("default",
txt_qtn_call_notification_show_call_history,
CALL_HISTORY_SERVICE_NAME,
CALL_HISTORY_OBJECT_PATH,
CALL_HISTORY_INTERFACE,
CALL_HISTORY_METHOD,
QVariantList() << CALL_HISTORY_PARAMETER));
remoteActions.append(dbusAction("app",
QString(),
CALL_HISTORY_SERVICE_NAME,
CALL_HISTORY_OBJECT_PATH,
CALL_HISTORY_INTERFACE,
CALL_HISTORY_METHOD,
QVariantList() << CALL_HISTORY_PARAMETER));
break;
case PersonalNotification::Voicemail:
remoteActions.append(dbusAction("default",
txt_qtn_voicemail_notification_show_voicemail,
CALL_HISTORY_SERVICE_NAME,
VOICEMAIL_OBJECT_PATH,
VOICEMAIL_INTERFACE,
VOICEMAIL_METHOD));
remoteActions.append(dbusAction("app",
QString(),
CALL_HISTORY_SERVICE_NAME,
VOICEMAIL_OBJECT_PATH,
VOICEMAIL_INTERFACE,
VOICEMAIL_METHOD));
break;
}
notification->setRemoteActions(remoteActions);
}
void NotificationManager::slotContactResolveFinished()
{
DEBUG() << Q_FUNC_INFO;
// All events are now resolved
foreach (PersonalNotification *notification, m_unresolvedNotifications) {
DEBUG() << "Resolved contact for notification" << notification->account() << notification->remoteUid() << notification->contactId();
notification->updateRecipientData();
addNotification(notification);
}
m_unresolvedNotifications.clear();
}
void NotificationManager::slotContactChanged(const RecipientList &recipients)
{
DEBUG() << Q_FUNC_INFO << recipients;
// Check all existing notifications and update if necessary
foreach (NotificationGroup *group, m_Groups) {
foreach (PersonalNotification *notification, group->notifications()) {
if (recipients.contains(notification->recipient())) {
DEBUG() << "Contact changed for notification" << notification->account() << notification->remoteUid() << notification->contactId();
notification->updateRecipientData();
}
}
}
}
void NotificationManager::slotContactInfoChanged(const RecipientList &recipients)
{
DEBUG() << Q_FUNC_INFO << recipients;
// Check all existing notifications and update if necessary
foreach (NotificationGroup *group, m_Groups) {
foreach (PersonalNotification *notification, group->notifications()) {
if (recipients.contains(notification->recipient())) {
DEBUG() << "Contact info changed for notification" << notification->account() << notification->remoteUid() << notification->contactId();
notification->updateRecipientData();
}
}
}
}
void NotificationManager::slotClassZeroError(const QDBusError &error)
{
qWarning() << "Class 0 SMS notification failed:" << error.message();
}
CommHistory::GroupModel* NotificationManager::groupModel()
{
if (!m_GroupModel) {
m_GroupModel = new CommHistory::GroupModel(this);
m_GroupModel->setResolveContacts(GroupManager::DoNotResolve);
connect(m_GroupModel,
SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)),
this,
SLOT(slotGroupRemoved(const QModelIndex&, int, int)));
connect(m_GroupModel,
SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
this,
SLOT(slotGroupDataChanged(const QModelIndex&, const QModelIndex&)));
if (!m_GroupModel->getGroups()) {
qCritical() << "Failed to request group ";
delete m_GroupModel;
m_GroupModel = 0;
}
}
return m_GroupModel;
}
void NotificationManager::slotGroupRemoved(const QModelIndex &index, int start, int end)
{
DEBUG() << Q_FUNC_INFO;
for (int i = start; i <= end; i++) {
QModelIndex row = m_GroupModel->index(i, 0, index);
Group group = m_GroupModel->group(row);
if (group.isValid() && !group.recipients().isEmpty()) {
removeConversationNotifications(group.recipients().value(0), group.chatType());
}
}
}
void NotificationManager::showVoicemailNotification(int count)
{
Q_UNUSED(count)
qWarning() << Q_FUNC_INFO << "Stub";
}
void NotificationManager::slotGroupDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
DEBUG() << Q_FUNC_INFO;
QSet<NotificationGroup> updatedGroups;
// Update MUC notifications if MUC topic has changed
for (int i = topLeft.row(); i <= bottomRight.row(); i++) {
QModelIndex row = m_GroupModel->index(i, 0);
CommHistory::Group group = m_GroupModel->group(row);
if (group.isValid()) {
const Recipient &groupRecipient(group.recipients().value(0));
foreach (NotificationGroup *g, m_Groups) {
if (g->localUid() != groupRecipient.localUid()) {
continue;
}
foreach (PersonalNotification *pn, g->notifications()) {
// If notification is for MUC and matches to changed group...
if (!pn->chatName().isEmpty()) {
const Recipient notificationRecipient(pn->account(), pn->targetId());
if (notificationRecipient.matches(groupRecipient)) {
QString newChatName;
if (group.chatName().isEmpty() && pn->chatName() != txt_qtn_msg_group_chat)
newChatName = txt_qtn_msg_group_chat;
else if (group.chatName() != pn->chatName())
newChatName = group.chatName();
if (!newChatName.isEmpty()) {
DEBUG() << Q_FUNC_INFO << "Changing chat name to" << newChatName;
pn->setChatName(newChatName);
}
}
}
}
}
}
}
}
void NotificationManager::slotNgfEventFinished(quint32 id)
{
if (id == m_ngfEvent)
m_ngfEvent = 0;
}
void NotificationManager::slotVoicemailWaitingChanged()
{
QOfonoMessageWaiting *mw = (QOfonoMessageWaiting*)sender();
const bool waiting(mw->voicemailWaiting());
const int messageCount(mw->voicemailMessageCount());
DEBUG() << Q_FUNC_INFO << waiting << messageCount;
uint currentId = 0;
// See if there is a current notification for voicemail waiting
QList<QObject*> notifications = Notification::notifications();
foreach (QObject *o, notifications) {
Notification *n = static_cast<Notification*>(o);
if (n->category() == voicemailWaitingCategory) {
if (waiting) {
// The notification is already present; do nothing
currentId = n->replacesId();
DEBUG() << "Extant voicemail waiting notification:" << n->replacesId();
} else {
// Close this notification
DEBUG() << "Closing voicemail waiting notification:" << n->replacesId();
n->close();
}
}
}
qDeleteAll(notifications);
notifications.clear();
if (waiting) {
const QString voicemailNumber(mw->voicemailMailboxNumber());
// If ofono reports zero voicemail messages, we don't know the real number; report 1 as a fallback
const int voicemailCount(messageCount > 0 ? messageCount : 1);
// Publish a new voicemail-waiting notification
Notification voicemailNotification;
voicemailNotification.setAppName(NotificationGroup::groupName(PersonalNotification::Voicemail));
voicemailNotification.setCategory(voicemailWaitingCategory);
// If ofono reports zero voicemail messages, we don't know the real number; report 1 as a fallback
voicemailNotification.setPreviewSummary(txt_qtn_call_voicemail_notification(voicemailCount));
voicemailNotification.setPreviewBody(txt_qtn_voicemail_prompt);
voicemailNotification.setSummary(voicemailNotification.previewSummary());
voicemailNotification.setItemCount(voicemailCount);
QString displayName;
QString service;
QString path;
QString iface;
QString method;
QVariantList args;
if (!voicemailNumber.isEmpty()) {
displayName = txt_qtn_voicemail_notification_call;
service = VOICEMAIL_WAITING_SERVICE;
path = VOICEMAIL_WAITING_OBJECT_PATH;
iface = VOICEMAIL_WAITING_INTERFACE;
method = VOICEMAIL_WAITING_METHOD;
args.append(QVariant(QVariantList() << QString(QStringLiteral("tel://")) + voicemailNumber));
} else {
displayName = txt_qtn_call_notification_show_call_history;
service = CALL_HISTORY_SERVICE_NAME;
path = CALL_HISTORY_OBJECT_PATH;
iface = CALL_HISTORY_INTERFACE;
method = CALL_HISTORY_METHOD;
args.append(CALL_HISTORY_PARAMETER);
}
voicemailNotification.setRemoteActions(QVariantList() << dbusAction("default", displayName, service, path, iface, method, args)
<< dbusAction("app", QString(), service, path, iface, method, args));
voicemailNotification.setReplacesId(currentId);
voicemailNotification.publish();
DEBUG() << (currentId ? "Updated" : "Created") << "voicemail waiting notification:" << voicemailNotification.replacesId();
}
}
void NotificationManager::slotModemsChanged(QStringList modems)
{
DEBUG() << "NotificationManager::slotModemsChanged";
qDeleteAll(interfaces.values());
interfaces.clear();
foreach (QString path, modems)
addModem(path);
}
void NotificationManager::slotModemAdded(QString path)
{
DEBUG() << "NotificationManager::slotModemAdded: " << path;
delete interfaces.take(path);
addModem(path);
}
void NotificationManager::slotModemRemoved(QString path)
{
DEBUG() << "NotificationManager::slotModemRemoved: " << path;
delete interfaces.take(path);
}
void NotificationManager::slotValidChanged(bool valid)
{
DEBUG() << "NotificationManager::slotValidChanged to: " << valid;
QOfonoMessageWaiting *mw = (QOfonoMessageWaiting*)sender();
if (mw->isValid()) {
slotVoicemailWaitingChanged();
}
}