Subversion Repositories Scribus

Rev

Rev 5781 | 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"
11
#include "satemplate.moc"
3207 craig 12
#include "satdialog.h"
5781 cbradney 13
 
5917 jghali 14
#include "scpaths.h"
5249 mrdocs 15
#include "scribuscore.h"
5781 cbradney 16
#include "scribusdoc.h"
2834 cbradney 17
#include "prefsfile.h"
18
#include "prefsmanager.h"
308 Franz 19
 
3207 craig 20
int saveastemplateplugin_getPluginAPIVersion()
304 Franz 21
{
3207 craig 22
        return PLUGIN_API_VERSION;
304 Franz 23
}
24
 
3207 craig 25
ScPlugin* saveastemplateplugin_getPlugin()
304 Franz 26
{
3207 craig 27
        SaveAsTemplatePlugin* plug = new SaveAsTemplatePlugin();
28
        Q_CHECK_PTR(plug);
29
        return plug;
304 Franz 30
}
31
 
3207 craig 32
void saveastemplateplugin_freePlugin(ScPlugin* plugin)
512 fschmid 33
{
3207 craig 34
        SaveAsTemplatePlugin* plug = dynamic_cast<SaveAsTemplatePlugin*>(plugin);
35
        Q_ASSERT(plug);
36
        delete plug;
512 fschmid 37
}
38
 
3487 craig 39
SaveAsTemplatePlugin::SaveAsTemplatePlugin() : ScActionPlugin()
1208 cbradney 40
{
3207 craig 41
        // Set action info in languageChange, so we only have to do
42
        // it in one place.
43
        languageChange();
1208 cbradney 44
}
45
 
3207 craig 46
SaveAsTemplatePlugin::~SaveAsTemplatePlugin() {};
47
 
48
void SaveAsTemplatePlugin::languageChange()
1208 cbradney 49
{
3207 craig 50
        // Note that we leave the unused members unset. They'll be initialised
51
        // with their default ctors during construction.
52
        // Action name
53
        m_actionInfo.name = "SaveAsDocumentTemplate";
54
        // Action text for menu, including accel
55
        m_actionInfo.text = tr("Save as &Template...");
56
        // Shortcut
57
        m_actionInfo.keySequence = "Ctrl+Alt+S";
58
        // Menu
59
        m_actionInfo.menu = "File";
60
        m_actionInfo.menuAfterName = "SaveAs";
61
        m_actionInfo.enabledOnStartup = true;
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
        }
120
        QString currentDirPath = QDir::currentDirPath();
5781 cbradney 121
        QString currentFile = doc->DocName;
122
        bool hasName = doc->hasName;
123
        bool isModified = doc->isModified();
2834 cbradney 124
        QString userTemplatesDir = PrefsManager::instance()->appPrefs.documentTemplatesDir;
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) == "/")
133
                        userTemplatesDir = userTemplatesDir.left(userTemplatesDir.length() - 1);
415 Franz 134
                templatesDir = userTemplatesDir;
409 Franz 135
        }
415 Franz 136
        dirs->set("collect", templatesDir);
5781 cbradney 137
        if (doc->scMW()->Collect().isEmpty())
308 Franz 138
                return;
415 Franz 139
        if (oldCollect != ".")
140
                dirs->set("collect", oldCollect);
5781 cbradney 141
        QString docPath = doc->DocName;
304 Franz 142
        QString docDir = docPath.left(docPath.findRev('/'));
143
        QString docName = docPath.right(docPath.length() - docPath.findRev('/') - 1);
144
        docName = docName.left(docName.findRev(".s"));
308 Franz 145
 
5781 cbradney 146
        if (currentFile !=  doc->DocName)
