PageRenderTime 49ms CodeModel.GetById 18ms 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. #include "Settings.h"
  19. #include "vocab.h"
  20. #include <QCoreApplication>
  21. Settings * Settings::instance = 0;
  22. Settings * Settings::singleton()
  23. {
  24. if (!instance)
  25. {
  26. QCoreApplication::setOrganizationName("Thomas Keller");
  27. QCoreApplication::setOrganizationDomain("thomaskeller.biz");
  28. QCoreApplication::setApplicationName("guitone");
  29. instance = new Settings();
  30. }
  31. return instance;
  32. }
  33. Settings::Settings() : inner(0)
  34. {
  35. defaults.insert("AnnotationColorOldestRevision", "#FCFFC0");
  36. defaults.insert("AnnotationColorNewestRevision", "#FF6609");
  37. defaults.insert("AnnotationColorHighlight", "#EEEEEE");
  38. defaults.insert("DiffColorAddedLines", "#90EE90");
  39. defaults.insert("DiffColorRemovedLines", "#FFCC99");
  40. defaults.insert("ChangesetBrowserSuspendedBranches", "#AAAAAA");
  41. defaults.insert("ChangesetBrowserMergeRevisions", "#32CD32");
  42. defaults.insert("ChangesetBrowserOutOfBranchRevisions", "#AAAAAA");
  43. defaults.insert("AskFileOpenExt",
  44. "exe,com,bat,pif,hta,js,jse,inf,lnk," // Windows
  45. "scr,wsc,wsf,wsh,vb,vbe,vbs,vbscript," // Windows, continued
  46. "sh,bsh,zsh,csh,ksh,py,pl,php,php3," // Unices
  47. "php4,rb,ruby,awk,cgi,jar," // Unices, continued
  48. "app,pkg,dmg,scpt" // Mac OS X
  49. );
  50. defaults.insert("SkipGuitoneVersion", QString());
  51. defaults.insert("LogLevel", DebugLog::Info);
  52. defaults.insert("MtnExePath", "mtn");
  53. defaults.insert("ConsoleLogEnabled", true);
  54. defaults.insert("FileLogEnabled", false);
  55. defaults.insert("CheckForUpdates", true);
  56. defaults.insert("DisableChangelogAutoCompletion", false);
  57. defaults.insert("FixUnwantedReverseDiffs", true);
  58. defaults.insert("SaveEncodingAsFileAttribute", true);
  59. defaults.insert("RelaxedVersionCheck", false);
  60. defaults.insert("AskFileOpen", true);
  61. defaults.insert("ReadWorkspaceIncrementally", false);
  62. defaults.insert("ReadUserRcFiles", false);
  63. defaults.insert("ChangesetBrowserTree", false);
  64. defaults.insert("PromptWhenPrivateKeyIsMissing", true);
  65. }
  66. Settings::~Settings()
  67. {
  68. if (inner) delete inner;
  69. }
  70. QSettings * Settings::getInner()
  71. {
  72. if (inner == 0 && QCoreApplication::instance() != 0)
  73. {
  74. inner = new QSettings();
  75. }
  76. return inner;
  77. }
  78. QVariant Settings::value(const QString & key) const
  79. {
  80. I(defaults.contains(key));
  81. QVariant def = defaults.value(key);
  82. return value(key, def);
  83. }
  84. QVariant Settings::value(const QString & key, const QVariant & defaultValue) const
  85. {
  86. QSettings * settings = const_cast<Settings *>(this)->getInner();
  87. if (settings)
  88. return settings->value(key, defaultValue);
  89. return defaultValue;
  90. }
  91. void Settings::setValue(const QString & key, const QVariant & value)
  92. {
  93. QSettings * settings = getInner();
  94. if (settings)
  95. settings->setValue(key, value);
  96. }
  97. void Settings::doClear()
  98. {
  99. QSettings * settings = getInner();
  100. if (settings)
  101. settings->clear();
  102. }
  103. void Settings::doSync()
  104. {
  105. QSettings * settings = getInner();
  106. if (settings)
  107. settings->sync();
  108. }
  109. void Settings::setBool(const QString & name, bool value)
  110. {
  111. I(!name.isEmpty());
  112. singleton()->setValue(name, value);
  113. }
  114. bool Settings::getBool(const QString & name)
  115. {
  116. I(!name.isEmpty());
  117. return singleton()->value(name).toBool();
  118. }
  119. void Settings::setString(const QString & name, const QString & value)
  120. {
  121. I(!name.isEmpty());
  122. singleton()->setValue(name, value);
  123. }
  124. QString Settings::getString(const QString & name)
  125. {
  126. I(!name.isEmpty());
  127. return singleton()->value(name).toString();
  128. }
  129. void Settings::setWindowGeometry(const QString & windowClass, const QByteArray & data)
  130. {
  131. I(!windowClass.isEmpty());
  132. singleton()->setValue(windowClass, data);
  133. }
  134. QByteArray Settings::getWindowGeometry(const QString & windowClass)
  135. {
  136. I(!windowClass.isEmpty());
  137. return singleton()->value(windowClass, QByteArray()).toByteArray();
  138. }
  139. void Settings::sync()
  140. {
  141. singleton()->doSync();
  142. }
  143. void Settings::clear()
  144. {
  145. singleton()->doClear();
  146. }
  147. int Settings::getLogLevel()
  148. {
  149. return singleton()->value("LogLevel").toInt();
  150. }
  151. void Settings::setLogLevel(int verbosity)
  152. {
  153. I(verbosity >= 1 && verbosity <= 5);
  154. singleton()->setValue("LogLevel", verbosity);
  155. }
  156. void Settings::saveHeaderViewState(QHeaderView * view, const QString & name)
  157. {
  158. I(!name.isEmpty());
  159. QStringList cols;
  160. for (int i=0, j=view->count(); i<j; i++)
  161. {
  162. // save col size and visual index separated by a single colon
  163. cols.append(QString::number(view->sectionSize(i)).
  164. append(":").
  165. append(QString::number(view->visualIndex(i)))
  166. );
  167. }
  168. Settings *settings = singleton();
  169. settings->setValue(name, cols.join(","));
  170. }
  171. void Settings::restoreHeaderViewState(QHeaderView * view, const QString & name)
  172. {
  173. I(!name.isEmpty());
  174. QString colConfig(singleton()->value(name, QString()).toString());
  175. QStringList cols = colConfig.split(",", QString::SkipEmptyParts);
  176. int colCount = cols.size();
  177. if (colCount == 0) return;
  178. int curColCount = view->count();
  179. for (int i=0; i < colCount && i < curColCount; i++)
  180. {
  181. QStringList parts = cols.at(i).split(":", QString::SkipEmptyParts);
  182. I(parts.size() == 2);
  183. view->resizeSection(i, parts.at(0).toInt());
  184. view->moveSection(view->visualIndex(i), parts.at(1).toInt());
  185. }
  186. }
  187. QByteArray Settings::getSplitterState(const QString & name)
  188. {
  189. I(!name.isEmpty());
  190. return singleton()->value(name, QByteArray()).toByteArray();
  191. }
  192. void Settings::setSplitterState(const QByteArray & byteArray, const QString & name)
  193. {
  194. I(!name.isEmpty());
  195. Settings *settings = singleton();
  196. settings->setValue(name, byteArray);
  197. }
  198. void Settings::setItemList(const QString & name, const QStringList & items)
  199. {
  200. I(!name.isEmpty());
  201. singleton()->setValue(name, items);
  202. }
  203. QStringList Settings::getItemList(const QString & name)
  204. {
  205. I(!name.isEmpty());
  206. return singleton()->value(name, QStringList()).toStringList();
  207. }
  208. void Settings::addItemToList(const QString & name, const QString & item, int maxItems)
  209. {
  210. QStringList list = getItemList(name);
  211. // move an already recorded item to the front
  212. int pos = list.indexOf(item);
  213. if (pos > -1)
  214. {
  215. list.move(pos, 0);
  216. }
  217. else
  218. {
  219. if (list.size() > maxItems)
  220. {
  221. list.removeLast();
  222. }
  223. list.prepend(item);
  224. }
  225. setItemList(name, list);
  226. }
  227. void Settings::removeItemFromList(const QString & name, const QString & item)
  228. {
  229. QStringList list = getItemList(name);
  230. int pos = list.indexOf(item);
  231. if (pos == -1) return;
  232. list.removeAt(pos);
  233. setItemList(name, list);
  234. }
  235. void Settings::setItemMap(const QString & name, const QMap<QString, QVariant> & items)
  236. {
  237. I(!name.isEmpty());
  238. singleton()->setValue(name, items);
  239. }
  240. QMap<QString, QVariant> Settings::getItemMap(const QString & name)
  241. {
  242. I(!name.isEmpty());
  243. return singleton()->value(name, QMap<QString, QVariant>()).toMap();
  244. }
  245. QVariant Settings::getItemFromMap(const QString & name, const QString & key)
  246. {
  247. QMap<QString, QVariant> map = getItemMap(name);
  248. if (map.contains(key))
  249. return map.value(key);
  250. return QVariant();
  251. }
  252. void Settings::addItemToMap(const QString & name, const QString & key, const QVariant & value)
  253. {
  254. QMap<QString, QVariant> map = getItemMap(name);
  255. map.insert(key, value);
  256. setItemMap(name, map);
  257. }
  258. void Settings::removeItemFromMap(const QString & name, const QString & key)
  259. {
  260. QMap<QString, QVariant> map = getItemMap(name);
  261. map.remove(key);
  262. setItemMap(name, map);
  263. }