settings.cpp
author insilmaril
Tue, 01 Aug 2006 09:30:57 +0000
changeset 363 3dc98c8843bc
parent 188 90969608255c
child 366 e95081c21da2
permissions -rw-r--r--
updated vym.pro to respect INSTALLDIR
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@163
    64
QString SimpleSettings::readEntry (const QString &key)
insilmaril@163
    65
{
insilmaril@163
    66
	QStringList::Iterator itk=keylist.begin();
insilmaril@163
    67
	QStringList::Iterator itv=valuelist.begin();
insilmaril@163
    68
insilmaril@163
    69
	// First search for value in settings saved in map
insilmaril@163
    70
	while (itk !=keylist.end() )
insilmaril@163
    71
	{
insilmaril@163
    72
		if (*itk == key)
insilmaril@163
    73
			return *itv;
insilmaril@163
    74
		itk++;
insilmaril@163
    75
		itv++;
insilmaril@163
    76
	}
insilmaril@163
    77
	qWarning ("SimpleSettings::readEntry()  Couldn't find key "+key);
insilmaril@163
    78
	return "";
insilmaril@163
    79
}
insilmaril@163
    80
insilmaril@163
    81
void SimpleSettings::setEntry (const QString &key, const QString &value)
insilmaril@163
    82
{
insilmaril@163
    83
	QStringList::Iterator itk=keylist.begin();
insilmaril@163
    84
	QStringList::Iterator itv=valuelist.begin();
insilmaril@163
    85
insilmaril@163
    86
	if (!key.isEmpty() )
insilmaril@163
    87
	{
insilmaril@163
    88
		// Search for existing entry first
insilmaril@163
    89
		while (itk !=keylist.end() )
insilmaril@163
    90
		{
insilmaril@163
    91
			if (*itk == key)
insilmaril@163
    92
			{
insilmaril@163
    93
				if (!value.isEmpty())
insilmaril@163
    94
					*itv=value;
insilmaril@163
    95
				else
insilmaril@163
    96
					*itv="";
insilmaril@164
    97
				*itv=value;
insilmaril@163
    98
				return;
insilmaril@163
    99
			}
insilmaril@163
   100
			itk++;
insilmaril@163
   101
			itv++;
insilmaril@163
   102
		}
insilmaril@163
   103
		
insilmaril@163
   104
		// If no entry exists, append a new one
insilmaril@163
   105
		keylist.append (key);
insilmaril@163
   106
		valuelist.append (value);
insilmaril@163
   107
	}
insilmaril@163
   108
}
insilmaril@163
   109
insilmaril@163
   110
insilmaril@163
   111
insilmaril@0
   112
/////////////////////////////////////////////////////////////////
insilmaril@0
   113
// Settings
insilmaril@0
   114
/////////////////////////////////////////////////////////////////
insilmaril@0
   115
Settings::Settings()
insilmaril@0
   116
{
insilmaril@0
   117
	clear();		 
insilmaril@0
   118
}
insilmaril@0
   119
insilmaril@0
   120
Settings::~Settings()
insilmaril@0
   121
{
insilmaril@0
   122
}
insilmaril@0
   123
insilmaril@0
   124
void Settings::clear()
insilmaril@0
   125
{
insilmaril@0
   126
	pathlist.clear();
insilmaril@0
   127
	keylist.clear();
insilmaril@0
   128
	valuelist.clear();
insilmaril@0
   129
}
insilmaril@0
   130
insilmaril@0
   131
void Settings::clearLocal(const QString &s)
insilmaril@0
   132
{
insilmaril@0
   133
	QStringList::Iterator itp=pathlist.begin();
insilmaril@0
   134
	QStringList::Iterator itk=keylist.begin();
insilmaril@0
   135
	QStringList::Iterator itv=valuelist.begin();
insilmaril@0
   136
insilmaril@0
   137
	while (itp !=pathlist.end() )
insilmaril@0
   138
	{
insilmaril@0
   139
		if ((*itk).startsWith (s))
insilmaril@0
   140
		{
insilmaril@0
   141
			itp=pathlist.remove (itp);
insilmaril@0
   142
			itk=keylist.remove (itk);
insilmaril@0
   143
			itv=valuelist.remove (itv);
insilmaril@0
   144
		}	else
insilmaril@0
   145
		{
insilmaril@0
   146
			itp++;
insilmaril@0
   147
			itk++;
insilmaril@0
   148
			itv++;
insilmaril@0
   149
		}
insilmaril@0
   150
	}
insilmaril@0
   151
}
insilmaril@0
   152
