Back to index

scribus-ng  1.3.4.dfsg+svn20071115
nftsettings.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Riku Leino, tsoots@gmail.com                                          *
00009  ***************************************************************************/
00010 #include "nftsettings.h"
00011 #include "scpaths.h"
00012 
00013 nftsettings::nftsettings(QString guilang, QString templateDir)
00014 {
00015        lang = guilang;
00016        scribusShare = ScPaths::instance().templateDir();
00017        scribusUserHome = QDir::convertSeparators(ScPaths::getApplicationDataDir());
00018        userTemplateDir = templateDir;
00019        if (userTemplateDir.right(1) == "/")
00020               userTemplateDir = userTemplateDir.left(userTemplateDir.length() - 1);
00021        read();
00022 }
00023 
00024 void nftsettings::read()
00025 {
00026        handler = new nftrcreader(&templates,scribusUserHome);
00027        reader = new QXmlSimpleReader();
00028        reader->setContentHandler(handler);
00029 
00030        addTemplates(scribusShare);
00031        addTemplates(scribusUserHome+"/templates");
00032        if ((!userTemplateDir.isNull()) && (!userTemplateDir.isEmpty()))
00033               addTemplates(userTemplateDir);
00034 }
00035 
00036 void nftsettings::addTemplates(QString dir) // dir will be searched for a sub folder called templates
00037 {
00038        // Add templates from the dir itself
00039        QString tmplFile = findTemplateXml(dir);
00040        QFile* tmplxml = new QFile(QDir::convertSeparators(tmplFile));
00041        handler->setSourceDir(dir);
00042        handler->setSourceFile(tmplFile);
00043        if (tmplxml->exists())
00044        {
00045               QXmlInputSource* source = new QXmlInputSource(tmplxml);
00046               reader->parse(source);
00047               delete source;
00048        }
00049        delete tmplxml;
00050        
00051        
00052        // And from all the subdirectories. template.xml file is only searched one dir level deeper than the dir
00053        QDir tmpldir(dir);
00054        if (tmpldir.exists())
00055        {
00056               tmpldir.setFilter(QDir::Dirs);
00057               QStringList dirs = tmpldir.entryList();
00058               for (uint i = 0; i < dirs.size(); ++i)
00059               {
00060                      if ((dirs[i] != ".") && (dirs[i] != "..")) {
00061                             tmplFile = findTemplateXml(dir + "/" + dirs[i]);
00062                             QFile* tmplxml = new QFile(QDir::convertSeparators(tmplFile));
00063                             handler->setSourceDir(dir+"/"+dirs[i]);
00064                             handler->setSourceFile(tmplFile);
00065                             if (tmplxml->exists())
00066                             {
00067                                    QXmlInputSource* source = new QXmlInputSource(tmplxml);
00068                                    reader->parse(source);
00069                                    delete source;
00070                             }
00071                             delete tmplxml;
00072                      }
00073               }
00074        }
00075 }
00076 
00077 QString nftsettings::findTemplateXml(QString dir)
00078 {
00079        QString tmp = dir + "/template." + lang + ".xml";
00080        if (QFile(tmp).exists())
00081               return tmp;
00082 
00083        if (lang.length() > 2)
00084        {
00085               tmp = dir + "/template." + lang.left(2) + ".xml";
00086               if (QFile(tmp).exists())
00087                      return tmp;
00088        }
00089        return dir + "/template.xml";      
00090 }
00091 
00092 nftsettings::~ nftsettings()
00093 {
00094        delete reader;
00095        delete handler;
00096        for (uint i = 0; i < templates.size(); ++i)
00097        {
00098               if (templates[i] != NULL)
00099                      delete templates[i];
00100        }
00101 }