settings.cpp
author insilmaril
Wed, 25 Apr 2007 16:02:54 +0000
changeset 463 694f5e35e87c
parent 378 1ab7353f8f44
child 619 b48f072a347a
permissions -rw-r--r--
started doxygen documentation
     1 #include <iostream>
     2 #include <qregexp.h>
     3 #include "settings.h"
     4 #include "file.h"
     5 
     6 using namespace std;
     7 
     8 /////////////////////////////////////////////////////////////////
     9 // SimpleSettings
    10 /////////////////////////////////////////////////////////////////
    11 SimpleSettings::SimpleSettings()
    12 {
    13 	clear();		 
    14 }
    15 
    16 SimpleSettings::~SimpleSettings()
    17 {
    18 }
    19 
    20 void SimpleSettings::clear()
    21 {
    22 	keylist.clear();
    23 	valuelist.clear();
    24 }
    25 
    26 void SimpleSettings::readSettings (const QString &path)
    27 {
    28 	QString s;
    29 	if (!loadStringFromDisk(path,s)) 
    30 	{
    31 		qWarning ("SimpleSettings::readSettings() Couldn't read "+path);
    32 		return;
    33 	}	
    34 	QStringList lines;
    35 	lines=QStringList::split (QRegExp("\n"),s,false);
    36 	int i;
    37 	QStringList::Iterator it=lines.begin();
    38 	while (it !=lines.end() )
    39 	{
    40 		i=(*it).find("=",0);
    41 		keylist.append((*it).left(i));
    42 		valuelist.append((*it).right((*it).length()-i-1));
    43 		it++;
    44 	}
    45 }
    46 
    47 void SimpleSettings::writeSettings (const QString &path)
    48 {
    49 	QString s;
    50 	QStringList::Iterator itk=keylist.begin();
    51 	QStringList::Iterator itv=valuelist.begin();
    52 
    53 	// First search for value in settings saved in map
    54 	while (itk !=keylist.end() )
    55 	{
    56 		s+=*itk+"="+*itv+"\n";
    57 		itk++;
    58 		itv++;
    59 	}
    60 	if (!saveStringToDisk(path,s)) 
    61 		qWarning ("SimpleSettings::writeSettings() Couldn't write "+path);
    62 }
    63 
    64 /*
    65 QString SimpleSettings::readEntry (const QString &key)
    66 {
    67 	QStringList::Iterator itk=keylist.begin();
    68 	QStringList::Iterator itv=valuelist.begin();
    69 
    70 	// First search for value in settings saved in map
    71 	while (itk !=keylist.end() )
    72 	{
    73 		if (*itk == key)
    74 			return *itv;
    75 		itk++;
    76 		itv++;
    77 	}
    78 	qWarning ("SimpleSettings::readEntry()  Couldn't find key "+key);
    79 	return "";
    80 }
    81 */
    82 
    83 QString SimpleSettings::readEntry (const QString &key, const QString &def)
    84 {
    85 	QStringList::Iterator itk=keylist.begin();
    86 	QStringList::Iterator itv=valuelist.begin();
    87 
    88 	// First search for value in settings saved in map
    89 	while (itk !=keylist.end() )
    90 	{
    91 		if (*itk == key)
    92 			return *itv;
    93 		itk++;
    94 		itv++;
    95 	}
    96 	return def;
    97 }
    98 
    99 int SimpleSettings::readNumEntry (const QString &key, const int &def)
   100 {
   101 	QStringList::Iterator itk=keylist.begin();
   102 	QStringList::Iterator itv=valuelist.begin();
   103 
   104 	// First search for value in settings saved in map
   105 	while (itk !=keylist.end() )
   106 	{
   107 		if (*itk == key)
   108 		{
   109 			bool ok;
   110 			int i=(*itv).toInt(&ok,10);
   111 			if (ok)
   112 				return i;
   113 			else
   114 				return def;
   115 		}	
   116 		itk++;
   117 		itv++;
   118 	}
   119 	return def;
   120 }
   121 
   122 void SimpleSettings::setEntry (const QString &key, const QString &value)
   123 {
   124 	QStringList::Iterator itk=keylist.begin();
   125 	QStringList::Iterator itv=valuelist.begin();
   126 
   127 	if (!key.isEmpty() )
   128 	{
   129 		// Search for existing entry first
   130 		while (itk !=keylist.end() )
   131 		{
   132 			if (*itk == key)
   133 			{
   134 				if (!value.isEmpty())
   135 					*itv=value;
   136 				else
   137 					*itv="";
   138 				*itv=value;
   139 				return;
   140 			}
   141 			itk++;
   142 			itv++;
   143 		}
   144 		
   145 		// If no entry exists, append a new one
   146 		keylist.append (key);
   147 		valuelist.append (value);
   148 	}
   149 }
   150 
   151 
   152 
   153 /////////////////////////////////////////////////////////////////
   154 // Settings
   155 /////////////////////////////////////////////////////////////////
   156 Settings::Settings()
   157 {
   158 	clear();		 
   159 }
   160 
   161 Settings::Settings(const QString & organization, const QString & application ):QSettings (organization,application)
   162 {
   163 	clear();		 
   164 }
   165 
   166 Settings::~Settings()
   167 {
   168 }
   169 
   170 void Settings::clear()
   171 {
   172 	pathlist.clear();
   173 	keylist.clear();
   174 	valuelist.clear();
   175 }
   176 
   177 void Settings::clearLocal(const QString &s)
   178 {
   179 	QStringList::Iterator itp=pathlist.begin();
   180 	QStringList::Iterator itk=keylist.begin();
   181 	QStringList::Iterator itv=valuelist.begin();
   182 
   183 	while (itp !=pathlist.end() )
   184 	{
   185 		if ((*itk).startsWith (s))
   186 		{
   187 			itp=pathlist.remove (itp);
   188 			itk=keylist.remove (itk);
   189 			itv=valuelist.remove (itv);
   190 		}	else
   191 		{
   192 			itp++;
   193 			itk++;
   194 			itv++;
   195 		}
   196 	}
   197 }
   198 
   199 QString Settings::readLocalEntry ( const QString &fpath, const QString & key, const QString & def = QString::null ) 
   200 {
   201 	QStringList::Iterator itp=pathlist.begin();
   202 	QStringList::Iterator itk=keylist.begin();
   203 	QStringList::Iterator itv=valuelist.begin();
   204 
   205 	// First search for value in settings saved in map
   206 	while (itp !=pathlist.end() )
   207 	{
   208 		if (*itp == fpath && *itk == key)
   209 			return *itv;
   210 		itp++;
   211 		itk++;
   212 		itv++;
   213 	}
   214 
   215 	// Fall back to global vym settings
   216 	bool ok;
   217 	return readEntry (key,def, &ok);
   218 }	
   219 
   220 void Settings::setLocalEntry (const QString &fpath, const QString &key, const QString &value)
   221 {
   222 	QStringList::Iterator itp=pathlist.begin();
   223 	QStringList::Iterator itk=keylist.begin();
   224 	QStringList::Iterator itv=valuelist.begin();
   225 
   226 	if (!fpath.isEmpty() && !key.isEmpty() && !value.isEmpty() )
   227 	{
   228 		// Search for existing entry first
   229 		while (itp !=pathlist.end() )
   230 		{
   231 			if (*itp == fpath && *itk == key)
   232 			{
   233 				*itv=value;
   234 				return;
   235 			}
   236 			itp++;
   237 			itk++;
   238 			itv++;
   239 		}
   240 		
   241 		// If no entry exists, append a new one
   242 		pathlist.append (fpath);
   243 		keylist.append (key);
   244 		valuelist.append (value);
   245 	}
   246 }
   247 
   248 QString Settings::getXMLData (const QString &fpath)
   249 {
   250 	QString s;
   251 	QStringList::Iterator itp=pathlist.begin();
   252 	QStringList::Iterator itk=keylist.begin();
   253 	QStringList::Iterator itv=valuelist.begin();
   254 
   255 	while (itp !=pathlist.end() )
   256 	{
   257 		if (*itp == fpath )
   258 			if (!(*itv).isEmpty())
   259 				s+=singleElement (
   260 					"setting",
   261 					attribut ("key",*itk) 
   262 					+attribut ("value",*itv)
   263 				)+"\n";
   264 		itp++;
   265 		itk++;
   266 		itv++;
   267 	}
   268 	return s;
   269 }
   270