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

Nepomuk-Core

  • KTp
global-presence.cpp
Go to the documentation of this file.
1 /*
2  * Global Presence - wraps calls to set and get presence for all accounts.
3  *
4  * Copyright (C) 2011 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-presence.h"
22 
23 #include "presence.h"
24 
25 #include <TelepathyQt/AccountSet>
26 #include <TelepathyQt/Account>
27 
28 #include <KDebug>
29 
30 namespace KTp
31 {
32 
33 GlobalPresence::GlobalPresence(QObject *parent)
34  : QObject(parent),
35  m_changingPresence(false)
36 {
37  Tp::Presence unknown;
38  unknown.setStatus(Tp::ConnectionPresenceTypeUnknown, QLatin1String("unknown"), QString());
39 
40  m_requestedPresence = KTp::Presence(unknown);
41  m_currentPresence = KTp::Presence(unknown);
42 }
43 
44 void GlobalPresence::setAccountManager(const Tp::AccountManagerPtr &accountManager)
45 {
46  if (! accountManager->isReady()) {
47  kWarning() << "GlobalPresence used with unready account manager";
48  }
49 
50  m_enabledAccounts = accountManager->enabledAccounts();
51  m_onlineAccounts = accountManager->onlineAccounts();
52 
53  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
54  onAccountAdded(account);
55  }
56 
57  onCurrentPresenceChanged();
58  onRequestedPresenceChanged();
59  onChangingPresence();
60 
61  connect(m_enabledAccounts.data(), SIGNAL(accountAdded(Tp::AccountPtr)), SLOT(onAccountAdded(Tp::AccountPtr)));
62 }
63 
64 
65 Tp::ConnectionStatus GlobalPresence::connectionStatus() const
66 {
67  return m_connectionStatus;
68 }
69 
70 Presence GlobalPresence::currentPresence() const
71 {
72  return m_currentPresence;
73 }
74 
75 Presence GlobalPresence::requestedPresence() const
76 {
77  return m_requestedPresence;
78 }
79 
80 bool GlobalPresence::isChangingPresence() const
81 {
82  return m_changingPresence;
83 }
84 
85 void GlobalPresence::setPresence(const Tp::Presence &presence)
86 {
87  if (m_enabledAccounts.isNull()) {
88  kWarning() << "Requested presence change on empty accounts set";
89  return;
90  }
91 
92  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
93  account->setRequestedPresence(presence);
94  }
95 }
96 
97 void GlobalPresence::onAccountAdded(const Tp::AccountPtr &account)
98 {
99  connect(account.data(), SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)), SLOT(onConnectionStatusChanged()));
100  connect(account.data(), SIGNAL(changingPresence(bool)), SLOT(onChangingPresence()));
101  connect(account.data(), SIGNAL(requestedPresenceChanged(Tp::Presence)), SLOT(onRequestedPresenceChanged()));
102  connect(account.data(), SIGNAL(currentPresenceChanged(Tp::Presence)), SLOT(onCurrentPresenceChanged()));
103 }
104 
105 void GlobalPresence::onCurrentPresenceChanged()
106 {
107  Tp::Presence highestCurrentPresence = Tp::Presence::offline();
108  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
109  if (Presence::sortPriority(account->currentPresence().type()) < Presence::sortPriority(highestCurrentPresence.type())) {
110  highestCurrentPresence = account->currentPresence();
111  }
112  }
113 
114  kDebug() << "Current presence changed";
115 
116  if (highestCurrentPresence.type() != m_currentPresence.type() ||
117  highestCurrentPresence.status() != m_currentPresence.status() ||
118  highestCurrentPresence.statusMessage() != m_currentPresence.statusMessage()) {
119 
120  m_currentPresence = Presence(highestCurrentPresence);
121  Q_EMIT currentPresenceChanged(m_currentPresence);
122  }
123 }
124 
125 void GlobalPresence::onRequestedPresenceChanged()
126 {
127  Tp::Presence highestRequestedPresence = Tp::Presence::offline();
128  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
129  if (Presence::sortPriority(account->requestedPresence().type()) < Presence::sortPriority(highestRequestedPresence.type())) {
130  highestRequestedPresence = account->requestedPresence();
131  }
132  }
133 
134  if (highestRequestedPresence.type() != m_requestedPresence.type() ||
135  highestRequestedPresence.status() != m_requestedPresence.status() ||
136  highestRequestedPresence.statusMessage() != m_requestedPresence.statusMessage()) {
137  m_requestedPresence = Presence(highestRequestedPresence);
138  Q_EMIT requestedPresenceChanged(m_requestedPresence);
139  }
140 }
141 
142 void GlobalPresence::onChangingPresence()
143 {
144  bool isChangingPresence = false;
145  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
146  if (account->isChangingPresence()) {
147  isChangingPresence = true;
148  }
149  }
150 
151  if (isChangingPresence != m_changingPresence) {
152  m_changingPresence = isChangingPresence;
153  Q_EMIT changingPresence(m_changingPresence);
154  }
155 }
156 
157 void GlobalPresence::onConnectionStatusChanged()
158 {
159  Tp::ConnectionStatus connectionStatus = Tp::ConnectionStatusDisconnected;
160 
161  Q_FOREACH(const Tp::AccountPtr &account, m_enabledAccounts->accounts()) {
162  switch (account->connectionStatus()) {
163  case Tp::ConnectionStatusConnecting:
164  //connecting is the highest state, use this always
165  connectionStatus = Tp::ConnectionStatusConnecting;
166  break;
167  case Tp::ConnectionStatusConnected:
168  //only set to connected if we're not at connecting
169  if (connectionStatus == Tp::ConnectionStatusDisconnected) {
170  connectionStatus = Tp::ConnectionStatusConnected;
171  }
172  break;
173  default:
174  break;
175  }
176  }
177 
178  if (connectionStatus != m_connectionStatus) {
179  m_connectionStatus = connectionStatus;
180  Q_EMIT connectionStatusChanged(m_connectionStatus);
181  }
182 }
183 
184 
185 bool GlobalPresence::hasEnabledAccounts() const
186 {
187  if (m_enabledAccounts->accounts().isEmpty()) {
188  return false;
189  }
190 
191  return true;
192 }
193 
194 void GlobalPresence::saveCurrentPresence()
195 {
196  kDebug() << "Saving presence with message:" << m_currentPresence.statusMessage();
197  m_savedPresence = m_currentPresence;
198 }
199 
200 void GlobalPresence::restoreSavedPresence()
201 {
202  kDebug() << m_savedPresence.statusMessage();
203  setPresence(m_savedPresence);
204 }
205 
206 Tp::AccountSetPtr GlobalPresence::onlineAccounts() const
207 {
208  return m_onlineAccounts;
209 }
210 
211 }
212 
213 
214 #include "global-presence.moc"
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