Back to index

scribus-ng  1.3.4.dfsg+svn20071115
satemplate.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 "satemplate.h"
00011 #include "satemplate.moc"
00012 #include "satdialog.h"
00013 
00014 #include "scpaths.h"
00015 #include "scribuscore.h"
00016 #include "scribusdoc.h"
00017 #include "prefsfile.h"
00018 #include "prefsmanager.h"
00019 
00020 int saveastemplateplugin_getPluginAPIVersion()
00021 {
00022        return PLUGIN_API_VERSION;
00023 }
00024 
00025 ScPlugin* saveastemplateplugin_getPlugin()
00026 {
00027        SaveAsTemplatePlugin* plug = new SaveAsTemplatePlugin();
00028        Q_CHECK_PTR(plug);
00029        return plug;
00030 }
00031 
00032 void saveastemplateplugin_freePlugin(ScPlugin* plugin)
00033 {
00034        SaveAsTemplatePlugin* plug = dynamic_cast<SaveAsTemplatePlugin*>(plugin);
00035        Q_ASSERT(plug);
00036        delete plug;
00037 }
00038 
00039 SaveAsTemplatePlugin::SaveAsTemplatePlugin() : ScActionPlugin()
00040 {
00041        // Set action info in languageChange, so we only have to do
00042        // it in one place.
00043        languageChange();
00044 }
00045 
00046 SaveAsTemplatePlugin::~SaveAsTemplatePlugin() {};
00047 
00048 void SaveAsTemplatePlugin::languageChange()
00049 {
00050        // Note that we leave the unused members unset. They'll be initialised
00051        // with their default ctors during construction.
00052        // Action name
00053        m_actionInfo.name = "SaveAsDocumentTemplate";
00054        // Action text for menu, including accel
00055        m_actionInfo.text = tr("Save as &Template...");
00056        // Shortcut
00057        m_actionInfo.keySequence = "Ctrl+Alt+S";
00058        // Menu
00059        m_actionInfo.menu = "File";
00060        m_actionInfo.menuAfterName = "SaveAs";
00061        m_actionInfo.enabledOnStartup = true;
00062 }
00063 
00064 const QString SaveAsTemplatePlugin::fullTrName() const
00065 {
00066        return QObject::tr("Save As Template");
00067 }
00068 
00069 const ScActionPlugin::AboutData* SaveAsTemplatePlugin::getAboutData() const
00070 {
00071        AboutData* about = new AboutData;
00072        Q_CHECK_PTR(about);
00073        about->authors = QString::fromUtf8("Riku Leino <riku@scribus.info>");
00074        about->shortDescription = tr("Save a document as a template");
00075        about->description = tr("Save a document as a template. Good way to ease the "
00076                                "initial work for documents with a constant look");
00077     // about->version
00078     // about->releaseDate
00079     // about->copyright
00080        about->license = "GPL";
00081        return about;
00082 }
00083 
00084 void SaveAsTemplatePlugin::deleteAboutData(const AboutData* about) const
00085 {
00086        Q_ASSERT(about);
00087        delete about;
00088 }
00089 
00090 bool SaveAsTemplatePlugin::run(ScribusDoc* doc, QString target)
00091 /*{
00092        Q_ASSERT(target.isEmpty());
00093        Sat = new MenuSAT();
00094        Sat->RunSATPlug();
00095        delete Sat;
00096        Sat = 0;
00097        return true;
00098 }
00099 */
00100 /* jghali's fix when the new file dialog is cancelled SaT is still active in the menu - PL */
00101 {
00102        m_Doc=doc;
00103        if ( m_Doc )
00104        {
00105               Q_ASSERT(target.isEmpty());
00106               Sat = new MenuSAT();
00107               Sat->RunSATPlug(m_Doc);
00108               delete Sat;
00109               Sat = 0;
00110        }
00111        return true;
00112 }
00113 void MenuSAT::RunSATPlug(ScribusDoc* doc)
00114 {
00115        QDir templates(ScPaths::getApplicationDataDir());
00116        if (!templates.exists("templates"))
00117        {
00118               templates.mkdir("templates");
00119        }
00120        QString currentDirPath = QDir::currentDirPath();
00121        QString currentFile = doc->DocName;
00122        bool hasName = doc->hasName;
00123        bool isModified = doc->isModified();
00124        QString userTemplatesDir = PrefsManager::instance()->appPrefs.documentTemplatesDir;
00125        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00126        QString oldCollect = dirs->get("collect", ".");
00127        QString templatesDir = ".";
00128        if (userTemplatesDir.isEmpty())
00129               templatesDir = ScPaths::getApplicationDataDir() + "templates";
00130        else
00131        {
00132               if (userTemplatesDir.right(1) == "/")
00133                      userTemplatesDir = userTemplatesDir.left(userTemplatesDir.length() - 1);
00134               templatesDir = userTemplatesDir;
00135        }
00136        dirs->set("collect", templatesDir);
00137        if (doc->scMW()->Collect().isEmpty())
00138               return;
00139        if (oldCollect != ".")
00140               dirs->set("collect", oldCollect);
00141        QString docPath = doc->DocName;
00142        QString docDir = docPath.left(docPath.findRev('/'));
00143        QString docName = docPath.right(docPath.length() - docPath.findRev('/') - 1);
00144        docName = docName.left(docName.findRev(".s"));
00145 
00146        if (currentFile !=  doc->DocName)
00147        {
00148               satdialog* satdia = new satdialog(doc->scMW(),docName,
00149                                           static_cast<int>(doc->pageWidth + 0.5),
00150                                           static_cast<int>(doc->pageHeight + 0.5));
00151               if (satdia->exec())
00152               {
00153                      sat* s = new sat(doc, satdia, docPath.right(docPath.length() - docPath.findRev('/') - 1),docDir);
00154                      s->createImages();
00155                      s->createTmplXml();
00156                      delete s;
00157               }
00158               // Restore the state that was before ScMW->Collect()
00159               doc->DocName = currentFile;
00160               doc->hasName = hasName;
00161               doc->setModified(isModified);
00162               QString newCaption=currentFile;
00163               if (isModified)
00164                      newCaption.append('*');
00165               doc->scMW()->updateActiveWindowCaption(newCaption);
00166               doc->scMW()->removeRecent(docPath);
00167               QDir::setCurrent(currentDirPath);
00168               delete satdia;
00169        }
00170 }
00171 
00172 // --------------------- CLASS sat ------------------------------------------------//
00173 
00174 sat::sat(ScribusDoc* doc, satdialog* satdia, QString fileName, QString tmplDir)
00175 {
00176        lang = ScCore->getGuiLanguage();
00177        m_Doc = doc;
00178        dia = satdia;
00179        dir = tmplDir;
00180        if (dir.right(1) == "/")
00181               dir = tmplDir.left(tmplDir.length() - 1);
00182        file = fileName;
00183        tmplXmlFile = findTemplateXml(dir);
00184 }
00185 
00186 void sat::createTmplXml()
00187 {
00188        QFile tmplXml(tmplXmlFile);
00189        if (tmplXml.exists())
00190        {
00191               appendTmplXml();
00192               return; // We don't want to overwrite the template.xml file
00193        }
00194        QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00195        xml += "<templates>\n";
00196        xml += getTemplateTag();
00197        xml += "</templates>\n";
00198        if ( tmplXml.open( IO_WriteOnly ) )
00199        {
00200               QTextStream stream(&tmplXml);
00201               stream.setEncoding(QTextStream::UnicodeUTF8);
00202               stream << xml;
00203               tmplXml.close();
00204        }
00205 }
00206 
00207 void sat::createImages()
00208 {
00209        QString tnsmallName = dia->nameEdit->text() + "tn.png";
00210        QString tnlargeName = dia->nameEdit->text() + ".png";
00211        double pageh = m_Doc->pageHeight;
00212        double pagew = m_Doc->pageWidth;
00213        int pageSizeSmall = 0;
00214        int pageSizeLarge = 0;
00215        if (pageh > pagew)
00216        {
00217               pageSizeSmall = static_cast<int>(pageh / 10);
00218               pageSizeLarge = static_cast<int>(pageh / 3);
00219        }
00220        else
00221        {
00222               pageSizeSmall = static_cast<int>(pagew / 10);
00223               pageSizeLarge = static_cast<int>(pagew / 3);
00224        }
00225        QImage tnsmall = m_Doc->view()->PageToPixmap(0,pageSizeSmall);
00226        QImage tnlarge = m_Doc->view()->PageToPixmap(0,pageSizeLarge);
00227        tnsmall.save(dir+"/"+tnsmallName,"PNG",70);
00228        tnlarge.save(dir+"/"+tnlargeName, "PNG", 70);
00229 }
00230 
00231 void sat::appendTmplXml()
00232 {
00233        QFile tmplXml(tmplXmlFile);
00234        if (tmplXml.open(IO_ReadOnly))
00235        {
00236               QTextStream stream(&tmplXml);
00237               QString tmp = stream.readLine();
00238               QString file = "";
00239               while (tmp != NULL)
00240               {
00241                      file += tmp + "\n";
00242                      tmp = stream.readLine();
00243                      if (tmp.find("</templates>") != -1)
00244                             file += getTemplateTag();
00245               }
00246               tmplXml.close();
00247               if ( tmplXml.open( IO_WriteOnly ) )
00248               {
00249                      QTextStream stream2(&tmplXml);
00250                      stream2.setEncoding(QTextStream::UnicodeUTF8);
00251                      stream2 << file;
00252                      tmplXml.close();
00253               }
00254        }
00255 }
00256 
00257 QString sat::getTemplateTag()
00258 {
00259        QString category = dia->catsCombo->currentText();
00260        if (category.isEmpty())
00261               category = QObject::tr("Own Templates");
00262        else
00263        {
00264               for (uint i = 0; i < dia->cats.size(); i++)
00265               {
00266                      QString tmp = *dia->cats[i]->second;
00267                      if (category == tmp)
00268                      {
00269                             category = *dia->cats[i]->first;
00270                             break;
00271                      }
00272               }
00273        }
00274        QDate now = QDate::currentDate();
00275        QString cat = QString(category);
00276        replaceIllegalChars(cat);
00277        QString tag = "\t<template category=\""+cat+"\">\n";
00278        QString name = QString(dia->nameEdit->text());
00279        replaceIllegalChars(name);
00280        tag += "\t\t<name>"+name+"</name>\n";
00281        tag += "\t\t<file>"+file+"</file>\n";
00282        tag += "\t\t<tnail>"+name+"tn.png"+"</tnail>\n";
00283        tag += "\t\t<img>"+name+".png"+"</img>\n";
00284        QString psize = QString(dia->psizeEdit->text());
00285        replaceIllegalChars(psize);
00286        tag += "\t\t<psize>"+psize+"</psize>\n";
00287        QString colors = QString(dia->colorsEdit->text());
00288        replaceIllegalChars(colors);
00289        tag += "\t\t<color>"+colors+"</color>\n";
00290        QString descr = QString(dia->descrEdit->text());
00291        replaceIllegalChars(descr);
00292        tag += "\t\t<descr>"+descr+"</descr>\n";
00293        QString usage = QString(dia->usageEdit->text());
00294        replaceIllegalChars(usage);
00295        tag += "\t\t<usage>"+usage+"</usage>\n";
00296        QString scribus_version = QString(VERSION);
00297        replaceIllegalChars(scribus_version);
00298        tag += "\t\t<scribus_version>" + scribus_version + "</scribus_version>\n";
00299        QString date = QString(now.toString(Qt::ISODate));
00300        replaceIllegalChars(date);
00301        tag += "\t\t<date>" + date + "</date>\n";
00302        QString author = QString(dia->authorEdit->text());
00303        replaceIllegalChars(author);
00304        tag += "\t\t<author>"+author+"</author>\n";
00305        QString email = QString(dia->emailEdit->text());
00306        replaceIllegalChars(email);
00307        tag += "\t\t<email>"+email+"</email>\n";
00308        tag += "\t</template>\n";
00309 
00310        return tag;
00311 }
00312 
00313 void sat::replaceIllegalChars(QString& s)
00314 {
00315        s.replace("&amp;", "&");
00316        s.replace("&lt;", "<");
00317        s.replace("&gt;", ">");
00318        s.replace("&quot;", "\"");
00319        s.replace("&apos;", "\'");
00320        s.replace("&", "&amp;");
00321        s.replace("<", "&lt;");
00322        s.replace(">", "&gt;");
00323        s.replace("\"", "&quot;");
00324        s.replace("\'", "&apos;");
00325 }
00326 
00327 QString sat::findTemplateXml(QString dir)
00328 {
00329        QString tmp = dir + "/template." + lang + ".xml";
00330        if (QFile(tmp).exists())
00331               return tmp;
00332 
00333        if (lang.length() > 2)
00334        {
00335               tmp = dir + "/template." + lang.left(2) + ".xml";
00336               if (QFile(tmp).exists())
00337                      return tmp;
00338        }
00339        return dir + "/template.xml";
00340 }
00341 
00342 sat::~sat()
00343 {
00344 }