insilmaril@0
   153
QString Settings::readLocalEntry ( const QString &fpath, const QString & key, const QString & def = QString::null ) 
insilmaril@0
   154
{
insilmaril@0
   155
	QStringList::Iterator itp=pathlist.begin();
insilmaril@0
   156
	QStringList::Iterator itk=keylist.begin();
insilmaril@0
   157
	QStringList::Iterator itv=valuelist.begin();
insilmaril@0
   158
insilmaril@0
   159
	// First search for value in settings saved in map
insilmaril@0
   160
	while (itp !=pathlist.end() )
insilmaril@0
   161
	{
insilmaril@0
   162
		if (*itp == fpath && *itk == key)
insilmaril@0
   163
			return *itv;
insilmaril@0
   164
		itp++;
insilmaril@0
   165
		itk++;
insilmaril@0
   166
		itv++;
insilmaril@0
   167
	}
insilmaril@0
   168
insilmaril@0
   169
	// Fall back to global vym settings
insilmaril@0
   170
	bool ok;
insilmaril@0
   171
	return readEntry (key,def, &ok);
insilmaril@0
   172
}	
insilmaril@0
   173
insilmaril@0
   174
void Settings::setLocalEntry (const QString &fpath, const QString &key, const QString &value)
insilmaril@0
   175
{
insilmaril@0
   176
	QStringList::Iterator itp=pathlist.begin();
insilmaril@0
   177
	QStringList::Iterator itk=keylist.begin();
insilmaril@0
   178
	QStringList::Iterator itv=valuelist.begin();
insilmaril@0
   179
insilmaril@0
   180
	if (!fpath.isEmpty() && !key.isEmpty() && !value.isEmpty() )
insilmaril@0
   181
	{
insilmaril@0
   182
		// Search for existing entry first
insilmaril@0
   183
		while (itp !=pathlist.end() )
insilmaril@0
   184
		{
insilmaril@0
   185
			if (*itp == fpath && *itk == key)
insilmaril@0
   186
			{
insilmaril@0
   187
				*itv=value;
insilmaril@0
   188
				return;
insilmaril@0
   189
			}
insilmaril@0
   190
			itp++;
insilmaril@0
   191
			itk++;
insilmaril@0
   192
			itv++;
insilmaril@0
   193
		}
insilmaril@0
   194
		
insilmaril@0
   195
		// If no entry exists, append a new one
insilmaril@0
   196
		pathlist.append (fpath);
insilmaril@0
   197
		keylist.append (key);
insilmaril@0
   198
		valuelist.append (value);
insilmaril@0
   199
	}
insilmaril@0
   200
}
insilmaril@0
   201
insilmaril@0
   202
QString Settings::getXMLData (const QString &fpath)
insilmaril@0
   203
{
insilmaril@0
   204
	QString s;
insilmaril@0
   205
	QStringList::Iterator itp=pathlist.begin();
insilmaril@0
   206
	QStringList::Iterator itk=keylist.begin();
insilmaril@0
   207
	QStringList::Iterator itv=valuelist.begin();
insilmaril@0
   208
insilmaril@0
   209
	while (itp !=pathlist.end() )
insilmaril@0
   210
	{
insilmaril@0
   211
		if (*itp == fpath )
insilmaril@0
   212
			if (!(*itv).isEmpty())
insilmaril@0
   213
				s+=singleElement (
insilmaril@0
   214
					"setting",
insilmaril@0
   215
					attribut ("key",*itk) 
insilmaril@0
   216
					+attribut ("value",*itv)
insilmaril@0
   217
				)+"\n";
insilmaril@0
   218
		itp++;
insilmaril@0
   219
		itk++;
insilmaril@0
   220
		itv++;
insilmaril@0
   221
	}
insilmaril@0
   222
	return s;
insilmaril@0
   223
}
insilmaril@0
   224