• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
global-contact-manager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of telepathy-common-internals
3  *
4  * Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "global-contact-manager.h"
22 
23 #include <TelepathyQt/AccountManager>
24 #include <TelepathyQt/Account>
25 #include <TelepathyQt/ContactManager>
26 #include <TelepathyQt/PendingReady>
27 
28 #include <KDebug>
29 
30 
31 namespace KTp {
32 class GlobalContactManagerPrivate {
33 public:
34  Tp::AccountManagerPtr accountManager;
35 };
36 }
37 
38 using namespace KTp;
39 
40 GlobalContactManager::GlobalContactManager(const Tp::AccountManagerPtr &accountManager, QObject *parent) :
41  QObject(parent),
42  d(new GlobalContactManagerPrivate())
43 {
44  d->accountManager = accountManager;
45  connect(d->accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)), SLOT(onAccountManagerReady(Tp::PendingOperation*)));
46 }
47 
48 GlobalContactManager::~GlobalContactManager()
49 {
50  delete d;
51 }
52 
53 Tp::Contacts GlobalContactManager::allKnownContacts() const
54 {
55  Tp::Contacts allContacts;
56  if (d->accountManager.isNull()) {
57  return allContacts;
58  }
59 
60  Q_FOREACH(const Tp::AccountPtr &account, d->accountManager->allAccounts()) {
61  if (!account->connection().isNull() && account->connection()->contactManager()->state() == Tp::ContactListStateSuccess) {
62  allContacts.unite(account->connection()->contactManager()->allKnownContacts());
63  }
64  }
65  return allContacts;
66 }
67 
68 void GlobalContactManager::onAccountManagerReady(Tp::PendingOperation *op)
69 {
70  if (op->isError()) {
71  kWarning() << "Account Manager becomeReady failed";
72  }
73 
74  Q_FOREACH(const Tp::AccountPtr &account, d->accountManager->allAccounts()) {
75  onNewAccount(account);
76  }
77  connect(d->accountManager.data(), SIGNAL(newAccount(Tp::AccountPtr)), SLOT(onNewAccount(Tp::AccountPtr)));
78 
79 }
80 
81 void GlobalContactManager::onNewAccount(const Tp::AccountPtr &account)
82 {
83  onConnectionChanged(account->connection());
84  connect(account.data(), SIGNAL(connectionChanged(Tp::ConnectionPtr)), SLOT(onConnectionChanged(Tp::ConnectionPtr)));
85 }
86 
87 
88 void GlobalContactManager::onConnectionChanged(const Tp::ConnectionPtr &connection)
89 {
90  if (connection.isNull()) {
91  return;
92  }
93  onContactManagerStateChanged(connection->contactManager(), connection->contactManager()->state());
94  connect(connection->contactManager().data(), SIGNAL(stateChanged(Tp::ContactListState)), SLOT(onContactManagerStateChanged(Tp::ContactListState)));
95 }
96 
97 void GlobalContactManager::onContactManagerStateChanged(Tp::ContactListState state)
98 {
99  Tp::ContactManager* contactManager = qobject_cast<Tp::ContactManager*>(sender());
100  Q_ASSERT(contactManager);
101  onContactManagerStateChanged(Tp::ContactManagerPtr(contactManager), state);
102 }
103 
104 void GlobalContactManager::onContactManagerStateChanged(const Tp::ContactManagerPtr &contactManager, Tp::ContactListState state)
105 {
106  //contact manager still isn't ready. Do nothing.
107  if (state != Tp::ContactListStateSuccess) {
108  return;
109  }
110 
111  //contact manager connected, inform everyone of potential new contacts
112  Q_EMIT allKnownContactsChanged(contactManager->allKnownContacts(), Tp::Contacts());
113 
114  connect(contactManager.data(), SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts,Tp::Channel::GroupMemberChangeDetails)), SIGNAL(allKnownContactsChanged(Tp::Contacts,Tp::Contacts)));
115 }
116 
117 Tp::AccountPtr GlobalContactManager::accountForContact(const Tp::ContactPtr &contact) const
118 {
119  return accountForConnection(contact->manager()->connection());
120 }
121 
122 Tp::AccountPtr GlobalContactManager::accountForConnection(const Tp::ConnectionPtr &connection) const
123 {
124  //loop through all accounts looking for a matching connection.
125  //arguably inneficient, but no. of accounts is normally very low, and it's not called very often.
126  Q_FOREACH(const Tp::AccountPtr &account, d->accountManager->allAccounts()) {
127  if (account->connection() == connection) {
128  return account;
129  }
130  }
131 
132  return Tp::AccountPtr();
133 }
134 
135 Tp::AccountPtr GlobalContactManager::accountForAccountId(const QString &accountId) const
136 {
137  if (!d->accountManager.isNull() && d->accountManager->isReady()) {
138  Q_FOREACH(const Tp::AccountPtr &account, d->accountManager->allAccounts()) {
139  if (account->uniqueIdentifier() == accountId) {
140  return account;
141  }
142  }
143  }
144 
145  return Tp::AccountPtr();
146 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal