settings.cpp
author insilmaril
Fri, 24 Feb 2006 11:22:50 +0000
changeset 222 4b872954721b
parent 188 90969608255c
child 366 e95081c21da2
permissions -rw-r--r--
oops. removed debugging output from xml.cpp
     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 QString SimpleSettings::readEntry (const QString &key)
    65 {
    66 	QStringList::Iterator itk=keylist.begin();
    67 	QStringList::Iterator itv=valuelist.begin();
    68 
    69 	// First search for value in settings saved in map
    70 	while (itk !=keylist.end() )
    71 	{
    72 		if (*itk == key)
    73 			return *itv;
    74 		itk++;
    75 		itv++;
    76 	}
    77 	qWarning ("SimpleSettings::readEntry()  Couldn't find key "+key);
    78 	return "";
    79 }
    80 
    81 void SimpleSettings::setEntry (const QString &key, const QString &value)
    82 {
    83 	QStringList::Iterator itk=keylist.begin();
    84 	QStringList::Iterator itv=valuelist.begin();
    85 
    86 	if (!key.isEmpty() )
    87 	{
    88 		// Search for existing entry first
    89 		while (itk !=keylist.end() )
    90 		{
    91 			if (*itk == key)
    92 			{
    93 				if (!value.isEmpty())
    94 					*itv=value;
    95 				else
    96 					*itv="";
    97 				*itv=value;
    98 				return;
    99 			}
   100 			itk++;
   101 			itv++;
   102 		}
   103 		
   104 		// If no entry exists, append a new one
   105 		keylist.append (key);
   106 		valuelist.append (value);
   107 	}
   108 }
   109 
   110 
   111 
   112 /////////////////////////////////////////////////////////////////
   113 // Settings
   114 /////////////////////////////////////////////////////////////////
   115 Settings::Settings()
   116 {
   117 	clear();		 
   118 }
   119 
   120 Settings::~Settings()
   121 {
   122 }
   123 
   124 void Settings::clear()
   125 {
   126 	pathlist.clear();
   127 	keylist.clear();
   128 	valuelist.clear();
   129 }
   130 
   131 void Settings::clearLocal(const QString &s)
   132 {
   133 	QStringList::Iterator itp=pathlist.begin();
   134 	QStringList::Iterator itk=keylist.begin();
   135 	QStringList::Iterator itv=valuelist.begin();
   136 
   137 	while (itp !=pathlist.end() )
   138 	{
   139 		if ((*itk).startsWith (s))
   140 		{
   141 			itp=pathlist.remove (itp);
   142 			itk=keylist.remove (itk);
   143 			itv=valuelist.remove (itv);
   144 		}	else
   145 		{
   146 			itp++;
   147 			itk++;
   148 			itv++;
   149 		}
   150 	}
   151 }
   152 
   153 QString Settings::readLocalEntry ( const QString &fpath, const QString & key, const QString & def = QString::null ) 
   154 {
   155 	QStringList::Iterator itp=pathlist.begin();
   156 	QStringList::Iterator itk=keylist.begin();
   157 	QStringList::Iterator itv=valuelist.begin();
   158 
   159 	// First search for value in settings saved in map
   160 	while (itp !=pathlist.end() )
   161 	{
   162 		if (*itp == fpath && *itk == key)
   163 			return *itv;
   164 		itp++;
   165 		itk++;
   166 		itv++;
   167 	}
   168 
   169 	// Fall back to global vym settings
   170 	bool ok;
   171 	return readEntry (key,def, &ok);
   172 }	
   173 
   174 void Settings::setLocalEntry (const QString &fpath, const QString &key, const QString &value)
   175 {
   176 	QStringList::Iterator itp=pathlist.begin();
   177 	QStringList::Iterator itk=keylist.begin();
   178 	QStringList::Iterator itv=valuelist.begin();
   179 
   180 	if (!fpath.isEmpty() && !key.isEmpty() && !value.isEmpty() )
   181 	{
   182 		// Search for existing entry first
   183 		while (itp !=pathlist.end() )
   184 		{
   185 			if (*itp == fpath && *itk == key)
   186 			{
   187 				*itv=value;
   188 				return;
   189 			}
   190 			itp++;
   191 			itk++;
   192 			itv++;
   193 		}
   194 		
   195 		// If no entry exists, append a new one
   196 		pathlist.append (fpath);
   197 		keylist.append (key);
   198 		valuelist.append (value);
   199 	}
   200 }
   201 
   202 QString Settings::getXMLData (const QString &fpath)
   203 {
   204 	QString s;
   205 	QStringList::Iterator itp=pathlist.begin();
   206 	QStringList::Iterator itk=keylist.begin();
   207 	QStringList::Iterator itv=valuelist.begin();
   208 
   209 	while (itp !=pathlist.end() )
   210 	{
   211 		if (*itp == fpath )
   212 			if (!(*itv).isEmpty())
   213 				s+=singleElement (
   214 					"setting",
   215 					attribut ("key",*itk) 
   216 					+attribut ("value",*itv)
   217 				)+"\n";
   218 		itp++;
   219 		itk++;
   220 		itv++;
   221 	}
   222 	return s;
   223 }
   224