summaryrefslogtreecommitdiff
path: root/src/frontend/profile/cprofile.cpp
blob: f6c8be163a01e34d69bd4c65faa888c9b24de1c6 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "cprofile.h"
#include "util/directoryutil.h"

//Qt includes
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QDomDocument>

#define CURRENT_SYNTAX_VERSION 3

namespace Profile {

CProfile::CProfile( const QString& file, const QString& name )
	: m_name(name.isEmpty() ? QObject::tr("unknown") : name),
	m_filename(file),
	m_fullscreen(false),
	m_geometry(10,20,640,480),
	m_mdiArrangementMode((CMDIArea::MDIArrangementMode)0) //0 is not a valid enum entry, means "unknown"
{
	if (!m_filename.isEmpty() && name.isEmpty()) {
		loadBasics();
	}
	else if (m_filename.isEmpty() && !name.isEmpty()) {
		m_filename = name;
		m_filename.replace(QRegExp("\\s=#."),"_");
		m_filename = util::filesystem::DirectoryUtil::getUserSessionsDir().absolutePath() + "/"  + m_filename + ".xml";
		init(m_filename);
	}
	else {
		qWarning("CProfile: empty file name!");
	}
}

CProfile::~CProfile() {
	qDeleteAll(m_profileWindows); //there's no autodelete feature in qt4
	m_profileWindows.clear();  //delete all CProfileWindows objects
}

/** Loads the profile from the file given in the constructor. */
QList<CProfileWindow*> CProfile::load() {
	QFile file(m_filename);
	if (!file.exists())
	{
		//qWarning() << "Standard profile not found at filename " << m_filename;
		return QList<CProfileWindow*>();
	}

	QDomDocument doc;
	if (file.open(QIODevice::ReadOnly)) {
		QTextStream t( &file );
		t.setCodec("UTF-8");
		doc.setContent(t.readAll());
		file.close();
	}

	QDomElement document = doc.documentElement();
	if( document.tagName() != "BibleTimeProfile" && document.tagName() != "BibleTime" ) { //BibleTime was used in syntax version 1.0
		qWarning("CProfile::load: Missing BibleTime doc");
		return m_profileWindows;
	}
	if (document.hasAttribute("name")) {
		m_name = document.attribute("name");
	}

	//load settings of the main window
	{
		// see if there's a section with the name MAINWINDOW
		QDomElement elem = document.firstChild().toElement();
		QDomElement mainWindow;
		while (!elem.isNull()) {
			if (elem.tagName() == "MAINWINDOW") {
				mainWindow = elem;
				break; //found the element
			}
			elem = elem.nextSibling().toElement();
		}
		if (!mainWindow.isNull()) { //was found
			setFullscreen( (bool)mainWindow.attribute("fullscreen").toInt());
			
			QByteArray bstate;
			bstate += mainWindow.attribute("state");
			setMainwindowState(QByteArray::fromHex(bstate));
			
			QDomElement geometry_element = mainWindow.namedItem("GEOMETRY").toElement();
			QRect rect;
			if(!geometry_element.isNull()) {
				if (geometry_element.hasAttribute("x")) {
					rect.setX(geometry_element.attribute("x").toInt());
				}
				if (geometry_element.hasAttribute("y")) {
					rect.setY(geometry_element.attribute("y").toInt());
				}
				if (geometry_element.hasAttribute("width")) {
					rect.setWidth(geometry_element.attribute("width").toInt());
				}
				if (geometry_element.hasAttribute("height")) {
					rect.setHeight(geometry_element.attribute("height").toInt());
				}
				if (geometry_element.hasAttribute("isMaximized")) {
					this->setMaximized( static_cast<bool>(geometry_element.attribute("isMaximized").toInt()) );
				}
				setGeometry(rect);
			}

			QDomElement mdi_element = mainWindow.namedItem("MDI").toElement();
			if(!mdi_element.isNull()) {
				if (mdi_element.hasAttribute("ArrangementMode")) {
					this->setMDIArrangementMode((CMDIArea::MDIArrangementMode)mdi_element.attribute("ArrangementMode").toInt());
				}
			}
		}
	}

	m_profileWindows.clear();
	QDomElement elem = document.firstChild().toElement();
	while (!elem.isNull()) {
		CProfileWindow* p = 0;
		if (elem.tagName() == "BIBLE") {
			p = new CProfileWindow(CSwordModuleInfo::Bible);
		}
		else if (elem.tagName() == "COMMENTARY") {
			p = new CProfileWindow(CSwordModuleInfo::Commentary);
		}
		else if (elem.tagName() == "LEXICON") {
			p = new CProfileWindow(CSwordModuleInfo::Lexicon);
		}
		else if (elem.tagName() == "BOOK") {
			p = new CProfileWindow(CSwordModuleInfo::GenericBook);
		}

		if (p) {
			m_profileWindows.append(p);

			if (elem.hasAttribute("windowSettings")) {
				p->setWindowSettings( elem.attribute("windowSettings").toInt() );
			}
			if (elem.hasAttribute("writeWindowType")) {
				p->setWriteWindowType( elem.attribute("writeWindowType").toInt() );
			}
			if (elem.hasAttribute("hasFocus")) {
				p->setFocus( static_cast<bool>(elem.attribute("hasFocus").toInt()) );
			}

			QRect rect;

			QDomElement object = elem.namedItem("GEOMETRY").toElement();
			if(!object.isNull()) {
				if (object.hasAttribute("x")) {
					rect.setX(object.attribute("x").toInt());
				}
				if (object.hasAttribute("y")) {
					rect.setY(object.attribute("y").toInt());
				}
				if (object.hasAttribute("width")) {
					rect.setWidth(object.attribute("width").toInt());
				}
				if (object.hasAttribute("height")) {
					rect.setHeight(object.attribute("height").toInt());
				}
				if (object.hasAttribute("isMaximized")) {
					p->setMaximized( static_cast<bool>(object.attribute("isMaximized").toInt()) );
				}
			}
			p->setGeometry(rect);

			object = elem.namedItem("MODULES").toElement();
			if(!object.isNull()) {
				if (object.hasAttribute("list")) {
					const QString sep = object.hasAttribute("separator") ? object.attribute("separator") : "|";
					QStringList modules = object.attribute("list").split(sep);
					p->setModules(modules);
				}
			}

			object = elem.namedItem("KEY").toElement();
			if(!object.isNull()) {
				if (object.hasAttribute("name"))
					p->setKey(object.attribute("name"));
			}

			object = elem.namedItem("SCROLLBARS").toElement();
			if(!object.isNull()) {
				int horizontal = 0, vertical = 0;
				if (object.hasAttribute("horizontal"))
					horizontal = object.attribute("horizontal").toInt();
				if (object.hasAttribute("vertical"))
					vertical = object.attribute("vertical").toInt();

				p->setScrollbarPositions(horizontal, vertical);
			}
		}
		elem = elem.nextSibling().toElement();
	}
	return m_profileWindows;
}

/** Saves the profile to the file given in the constructor. */
bool CProfile::save(QList<CProfileWindow*> windows) {
	/** Save the settings using a XML file
	* Save the CProfileWindow objects using a XML file which name is in m_filename
	*/
	bool ret = false;
	QDomDocument doc("DOC");
	doc.appendChild( doc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );

	QDomElement content = doc.createElement("BibleTimeProfile");
	content.setAttribute("syntaxVersion", CURRENT_SYNTAX_VERSION);
	content.setAttribute("name", m_name);
	doc.appendChild(content);

	//save mainwindow settings
	{
		QDomElement mainWindow = doc.createElement("MAINWINDOW");
		mainWindow.setAttribute("fullscreen", fullscreen());
		
		QString sstate = QString(getMainwindowState().toHex());
		mainWindow.setAttribute("state", sstate);

		QDomElement geometry = doc.createElement("GEOMETRY");
		mainWindow.appendChild(geometry);
		const QRect r = this->geometry();
		geometry.setAttribute("x",r.x());
		geometry.setAttribute("y",r.y());
		geometry.setAttribute("width",r.width());
		geometry.setAttribute("height",r.height());
		geometry.setAttribute("isMaximized",static_cast<int>(this->maximized()));

		QDomElement mdi = doc.createElement("MDI");
		mainWindow.appendChild(mdi);
		mdi.setAttribute("ArrangementMode",static_cast<int>(this->getMDIArrangementMode()));

		content.appendChild(mainWindow);
	}

	//for (CProfileWindow* p = windows.first(); p; p = windows.next()) {
	foreach(CProfileWindow* p, windows) {
		QDomElement window;
		switch (p->type()) {
			case CSwordModuleInfo::Bible:
			window = doc.createElement("BIBLE");
			break;
			case CSwordModuleInfo::Commentary:
			window = doc.createElement("COMMENTARY");
			break;
			case CSwordModuleInfo::Lexicon:
			window = doc.createElement("LEXICON");
			break;
			case CSwordModuleInfo::GenericBook:
			window = doc.createElement("BOOK");
			break;
			default:
			break;
		}
		if (window.isNull())
			break;
		window.setAttribute("windowSettings", p->windowSettings());
		window.setAttribute("writeWindowType", p->writeWindowType());
		window.setAttribute("hasFocus", p->hasFocus());

		//save geomtery
		const QRect r = p->geometry();
		QDomElement geometry = doc.createElement("GEOMETRY");
		geometry.setAttribute("x",r.x());
		geometry.setAttribute("y",r.y());
		geometry.setAttribute("width",r.width());
		geometry.setAttribute("height",r.height());
		geometry.setAttribute("isMaximized",static_cast<int>(p->maximized()));
		window.appendChild( geometry );

		QDomElement modules = doc.createElement("MODULES");
		modules.setAttribute("separator", "|");
		modules.setAttribute("list", p->modules().join("|"));
		window.appendChild( modules );

		QDomElement key = doc.createElement("KEY");
		key.setAttribute("name", p->key());
		window.appendChild( key );

		QDomElement scrollbars = doc.createElement("SCROLLBARS");
		scrollbars.setAttribute("horizontal", p->scrollbarPositions().horizontal);
		scrollbars.setAttribute("vertical", p->scrollbarPositions().vertical);
		window.appendChild( scrollbars );

		content.appendChild( window );
	}

	QFile file(m_filename);
	if ( file.open(QIODevice::WriteOnly) ) {
		ret = true;
		QTextStream t( &file );
		t.setCodec("UTF-8");
		t << doc.toString();
		file.close();
	}
	else
		ret = false;

	return ret;
}

/** Saves the profile to the file given in the constructor. */
bool CProfile::save() {
	return save(m_profileWindows);
}

/** Returns the filename used for this profile. */
const QString& CProfile::filename() {
	return m_filename;
}

/** Returns the name of this profile. */
const QString& CProfile::name() {
	return m_name;
}

/** Initializes the XML for the first time (use to create a new profile) */
void CProfile::init(const QString file) {
	const QString oldFile = m_filename;
	m_filename = file;
	save(QList<CProfileWindow*>());
	m_filename = oldFile;
}

/** Changes the name of this profile. */
void CProfile::setName( const QString& newName ) {
	m_name = newName;
	saveBasics(); //save chanegd name
}

/** Loads the basic settings requires for proper operation. */
void CProfile::loadBasics() {
	QFile file(m_filename);
	if (!file.exists())
		return;

	QDomDocument doc;
	if (file.open(QIODevice::ReadOnly)) {
		QTextStream t( &file );
		t.setCodec("UTF-8");
		doc.setContent(t.readAll());
		file.close();
	}
	QDomElement document = doc.documentElement();
	if (document.hasAttribute("name"))
		m_name = document.attribute("name");
}

void CProfile::saveBasics() {
	QFile file(m_filename);
	if (!file.exists())
		return;

	QDomDocument doc;
	if (file.open(QIODevice::ReadOnly)) {
		QTextStream t(&file);
		t.setCodec("UTF-8");
		doc.setContent(t.readAll());
		file.close();
	}

	QDomElement document = doc.documentElement();
	document.setAttribute("name", m_name);

	if (file.open(QIODevice::WriteOnly)) {
		QTextStream t( &file );
		t.setCodec("UTF-8");
		t << doc.toString();
		file.close();
	}
}

/** Returns true if the main window was in fullscreen mode as the profile was saved. */
bool CProfile::fullscreen() const {
	return m_fullscreen;
}

/** Set the parameter to true if the main window coveres the full screen size. */
void CProfile::setFullscreen( const bool fullscreen ) {
	m_fullscreen = fullscreen;
}

/** Returns true if the main window was maximized as the profile was saved. */
bool CProfile::maximized() const {
	return m_maximized;
}

/** Set the parameter to true if the main window is maximized. */
void CProfile::setMaximized( const bool maximized ) {
	m_maximized = maximized;
}

/** Returns the geometry of the main window */
const QRect CProfile::geometry() {
	return m_geometry;
}

/** Stes the geoemtry of the main window */
void CProfile::setGeometry( const QRect rect ) {
	m_geometry = rect;
}

void CProfile::setMDIArrangementMode(const CMDIArea::MDIArrangementMode newArrangementMode)
{
	m_mdiArrangementMode = newArrangementMode;
}

CMDIArea::MDIArrangementMode CProfile::getMDIArrangementMode(void)
{
	return m_mdiArrangementMode;
}

void CProfile::setMainwindowState(const QByteArray& state)
{
	m_mainwindowState = state;
}

QByteArray CProfile::getMainwindowState()
{
	return m_mainwindowState;
}


} //end of namespace Profile