~phablet-team/telephony-service/trunk

« back to all changes in this revision

Viewing changes to handler/tests/telepathyhelper.cpp

  • Committer: Michael Terry
  • Date: 2014-01-29 15:16:16 UTC
  • mfrom: (766 trunk)
  • mto: This revision was merged to the branch mainline in revision 769.
  • Revision ID: michael.terry@canonical.com-20140129151616-63986zad796m4gtl
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License version 3, as published by
 
6
 * the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
9
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 
10
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: 
 
17
 *  Tiago Salem Herrmann <tiago.herrmann@canonical.com>
 
18
 *  Gustavo Pichorim Boiko <gustavo.boiko@canonical.com>
 
19
 */
 
20
 
 
21
#include "telepathyhelper.h"
 
22
#include <TelepathyQt/AbstractClient>
 
23
#include <TelepathyQt/AccountSet>
 
24
#include <TelepathyQt/ClientRegistrar>
 
25
#include <TelepathyQt/PendingReady>
 
26
#include <TelepathyQt/PendingAccount>
 
27
 
 
28
TelepathyHelper::TelepathyHelper(QObject *parent)
 
29
    : QObject(parent),
 
30
      //mChannelObserver(0),
 
31
      mFirstTime(true),
 
32
      mConnected(false),
 
33
      mHandlerInterface(0)
 
34
{
 
35
    Tp::registerTypes();
 
36
 
 
37
    mAccountFeatures << Tp::Account::FeatureCore;
 
38
    mContactFeatures << Tp::Contact::FeatureAlias
 
39
                     << Tp::Contact::FeatureAvatarData
 
40
                     << Tp::Contact::FeatureAvatarToken
 
41
                     << Tp::Contact::FeatureCapabilities
 
42
                     << Tp::Contact::FeatureSimplePresence;
 
43
    mConnectionFeatures << Tp::Connection::FeatureCore
 
44
                        << Tp::Connection::FeatureSelfContact
 
45
                        << Tp::Connection::FeatureSimplePresence;
 
46
 
 
47
    Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
 
48
    channelFactory->addCommonFeatures(Tp::Channel::FeatureCore);
 
49
 
 
50
    mAccountManager = Tp::AccountManager::create(
 
51
            Tp::AccountFactory::create(QDBusConnection::sessionBus(), mAccountFeatures),
 
52
            Tp::ConnectionFactory::create(QDBusConnection::sessionBus(), mConnectionFeatures),
 
53
            channelFactory,
 
54
            Tp::ContactFactory::create(mContactFeatures));
 
55
 
 
56
    connect(mAccountManager->becomeReady(Tp::AccountManager::FeatureCore),
 
57
            SIGNAL(finished(Tp::PendingOperation*)),
 
58
            SLOT(onAccountManagerReady(Tp::PendingOperation*)));
 
59
 
 
60
    mClientRegistrar = Tp::ClientRegistrar::create(mAccountManager);
 
61
}
 
62
 
 
63
TelepathyHelper::~TelepathyHelper()
 
64
{
 
65
}
 
66
 
 
67
TelepathyHelper *TelepathyHelper::instance()
 
68
{
 
69
    static TelepathyHelper* helper = new TelepathyHelper();
 
70
    return helper;
 
71
}
 
72
 
 
73
QString TelepathyHelper::accountId() const
 
74
{
 
75
    if (mAccount) {
 
76
        return mAccount->uniqueIdentifier();
 
77
    }
 
78
    return QString();
 
79
}
 
80
 
 
81
Tp::AccountPtr TelepathyHelper::account() const
 
82
{
 
83
    return mAccount;
 
84
}
 
85
 
 
86
/*
 
87
ChannelObserver *TelepathyHelper::channelObserver() const
 
88
{
 
89
    return mChannelObserver;
 
90
}
 
91
*/
 
92
 
 
93
bool TelepathyHelper::connected() const
 
94
{
 
95
    return mConnected;
 
96
}
 
97
 
 
98
/*
 
99
void TelepathyHelper::registerChannelObserver(const QString &observerName)
 
100
{
 
101
    QString name = observerName;
 
102
 
 
103
    if (name.isEmpty()) {
 
104
        name = "TelephonyPluginObserver";
 
105
    }
 
106
 
 
107
    if (mChannelObserver) {
 
108
        mChannelObserver->deleteLater();
 
109
    }
 
110
 
 
111
    mChannelObserver = new ChannelObserver(this);
 
112
    registerClient(mChannelObserver, name);
 
113
 
 
114
    // messages
 
115
    connect(mChannelObserver, SIGNAL(textChannelAvailable(Tp::TextChannelPtr)),
 
116
            ChatManager::instance(), SLOT(onTextChannelAvailable(Tp::TextChannelPtr)));
 
117
 
 
118
    // calls
 
119
    connect(mChannelObserver, SIGNAL(callChannelAvailable(Tp::CallChannelPtr)),
 
120
            CallManager::instance(), SLOT(onCallChannelAvailable(Tp::CallChannelPtr)));
 
121
 
 
122
    Q_EMIT channelObserverCreated(mChannelObserver);
 
123
}
 
124
 
 
125
void TelepathyHelper::unregisterChannelObserver()
 
126
{
 
127
    Tp::AbstractClientPtr clientPtr(mChannelObserver);
 
128
    if (clientPtr) {
 
129
        mClientRegistrar->unregisterClient(clientPtr);
 
130
    }
 
131
    mChannelObserver->deleteLater();
 
132
    mChannelObserver = NULL;
 
133
    Q_EMIT channelObserverUnregistered();
 
134
}
 
135
*/
 
136
 
 
137
QStringList TelepathyHelper::supportedProtocols() const
 
138
{
 
139
    QStringList protocols;
 
140
    protocols << "mock";
 
141
    return protocols;
 
142
}
 
