~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to libtelephonyservice/accountentry.cpp

- Refactor TelepathyHelper initialization
- Refactor AccountEntry initialization

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "accountentry.h"
25
25
#include "protocolmanager.h"
26
26
 
 
27
Q_DECLARE_METATYPE(Tp::ConnectionPtr);
 
28
 
27
29
AccountEntry::AccountEntry(const Tp::AccountPtr &account, QObject *parent) :
28
30
    QObject(parent), mAccount(account), mReady(false), mProtocol(0)
29
31
{
 
32
    qRegisterMetaType<Tp::ConnectionPtr>();
30
33
    initialize();
31
34
}
32
35
 
99
102
bool AccountEntry::connected() const
100
103
{
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;
104
106
}
105
107
 
106
108
Tp::AccountPtr AccountEntry::account() const
145
147
    connect(mAccount.data(),
146
148
            SIGNAL(displayNameChanged(QString)),
147
149
            SIGNAL(displayNameChanged()));
148
 
    \
149
 
    // watch for account state and connection changes
150
 
    connect(mAccount.data(), &Tp::Account::stateChanged, [this](bool enabled) {
151
 
        if (!enabled) {
152
 
            ensureEnabled();
153
 
        }
154
 
    });
155
150
 
156
151
    connect(mAccount.data(),
157
152
            SIGNAL(connectionChanged(Tp::ConnectionPtr)),
158
 
            SLOT(onConnectionChanged()));
 
153
            SLOT(onConnectionChanged(Tp::ConnectionPtr)));
 
154
 
 
155
    connect(mAccount.data(),
 
156
            SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)),
 
157
            SIGNAL(connectionStatusChanged(Tp::ConnectionStatus)));
159
158
 
160
159
    connect(this,
161
160
            SIGNAL(connectedChanged()),
162
161
            SIGNAL(activeChanged()));
163
162
 
164
 
    // emit the statusChanged and statusMessageChanged signals together with the connectedChanged to be consistent
165
 
    connect(this,
166
 
            SIGNAL(connectedChanged()),
167
 
            SIGNAL(statusChanged()));
168
 
    connect(this,
169
 
            SIGNAL(connectedChanged()),
170
 
            SIGNAL(statusMessageChanged()));
171
 
 
172
 
    // and make sure it is enabled and connected
173
 
    if (!mAccount->isEnabled()) {
174
 
        QTimer::singleShot(0, this, SLOT(ensureEnabled()));
175
 
    } else {
176
 
        QTimer::singleShot(0, this, SLOT(ensureConnected()));
177
 
    }
178
163
    Q_EMIT accountIdChanged();
179
 
}
180
 
 
181
 
void AccountEntry::ensureEnabled()
182
 
{
183
 
    mAccount->setConnectsAutomatically(true);
184
 
    connect(mAccount->setEnabled(true),
185
 
            SIGNAL(finished(Tp::PendingOperation*)),
186
 
            SLOT(ensureConnected()));
187
 
}
188
 
 
189
 
void AccountEntry::ensureConnected()
190
 
{
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);
195
 
    } else {
196
 
        onConnectionChanged();
197
 
    }
198
 
 
 
164
    
 
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);
199
168
    mReady = true;
200
 
    Q_EMIT accountReady();
201
169
}
202
170
 
203
171
void AccountEntry::watchSelfContactPresence()
208
176
 
209
177
    connect(mAccount->connection()->selfContact().data(),
210
178
            SIGNAL(presenceChanged(Tp::Presence)),
 
179
            SIGNAL(statusChanged()));
 
180
 
 
181
    connect(mAccount->connection()->selfContact().data(),
 
182
            SIGNAL(presenceChanged(Tp::Presence)),
 
183
            SIGNAL(statusMessageChanged()));
 
184
 
 
185
    connect(mAccount->connection()->selfContact().data(),
 
186
            SIGNAL(presenceChanged(Tp::Presence)),
 
187
            SIGNAL(activeChanged()));
 
188
 
 
189
    connect(mAccount->connection()->selfContact().data(),
 
190
            SIGNAL(presenceChanged(Tp::Presence)),
211
191
            SIGNAL(connectedChanged()));
 
192
 
212
193
}
213
194
 
214
195
void AccountEntry::onSelfHandleChanged(uint handle)
220
201
    Q_EMIT selfContactIdChanged();
221
202
}
222
203
 
223
 
void AccountEntry::onConnectionChanged()
 
204
void AccountEntry::onConnectionChanged(Tp::ConnectionPtr connection)
224
205
{
225
 
    if (!mAccount->connection()) {
226
 
        // ensure the account gets connected
227
 
        ensureConnected();
228
 
    } else {
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();
231
209
 
232
 
        connect(mAccount->connection().data(),
 
210
        connect(connection.data(),
233
211
                SIGNAL(selfHandleChanged(uint)),
234
212
                SLOT(onSelfHandleChanged(uint)));
235
213
 
236
214
        watchSelfContactPresence();
 
215
    } else {
 
216
        mConnectionInfo.busName = QString();
 
217
        mConnectionInfo.objectPath = QString();
237
218
    }
238
219
 
239
220
    Q_EMIT connectedChanged();