1 /************************************************************************
2 **
3 ** Copyright (C) 2015-2021 Kevin B. Hendricks, Stratford, Ontario, Canada
4 ** Copyright (C) 2009-2011 Strahinja Markovic <strahinja.markovic@gmail.com>
5 **
6 ** This file is part of Sigil.
7 **
8 ** Sigil is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation, either version 3 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Sigil is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with Sigil. If not, see <http://www.gnu.org/licenses/>.
20 **
21 *************************************************************************/
22
23 #include "EmbedPython/EmbeddedPython.h"
24 #include <QFileInfo>
25 #include <QDir>
26 #include <QStringList>
27
28 #include "Misc/Utility.h"
29 #include "SourceUpdates/PerformXMLUpdates.h"
30 #include "sigil_constants.h"
31
32
PerformXMLUpdates(const QString & source,const QString & newbookpath,const QHash<QString,QString> & xml_updates,const QString & currentpath,const QString & mtype)33 PerformXMLUpdates::PerformXMLUpdates(const QString &source,
34 const QString &newbookpath,
35 const QHash<QString, QString> &xml_updates,
36 const QString ¤tpath,
37 const QString &mtype)
38 :
39 m_Source(source),
40 m_XMLUpdates(xml_updates),
41 m_CurrentPath(currentpath),
42 m_MediaType(mtype),
43 m_newbookpath(newbookpath)
44 {
45 }
46
47
operator ()()48 QString PerformXMLUpdates::operator()()
49 {
50 QString newsource = m_Source;
51
52 // serialize the hash for passing to python
53 QStringList dictkeys = m_XMLUpdates.keys();
54 QStringList dictvals;
55 foreach(QString key, dictkeys) {
56 dictvals.append(m_XMLUpdates.value(key));
57 }
58
59 int rv = 0;
60 QString error_traceback;
61
62 QList<QVariant> args;
63 args.append(QVariant(newsource));
64 args.append(QVariant(m_newbookpath));
65 args.append(QVariant(m_CurrentPath));
66 args.append(QVariant(dictkeys));
67 args.append(QVariant(dictvals));
68
69 QString routine;
70
71 // MISC_XML_MIMETYPES is defined in BookManipulation/FolderKeeper.cpp and sigil_constants.h
72 if (MISC_XML_MIMETYPES.contains(m_MediaType)) {
73 if (m_MediaType == "application/smil+xml") {
74 routine = "performSMILUpdates";
75 } else if ((m_MediaType == "application/oebps-page-map+xml") ||
76 (m_MediaType == "application/vnd.adobe-page-map+xml")) {
77 routine = "performPageMapUpdates";
78 } else {
79 // We allow editing, but currently have no python parsing/repair/link-updating routines.
80 // Make no changes.
81 // application/adobe-page-template+xml, application/vnd.adobe-page-template+xml, "application/pls+xml"
82 return newsource;
83 }
84 // Utterly unsupported XML mimetypes
85 } else {
86 Utility::DisplayStdWarningDialog(QString("Unsupported XML media-type: ") + m_MediaType);
87 // make no changes
88 return newsource;
89 }
90
91 EmbeddedPython * epython = EmbeddedPython::instance();
92
93 QVariant res = epython->runInPython( QString("xmlprocessor"),
94 routine,
95 args,
96 &rv,
97 error_traceback);
98 if (rv != 0) {
99 Utility::DisplayStdWarningDialog(QString("error in xmlprocessor performXMLUpdates: ") + QString::number(rv),
100 error_traceback);
101 // an error happened - make no changes
102 return newsource;
103 }
104
105 return res.toString();
106 }
107