/guitone-1.0rc5/src/util/Settings.cpp
C++ | 309 lines | 244 code | 46 blank | 19 comment | 22 complexity | 3f8147c71f3ec182ccbdab961bad4efd MD5 | raw file
Possible License(s): GPL-3.0
- /***************************************************************************
- * Copyright (C) 2006 by Ingo Maindorfer *
- * ingo@liquidcooling.de *
- * *
- * 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 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program 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 General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program. If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
- #include "Settings.h"
- #include "vocab.h"
- #include <QCoreApplication>
- Settings * Settings::instance = 0;
- Settings * Settings::singleton()
- {
- if (!instance)
- {
- QCoreApplication::setOrganizationName("Thomas Keller");
- QCoreApplication::setOrganizationDomain("thomaskeller.biz");
- QCoreApplication::setApplicationName("guitone");
- instance = new Settings();
- }
- return instance;
- }
- Settings::Settings() : inner(0)
- {
- defaults.insert("AnnotationColorOldestRevision", "#FCFFC0");
- defaults.insert("AnnotationColorNewestRevision", "#FF6609");
- defaults.insert("AnnotationColorHighlight", "#EEEEEE");
- defaults.insert("DiffColorAddedLines", "#90EE90");
- defaults.insert("DiffColorRemovedLines", "#FFCC99");
- defaults.insert("ChangesetBrowserSuspendedBranches", "#AAAAAA");
- defaults.insert("ChangesetBrowserMergeRevisions", "#32CD32");
- defaults.insert("ChangesetBrowserOutOfBranchRevisions", "#AAAAAA");
- defaults.insert("AskFileOpenExt",
- "exe,com,bat,pif,hta,js,jse,inf,lnk," // Windows
- "scr,wsc,wsf,wsh,vb,vbe,vbs,vbscript," // Windows, continued
- "sh,bsh,zsh,csh,ksh,py,pl,php,php3," // Unices
- "php4,rb,ruby,awk,cgi,jar," // Unices, continued
- "app,pkg,dmg,scpt" // Mac OS X
- );
- defaults.insert("SkipGuitoneVersion", QString());
- defaults.insert("LogLevel", DebugLog::Info);
- defaults.insert("MtnExePath", "mtn");
- defaults.insert("ConsoleLogEnabled", true);
- defaults.insert("FileLogEnabled", false);
- defaults.insert("CheckForUpdates", true);
- defaults.insert("DisableChangelogAutoCompletion", false);
- defaults.insert("FixUnwantedReverseDiffs", true);
- defaults.insert("SaveEncodingAsFileAttribute", true);
- defaults.insert("RelaxedVersionCheck", false);
- defaults.insert("AskFileOpen", true);
- defaults.insert("ReadWorkspaceIncrementally", false);
- defaults.insert("ReadUserRcFiles", false);
- defaults.insert("ChangesetBrowserTree", false);
- defaults.insert("PromptWhenPrivateKeyIsMissing", true);
- }
- Settings::~Settings()
- {
- if (inner) delete inner;
- }
- QSettings * Settings::getInner()
- {
- if (inner == 0 && QCoreApplication::instance() != 0)
- {
- inner = new QSettings();
- }
- return inner;
- }
- QVariant Settings::value(const QString & key) const
- {
- I(defaults.contains(key));
- QVariant def = defaults.value(key);
- return value(key, def);
- }
- QVariant Settings::value(const QString & key, const QVariant & defaultValue) const
- {
- QSettings * settings = const_cast<Settings *>(this)->getInner();
- if (settings)
- return settings->value(key, defaultValue);
- return defaultValue;
- }
- void Settings::setValue(const QString & key, const QVariant & value)
- {
- QSettings * settings = getInner();
- if (settings)
- settings->setValue(key, value);
- }
- void Settings::doClear()
- {
- QSettings * settings = getInner();
- if (settings)
- settings->clear();
- }
- void Settings::doSync()
- {
- QSettings * settings = getInner();
- if (settings)
- settings->sync();
- }
- void Settings::setBool(const QString & name, bool value)
- {
- I(!name.isEmpty());
- singleton()->setValue(name, value);
- }
- bool Settings::getBool(const QString & name)
- {
- I(!name.isEmpty());
- return singleton()->value(name).toBool();
- }
- void Settings::setString(const QString & name, const QString & value)
- {
- I(!name.isEmpty());
- singleton()->setValue(name, value);
- }
- QString Settings::getString(const QString & name)
- {
- I(!name.isEmpty());
- return singleton()->value(name).toString();
- }
- void Settings::setWindowGeometry(const QString & windowClass, const QByteArray & data)
- {
- I(!windowClass.isEmpty());
- singleton()->setValue(windowClass, data);
- }
- QByteArray Settings::getWindowGeometry(const QString & windowClass)
- {
- I(!windowClass.isEmpty());
- return singleton()->value(windowClass, QByteArray()).toByteArray();
- }
- void Settings::sync()
- {
- singleton()->doSync();
- }
- void Settings::clear()
- {
- singleton()->doClear();
- }
- int Settings::getLogLevel()
- {
- return singleton()->value("LogLevel").toInt();
- }
- void Settings::setLogLevel(int verbosity)
- {
- I(verbosity >= 1 && verbosity <= 5);
- singleton()->setValue("LogLevel", verbosity);
- }
- void Settings::saveHeaderViewState(QHeaderView * view, const QString & name)
- {
- I(!name.isEmpty());
- QStringList cols;
- for (int i=0, j=view->count(); i<j; i++)
- {
- // save col size and visual index separated by a single colon
- cols.append(QString::number(view->sectionSize(i)).
- append(":").
- append(QString::number(view->visualIndex(i)))
- );
- }
- Settings *settings = singleton();
- settings->setValue(name, cols.join(","));
- }
- void Settings::restoreHeaderViewState(QHeaderView * view, const QString & name)
- {
- I(!name.isEmpty());
- QString colConfig(singleton()->value(name, QString()).toString());
- QStringList cols = colConfig.split(",", QString::SkipEmptyParts);
- int colCount = cols.size();
- if (colCount == 0) return;
- int curColCount = view->count();
- for (int i=0; i < colCount && i < curColCount; i++)
- {
- QStringList parts = cols.at(i).split(":", QString::SkipEmptyParts);
- I(parts.size() == 2);
- view->resizeSection(i, parts.at(0).toInt());
- view->moveSection(view->visualIndex(i), parts.at(1).toInt());
- }
- }
- QByteArray Settings::getSplitterState(const QString & name)
- {
- I(!name.isEmpty());
- return singleton()->value(name, QByteArray()).toByteArray();
- }
- void Settings::setSplitterState(const QByteArray & byteArray, const QString & name)
- {
- I(!name.isEmpty());
- Settings *settings = singleton();
- settings->setValue(name, byteArray);
- }
- void Settings::setItemList(const QString & name, const QStringList & items)
- {
- I(!name.isEmpty());
- singleton()->setValue(name, items);
- }
- QStringList Settings::getItemList(const QString & name)
- {
- I(!name.isEmpty());
- return singleton()->value(name, QStringList()).toStringList();
- }
- void Settings::addItemToList(const QString & name, const QString & item, int maxItems)
- {
- QStringList list = getItemList(name);
- // move an already recorded item to the front
- int pos = list.indexOf(item);
- if (pos > -1)
- {
- list.move(pos, 0);
- }
- else
- {
- if (list.size() > maxItems)
- {
- list.removeLast();
- }
- list.prepend(item);
- }
- setItemList(name, list);
- }
- void Settings::removeItemFromList(const QString & name, const QString & item)
- {
- QStringList list = getItemList(name);
- int pos = list.indexOf(item);
- if (pos == -1) return;
- list.removeAt(pos);
- setItemList(name, list);
- }
- void Settings::setItemMap(const QString & name, const QMap<QString, QVariant> & items)
- {
- I(!name.isEmpty());
- singleton()->setValue(name, items);
- }
- QMap<QString, QVariant> Settings::getItemMap(const QString & name)
- {
- I(!name.isEmpty());
- return singleton()->value(name, QMap<QString, QVariant>()).toMap();
- }
- QVariant Settings::getItemFromMap(const QString & name, const QString & key)
- {
- QMap<QString, QVariant> map = getItemMap(name);
- if (map.contains(key))
- return map.value(key);
- return QVariant();
- }
- void Settings::addItemToMap(const QString & name, const QString & key, const QVariant & value)
- {
- QMap<QString, QVariant> map = getItemMap(name);
- map.insert(key, value);
- setItemMap(name, map);
- }
- void Settings::removeItemFromMap(const QString & name, const QString & key)
- {
- QMap<QString, QVariant> map = getItemMap(name);
- map.remove(key);
- setItemMap(name, map);
- }