aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/infoxmlbackend.cpp
blob: d4281a3c61c6f95cc7b65e0f0a9737c09783b45d (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * 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 <QFileInfo>
#include <QDir>
#include <QMutex>
#include <QXmlSimpleReader>
#include <QXmlInputSource>
#include <QFile>
#include <QList>
#include <stdlib.h>
#include "sconnect.h"
#include "infoxmlbackend.h"
#include "logging.h"
#include "loggingfeatures.h"
#include "contextproviderinfo.h"
#include "nanoxml.h"

/*!
    \class InfoXmlBackend

    \brief Implements the InfoBackend for reading data from a directory
    with xml files.

    This class is not exported in the public API. It keeps all the data cached
    in the memory. It's assumed that this backend is not going to be used live in
    production systems and does not need to be ultra-fast (instead, implementation
    simplicity and corectness are preffered). For fast backend see the InfoCdbBackend.
*/

InfoXmlBackend::InfoXmlBackend(QObject *parent)
    : InfoBackend(parent)
{
    /* Thinking about locking... the watcher notifications are delivered synced,
       so asuming the changes in the dir are atomic this is all we need. */

    contextDebug() << F_XML << "Initializing xml backend with path:" << InfoXmlBackend::registryPath();

    QDir dir = QDir(InfoXmlBackend::registryPath());
    if (! dir.exists() || ! dir.isReadable()) {
        contextWarning() << "Registry path" << InfoXmlBackend::registryPath()
                         << "is not a directory or is not readable!";
    } else {
        watcher.addPath(InfoXmlBackend::registryPath());
        sconnect(&watcher, SIGNAL(directoryChanged(QString)), this, SLOT(onDirectoryChanged(QString)));
        sconnect(&watcher, SIGNAL(fileChanged(QString)), this, SLOT(onFileChanged(QString)));
    }

    regenerateKeyDataList();
}

/// Returns 'xml'.
QString InfoXmlBackend::name() const
{
    return QString("xml");
}

QStringList InfoXmlBackend::listKeys() const
{
    QStringList list;

    // FIXME Hmm, we could return just the keyDataHash.keys itself here...
    Q_FOREACH (QString key, keyDataHash.keys()) {
        list << keyDataHash.value(key).name;
    }

    return list;
}

QString InfoXmlBackend::docForKey(QString key) const
{
    if (! keyDataHash.contains(key))
        return "";

    return keyDataHash.value(key).doc;
}

bool InfoXmlBackend::keyDeclared(QString key) const
{
    if (keyDataHash.contains(key))
        return true;
    else
        return false;
}

bool InfoXmlBackend::keyDeprecated(QString key) const
{
    if (! keyDataHash.contains(key))
        return false;

    return keyDataHash.value(key).deprecated;
}

/// Returns the full path to the registry directory. Takes the
/// \c CONTEXT_PROVIDERS env variable into account.
QString InfoXmlBackend::registryPath()
{
    const char *regpath = getenv("CONTEXT_PROVIDERS");
    if (! regpath)
        regpath = DEFAULT_CONTEXT_PROVIDERS;

    return QString(regpath);
}

/// Returns the full path to the core property declaration file. Takes
/// the \c CONTEXT_CORE_DECLARATIONS env variable into account.
QString InfoXmlBackend::coreDeclPath()
{
    const char *corepath = getenv("CONTEXT_CORE_DECLARATIONS");
    if (! corepath)
        corepath = DEFAULT_CONTEXT_CORE_DECLARATIONS;

    return QString(corepath);
}

/* Slots */

/// Called when one of the parsed XML files changed. This
/// triggers a whole registry rebuild + signal emissions.
void InfoXmlBackend::onFileChanged(const QString &path)
{
    // If one of the watched xml files changed this it pretty much
    // an unconditional reload message for us.

    contextDebug() << F_XML << path << "changed.";

    QStringList oldKeys = listKeys();
    regenerateKeyDataList();
    QStringList currentKeys = listKeys();

    // In the cdb (fast) backend we do check if anybody is watching
    // before doing this list processing. But in xml backend the perf
    // is not an issue.

    // Emissions
    checkAndEmitKeysAdded(currentKeys, oldKeys); // DEPRECATED emission
    checkAndEmitKeysRemoved(currentKeys, oldKeys); // DEPRECATED emission
    Q_EMIT keysChanged(listKeys()); // DEPRECATED emission

    Q_EMIT listChanged();
    checkAndEmitKeyChanged(currentKeys, oldKeys);
}

/// Called when the registry directory changed (ie. file removed or added).
/// Triggers a whole registry rebuild + signal emissions. It detects a situation
/// when a added/removed file was not a parsed(xml) file.
void InfoXmlBackend::onDirectoryChanged(const QString &path)
{
    // It could be that some other file was added to the directory which
    // we don't care about anyways.

    QDir dir = QDir(registryPath());
    dir.setFilter(QDir::Files);
    dir.setNameFilters(QStringList("*.context"));

    // It's enough for us to compare sizes here, not actual content (filenames). The
    // latter case is always handled by the fileChanged.

    if (dir.entryInfoList().size() == countOfFilesInLastParse)
        return;

    contextDebug() << F_XML << registryPath() << "directory changed.";

    QStringList oldKeys = listKeys();
    regenerateKeyDataList();
    QStringList currentKeys = listKeys();

    // In the cdb (fast) backend we do check if anybody is watching
    // before doing this list processing. But in xml backend the perf
    // is not an issue.

    // Emissions
    checkAndEmitKeysAdded(currentKeys, oldKeys); // DEPRECATED emission
    checkAndEmitKeysRemoved(currentKeys, oldKeys); // DEPRECATED emission
    Q_EMIT keysChanged(listKeys()); // DEPRECATED emission

    Q_EMIT listChanged();
    checkAndEmitKeyChanged(currentKeys, oldKeys);
}

/* Private */

/// Clears all the stored data about the registry and parses it
/// all over again.
void InfoXmlBackend::regenerateKeyDataList()
{
    keyDataHash.clear();
    keyProvidersHash.clear();
    countOfFilesInLastParse = 0;

    // Stop watching all files. We do keep wathching the dir though.
    QStringList watchedFiles = watcher.files();
    if (watchedFiles.size() > 0)
        watcher.removePaths(watchedFiles);

    if (QFile(InfoXmlBackend::coreDeclPath()).exists()) {
        contextDebug() << F_XML << "Reading core declarations from:" << InfoXmlBackend::coreDeclPath();
        readKeyDataFromXml (InfoXmlBackend::coreDeclPath());
    } else {
        contextDebug() << F_XML << "Core declarations file" << InfoXmlBackend::coreDeclPath() << "does not exist.";
    }

    contextDebug() << F_XML << "Re-reading xml contents from" << InfoXmlBackend::registryPath();

    // Read the core property declarations.
    // For each xml file in the registry we parse it and
    // add it to our hash. We did some sanity checks in the constructor
    // so we skip them now.

    QDir dir = QDir(registryPath());

    // Bail out now if no directory
    if (! dir.exists() || ! dir.isReadable())
        return;

    dir.setFilter(QDir::Files);
    dir.setNameFilters(QStringList("*.context"));

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

        if (! watcher.files().contains(f.filePath()))
            watcher.addPath(f.filePath());

        countOfFilesInLastParse++;
    }
}

