Subversion Repositories Scribus

Rev

Rev 17572 | Rev 19093 | Go to most recent revision | 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 "satemplate.h"
3207 craig 11
#include "satdialog.h"
5781 cbradney 12
 
5917 jghali 13
#include "scpaths.h"
5249 mrdocs 14
#include "scribuscore.h"
5781 cbradney 15
#include "scribusdoc.h"
2834 cbradney 16
#include "prefsfile.h"
17
#include "prefsmanager.h"
308 Franz 18
 
3207 craig 19
int saveastemplateplugin_getPluginAPIVersion()
304 Franz 20
{
3207 craig 21
        return PLUGIN_API_VERSION;
304 Franz 22
}
23
 
3207 craig 24
ScPlugin* saveastemplateplugin_getPlugin()
304 Franz 25
{
3207 craig 26
        SaveAsTemplatePlugin* plug = new SaveAsTemplatePlugin();
27
        Q_CHECK_PTR(plug);
28
        return plug;
304 Franz 29
}
30
 
3207 craig 31
void saveastemplateplugin_freePlugin(ScPlugin* plugin)
512 fschmid 32
{
3207 craig 33
        SaveAsTemplatePlugin* plug = dynamic_cast<SaveAsTemplatePlugin*>(plugin);
34
        Q_ASSERT(plug);
35
        delete plug;
512 fschmid 36
}
37
 
3487 craig 38
SaveAsTemplatePlugin::SaveAsTemplatePlugin() : ScActionPlugin()
1208 cbradney 39
{
3207 craig 40
        // Set action info in languageChange, so we only have to do
41
        // it in one place.
42
        languageChange();
1208 cbradney 43
}
44
 
3207 craig 45
SaveAsTemplatePlugin::~SaveAsTemplatePlugin() {};
46
 
47
void SaveAsTemplatePlugin::languageChange()
1208 cbradney 48
{
3207 craig 49
        // Note that we leave the unused members unset. They'll be initialised
50
        // with their default ctors during construction.
51
        // Action name
52
        m_actionInfo.name = "SaveAsDocumentTemplate";
53
        // Action text for menu, including accel
54
        m_actionInfo.text = tr("Save as &Template...");
55
        // Shortcut
56
        m_actionInfo.keySequence = "Ctrl+Alt+S";
57
        // Menu
58
        m_actionInfo.menu = "File";
17572 craig 59
        m_actionInfo.menuAfterName = "fileSaveAs";
3207 craig 60
        m_actionInfo.enabledOnStartup = true;
10956 fschmid 61
        m_actionInfo.needsNumObjects = -1;
1208 cbradney 62
}
63
 
3207 craig 64
const QString SaveAsTemplatePlugin::fullTrName() const
1208 cbradney 65
{
3207 craig 66
        return QObject::tr("Save As Template");
1208 cbradney 67
}
68
 
3207 craig 69
const ScActionPlugin::AboutData* SaveAsTemplatePlugin::getAboutData() const
1208 cbradney 70
{
3241 craig 71
        AboutData* about = new AboutData;
72
        Q_CHECK_PTR(about);
3834 tsoots 73
        about->authors = QString::fromUtf8("Riku Leino <riku@scribus.info>");
74
        about->shortDescription = tr("Save a document as a template");
75
        about->description = tr("Save a document as a template. Good way to ease the "
76
                                "initial work for documents with a constant look");
77
    // about->version
78
    // about->releaseDate
79
    // about->copyright
80
        about->license = "GPL";
3241 craig 81
        return about;
1208 cbradney 82
}
83
 
3241 craig 84
void SaveAsTemplatePlugin::deleteAboutData(const AboutData* about) const
1208 cbradney 85
{
3241 craig 86
        Q_ASSERT(about);
87
        delete about;
1208 cbradney 88
}
304 Franz 89
 
