settings.cpp
author insilmaril
Tue Jan 24 15:09:48 2006 +0000 (2006-01-24)
changeset 183 97e0bd468439
parent 163 30b22f7bd009
child 188 90969608255c
permissions -rw-r--r--
Introduced basic export to Open Document format
     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 				*itv=value;
   130 				return;
   131 			}
   132 			itk++;
   133 			itv++;
   134 		}
   135 		
   136 		// If no entry exists, append a new one
   137 		keylist.append (key);
   138 		valuelist.append (value);
   139 	}
   140 }
   141 
   142 
   143 
   144 /////////////////////////////////////////////////////////////////
   145 // Settings
   146 /////////////////////////////////////////////////////////////////
   147 Settings::Settings()
   148 {
   149 	clear();		 
   150 }
   151 
   152 Settings::~Settings()
   153 {
   154 }
   155 
   156 void Settings::clear()
   157 {
   158 	pathlist.clear();
   159 	keylist.clear();
   160 	valuelist.clear();
   161 }
   162 
   163 void Settings::clearLocal(const QString &s)
   164 {
   165 	QStringList::Iterator itp=pathlist.begin();
   166 	QStringList::Iterator itk=keylist.begin();
   167 	QStringList::Iterator itv=valuelist.begin();
   168 
   169 	while (itp !=pathlist.end() )
   170 	{
   171 		if ((*itk).startsWith (s))
   172 		{
   173 			itp=pathlist.remove (itp);
   174 			itk=keylist.remove (itk);
   175 			itv=valuelist.remove (itv);
   176 		}	else
   177 		{
   178 			itp++;
   179 			itk++;
   180 			itv++;
   181 		}
   182 	}
   183 }
   184 
   185 QString Settings::readLocalEntry ( const QString &fpath, const QString & key, const QString & def = QString::null ) 
   186 {
   187 	QStringList::Iterator itp=pathlist.begin();
   188 	QStringList::Iterator itk=keylist.begin();
   189 	QStringList::Iterator itv=valuelist.begin();
   190 
   191 	// First search for value in settings saved in map
   192 	while (itp !=pathlist.end() )
   193 	{
   194 		if (*itp == fpath && *itk == key)
   195 			return *itv;
   196 		itp++;
   197 		itk++;
   198 		itv++;
   199 	}
   200 
   201 	// Fall back to global vym settings
   202 	bool ok;
   203 	return readEntry (key,def, &ok);
   204 }	
   205 
   206 void Settings::setLocalEntry (const QString &fpath, const QString &key, const QString &value)
   207 {
   208 	QStringList::Iterator itp=pathlist.begin();
   209 	QStringList::Iterator itk=keylist.begin();
   210 	QStringList::Iterator itv=valuelist.begin();
   211 
   212 	if (!fpath.isEmpty() && !key.isEmpty() && !value.isEmpty() )
   213 	{
   214 		// Search for existing entry first
   215 		while (itp !=pathlist.end() )
   216 		{
   217 			if (*itp == fpath && *itk == key)
   218 			{
   219 				*itv=value;
   220 				return;
   221 			}
   222 			itp++;
   223 			itk++;
   224 			itv++;
   225 		}
   226 		
   227 		// If no entry exists, append a new one
   228 		pathlist.append (fpath);
   229 		keylist.append (key);
   230 		valuelist.append (value);
   231 	}
   232 }
   233 
   234 QString Settings::getXMLData (const QString &fpath)
   235 {
   236 	QString s;
   237 	QStringList::Iterator itp=pathlist.begin();
   238 	QStringList::Iterator itk=keylist.begin();
   239 	QStringList::Iterator itv=valuelist.begin();
   240 
   241 	while (itp !=pathlist.end() )
   242 	{
   243 		if (*itp == fpath )
   244 			if (!(*itv).isEmpty())
   245 				s+=singleElement (
   246 					"setting",
   247 					attribut ("key",*itk) 
   248 					+attribut ("value",*itv)
   249 				)+"\n";
   250 		itp++;
   251 		itk++;
   252 		itv++;
   253 	}
   254 	return s;
   255 }
   256