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