143
 
 
144
void TelepathyHelper::initializeAccount()
 
145
{
 
146
    // watch for account state and connection changes
 
147
    connect(mAccount.data(),
 
148
            SIGNAL(stateChanged(bool)),
 
149
            SLOT(onAccountStateChanged(bool)));
 
150
    connect(mAccount.data(),
 
151
            SIGNAL(connectionChanged(const Tp::ConnectionPtr&)),
 
152
            SLOT(onAccountConnectionChanged(const Tp::ConnectionPtr&)));
 
153
 
 
154
    // and make sure it is enabled and connected
 
155
    if (!mAccount->isEnabled()) {
 
156
        ensureAccountEnabled();
 
157
    } else {
 
158
        ensureAccountConnected();
 
159
    }
 
160
}
 
161
 
 
162
void TelepathyHelper::ensureAccountEnabled()
 
163
{
 
164
    mAccount->setConnectsAutomatically(true);
 
165
    connect(mAccount->setEnabled(true),
 
166
            SIGNAL(finished(Tp::PendingOperation*)),
 
167
            SLOT(onAccountEnabled(Tp::PendingOperation*)));
 
168
}
 
169
 
 
170
void TelepathyHelper::ensureAccountConnected()
 
171
{
 
172
    // if the account is not connected, request it to connect
 
173
    if (!mAccount->connection() || mAccount->connectionStatus() != Tp::ConnectionStatusConnected) {
 
174
        Tp::Presence presence(Tp::ConnectionPresenceTypeAvailable, "available", "online");
 
175
        mAccount->setRequestedPresence(presence);
 
176
    } else {
 
177
        watchSelfContactPresence();
 
178
    }
 
179
 
 
180
    if (mFirstTime) {
 
181
        Q_EMIT accountReady();
 
182
        mFirstTime = false;
 
183
    }
 
184
}
 
185
 
 
186
void TelepathyHelper::watchSelfContactPresence()
 
187
{
 
188
    if (mAccount.isNull() || mAccount->connection().isNull()) {
 
189
        return;
 
190
    }
 
191
 
 
192
    connect(mAccount->connection()->selfContact().data(),
 
193
            SIGNAL(presenceChanged(Tp::Presence)),
 
194
            SLOT(onPresenceChanged(Tp::Presence)));
 
195
    onPresenceChanged(mAccount->connection()->selfContact()->presence());
 
196
}
 
197
 
 
198
void TelepathyHelper::registerClient(Tp::AbstractClient *client, QString name)
 
199
{
 
200
    Tp::AbstractClientPtr clientPtr(client);
 
201
    bool succeeded = mClientRegistrar->registerClient(clientPtr, name);
 
202
    if (!succeeded) {
 
203
        name.append("%1");
 
204
        int count = 0;
 
205
        // limit the number of registered clients to 20, that should be a safe margin
 
206
        while (!succeeded && count < 20) {
 
207
            succeeded = mClientRegistrar->registerClient(clientPtr, name.arg(++count));
 
208
            if (succeeded) {
 
209
                name = name.arg(count);
 
210
            }
 
211
        }
 
212
    }
 
213
 
 
214
    if (succeeded) {
 
215
        QObject *object = dynamic_cast<QObject*>(client);
 
216
        if (object) {
 
217
            object->setProperty("clientName", TP_QT_IFACE_CLIENT + "." + name );
 
218
        }
 
219
    }
 
220
}
 
221
 
 
222
void TelepathyHelper::onAccountManagerReady(Tp::PendingOperation *op)
 
223
{
 
224
    Q_UNUSED(op)
 
225
 
 
226
    Tp::AccountSetPtr accountSet;
 
227
    // try to find an account of the one of supported protocols
 
228
    Q_FOREACH(const QString &protocol, supportedProtocols()) {
 
229
        accountSet = mAccountManager->accountsByProtocol(protocol);
 
230
        if (accountSet->accounts().count() > 0) {
 
231
            break;
 
232
        }
 
233
    }
 
234
 
 
235
    if (accountSet->accounts().count() == 0) {
 
236
        qCritical() << "No compatible telepathy account found!";
 
237
        return;
 
238
    }
 
239
 
 
240
    mAccount = accountSet->accounts()[0];
 
241
 
 
242
    // in case we have more than one account, the first one to show on the list is going to be used
 
243
    if (accountSet->accounts().count() > 1) {
 
244
        qWarning() << "There are more than just one account of type" << mAccount->protocolName();
 
245
    }
 
246
 
 
247
    Q_EMIT accountIdChanged();
 
248
 
 
249
    initializeAccount();
 
250
}
 
251
 
 
252
void TelepathyHelper::onAccountEnabled(Tp::PendingOperation *op)
 
253
{
 
254
    // we might need to do more stuff once the account is enabled, but making sure it is connected is a good start
 
255
    ensureAccountConnected();
 
256
}
 
257
 
 
258
void TelepathyHelper::onAccountStateChanged(bool enabled)
 
259
{
 
260
    if (!enabled) {
 
261
        ensureAccountEnabled();
 
262
    }
 
263
}
 
264
 
 
265
void TelepathyHelper::onAccountConnectionChanged(const Tp::ConnectionPtr &connection)
 
266
{
 
267
    if (connection.isNull()) {
 
268
        ensureAccountConnected();
 
269
    } else {
 
270
        watchSelfContactPresence();
 
271
    }
 
272
    Q_EMIT connectionChanged();
 
273
}
 
274
 
 
275
void TelepathyHelper::onPresenceChanged(const Tp::Presence &presence)
 
276
{
 
277
    mConnected = (presence.type() == Tp::ConnectionPresenceTypeAvailable);
 
278
    Q_EMIT connectedChanged();
 
279
}