aboutsummaryrefslogtreecommitdiff
path: root/libcontextsubscriber/src/infocdbbackend.cpp
blob: 87025d6f04d2372633527533f7eafc66473bd5a0 (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
/*
 * 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 <QDebug>
#include <QFile>
#include <stdlib.h>
#include <QHash>
#include "sconnect.h"
#include "infocdbbackend.h"
#include "logging.h"
#include "loggingfeatures.h"

/*!
    \class InfoCdbBackend

    \brief Implements the InfoBackend for reading data from a cdb database.

    This class is not exported in the public API. It does not cache any data
    to optimize the memory consumption. It's assumed that most data is anyways
    cached (as needed) in the ContextPropertyInfo and the cdb key-based access
    (no enumetation) is fast anyways. It observers the \c cache.cdb
    with a file system watcher.
*/

InfoCdbBackend::InfoCdbBackend(QObject *parent)
    : InfoBackend(parent), reader(InfoCdbBackend::databasePath())
{
    contextDebug() << F_CDB << "Initializing cdb backend with database:" << InfoCdbBackend::databasePath();

    sconnect(&watcher, SIGNAL(fileChanged(QString)), this, SLOT(onDatabaseFileChanged(QString)));
    sconnect(&watcher, SIGNAL(directoryChanged(QString)), this, SLOT(onDatabaseDirectoryChanged(QString)));

    watchPathOrDirectory();
}

/// Returns 'cdb'.
QString InfoCdbBackend::name() const
{
    return QString("cdb");
}

QStringList InfoCdbBackend::variantListToStringList(const QVariantList &l)
{
    QStringList ret;
    foreach (QVariant v, l)
        ret << v.toString();

    return ret;
}


QStringList InfoCdbBackend::listKeys() const
{
    return variantListToStringList(reader.valuesForKey("KEYS"));
}

QStringList InfoCdbBackend::listKeysForPlugin(QString plugin) const
{
    return variantListToStringList(reader.valuesForKey(plugin + ":KEYS"));
}

QStringList InfoCdbBackend::listPlugins() const
{
    return variantListToStringList(reader.valuesForKey("PLUGINS"));
}

QString InfoCdbBackend::typeForKey(QString key) const
{
    return reader.valueForKey(key + ":KEYTYPE").toString();
}

QString InfoCdbBackend::docForKey(QString key) const
{
    return reader.valueForKey(key + ":KEYDOC").toString();
}

QString InfoCdbBackend::pluginForKey(QString key) const
{
    return reader.valueForKey(key + ":KEYPLUGIN").toString();
}

QString InfoCdbBackend::constructionStringForKey(QString key) const
{
    return reader.valueForKey(key + ":KEYCONSTRUCTIONSTRING").toString();
}

bool InfoCdbBackend::keyExists(QString key) const
{
    if (reader.valuesForKey("KEYS").contains(key))
        return true;
    else
        return false;
}

bool InfoCdbBackend::keyProvided(QString key) const
{
    QString plugin = reader.valueForKey(key + ":KEYPLUGIN").toString();
    if (plugin == "")
        return false;

    return true;
}

/// Returns true if the database file is present.
bool InfoCdbBackend::databaseExists()
{
    QFile file(databasePath());
    return file.exists();
}

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

    return QDir(regpath).filePath("cache.cdb");
}

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

    return QString(regpath);
}

/* Private */

/// Start watching the database direcory for changes.
void InfoCdbBackend::watchDirectory()
{
    if (! watcher.directories().contains(InfoCdbBackend::databaseDirectory()))
        watcher.addPath(InfoCdbBackend::databaseDirectory());
}

/// Start watching the database file for changes.
void InfoCdbBackend::watchPath()
{
    if (! watcher.files().contains(InfoCdbBackend::databasePath()))
        watcher.addPath(InfoCdbBackend::databasePath());
}

/// Depending on our readability status, watch either path or the
/// directory.
void InfoCdbBackend::watchPathOrDirectory()
{
    if (reader.isReadable() == false) {
        contextDebug() << F_CDB << InfoCdbBackend::databasePath() << "is not readable. Watching dir instead.";
        watchDirectory();
    } else {
        watchPath();
    }
}

/* Slots */

/// Called when the database changes. Reopens the database and emits
/// the change signals. If database does not exist it bails out but keeps observing.
void InfoCdbBackend::onDatabaseFileChanged(const QString &path)
{
    QStringList oldKeys = listKeys();

    contextDebug() << F_CDB << InfoCdbBackend::databasePath() << "changed, re-opening database.";

    reader.reopen();
    watchPathOrDirectory();

    // If nobody is watching us anyways, drop out now and skip
    // the further processing. This could be made more granular
    // (ie. in many cases nobody will be watching on added/removed)
    // but will be watching on changed.
    if (connectCount == 0)
        return;

    QStringList currentKeys = listKeys();

    // Emissions
    checkAndEmitKeysAdded(currentKeys, oldKeys);
    checkAndEmitKeysRemoved(currentKeys, oldKeys);
    emit keysChanged(listKeys());
    checkAndEmitKeysChanged(currentKeys, oldKeys);
}

/// Called when the directory with cache.db chanes. We start to observe this
/// directory only when we don't have the cache.db in the first place.
void InfoCdbBackend::onDatabaseDirectoryChanged(const QString &path)
{
    if (reader.isReadable())
        return;

    onDatabaseFileChanged(path);
}