settings.cpp
branchrelease-1-12-maintained
changeset 55 79b0656dbe7d
parent 0 7a96bd401351
     1.1 --- a/settings.cpp	Mon Dec 01 16:44:35 2008 +0000
     1.2 +++ b/settings.cpp	Thu Dec 04 13:47:47 2008 +0000
     1.3 @@ -1,8 +1,153 @@
     1.4  #include <iostream>
     1.5 +#include <qregexp.h>
     1.6  #include "settings.h"
     1.7 -#include "misc.h"
     1.8 +#include "file.h"
     1.9  
    1.10 -using namespace std;
    1.11 +/////////////////////////////////////////////////////////////////
    1.12 +// SimpleSettings
    1.13 +/////////////////////////////////////////////////////////////////
    1.14 +SimpleSettings::SimpleSettings()
    1.15 +{
    1.16 +	clear();		 
    1.17 +}
    1.18 +
    1.19 +SimpleSettings::~SimpleSettings()
    1.20 +{
    1.21 +}
    1.22 +
    1.23 +void SimpleSettings::clear()
    1.24 +{
    1.25 +	keylist.clear();
    1.26 +	valuelist.clear();
    1.27 +}
    1.28 +
    1.29 +void SimpleSettings::readSettings (const QString &path)
    1.30 +{
    1.31 +	QString s;
    1.32 +	if (!loadStringFromDisk(path,s)) 
    1.33 +	{
    1.34 +		qWarning ("SimpleSettings::readSettings() Couldn't read "+path);
    1.35 +		return;
    1.36 +	}	
    1.37 +	QStringList lines;
    1.38 +	lines=QStringList::split (QRegExp("\n"),s,false);
    1.39 +	int i;
    1.40 +	QStringList::Iterator it=lines.begin();
    1.41 +	while (it !=lines.end() )
    1.42 +	{
    1.43 +		i=(*it).find("=",0);
    1.44 +		keylist.append((*it).left(i));
    1.45 +		valuelist.append((*it).right((*it).length()-i-1));
    1.46 +		it++;
    1.47 +	}
    1.48 +}
    1.49 +
    1.50 +void SimpleSettings::writeSettings (const QString &path)
    1.51 +{
    1.52 +	QString s;
    1.53 +	QStringList::Iterator itk=keylist.begin();
    1.54 +	QStringList::Iterator itv=valuelist.begin();
    1.55 +
    1.56 +	// First search for value in settings saved in map
    1.57 +	while (itk !=keylist.end() )
    1.58 +	{
    1.59 +		s+=*itk+"="+*itv+"\n";
    1.60 +		itk++;
    1.61 +		itv++;
    1.62 +	}
    1.63 +	if (!saveStringToDisk(path,s)) 
    1.64 +		qWarning ("SimpleSettings::writeSettings() Couldn't write "+path);
    1.65 +}
    1.66 +
    1.67 +/*
    1.68 +QString SimpleSettings::readEntry (const QString &key)
    1.69 +{
    1.70 +	QStringList::Iterator itk=keylist.begin();
    1.71 +	QStringList::Iterator itv=valuelist.begin();
    1.72 +
    1.73 +	// First search for value in settings saved in map
    1.74 +	while (itk !=keylist.end() )
    1.75 +	{
    1.76 +		if (*itk == key)
    1.77 +			return *itv;
    1.78 +		itk++;
    1.79 +		itv++;
    1.80 +	}
    1.81 +	qWarning ("SimpleSettings::readEntry()  Couldn't find key "+key);
    1.82 +	return "";
    1.83 +}
    1.84 +*/
    1.85 +
    1.86 +QString SimpleSettings::readEntry (const QString &key, const QString &def)
    1.87 +{
    1.88 +	QStringList::Iterator itk=keylist.begin();
    1.89 +	QStringList::Iterator itv=valuelist.begin();
    1.90 +
    1.91 +	// First search for value in settings saved in map
    1.92 +	while (itk !=keylist.end() )
    1.93 +	{
    1.94 +		if (*itk == key)
    1.95 +			return *itv;
    1.96 +		itk++;
    1.97 +		itv++;
    1.98 +	}
    1.99 +	return def;
   1.100 +}
   1.101 +
   1.102 +int SimpleSettings::readNumEntry (const QString &key, const int &def)
   1.103 +{
   1.104 +	QStringList::Iterator itk=keylist.begin();
   1.105 +	QStringList::Iterator itv=valuelist.begin();
   1.106 +
   1.107 +	// First search for value in settings saved in map
   1.108 +	while (itk !=keylist.end() )
   1.109 +	{
   1.110 +		if (*itk == key)
   1.111 +		{
   1.112 +			bool ok;
   1.113 +			int i=(*itv).toInt(&ok,10);
   1.114 +			if (ok)
   1.115 +				return i;
   1.116 +			else
   1.117 +				return def;
   1.118 +		}	
   1.119 +		itk++;
   1.120 +		itv++;
   1.121 +	}
   1.122 +	return def;
   1.123 +}
   1.124 +
   1.125 +void SimpleSettings::setEntry (const QString &key, const QString &value)
   1.126 +{
   1.127 +	QStringList::Iterator itk=keylist.begin();
   1.128 +	QStringList::Iterator itv=valuelist.begin();
   1.129 +
   1.130 +	if (!key.isEmpty() )
   1.131 +	{
   1.132 +		// Search for existing entry first
   1.133 +		while (itk !=keylist.end() )
   1.134 +		{
   1.135 +			if (*itk == key)
   1.136 +			{
   1.137 +				if (!value.isEmpty())
   1.138 +					*itv=value;
   1.139 +				else
   1.140 +					*itv="";
   1.141 +				*itv=value;
   1.142 +				return;
   1.143 +			}
   1.144 +			itk++;
   1.145 +			itv++;
   1.146 +		}
   1.147 +		
   1.148 +		// If no entry exists, append a new one
   1.149 +		keylist.append (key);
   1.150 +		valuelist.append (value);
   1.151 +	}
   1.152 +}
   1.153 +
   1.154 +
   1.155 +
   1.156  /////////////////////////////////////////////////////////////////
   1.157  // Settings
   1.158  /////////////////////////////////////////////////////////////////
   1.159 @@ -11,6 +156,11 @@
   1.160  	clear();		 
   1.161  }
   1.162  
   1.163 +Settings::Settings(const QString & organization, const QString & application ):QSettings (organization,application)
   1.164 +{
   1.165 +	clear();		 
   1.166 +}
   1.167 +
   1.168  Settings::~Settings()
   1.169  {
   1.170  }
   1.171 @@ -93,7 +243,7 @@
   1.172  	}
   1.173  }
   1.174  
   1.175 -QString Settings::getXMLData (const QString &fpath)
   1.176 +QString Settings::getDataXML (const QString &fpath)
   1.177  {
   1.178  	QString s;
   1.179  	QStringList::Iterator itp=pathlist.begin();
   1.180 @@ -116,17 +266,3 @@
   1.181  	return s;
   1.182  }
   1.183  
   1.184 -void Settings::write()
   1.185 -{
   1.186 -	QStringList::Iterator itp=pathlist.begin();
   1.187 -	QStringList::Iterator itk=keylist.begin();
   1.188 -	QStringList::Iterator itv=valuelist.begin();
   1.189 -
   1.190 -	while (itp !=pathlist.end() )
   1.191 -	{
   1.192 -		itp++;
   1.193 -		itk++;
   1.194 -		itv++;
   1.195 -	}
   1.196 -}
   1.197 -