2009-05-19 19:20:53 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2009-05-19 19:20:53 +02:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2009-05-19 19:20:53 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2009-05-19 19:20:53 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2009-05-19 19:20:53 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "settingsdatabase.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QVariant>
|
2009-10-01 16:38:08 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSqlDatabase>
|
|
|
|
|
#include <QSqlError>
|
|
|
|
|
#include <QSqlQuery>
|
|
|
|
|
#include <QDebug>
|
2009-05-19 19:20:53 +02:00
|
|
|
|
2009-05-20 10:55:27 +02:00
|
|
|
/*!
|
|
|
|
|
\class Core::SettingsDatabase
|
|
|
|
|
\brief An alternative to the application-wide QSettings that is more
|
|
|
|
|
suitable for storing large amounts of data.
|
|
|
|
|
|
|
|
|
|
The settings database is SQLite based, and lazily retrieves data when it
|
|
|
|
|
is asked for. It also does incremental updates of the database rather than
|
|
|
|
|
rewriting the whole file each time one of the settings change.
|
|
|
|
|
|
|
|
|
|
The SettingsDatabase API mimics that of QSettings.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-05-19 19:20:53 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
2009-05-20 17:15:59 +02:00
|
|
|
enum { debug_settings = 0 };
|
2009-05-19 19:20:53 +02:00
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
typedef QMap<QString, QVariant> SettingsMap;
|
|
|
|
|
|
|
|
|
|
class SettingsDatabasePrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QString effectiveGroup() const
|
|
|
|
|
{
|
2010-02-01 12:43:56 +01:00
|
|
|
return m_groups.join(QString(QLatin1Char('/')));
|
2009-05-19 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString effectiveKey(const QString &key) const
|
|
|
|
|
{
|
|
|
|
|
QString g = effectiveGroup();
|
2009-05-20 17:15:59 +02:00
|
|
|
if (!g.isEmpty() && !key.isEmpty())
|
2009-05-19 19:20:53 +02:00
|
|
|
g += QLatin1Char('/');
|
|
|
|
|
g += key;
|
|
|
|
|
return g;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SettingsMap m_settings;
|
|
|
|
|
|
|
|
|
|
QStringList m_groups;
|
|
|
|
|
QStringList m_dirtyKeys;
|
|
|
|
|
|
|
|
|
|
QSqlDatabase m_db;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Core
|
|
|
|
|
|
|
|
|
|
SettingsDatabase::SettingsDatabase(const QString &path,
|
|
|
|
|
const QString &application,
|
|
|
|
|
QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, d(new SettingsDatabasePrivate)
|
|
|
|
|
{
|
|
|
|
|
const QLatin1Char slash('/');
|
|
|
|
|
|
|
|
|
|
// TODO: Don't rely on a path, but determine automatically
|
2009-06-12 14:51:01 +02:00
|
|
|
QDir pathDir(path);
|
|
|
|
|
if (!pathDir.exists())
|
|
|
|
|
pathDir.mkpath(pathDir.absolutePath());
|
|
|
|
|
|
2009-05-19 19:20:53 +02:00
|
|
|
QString fileName = path;
|
|
|
|
|
if (!fileName.endsWith(slash))
|
|
|
|
|
fileName += slash;
|
|
|
|
|
fileName += application;
|
|
|
|
|
fileName += QLatin1String(".db");
|
|
|
|
|
|
2011-12-22 14:44:14 +01:00
|
|
|
d->m_db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), QLatin1String("settings"));
|
2009-05-19 19:20:53 +02:00
|
|
|
d->m_db.setDatabaseName(fileName);
|
2009-06-05 12:40:00 +02:00
|
|
|
if (!d->m_db.open()) {
|
|
|
|
|
qWarning().nospace() << "Warning: Failed to open settings database at " << fileName << " ("
|
|
|
|
|
<< d->m_db.lastError().driverText() << ")";
|
|
|
|
|
} else {
|
|
|
|
|
// Create the settings table if it doesn't exist yet
|
|
|
|
|
QSqlQuery query(d->m_db);
|
|
|
|
|
query.prepare(QLatin1String("CREATE TABLE IF NOT EXISTS settings ("
|
|
|
|
|
"key PRIMARY KEY ON CONFLICT REPLACE, "
|
|
|
|
|
"value)"));
|
|
|
|
|
if (!query.exec())
|
|
|
|
|
qWarning().nospace() << "Warning: Failed to prepare settings database! ("
|
|
|
|
|
<< query.lastError().driverText() << ")";
|
|
|
|
|
|
|
|
|
|
// Retrieve all available keys (values are retrieved lazily)
|
|
|
|
|
if (query.exec(QLatin1String("SELECT key FROM settings"))) {
|
|
|
|
|
while (query.next()) {
|
|
|
|
|
d->m_settings.insert(query.value(0).toString(), QVariant());
|
|
|
|
|
}
|
2009-05-19 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SettingsDatabase::~SettingsDatabase()
|
|
|
|
|
{
|
|
|
|
|
sync();
|
|
|
|
|
|
|
|
|
|
delete d;
|
|
|
|
|
QSqlDatabase::removeDatabase(QLatin1String("settings"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SettingsDatabase::setValue(const QString &key, const QVariant &value)
|
|
|
|
|
{
|
|
|
|
|
const QString effectiveKey = d->effectiveKey(key);
|
|
|
|
|
|
|
|
|
|
// Add to cache
|
|
|
|
|
d->m_settings.insert(effectiveKey, value);
|
|
|
|
|
|
2009-06-05 12:40:00 +02:00
|
|
|
if (!d->m_db.isOpen())
|
|
|
|
|
return;
|
|
|
|
|
|
2009-05-19 19:20:53 +02:00
|
|
|
// Instant apply (TODO: Delay writing out settings)
|
|
|
|
|
QSqlQuery query(d->m_db);
|
|
|
|
|
query.prepare(QLatin1String("INSERT INTO settings VALUES (?, ?)"));
|
|
|
|
|
query.addBindValue(effectiveKey);
|
|
|
|
|
query.addBindValue(value);
|
|
|
|
|
query.exec();
|
|
|
|
|
|
|
|
|
|
if (debug_settings)
|
|
|
|
|
qDebug() << "Stored:" << effectiveKey << "=" << value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant SettingsDatabase::value(const QString &key, const QVariant &defaultValue) const
|
|
|
|
|
{
|
|
|
|
|
const QString effectiveKey = d->effectiveKey(key);
|
|
|
|
|
QVariant value = defaultValue;
|
|
|
|
|
|
|
|
|
|
SettingsMap::const_iterator i = d->m_settings.constFind(effectiveKey);
|
|
|
|
|
if (i != d->m_settings.constEnd() && i.value().isValid()) {
|
|
|
|
|
value = i.value();
|
2009-06-05 12:40:00 +02:00
|
|
|
} else if (d->m_db.isOpen()) {
|
2009-05-19 19:20:53 +02:00
|
|
|
// Try to read the value from the database
|
|
|
|
|
QSqlQuery query(d->m_db);
|
|
|
|
|
query.prepare(QLatin1String("SELECT value FROM settings WHERE key = ?"));
|
|
|
|
|
query.addBindValue(effectiveKey);
|
|
|
|
|
query.exec();
|
|
|
|
|
if (query.next()) {
|
|
|
|
|
value = query.value(0);
|
|
|
|
|
|
|
|
|
|
if (debug_settings)
|
|
|
|
|
qDebug() << "Retrieved:" << effectiveKey << "=" << value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cache the result
|
|
|
|
|
d->m_settings.insert(effectiveKey, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SettingsDatabase::contains(const QString &key) const
|
|
|
|
|
{
|
|
|
|
|
return d->m_settings.contains(d->effectiveKey(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SettingsDatabase::remove(const QString &key)
|
|
|
|
|
{
|
2009-05-20 17:15:59 +02:00
|
|
|
const QString effectiveKey = d->effectiveKey(key);
|
|
|
|
|
|
|
|
|
|
// Remove keys from the cache
|
|
|
|
|
foreach (const QString &k, d->m_settings.keys()) {
|
|
|
|
|
// Either it's an exact match, or it matches up to a /
|
|
|
|
|
if (k.startsWith(effectiveKey)
|
|
|
|
|
&& (k.length() == effectiveKey.length()
|
|
|
|
|
|| k.at(effectiveKey.length()) == QLatin1Char('/')))
|
|
|
|
|
{
|
|
|
|
|
d->m_settings.remove(k);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-05 12:40:00 +02:00
|
|
|
|
|
|
|
|
if (!d->m_db.isOpen())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Delete keys from the database
|
|
|
|
|
QSqlQuery query(d->m_db);
|
|
|
|
|
query.prepare(QLatin1String("DELETE FROM settings WHERE key = ? OR key LIKE ?"));
|
|
|
|
|
query.addBindValue(effectiveKey);
|
2009-06-08 15:54:05 +02:00
|
|
|
query.addBindValue(QString(effectiveKey + QLatin1String("/%")));
|
2009-06-05 12:40:00 +02:00
|
|
|
query.exec();
|
2009-05-19 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SettingsDatabase::beginGroup(const QString &prefix)
|
|
|
|
|
{
|
|
|
|
|
d->m_groups.append(prefix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SettingsDatabase::endGroup()
|
|
|
|
|
{
|
|
|
|
|
d->m_groups.removeLast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SettingsDatabase::group() const
|
|
|
|
|
{
|
|
|
|
|
return d->effectiveGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList SettingsDatabase::childKeys() const
|
|
|
|
|
{
|
2010-01-11 10:22:55 +01:00
|
|
|
QStringList children;
|
2009-05-19 19:20:53 +02:00
|
|
|
|
|
|
|
|
const QString g = group();
|
|
|
|
|
QMapIterator<QString, QVariant> i(d->m_settings);
|
|
|
|
|
while (i.hasNext()) {
|
|
|
|
|
const QString &key = i.next().key();
|
|
|
|
|
if (key.startsWith(g) && key.indexOf(QLatin1Char('/'), g.length() + 1) == -1) {
|
2010-01-11 10:22:55 +01:00
|
|
|
children.append(key.mid(g.length() + 1));
|
2009-05-19 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 10:22:55 +01:00
|
|
|
return children;
|
2009-05-19 19:20:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SettingsDatabase::sync()
|
|
|
|
|
{
|
|
|
|
|
// TODO: Delay writing of dirty keys and save them here
|
|
|
|
|
}
|