Commit c9e88a96 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer

Introduced settings database based on sqlite

Potential replacement for QSettings. This database will allow us to save
settings incrementially, removing most of the performance issues we're
having when our few megabytes of settings are being saved.

Not done in the form of a QSettings backend because it doesn't seem the
backends can do an incremental update of the settings.

Currently used by the QuickOpenPlugin for testing.
parent 8951302a
......@@ -129,6 +129,11 @@ QSettings *CoreImpl::settings() const
return m_mainwindow->settings();
}
SettingsDatabase *CoreImpl::settingsDatabase() const
{
return m_mainwindow->settingsDatabase();
}
QPrinter *CoreImpl::printer() const
{
return m_mainwindow->printer();
......
......@@ -64,6 +64,7 @@ public:
MimeDatabase *mimeDatabase() const;
QSettings *settings() const;
SettingsDatabase *settingsDatabase() const;
QPrinter *printer() const;
QString resourcePath() const;
......
......@@ -4,7 +4,8 @@ DEFINES += CORE_LIBRARY
QT += xml \
network \
script \
svg
svg \
sql
include(../../qtcreatorplugin.pri)
include(../../libs/utils/utils.pri)
include(../../shared/scriptwrapper/scriptwrapper.pri)
......@@ -77,7 +78,8 @@ SOURCES += mainwindow.cpp \
icore.cpp \
editormanager/ieditor.cpp \
dialogs/ioptionspage.cpp \
dialogs/iwizard.cpp
dialogs/iwizard.cpp \
settingsdatabase.cpp
HEADERS += mainwindow.h \
welcomemode.h \
welcomemode_p.h \
......@@ -156,7 +158,8 @@ HEADERS += mainwindow.h \
rightpane.h \
sidebar.h \
fileiconprovider.h \
mimedatabase.h
mimedatabase.h \
settingsdatabase.h
FORMS += dialogs/newdialog.ui \
dialogs/settingsdialog.ui \
dialogs/shortcutsettings.ui \
......
......@@ -52,6 +52,7 @@ class MimeDatabase;
class ModeManager;
class ProgressManager;
class ScriptManager;
class SettingsDatabase;
class UniqueIDManager;
class VariableManager;
class VCSManager;
......@@ -87,6 +88,7 @@ public:
virtual MimeDatabase *mimeDatabase() const = 0;
virtual QSettings *settings() const = 0;
virtual SettingsDatabase *settingsDatabase() const = 0;
virtual QPrinter *printer() const = 0;
virtual QString resourcePath() const = 0;
......
......@@ -66,6 +66,7 @@
#include "ioutputpane.h"
#include <coreplugin/findplaceholder.h>
#include <coreplugin/settingsdatabase.h>
#include <utils/pathchooser.h>
#include <extensionsystem/pluginmanager.h>
......@@ -111,7 +112,11 @@ MainWindow::MainWindow() :
m_uniqueIDManager(new UniqueIDManager()),
m_globalContext(QList<int>() << Constants::C_GLOBAL_ID),
m_additionalContexts(m_globalContext),
m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope, QLatin1String("Nokia"), QLatin1String("QtCreator"), this)),
m_settings(new QSettings(QSettings::IniFormat, QSettings::UserScope,
QLatin1String("Nokia"), QLatin1String("QtCreator"), this)),
m_settingsDatabase(new SettingsDatabase(QFileInfo(m_settings->fileName()).path(),
QLatin1String("QtCreator"),
this)),
m_printer(0),
m_actionManager(new ActionManagerPrivate(this)),
m_editorManager(0),
......
......@@ -60,6 +60,7 @@ class ModeManager;
class ProgressManager;
class RightPaneWidget;
class ScriptManager;
class SettingsDatabase;
class UniqueIDManager;
class VariableManager;
class VCSManager;
......@@ -109,6 +110,7 @@ public:
VCSManager *vcsManager() const;
inline QSettings *settings() const { return m_settings; }
inline SettingsDatabase *settingsDatabase() const { return m_settingsDatabase; }
virtual QPrinter *printer() const;
IContext * currentContextObject() const;
QStatusBar *statusBar() const;
......@@ -169,6 +171,7 @@ private:
QList<int> m_globalContext;
QList<int> m_additionalContexts;
QSettings *m_settings;
SettingsDatabase *m_settingsDatabase;
mutable QPrinter *m_printer;
ActionManagerPrivate *m_actionManager;
EditorManager *m_editorManager;
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/
#include "settingsdatabase.h"
#include <QtCore/QMap>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>
using namespace Core;
using namespace Core::Internal;
enum { debug_settings = 1 };
namespace Core {
namespace Internal {
typedef QMap<QString, QVariant> SettingsMap;
class SettingsDatabasePrivate
{
public:
QString effectiveGroup() const
{
return m_groups.join(QLatin1String("/"));
}
QString effectiveKey(const QString &key) const
{
QString g = effectiveGroup();
if (!g.isEmpty())
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
QString fileName = path;
if (!fileName.endsWith(slash))
fileName += slash;
fileName += application;
fileName += QLatin1String(".db");
d->m_db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("settings"));
d->m_db.setDatabaseName(fileName);
if (!d->m_db.open())
qWarning() << "Warning: Failed to open settings database!";
// 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 (d->m_db.isOpen() && !query.exec())
qWarning() << "Warning: Failed to prepare settings database!";
// 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());
}
}
}
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);
// 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();
} else {
// 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)
{
Q_UNUSED(key);
// TODO: Remove key and all subkeys
}
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
{
QStringList childs;
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) {
childs.append(key.mid(g.length() + 1));
}
}
return childs;
}
void SettingsDatabase::sync()
{
// TODO: Delay writing of dirty keys and save them here
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/
#ifndef SETTINGSDATABASE_H
#define SETTINGSDATABASE_H
#include "core_global.h"
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
namespace Core {
namespace Internal {
class SettingsDatabasePrivate;
}
class CORE_EXPORT SettingsDatabase : public QObject
{
public:
SettingsDatabase(const QString &path, const QString &application, QObject *parent = 0);
~SettingsDatabase();
void setValue(const QString &key, const QVariant &value);
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
bool contains(const QString &key) const;
void remove(const QString &key);
void beginGroup(const QString &prefix);
void endGroup();
QString group() const;
QStringList childKeys() const;
void sync();
private:
Internal::SettingsDatabasePrivate *d;
};
} // namespace Core
#endif // SETTINGSDATABASE_H
......@@ -38,12 +38,12 @@
#include "settingspage.h"
#include <QtCore/QtPlugin>
#include <QtCore/QSettings>
#include <QtCore/QFuture>
#include <QtCore/QFutureWatcher>
#include <coreplugin/baseview.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/settingsdatabase.h>
#include <coreplugin/icore.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -145,27 +145,27 @@ void QuickOpenPlugin::startSettingsLoad()
void QuickOpenPlugin::loadSettings()
{
QSettings settings;
settings.beginGroup("QuickOpen");
m_refreshTimer.setInterval(settings.value("RefreshInterval", 60).toInt()*60000);
Core::SettingsDatabase *settings = Core::ICore::instance()->settingsDatabase();
settings->beginGroup("QuickOpen");
m_refreshTimer.setInterval(settings->value("RefreshInterval", 60).toInt() * 60000);
foreach (IQuickOpenFilter *filter, m_filters) {
if (settings.contains(filter->name())) {
const QByteArray state = settings.value(filter->name()).toByteArray();
if (settings->contains(filter->name())) {
const QByteArray state = settings->value(filter->name()).toByteArray();
if (!state.isEmpty())
filter->restoreState(state);
}
}
settings.beginGroup("CustomFilters");
settings->beginGroup("CustomFilters");
QList<IQuickOpenFilter *> customFilters;
foreach (const QString &key, settings.childKeys()) {
foreach (const QString &key, settings->childKeys()) {
IQuickOpenFilter *filter = new DirectoryFilter;
filter->restoreState(settings.value(key).toByteArray());
filter->restoreState(settings->value(key).toByteArray());
m_filters.append(filter);
customFilters.append(filter);
}
setCustomFilters(customFilters);
settings.endGroup();
settings.endGroup();
settings->endGroup();
settings->endGroup();
}
void QuickOpenPlugin::settingsLoaded()
......@@ -178,8 +178,8 @@ void QuickOpenPlugin::settingsLoaded()
void QuickOpenPlugin::saveSettings()
{
Core::ICore *core = Core::ICore::instance();
if (core && core->settings()) {
QSettings *s = core->settings();
if (core && core->settingsDatabase()) {
Core::SettingsDatabase *s = core->settingsDatabase();
s->beginGroup("QuickOpen");
s->setValue("Interval", m_refreshTimer.interval() / 60000);
s->remove("");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment