1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8  *   Riku Leino, tsoots@gmail.com                                          *
9  ***************************************************************************/
10 
11 #include "nftsettings.h"
12 #include "prefsmanager.h"
13 #include "scpaths.h"
14 
nftsettings(const QString & guilang)15 nftsettings::nftsettings(const QString& guilang)
16 {
17 	lang = guilang;
18 	read();
19 }
20 
read()21 void nftsettings::read()
22 {
23 	nftrcreader reader(&templates, QDir::toNativeSeparators(ScPaths::applicationDataDir()));
24 
25 	addTemplates(reader, ScPaths::instance().templateDir());
26 	addTemplates(reader, ScPaths::instance().userTemplateDir(true));
27 }
28 
addTemplates(nftrcreader & reader,const QString & dir)29 void nftsettings::addTemplates(nftrcreader& reader, const QString& dir) // dir will be searched for a sub folder called templates
30 {
31 	if (dir.isEmpty())
32 		return;
33 	// Add templates from the dir itself
34 	QString tmplFile = findTemplateXml(dir);
35 	QString tmplFilePath = QDir::toNativeSeparators(tmplFile);
36 	reader.setSourceDir(dir);
37 	reader.setSourceFile(tmplFile);
38 	if (QFile::exists(tmplFilePath))
39 		reader.parse(tmplFilePath);
40 
41 	// And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
42 	QDir tmpldir(dir);
43 	if (tmpldir.exists())
44 	{
45 		tmpldir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
46 		QStringList dirs = tmpldir.entryList();
47 		for (int i = 0; i < dirs.size(); ++i)
48 		{
49 			tmplFile = findTemplateXml(dir + "/" + dirs[i]);
50 			tmplFilePath = QDir::toNativeSeparators(tmplFile);
51 			reader.setSourceDir(dir+"/"+dirs[i]);
52 			reader.setSourceFile(tmplFile);
53 			if (QFile::exists(tmplFilePath))
54 				reader.parse(tmplFilePath);
55 		}
56 	}
57 }
58 
findTemplateXml(const QString & dir)59 QString nftsettings::findTemplateXml(const QString& dir)
60 {
61 	QString tmp = dir + "/template." + lang + ".xml";
62 	if (QFile(tmp).exists())
63 		return tmp;
64 
65 	if (lang.length() > 2)
66 	{
67 		tmp = dir + "/template." + lang.left(2) + ".xml";
68 		if (QFile(tmp).exists())
69 			return tmp;
70 	}
71 	return dir + "/template.xml";
72 }
73 
~nftsettings()74 nftsettings::~ nftsettings()
75 {
76 	for (uint i = 0; i < templates.size(); ++i)
77 	{
78 		if (templates[i] != nullptr)
79 			delete templates[i];
80 	}
81 }
82