]> Shamusworld >> Repos - architektonas/blob - src/base/rs_settings.cpp.old
7d0e04bdee0669b6fe8a428c90865630c8c474b5
[architektonas] / src / base / rs_settings.cpp.old
1 // rs_settings.cpp
2 //
3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -----------------------------------------------------------
11 // JLH  05/19/2010  Created this file. :-)
12 //
13
14 #include "rs_settings.h"
15
16 // Class variable
17 RS_Settings * RS_Settings::uniqueInstance = NULL;
18
19 RS_Settings::RS_Settings():
20         initialized(false), companyKey(""), appKey(""), group("")
21 {
22 }
23
24 /**
25  * Destructor
26  */
27 RS_Settings::~RS_Settings()
28 {
29         // Needed since QList doesn't have AutoDelete...
30         while (!cache.isEmpty())
31         {
32                 QString * value = *cache.begin();
33                 cache.erase(cache.begin());
34                 delete value;
35         }
36 }
37
38 // Class method
39 /**
40  * @return Instance to the unique settings object.
41  */
42 RS_Settings * RS_Settings::instance()
43 {
44         if (uniqueInstance == NULL)
45                 uniqueInstance = new RS_Settings();
46
47         return uniqueInstance;
48 }
49
50 /**
51  * Initialisation.
52  *
53  * @param companyKey String that identifies the company. Must start
54  *        with a "/". E.g. "/RibbonSoft"
55  * @param appKey String that identifies the application. Must start
56  *        with a "/". E.g. "/QCad2"
57  */
58 void RS_Settings::init(const QString & comp, const QString & app)
59 {
60         group = "";
61         appKey = app;
62         companyKey = comp;
63
64 //In Qt4, there is no such thing as a search path anymore...
65         //insertSearchPath(QSettings::Windows, companyKey + appKey);
66         //insertSearchPath(QSettings::Unix, "/usr/share/");
67         initialized = true;
68 }
69
70 void RS_Settings::beginGroup(const QString & grp)
71 {
72         group = grp;
73 }
74
75 void RS_Settings::endGroup()
76 {
77         group = "";
78 }
79
80 bool RS_Settings::writeEntry(const QString & key, int value)
81 {
82         QString s = QString("%1").arg(value);
83         return writeEntry(key, s);
84 }
85
86 bool RS_Settings::writeEntry(const QString & key, double value)
87 {
88         QString s = QString("%1").arg(value);
89         return writeEntry(key, s);
90 }
91
92 bool RS_Settings::writeEntry(const QString & key, const QString & value)
93 {
94 //[DONE, mostly]#warning "!!! Need to revamp outdated settings system !!!"
95         QSettings settings("Underground Software", "Architektonas");
96
97 //      bool ret = s.writeEntry(QString("%1%2%3").arg(appKey).arg(group).arg(key), value);
98         settings.setValue(QString("%1%2").arg(group).arg(key), value);
99         addToCache(key, value);
100
101 //      return ret;
102         // The old system told you if it failed or not, but the new assumes success
103         return true;
104 }
105
106 QString RS_Settings::readEntry(const QString & key, const QString & def, bool * ok)
107 {
108         // lookup:
109         QString entry = readEntryCache(key);
110
111         if (entry != QString::null)
112                 return entry;
113
114         QSettings settings("Underground Software", "Architektonas");
115
116         QString s = QString("%1%2").arg(group).arg(key);
117
118         if (ok)
119                 *ok = settings.contains(s);
120
121 //      ret = s.readEntry(QString("%1%2%3").arg(appKey).arg(group).arg(key), def, ok);
122         entry = settings.value(s, def).toString();
123         addToCache(key, entry);
124
125         return entry;
126 }
127
128 int RS_Settings::readNumEntry(const QString & key, int def, bool * ok)
129 {
130         // lookup:
131         QString result = readEntryCache(key);
132
133         if (result != QString::null)
134                 return result.toInt();
135
136         QSettings settings("Underground Software", "Architektonas");
137
138 //      int ret = s.readNumEntry(QString("%1%2%3").arg(appKey).arg(group).arg(key), def, ok);
139         QString s = QString("%1%2").arg(group).arg(key);
140
141         if (ok)
142                 *ok = settings.contains(s);
143
144         int entry = settings.value(s, def).toInt();
145         addToCache(key, QString("%1").arg(entry));
146
147         return entry;
148 }
149
150 QString RS_Settings::readEntryCache(const QString & key)
151 {
152         QString * s = cache.value(key);
153
154         if (s == NULL)
155                 return QString::null;
156
157         return *s;
158 }
159
160 void RS_Settings::addToCache(const QString & key, const QString & value)
161 {
162         cache.replace(key, new QString(value));
163 }