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