1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 
27 // RVT_PORT changed QSettings s(QSettings::Ini) to QSettings s("./qcad.ini", QSettings::IniFormat);
28 #include <QSettings>
29 #include "rs_settings.h"
30 
31 RS_Settings* RS_Settings::uniqueInstance = nullptr;
32 bool RS_Settings::save_is_allowed = true;
33 
RS_Settings()34 RS_Settings::RS_Settings():
35 	initialized(false)
36 {
37 }
38 
instance()39 RS_Settings* RS_Settings::instance() {
40 	if (!uniqueInstance) {
41 		uniqueInstance = new RS_Settings();
42 	}
43 	return uniqueInstance;
44 }
45 
46 /**
47  * Initialisation.
48  *
49  * @param companyKey String that identifies the company. Must start
50  *        with a "/". E.g. "/RibbonSoft"
51  * @param appKey String that identifies the application. Must start
52  *        with a "/". E.g. "/LibreCAD"
53  */
init(const QString & companyKey,const QString & appKey)54 void RS_Settings::init(const QString& companyKey,
55                        const QString& appKey) {
56 
57     group = "";
58 
59     this->companyKey = companyKey;
60     this->appKey = appKey;
61 
62     //insertSearchPath(QSettings::Windows, companyKey + appKey);
63     //insertSearchPath(QSettings::Unix, "/usr/local/share/");
64     initialized = true;
65 }
66 
67 
68 /**
69  * Destructor
70  */
~RS_Settings()71 RS_Settings::~RS_Settings() {
72 }
73 
74 
75 
beginGroup(const QString & group)76 void RS_Settings::beginGroup(const QString& group) {
77     this->group = group;
78 }
79 
endGroup()80 void RS_Settings::endGroup() {
81     this->group = "";
82 }
83 
writeEntry(const QString & key,int value)84 bool RS_Settings::writeEntry(const QString& key, int value) {
85     return writeEntry(key, QVariant(value));
86 }
87 
writeEntry(const QString & key,const QString & value)88 bool RS_Settings::writeEntry(const QString& key,const QString& value) {
89     return writeEntry(key, QVariant(value));
90 }
91 
writeEntry(const QString & key,double value)92 bool RS_Settings::writeEntry(const QString& key, double value) {
93     return writeEntry(key, QVariant(value));
94 }
95 
writeEntry(const QString & key,const QVariant & value)96 bool RS_Settings::writeEntry(const QString& key, const QVariant& value) {
97 
98     // Skip writing operations if the key is found in the cache and
99     // its value is the same as the new one (it was already written).
100     QVariant ret = readEntryCache(key);
101     if (ret.isValid() && ret == value) {
102         return true;
103     }
104 
105 	QSettings s(companyKey, appKey);
106     // RVT_PORT not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);
107 
108     s.setValue(QString("%1%2").arg(group).arg(key), value);
109 	cache[key]=value;
110 
111     return true;
112 }
113 
readEntry(const QString & key,const QString & def,bool * ok)114 QString RS_Settings::readEntry(const QString& key,
115                                  const QString& def,
116                                  bool* ok) {
117 
118     // lookup:
119     QVariant ret = readEntryCache(key);
120     if (!ret.isValid()) {
121 
122         QSettings s(companyKey, appKey);
123     	// RVT_PORT not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);
124 
125 		if (ok) {
126 			*ok=s.contains(QString("%1%2").arg(group).arg(key));
127 		}
128 
129         ret = s.value(QString("%1%2").arg(group).arg(key), QVariant(def));
130 		cache[key]=ret;
131     }
132 
133     return ret.toString();
134 
135 }
136 
readByteArrayEntry(const QString & key,const QString & def,bool * ok)137 QByteArray RS_Settings::readByteArrayEntry(const QString& key,
138                     const QString& def,
139                     bool* ok) {
140     QVariant ret = readEntryCache(key);
141     if (!ret.isValid()) {
142 
143         QSettings s(companyKey, appKey);
144         // RVT_PORT not supported anymore s.insertSearchPath(QSettings::Windows, companyKey);
145 
146                 if (ok) {
147                         *ok=s.contains(QString("%1%2").arg(group).arg(key));
148                 }
149 
150         ret = s.value(QString("%1%2").arg(group).arg(key), QVariant(def));
151 		cache[key]=ret;
152     }
153 
154     return ret.toByteArray();
155 
156 }
157 
readNumEntry(const QString & key,int def)158 int RS_Settings::readNumEntry(const QString& key, int def)
159 {
160 	QVariant value = readEntryCache(key);
161 	if (!value.isValid())
162 	{
163 		QSettings s(companyKey, appKey);
164 		QString str = QString("%1%2").arg(group).arg(key);
165 		// qDebug() << str;
166 		value = s.value(str, QVariant(def));
167 		cache[key] = value;
168 	}
169 	return value.toInt();
170 }
171 
172 
readEntryCache(const QString & key)173 QVariant RS_Settings::readEntryCache(const QString& key) {
174 	if(!cache.count(key)) return QVariant();
175 	return cache[key];
176 }
177 
178 
addToCache(const QString & key,const QVariant & value)179 void RS_Settings::addToCache(const QString& key, const QVariant& value) {
180     cache[key]=value;
181 }
182 
clear_all()183 void RS_Settings::clear_all()
184 {
185     QSettings s(companyKey, appKey);
186     s.clear();
187     save_is_allowed = false;
188 }
189 
clear_geometry()190 void RS_Settings::clear_geometry()
191 {
192     QSettings s(companyKey, appKey);
193     s.remove("/Geometry");
194     save_is_allowed = false;
195 }
196