summaryrefslogtreecommitdiff
path: root/TelepathyQt/profile-manager.cpp
blob: 46391b6e63304d6c9d5e179a01de7bd24f540315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2010 Nokia Corporation
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt/ProfileManager>

#include "TelepathyQt/_gen/profile-manager.moc.hpp"
#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/ConnectionManager>
#include <TelepathyQt/PendingComposite>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/PendingStringList>
#include <TelepathyQt/Profile>
#include <TelepathyQt/ReadinessHelper>

#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QStringList>

namespace Tp
{

struct TP_QT_NO_EXPORT ProfileManager::Private
{
    Private(ProfileManager *parent, const QDBusConnection &bus);

    static void introspectMain(Private *self);
    static void introspectFakeProfiles(Private *self);

    ProfileManager *parent;
    ReadinessHelper *readinessHelper;
    QDBusConnection bus;
    QHash<QString, ProfilePtr> profiles;
    QList<ConnectionManagerPtr> cms;
};

ProfileManager::Private::Private(ProfileManager *parent, const QDBusConnection &bus)
    : parent(parent),
      readinessHelper(parent->readinessHelper()),
      bus(bus)
{
    ReadinessHelper::Introspectables introspectables;

    ReadinessHelper::Introspectable introspectableCore(
        QSet<uint>() << 0,                                           // makesSenseForStatuses
        Features(),                                                  // dependsOnFeatures
        QStringList(),                                               // dependsOnInterfaces
        (ReadinessHelper::IntrospectFunc) &Private::introspectMain,
        this);
    introspectables[FeatureCore] = introspectableCore;

    ReadinessHelper::Introspectable introspectableFakeProfiles(
        QSet<uint>() << 0,                                           // makesSenseForStatuses
        Features() << FeatureCore,                                   // dependsOnFeatures
        QStringList(),                                               // dependsOnInterfaces
        (ReadinessHelper::IntrospectFunc) &Private::introspectFakeProfiles,
        this);
    introspectables[FeatureFakeProfiles] = introspectableFakeProfiles;

    readinessHelper->addIntrospectables(introspectables);
}

void ProfileManager::Private::introspectMain(ProfileManager::Private *self)
{
    QStringList searchDirs = Profile::searchDirs();

    foreach (const QString searchDir, searchDirs) {
        QDir dir(searchDir);
        dir.setFilter(QDir::Files);

        QFileInfoList list = dir.entryInfoList();
        for (int i = 0; i < list.size(); ++i) {
            QFileInfo fi = list.at(i);

            if (fi.completeSuffix() !=  QLatin1String("profile")) {
                continue;
            }

            QString fileName = fi.absoluteFilePath();
            QString serviceName = fi.baseName();

            if (self->profiles.contains(serviceName)) {
                debug() << "Profile for service" << serviceName << "already "
                    "exists. Ignoring profile file:" << fileName;
                continue;
            }

            ProfilePtr profile = Profile::createForFileName(fileName);
            if (!profile->isValid()) {
                continue;
            }

            if (profile->type() != QLatin1String("IM")) {
                debug() << "Ignoring profile for service" << serviceName <<
                    ": type != IM. Profile file:" << fileName;
                continue;
            }

            debug() << "Found profile for service" << serviceName <<
                "- profile file:" << fileName;
            self->profiles.insert(serviceName, profile);
        }
    }

    self->readinessHelper->setIntrospectCompleted(FeatureCore, true);
}

void ProfileManager::Private::introspectFakeProfiles(ProfileManager::Private *self)
{
    PendingStringList *pendingCmNames = ConnectionManager::listNames(self->bus);
    self->parent->connect(pendingCmNames,
            SIGNAL(finished(Tp::PendingOperation *)),
            SLOT(onCmNamesRetrieved(Tp::PendingOperation *)));
}

/**
 * \class ProfileManager
 * \headerfile TelepathyQt/profile-manager.h <TelepathyQt/ProfileManager>
 *
 * \brief The ProfileManager class provides helper methods to retrieve Profile
 * objects.
 */

/**
 * Feature representing the core that needs to become ready to make the ProfileManager
 * object usable.
 *
 * Note that this feature must be enabled in order to use all ProfileManager methods.
 *
 * When calling isReady(), becomeReady(), this feature is implicitly added
 * to the requested features.
 */
const Feature ProfileManager::FeatureCore = Feature(QLatin1String(ProfileManager::staticMetaObject.className()), 0, true);

/**
 * Enabling this feature will make ProfileManager create fake Profile objects to all protocols
 * supported on the installed connection managers, even if they don't have .profile files installed
 * making use of them.
 *
 * Fake profiles are identified by Profile::isFake() returning \c true.
 *
 * The fake profile will contain the following info:
 *  - Profile::type() will return "IM"
 *  - Profile::provider() will return an empty string
 *  - Profile::serviceName() will return cmName-protocolName
 *  - Profile::name() and Profile::protocolName() will return protocolName
 *  - Profile::iconName() will return "im-protocolName"
 *  - Profile::cmName() will return cmName
 *  - Profile::parameters() will return a list matching CM default parameters for protocol with name
 *    protocolName.
 *  - Profile::presences() will return an empty list and
 *    Profile::allowOtherPresences() will return \c true, meaning that CM
 *    presences should be used
 *  - Profile::unsupportedChannelClassSpecs() will return an empty list
 *
 * Where cmName and protocolName are the name of the connection manager and the name of the protocol
 * for which this fake Profile is created, respectively.
 */
const Feature ProfileManager::FeatureFakeProfiles = Feature(QLatin1String(ProfileManager::staticMetaObject.className()), 1);

/**
 * Create a new ProfileManager object.
 */
ProfileManagerPtr ProfileManager::create(const QDBusConnection &bus)
{
    return ProfileManagerPtr(new ProfileManager(bus));
}

/**
 * Construct a new ProfileManager object.
 */
ProfileManager::ProfileManager(const QDBusConnection &bus)
    : Object(),
      ReadyObject(this, FeatureCore),
      mPriv(new Private(this, bus))
{
}

/**
 * Class destructor.
 */
ProfileManager::~ProfileManager()
{
    delete mPriv;
}

/**
 * Return a list of all available profiles.
 *
 * \return A list of all available profiles.
 */
QList<ProfilePtr> ProfileManager::profiles() const
{
    return mPriv->profiles.values();
}

/**
 * Return a list of all available profiles for a given connection manager.
 *
 * \param cmName Connection manager name.
 * \return A list of all available profiles for a given connection manager.
 */
QList<ProfilePtr> ProfileManager::profilesForCM(const QString &cmName) const
{
    QList<ProfilePtr> ret;
    foreach (const ProfilePtr &profile, mPriv->profiles) {
        if (profile->cmName() == cmName) {
            ret << profile;
        }
    }
    return ret;
}

/**
 * Return a list of all available profiles for a given \a protocol.
 *
 * \param protocolName Protocol name.
 * \return A list of all available profiles for a given \a protocol.
 */
QList<ProfilePtr> ProfileManager::profilesForProtocol(
        const QString &protocolName) const
{
    QList<ProfilePtr> ret;
    foreach (const ProfilePtr &profile, mPriv->profiles) {
        if (profile->protocolName() == protocolName) {
            ret << profile;
        }
    }
    return ret;
}

/**
 * Return the profile for a given \a service.
 *
 * \param serviceName Service name.
 * \return The profile for \a service.
 */
ProfilePtr ProfileManager::profileForService(const QString &serviceName) const
{
    return mPriv->profiles.value(serviceName);
}

void ProfileManager::onCmNamesRetrieved(Tp::PendingOperation *op)
{
    if (op->isError()) {
        warning().nospace() << "Getting available CMs failed with " <<
            op->errorName() << ":" << op->errorMessage();
        mPriv->readinessHelper->setIntrospectCompleted(FeatureFakeProfiles, false,
                op->errorName(), op->errorMessage());
        return;
    }

    PendingStringList *pendingCmNames = qobject_cast<PendingStringList *>(op);
    QStringList cmNames(pendingCmNames->result());
    if (cmNames.isEmpty()) {
        mPriv->readinessHelper->setIntrospectCompleted(FeatureFakeProfiles, true);
        return;
    }

    QList<PendingOperation *> ops;
    foreach (const QString &cmName, cmNames) {
        ConnectionManagerPtr cm = ConnectionManager::create(mPriv->bus, cmName);
        mPriv->cms.append(cm);
        ops.append(cm->becomeReady());
    }

    PendingComposite *pc = new PendingComposite(ops, false, ProfileManagerPtr(this));
    connect(pc,
            SIGNAL(finished(Tp::PendingOperation *)),
            SLOT(onCMsReady(Tp::PendingOperation *)));
}

void ProfileManager::onCMsReady(Tp::PendingOperation *op)
{
    if (op->isError()) {
        warning() << "Failed introspecting all CMs, trying to create fake profiles anyway";
    }

    ProfilePtr profile;
    foreach (const ConnectionManagerPtr &cm, mPriv->cms) {
        if (!cm->isReady()) {
            continue;
        }

        foreach (const QString &protocolName, cm->supportedProtocols()) {
            /* check if there is a profile whose service name is protocolName, and if found,
             * check if the profile is for cm, if not check if there is a profile whose service
             * name is cm-protocolName, and if not found create one named cm-protocolName. */
            profile = profileForService(protocolName);
            if (profile && profile->cmName() == cm->name()) {
                continue;
            }

            QString serviceName = QString(QLatin1String("%1-%2")).arg(cm->name()).arg(protocolName);
            profile = profileForService(serviceName);
            if (profile) {
                continue;
            }

            profile = ProfilePtr(new Profile(
                        serviceName,
                        cm->name(),
                        protocolName,
                        cm->protocol(protocolName)));
            mPriv->profiles.insert(serviceName, profile);
        }
    }

    mPriv->readinessHelper->setIntrospectCompleted(FeatureFakeProfiles, true);
}

} // Tp