summaryrefslogtreecommitdiff
path: root/OptionEngine.cpp
blob: 807e027ae9bfa8b8670e48f1e22911ab9244d1df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/** \file OptionEngine.cpp
\brief Define the class of the event dispatcher
\author alpha_one_x86
\version 0.3
\date 2010
\licence GPL3, see the file COPYING */

#include <QFileInfo>
#include <QDir>
#include <QPluginLoader>
#include <QLabel>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QMessageBox>

#include "OptionEngine.h"

/// \todo async the options write

/// \brief Initiate the option, load from backend
OptionEngine::OptionEngine()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	//locate the settings
	#ifdef ULTRACOPIER_VERSION_PORTABLE
		resources=ResourcesManager::getInstance();
		QString settingsFilePath=resources->getWritablePath();
		if(settingsFilePath!="")
			settings = new QSettings(settingsFilePath+"Ultracopier.conf",QSettings::IniFormat);
		else
			settings = NULL;
	#else // ULTRACOPIER_VERSION_PORTABLE
		settings = new QSettings("Ultracopier","Ultracopier");
	#endif // ULTRACOPIER_VERSION_PORTABLE
	if(settings!=NULL)
	{
		//do some write test
		if(settings->status()!=QSettings::NoError)
		{
			delete settings;
			settings=NULL;
		}
		else if(!settings->isWritable())
		{
			delete settings;
			settings=NULL;
		}
		else
		{
			settings->setValue("test","test");
			if(settings->status()!=QSettings::NoError)
			{
				delete settings;
				settings=NULL;
			}
			else
			{
				settings->remove("test");
				if(settings->status()!=QSettings::NoError)
				{
					delete settings;
					settings=NULL;
				}
			}
		}
	}
	//set the backend
	if(settings==NULL)
	{
		#ifdef ULTRACOPIER_VERSION_PORTABLE
		resources->disableWritablePath();
		#endif // ULTRACOPIER_VERSION_PORTABLE
		currentBackend=Memory;
	}
	else
		currentBackend=File;
}

/// \brief Destroy the option
OptionEngine::~OptionEngine()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	ResourcesManager::destroyInstanceAtTheLastCall();
}

/// \brief To add option group to options
bool OptionEngine::addOptionGroup(const QString &groupName,const QList<QPair<QString, QVariant> > &KeysList)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start(\""+groupName+"\",[...])");
	//search if previous with the same name exists
	indexGroup=0;
	loop_size=GroupKeysList.size();
	while(indexGroup<loop_size)
	{
		if(GroupKeysList.at(indexGroup).groupName==groupName)
		{
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"group already used previously");
			break;
		}
		indexGroup++;
	}
	//else create the entry
	if(indexGroup==loop_size)
	{
		OptionEngineGroup tempEntry;
		tempEntry.groupName=groupName;
		GroupKeysList << tempEntry;
	}
	//here GroupKeysList.at(indexGroup) can be used
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"variable used: GroupKeysList.at("+QString::number(indexGroup)+")");
	//if the backend is file, enter into the group
	if(currentBackend==File)
		settings->beginGroup(groupName);
	//browse all key, and append it to the key
	index=0;
	QList<OptionEngineGroupKey> KeyListTemp;
	loop_size=KeysList.size();
	while(index<loop_size)
	{
		OptionEngineGroupKey theCurrentKey;
		theCurrentKey.variableName=KeysList.at(index).first;
		theCurrentKey.defaultValue=KeysList.at(index).second;
		theCurrentKey.emptyList=false;
		//if memory backend, load the default value into the current value
		if(currentBackend==Memory)
			theCurrentKey.currentValue=theCurrentKey.defaultValue;
		else if(settings->contains(theCurrentKey.variableName))//if file backend, load the default value from the file
			theCurrentKey.currentValue=settings->value(theCurrentKey.variableName);
		else //or if not found load the default value and set into the file
		{
			theCurrentKey.currentValue=theCurrentKey.defaultValue;
			//to switch default value if is unchanged
			//settings->setValue(theCurrentKey.variableName,theCurrentKey.defaultValue);
		}
		if(settings->status()!=QSettings::NoError)
		{
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Have writing error, switch to memory only options");
			#ifdef ULTRACOPIER_VERSION_PORTABLE
			resources->disableWritablePath();
			#endif // ULTRACOPIER_VERSION_PORTABLE
			currentBackend=Memory;
		}
		GroupKeysList[indexGroup].KeysList << theCurrentKey;
		index++;
	}
	//if the backend is file, leave into the group
	if(currentBackend==File)
		settings->endGroup();
	return true;
}

