Back to index

scribus-ng  1.3.4.dfsg+svn20071115
nfttemplate.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 "nfttemplate.h"
00011 #include <qfileinfo.h>
00012 
00013 nfttemplate::nfttemplate(QFile* tmplXmlFile, const QString &tmplCategory) 
00014 {
00015        tmplXml = tmplXmlFile;
00016        templateCategory = tmplCategory;
00017        isWritable = tmplXml->open(IO_WriteOnly | IO_ReadOnly);
00018        tmplXml->close();
00019        isDeleted = false;
00020 }
00021 
00022 void nfttemplate::remove()
00023 {
00024        if (tmplXml->exists())
00025        {
00026               QString newTmplXml = "";
00027               QString tmp;
00028               bool collect = false;
00029               tmplXml->open(IO_ReadOnly);
00030               QTextStream stream(tmplXml);
00031               QString line = stream.readLine();
00032               while (line != NULL)
00033               {
00034                      if ((line.find(enCategory) != -1) || collect)
00035                      {
00036                             collect = true;
00037                             line += "\n";
00038                             tmp += line;
00039                             if (line.find("name") != -1)
00040                             {
00041                                    if (line.find(name) == -1)
00042                                    {
00043                                           collect = false;
00044                                           newTmplXml += tmp;
00045                                           tmp = "";
00046                                    }
00047                             } 
00048                             else if (line.find("file") != -1)
00049                             {
00050                                    QString shortFile = file.right(file.length() - file.findRev("/") -1);
00051                                    if (line.find(shortFile) == -1)
00052                                    {
00053                                           collect = false;
00054                                           newTmplXml += tmp;
00055                                           tmp = "";
00056                                    }
00057                             } 
00058                             else if (line.find("</template>") != -1)
00059                             {
00060                                    collect = false;
00061                                    tmp = "";
00062                             }
00063                      }
00064                      else
00065                      {
00066                             line += "\n";
00067                             newTmplXml += line;
00068                      }
00069                      line = stream.readLine();
00070               }
00071               tmplXml->close();
00072               tmplXml->open(IO_WriteOnly);
00073               QTextStream instream(tmplXml);
00074               instream.setEncoding(QTextStream::UnicodeUTF8);
00075               instream << newTmplXml;
00076               tmplXml->close();
00077        }
00078 }
00079 
00080 bool nfttemplate::canWrite()
00081 {
00082        return isWritable;
00083 }
00084 
00085 bool nfttemplate::isValid()
00086 {
00087        bool ret = true;
00088        
00089        QFileInfo *fi = new QFileInfo(file);
00090        if (!fi->exists())
00091               ret = false;
00092        delete fi;
00093        
00094        fi = new QFileInfo(tnail);
00095        if (!fi->exists())
00096               ret = false;
00097        delete fi;
00098        
00099        fi = new QFileInfo(img);
00100        if (!fi->exists())
00101               ret = false;
00102        delete fi;
00103        
00104        return ret;   
00105 }
00106 
00107 nfttemplate::~nfttemplate() 
00108 {
00109        if (isDeleted)
00110               remove();
00111 }