~phablet-team/telephony-service/manage-cancel-channel-errors

« back to all changes in this revision

Viewing changes to indicator/textchannelobserver.cpp

  • Committer: Tiago Salem Herrmann
  • Date: 2016-08-11 21:04:39 UTC
  • mfrom: (1199.2.29 08-start_chat)
  • Revision ID: tiago.herrmann@canonical.com-20160811210439-iclps8714lpnwn85
merge parent branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
        QStringList recipients;
65
65
        recipients << notificationData->participantIds;
66
66
        recipients.removeDuplicates();
67
 
        notificationData->observer->sendMessage(recipients, notificationData->messageText, accountId);
 
67
        notificationData->observer->sendMessage(*notificationData);
68
68
    }
69
69
 
70
70
    notify_notification_close(notification, &error);
89
89
                                                                                         true);
90
90
            History::TextEvent textEvent(notificationData->accountId,
91
91
                                         thread.threadId(), 
92
 
                                         notificationData->eventId, 
 
92
                                         QString(QByteArray::fromHex(notificationData->encodedEventId.toUtf8())),
93
93
                                         notificationData->senderId,
94
94
                                         notificationData->timestamp,
95
95
                                         false,
144
144
    QObject(parent)
145
145
{
146
146
    connect(MessagingMenu::instance(),
147
 
            SIGNAL(replyReceived(QStringList,QString,QString)),
148
 
            SLOT(onReplyReceived(QStringList,QString,QString)));
 
147
            SIGNAL(replyReceived(NotificationData)),
 
148
            SLOT(onReplyReceived(NotificationData)));
149
149
    connect(MessagingMenu::instance(),
150
 
            SIGNAL(messageRead(QStringList,QString,QString)),
151
 
            SLOT(onMessageRead(QStringList,QString,QString)));
 
150
            SIGNAL(messageRead(NotificationData)),
 
151
            SLOT(onMessageRead(NotificationData)));
152
152
 
153
153
    if (GreeterContacts::isGreeterMode()) {
154
154
        connect(GreeterContacts::instance(), SIGNAL(contactUpdated(QtContacts::QContact)),
169
169
    mNotifications.clear();
170
170
}
171
171
 
172
 
void TextChannelObserver::sendMessage(const QStringList &recipients, const QString &text, const QString &accountId)
 
172
void TextChannelObserver::sendMessage(NotificationData notificationData)
173
173
{
174
 
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
175
 
    if (!account || accountId.isEmpty()) {
 
174
    AccountEntry *account = TelepathyHelper::instance()->accountForId(notificationData.accountId);
 
175
    if (!account || notificationData.accountId.isEmpty()) {
176
176
        // fallback to the default account
177
177
        if (TelepathyHelper::instance()->defaultMessagingAccount() && TelepathyHelper::instance()->defaultMessagingAccount()->active()) {
178
178
            account = TelepathyHelper::instance()->defaultMessagingAccount();
191
191
        bool phoneNumberBased = account->type() == AccountEntry::PhoneAccount || account->type() == AccountEntry::MultimediaAccount;
192
192
        History::Thread thread = History::Manager::instance()->threadForParticipants(account->accountId(),
193
193
                                                                                     History::EventTypeText,
194
 
                                                                                     recipients,
 
194
                                                                                     notificationData.participantIds,
195
195
                                                                                     phoneNumberBased ? History::MatchPhoneNumber :
196
196
                                                                                                        History::MatchCaseSensitive,
197
197
                                                                                     true);
201
201
                                     "self",
202
202
                                     QDateTime::currentDateTime(),
203
203
                                     false,
204
 
                                     text,
 
204
                                     notificationData.messageReply,
205
205
                                     History::MessageTypeText,
206
206
                                     History::MessageStatusPermanentlyFailed);
207
207
        History::Events events;
231
231
        g_object_unref(icon);
232
232
        g_free(iconPath);
233
233
        NotificationData *data = new NotificationData();
234
 
        data->participantIds = recipients;
235
 
        data->accountId = account->accountId();
236
 
        data->messageText = text;
 
234
        *data = notificationData;
237
235
        data->notificationList = &mNotifications;
238
236
        mNotifications.insert(notification, data);
239
237
 
261
259
    }
262
260
 
263
261
    QVariantMap properties;
264
 
    properties["participantIds"] = recipients;
265
 
    ChatManager::instance()->sendMessage(account->accountId(), text, QVariantMap(), properties);
 
262
    properties["participantIds"] = notificationData.participantIds;
 
263
    properties["chatType"] = notificationData.targetType;
 
264
    if (notificationData.targetId.isEmpty()) {
 
265
        properties["threadId"] = notificationData.targetId;
 
266
    }
 
267
    ChatManager::instance()->sendMessage(account->accountId(), notificationData.messageReply, QVariantMap(), properties);
266
268
}
267
269
 
268
270
void TextChannelObserver::clearNotifications()
273
275
    }
274
276
}
275
277
 
 
278
void TextChannelObserver::showNotificationForNewGroup(const QString &accountId, const Tp::TextChannelPtr channel)
 
