]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-base.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / export-base.cpp
1 #include "export-base.h"
2
3 #include <cstdlib>
4
5 #include <QDebug>
6 #include <QFileDialog>
7 #include <QHash>
8 #include <QMessageBox>
9
10 #include "branchitem.h"
11 #include "file.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
14 #include "misc.h"
15 #include "vymprocess.h"
16 #include "warningdialog.h"
17 #include "xsltproc.h"
18
19 extern Main *mainWindow;
20 extern QDir lastExportDir;
21
22 ExportBase::ExportBase() { init(); }
23
24 ExportBase::ExportBase(VymModel *m)
25 {
26     model = m;
27     init();
28 }
29
30 ExportBase::~ExportBase()
31 {
32     // Cleanup tmpdir: No longer required, part of general tmp dir of vym
33     // instance now
34
35     // Remember current directory
36     lastExportDir = QDir(dirPath);
37 }
38
39 void ExportBase::init()
40 {
41     indentPerDepth = "  ";
42     exportName = "unnamed";
43     lastCommand = "";
44     cancelFlag = false;
45     result = Undefined;
46     defaultDirPath = lastExportDir.absolutePath();
47     dirPath = defaultDirPath;
48 }
49
50 void ExportBase::setupTmpDir()
51 {
52     bool ok;
53     tmpDir.setPath(makeTmpDir(ok, model->tmpDirPath(),
54                               QString("export-%2").arg(exportName)));
55     if (!tmpDir.exists() || !ok)
56         QMessageBox::critical(
57             0, QObject::tr("Error"),
58             QObject::tr("Couldn't access temporary directory\n"));
59 }
60
61 void ExportBase::setDirPath(const QString &s)
62 {
63     if (!s.isEmpty())
64         dirPath = s;
65     // Otherwise lastExportDir is used, which defaults to current dir
66 }
67
68 QString ExportBase::getDirPath() { return dirPath; }
69
70 void ExportBase::setFilePath(const QString &s)
71 {
72     if (!s.isEmpty()) {
73         filePath = s;
74         if (!filePath.startsWith("/"))
75             // Absolute path
76             filePath = lastExportDir.absolutePath() + "/" + filePath;
77     }
78 }
79
80 QString ExportBase::getFilePath()
81 {
82     if (!filePath.isEmpty())
83         return filePath;
84     else
85         return dirPath + "/" + model->getMapName() + extension;
86 }
87
88 QString ExportBase::getMapName()
89 {
90     QString fn = basename(filePath);
91     return fn.left(fn.lastIndexOf("."));
92 }
93
94 void ExportBase::setModel(VymModel *m) { model = m; }
95
96 void ExportBase::setWindowTitle(const QString &s) { caption = s; }
97
98 void ExportBase::setName(const QString &s) { exportName = s; }
99
100 QString ExportBase::getName() { return exportName; }
101
102 void ExportBase::addFilter(const QString &s) { filter = s; }
103
104 void ExportBase::setListTasks(bool b) { listTasks = b; }
105
106 bool ExportBase::execDialog()
107 {
108     QString fn =
109         QFileDialog::getSaveFileName(nullptr, caption, filePath, filter, nullptr,
110                                      QFileDialog::DontConfirmOverwrite);
111
112     if (!fn.isEmpty()) {
113         if (QFile(fn).exists()) {
114             WarningDialog dia;
115             dia.showCancelButton(true);
116             dia.setCaption(QObject::tr("Warning: Overwriting file"));
117             dia.setText(
118                 QObject::tr(
119                     "Exporting to %1 will overwrite the existing file:\n%2")
120                     .arg(exportName)
121                     .arg(fn));
122             dia.setShowAgainName("/exports/overwrite/" + exportName);
123             if (!(dia.exec() == QDialog::Accepted)) {
124                 cancelFlag = true;
125                 return false;
126             }
127         }
128         dirPath = fn.left(fn.lastIndexOf("/"));
129         filePath = fn;
130         return true;
131     }
132     return false;
133 }
134
135 bool ExportBase::canceled() { return cancelFlag; }
136
137 void ExportBase::setLastCommand(const QString &s) { lastCommand = s; }
138
139 void ExportBase::setResult(const Result &r)
140 {
141     result = r;
142 }
143
144 void ExportBase::completeExport(QStringList args)
145 {
146     QString command;
147
148     if (args.isEmpty()) {
149         // Add at least filepath as argument. exportName is added anyway
150         command = QString("vym.currentMap().exportMap(\"%1\",\"%2\")")
151                       .arg(exportName)
152                       .arg(filePath);
153     }
154     else {
155         // Only add exportName as default, rest of arguments need to be passed
156         // (Cloud exports ahve no filename...)
157         command = QString("vym.currentMap().exportMap(\"%1\"")
158                       .arg(exportName);
159
160         foreach (QString arg, args)
161             command += QString(", \"%1\"").arg(arg);
162
163         command += ")";
164     }
165
166     model->setExportLastCommand(command);
167     model->setExportLastDestination(displayedDestination);
168     model->setExportLastDescription(exportName);
169
170     // Trigger saving of export command if it has changed
171     if (model && (lastCommand != command))
172         model->setChanged();
173
174     switch (result) {
175         case Success:
176             mainWindow->statusMessage(
177                 QString("Exported as %1 to %2").arg(exportName).arg(displayedDestination));
178             break;
179         case Failed:
180             mainWindow->statusMessage(QString("Failed to export as %1 to %2")
181                                       .arg(exportName)
182                                       .arg(displayedDestination));
183             break;
184         case Ongoing:
185             break;
186         default:
187             qWarning() << "Export base: undefined export result for " << exportName;
188         }
189 }
190
191 void ExportBase::completeExport()
192 {
193     completeExport(QStringList());
194 }
195
196 QString ExportBase::getSectionString(TreeItem *start)
197 {
198     // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
199     QString r;
200     TreeItem *ti = start;
201     int depth = ti->depth();
202     while (depth > 0) {
203         r = QString("%1").arg(1 + ti->num(), 0, 10) + "." + r;
204         ti = ti->parent();
205         depth = ti->depth();
206     }
207     if (r.isEmpty())
208         return r;
209     else
210         return r + " ";
211 }
212
213 QString ExportBase::indent(const int &n, bool useBullet)
214 {
215     QString s;
216     for (int i = 0; i < n; i++)
217         s += indentPerDepth;
218     if (useBullet && s.length() >= 2 && bulletPoints.count() > n)
219         s.replace(s.length() - 2, 1, bulletPoints.at(n));
220     return s;
221 }