/
contactauthorizationlistener.cpp
291 lines (254 loc) · 12.1 KB
/
contactauthorizationlistener.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
/******************************************************************************
**
** This file is part of commhistory-daemon.
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Reto Zingg <reto.zingg@nokia.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
**
******************************************************************************/
#include "contactauthorizationlistener.h"
#include "connectionutils.h"
#include "locstrings.h"
#include "constants.h"
#include "debug.h"
// Tp
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/AccountSet>
// Nemomobile
#include <notification.h>
using namespace RTComLogger;
ContactAuthorizationListener::ContactAuthorizationListener(ConnectionUtils *connectionUtils,
QObject *parent) :
QObject(parent),
m_pConnectionUtils(connectionUtils)
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
connect(m_pConnectionUtils,
SIGNAL(connectionReady(Tp::ConnectionPtr)),
SLOT(slotConnectionReady(Tp::ConnectionPtr)));
/*
* You must be wondering why we're connecting to showAuthorizationDialog from here too. As you
* can see, we don't have a ContactAuthorizer for a given account before the connection becomes
* ready. And of course the ContactAuthorizer is the one that takes care of reacting when the
* notification system tells us that the user wants to authorize/block/ignore a certain
* contact. Problem is, we cannot perform that operation if the account is not up. So if that's
* the case, we need to display an informaton banner that says "Sorry, you cannot do this now.
* Go online first!"
* We can't do that from the ContactAuthorizer because it doesn't exist yet, as the account is
* not ready! So we need to do it here. The slot slotShowUnableToAuthorizeDialog will check if
* the account is online, and if is, then just do nothing. If the account is offline, it will
* display the banner.
*/
connect(parent, SIGNAL(showAuthorizationDialog(QString, QString, QString,
QString, QString, QString)),
this, SLOT(slotShowUnableToAuthorizeDialog(QString, QString, QString,
QString, QString, QString)));
}
ContactAuthorizationListener::~ContactAuthorizationListener()
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
}
void ContactAuthorizationListener::slotConnectionReady(const Tp::ConnectionPtr& connection)
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
if (!connection)
return;
Tp::AccountPtr account;
Tp::AccountManagerPtr accountManager = m_pConnectionUtils->accountManager();
if (accountManager && accountManager->isReady()) {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Connection object path: " << connection->objectPath();
#if 1
foreach (Tp::AccountPtr a, accountManager->validAccounts()->accounts()) {
if (a->connection() && a->connection()->objectPath() == connection->objectPath()) {
account = a;
break;
}
}
#else // TODO: enable this back when tp-qt declares metatype for Tp::ConnectionPtr
// and type Tp::Account::connection property
QVariantMap filter;
filter.insert(QLatin1String("connection"), QVariant::fromValue(connection));
Tp::AccountSetPtr accountSet =
accountManager->filterAccounts(filter);
if (!accountSet->accounts().isEmpty()) {
account = accountSet->accounts().first();
}
#endif
}
if (account) {
// Create ContactAuthorizer only for IM accounts:
if (connection->actualFeatures().contains(Tp::Connection::FeatureSimplePresence))
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Creating ContactAuthorizer for " << account->uniqueIdentifier();
/* After connection is ready we need to listen account's connection status changes so that we can
instantiate ContactAuthorizer again when account goes from offline (ContactAuthorizer destroyed then)
state to online state: */
connect(account.data(),
SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)),
this,
SLOT(slotAccountConnectionStatusChanged(Tp::ConnectionStatus)),
Qt::UniqueConnection);
// body authorization notifications should be removed when account is disabled...
connect(account.data(),
SIGNAL(stateChanged(bool)),
this,
SLOT(slotAccountStateChanged(bool)),
Qt::UniqueConnection);
// ...or removed completely
connect(account.data(),
SIGNAL(removed()),
this,
SLOT(slotAccountRemoved()),
Qt::UniqueConnection);
ContactAuthorizer *authorizer = new ContactAuthorizer(connection, account, this);
connect(authorizer, SIGNAL(destroyed(QObject*)), this, SLOT(slotRemoveAuthorizer(QObject*)));
m_authorizers.insert(account->uniqueIdentifier(), authorizer);
if (parent()) {
connect(parent(), SIGNAL(showAuthorizationDialog(QString, QString, QString,
QString, QString, QString)),
authorizer, SLOT(slotShowAuthorizationDialog(QString, QString, QString,
QString, QString, QString)));
}
// Keep a reference to the AccountPtr so it won't be destroyed
m_accounts.insert(account->objectPath(), account);
}
}
}
void ContactAuthorizationListener::slotRemoveAuthorizer(QObject *removedAuthorizer)
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
if (removedAuthorizer) {
QMutableMapIterator<QString,ContactAuthorizer*> i(m_authorizers);
while (i.hasNext()) {
i.next();
if (i.value() == removedAuthorizer) {
i.remove();
}
}
}
}
void ContactAuthorizationListener::slotAccountConnectionStatusChanged(Tp::ConnectionStatus connectionStatus)
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
Tp::Account *account = qobject_cast<Tp::Account *>(sender());
if (account != 0 && account->isValid()) {
if (connectionStatus == Tp::ConnectionStatusConnected) {
if (!m_authorizers.contains(account->uniqueIdentifier())) {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Connection status changed to Connected";
Tp::ConnectionPtr connection = account->connection();
if (!connection.isNull() && connection->isValid()) {
if (connection->isReady()) {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Connection is ready";
slotConnectionReady(connection);
} else {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Asking connection to become ready";
connect(connection->becomeReady(Tp::Connection::FeatureSimplePresence),
SIGNAL(finished(Tp::PendingOperation*)),
m_pConnectionUtils,
SLOT(slotConnectionReady(Tp::PendingOperation*)));
}
}
} else {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "ContactAuthorizer for account " << account->uniqueIdentifier() << " already exists.";
}
} else if (connectionStatus == Tp::ConnectionStatusDisconnected) {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Connection status changed to disconnected";
} else if (connectionStatus == Tp::ConnectionStatusConnecting) {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Connection status changed to connecting...";
} else {
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << "Unknown connection status!";
}
}
}
void ContactAuthorizationListener::slotShowUnableToAuthorizeDialog(const QString& unused1,
const QString& unused2,
const QString& unused3,
const QString& unused4,
const QString& unused5,
const QString& accountUniqueIdentifier)
{
Q_UNUSED(unused1)
Q_UNUSED(unused2)
Q_UNUSED(unused3)
Q_UNUSED(unused4)
Q_UNUSED(unused5)
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
if (m_pConnectionUtils == NULL) {
qWarning() << "ConnectionUtils is NULL.";
return;
}
bool showNotification = false;
QString accountPath;
Tp::AccountManagerPtr accountManager = m_pConnectionUtils->accountManager();
if (accountManager && accountManager->isReady()) {
QVariantMap filter;
filter.insert(QLatin1String("uniqueIdentifier"), accountUniqueIdentifier);
Tp::AccountSetPtr accountSet = accountManager->filterAccounts(filter);
qCDebug(lcCommhistoryd) << "Finding out if account is online: " << accountUniqueIdentifier;
if (!accountSet->accounts().isEmpty()) {
Tp::AccountPtr account = accountSet->accounts().first();
if (account.isNull()) {
qWarning() << "Account is null or invalid.";
return;
}
if (account->connectionStatus() == Tp::ConnectionStatusConnected) {
qCDebug(lcCommhistoryd) << "Nothing to do here: the account is online.";
} else {
qCDebug(lcCommhistoryd) << "The account is offline: cannot authorize anyway.";
showNotification = true;
accountPath = account->objectPath();
}
}
} else {
qCDebug(lcCommhistoryd) << "Nothing to do here: the account manager is not even ready!";
showNotification = true;
}
if (showNotification) {
// TODO: No category for this notification?
Notification notification;
notification.setAppName(txt_qtn_msg_notifications_group);
notification.setAppIcon("icon-lock-chat");
notification.setBody(txt_qtn_pers_offline);
if (!accountPath.isEmpty()) {
notification.setHintValue(ACCOUNT_PATH_HINT, accountPath);
}
notification.publish();
}
}
void ContactAuthorizationListener::slotAccountStateChanged(bool isEnabled)
{
qCDebug(lcCommhistoryd) << Q_FUNC_INFO << isEnabled;
// remove buddy authorization notifications when account is disabled
// sure we will lose them and not be able to see them when the account is
// re-enabled, but we can live with that...
if (!isEnabled)
slotAccountRemoved();
}
void ContactAuthorizationListener::slotAccountRemoved() {
// We need to remove the ongoing notifications for this account
qCDebug(lcCommhistoryd) << Q_FUNC_INFO;
Tp::Account *account = qobject_cast<Tp::Account*>(sender());
foreach (QObject *obj, Notification::notifications()) {
if (Notification *n = qobject_cast<Notification *>(obj)) {
if (n->hintValue(ACCOUNT_PATH_HINT) == account->objectPath()) {
n->close();
}
}
delete obj;
}
m_accounts.remove(account->objectPath());
}