279
{
 
280
    // show the notification
 
281
    QString text;
 
282
    NotificationData *data = new NotificationData();
 
283
    data->accountId = accountId;
 
284
    data->timestamp = QDateTime::currentDateTime();
 
285
    data->targetId = channel->targetId();
 
286
    data->targetType = channel->targetHandleType();
 
287
    data->notificationTitle = QString::fromUtf8(C::gettext("New Group"));
 
288
    data->encodedEventId = QByteArray(QDateTime::currentDateTime().toString("yyyy-MM-ddTHH:mm:ss.zzz").toUtf8()).toHex();
 
289
    data->icon = QUrl::fromLocalFile(telephonyServiceDir() + "/assets/contact-group.svg").toString();
 
290
 
 
291
    Tp::Client::ChannelInterfaceRoomInterface *roomInterface = channel->optionalInterface<Tp::Client::ChannelInterfaceRoomInterface>();
 
292
    Tp::Client::ChannelInterfaceRoomConfigInterface *roomConfigInterface = channel->optionalInterface<Tp::Client::ChannelInterfaceRoomConfigInterface>();
 
293
    QVariantMap roomInterfaceProps = getInterfaceProperties(roomInterface);
 
294
    QVariantMap roomConfigInterfaceProps = getInterfaceProperties(roomConfigInterface);
 
295
    if (roomConfigInterfaceProps.contains("Title") && !roomConfigInterfaceProps["Title"].toString().isEmpty()) {
 
296
        // TRANSLATORS : %1 is the group name
 
297
        data->messageText = QString::fromUtf8(C::gettext("You joined group %1 ")).arg(roomConfigInterfaceProps["Title"].toString());
 
298
        data->roomName = roomConfigInterfaceProps["Title"].toString();
 
299
    } else if (roomInterfaceProps.contains("RoomName") && !roomInterfaceProps["RoomName"].toString().isEmpty()) {
 
300
        // TRANSLATORS : %1 is the group name
 
301
        data->messageText = QString::fromUtf8(C::gettext("You joined group %1")).arg(roomInterfaceProps["RoomName"].toString());
 
302
        data->roomName = roomInterfaceProps["RoomName"].toString();
 
303
    } else {
 
304
        data->messageText = QString::fromUtf8(C::gettext("You joined a new group"));
 
305
    }
 
306
 
 
307
    if (roomInterfaceProps["CreationTimestamp"].toDateTime().isValid()) {
 
308
        data->timestamp = roomInterfaceProps["CreationTimestamp"].toDateTime();
 
309
    }
 
310
 
 
311
    NotificationData messagingMenuData = *data;
 
312
 
 
313
    MessagingMenu::instance()->addNotification(messagingMenuData);
 
314
 
 
315
    NotifyNotification *notification = notify_notification_new(data->notificationTitle.toStdString().c_str(),
 
316
                                                               data->messageText.toStdString().c_str(),
 
317
                                                               data->icon.toStdString().c_str());
 
318
 
 
319
    mNotifications.insert(notification, data);
 
320
 
 
321
    notify_notification_add_action (notification,
 
322
                                    "notification_action",
 
323
                                    C::gettext("View Group"),
 
324
                                    notification_action,
 
325
                                    data,
 
326
                                    NULL);
 
327
    g_signal_connect(notification, "closed", G_CALLBACK(notification_closed), &mNotifications);
 
328
 
 
329
    GError *error = NULL;
 
330
    if (!notify_notification_show(notification, &error)) {
 
331
        qWarning() << "Failed to show message notification:" << error->message;
 
332
        g_error_free (error);
 
333
    }
 
334
 
 
335
    Ringtone::instance()->playIncomingMessageSound();
 
336
}
 
337
 
276
338
void TextChannelObserver::showNotificationForFlashMessage(const Tp::ReceivedMessage &message, const QString &accountId)
277
339
{
278
340
    Tp::ContactPtr contact = message.sender();
287
349
    data->accountId = accountId;
288
350
    data->timestamp = message.received();
289
351
    data->messageText = message.text();
290
 
    data->eventId = message.messageToken().toUtf8();
 
352
    data->encodedEventId = token.toHex();
291
353
 
292
354
    NotificationData messagingMenuData = *data;
293
 
    messagingMenuData.eventId = token.toHex();
294
355
 
295
356
    MessagingMenu::instance()->addFlashMessage(messagingMenuData);
296
357
 
471
532
    messagingMenuData.alias = telepathyContact->alias();
472
533
    messagingMenuData.participantIds = participantIds;
473
534
    messagingMenuData.accountId = accountId;
474
 
    messagingMenuData.eventId = token.toHex();
 
535
    messagingMenuData.encodedEventId = token.toHex();
475
536
    messagingMenuData.timestamp = message.received();
476
537
    messagingMenuData.messageText = messageText;
477
538
    messagingMenuData.targetId = channel->targetId();
524
585
    data->participantIds = participantIds;
525
586
    data->alias = alias;
526
587
    data->messageText = messageText;
 
588
    data->targetId = channel->targetId();
 
589
    data->targetType = channel->targetHandleType();
527
590
    data->notificationList = &mNotifications;
528
 
    data->targetId = channel->targetId();
529
 
    data->targetType = channel->targetHandleType();
530
591
    mNotifications.insert(notification, data);
531
592
 
532
593
    // add the callback action
606
667
            SIGNAL(messageSent(Tp::Message,Tp::MessageSendingFlags,QString)),
607
668
            SLOT(onMessageSent(Tp::Message,Tp::MessageSendingFlags,QString)));
