aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/contextregistryinfo.cpp
blob: 08cab37d4524b405d111b5824008d90b13c93232 (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
/*
 * Copyright (C) 2008 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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 "contextregistryinfo.h"
#include "infobackend.h"
#include "sconnect.h"
#include "logging.h"
#include "loggingfeatures.h"
#include "contextproviderinfo.h"
#include <QMutex>
#include <QMutexLocker>
#include <QCoreApplication>

/*!
    \class ContextRegistryInfo

    \brief A class to introspect the registry contents.

    This is a singelton class used to obtain information about the keys (properties)
    in the registry database. The information can be provided either from xml files
    or from a cdb database. It's possible to list all the keys in the registry and
    also list all keys belonging to a one particular provider.
*/

ContextRegistryInfo* ContextRegistryInfo::registryInstance = NULL;

/* Public */

/// Returns the singleton instance of the ContextRegistryInfo. The object
/// is constructed automaticall on first access.
/// \param backendName the optional name of the backend to use (force).

ContextRegistryInfo* ContextRegistryInfo::instance(const QString &backendName)
{
    static QMutex mutex;
    QMutexLocker locker(&mutex);
    if (! registryInstance) {
        InfoBackend::instance(backendName);
        registryInstance = new ContextRegistryInfo;

        InfoBackend* infoBackend = InfoBackend::instance();

        sconnect(infoBackend, SIGNAL(keysChanged(QStringList)),
                 registryInstance, SLOT(onKeysChanged(QStringList)));

        sconnect(infoBackend, SIGNAL(keysAdded(QStringList)),
                 registryInstance, SLOT(onKeysAdded(QStringList)));

        sconnect(infoBackend, SIGNAL(keysRemoved(QStringList)),
                 registryInstance, SLOT(onKeysRemoved(QStringList)));

        sconnect(infoBackend, SIGNAL(listChanged()),
                 registryInstance, SLOT(onListChanged()));

        // Move the backend to the main thread
        registryInstance->moveToThread(QCoreApplication::instance()->thread());
    }

    return registryInstance;
}

/// Returns the list of all the keys currently availible in the registry.
QStringList ContextRegistryInfo::listKeys() const
{
    return InfoBackend::instance()->listKeys();
}

/// Returns the list of all the keys associated with the given provider.
QStringList ContextRegistryInfo::listKeys(QString providerName) const
{
    contextWarning() << F_DEPRECATION << "ContextRegistryInfo::listKeys(QString provider) is deprecated.";

    QSet<QString> keys;

    Q_FOREACH (QString key, listKeys()) {
        Q_FOREACH (ContextProviderInfo info, InfoBackend::instance()->providersForKey(key)) {
            if (info.plugin == "contextkit-dbus" &&
                info.constructionString.split(":").last() == providerName) {
                keys.insert(key);
            }
        }
    }

    return keys.toList();
}

/// DEPRECATED Returns the list of all the keys associated with the given plugin.
QStringList ContextRegistryInfo::listKeysForPlugin(QString plugin) const
{
    contextWarning() << F_DEPRECATION << "ContextRegistryInfo::listKeysForPlugin() is deprecated.";

    QSet<QString> keys;

    Q_FOREACH (QString key, listKeys()) {
        Q_FOREACH (ContextProviderInfo info, InfoBackend::instance()->providersForKey(key)) {
            if (info.plugin == plugin) {
                keys.insert(key);
            }
        }

    }

    return keys.toList();
}

/// DEPRECATED Returns the list of all unique providers in the registry.
/// The lists consist of strings with dbus names of the providers.
QStringList ContextRegistryInfo::listProviders() const
{
    contextWarning() << F_DEPRECATION << "ContextRegistryInfo::listProviders() is deprecated.";

    QSet<QString> providers;

    Q_FOREACH (QString key, listKeys()) {
        Q_FOREACH (ContextProviderInfo info, InfoBackend::instance()->providersForKey(key)) {
            if (info.plugin == "contextkit-dbus") {
                providers.insert(info.constructionString.split(":").last());
            }
        }
    }

    return providers.toList();
}

/// DEPRECATED Returns the list of all unique plugins in the registry.
QStringList ContextRegistryInfo::listPlugins() const
{
    contextWarning() << F_DEPRECATION << "ContextRegistryInfo::listPlugins() is deprecated.";

    QSet<QString> plugins;

    Q_FOREACH (QString key, listKeys()) {
        Q_FOREACH (ContextProviderInfo info, InfoBackend::instance()->providersForKey(key)) {
            plugins.insert(info.plugin);
        }
    }

    return plugins.toList();
}

/// Returns the name of the currently used registry backend. Ie. "cdb" or "xml".
QString ContextRegistryInfo::backendName() const
{
    return InfoBackend::instance()->name();
}

/* Slots */

/// This is connected to the \a keysChanged of the actual info backend instance.
/// Will be removed when deprecated keysChanged() signal is removed.
void ContextRegistryInfo::onKeysChanged(const QStringList& currentKeys)
{
    Q_EMIT keysChanged(currentKeys);
}

/// This is connected to the \a keysAdded of the actual info backend instance.
/// Will be removed when deprecated keysAdded() signal is removed.
void ContextRegistryInfo::onKeysAdded(const QStringList& newKeys)
{
    Q_EMIT keysAdded(newKeys);
}

/// This is connected to the \a keysRemoved of the actual info backend instance.
/// Will be removed when deprecated keysRemoved() signal is removed.
void ContextRegistryInfo::onKeysRemoved(const QStringList& removedKeys)
{
    Q_EMIT keysRemoved(removedKeys);
}

/// Called when people connect to signals. Used to emit deprecation warnings
/// when people connect to deprecated signals.
void ContextRegistryInfo::connectNotify(const char *signal)
{
    QObject::connectNotify(signal);

    if (signal == SIGNAL(keysChanged(QStringList)))
        contextWarning() << F_DEPRECATION << "ContextRegistryInfo::keysChanged signal is deprecated.";
    else if (signal == SIGNAL(keysAdded(QStringList)))
        contextWarning() << F_DEPRECATION << "ContextRegistryInfo::keysAdded signal is deprecated.";
    else if (signal == SIGNAL(keysRemoved(QStringList)))
        contextWarning() << F_DEPRECATION << "ContextRegistryInfo::keysRemoved signal is deprecated.";
}

/// This is connected to the \a listChanged of the actual info backend instance.
/// Gets called when the list of keys changes.
void ContextRegistryInfo::onListChanged()
{
    Q_EMIT changed();
}