5781 cbradney 90
bool SaveAsTemplatePlugin::run(ScribusDoc* doc, QString target)
3925 mrdocs 91
/*{
92
        Q_ASSERT(target.isEmpty());
93
        Sat = new MenuSAT();
94
        Sat->RunSATPlug();
95
        delete Sat;
96
        Sat = 0;
97
        return true;
98
}
99
*/
100
/* jghali's fix when the new file dialog is cancelled SaT is still active in the menu - PL */
1322 cbradney 101
{
5781 cbradney 102
        m_Doc=doc;
103
        if ( m_Doc )
104
        {
105
                Q_ASSERT(target.isEmpty());
106
                Sat = new MenuSAT();
107
                Sat->RunSATPlug(m_Doc);
108
                delete Sat;
109
                Sat = 0;
110
        }
3207 craig 111
        return true;
1322 cbradney 112
}
5781 cbradney 113
void MenuSAT::RunSATPlug(ScribusDoc* doc)
304 Franz 114
{
5917 jghali 115
        QDir templates(ScPaths::getApplicationDataDir());
304 Franz 116
        if (!templates.exists("templates"))
117
        {
118
                templates.mkdir("templates");
119
        }
10430 cbradney 120
        QString currentPath(QDir::currentPath());
121
        QString currentFile(doc->DocName);
5781 cbradney 122
        bool hasName = doc->hasName;
123
        bool isModified = doc->isModified();
13876 cbradney 124
        QString userTemplatesDir = PrefsManager::instance()->appPrefs.pathPrefs.documentTemplates;
2856 cbradney 125
        PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
415 Franz 126
        QString oldCollect = dirs->get("collect", ".");
127
        QString templatesDir = ".";
2877 cbradney 128
        if (userTemplatesDir.isEmpty())
5917 jghali 129
                templatesDir = ScPaths::getApplicationDataDir() + "templates";
409 Franz 130
        else
131
        {
132
                if (userTemplatesDir.right(1) == "/")
15498 jghali 133
                        userTemplatesDir.chop(1);
415 Franz 134
                templatesDir = userTemplatesDir;
409 Franz 135
        }
415 Franz 136
        dirs->set("collect", templatesDir);
15207 cbradney 137
        if (doc->scMW()->fileCollect().isEmpty())
308 Franz 138
                return;
415 Franz 139
        if (oldCollect != ".")
140
                dirs->set("collect", oldCollect);
5781 cbradney 141
        QString docPath = doc->DocName;
10398 cbradney 142
        QString docDir = docPath.left(docPath.lastIndexOf('/'));
143
        QString docName = docPath.right(docPath.length() - docPath.lastIndexOf('/') - 1);
144
        docName = docName.left(docName.lastIndexOf(".s"));
308 Franz 145
 
5781 cbradney 146
        if (currentFile !=  doc->DocName)
409 Franz 147
        {
17578 craig 148
                SATDialog* satdia = new SATDialog(doc->scMW(),docName,
14991 cbradney 149
                                                                                  static_cast<int>(doc->pageWidth() + 0.5),
150
                                                                                  static_cast<int>(doc->pageHeight() + 0.5));
304 Franz 151
                if (satdia->exec())
152
                {
10398 cbradney 153
                        sat* s = new sat(doc, satdia, docPath.right(docPath.length() - docPath.lastIndexOf('/') - 1),docDir);
304 Franz 154
                        s->createImages();
155
                        s->createTmplXml();
156
                        delete s;
157
                }
4026 craig 158
                // Restore the state that was before ScMW->Collect()
5781 cbradney 159
                doc->DocName = currentFile;
160
                doc->hasName = hasName;
161
                doc->setModified(isModified);
3565 cbradney 162
                QString newCaption=currentFile;
309 Franz 163
                if (isModified)
3565 cbradney 164
                        newCaption.append('*');
5781 cbradney 165
                doc->scMW()->updateActiveWindowCaption(newCaption);
166
                doc->scMW()->removeRecent(docPath);
10398 cbradney 167
                QDir::setCurrent(currentPath);
304 Franz 168
                delete satdia;
169
        }
170
}
171
 
172
// --------------------- CLASS sat ------------------------------------------------//
173
 
17578 craig 174
sat::sat(ScribusDoc* doc, SATDialog* satdia, QString fileName, QString tmplDir)
304 Franz 175
{
5249 mrdocs 176
        lang = ScCore->getGuiLanguage();
5781 cbradney 177
        m_Doc = doc;
304 Franz 178
        dia = satdia;
179
        dir = tmplDir;
180
        if (dir.right(1) == "/")
181
                dir = tmplDir.left(tmplDir.length() - 1);
182
        file = fileName;
320 Franz 183
        tmplXmlFile = findTemplateXml(dir);
304 Franz 184
}
185
 
186
void sat::createTmplXml()
187
{
188
        QFile tmplXml(tmplXmlFile);
189
        if (tmplXml.exists())
190
        {
191
                appendTmplXml();
192
                return; // We don't want to overwrite the template.xml file
193
        }
194
        QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
195
        xml += "<templates>\n";
196
        xml += getTemplateTag();
197
        xml += "</templates>\n";
8501 cbradney 198
        if ( tmplXml.open( QIODevice::WriteOnly ) )
304 Franz 199
        {
9603 subik 200
                QTextStream stream(&tmplXml);
10004 fschmid 201
                stream.setCodec("UTF-8");
304 Franz 202
                stream << xml;
203
                tmplXml.close();
204
        }
205
}
206
 
207
void sat::createImages()
208
{
209
        QString tnsmallName = dia->nameEdit->text() + "tn.png";
210
        QString tnlargeName = dia->nameEdit->text() + ".png";
14991 cbradney 211
        double pageh = m_Doc->pageHeight();
212
        double pagew = m_Doc->pageWidth();
304 Franz 213
        int pageSizeSmall = 0;
214
        int pageSizeLarge = 0;
215
        if (pageh > pagew)
216
        {
217
                pageSizeSmall = static_cast<int>(pageh / 10);
218
                pageSizeLarge = static_cast<int>(pageh / 3);
219
        }
220
        else
221
        {
222
                pageSizeSmall = static_cast<int>(pagew / 10);
223
                pageSizeLarge = static_cast<int>(pagew / 3);
224
        }
5781 cbradney 225
        QImage tnsmall = m_Doc->view()->PageToPixmap(0,pageSizeSmall);
226
        QImage tnlarge = m_Doc->view()->PageToPixmap(0,pageSizeLarge);
304 Franz 227
        tnsmall.save(dir+"/"+tnsmallName,"PNG",70);
228
        tnlarge.save(dir+"/"+tnlargeName, "PNG", 70);
229
}
230
 