608
669
 
 
670
    AccountEntry *account = TelepathyHelper::instance()->accountForConnection(textChannel->connection());
 
671
    if (!account) {
 
672
        return;
 
673
    }
 
674
 
 
675
    if (textChannel->targetHandleType() == Tp::HandleTypeRoom && !textChannel->isRequested()) {
 
676
        QVariantMap properties;
 
677
        properties["accountId"] = account->accountId();
 
678
        properties["threadId"] = textChannel->targetId();
 
679
        properties["chatType"] = Tp::HandleTypeRoom;
 
680
        History::Thread thread = History::Manager::instance()->threadForProperties(account->accountId(),
 
681
                                                                                   History::EventTypeText,
 
682
                                                                                   properties);
 
683
        if (thread.isNull()) {
 
684
            // notify that we got added to a new group
 
685
            showNotificationForNewGroup(account->accountId(), textChannel);
 
686
        }
 
687
    }
609
688
 
610
689
    if (textChannel->immutableProperties().contains(TP_QT_IFACE_CHANNEL_INTERFACE_SMS + QLatin1String(".Flash")) && 
611
690
           textChannel->immutableProperties()[TP_QT_IFACE_CHANNEL_INTERFACE_SMS + QLatin1String(".Flash")].toBool()) {
694
773
    MessagingMenu::instance()->removeMessage(token.toHex());
695
774
}
696
775
 
697
 
void TextChannelObserver::onReplyReceived(const QStringList &recipients, const QString &accountId, const QString &reply)
 
776
void TextChannelObserver::onReplyReceived(NotificationData notificationData)
698
777
{
699
 
    AccountEntry *account = TelepathyHelper::instance()->accountForId(accountId);
 
778
    AccountEntry *account = TelepathyHelper::instance()->accountForId(notificationData.accountId);
700
779
    if (!account) {
701
780
        return;
702
781
    }
703
782
 
704
783
    // FIXME - we need to find a better way to deal with dual sim in the messaging-menu
705
 
    if (!TelepathyHelper::instance()->defaultMessagingAccount()
706
 
            && TelepathyHelper::instance()->activeAccounts().size() > 1
707
 
            && account->type() != AccountEntry::GenericAccount) {
 
784
    if (account->type() == AccountEntry::PhoneAccount
 
785
            && !TelepathyHelper::instance()->defaultMessagingAccount()
 
786
            && TelepathyHelper::instance()->activeAccounts().size() > 1) {
708
787
        NotifyNotification *notification = notify_notification_new(C::gettext("Please, select a SIM card:"),
709
 
                                                                   reply.toStdString().c_str(),
 
788
                                                                   notificationData.messageReply.toStdString().c_str(),
710
789
                                                                   "");
711
790
        NotificationData *data = new NotificationData();
712
 
        data->participantIds = recipients;
713
 
        data->accountId = accountId;
714
 
        data->messageText = reply;
 
791
        *data = notificationData;
715
792
        data->observer = this;
716
793
        mNotifications.insert(notification, data);
717
794
 
737
814
        return;
738
815
    }
739
816
 
740
 
    if (account->type() == AccountEntry::GenericAccount) {
741
 
        sendMessage(recipients, reply, accountId);
742
 
    } else {
743
 
        // FIXME: for now we dont specify any accountId
744
 
        sendMessage(recipients, reply, "");
745
 
    }
 
817
    sendMessage(notificationData);
746
818
}
747
819
 
748
 
void TextChannelObserver::onMessageRead(const QStringList &recipients, const QString &accountId, const QString &encodedMessageId)
 
820
void TextChannelObserver::onMessageRead(NotificationData notificationData)
749
821
{
750
 
    QString messageId(QByteArray::fromHex(encodedMessageId.toUtf8()));
 
822
    QString messageId(QByteArray::fromHex(notificationData.encodedEventId.toUtf8()));
751
823
    QVariantMap properties;
752
 
    properties["accountId"] = accountId;
753
 
    properties["participantIds"] = recipients;
754
 
    properties["messageId"] = encodedMessageId;
 
824
    properties["accountId"] = notificationData.accountId;
 
825
    properties["participantIds"] = notificationData.participantIds;
 
826
    properties["messageId"] = notificationData.encodedEventId;
755
827
    ChatManager::instance()->acknowledgeMessage(properties);
756
828
}
757
829