Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

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 "satdialog.h"
00012 
00013 #include "scpaths.h"
00014 #include "scribuscore.h"
00015 #include "scribusdoc.h"
00016 #include "prefsfile.h"
00017 #include "prefsmanager.h"
00018 
00019 int saveastemplateplugin_getPluginAPIVersion()
00020 {
00021     return PLUGIN_API_VERSION;
00022 }
00023 
00024 ScPlugin* saveastemplateplugin_getPlugin()
00025 {
00026     SaveAsTemplatePlugin* plug = new SaveAsTemplatePlugin();
00027     Q_CHECK_PTR(plug);
00028     return plug;
00029 }
00030 
00031 void saveastemplateplugin_freePlugin(ScPlugin* plugin)
00032 {
00033     SaveAsTemplatePlugin* plug = dynamic_cast<SaveAsTemplatePlugin*>(plugin);
00034     Q_ASSERT(plug);
00035     delete plug;
00036 }
00037 
00038 SaveAsTemplatePlugin::SaveAsTemplatePlugin() : ScActionPlugin()
00039 {
00040     // Set action info in languageChange, so we only have to do
00041     // it in one place.
00042     languageChange();
00043 }
00044 
00045 SaveAsTemplatePlugin::~SaveAsTemplatePlugin() {};
00046 
00047 void SaveAsTemplatePlugin::languageChange()
00048 {
00049     // Note that we leave the unused members unset. They'll be initialised
00050     // with their default ctors during construction.
00051     // Action name
00052     m_actionInfo.name = "SaveAsDocumentTemplate";
00053     // Action text for menu, including accel
00054     m_actionInfo.text = tr("Save as &Template...");
00055     // Shortcut
00056     m_actionInfo.keySequence = "Ctrl+Alt+S";
00057     // Menu
00058     m_actionInfo.menu = "File";
00059     m_actionInfo.menuAfterName = "SaveAs";
00060     m_actionInfo.enabledOnStartup = true;
00061     m_actionInfo.needsNumObjects = -1;
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 currentPath(QDir::currentPath());
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.lastIndexOf('/'));
00143     QString docName = docPath.right(docPath.length() - docPath.lastIndexOf('/') - 1);
00144     docName = docName.left(docName.lastIndexOf(".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.lastIndexOf('/') - 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(currentPath);
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( QIODevice::WriteOnly ) )
00199     {
00200         QTextStream stream(&tmplXml);
00201         stream.setCodec("UTF-8");
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(QIODevice::ReadOnly))
00235     {
00236         QTextStream stream(&tmplXml);
00237         QString tmp = stream.readLine();
00238         QString file = "";
00239         while (!tmp.isNull())
00240         {
00241             file += tmp + "\n";
00242             tmp = stream.readLine();
00243             if (tmp.indexOf("</templates>") != -1)
00244                 file += getTemplateTag();
00245         }
00246         tmplXml.close();
00247         if ( tmplXml.open( QIODevice::WriteOnly ) )
00248         {
00249             QTextStream stream2(&tmplXml);
00250             stream2.setCodec("UTF-8");
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         category = dia->cats.key(category, category);
00264     QDate now = QDate::currentDate();
00265     QString cat = QString(category);
00266     replaceIllegalChars(cat);
00267     QString tag = "\t<template category=\""+cat+"\">\n";
00268     QString name = QString(dia->nameEdit->text());
00269     replaceIllegalChars(name);
00270     tag += "\t\t<name>"+name+"</name>\n";
00271     tag += "\t\t<file>"+file+"</file>\n";
00272     tag += "\t\t<tnail>"+name+"tn.png"+"</tnail>\n";
00273     tag += "\t\t<img>"+name+".png"+"</img>\n";
00274     QString psize = QString(dia->psizeEdit->text());
00275     replaceIllegalChars(psize);
00276     tag += "\t\t<psize>"+psize+"</psize>\n";
00277     QString colors = QString(dia->colorsEdit->text());
00278     replaceIllegalChars(colors);
00279     tag += "\t\t<color>"+colors+"</color>\n";
00280     QString descr = QString(dia->descrEdit->toPlainText());
00281     replaceIllegalChars(descr);
00282     tag += "\t\t<descr>"+descr+"</descr>\n";
00283     QString usage = QString(dia->usageEdit->toPlainText());
00284     replaceIllegalChars(usage);
00285     tag += "\t\t<usage>"+usage+"</usage>\n";
00286     QString scribus_version = QString(VERSION);
00287     replaceIllegalChars(scribus_version);
00288     tag += "\t\t<scribus_version>" + scribus_version + "</scribus_version>\n";
00289     QString date = QString(now.toString(Qt::ISODate));
00290     replaceIllegalChars(date);
00291     tag += "\t\t<date>" + date + "</date>\n";
00292     QString author = QString(dia->authorEdit->text());
00293     replaceIllegalChars(author);
00294     tag += "\t\t<author>"+author+"</author>\n";
00295     QString email = QString(dia->emailEdit->text());
00296     replaceIllegalChars(email);
00297     tag += "\t\t<email>"+email+"</email>\n";
00298     tag += "\t</template>\n";
00299 
00300     return tag;
00301 }
00302 
00303 void sat::replaceIllegalChars(QString& s)
00304 {
00305     s.replace("&amp;", "&");
00306     s.replace("&lt;", "<");
00307     s.replace("&gt;", ">");
00308     s.replace("&quot;", "\"");
00309     s.replace("&apos;", "\'");
00310     s.replace("&", "&amp;");
00311     s.replace("<", "&lt;");
00312     s.replace(">", "&gt;");
00313     s.replace("\"", "&quot;");
00314     s.replace("\'", "&apos;");
00315 }
00316 
00317 QString sat::findTemplateXml(QString dir)
00318 {
00319     QString tmp = dir + "/template." + lang + ".xml";
00320     if (QFile(tmp).exists())
00321         return tmp;
00322 
00323     if (lang.length() > 2)
00324     {
00325         tmp = dir + "/template." + lang.left(2) + ".xml";
00326         if (QFile(tmp).exists())
00327             return tmp;
00328     }
00329     return dir + "/template.xml";
00330 }
00331 
00332 sat::~sat()
00333 {
00334 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.