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 <QStringList>
25 #include <QFileInfo>
26 #include <QDir>
27 #include "Misc/Utility.h"
28 #include "SourceUpdates/PerformOPFUpdates.h"
29 
30 
PerformOPFUpdates(const QString & source,const QString & newbookpath,const QHash<QString,QString> & xml_updates,const QString & currentpath)31 PerformOPFUpdates::PerformOPFUpdates(const QString &source,
32                                      const QString &newbookpath,
33                                      const QHash<QString, QString> &xml_updates,
34                                      const QString& currentpath)
35     :
36     m_XMLUpdates(xml_updates),
37     m_CurrentPath(currentpath),
38     m_source(source),
39     m_newbookpath(newbookpath)
40 {
41 }
42 
43 
operator ()()44 QString PerformOPFUpdates::operator()()
45 {
46     QString newsource = m_source;
47 
48     // serialize the hash for passing to python
49     QStringList dictkeys = m_XMLUpdates.keys();
50     QStringList dictvals;
51     foreach(QString key, dictkeys) {
52         dictvals.append(m_XMLUpdates.value(key));
53     }
54 
55     int rv = 0;
56     QString error_traceback;
57 
58     QList<QVariant> args;
59     args.append(QVariant(m_source));
60     args.append(QVariant(m_newbookpath));
61     args.append(QVariant(m_CurrentPath));
62     args.append(QVariant(dictkeys));
63     args.append(QVariant(dictvals));
64 
65     EmbeddedPython * epython  = EmbeddedPython::instance();
66 
67     QVariant res = epython->runInPython( QString("xmlprocessor"),
68                                          QString("performOPFSourceUpdates"),
69                                          args,
70                                          &rv,
71                                          error_traceback);
72     if (rv != 0) {
73         Utility::DisplayStdWarningDialog(QString("error in xmlprocessor performOPFSourceUpdates: ") + QString::number(rv),
74                                      error_traceback);
75         // an error happened - make no changes
76         return newsource;
77     }
78     return res.toString();
79 }
80