PageRenderTime 45ms CodeModel.GetById 24ms app.highlight 19ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1/***************************************************************************
  2 *   Copyright (C) 2006 by Ingo Maindorfer								   *
  3 *   ingo@liquidcooling.de												   *
  4 *                                                                         *
  5 *   This program is free software; you can redistribute it and/or modify  *
  6 *   it under the terms of the GNU General Public License as published by  *
  7 *   the Free Software Foundation, either version 3 of the License, or     *
  8 *   (at your option) any later version.                                   *
  9 *                                                                         *
 10 *   This program is distributed in the hope that it will be useful,       *
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 13 *   GNU General Public License for more details.                          *
 14 *                                                                         *
 15 *   You should have received a copy of the GNU General Public License     *
 16 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 17 ***************************************************************************/
 18
 19#include "Settings.h"
 20#include "vocab.h"
 21
 22#include <QCoreApplication>
 23
 24Settings * Settings::instance = 0;
 25
 26Settings * Settings::singleton()
 27{
 28    if (!instance)
 29    {
 30        QCoreApplication::setOrganizationName("Thomas Keller");
 31        QCoreApplication::setOrganizationDomain("thomaskeller.biz");
 32        QCoreApplication::setApplicationName("guitone");
 33
 34        instance = new Settings();
 35    }
 36    return instance;
 37}
 38
 39Settings::Settings() : inner(0)
 40{
 41    defaults.insert("AnnotationColorOldestRevision", "#FCFFC0");
 42    defaults.insert("AnnotationColorNewestRevision", "#FF6609");
 43    defaults.insert("AnnotationColorHighlight", "#EEEEEE");
 44    defaults.insert("DiffColorAddedLines", "#90EE90");
 45    defaults.insert("DiffColorRemovedLines", "#FFCC99");
 46    defaults.insert("ChangesetBrowserSuspendedBranches", "#AAAAAA");
 47    defaults.insert("ChangesetBrowserMergeRevisions", "#32CD32");
 48    defaults.insert("ChangesetBrowserOutOfBranchRevisions", "#AAAAAA");
 49
 50    defaults.insert("AskFileOpenExt",
 51        "exe,com,bat,pif,hta,js,jse,inf,lnk,"     // Windows
 52        "scr,wsc,wsf,wsh,vb,vbe,vbs,vbscript,"    // Windows, continued
 53        "sh,bsh,zsh,csh,ksh,py,pl,php,php3,"      // Unices
 54        "php4,rb,ruby,awk,cgi,jar,"               // Unices, continued
 55        "app,pkg,dmg,scpt"                        // Mac OS X
 56    );
 57
 58    defaults.insert("SkipGuitoneVersion", QString());
 59    defaults.insert("LogLevel", DebugLog::Info);
 60    defaults.insert("MtnExePath", "mtn");
 61
 62    defaults.insert("ConsoleLogEnabled", true);
 63    defaults.insert("FileLogEnabled", false);
 64    defaults.insert("CheckForUpdates", true);
 65    defaults.insert("DisableChangelogAutoCompletion", false);
 66    defaults.insert("FixUnwantedReverseDiffs", true);
 67    defaults.insert("SaveEncodingAsFileAttribute", true);
 68    defaults.insert("RelaxedVersionCheck", false);
 69    defaults.insert("AskFileOpen", true);
 70    defaults.insert("ReadWorkspaceIncrementally", false);
 71    defaults.insert("ReadUserRcFiles", false);
 72    defaults.insert("ChangesetBrowserTree", false);
 73    defaults.insert("PromptWhenPrivateKeyIsMissing", true);
 74}
 75
 76Settings::~Settings()
 77{
 78    if (inner) delete inner;
 79}
 80
 81QSettings * Settings::getInner()
 82{
 83    if (inner == 0 && QCoreApplication::instance() != 0)
 84    {
 85        inner = new QSettings();
 86    }
 87    return inner;
 88}
 89
 90QVariant Settings::value(const QString & key) const
 91{
 92    I(defaults.contains(key));
 93    QVariant def = defaults.value(key);
 94    return value(key, def);
 95}
 96
 97QVariant Settings::value(const QString & key, const QVariant & defaultValue) const
 98{
 99    QSettings * settings = const_cast<Settings *>(this)->getInner();
100    if (settings)
101        return settings->value(key, defaultValue);
102    return defaultValue;
103}
104
105void Settings::setValue(const QString & key, const QVariant & value)
106{
107    QSettings * settings = getInner();
108    if (settings)
109        settings->setValue(key, value);
110}
111
112void Settings::doClear()
113{
114    QSettings * settings = getInner();
115    if (settings)
116        settings->clear();
117}
118
119void Settings::doSync()
120{
121    QSettings * settings = getInner();
122    if (settings)
123        settings->sync();
124}
125
126void Settings::setBool(const QString & name, bool value)
127{
128    I(!name.isEmpty());
129    singleton()->setValue(name, value);
130}
131
132bool Settings::getBool(const QString & name)
133{
134    I(!name.isEmpty());
135    return singleton()->value(name).toBool();
136}
137
138void Settings::setString(const QString & name, const QString & value)
139{
140    I(!name.isEmpty());
141    singleton()->setValue(name, value);
142}
143
144QString Settings::getString(const QString & name)
145{
146    I(!name.isEmpty());
147    return singleton()->value(name).toString();
148}
149
150void Settings::setWindowGeometry(const QString & windowClass, const QByteArray & data)
151{
152    I(!windowClass.isEmpty());
153    singleton()->setValue(windowClass, data);
154}
155
156QByteArray Settings::getWindowGeometry(const QString & windowClass)
157{
158    I(!windowClass.isEmpty());
159    return singleton()->value(windowClass, QByteArray()).toByteArray();
160}
161
162void Settings::sync()
163{
164    singleton()->doSync();
165}
166
167void Settings::clear()
168{
169    singleton()->doClear();
170}
171
172int Settings::getLogLevel()
173{
174    return singleton()->value("LogLevel").toInt();
175}
176
177void Settings::setLogLevel(int verbosity)
178{
179    I(verbosity >= 1 && verbosity <= 5);
180    singleton()->setValue("LogLevel", verbosity);
181}
182
183void Settings::saveHeaderViewState(QHeaderView * view, const QString & name)
184{
185    I(!name.isEmpty());
186    QStringList cols;
187    for (int i=0, j=view->count(); i<j; i++)
188    {
189        // save col size and visual index separated by a single colon
190        cols.append(QString::number(view->sectionSize(i)).
191                    append(":").
192                    append(QString::number(view->visualIndex(i)))
193        );
194    }
195    Settings *settings = singleton();
196    settings->setValue(name, cols.join(","));
197}
198
199void Settings::restoreHeaderViewState(QHeaderView * view, const QString & name)
200{
201    I(!name.isEmpty());
202    QString colConfig(singleton()->value(name, QString()).toString());
203    QStringList cols = colConfig.split(",", QString::SkipEmptyParts);
204
205    int colCount = cols.size();
206    if (colCount == 0) return;
207
208    int curColCount = view->count();
209
210    for (int i=0; i < colCount && i < curColCount; i++)
211    {
212        QStringList parts = cols.at(i).split(":", QString::SkipEmptyParts);
213
214        I(parts.size() == 2);
215
216        view->resizeSection(i, parts.at(0).toInt());
217        view->moveSection(view->visualIndex(i), parts.at(1).toInt());
218    }
219}
220
221QByteArray Settings::getSplitterState(const QString & name)
222{
223    I(!name.isEmpty());
224    return singleton()->value(name, QByteArray()).toByteArray();
225}
226
227void Settings::setSplitterState(const QByteArray & byteArray, const QString & name)
228{
229    I(!name.isEmpty());
230    Settings *settings = singleton();
231    settings->setValue(name, byteArray);
232}
233
234void Settings::setItemList(const QString & name, const QStringList & items)
235{
236    I(!name.isEmpty());
237    singleton()->setValue(name, items);
238}
239
240QStringList Settings::getItemList(const QString & name)
241{
242    I(!name.isEmpty());
243    return singleton()->value(name, QStringList()).toStringList();
244}
245
246void Settings::addItemToList(const QString & name, const QString & item, int maxItems)
247{
248    QStringList list = getItemList(name);
249
250    // move an already recorded item to the front
251    int pos = list.indexOf(item);
252    if (pos > -1)
253    {
254        list.move(pos, 0);
255    }
256    else
257    {
258        if (list.size() > maxItems)
259        {
260            list.removeLast();
261        }
262        list.prepend(item);
263    }
264    setItemList(name, list);
265}
266
267void Settings::removeItemFromList(const QString & name, const QString & item)
268{
269    QStringList list = getItemList(name);
270    int pos = list.indexOf(item);
271    if (pos == -1) return;
272    list.removeAt(pos);
273    setItemList(name, list);
274}
275
276void Settings::setItemMap(const QString & name, const QMap<QString, QVariant> & items)
277{
278    I(!name.isEmpty());
279    singleton()->setValue(name, items);
280}
281
282QMap<QString, QVariant> Settings::getItemMap(const QString & name)
283{
284    I(!name.isEmpty());
285    return singleton()->value(name, QMap<QString, QVariant>()).toMap();
286}
287
288QVariant Settings::getItemFromMap(const QString & name, const QString & key)
289{
290    QMap<QString, QVariant> map = getItemMap(name);
291    if (map.contains(key))
292        return map.value(key);
293    return QVariant();
294}
295
296void Settings::addItemToMap(const QString & name, const QString & key, const QVariant & value)
297{
298    QMap<QString, QVariant> map = getItemMap(name);
299    map.insert(key, value);
300    setItemMap(name, map);
301}
302
303void Settings::removeItemFromMap(const QString & name, const QString & key)
304{
305    QMap<QString, QVariant> map = getItemMap(name);
306    map.remove(key);
307    setItemMap(name, map);
308}
309