231
void sat::appendTmplXml()
232
{
233
        QFile tmplXml(tmplXmlFile);
8501 cbradney 234
        if (tmplXml.open(QIODevice::ReadOnly))
304 Franz 235
        {
9603 subik 236
                QTextStream stream(&tmplXml);
304 Franz 237
                QString tmp = stream.readLine();
238
                QString file = "";
8548 cbradney 239
                while (!tmp.isNull())
304 Franz 240
                {
241
                        file += tmp + "\n";
242
                        tmp = stream.readLine();
10398 cbradney 243
                        if (tmp.indexOf("</templates>") != -1)
304 Franz 244
                                file += getTemplateTag();
245
                }
246
                tmplXml.close();
8501 cbradney 247
                if ( tmplXml.open( QIODevice::WriteOnly ) )
304 Franz 248
                {
9603 subik 249
                        QTextStream stream2(&tmplXml);
10004 fschmid 250
                        stream2.setCodec("UTF-8");
304 Franz 251
                        stream2 << file;
252
                        tmplXml.close();
253
                }
254
        }
255
}
256
 
257
QString sat::getTemplateTag()
258
{
259
        QString category = dia->catsCombo->currentText();
2877 cbradney 260
        if (category.isEmpty())
304 Franz 261
                category = QObject::tr("Own Templates");
262
        else
10713 jghali 263
                category = dia->cats.key(category, category);
308 Franz 264
        QDate now = QDate::currentDate();
385 Franz 265
        QString cat = QString(category);
266
        replaceIllegalChars(cat);
267
        QString tag = "\t<template category=\""+cat+"\">\n";
268
        QString name = QString(dia->nameEdit->text());
269
        replaceIllegalChars(name);
270
        tag += "\t\t<name>"+name+"</name>\n";
304 Franz 271
        tag += "\t\t<file>"+file+"</file>\n";
385 Franz 272
        tag += "\t\t<tnail>"+name+"tn.png"+"</tnail>\n";
273
        tag += "\t\t<img>"+name+".png"+"</img>\n";
274
        QString psize = QString(dia->psizeEdit->text());
275
        replaceIllegalChars(psize);
276
        tag += "\t\t<psize>"+psize+"</psize>\n";
277
        QString colors = QString(dia->colorsEdit->text());
278
        replaceIllegalChars(colors);
279
        tag += "\t\t<color>"+colors+"</color>\n";
10599 fschmid 280
        QString descr = QString(dia->descrEdit->toPlainText());
385 Franz 281
        replaceIllegalChars(descr);
282
        tag += "\t\t<descr>"+descr+"</descr>\n";
10599 fschmid 283
        QString usage = QString(dia->usageEdit->toPlainText());
385 Franz 284
        replaceIllegalChars(usage);
285
        tag += "\t\t<usage>"+usage+"</usage>\n";
286
        QString scribus_version = QString(VERSION);
287
        replaceIllegalChars(scribus_version);
288
        tag += "\t\t<scribus_version>" + scribus_version + "</scribus_version>\n";
289
        QString date = QString(now.toString(Qt::ISODate));
290
        replaceIllegalChars(date);
291
        tag += "\t\t<date>" + date + "</date>\n";
292
        QString author = QString(dia->authorEdit->text());
293
        replaceIllegalChars(author);
294
        tag += "\t\t<author>"+author+"</author>\n";
295
        QString email = QString(dia->emailEdit->text());
296
        replaceIllegalChars(email);
297
        tag += "\t\t<email>"+email+"</email>\n";
304 Franz 298
        tag += "\t</template>\n";
1552 subik 299
 
304 Franz 300
        return tag;
301
}
302
 
385 Franz 303
void sat::replaceIllegalChars(QString& s)
304
{
305
        s.replace("&amp;", "&");
306
        s.replace("&lt;", "<");
307
        s.replace("&gt;", ">");
308
        s.replace("&quot;", "\"");
309
        s.replace("&apos;", "\'");
310
        s.replace("&", "&amp;");
311
        s.replace("<", "&lt;");
312
        s.replace(">", "&gt;");
313
        s.replace("\"", "&quot;");
314
        s.replace("\'", "&apos;");
315
}
316
 
320 Franz 317
QString sat::findTemplateXml(QString dir)
318
{
319
        QString tmp = dir + "/template." + lang + ".xml";
320
        if (QFile(tmp).exists())
321
                return tmp;
322
 
323
        if (lang.length() > 2)
324
        {
325
                tmp = dir + "/template." + lang.left(2) + ".xml";
326
                if (QFile(tmp).exists())
327
                        return tmp;
328
        }
329
        return dir + "/template.xml";
330
}
331
 
304 Franz 332
sat::~sat()
333
{
334
}