AlbumShaper 1.0a3
configuration.cpp
Go to the documentation of this file.
1//==============================================
2// copyright : (C) 2003-2005 by Will Stokes
3//==============================================
4// This program is free software; you can redistribute it
5// and/or modify it under the terms of the GNU General
6// Public License as published by the Free Software
7// Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//==============================================
10
11//Systemwide includes
12#include <qdir.h>
13#include <qfile.h>
14#include <qstring.h>
15#include <q3textstream.h>
16#include <qdom.h>
17
18#if defined(Q_OS_WIN)
19#include <stdlib.h>
20#endif
21
22//Projectwide includes
23#include "configuration.h"
24#include "settinggroup.h"
25#include "../config.h"
28//==============================================
30{
31 //PLATFORM_SPECIFIC_CODE
32
33 //-----------------------------
34 //Mac OSX requires no directories to be created
35 #if defined(Q_OS_MACX)
36 return true;
37 //-----------------------------
38 //Windows
39 #elif defined(Q_OS_WIN)
40 bool configDirMade = true;
41
42 //attempt to get folder location using windows api, if this fails try hard coded path as a last resort
43 QString folderLoc;
44 if( !getWindowsFolderLocation(LOCAL_SETTINGS_APPLICATION_DATA, folderLoc) )
45 {
46 folderLoc = getenv("USERPROFILE") + QString("/Local Settings/Application Data");
47 }
48 QDir dataDir( folderLoc );
49 if(!dataDir.exists("Album Shaper"))
50 {
51 configDirMade = dataDir.mkdir("Album Shaper");
52 }
53 return configDirMade;
54 //-----------------------------
55 //Unix/Linux/BSD
56 #else
57 bool configDirMade = true;
58 QDir homeDir( QDir::homeDirPath() );
59 if(!homeDir.exists(".albumShaper"))
60 {
61 configDirMade = homeDir.mkdir(".albumShaper");
62 }
63 return configDirMade;
64 #endif
65 //-----------------------------
66}
67//==============================================
69{
70 //-----------------------------
71 //Determine settings filename
72 //-----------------------------
73
74 //PLATFORM_SPECIFIC_CODE
75
76 //Mac OS X
77 #if defined(Q_OS_MACX)
78 settingsFilename = QDir::homeDirPath() + QString("/Library/Preferences/net.sourceforge.albumshaper.xml");
79 //-----------------------------
80 //Windows
81 #elif defined(Q_OS_WIN)
82 //attempt to get folder location using windows api, if this fails try hard coded path as a last resort
83 QString tmp;
84 if( !getWindowsFolderLocation(LOCAL_SETTINGS_APPLICATION_DATA, tmp) )
85 {
86 tmp = getenv("USERPROFILE") + QString("/Local Settings/Application Data");
87 }
88 settingsFilename = QDir::convertSeparators( tmp + "/Album Shaper/settings.xml" );
89 //-----------------------------
90 //Unix/Linux/BSD
91 #else
92 settingsFilename = QDir::homeDirPath() + QString("/.albumShaper/settings.xml");
93 #endif
94 //-----------------------------
95
96 //no groups by default
97 firstGroup = NULL;
98 lastGroup = NULL;
99
100 //no group selected by default
101 curGroup = NULL;
102}
103//==============================================
105{
106 //delete all setting groups
108 while(cur != NULL)
109 {
110 SettingGroup* next = cur->getNext();
111 delete cur;
112 cur = next;
113 }
114}
115//==============================================
116void Configuration::setString( QString group, QString key, QString value)
117{
118 //check if cached group has same name, if not find group, create it if group does not exist
119 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
120 {
122 while(curGroup != NULL)
123 {
124 if(curGroup->getName().compare(group) == 0)
125 break;
127 }
128
129 //if we have not found the group create it and add to list
130 if(curGroup == NULL)
131 {
132 //create new group
133 curGroup = new SettingGroup(group);
134
135 //add group to list
136 if(firstGroup == NULL)
138 else
141 }
142 }
143
144 //set setting value
145 curGroup->setValue(key, value);
146}
147//==============================================
148void Configuration::setBool( QString group, QString key, bool val )
149{
150 setString( group, key, (val ? "1" : "0" ) );
151}
152//==============================================
153void Configuration::setInt( QString group, QString key, int val )
154{
155 setString( group, key, QString("%1").arg(val) );
156}
157//==============================================
158QString Configuration::getString(QString group, QString key)
159{
160 //check if cached group is correct group, if not find correct group
161 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
162 {
164 while(curGroup != NULL)
165 {
166 if(curGroup->getName().compare(group) == 0)
167 break;
169 }
170
171 //if we have not found the group return error value (-1)
172 if(curGroup == NULL)
173 {
174 return "-1";
175 }
176 }
177
178 //return setting value from group
179 return curGroup->getValue(key);
180}
181//==============================================
182void Configuration::resetSetting(QString group, QString key)
183{
184 //check if cached group is correct group, if not find correct group
185 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
186 {
188 while(curGroup != NULL)
189 {
190 if(curGroup->getName().compare(group) == 0)
191 break;
193 }
194
195 //if we have not found the group return error value (-1)
196 if(curGroup == NULL)
197 {
198 return;
199 }
200 }
201
202 //return setting value from group
204}
205//==============================================
206bool Configuration::getBool(QString group, QString key)
207{
208 return ( getString(group,key).compare("1") == 0 );
209}
210//==============================================
211int Configuration::getInt(QString group, QString key)
212{
213 return getString(group,key).toInt();
214}
215//==============================================
216float Configuration::getFloat(QString group, QString key)
217{
218 return getString(group,key).toFloat();
219}
220//==============================================
221double Configuration::getDouble(QString group, QString key)
222{
223 return getString(group,key).toDouble();
224}
225//==============================================
226void Configuration::removeGroup(QString group)
227{
228 //iterate through groups, remove group once found
229 SettingGroup* prev = NULL;
231 while(curGroup != NULL)
232 {
233 //found
234 if(curGroup->getName().compare(group) == 0)
235 {
236 //keep handle on group for deletion purposes
237 SettingGroup* temp = curGroup;
238
239 //fix head if necessary
240 if(curGroup == firstGroup)
242
243 //fix tail if necessary
244 if(lastGroup == curGroup)
245 lastGroup = prev;
246
247 //splice out group
248 if(prev != NULL)
249 prev->setNext( curGroup->getNext() );
250
251 //update curGroup pointer so valid
253
254 //free group
255 delete temp;
256 temp = NULL;
257
258 //done
259 return;
260 }
261
262 //update prev and cur pointers and move along
263 prev = curGroup;
265 }
266}
267//==============================================
269{
270 //-----------------------------------
271 //attempt to load xml settings file and construct dom, if either action failes return false
272 QFile settingsFile( settingsFilename );
273 if( !settingsFile.open( QIODevice::ReadOnly ) )
274 return false;
275
276 QDomDocument DOM;
277 if( !DOM.setContent( &settingsFile ) )
278 return false;
279
280 settingsFile.close();
281
282 //-----------------------------------
283 //walk though DOM and look for setting nodes.
284 //for each setting fetch, type, key, and value
285 //walk through list of settings and find previous setting
286 //if previous setting found replace value, otherwise add new setting to list
287 QDomElement root = DOM.documentElement();
288 QDomNode node = root.firstChild();
289
290 while( !node.isNull() )
291 {
292 if( node.isElement() && node.nodeName() == "group" )
293 {
294 //find group name, if no name found then move on to next group
295 QDomNamedNodeMap attributes = node.attributes();
296 if(attributes.namedItem("name").isNull())
297 {
298 node = node.nextSibling();
299 continue;
300 }
301
302 //create group if it does not already exist
303 SettingGroup* loadedGroup = NULL;
304
305 //last used group is the one we are looking for
306 if(curGroup->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
307 loadedGroup = curGroup;
308 //search list of groups
309 else
310 {
312 while(cur != NULL)
313 {
314 //found it!
315 if(cur->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
316 {
317 loadedGroup = cur;
318 break;
319 }
320 //nope, move on to next group
321 cur = cur->getNext();
322 }
323 }
324 //if group to be loaded is not found then create it
325 if(loadedGroup == NULL)
326 {
327 loadedGroup = new SettingGroup( attributes.namedItem("name").nodeValue() );
328 if(firstGroup == NULL)
329 firstGroup = loadedGroup;
330 else
331 lastGroup->setNext(loadedGroup);
332 lastGroup = loadedGroup;
333 }
334
335 loadedGroup->loadSettings(node);
336 }
337 //move on to next setting
338 node = node.nextSibling();
339 }
340 //-----------------------------------
341 //loading of settingings was successful
342 return true;
343}
344//==============================================
346{
347 //create/open html file
348 QFile file( settingsFilename );
349 if(file.open(QIODevice::WriteOnly))
350 {
351 //-----
352 Q3TextStream stream;
353 stream.setDevice( &file );
354 stream.setEncoding( Q3TextStream::UnicodeUTF8 );
355
356 //write header
357 stream << "<settings app=\"Album Shaper\" version=\"" << ALBUMSHAPER_VERSION << "\">\n";
358
359 //iterate over every group
361 while(curGroup != NULL)
362 {
363 curGroup->saveSettings( stream );
365 }
366
367 //end xml file
368 stream << "</settings>\n";
369
370 //success saving settings!
371 file.close();
372 return true;
373 }
374
375 //opening file for saving failed
376 file.close();
377 return false;
378}
379//==============================================
bool getBool(QString group, QString key)
Fetch bool setting.
~Configuration()
Destructor saves settings to disk.
QString settingsFilename
Settings filename.
double getDouble(QString group, QString key)
Fetch double setting.
void removeGroup(QString group)
Removes an entire group of settings.
SettingGroup * curGroup
pointer to currently selected group
SettingGroup * lastGroup
pointer to last group
float getFloat(QString group, QString key)
Fetch float setting.
void setInt(QString group, QString key, int val)
Set int setting.
Configuration()
Creates configuration variables using default values, then attempts to load settings from disk.
SettingGroup * firstGroup
pointer to first group
void setBool(QString group, QString key, bool val)
Set bool setting.
bool loadSettings()
Loads settings.
bool saveSettings()
Saves settings.
void setString(QString group, QString key, QString value)
Sets a setting value, if group does not exist it is created, if setting does not exist it is also cre...
void resetSetting(QString group, QString key)
Resets a setting to it's default value.
static bool constructSettingsDirectory()
Constructs any necessary directories for loading and saving user settings, returns false if unsuccess...
QString getString(QString group, QString key)
Fetch string setting.
int getInt(QString group, QString key)
Fetch int setting.
SettingGroup contains settings which are releated.
QString getValue(QString key)
Returns a setting value.
void resetSetting(QString key)
resets a setting to its default value
void setNext(SettingGroup *next)
sets the next group setting
void setValue(QString key, QString value)
Sets a setting value, create new setting if setting not found.
QString getName()
Returns group's name.
SettingGroup * getNext()
returns the next groupsetting
void saveSettings(Q3TextStream &stream)
writes out this group to file
void loadSettings(QDomNode &node)
#define ALBUMSHAPER_VERSION
Definition config.h:21