00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <qdir.h>
00013 #include <qfile.h>
00014 #include <qstring.h>
00015 #include <qtextstream.h>
00016 #include <qdom.h>
00017
00018 #if defined(Q_OS_WIN)
00019 #include <stdlib.h>
00020 #endif
00021
00022
00023 #include "configuration.h"
00024 #include "settinggroup.h"
00025 #include "../config.h"
00026 #include "../backend/tools/xmlTools.h"
00027 #include "../backend/tools/fileTools.h"
00028
00029 bool Configuration::constructSettingsDirectory()
00030 {
00031
00032
00033
00034
00035 #if defined(Q_OS_MACX)
00036 return true;
00037
00038
00039 #elif defined(Q_OS_WIN)
00040 bool configDirMade = true;
00041
00042
00043 QString folderLoc;
00044 if( !getWindowsFolderLocation(LOCAL_SETTINGS_APPLICATION_DATA, folderLoc) )
00045 {
00046 folderLoc = getenv("USERPROFILE") + QString("/Local Settings/Application Data");
00047 }
00048 QDir dataDir( folderLoc );
00049 if(!dataDir.exists("Album Shaper"))
00050 {
00051 configDirMade = dataDir.mkdir("Album Shaper");
00052 }
00053 return configDirMade;
00054
00055
00056 #else
00057 bool configDirMade = true;
00058 QDir homeDir( QDir::homeDirPath() );
00059 if(!homeDir.exists(".albumShaper"))
00060 {
00061 configDirMade = homeDir.mkdir(".albumShaper");
00062 }
00063 return configDirMade;
00064 #endif
00065
00066 }
00067
00068 Configuration::Configuration()
00069 {
00070
00071
00072
00073
00074
00075
00076
00077 #if defined(Q_OS_MACX)
00078 settingsFilename = QDir::homeDirPath() + QString("/Library/Preferences/net.sourceforge.albumshaper.xml");
00079
00080
00081 #elif defined(Q_OS_WIN)
00082
00083 QString tmp;
00084 if( !getWindowsFolderLocation(LOCAL_SETTINGS_APPLICATION_DATA, tmp) )
00085 {
00086 tmp = getenv("USERPROFILE") + QString("/Local Settings/Application Data");
00087 }
00088 settingsFilename = QDir::convertSeparators( tmp + "/Album Shaper/settings.xml" );
00089
00090
00091 #else
00092 settingsFilename = QDir::homeDirPath() + QString("/.albumShaper/settings.xml");
00093 #endif
00094
00095
00096
00097 firstGroup = NULL;
00098 lastGroup = NULL;
00099
00100
00101 curGroup = NULL;
00102 }
00103
00104 Configuration::~Configuration()
00105 {
00106
00107 SettingGroup* cur = firstGroup;
00108 while(cur != NULL)
00109 {
00110 SettingGroup* next = cur->getNext();
00111 delete cur;
00112 cur = next;
00113 }
00114 }
00115
00116 void Configuration::setString( QString group, QString key, QString value)
00117 {
00118
00119 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
00120 {
00121 curGroup = firstGroup;
00122 while(curGroup != NULL)
00123 {
00124 if(curGroup->getName().compare(group) == 0)
00125 break;
00126 curGroup = curGroup->getNext();
00127 }
00128
00129
00130 if(curGroup == NULL)
00131 {
00132
00133 curGroup = new SettingGroup(group);
00134
00135
00136 if(firstGroup == NULL)
00137 firstGroup = curGroup;
00138 else
00139 lastGroup->setNext(curGroup);
00140 lastGroup = curGroup;
00141 }
00142 }
00143
00144
00145 curGroup->setValue(key, value);
00146 }
00147
00148 void Configuration::setBool( QString group, QString key, bool val )
00149 {
00150 setString( group, key, (val ? "1" : "0" ) );
00151 }
00152
00153 void Configuration::setInt( QString group, QString key, int val )
00154 {
00155 setString( group, key, QString("%1").arg(val) );
00156 }
00157
00158 QString Configuration::getString(QString group, QString key)
00159 {
00160
00161 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
00162 {
00163 curGroup = firstGroup;
00164 while(curGroup != NULL)
00165 {
00166 if(curGroup->getName().compare(group) == 0)
00167 break;
00168 curGroup = curGroup->getNext();
00169 }
00170
00171
00172 if(curGroup == NULL)
00173 {
00174 return "-1";
00175 }
00176 }
00177
00178
00179 return curGroup->getValue(key);
00180 }
00181
00182 void Configuration::resetSetting(QString group, QString key)
00183 {
00184
00185 if(curGroup == NULL || curGroup->getName().compare(group) != 0)
00186 {
00187 curGroup = firstGroup;
00188 while(curGroup != NULL)
00189 {
00190 if(curGroup->getName().compare(group) == 0)
00191 break;
00192 curGroup = curGroup->getNext();
00193 }
00194
00195
00196 if(curGroup == NULL)
00197 {
00198 return;
00199 }
00200 }
00201
00202
00203 curGroup->resetSetting(key);
00204 }
00205
00206 bool Configuration::getBool(QString group, QString key)
00207 {
00208 return ( getString(group,key).compare("1") == 0 );
00209 }
00210
00211 int Configuration::getInt(QString group, QString key)
00212 {
00213 return getString(group,key).toInt();
00214 }
00215
00216 float Configuration::getFloat(QString group, QString key)
00217 {
00218 return getString(group,key).toFloat();
00219 }
00220
00221 double Configuration::getDouble(QString group, QString key)
00222 {
00223 return getString(group,key).toDouble();
00224 }
00225
00226 void Configuration::removeGroup(QString group)
00227 {
00228
00229 SettingGroup* prev = NULL;
00230 curGroup = firstGroup;
00231 while(curGroup != NULL)
00232 {
00233
00234 if(curGroup->getName().compare(group) == 0)
00235 {
00236
00237 SettingGroup* temp = curGroup;
00238
00239
00240 if(curGroup == firstGroup)
00241 firstGroup = curGroup->getNext();
00242
00243
00244 if(lastGroup == curGroup)
00245 lastGroup = prev;
00246
00247
00248 if(prev != NULL)
00249 prev->setNext( curGroup->getNext() );
00250
00251
00252 curGroup = curGroup->getNext();
00253
00254
00255 delete temp;
00256 temp = NULL;
00257
00258
00259 return;
00260 }
00261
00262
00263 prev = curGroup;
00264 curGroup = curGroup->getNext();
00265 }
00266 }
00267
00268 bool Configuration::loadSettings()
00269 {
00270
00271
00272 QFile settingsFile( settingsFilename );
00273 if( !settingsFile.open( IO_ReadOnly ) )
00274 return false;
00275
00276 QDomDocument DOM;
00277 if( !DOM.setContent( &settingsFile ) )
00278 return false;
00279
00280 settingsFile.close();
00281
00282
00283
00284
00285
00286
00287 QDomElement root = DOM.documentElement();
00288 QDomNode node = root.firstChild();
00289
00290 while( !node.isNull() )
00291 {
00292 if( node.isElement() && node.nodeName() == "group" )
00293 {
00294
00295 QDomNamedNodeMap attributes = node.attributes();
00296 if(attributes.namedItem("name").isNull())
00297 {
00298 node = node.nextSibling();
00299 continue;
00300 }
00301
00302
00303 SettingGroup* loadedGroup = NULL;
00304
00305
00306 if(curGroup->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
00307 loadedGroup = curGroup;
00308
00309 else
00310 {
00311 SettingGroup* cur = firstGroup;
00312 while(cur != NULL)
00313 {
00314
00315 if(cur->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
00316 {
00317 loadedGroup = cur;
00318 break;
00319 }
00320
00321 cur = cur->getNext();
00322 }
00323 }
00324
00325 if(loadedGroup == NULL)
00326 {
00327 loadedGroup = new SettingGroup( attributes.namedItem("name").nodeValue() );
00328 if(firstGroup == NULL)
00329 firstGroup = loadedGroup;
00330 else
00331 lastGroup->setNext(loadedGroup);
00332 lastGroup = loadedGroup;
00333 }
00334
00335 loadedGroup->loadSettings(node);
00336 }
00337
00338 node = node.nextSibling();
00339 }
00340
00341
00342 return true;
00343 }
00344
00345 bool Configuration::saveSettings()
00346 {
00347
00348 QFile file( settingsFilename );
00349 if(file.open(IO_WriteOnly))
00350 {
00351
00352 QTextStream stream;
00353 stream.setDevice( &file );
00354 stream.setEncoding( QTextStream::UnicodeUTF8 );
00355
00356
00357 stream << "<settings app=\"Album Shaper\" version=\"" << ALBUMSHAPER_VERSION << "\">\n";
00358
00359
00360 curGroup = firstGroup;
00361 while(curGroup != NULL)
00362 {
00363 curGroup->saveSettings( stream );
00364 curGroup = curGroup->getNext();
00365 }
00366
00367
00368 stream << "</settings>\n";
00369
00370
00371 file.close();
00372 return true;
00373 }
00374
00375
00376 file.close();
00377 return false;
00378 }
00379