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