-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreferences.cpp
235 lines (206 loc) · 7 KB
/
Preferences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/****************************************************************************
**
** Copyright (C) 2007~2017 Colin Willcocks.
** Copyright (C) 2005~2007 Uco Mesdag.
** All rights reserved.
** This file is part of "GT-8 Fx FloorBoard".
**
** 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 2 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, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**
****************************************************************************/
#include "Preferences.h"
#include "PreferencesDestroyer.h"
#include <QList>
#include <QFile>
#include <QTextCodec>
#include <QHash>
#include "xmlwriter/xmlwriter.h"
#include <QMessageBox>
Preferences::Preferences()
{
loadPreferences(":preferences.xml.dist");
QString buildVersion = this->getPreferences("General", "Application", "version");
if(!QFile("preferences.xml").exists())
{
loadPreferences(":preferences.xml.dist");
}
else
{
loadPreferences("preferences.xml");
if(this->getPreferences("General", "Application", "version")!=buildVersion)
{
loadPreferences(":preferences.xml.dist");
}
}
}
Preferences* Preferences::_instance = 0;// initialize pointer
PreferencesDestroyer Preferences::_destroyer;
Preferences* Preferences::Instance()
{
/* Multi-threading safe */
if (!_instance /*_instance == 0*/) // is it the first call?
{
_instance = new Preferences; // create sole instance
_destroyer.SetPreferences(_instance);
}
return _instance; // address of sole instance
/* Single-threading */
/*
static Preferences inst;
return &inst;
*/
}
QString Preferences::getPreferences(QString prefGroupName, QString prefTypeName, QString prefItemName)
{
/* Look op and return of the value coresponding to the group->type->item */
QString setting;
int indexOfValue = this->metaSearch.indexOf(QString(prefGroupName + ":" + prefTypeName + ":" + prefItemName));
if(indexOfValue!=-1)
{
setting = this->prefValues.at(indexOfValue);
}
else
{
setting = "";
}
return setting;
}
void Preferences::setPreferences(QString prefGroupName, QString prefTypeName, QString prefItemName, QString prefValueData)
{
/* Look op and set the value coresponding to the group->type->item */
int indexOfValue = this->metaSearch.indexOf(QString(prefGroupName + ":" + prefTypeName + ":" + prefItemName));
if(indexOfValue!=-1)
{
this->prefValues.replace(indexOfValue, prefValueData);
}
else
{
/* If the group->type->item does not exist/found it will be created/added */
this->metaSearch.append(QString(prefGroupName + ":" + prefTypeName + ":" + prefItemName));
this->groupNames.append(prefGroupName);
this->typeNames.append(prefTypeName);
this->itemNames.append(prefItemName);
this->prefValues.append(prefValueData);
}
}
void Preferences::loadPreferences(QString fileName)
{
this->metaSearch.clear();
this->groupNames.clear();
this->typeNames.clear();
this->itemNames.clear();
this->prefValues.clear();
QFile file;
file.setFileName(fileName);
/* Loads the xml document and creates the QDomElement root */
QDomDocument doc( "Application Preferences" );
doc.setContent( &file ); // file is a QFile
file.close();
QDomElement root = doc.documentElement(); // Points to <Preferences>
this->root = root;
/* Iterate trough xml tree and load in to the assigned vector */
QDomNode node = root.firstChild(); // Points to prefBuffer group
while ( !node.isNull() )
{
QString _groupName = node.nodeName();
QDomNode childNode = node.firstChild(); // Points to prefBuffer type
while ( !childNode.isNull() )
{
QString _typeName = childNode.nodeName();
for (int itemNum=0;itemNum<childNode.attributes().count();itemNum++ )
{ // Iterates trough all the atributes
QString _itemName = childNode.attributes().item(itemNum).nodeName();
QString _value = childNode.attributes().namedItem(_itemName).nodeValue();
this->metaSearch.append(_groupName + ":" + _typeName + ":" + _itemName);
this->prefValues.append(_value);
this->groupNames.append(_groupName);
this->typeNames.append(_typeName);
this->itemNames.append(_itemName);
}
childNode = childNode.nextSibling();
}
node = node.nextSibling();
}
}
struct IndexList // Create a structure of metaSearchData and indexnumbers to sort and match them afterwords
{
QString metaSearchData, indexNumber;
bool operator<(const IndexList& rhs) const;
//indexList(QString metaSearchData_, QString indexNumber_) : metaSearchData(metaSearchData_), indexNumber(indexNumber_) {}
};
bool IndexList::operator<(const IndexList& rhs) const
{
return (metaSearchData < rhs.metaSearchData) ? true : (metaSearchData == rhs.metaSearchData) ? rhs.indexNumber > indexNumber : false;
}
void Preferences::savePreferences()
{
//Write back to file/create
QFile file("preferences.xml");
if (!file.open(QIODevice::WriteOnly)) {
// ERROR
}
XmlWriter xout(&file);
QHash<QString, QString> attrs;
xout.writeComment("Preferences of the GT-8 Fx FloorBoard application.");
xout.writeOpenTag("Preferences");
unsigned int aSize = this->metaSearch.size();
QList<IndexList> sortIndexList;
IndexList tmp;
for(unsigned int n=0;n<aSize;n++)
{
//sortIndexList.append( indexList(this->metaSearch.at(n), QString::number(n, 10)) );
tmp.metaSearchData = this->metaSearch.at(n);
tmp.indexNumber = QString::number(n, 10);
sortIndexList.append( tmp );
}
qSort(sortIndexList.begin(), sortIndexList.end());
bool ok;
int i =0, a = 0;
QString currentGroupName;
for(unsigned int n=0; n<aSize;n++)
{
i = sortIndexList.at(n).indexNumber.toInt(&ok, 10);
if( this->groupNames.at(i) != currentGroupName )
{
xout.writeOpenTag(this->groupNames.at(i));
}
currentGroupName = this->groupNames.at(i);
attrs.insert(this->itemNames.at(i), this->prefValues.at(i));
if(n<aSize-1)
{
a = sortIndexList.at(n + 1).indexNumber.toInt(&ok, 10);
if(this->typeNames.at(i) != this->typeNames.at(a))
{
xout.writeAtomTag(this->typeNames.at(i), attrs);
attrs.clear();
}
}
else
{
xout.writeAtomTag(this->typeNames.at(i), attrs);
attrs.clear();
}
if(n==aSize-1)
{
xout.writeCloseTag(currentGroupName);
}
else if( currentGroupName != this->groupNames.at(a) )
{
xout.writeCloseTag(currentGroupName);
}
}
xout.writeCloseTag("Preferences");
//file.close();
}