settings.cpp
author insilmaril
Wed, 25 Feb 2009 12:44:10 +0000
branchrelease-1-12-maintained
changeset 57 d045ba89798e
parent 55 79b0656dbe7d
permissions -rw-r--r--
Bugfix: Enabled multiple mapcenters for export to OpenOffice.org
insilmaril@0
     1
#include <iostream>
insilmaril@55
     2
#include <qregexp.h>
insilmaril@0
     3
#include "settings.h"
insilmaril@55
     4
#include "file.h"
insilmaril@0
     5
insilmaril@55
     6
/////////////////////////////////////////////////////////////////
insilmaril@55
     7
// SimpleSettings
insilmaril@55
     8
/////////////////////////////////////////////////////////////////
insilmaril@55
     9
SimpleSettings::SimpleSettings()
insilmaril@55
    10
{
insilmaril@55
    11
	clear();		 
insilmaril@55
    12
}
insilmaril@55
    13
insilmaril@55
    14
SimpleSettings::~SimpleSettings()
insilmaril@55
    15
{
insilmaril@55
    16
}
insilmaril@55
    17
insilmaril@55
    18
void SimpleSettings::clear()
insilmaril@55
    19
{
insilmaril@55
    20
	keylist.clear();
insilmaril@55
    21
	valuelist.clear();
insilmaril@55
    22
}
insilmaril@55
    23
insilmaril@55
    24
void SimpleSettings::readSettings (const QString &path)
insilmaril@55
    25
{
insilmaril@55
    26
	QString s;
insilmaril@55
    27
	if (!loadStringFromDisk(path,s)) 
insilmaril@55
    28
	{
insilmaril@55
    29
		qWarning ("SimpleSettings::readSettings() Couldn't read "+path);
insilmaril@55
    30
		return;
insilmaril@55
    31
	}	
insilmaril@55
    32
	QStringList lines;
insilmaril@55
    33
	lines=QStringList::split (QRegExp("\n"),s,false);
insilmaril@55
    34
	int i;
insilmaril@55
    35
	QStringList::Iterator it=lines.begin();
insilmaril@55
    36
	while (it !=lines.end() )
insilmaril@55
    37
	{
insilmaril@55
    38
		i=(*it).find("=",0);
insilmaril@55
    39
		keylist.append((*it).left(i));
insilmaril@55
    40
		valuelist.append((*it).right((*it).length()-i-1));
insilmaril@55
    41
		it++;
insilmaril@55
    42
	}
insilmaril@55
    43
}
insilmaril@55
    44
insilmaril@55
    45
void SimpleSettings::writeSettings (const QString &path)
insilmaril@55
    46
{
insilmaril@55
    47
	QString s;
insilmaril@55
    48
	QStringList::Iterator itk=keylist.begin();
insilmaril@55
    49
	QStringList::Iterator itv=valuelist.begin();
insilmaril@55
    50
insilmaril@55
    51
	// First search for value in settings saved in map
insilmaril@55
    52
	while (itk !=keylist.end() )
insilmaril@55
    53
	{
insilmaril@55
    54
		s+=*itk+"="+*itv+"\n";
insilmaril@55
    55
		itk++;
insilmaril@55
    56
		itv++;
insilmaril@55
    57
	}
insilmaril@55
    58
	if (!saveStringToDisk(path,s)) 
insilmaril@55
    59
		qWarning ("SimpleSettings::writeSettings() Couldn't write "+path);
insilmaril@55
    60
}
insilmaril@55
    61
insilmaril@55
    62
/*
insilmaril@55
    63
QString SimpleSettings::readEntry (const QString &key)
insilmaril@55
    64
{
insilmaril@55
    65
	QStringList::Iterator itk=keylist.begin();
insilmaril@55
    66
	QStringList::Iterator itv=valuelist.begin();
insilmaril@55
    67
insilmaril@55
    68
	// First search for value in settings saved in map
insilmaril@55
    69
	while (itk !=keylist.end() )
insilmaril@55
    70
	{
insilmaril@55
    71
		if (*itk == key)
insilmaril@55
    72
			return *itv;
insilmaril@55
    73
		itk++;
insilmaril@55
    74
		itv++;
insilmaril@55
    75
	}
insilmaril@55
    76
	qWarning ("SimpleSettings::readEntry()  Couldn't find key "+key);
insilmaril@55
    77
	return "";
insilmaril@55
    78
}
insilmaril@55
    79
*/
insilmaril@55
    80
