diff options
author | Gergely Risko <gergely+context@risko.hu> | 2009-09-16 11:05:05 +0300 |
---|---|---|
committer | Gergely Risko <gergely+context@risko.hu> | 2009-09-16 11:05:05 +0300 |
commit | 97f304f084401c23db06de7e899230469f9c4c3b (patch) | |
tree | e973444d7a9b9944cb23ccfe050bd311edfa9383 /libcontextsubscriber | |
parent | 3b5251a8f9c8b02e2b9e27673dcea5e5cbb1f078 (diff) |
Refactored CDB things to store serialized QVariants as values instead of QStrings.
Diffstat (limited to 'libcontextsubscriber')
-rw-r--r-- | libcontextsubscriber/src/cdbreader.cpp | 71 | ||||
-rw-r--r-- | libcontextsubscriber/src/cdbreader.h | 7 | ||||
-rw-r--r-- | libcontextsubscriber/src/cdbwriter.cpp | 28 | ||||
-rw-r--r-- | libcontextsubscriber/src/cdbwriter.h | 10 | ||||
-rw-r--r-- | libcontextsubscriber/src/infocdbbackend.cpp | 28 | ||||
-rw-r--r-- | libcontextsubscriber/src/infocdbbackend.h | 1 |
6 files changed, 73 insertions, 72 deletions
diff --git a/libcontextsubscriber/src/cdbreader.cpp b/libcontextsubscriber/src/cdbreader.cpp index 7955776c..42bdb865 100644 --- a/libcontextsubscriber/src/cdbreader.cpp +++ b/libcontextsubscriber/src/cdbreader.cpp @@ -23,7 +23,9 @@ #include <cdb.h> #include <fcntl.h> #include <QDebug> +#include <QByteArray> #include <QFile> +#include <QDataStream> #include "cdbreader.h" #include "logging.h" #include "loggingfeatures.h" @@ -34,12 +36,13 @@ \brief A wrapper class to read data from a tiny-cdb database. - This class is not a part of the public API. - The reader operates only on strings and can read a string value for a string key - or a list of string values for a string key. The reader automatically closes the - underlying filesystem resource on destruction but can be also closed manually. + This class is not a part of the public API. The reader operates + on string-qvariant pairs: can read a qvariant value for a string + key or a list of qvariant values for a string key. The reader + automatically closes the underlying filesystem resource on + destruction but can be also closed manually. - Reading from a closed reader with return empty strings. + Reading from a closed reader will return empty strings. */ /// Constructs a new CDBReader reading from cdb database at \a dbpath @@ -84,7 +87,7 @@ void CDBReader::reopen() if (! QFile::exists(path)) return; - fd = open(path.toUtf8().constData(), O_RDONLY); + fd = open(path.toLocal8Bit().constData(), O_RDONLY); if (fd > 0) { cdb = calloc(1, sizeof(struct cdb)); @@ -95,45 +98,16 @@ void CDBReader::reopen() } } -/// Returns a string value for the given \a key. -/// First value is returned if there are many values for one key. -/// \param key The key name in the database. -QString CDBReader::valueForKey(const QString &key) const -{ - if (! cdb) { - contextDebug() << F_CDB << "Trying to read value key:" << key << "from database that is closed!"; - return ""; - } - - QByteArray utf8Data = key.toUtf8(); - unsigned int klen = utf8Data.size(); - const char *kval = utf8Data.constData(); - - if (cdb_find((struct cdb*) cdb, kval, klen)) { - unsigned int vpos = cdb_datapos((struct cdb*) cdb); - unsigned int vlen = cdb_datalen((struct cdb*) cdb); - char *val = (char *) malloc(vlen + 1); - cdb_read((struct cdb*) cdb, val, vlen, vpos); - val[vlen] = 0; - - QString str(val); - free(val); - return str; - } else - return ""; -} - /// Returns all values associated with the given key from the database. /// \param key The key name in the database. -QStringList CDBReader::valuesForKey(const QString &key) const +QVariantList CDBReader::valuesForKey(const QString &key) const { - QStringList list; - if (! cdb) { contextDebug() << F_CDB << "Trying to read values for key:" << key << "from database that is closed!"; - return list; + return QVariantList(); } + QVariantList list; QByteArray utf8Data = key.toUtf8(); unsigned int klen = utf8Data.size(); const char *kval = utf8Data.constData(); @@ -144,18 +118,29 @@ QStringList CDBReader::valuesForKey(const QString &key) const while(cdb_findnext(&cdbf) > 0) { unsigned int vpos = cdb_datapos((struct cdb*) cdb); unsigned int vlen = cdb_datalen((struct cdb*) cdb); - char *val = (char *) malloc(vlen + 1); + char *val = (char *) malloc(vlen); cdb_read((struct cdb*) cdb, val, vlen, vpos); - val[vlen] = 0; - - QString str(val); + QByteArray valArray(val, vlen); + QDataStream ds(&valArray, QIODevice::ReadOnly); + QVariant valVariant; + ds >> valVariant; + list << valVariant; free(val); - list << str; } return list; } +/// Returns a value for the given \a key. +/// First value is returned if there are many values for one key. +/// \param key The key name in the database. +QVariant CDBReader::valueForKey(const QString &key) const +{ + QVariantList ret = valuesForKey(key); + if (ret.size() == 0) return QVariant(); + return ret[0]; +} + /// Returns the current state of the reader. Reader is not readable if /// it was created with a path that doesn't exist or if it was closed. bool CDBReader::isReadable() diff --git a/libcontextsubscriber/src/cdbreader.h b/libcontextsubscriber/src/cdbreader.h index adcbe840..51330c3b 100644 --- a/libcontextsubscriber/src/cdbreader.h +++ b/libcontextsubscriber/src/cdbreader.h @@ -24,6 +24,9 @@ #include <QStringList> #include <QObject> +#include <QVariant> +#include <QVariantList> +#include <QByteArray> class CDBReader : public QObject { @@ -35,8 +38,8 @@ public: void close(); void reopen(); - QString valueForKey(const QString &key) const; - QStringList valuesForKey(const QString &key) const; + QVariantList valuesForKey(const QString &key) const; + QVariant valueForKey(const QString &key) const; bool isReadable(); int fileDescriptor() const; diff --git a/libcontextsubscriber/src/cdbwriter.cpp b/libcontextsubscriber/src/cdbwriter.cpp index 0f706e78..cf04a811 100644 --- a/libcontextsubscriber/src/cdbwriter.cpp +++ b/libcontextsubscriber/src/cdbwriter.cpp @@ -31,10 +31,11 @@ \brief A wrapper class to write data (create) tiny-cdb databases. - This class is not a part of the public API. - The writer operates only on strings. String values can be associated with string keys. - Several operations are supported - ading, replacing and inserting. Those operations differ - in how they handle existing keys with same name. + This class is not a part of the public API. The writer operates + on string-qvariant pairs. QVariant values can be associated with + string keys. Several operations are supported - adding, replacing + and inserting. Those operations differ in how they handle existing + keys with same name. The writer automatically cloes the filesystem resource on destruction but can be also closed manually. Writing to a closed writer has no effect. @@ -87,7 +88,7 @@ CDBWriter::~CDBWriter() /// for this key already exists, another one is added. /// \param key Key name as string. /// \param val Value as string. -void CDBWriter::add(const QString &key, const QString &val) +void CDBWriter::add(const QString &key, const QVariant &val) { put(key, val, CDB_PUT_ADD); } @@ -96,7 +97,7 @@ void CDBWriter::add(const QString &key, const QString &val) /// for this key already exists, nothing is done. /// \param key Key name as string. /// \param val Value as string. -void CDBWriter::insert(const QString &key, const QString &val) +void CDBWriter::insert(const QString &key, const QVariant &val) { put(key, val, CDB_PUT_INSERT); } @@ -106,7 +107,7 @@ void CDBWriter::insert(const QString &key, const QString &val) /// new one. /// \param key Key name as string. /// \param val Value as string. -void CDBWriter::replace(const QString &key, const QString &val) +void CDBWriter::replace(const QString &key, const QVariant &val) { put(key, val, CDB_PUT_REPLACE); } @@ -145,22 +146,23 @@ bool CDBWriter::isWritable() /// Puts a new \a key with value \a val into the database. Depending /// on the \a flag the key is added, inserted or replaced. The public /// methods of this class are wrapperes of this method with proper flags. -void CDBWriter::put(const QString &key, const QString &val, int flag) +void CDBWriter::put(const QString &key, const QVariant &val, int flag) { if (! cdbm) return; QByteArray keyUtf8Data = key.toUtf8(); - QByteArray valUtf8Data = val.toUtf8(); - int klen = keyUtf8Data.size(); const char *kval = keyUtf8Data.constData(); - int vlen = valUtf8Data.size(); - const char *vval = valUtf8Data.constData(); + + QByteArray valArray; + QDataStream ds(&valArray, QIODevice::Truncate | QIODevice::WriteOnly); + ds << val; + int vlen = valArray.size(); + const char *vval = valArray.constData(); cdb_make_put((struct cdb_make *) cdbm, kval, klen, vval, vlen, (cdb_put_mode) flag); } - diff --git a/libcontextsubscriber/src/cdbwriter.h b/libcontextsubscriber/src/cdbwriter.h index 3ab5cc51..b7067612 100644 --- a/libcontextsubscriber/src/cdbwriter.h +++ b/libcontextsubscriber/src/cdbwriter.h @@ -23,6 +23,8 @@ #define CDBWRITER_H #include <QObject> +#include <QString> +#include <QVariant> class CDBWriter : public QObject { @@ -33,9 +35,9 @@ public: explicit CDBWriter(int fd, QObject *parent = 0); virtual ~CDBWriter(); - void add(const QString &key, const QString &val); - void insert(const QString &key, const QString &val); - void replace(const QString &key, const QString &val); + void add(const QString &key, const QVariant &val); + void insert(const QString &key, const QVariant &val); + void replace(const QString &key, const QVariant &val); void close(); bool isWritable(); int fileDescriptor() const; @@ -44,7 +46,7 @@ private: void *cdbm; ///< A cdb library structure used to read data. int fd; ///< A file descriptor pointing to the database. - void put(const QString &key, const QString &val, int flag); + void put(const QString &key, const QVariant &val, int flag); }; #endif diff --git a/libcontextsubscriber/src/infocdbbackend.cpp b/libcontextsubscriber/src/infocdbbackend.cpp index b390b83f..ebb28783 100644 --- a/libcontextsubscriber/src/infocdbbackend.cpp +++ b/libcontextsubscriber/src/infocdbbackend.cpp @@ -46,7 +46,7 @@ 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))); @@ -59,41 +59,49 @@ 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 reader.valuesForKey("KEYS"); + return variantListToStringList(reader.valuesForKey("KEYS")); } QStringList InfoCdbBackend::listKeysForPlugin(QString plugin) const { - return reader.valuesForKey(plugin + ":KEYS"); + return variantListToStringList(reader.valuesForKey(plugin + ":KEYS")); } QStringList InfoCdbBackend::listPlugins() const { - return reader.valuesForKey("PLUGINS"); + return variantListToStringList(reader.valuesForKey("PLUGINS")); } QString InfoCdbBackend::typeForKey(QString key) const { - return reader.valueForKey(key + ":KEYTYPE"); + return reader.valueForKey(key + ":KEYTYPE").toString(); } QString InfoCdbBackend::docForKey(QString key) const { - return reader.valueForKey(key + ":KEYDOC"); + return reader.valueForKey(key + ":KEYDOC").toString(); } QString InfoCdbBackend::pluginForKey(QString key) const { - // FIXME: support the old format - return reader.valueForKey(key + ":KEYPLUGIN"); + return reader.valueForKey(key + ":KEYPLUGIN").toString(); } QString InfoCdbBackend::constructionStringForKey(QString key) const { - // FIXME: support the old format - return reader.valueForKey(key + ":KEYCONSTRUCTIONSTRING"); + return reader.valueForKey(key + ":KEYCONSTRUCTIONSTRING").toString(); } /// Returns true if the database file is present. diff --git a/libcontextsubscriber/src/infocdbbackend.h b/libcontextsubscriber/src/infocdbbackend.h index 1ea223a9..f9f17c06 100644 --- a/libcontextsubscriber/src/infocdbbackend.h +++ b/libcontextsubscriber/src/infocdbbackend.h @@ -55,6 +55,7 @@ private: void watchPathOrDirectory(); void watchDirectory(); void watchPath(); + static QStringList variantListToStringList(const QVariantList &l); private slots: void onDatabaseFileChanged(const QString &path); |