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