27
27
#include <TelepathyQt/ContactManager>
28
28
#include <TelepathyQt/PendingContacts>
30
template<> bool qMapLessThanKey<QStringList>(const QStringList &key1, const QStringList &key2)
32
return key1.size() > key2.size(); // sort by operator> !
30
35
TextHandler::TextHandler(QObject *parent)
57
void TextHandler::startChat(const QString &phoneNumber)
62
void TextHandler::startChat(const QStringList &phoneNumbers)
59
64
// Request the contact to start chatting to
60
65
Tp::AccountPtr account = TelepathyHelper::instance()->account();
61
connect(account->connection()->contactManager()->contactsForIdentifiers(QStringList() << phoneNumber),
66
connect(account->connection()->contactManager()->contactsForIdentifiers(phoneNumbers),
62
67
SIGNAL(finished(Tp::PendingOperation*)),
63
68
SLOT(onContactsAvailable(Tp::PendingOperation*)));
66
void TextHandler::sendMessage(const QString &phoneNumber, const QString &message)
71
void TextHandler::startChat(const Tp::Contacts &contacts)
73
Tp::AccountPtr account = TelepathyHelper::instance()->account();
75
if (contacts.size() == 1) {
76
account->ensureTextChat(contacts.values()[0], QDateTime::currentDateTime(), TP_QT_IFACE_CLIENT + ".TelephonyServiceHandler");
78
account->createConferenceTextChat(QList<Tp::ChannelPtr>(), contacts.toList(), QDateTime::currentDateTime(), TP_QT_IFACE_CLIENT + ".TelephonyServiceHandler");
80
// start chatting to the contacts
81
Q_FOREACH(Tp::ContactPtr contact, contacts) {
82
// hold the ContactPtr to make sure its refcounting stays bigger than 0
83
mContacts[contact->id()] = contact;
87
void TextHandler::sendMessage(const QStringList &phoneNumbers, const QString &message)
68
89
if (!TelepathyHelper::instance()->connected()) {
69
mPendingMessages[phoneNumber].append(message);
90
mPendingMessages[phoneNumbers].append(message);
73
Tp::TextChannelPtr channel = existingChat(phoneNumber);
94
Tp::TextChannelPtr channel = existingChat(phoneNumbers);
74
95
if (channel.isNull()) {
75
mPendingMessages[phoneNumber].append(message);
76
startChat(phoneNumber);
96
mPendingMessages[phoneNumbers].append(message);
97
startChat(phoneNumbers);
82
103
SLOT(onMessageSent(Tp::PendingOperation*)));
85
void TextHandler::acknowledgeMessages(const QString &phoneNumber, const QStringList &messageIds)
106
void TextHandler::acknowledgeMessages(const QStringList &phoneNumbers, const QStringList &messageIds)
87
Tp::TextChannelPtr channel = existingChat(phoneNumber);
108
Tp::TextChannelPtr channel = existingChat(phoneNumbers);
88
109
if (channel.isNull()) {
102
123
void TextHandler::onTextChannelAvailable(Tp::TextChannelPtr channel)
104
QString id = channel->targetContact()->id();
105
mChannels[id] = channel;
125
mChannels.append(channel);
107
127
// check for pending messages for this channel
108
QMap<QString, QStringList>::iterator it = mPendingMessages.begin();
109
while (it != mPendingMessages.end()) {
110
if (PhoneUtils::comparePhoneNumbers(it.key(), id)) {
111
Q_FOREACH(const QString &message, it.value()) {
112
connect(channel->send(message),
113
SIGNAL(finished(Tp::PendingOperation*)),
114
SLOT(onMessageSent(Tp::PendingOperation*)));
128
Q_FOREACH(const Tp::TextChannelPtr &channel, mChannels) {
129
QMap<QStringList, QStringList>::iterator it = mPendingMessages.begin();
130
while (it != mPendingMessages.end()) {
131
if (existingChat(it.key()) == channel) {
132
Q_FOREACH(const QString &message, it.value()) {
133
connect(channel->send(message),
134
SIGNAL(finished(Tp::PendingOperation*)),
135
SLOT(onMessageSent(Tp::PendingOperation*)));
137
it = mPendingMessages.erase(it);
116
it = mPendingMessages.erase(it);
137
Tp::TextChannelPtr TextHandler::existingChat(const QString &phoneNumber)
159
Tp::TextChannelPtr TextHandler::existingChat(const QStringList &phoneNumbers)
139
161
Tp::TextChannelPtr channel;
140
QMap<QString, Tp::TextChannelPtr>::iterator it = mChannels.begin();
141
while (it != mChannels.end()) {
142
if (PhoneUtils::comparePhoneNumbers(it.key(), phoneNumber)) {
143
channel = it.value();
163
Q_FOREACH(const Tp::TextChannelPtr &channel, mChannels) {
165
if (channel->groupContacts(false).size() != phoneNumbers.size()) {
168
Q_FOREACH(const QString &phoneNumberNew, phoneNumbers) {
169
Q_FOREACH(const Tp::ContactPtr &phoneNumberOld, channel->groupContacts(false)) {
170
if (PhoneUtils::comparePhoneNumbers(phoneNumberOld->id(), phoneNumberNew)) {
175
if (count == phoneNumbers.size()) {
157
187
qCritical() << "The pending object is not a Tp::PendingContacts";
161
Tp::AccountPtr account = TelepathyHelper::instance()->account();
163
// start chatting to the contacts
164
Q_FOREACH(Tp::ContactPtr contact, pc->contacts()) {
165
account->ensureTextChat(contact, QDateTime::currentDateTime(), TP_QT_IFACE_CLIENT + ".TelephonyServiceHandler");
167
// hold the ContactPtr to make sure its refcounting stays bigger than 0
168
mContacts[contact->id()] = contact;
190
startChat(pc->contacts().toSet());