/// Parse the given QVariant tree which is supposed to be a key tree.
void InfoXmlBackend::parseKey(const AssocTree &keyTree, const AssocTree &providerTree)
{
    QString key = keyTree.value("name").toString();
    QString plugin = providerTree.value("plugin").toString();
    QString constructionString = providerTree.value("constructionString").toString();
    QString doc = keyTree.value("doc").toString();
    QVariant deprecated_node = keyTree.node("deprecated");

    ContextTypeInfo typeInfo = keyTree.value("type");
    typeInfo = typeInfo.ensureNewTypes(); // Make sure to get rid of old names (INTEGER...)

    // Warn about description mismatch or add new
    if (keyDataHash.contains(key)) {
        if (typeInfo.name() != "" && typeInfo != keyDataHash[key].typeInfo)
            contextWarning() << F_XML << key << ": type mismatch in core property list and provider property list";
    } else {
        InfoKeyData keyData;
        keyData.name = key;
        keyData.typeInfo = typeInfo;
        keyData.doc = doc;
        keyData.deprecated = deprecated_node.isValid();

        contextDebug() << F_XML << "Adding new key" << key << "with type:" << keyData.typeInfo.name();
        keyDataHash.insert(key, keyData);
    }

    // Add provider details
    ContextProviderInfo providerInfo(plugin, constructionString);

    // Suport old-style XML...
    if (providerInfo.plugin == "") {
        QString currentProvider = providerTree.value("service").toString();
        QString currentBus = providerTree.value("bus").toString();

        if (currentBus != "" && currentProvider != "") {
            providerInfo.plugin = "contextkit-dbus";
            providerInfo.constructionString = currentBus + ":" + currentProvider;
        } else
            providerInfo.constructionString = "";
    }

    // If providerInfo is empty, do not add to the list
    if (providerInfo.plugin == "") {
        contextDebug() << F_XML << "Not adding provider info for key" << key << "no data";
        return;
    } else
        contextDebug() << F_XML << "Adding provider info for key" << key << "plugin:" << providerInfo.plugin << "constructionString:" << providerInfo.constructionString;

    // Add to the list of providers
    QList<ContextProviderInfo> list;
    if (keyProvidersHash.contains(key))
        list = keyProvidersHash.value(key);

    list.append(providerInfo);
    keyProvidersHash.insert(key, list);
}

/// Parses a given \a path file and adds it's contents to the hash.
/// Also adds the file to the watcher (starts observing it).
void InfoXmlBackend::readKeyDataFromXml(const QString &path)
{
    contextDebug() << F_XML << "Reading keys from" << path;

    NanoXml parser(path);

    // Check if format is all ok
    if (parser.didFail()) {
        contextWarning() << F_XML << "Reading" << path << "failed, parsing error.";
        return;
    }

    // Check the version of the file
    if (parser.namespaceUri() != "" && parser.namespaceUri() != BACKEND_COMPATIBILITY_NAMESPACE) {
        contextWarning() << F_XML << "Reading" << path << "failed, invalid version:" << parser.namespaceUri();
        return;
    }

    AssocTree rootTree = parser.result();

    if (rootTree.toList().at(0).toString() == "provider" ||
        rootTree.toList().at(0).toString() == "properties") {
        // One provider. Iterate over each key.
        Q_FOREACH (AssocTree keyTree, rootTree.nodes()) {
            if (keyTree.name() == "key")
                parseKey(keyTree, rootTree);
        }
    } else {
        // Multiple providers... iterate over providers and keys
        Q_FOREACH (AssocTree providerTree, rootTree.nodes())
            if (providerTree.name() == "provider")
                Q_FOREACH (AssocTree keyTree, providerTree.nodes())
                    if (keyTree.name() == "key")
                        parseKey(keyTree, providerTree);
    }
}

const QList<ContextProviderInfo> InfoXmlBackend::providersForKey(QString key) const
{
    return keyProvidersHash.value(key);
}

ContextTypeInfo InfoXmlBackend::typeInfoForKey(QString key) const
{
    if (! keyDataHash.contains(key))
        return ContextTypeInfo();

    return keyDataHash.value(key).typeInfo;
}