insilmaril@55
    81
QString SimpleSettings::readEntry (const QString &key, const QString &def)
insilmaril@55
    82
{
insilmaril@55
    83
	QStringList::Iterator itk=keylist.begin();
insilmaril@55
    84
	QStringList::Iterator itv=valuelist.begin();
insilmaril@55
    85
insilmaril@55
    86
	// First search for value in settings saved in map
insilmaril@55
    87
	while (itk !=keylist.end() )
insilmaril@55
    88
	{
insilmaril@55
    89
		if (*itk == key)
insilmaril@55
    90
			return *itv;
insilmaril@55
    91
		itk++;
insilmaril@55
    92
		itv++;
insilmaril@55
    93
	}
insilmaril@55
    94
	return def;
insilmaril@55
    95
}
insilmaril@55
    96
insilmaril@55
    97
int SimpleSettings::readNumEntry (const QString &key, const int &def)
insilmaril@55
    98
{
insilmaril@55
    99
	QStringList::Iterator itk=keylist.begin();
insilmaril@55
   100
	QStringList::Iterator itv=valuelist.begin();
insilmaril@55
   101
insilmaril@55
   102
	// First search for value in settings saved in map
insilmaril@55
   103
	while (itk !=keylist.end() )
insilmaril@55
   104
	{
insilmaril@55
   105
		if (*itk == key)
insilmaril@55
   106
		{
insilmaril@55
   107
			bool ok;
insilmaril@55
   108
			int i=(*itv).toInt(&ok,10);
insilmaril@55
   109
			if (ok)
insilmaril@55
   110
				return i;
insilmaril@55
   111
			else
insilmaril@55
   112
				return def;
insilmaril@55
   113
		}	
insilmaril@55
   114
		itk++;
insilmaril@55
   115
		itv++;
insilmaril@55
   116
	}
insilmaril@55
   117
	return def;
insilmaril@55
   118
}
insilmaril@55
   119
insilmaril@55
   120
void SimpleSettings::setEntry (const QString &key, const QString &value)
insilmaril@55
   121
{
insilmaril@55
   122
	QStringList::Iterator itk=keylist.begin();
insilmaril@55
   123
	QStringList::Iterator itv=valuelist.begin();
insilmaril@55
   124
insilmaril@55
   125
	if (!key.isEmpty() )
insilmaril@55
   126
	{
insilmaril@55
   127
		// Search for existing entry first
insilmaril@55
   128
		while (itk !=keylist.end() )
insilmaril@55
   129
		{
insilmaril@55
   130
			if (*itk == key)
insilmaril@55
   131
			{
insilmaril@55
   132
				if (!value.isEmpty())
insilmaril@55
   133
					*itv=value;
insilmaril@55
   134
				else
insilmaril@55
   135
					*itv="";
insilmaril@55
   136
				*itv=value;
insilmaril@55
   137
				return;
insilmaril@55
   138
			}
insilmaril@55
   139
			itk++;
insilmaril@55
   140
			itv++;
insilmaril@55
   141
		}
insilmaril@55
   142
		
insilmaril@55
   143
		// If no entry exists, append a new one
insilmaril@55
   144
		keylist.append (key);
insilmaril@55
   145
		valuelist.append (value);
insilmaril@55
   146
	}
insilmaril@55
   147
}
insilmaril@55
   148
insilmaril@55
   149
insilmaril@55
   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@55
   159
Settings::Settings(const QString & organization, const QString & application ):QSettings (organization,application)
insilmaril@55
   160
{
insilmaril@55
   161
	clear();		 
insilmaril@55
   162
}
insilmaril@55
   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@55
   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