summaryrefslogtreecommitdiffstats
path: root/scribus/nftsettings.cpp
blob: 8d88959de6ba31bce957ff92779c6e36bfd69453 (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
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
 *   Riku Leino, tsoots@gmail.com                                          *
 ***************************************************************************/
#include "nftsettings.h"
#include "prefsmanager.h"
#include "scpaths.h"

nftsettings::nftsettings(QString guilang)
{
	lang = guilang;
	scribusShare = ScPaths::instance().templateDir();
	scribusUserHome = QDir::toNativeSeparators(ScPaths::getApplicationDataDir());
	read();
}

void nftsettings::read()
{
	handler = new nftrcreader(&templates,scribusUserHome);
	reader = new QXmlSimpleReader();
	reader->setContentHandler(handler);

	addTemplates(scribusShare);
	addTemplates(scribusUserHome+"/templates");
	QString userTemplateDir(PrefsManager::instance()->appPrefs.documentTemplatesDir);
	if (userTemplateDir.right(1) == "/")
		userTemplateDir.chop(1);
	if ((!userTemplateDir.isNull()) && (!userTemplateDir.isEmpty()))
		addTemplates(userTemplateDir);}

void nftsettings::addTemplates(QString dir) // dir will be searched for a sub folder called templates
{
	// Add templates from the dir itself
	QString tmplFile = findTemplateXml(dir);
	QFile* tmplxml = new QFile(QDir::toNativeSeparators(tmplFile));
	handler->setSourceDir(dir);
	handler->setSourceFile(tmplFile);
	if (tmplxml->exists())
	{
		QXmlInputSource* source = new QXmlInputSource(tmplxml);
		reader->parse(source);
		delete source;
	}
	delete tmplxml;
	
	
	// And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
	QDir tmpldir(dir);
	if (tmpldir.exists())
	{
		tmpldir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
		QStringList dirs = tmpldir.entryList();
		for (int i = 0; i < dirs.size(); ++i)
		{
			tmplFile = findTemplateXml(dir + "/" + dirs[i]);
			QFile* tmplxml = new QFile(QDir::toNativeSeparators(tmplFile));
			handler->setSourceDir(dir+"/"+dirs[i]);
			handler->setSourceFile(tmplFile);
			if (tmplxml->exists())
			{
				QXmlInputSource* source = new QXmlInputSource(tmplxml);
				reader->parse(source);
				delete source;
			}
			delete tmplxml;
		}
	}
}

QString nftsettings::findTemplateXml(QString dir)
{
	QString tmp = dir + "/template." + lang + ".xml";
	if (QFile(tmp).exists())
		return tmp;

	if (lang.length() > 2)
	{
		tmp = dir + "/template." + lang.left(2) + ".xml";
		if (QFile(tmp).exists())
			return tmp;
	}
	return dir + "/template.xml";	
}

nftsettings::~ nftsettings()
{
	delete reader;
	delete handler;
	for (uint i = 0; i < templates.size(); ++i)
	{
		if (templates[i] != NULL)
			delete templates[i];
	}
}