settings.cpp
author insilmaril
Thu, 06 Jul 2006 14:47:01 +0000
branchqt4-port
changeset 9 f94317a94db1
parent 3 6a0342b3c519
child 16 41c3d7f9f532
permissions -rw-r--r--
Text Editor now shows states again
     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(const QString & organization, const QString & application ):QSettings (organization,application)
   121 {
   122 	clear();		 
   123 }
   124 
   125 Settings::~Settings()
   126 {
   127 }
   128 
   129 void Settings::clear()
   130 {
   131 	pathlist.clear();
   132 	keylist.clear();
   133 	valuelist.clear();
   134 }
   135 
   136 void Settings::clearLocal(const QString &s)
   137 {
   138 	QStringList::Iterator itp=pathlist.begin();
   139 	QStringList::Iterator itk=keylist.begin();
   140 	QStringList::Iterator itv=valuelist.begin();
   141 
   142 	while (itp !=pathlist.end() )
   143 	{
   144 		if ((*itk).startsWith (s))
   145 		{
   146 			itp=pathlist.remove (itp);
   147 			itk=keylist.remove (itk);
   148 			itv=valuelist.remove (itv);
   149 		}	else
   150 		{
   151 			itp++;
   152 			itk++;
   153 			itv++;
   154 		}
   155 	}
   156 }
   157 
   158 QString Settings::readLocalEntry ( const QString &fpath, const QString & key, const QString & def = QString::null ) 
   159 {
   160 	QStringList::Iterator itp=pathlist.begin();
   161 	QStringList::Iterator itk=keylist.begin();
   162 	QStringList::Iterator itv=valuelist.begin();
   163 
   164 	// First search for value in settings saved in map
   165 	while (itp !=pathlist.end() )
   166 	{
   167 		if (*itp == fpath && *itk == key)
   168 			return *itv;
   169 		itp++;
   170 		itk++;
   171 		itv++;
   172 	}
   173 
   174 	// Fall back to global vym settings
   175 	bool ok;
   176 	return readEntry (key,def, &ok);
   177 }	
   178 
   179 void Settings::setLocalEntry (const QString &fpath, const QString &key, const QString &value)
   180 {
   181 	QStringList::Iterator itp=pathlist.begin();
   182 	QStringList::Iterator itk=keylist.begin();
   183 	QStringList::Iterator itv=valuelist.begin();
   184 
   185 	if (!fpath.isEmpty() && !key.isEmpty() && !value.isEmpty() )
   186 	{
   187 		// Search for existing entry first
   188 		while (itp !=pathlist.end() )
   189 		{
   190 			if (*itp == fpath && *itk == key)
   191 			{
   192 				*itv=value;
   193 				return;
   194 			}
   195 			itp++;
   196 			itk++;
   197 			itv++;
   198 		}
   199 		
   200 		// If no entry exists, append a new one
   201 		pathlist.append (fpath);
   202 		keylist.append (key);
   203 		valuelist.append (value);
   204 	}
   205 }
   206 
   207 QString Settings::getXMLData (const QString &fpath)
   208 {
   209 	QString s;
   210 	QStringList::Iterator itp=pathlist.begin();
   211 	QStringList::Iterator itk=keylist.begin();
   212 	QStringList::Iterator itv=valuelist.begin();
   213 
   214 	while (itp !=pathlist.end() )
   215 	{
   216 		if (*itp == fpath )
   217 			if (!(*itv).isEmpty())
   218 				s+=singleElement (
   219 					"setting",
   220 					attribut ("key",*itk) 
   221 					+attribut ("value",*itv)
   222 				)+"\n";
   223 		itp++;
   224 		itk++;
   225 		itv++;
   226 	}
   227 	return s;
   228 }
   229