Mercurial > hg > svcore
changeset 156:059b0322009c
* Replace all uses of ConfigFile with QSettings
author | Chris Cannam |
---|---|
date | Thu, 03 Aug 2006 16:04:00 +0000 |
parents | ae9be6b6b522 |
children | c03ec31005e1 |
files | base/ConfigFile.cpp base/ConfigFile.h base/Preferences.cpp base/Preferences.h base/RecentFiles.cpp base/base.pro |
diffstat | 6 files changed, 49 insertions(+), 321 deletions(-) [+] |
line wrap: on
line diff
--- a/base/ConfigFile.cpp Thu Aug 03 15:40:11 2006 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,181 +0,0 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ - -/* - Sonic Visualiser - An audio file viewer and annotation editor. - Centre for Digital Music, Queen Mary, University of London. - This file copyright 2006 Chris Cannam. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. See the file - COPYING included with this distribution for more information. -*/ - -#include "ConfigFile.h" - -#include "Exceptions.h" - -#include <iostream> - -#include <QFile> -#include <QMutexLocker> -#include <QTextStream> -#include <QStringList> - -ConfigFile::ConfigFile(QString filename) : - m_filename(filename), - m_loaded(false), - m_modified(false) -{ -} - -ConfigFile::~ConfigFile() -{ - try { - commit(); - } catch (FileOperationFailed f) { - std::cerr << "WARNING: ConfigFile::~ConfigFile: Commit failed for " - << m_filename.toStdString() << std::endl; - } -} - -QString -ConfigFile::get(QString key, QString deft) -{ - if (!m_loaded) load(); - - QMutexLocker locker(&m_mutex); - - if (m_data.find(key) == m_data.end()) return deft; - return m_data[key]; -} - -int -ConfigFile::getInt(QString key, int deft) -{ - return get(key, QString("%1").arg(deft)).toInt(); -} - -bool -ConfigFile::getBool(QString key, bool deft) -{ - QString value = get(key, deft ? "true" : "false").trimmed().toLower(); - return (value == "true" || value == "yes" || value == "on" || value == "1"); -} - -float -ConfigFile::getFloat(QString key, float deft) -{ - return get(key, QString("%1").arg(deft)).toFloat(); -} - -QStringList -ConfigFile::getStringList(QString key) -{ - return get(key).split('|'); -} - -void -ConfigFile::set(QString key, QString value) -{ - if (!m_loaded) load(); - - QMutexLocker locker(&m_mutex); - - m_data[key] = value; - - m_modified = true; -} - -void -ConfigFile::set(QString key, int value) -{ - set(key, QString("%1").arg(value)); -} - -void -ConfigFile::set(QString key, bool value) -{ - set(key, value ? QString("true") : QString("false")); -} - -void -ConfigFile::set(QString key, float value) -{ - set(key, QString("%1").arg(value)); -} - -void -ConfigFile::set(QString key, const QStringList &values) -{ - set(key, values.join("|")); -} - -void -ConfigFile::commit() -{ - QMutexLocker locker(&m_mutex); - - if (!m_modified) return; - - // Really we should write to another file and then move to the - // intended target, but I don't think we're all that particular - // about reliability here at the moment - - QFile file(m_filename); - - if (!file.open(QFile::WriteOnly | QFile::Text)) { - throw FileOperationFailed(m_filename, "open for writing"); - } - - QTextStream out(&file); - - for (DataMap::const_iterator i = m_data.begin(); i != m_data.end(); ++i) { - out << i->first << "=" << i->second << endl; - } - - m_modified = false; -} - -bool -ConfigFile::load() -{ - QMutexLocker locker(&m_mutex); - - if (m_loaded) return true; - - QFile file(m_filename); - - if (!file.open(QFile::ReadOnly | QFile::Text)) { - return false; - } - - QTextStream in(&file); - - m_data.clear(); - - while (!in.atEnd()) { - - QString line = in.readLine(2048); - QString key = line.section('=', 0, 0); - QString value = line.section('=', 1, -1); - if (key == "") continue; - - m_data[key] = value; - } - - m_loaded = true; - m_modified = false; - return true; -} - -void -ConfigFile::reset() -{ - QMutexLocker locker(&m_mutex); - m_loaded = false; - m_modified = false; -} -
--- a/base/ConfigFile.h Thu Aug 03 15:40:11 2006 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ - -/* - Sonic Visualiser - An audio file viewer and annotation editor. - Centre for Digital Music, Queen Mary, University of London. - This file copyright 2006 Chris Cannam. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. See the file - COPYING included with this distribution for more information. -*/ - -#ifndef _CONFIG_FILE_H_ -#define _CONFIG_FILE_H_ - -#include <QString> -#include <QMutex> - -#include <map> - -class ConfigFile -{ -public: - ConfigFile(QString filename); - virtual ~ConfigFile(); - - /** - * Get a value, with a default if it hasn't been set. - */ - QString get(QString key, QString deft = ""); - - bool getBool(QString key, bool deft); - - int getInt(QString key, int deft); - - float getFloat(QString key, float deft); - - QStringList getStringList(QString key); - - /** - * Set a value. Values must not contain carriage return or other - * non-printable characters. Keys must contain [a-zA-Z0-9_-] only. - */ - void set(QString key, QString value); - - void set(QString key, bool value); - - void set(QString key, int value); - - void set(QString key, float value); - - void set(QString key, const QStringList &values); // must not contain '|' - - /** - * Write the data to file. May throw FileOperationFailed. - * - * This is called automatically on destruction if any data has - * changed since it was last called. At that time, any exception - * will be ignored. If you want to ensure that exceptions are - * handled, call it yourself before destruction. - */ - void commit(); - - /** - * Return to the stored values. You can also call this before - * destruction if you want to ensure that any values modified so - * far are not written out to file on destruction. - */ - void reset(); - -protected: - bool load(); - - QString m_filename; - - typedef std::map<QString, QString> DataMap; - DataMap m_data; - - bool m_loaded; - bool m_modified; - - QMutex m_mutex; -}; - -#endif -
--- a/base/Preferences.cpp Thu Aug 03 15:40:11 2006 +0000 +++ b/base/Preferences.cpp Thu Aug 03 16:04:00 2006 +0000 @@ -19,53 +19,40 @@ #include "TempDirectory.h" -#include "ConfigFile.h" //!!! reorg - #include <QDir> #include <QFileInfo> +#include <QMutex> +#include <QSettings> Preferences * -Preferences::m_instance = new Preferences(); +Preferences::m_instance = 0; + +Preferences * +Preferences::getInstance() +{ + if (!m_instance) m_instance = new Preferences(); + return m_instance; +} Preferences::Preferences() : m_smoothSpectrogram(true), m_tuningFrequency(440), m_propertyBoxLayout(VerticallyStacked), - m_windowType(HanningWindow), - m_configFile(0) + m_windowType(HanningWindow) { - // Let TempDirectory do its thing first, so as to avoid any race - // condition if it happens that we both want to use the same directory - TempDirectory::getInstance(); - - QString svDirBase = ".sv1"; - QString svDir = QDir::home().filePath(svDirBase); - if (!QFileInfo(svDir).exists()) { - if (!QDir::home().mkdir(svDirBase)) { - throw DirectoryCreationFailed(QString("%1 directory in $HOME") - .arg(svDirBase)); - } - } else if (!QFileInfo(svDir).isDir()) { - throw DirectoryCreationFailed(QString("$HOME/%1 is not a directory") - .arg(svDirBase)); - } - - m_configFile = new ConfigFile(QDir(svDir).filePath("preferences.cfg")); - - m_smoothSpectrogram = - m_configFile->getBool("preferences-smooth-spectrogram", true); - m_tuningFrequency = - m_configFile->getFloat("preferences-tuning-frequency", 440.f); + QSettings settings; + settings.beginGroup("Preferences"); + m_smoothSpectrogram = settings.value("smooth-spectrogram", true).toBool(); + m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble(); m_propertyBoxLayout = PropertyBoxLayout - (m_configFile->getInt("preferences-property-box-layout", - int(VerticallyStacked))); + (settings.value("property-box-layout", int(VerticallyStacked)).toInt()); m_windowType = WindowType - (m_configFile->getInt("preferences-window-type", int(HanningWindow))); + (settings.value("window-type", int(HanningWindow)).toInt()); + settings.endGroup(); } Preferences::~Preferences() { - delete m_configFile; } Preferences::PropertyList @@ -197,7 +184,10 @@ { if (m_smoothSpectrogram != smooth) { m_smoothSpectrogram = smooth; - m_configFile->set("preferences-smooth-spectrogram", smooth); + QSettings settings; + settings.beginGroup("Preferences"); + settings.setValue("smooth-spectrogram", smooth); + settings.endGroup(); emit propertyChanged("Smooth Spectrogram"); } } @@ -207,7 +197,10 @@ { if (m_tuningFrequency != freq) { m_tuningFrequency = freq; - m_configFile->set("preferences-tuning-frequency", freq); + QSettings settings; + settings.beginGroup("Preferences"); + settings.setValue("tuning-frequency", freq); + settings.endGroup(); emit propertyChanged("Tuning Frequency"); } } @@ -217,7 +210,10 @@ { if (m_propertyBoxLayout != layout) { m_propertyBoxLayout = layout; - m_configFile->set("preferences-property-box-layout", int(layout)); + QSettings settings; + settings.beginGroup("Preferences"); + settings.setValue("property-box-layout", int(layout)); + settings.endGroup(); emit propertyChanged("Property Box Layout"); } } @@ -227,7 +223,10 @@ { if (m_windowType != type) { m_windowType = type; - m_configFile->set("preferences-window-type", int(type)); + QSettings settings; + settings.beginGroup("Preferences"); + settings.setValue("window-type", int(type)); + settings.endGroup(); emit propertyChanged("Window Type"); } }
--- a/base/Preferences.h Thu Aug 03 15:40:11 2006 +0000 +++ b/base/Preferences.h Thu Aug 03 16:04:00 2006 +0000 @@ -20,14 +20,12 @@ #include "Window.h" -class ConfigFile; - class Preferences : public PropertyContainer { Q_OBJECT public: - static Preferences *getInstance() { return m_instance; } + static Preferences *getInstance(); virtual PropertyList getProperties() const; virtual QString getPropertyLabel(const PropertyName &) const; @@ -41,8 +39,6 @@ float getTuningFrequency() const { return m_tuningFrequency; } WindowType getWindowType() const { return m_windowType; } - ConfigFile *getConfigFile() { return m_configFile; } - //!!! harmonise with PaneStack enum PropertyBoxLayout { VerticallyStacked, @@ -68,7 +64,6 @@ float m_tuningFrequency; PropertyBoxLayout m_propertyBoxLayout; WindowType m_windowType; - ConfigFile *m_configFile; }; #endif
--- a/base/RecentFiles.cpp Thu Aug 03 15:40:11 2006 +0000 +++ b/base/RecentFiles.cpp Thu Aug 03 16:04:00 2006 +0000 @@ -14,11 +14,11 @@ */ #include "RecentFiles.h" -#include "ConfigFile.h" #include "Preferences.h" #include <QFileInfo> +#include <QSettings> RecentFiles * RecentFiles::m_instance = 0; @@ -47,28 +47,34 @@ RecentFiles::readFiles() { m_files.clear(); - ConfigFile *cf = Preferences::getInstance()->getConfigFile(); + QSettings settings; + settings.beginGroup("RecentFiles"); + for (unsigned int i = 0; i < 100; ++i) { QString key = QString("recent-file-%1").arg(i); - QString filename = cf->get(key); + QString filename = settings.value(key, "").toString(); if (filename == "") break; if (i < m_maxFileCount) m_files.push_back(filename); - else cf->set(key, ""); + else settings.setValue(key, ""); } - cf->commit(); + + settings.endGroup(); } void RecentFiles::writeFiles() { - ConfigFile *cf = Preferences::getInstance()->getConfigFile(); + QSettings settings; + settings.beginGroup("RecentFiles"); + for (unsigned int i = 0; i < m_maxFileCount; ++i) { QString key = QString("recent-file-%1").arg(i); QString filename = ""; if (i < m_files.size()) filename = m_files[i]; - cf->set(key, filename); + settings.setValue(key, filename); } - cf->commit(); + + settings.endGroup(); } void
--- a/base/base.pro Thu Aug 03 15:40:11 2006 +0000 +++ b/base/base.pro Thu Aug 03 16:04:00 2006 +0000 @@ -18,7 +18,6 @@ Clipboard.h \ Command.h \ CommandHistory.h \ - ConfigFile.h \ Exceptions.h \ Pitch.h \ PlayParameterRepository.h \ @@ -42,7 +41,6 @@ Clipboard.cpp \ Command.cpp \ CommandHistory.cpp \ - ConfigFile.cpp \ Exceptions.cpp \ Pitch.cpp \ PlayParameterRepository.cpp \