configuration.cpp

Go to the documentation of this file.
00001 //==============================================
00002 //  copyright            : (C) 2003-2005 by Will Stokes
00003 //==============================================
00004 //  This program is free software; you can redistribute it
00005 //  and/or modify it under the terms of the GNU General
00006 //  Public License as published by the Free Software
00007 //  Foundation; either version 2 of the License, or
00008 //  (at your option) any later version.
00009 //==============================================
00010 
00011 //Systemwide includes
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 //Projectwide includes
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   //PLATFORM_SPECIFIC_CODE
00032  
00033   //-----------------------------
00034   //Mac OSX requires no directories to be created
00035   #if defined(Q_OS_MACX)
00036   return true;
00037   //-----------------------------
00038   //Windows
00039   #elif defined(Q_OS_WIN)
00040   bool configDirMade = true;
00041 
00042   //attempt to get folder location using windows api, if this fails try hard coded path as a last resort
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   //Unix/Linux/BSD
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   //Determine settings filename
00072   //-----------------------------
00073 
00074   //PLATFORM_SPECIFIC_CODE
00075   
00076   //Mac OS X
00077   #if defined(Q_OS_MACX)
00078   settingsFilename = QDir::homeDirPath() + QString("/Library/Preferences/net.sourceforge.albumshaper.xml");
00079   //----------------------------- 
00080   //Windows
00081   #elif defined(Q_OS_WIN)
00082   //attempt to get folder location using windows api, if this fails try hard coded path as a last resort
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   //Unix/Linux/BSD
00091   #else
00092   settingsFilename = QDir::homeDirPath() + QString("/.albumShaper/settings.xml");
00093   #endif
00094   //-----------------------------
00095  
00096   //no groups by default
00097   firstGroup = NULL;
00098   lastGroup = NULL;
00099 
00100   //no group selected by default
00101   curGroup = NULL;
00102 }
00103 //==============================================
00104 Configuration::~Configuration()
00105 {
00106   //delete all setting groups
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   //check if cached group has same name, if not find group, create it if group does not exist
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     //if we have not found the group create it and add to list
00130     if(curGroup == NULL)
00131     {
00132       //create new group
00133       curGroup = new SettingGroup(group);
00134 
00135       //add group to list
00136       if(firstGroup == NULL)
00137         firstGroup = curGroup;
00138       else
00139         lastGroup->setNext(curGroup);
00140       lastGroup = curGroup;
00141     }
00142   }
00143 
00144   //set setting value
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   //check if cached group is correct group, if not find correct group
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     //if we have not found the group return error value (-1)
00172     if(curGroup == NULL)
00173     {
00174       return "-1";
00175     }
00176   }
00177 
00178   //return setting value from group
00179   return curGroup->getValue(key);
00180 }
00181 //==============================================
00182 void Configuration::resetSetting(QString group, QString key)
00183 {
00184   //check if cached group is correct group, if not find correct group
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     //if we have not found the group return error value (-1)
00196     if(curGroup == NULL)
00197     {
00198       return;
00199     }
00200   }
00201 
00202   //return setting value from group
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   //iterate through groups, remove group once found
00229   SettingGroup* prev = NULL;
00230   curGroup = firstGroup;
00231   while(curGroup != NULL)
00232   {
00233     //found
00234     if(curGroup->getName().compare(group) == 0)
00235     {
00236       //keep handle on group for deletion purposes
00237       SettingGroup* temp = curGroup;
00238 
00239       //fix head if necessary
00240       if(curGroup == firstGroup)
00241         firstGroup = curGroup->getNext();
00242             
00243       //fix tail if necessary
00244       if(lastGroup == curGroup)
00245         lastGroup = prev;
00246 
00247       //splice out group
00248       if(prev != NULL)
00249         prev->setNext( curGroup->getNext() );                 
00250       
00251       //update curGroup pointer so valid
00252       curGroup = curGroup->getNext();
00253 
00254       //free group
00255       delete temp;
00256       temp = NULL;
00257       
00258       //done       
00259       return;
00260     }
00261     
00262     //update prev and cur pointers and move along
00263     prev = curGroup;
00264     curGroup = curGroup->getNext();        
00265   }  
00266 }
00267 //==============================================
00268 bool Configuration::loadSettings()
00269 {
00270   //-----------------------------------
00271   //attempt to load xml settings file and construct dom, if either action failes return false
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   //walk though DOM and look for setting nodes.
00284   //for each setting fetch, type, key, and value
00285   //walk through list of settings and find previous setting
00286   //if previous setting found replace value, otherwise add new setting to list
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       //find group name, if no name found then move on to next group
00295       QDomNamedNodeMap attributes = node.attributes();
00296       if(attributes.namedItem("name").isNull())
00297       {
00298         node = node.nextSibling();
00299         continue;
00300       }
00301 
00302       //create group if it does not already exist
00303       SettingGroup* loadedGroup = NULL;
00304 
00305       //last used group is the one we are looking for
00306       if(curGroup->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
00307         loadedGroup = curGroup;
00308       //search list of groups
00309       else
00310       {
00311         SettingGroup* cur = firstGroup;
00312         while(cur != NULL)
00313          {
00314            //found it!
00315            if(cur->getName().compare( attributes.namedItem("name").nodeValue()) == 0)
00316            {
00317              loadedGroup = cur;
00318              break;
00319            }
00320           //nope, move on to next group
00321           cur = cur->getNext();
00322         }
00323       }
00324       //if group to be loaded is not found then create it
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     //move on to next setting
00338     node = node.nextSibling();
00339   }
00340   //-----------------------------------
00341   //loading of settingings was successful
00342   return true;
00343 }
00344 //==============================================
00345 bool Configuration::saveSettings()
00346 {
00347   //create/open html file
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     //write header
00357     stream << "<settings app=\"Album Shaper\" version=\"" << ALBUMSHAPER_VERSION << "\">\n";
00358 
00359     //iterate over every group
00360     curGroup = firstGroup;
00361     while(curGroup != NULL)
00362     {
00363       curGroup->saveSettings( stream );
00364       curGroup = curGroup->getNext();
00365     }
00366 
00367     //end xml file
00368     stream << "</settings>\n";
00369 
00370     //success saving settings!
00371     file.close();
00372     return true;
00373   }
00374 
00375   //opening file for saving failed
00376   file.close();
00377   return false;
00378 }
00379 //==============================================

Generated on Wed Nov 8 16:37:13 2006 for AlbumShaper by  doxygen 1.4.7