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