24
24
#include "accountentry.h"
25
25
#include "protocolmanager.h"
27
Q_DECLARE_METATYPE(Tp::ConnectionPtr);
27
29
AccountEntry::AccountEntry(const Tp::AccountPtr &account, QObject *parent) :
28
30
QObject(parent), mAccount(account), mReady(false), mProtocol(0)
32
qRegisterMetaType<Tp::ConnectionPtr>();
99
102
bool AccountEntry::connected() const
101
104
return !mAccount.isNull() && !mAccount->connection().isNull() &&
102
!mAccount->connection()->selfContact().isNull() &&
103
mAccount->connection()->selfContact()->presence().type() == Tp::ConnectionPresenceTypeAvailable;
105
mAccount->connection()->status() == Tp::ConnectionStatusConnected;
106
108
Tp::AccountPtr AccountEntry::account() const
145
147
connect(mAccount.data(),
146
148
SIGNAL(displayNameChanged(QString)),
147
149
SIGNAL(displayNameChanged()));
149
// watch for account state and connection changes
150
connect(mAccount.data(), &Tp::Account::stateChanged, [this](bool enabled) {
156
151
connect(mAccount.data(),
157
152
SIGNAL(connectionChanged(Tp::ConnectionPtr)),
158
SLOT(onConnectionChanged()));
153
SLOT(onConnectionChanged(Tp::ConnectionPtr)));
155
connect(mAccount.data(),
156
SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)),
157
SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)));
161
160
SIGNAL(connectedChanged()),
162
161
SIGNAL(activeChanged()));
164
// emit the statusChanged and statusMessageChanged signals together with the connectedChanged to be consistent
166
SIGNAL(connectedChanged()),
167
SIGNAL(statusChanged()));
169
SIGNAL(connectedChanged()),
170
SIGNAL(statusMessageChanged()));
172
// and make sure it is enabled and connected
173
if (!mAccount->isEnabled()) {
174
QTimer::singleShot(0, this, SLOT(ensureEnabled()));
176
QTimer::singleShot(0, this, SLOT(ensureConnected()));
178
163
Q_EMIT accountIdChanged();
181
void AccountEntry::ensureEnabled()
183
mAccount->setConnectsAutomatically(true);
184
connect(mAccount->setEnabled(true),
185
SIGNAL(finished(Tp::PendingOperation*)),
186
SLOT(ensureConnected()));
189
void AccountEntry::ensureConnected()
191
// if the account is not connected, request it to connect
192
if (!mAccount->connection() || mAccount->connectionStatus() == Tp::ConnectionStatusDisconnected) {
193
Tp::Presence presence(Tp::ConnectionPresenceTypeAvailable, "available", "online");
194
mAccount->setRequestedPresence(presence);
196
onConnectionChanged();
165
// we have to postpone this call to give telepathyhelper time to connect the signals
166
QMetaObject::invokeMethod(this, "onConnectionChanged", Qt::QueuedConnection, Q_ARG(Tp::ConnectionPtr, mAccount->connection()));
167
QMetaObject::invokeMethod(this, "accountReady", Qt::QueuedConnection);
200
Q_EMIT accountReady();
203
171
void AccountEntry::watchSelfContactPresence()
209
177
connect(mAccount->connection()->selfContact().data(),
210
178
SIGNAL(presenceChanged(Tp::Presence)),
179
SIGNAL(statusChanged()));
181
connect(mAccount->connection()->selfContact().data(),
182
SIGNAL(presenceChanged(Tp::Presence)),
183
SIGNAL(statusMessageChanged()));
185
connect(mAccount->connection()->selfContact().data(),
186
SIGNAL(presenceChanged(Tp::Presence)),
187
SIGNAL(activeChanged()));
189
connect(mAccount->connection()->selfContact().data(),
190
SIGNAL(presenceChanged(Tp::Presence)),
211
191
SIGNAL(connectedChanged()));
214
195
void AccountEntry::onSelfHandleChanged(uint handle)
220
201
Q_EMIT selfContactIdChanged();
223
void AccountEntry::onConnectionChanged()
204
void AccountEntry::onConnectionChanged(Tp::ConnectionPtr connection)
225
if (!mAccount->connection()) {
226
// ensure the account gets connected
229
mConnectionInfo.busName = mAccount->connection()->busName();
230
mConnectionInfo.objectPath = mAccount->connection()->objectPath();
206
if (!connection.isNull()) {
207
mConnectionInfo.busName = connection->busName();
208
mConnectionInfo.objectPath = connection->objectPath();
232
connect(mAccount->connection().data(),
210
connect(connection.data(),
233
211
SIGNAL(selfHandleChanged(uint)),
234
212
SLOT(onSelfHandleChanged(uint)));
236
214
watchSelfContactPresence();
216
mConnectionInfo.busName = QString();
217
mConnectionInfo.objectPath = QString();
239
220
Q_EMIT connectedChanged();