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