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
|
/** This file is part of Contacts daemon
**
** Copyright (c) 2010-2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public License
** version 2.1 as published by the Free Software Foundation and appearing in the
** file LICENSE.LGPL included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License version
** 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional rights.
** These rights are described in the Nokia Qt LGPL Exception version 1.1, included
** in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**/
#include <TelepathyQt/Account>
#include <TelepathyQt/AccountPropertyFilter>
#include <TelepathyQt/AndFilter>
#include <TelepathyQt/NotFilter>
#include <TelepathyQt/AccountSet>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/ContactManager>
#include <TelepathyQt/PendingContacts>
#include "buddymanagementadaptor.h"
#include "cdtpcontroller.h"
#include "debug.h"
const QLatin1String DBusObjectPath("/telepathy");
static const QString offlineRemovals = QString::fromLatin1("OfflineRemovals");
static const QString offlineInvitations = QString::fromLatin1("OfflineInvitations");
CDTpController::CDTpController(QObject *parent)
: QObject(parent)
, mOfflineRosterBuffer(QSettings::IniFormat, QSettings::UserScope, QLatin1String("Nokia"), QLatin1String("Contactsd"))
{
connect(&mStorage,
SIGNAL(error(int, const QString &)),
SIGNAL(error(int, const QString &)));
qCDebug(lcContactsd) << "Creating account manager";
const QDBusConnection &bus = QDBusConnection::sessionBus();
Tp::AccountFactoryPtr accountFactory = Tp::AccountFactory::create(bus,
Tp::Features() << Tp::Account::FeatureCore
<< Tp::Account::FeatureAvatar
<< Tp::Account::FeatureCapabilities);
Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(bus,
Tp::Features() << Tp::Connection::FeatureConnected
<< Tp::Connection::FeatureCore
<< Tp::Connection::FeatureRoster);
Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(bus);
Tp::ContactFactoryPtr contactFactory = Tp::ContactFactory::create(
Tp::Features() << Tp::Contact::FeatureAlias
<< Tp::Contact::FeatureAvatarToken
<< Tp::Contact::FeatureAvatarData
<< Tp::Contact::FeatureSimplePresence
<< Tp::Contact::FeatureInfo
<< Tp::Contact::FeatureLocation
<< Tp::Contact::FeatureCapabilities);
mAM = Tp::AccountManager::create(bus, accountFactory, connectionFactory,
channelFactory, contactFactory);
// Wait for AM to become ready
connect(mAM->becomeReady(Tp::AccountManager::FeatureCore),
SIGNAL(finished(Tp::PendingOperation*)),
SLOT(onAccountManagerReady(Tp::PendingOperation*)));
if (registerDBusObject()) {
(void) new BuddyManagementAdaptor(this);
}
}
CDTpController::~CDTpController()
{
QDBusConnection::sessionBus().unregisterObject(DBusObjectPath);
}
void CDTpController::onAccountManagerReady(Tp::PendingOperation *op)
{
if (op->isError()) {
qCDebug(lcContactsd) << "Could not make account manager ready:" <<
op->errorName() << "-" << op->errorMessage();
return;
}
qCDebug(lcContactsd) << "Account manager ready";
Tp::AccountPropertyFilterPtr propFilter;
Tp::AccountFilterPtr notFilter;
QList<Tp::AccountFilterConstPtr> filters;
propFilter = Tp::AccountPropertyFilter::create();
propFilter->addProperty(QString::fromLatin1("valid"), true);
filters << propFilter;
propFilter = Tp::AccountPropertyFilter::create();
propFilter->addProperty(QString::fromLatin1("normalizedName"), QString());
notFilter = Tp::NotFilter<Tp::Account>::create(propFilter);
filters << notFilter;
propFilter = Tp::AccountPropertyFilter::create();
propFilter->addProperty(QString::fromLatin1("cmName"), QLatin1String("ring"));
notFilter = Tp::NotFilter<Tp::Account>::create(propFilter);
filters << notFilter;
propFilter = Tp::AccountPropertyFilter::create();
propFilter->addProperty(QString::fromLatin1("cmName"), QLatin1String("mmscm"));
notFilter = Tp::NotFilter<Tp::Account>::create(propFilter);
filters << notFilter;
Tp::AccountFilterPtr filter = Tp::AndFilter<Tp::Account>::create(filters);
mAccountSet = mAM->filterAccounts(filter);
connect(mAccountSet.data(),
SIGNAL(accountAdded(const Tp::AccountPtr &)),
SLOT(onAccountAdded(const Tp::AccountPtr &)));
connect(mAccountSet.data(),
SIGNAL(accountRemoved(const Tp::AccountPtr &)),
SLOT(onAccountRemoved(const Tp::AccountPtr &)));
Q_FOREACH (const Tp::AccountPtr &account, mAccountSet->accounts()) {
insertAccount(account, false);
}
mStorage.reportPresenceStates();
mStorage.syncAccounts(mAccounts.values());
}
void CDTpController::onAccountAdded(const Tp::AccountPtr &account)
{
if (mAccounts.contains(account->objectPath())) {
qCWarning(lcContactsd) << "Internal error, account was already in controller";
return;
}
CDTpAccountPtr accountWrapper = insertAccount(account, true);
mStorage.createAccount(accountWrapper);
}
void CDTpController::onAccountRemoved(const Tp::AccountPtr &account)
{
CDTpAccountPtr accountWrapper(mAccounts.take(account->objectPath()));
if (not accountWrapper) {
qCWarning(lcContactsd) << "Internal error, account was not in controller";
return;
}
mStorage.removeAccount(accountWrapper);
// Drop pending offline operations
QString accountPath = accountWrapper->account()->objectPath();
mOfflineRosterBuffer.beginGroup(offlineRemovals);
mOfflineRosterBuffer.remove(accountPath);
mOfflineRosterBuffer.endGroup();
mOfflineRosterBuffer.beginGroup(offlineInvitations);
mOfflineRosterBuffer.remove(accountPath);
mOfflineRosterBuffer.endGroup();
mOfflineRosterBuffer.sync();
}
CDTpAccountPtr CDTpController::insertAccount(const Tp::AccountPtr &account, bool newAccount)
{
qCDebug(lcContactsd) << "Creating wrapper for account" << account->objectPath();
// Get the list of contact ids waiting to be removed from server
mOfflineRosterBuffer.beginGroup(offlineRemovals);
QStringList idsToRemove = mOfflineRosterBuffer.value(account->objectPath()).toStringList();
mOfflineRosterBuffer.endGroup();
CDTpAccountPtr accountWrapper = CDTpAccountPtr(new CDTpAccount(account, idsToRemove, newAccount, this));
mAccounts.insert(account->objectPath(), accountWrapper);
maybeStartOfflineOperations(accountWrapper);
// Connect change notifications
connect(accountWrapper.data(),
SIGNAL(rosterChanged(CDTpAccountPtr)),
SLOT(onRosterChanged(CDTpAccountPtr)));
connect(accountWrapper.data(),
SIGNAL(changed(CDTpAccountPtr, CDTpAccount::Changes)),
&mStorage,
SLOT(updateAccount(CDTpAccountPtr, CDTpAccount::Changes)));
connect(accountWrapper.data(),
SIGNAL(rosterUpdated(CDTpAccountPtr,
const QList<CDTpContactPtr> &,
const QList<CDTpContactPtr> &)),
&mStorage,
SLOT(syncAccountContacts(CDTpAccountPtr,
const QList<CDTpContactPtr> &,
const QList<CDTpContactPtr> &)));
connect(accountWrapper.data(),
SIGNAL(rosterContactChanged(CDTpContactPtr, CDTpContact::Changes)),
&mStorage,
SLOT(updateContact(CDTpContactPtr, CDTpContact::Changes)));
connect(accountWrapper.data(),
SIGNAL(syncStarted(Tp::AccountPtr)),
SLOT(onSyncStarted(Tp::AccountPtr)));
connect(accountWrapper.data(),
SIGNAL(syncEnded(Tp::AccountPtr, int, int)),
SLOT(onSyncEnded(Tp::AccountPtr, int, int)));
return accountWrapper;
}
void CDTpController::onSyncStarted(Tp::AccountPtr account)
{
Q_EMIT importStarted(account->serviceName(), account->objectPath());
}
void CDTpController::onSyncEnded(Tp::AccountPtr account, int contactsAdded, int contactsRemoved)
{
Q_EMIT importEnded(account->serviceName(), account->objectPath(),
contactsAdded, contactsRemoved, 0);
}
void CDTpController::onRosterChanged(CDTpAccountPtr accountWrapper)
{
mStorage.syncAccountContacts(accountWrapper);
maybeStartOfflineOperations(accountWrapper);
}
void CDTpController::maybeStartOfflineOperations(CDTpAccountPtr accountWrapper)
{
if (!accountWrapper->hasRoster()) {
return;
}
Tp::AccountPtr account = accountWrapper->account();
// Start removal operation
mOfflineRosterBuffer.beginGroup(offlineRemovals);
QStringList idsToRemove = mOfflineRosterBuffer.value(account->objectPath()).toStringList();
mOfflineRosterBuffer.endGroup();
if (!idsToRemove.isEmpty()) {
CDTpRemovalOperation *op = new CDTpRemovalOperation(accountWrapper, idsToRemove);
connect(op,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onRemovalFinished(Tp::PendingOperation *)));
}
// Start invitation operation
mOfflineRosterBuffer.beginGroup(offlineInvitations);
QStringList idsToInvite = mOfflineRosterBuffer.value(account->objectPath()).toStringList();
mOfflineRosterBuffer.endGroup();
if (!idsToInvite.isEmpty()) {
// FIXME: We should also save the localId for offline operations
CDTpInvitationOperation *op = new CDTpInvitationOperation(mStorage, accountWrapper, idsToInvite, 0);
connect(op,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onInvitationFinished(Tp::PendingOperation *)));
}
}
void CDTpController::inviteBuddies(const QString &accountPath, const QStringList &imIds)
{
inviteBuddiesOnContact(accountPath, imIds, 0);
}
void CDTpController::inviteBuddiesOnContact(const QString &accountPath, const QStringList &imIds, uint localId)
{
qCDebug(lcContactsd) << "InviteBuddies:" << accountPath << imIds.join(QLatin1String(", "));
// Add ids to offlineInvitations, in case operation does not succeed now
updateOfflineRosterBuffer(offlineInvitations, accountPath, imIds, QStringList());
CDTpAccountPtr accountWrapper = mAccounts[accountPath];
if (!accountWrapper) {
qCDebug(lcContactsd) << "Account not found";
return;
}
// Start invitation operation
if (accountWrapper->hasRoster()) {
CDTpInvitationOperation *op = new CDTpInvitationOperation(mStorage, accountWrapper, imIds, localId);
connect(op,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onInvitationFinished(Tp::PendingOperation *)));
}
}
void CDTpController::onInvitationFinished(Tp::PendingOperation *op)
{
// If an error happend, ids stay in the OfflineRosterBuffer and operation
// will be retried next time account connects.
if (op->isError()) {
qCDebug(lcContactsd) << "Error" << op->errorName() << ":" << op->errorMessage();
return;
}
CDTpInvitationOperation *iop = qobject_cast<CDTpInvitationOperation *>(op);
qCDebug(lcContactsd) << "Contacts invited:" << iop->contactIds().join(QLatin1String(", "));
CDTpAccountPtr accountWrapper = iop->accountWrapper();
const QString accountPath = accountWrapper->account()->objectPath();
updateOfflineRosterBuffer(offlineInvitations, accountPath, QStringList(), iop->contactIds());
}
void CDTpController::removeBuddies(const QString &accountPath, const QStringList &imIds)
{
qCDebug(lcContactsd) << "RemoveBuddies:" << accountPath << imIds.join(QLatin1String(", "));
// Add ids to offlineRemovals, in case it does not get removed right now from server
QStringList currentList = updateOfflineRosterBuffer(offlineRemovals, accountPath, imIds, QStringList());
CDTpAccountPtr accountWrapper = mAccounts[accountPath];
if (!accountWrapper) {
qCDebug(lcContactsd) << "Account not found";
return;
}
// Remove ids from storage
mStorage.removeAccountContacts(accountWrapper, imIds);
// Add contact to account's avoid list
accountWrapper->setContactsToAvoid(currentList);
// Start removal operation
if (accountWrapper->hasRoster()) {
CDTpRemovalOperation *op = new CDTpRemovalOperation(accountWrapper, imIds);
connect(op,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onRemovalFinished(Tp::PendingOperation *)));
}
}
void CDTpController::onRemovalFinished(Tp::PendingOperation *op)
{
// If an error happend, ids stay in the OfflineRosterBuffer and operation
// will be retried next time account connects.
if (op->isError()) {
qCDebug(lcContactsd) << "Error" << op->errorName() << ":" << op->errorMessage();
return;
}
CDTpRemovalOperation *rop = qobject_cast<CDTpRemovalOperation *>(op);
qCDebug(lcContactsd) << "Contacts removed from server:" << rop->contactIds().join(QLatin1String(", "));
CDTpAccountPtr accountWrapper = rop->accountWrapper();
const QString accountPath = accountWrapper->account()->objectPath();
QStringList currentList = updateOfflineRosterBuffer(offlineRemovals, accountPath, QStringList(), rop->contactIds());
// Update account's avoid list, in case they get added back
accountWrapper->setContactsToAvoid(currentList);
}
QStringList CDTpController::updateOfflineRosterBuffer(const QString group, const QString accountPath,
const QStringList idsToAdd, const QStringList idsToRemove)
{
mOfflineRosterBuffer.beginGroup(group);
QStringList currentList = mOfflineRosterBuffer.value(accountPath).toStringList();
Q_FOREACH (const QString &id, idsToAdd) {
if (!currentList.contains(id)) {
currentList << id;
}
}
Q_FOREACH (const QString id, idsToRemove) {
currentList.removeOne(id);
}
if (currentList.isEmpty()) {
mOfflineRosterBuffer.remove(accountPath);
} else {
mOfflineRosterBuffer.setValue(accountPath, currentList);
}
mOfflineRosterBuffer.endGroup();
mOfflineRosterBuffer.sync();
return currentList;
}
bool CDTpController::registerDBusObject()
{
QDBusConnection connection = QDBusConnection::sessionBus();
if (!connection.isConnected()) {
qCWarning(lcContactsd) << "Could not connect to DBus:" << connection.lastError();
return false;
}
if (!connection.registerObject(DBusObjectPath, this)) {
qCWarning(lcContactsd) << "Could not register DBus object '/':" <<
connection.lastError();
return false;
}
return true;
}
CDTpRemovalOperation::CDTpRemovalOperation(CDTpAccountPtr accountWrapper,
const QStringList &contactIds) : PendingOperation(accountWrapper),
mContactIds(contactIds), mAccountWrapper(accountWrapper)
{
qCDebug(lcContactsd) << "CDTpRemovalOperation: start";
if (accountWrapper->account()->connection().isNull()) {
// If the connection is null, we make up an error and emit it
// setFinishedWithError takes care of going through the event loop
setFinishedWithError(QString::fromLatin1("nullConnection"),
QString::fromLatin1("Account connection is null"));
return;
}
Tp::ContactManagerPtr manager = accountWrapper->account()->connection()->contactManager();
QList<Tp::ContactPtr> contactsToRemove;
Q_FOREACH(const QString contactId, mContactIds) {
Q_FOREACH(const Tp::ContactPtr tpcontact, manager->allKnownContacts()) {
if (tpcontact->id() == contactId) {
contactsToRemove << tpcontact;
}
}
}
Tp::PendingOperation *call = manager->removeContacts(contactsToRemove);
connect(call,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onContactsRemoved(Tp::PendingOperation *)));
}
void CDTpRemovalOperation::onContactsRemoved(Tp::PendingOperation *op)
{
if (op->isError()) {
setFinishedWithError(op->errorName(), op->errorMessage());
return;
}
setFinished();
}
CDTpInvitationOperation::CDTpInvitationOperation(CDTpStorage &storage,
CDTpAccountPtr accountWrapper,
const QStringList &contactIds,
uint contactLocalId)
: PendingOperation(accountWrapper)
, mStorage(storage)
, mContactIds(contactIds)
, mAccountWrapper(accountWrapper)
, mContactLocalId(contactLocalId)
{
qCDebug(lcContactsd) << "CDTpInvitationOperation: start";
if (accountWrapper->account()->connection().isNull()) {
// If the connection is null, we make up an error and emit it
// setFinishedWithError takes care of going through the event loop
setFinishedWithError(QString::fromLatin1("nullConnection"),
QString::fromLatin1("Account connection is null"));
return;
}
Tp::ContactManagerPtr manager = accountWrapper->account()->connection()->contactManager();
Tp::PendingContacts *call = manager->contactsForIdentifiers(mContactIds);
connect(call,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onContactsRetrieved(Tp::PendingOperation *)));
}
void CDTpInvitationOperation::onContactsRetrieved(Tp::PendingOperation *op)
{
if (op->isError()) {
// We still create the IMAddress on the contact if the request fails, so
// that user has a feedback
if (mContactLocalId != 0) {
mStorage.createAccountContacts(mAccountWrapper, mContactIds, mContactLocalId);
}
setFinishedWithError(op->errorName(), op->errorMessage());
return;
}
Tp::PendingContacts *pcontacts = qobject_cast<Tp::PendingContacts *>(op);
if (mContactLocalId != 0) {
QStringList resolvedIds;
foreach (const Tp::ContactPtr &c, pcontacts->contacts()) {
resolvedIds.append(c->id());
}
// Also create "failed" IMAddresses for invalid identifiers
foreach (const QString &id, pcontacts->invalidIdentifiers().keys()) {
resolvedIds.append(id);
}
mStorage.createAccountContacts(mAccountWrapper, resolvedIds, mContactLocalId);
}
PendingOperation *call = pcontacts->manager()->requestPresenceSubscription(pcontacts->contacts());
connect(call,
SIGNAL(finished(Tp::PendingOperation *)),
SLOT(onPresenceSubscriptionRequested(Tp::PendingOperation *)));
}
void CDTpInvitationOperation::onPresenceSubscriptionRequested(Tp::PendingOperation *op)
{
if (op->isError()) {
setFinishedWithError(op->errorName(), op->errorMessage());
return;
}
setFinished();
}
|