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