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