409 Franz 147
        {
5781 cbradney 148
                satdialog* satdia = new satdialog(doc->scMW(),docName,
149
                                          static_cast<int>(doc->pageWidth + 0.5),
150
                                          static_cast<int>(doc->pageHeight + 0.5));
304 Franz 151
                if (satdia->exec())
152
                {
5781 cbradney 153
                        sat* s = new sat(doc, satdia, docPath.right(docPath.length() - docPath.findRev('/') - 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);
309 Franz 167
                QDir::setCurrent(currentDirPath);
304 Franz 168
                delete satdia;
169
        }
170
}
171
 
172
// --------------------- CLASS sat ------------------------------------------------//
173
 
5781 cbradney 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";
198
        if ( tmplXml.open( IO_WriteOnly ) )
199
        {
200
                QTextStream stream(&tmplXml);
201
                stream.setEncoding(QTextStream::UnicodeUTF8);
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";
5781 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);
234
        if (tmplXml.open(IO_ReadOnly))
235
        {
236
                QTextStream stream(&tmplXml);
237
                QString tmp = stream.readLine();
238
                QString file = "";
239
                while (tmp != NULL)
240
                {
241
                        file += tmp + "\n";
242
                        tmp = stream.readLine();
243
                        if (tmp.find("</templates>") != -1)
244
                                file += getTemplateTag();
245
                }
246
                tmplXml.close();
247
                if ( tmplXml.open( IO_WriteOnly ) )
248
                {
249
                        QTextStream stream2(&tmplXml);
250
                        stream2.setEncoding(QTextStream::UnicodeUTF8);
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
263
        {
264
                for (uint i = 0; i < dia->cats.size(); i++)
265
                {
266
                        QString tmp = *dia->cats[i]->second;
267
                        if (category == tmp)
268
                        {
269
                                category = *dia->cats[i]->first;
270
                                break;
271
                        }
272
                }
273
        }
308 Franz 274
        QDate now = QDate::currentDate();
385 Franz 275
        QString cat = QString(category);
276
        replaceIllegalChars(cat);
277
        QString tag = "\t<template category=\""+cat+"\">\n";
278
        QString name = QString(dia->nameEdit->text());
279
        replaceIllegalChars(name);
280
        tag += "\t\t<name>"+name+"</name>\n";
304 Franz 281
        tag += "\t\t<file>"+file+"</file>\n";
385 Franz 282
        tag += "\t\t<tnail>"+name+"tn.png"+"</tnail>\n";
283
        tag += "\t\t<img>"+name+".png"+"</img>\n";
284
        QString psize = QString(dia->psizeEdit->text());
285
        replaceIllegalChars(psize);
286
        tag += "\t\t<psize>"+psize+"</psize>\n";
287
        QString colors = QString(dia->colorsEdit->text());
288
        replaceIllegalChars(colors);
289
        tag += "\t\t<color>"+colors+"</color>\n";
290
        QString descr = QString(dia->descrEdit->text());
291
        replaceIllegalChars(descr);
292
        tag += "\t\t<descr>"+descr+"</descr>\n";
293
        QString usage = QString(dia->usageEdit->text());
294
        replaceIllegalChars(usage);
295
        tag += "\t\t<usage>"+usage+"</usage>\n";
296
        QString scribus_version = QString(VERSION);
297
        replaceIllegalChars(scribus_version);
298
        tag += "\t\t<scribus_version>" + scribus_version + "</scribus_version>\n";
299
        QString date = QString(now.toString(Qt::ISODate));
300
        replaceIllegalChars(date);
301
        tag += "\t\t<date>" + date + "</date>\n";
302
        QString author = QString(dia->authorEdit->text());
303
        replaceIllegalChars(author);
304
        tag += "\t\t<author>"+author+"</author>\n";
305
        QString email = QString(dia->emailEdit->text());
306
        replaceIllegalChars(email);
307
        tag += "\t\t<email>"+email+"</email>\n";
304 Franz 308
        tag += "\t</template>\n";
1552 subik 309
 
304 Franz 310
        return tag;
311
}
312
 
385 Franz 313
void sat::replaceIllegalChars(QString& s)
314
{
315
        s.replace("&amp;", "&");
316
        s.replace("&lt;", "<");
317
        s.replace("&gt;", ">");
318
        s.replace("&quot;", "\"");
319
        s.replace("&apos;", "\'");
320
        s.replace("&", "&amp;");
321
        s.replace("<", "&lt;");
322
        s.replace(">", "&gt;");
323
        s.replace("\"", "&quot;");
324
        s.replace("\'", "&apos;");
325
}
326
 
320 Franz 327
QString sat::findTemplateXml(QString dir)
328
{
329
        QString tmp = dir + "/template." + lang + ".xml";
330
        if (QFile(tmp).exists())
331
                return tmp;
332
 
333
        if (lang.length() > 2)
334
        {
335
                tmp = dir + "/template." + lang.left(2) + ".xml";
336
                if (QFile(tmp).exists())
337
                        return tmp;
338
        }
339
        return dir + "/template.xml";
340
}
341
 
304 Franz 342
sat::~sat()
343
{
344
}