/// \brief To remove option group to options, remove the widget need be do into the calling object
bool OptionEngine::removeOptionGroup(const QString &groupName)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start, groupName: "+groupName);
	indexGroup=0;
	loop_size=GroupKeysList.size();
	while(indexGroup<loop_size)
	{
		if(GroupKeysList.at(indexGroup).groupName==groupName)
		{
			GroupKeysList.removeAt(indexGroup);
			return true;
		}
		indexGroup++;
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,"value not found, internal bug, groupName: "+groupName);
	return false;
}

/// \brief To get option value
QVariant OptionEngine::getOptionValue(const QString &groupName,const QString &variableName)
{
	indexGroup=0;
	loop_size=GroupKeysList.size();
	while(indexGroup<loop_size)
	{
		if(GroupKeysList.at(indexGroup).groupName==groupName)
		{
			//search if previous with the same name exists
			indexGroupKey=0;
			loop_sub_size=GroupKeysList.at(indexGroup).KeysList.size();
			while(indexGroupKey<loop_sub_size)
			{
				if(GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).variableName==variableName)
					return GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).currentValue;
				indexGroupKey++;
			}
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,"value not found, internal bug, groupName: "+groupName+", variableName: "+variableName);
			return QVariant();
		}
		indexGroup++;
	}
	QMessageBox::critical(NULL,"Internal error","Get the option value but not found");
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,"group \""+groupName+"\" not found, internal bug!");
	//return default value
	return QVariant();
}

/// \brief To set option value
void OptionEngine::setOptionValue(const QString &groupName,const QString &variableName,const QVariant &value)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"groupName: \""+groupName+"\", variableName: \""+variableName+"\", value: \""+value.toString()+"\"");
	indexGroup=0;
	loop_size=GroupKeysList.size();
	while(indexGroup<loop_size)
	{
		if(GroupKeysList.at(indexGroup).groupName==groupName)
		{
			//search if previous with the same name exists
			indexGroupKey=0;
			loop_sub_size=GroupKeysList.at(indexGroup).KeysList.size();
			while(indexGroupKey<loop_sub_size)
			{
				if(GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).variableName==variableName)
				{
					if(GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).currentValue!=value) //protection to prevent same value writing
					{
						GroupKeysList[indexGroup].KeysList[indexGroupKey].currentValue=value;
						if(currentBackend==File)
						{
							settings->beginGroup(groupName);
							settings->setValue(variableName,value);
							settings->endGroup();
							if(settings->status()!=QSettings::NoError)
							{
								ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"Have writing error, switch to memory only options");
								#ifdef ULTRACOPIER_VERSION_PORTABLE
								resources->disableWritablePath();
								#endif // ULTRACOPIER_VERSION_PORTABLE
								currentBackend=Memory;
							}
						}
						emit newOptionValue(groupName,variableName,value);
					}
					return;
				}
				indexGroupKey++;
			}
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"value not found, internal bug, groupName: \""+groupName+"\", variableName: \""+variableName+"\", value: \""+value.toString()+"\"");
			return;
		}
		indexGroup++;
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"group \""+groupName+"\" not found, internal bug, groupName: \""+groupName+"\", variableName: \""+variableName+"\", value: \""+value.toString()+"\"");
}

//the reset of right value of widget need be do into the calling object
void OptionEngine::internal_resetToDefaultValue()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	int indexGroup=0;
	while(indexGroup<GroupKeysList.size())
	{
		//search if previous with the same name exists
		int indexGroupKey=0;
		while(indexGroupKey<GroupKeysList.at(indexGroup).KeysList.size())
		{
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"option check: "+GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).variableName);
			if(GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).currentValue!=GroupKeysList.at(indexGroup).KeysList.at(indexGroupKey).defaultValue)
				GroupKeysList[indexGroup].KeysList[indexGroupKey].currentValue=GroupKeysList[indexGroup].KeysList[indexGroupKey].defaultValue;
			indexGroupKey++;
		}
		indexGroup++;
	}
}

void OptionEngine::queryResetOptions()
{
	emit resetOptions();
}