settinggroup.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 <qstring.h>
00013 #include <qtextstream.h>
00014 #include <qdom.h>
00015 
00016 //Projectwide includes
00017 #include "settinggroup.h"
00018 #include "setting.h"
00019 
00020 //==============================================
00021 SettingGroup::SettingGroup(QString name)
00022 {
00023   this->name = name;
00024   firstSetting = NULL;
00025   lastSetting = NULL;
00026   next = NULL;
00027 }
00028 //==============================================
00029 SettingGroup::~SettingGroup()
00030 {
00031   Setting* cur = firstSetting;
00032   while(cur != NULL)
00033   {
00034     Setting* t = cur->getNext();
00035     delete cur;
00036     cur = t;
00037   }
00038 }
00039 //==============================================
00040 QString SettingGroup::getName()
00041 {
00042   return name;
00043 }
00044 //==============================================
00045 QString SettingGroup::getValue(QString key)
00046 {
00047   Setting* cur = firstSetting;
00048   while(cur != NULL)
00049   {
00050     if(cur->getKey().compare(key) == 0)
00051     {
00052       return cur->getValue();
00053     }
00054     cur = cur->getNext();
00055   }
00056   return "-1";
00057 }
00058 //==============================================
00059 void SettingGroup::resetSetting(QString key)
00060 {
00061   Setting* cur = firstSetting;
00062   while(cur != NULL)
00063   {
00064     if(cur->getKey().compare(key) == 0)
00065     {
00066       cur->resetSetting();
00067     }
00068     cur = cur->getNext();
00069   }
00070 }
00071 //==============================================
00072 void SettingGroup::setValue(QString key, QString value)
00073 {
00074   Setting* cur = firstSetting;
00075   while(cur != NULL)
00076   {
00077     if(cur->getKey().compare(key) == 0)
00078     {
00079       cur->setValue(value);
00080       return;
00081     }
00082     cur = cur->getNext();
00083   }
00084 
00085   //setting not found, create new one and append to list
00086   cur = new Setting(key, value);
00087   if(firstSetting == NULL)
00088     firstSetting = cur;
00089   else
00090     lastSetting->setNext(cur);
00091   lastSetting = cur;
00092 }
00093 //==============================================
00094 SettingGroup* SettingGroup::getNext()
00095 {
00096   return next;
00097 }
00098 //==============================================
00099 void SettingGroup::setNext(SettingGroup* next)
00100 {
00101   this->next = next;
00102 }
00103 //==============================================
00104 void SettingGroup::saveSettings(QTextStream& stream)
00105 {
00106   stream << "  <group name=\"" << getName() << "\">\n";
00107 
00108   //iterate over every setting
00109   Setting* cur = firstSetting;
00110   while(cur != NULL)
00111   {
00112     stream << "    <setting key=\"" << cur->getKey() << "\" value=\"" << cur->getValue() << "\"/>\n";
00113     cur = cur->getNext();
00114   }
00115 
00116   stream << "  </group>\n";
00117 }
00118 //==============================================
00119 void SettingGroup::loadSettings(QDomNode& root)
00120 {
00121   //iterate over all children (Settings)
00122   QDomNode node = root.firstChild();
00123   QDomText val;
00124   while( !node.isNull() )
00125   {
00126     if( node.isElement() && node.nodeName() == "setting" )
00127     {
00128       //find key and value, if either is missing move on to next setting
00129       QDomNamedNodeMap attributes = node.attributes();
00130       if(attributes.namedItem("key").isNull() || attributes.namedItem("value").isNull())
00131       {
00132         node = node.nextSibling();
00133         continue;
00134       }
00135 
00136 
00137       QString k = attributes.namedItem("key").nodeValue();
00138       QString v = attributes.namedItem("value").nodeValue();
00139 
00140       //key and value found -> add new setting
00141       setValue( attributes.namedItem("key").nodeValue(),
00142                     attributes.namedItem("value").nodeValue() );
00143     }
00144 
00145     //move on to next setting
00146     node = node.nextSibling();
00147   }
00148 }
00149 //==============================================

Generated on Wed Jan 24 05:38:05 2007 for AlbumShaper by  doxygen 1.5.1