Subversion Repositories Scribus

Rev

Rev 4430 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 cbradney 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
*/
304 Franz 7
/***************************************************************************
1184 tsoots 8
 *   Riku Leino, tsoots@gmail.com                                          *
304 Franz 9
 ***************************************************************************/
10
#include "nftsettings.h"
1693 craig 11
#include "scpaths.h"
304 Franz 12
 
409 Franz 13
nftsettings::nftsettings(QString guilang, QString templateDir)
304 Franz 14
{
320 Franz 15
        lang = guilang;
1693 craig 16
        scribusShare = ScPaths::instance().templateDir();
5917 jghali 17
        scribusUserHome = QDir::convertSeparators(ScPaths::getApplicationDataDir());
409 Franz 18
        userTemplateDir = templateDir;
19
        if (userTemplateDir.right(1) == "/")
20
                userTemplateDir = userTemplateDir.left(userTemplateDir.length() - 1);
304 Franz 21
        read();
22
}
23
 
24
void nftsettings::read()
25
{
26
        handler = new nftrcreader(&templates,scribusUserHome);
27
        reader = new QXmlSimpleReader();
28
        reader->setContentHandler(handler);
409 Franz 29
 
872 cbradney 30
        addTemplates(scribusShare);
409 Franz 31
        addTemplates(scribusUserHome+"/templates");
2877 cbradney 32
        if ((!userTemplateDir.isNull()) && (!userTemplateDir.isEmpty()))
409 Franz 33
                addTemplates(userTemplateDir);
304 Franz 34
}
35
 
36
void nftsettings::addTemplates(QString dir) // dir will be searched for a sub folder called templates
37
{
308 Franz 38
        // Add templates from the dir itself
409 Franz 39
        QString tmplFile = findTemplateXml(dir);
320 Franz 40
        QFile* tmplxml = new QFile(QDir::convertSeparators(tmplFile));
409 Franz 41
        handler->setSourceDir(dir);
320 Franz 42
        handler->setSourceFile(tmplFile);
309 Franz 43
        if (tmplxml->exists())
44
        {
308 Franz 45
                QXmlInputSource* source = new QXmlInputSource(tmplxml);
46
                reader->parse(source);
47
                delete source;
48
        }
49
        delete tmplxml;
50
 
51
 
309 Franz 52
        // And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
409 Franz 53
        QDir tmpldir(dir);
309 Franz 54
        if (tmpldir.exists())
55
        {
304 Franz 56
                tmpldir.setFilter(QDir::Dirs);
57
                QStringList dirs = tmpldir.entryList();
309 Franz 58
                for (uint i = 0; i < dirs.size(); ++i)
59
                {
308 Franz 60
                        if ((dirs[i] != ".") && (dirs[i] != "..")) {
409 Franz 61
                                tmplFile = findTemplateXml(dir + "/" + dirs[i]);
320 Franz 62
                                QFile* tmplxml = new QFile(QDir::convertSeparators(tmplFile));
409 Franz 63
                                handler->setSourceDir(dir+"/"+dirs[i]);
320 Franz 64
                                handler->setSourceFile(tmplFile);
309 Franz 65
                                if (tmplxml->exists())
66
                                {
308 Franz 67
                                        QXmlInputSource* source = new QXmlInputSource(tmplxml);
68
                                        reader->parse(source);
69
                                        delete source;
70
                                }
71
                                delete tmplxml;
304 Franz 72
                        }
73
                }
74
        }
75
}
76
 
320 Franz 77
QString nftsettings::findTemplateXml(QString dir)
78
{
79
        QString tmp = dir + "/template." + lang + ".xml";
80
        if (QFile(tmp).exists())
81
                return tmp;
82
 
83
        if (lang.length() > 2)
84
        {
85
                tmp = dir + "/template." + lang.left(2) + ".xml";
86
                if (QFile(tmp).exists())
87
                        return tmp;
88
        }
89
        return dir + "/template.xml";  
90
}
91
 
304 Franz 92
nftsettings::~ nftsettings()
93
{
94
        delete reader;
95
        delete handler;
309 Franz 96
        for (uint i = 0; i < templates.size(); ++i)
320 Franz 97
        {
98
                if (templates[i] != NULL)
99
                        delete templates[i];
100
        }
304 Franz 101
}