]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-csv.cpp
New upstream version 2.9.22
[sven/vym.git] / src / export-csv.cpp
1 #include "mainwindow.h"
2 #include <QMessageBox>
3
4 #include "export-csv.h"
5
6 extern QString vymName;
7 extern Main *mainWindow;
8
9 ExportCSV::ExportCSV()
10 {
11     exportName = "CSV";
12     filter = "CSV (*.csv);;All (* *.*)";
13     caption = vymName + " -" + QObject::tr("Export as CSV");
14 }
15
16 void ExportCSV::doExport()
17 {
18     QFile file(filePath);
19     if (!file.open(QIODevice::WriteOnly)) {
20         QMessageBox::critical(
21             0, QObject::tr("Critical Export Error"),
22             QObject::tr("Could not export as CSV to %1").arg(filePath));
23         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
24         return;
25     }
26
27     QString out;
28
29     // Write header
30     out += "\"Note\"\n";
31
32     // Main loop over all branches
33     QString s;
34     QString curIndent("");
35     int i;
36     BranchItem *cur = NULL;
37     BranchItem *prev = NULL;
38     model->nextBranch(cur, prev);
39     while (cur) {
40         if (!cur->hasHiddenExportParent()) {
41             // If necessary, write note
42             if (!cur->isNoteEmpty()) {
43                 s = cur->getNoteASCII(0, 0);
44                 s = s.replace("\n", "\n" + curIndent);
45                 out += ("\"" + s + "\",");
46             }
47             else
48                 out += "\"\",";
49
50             // Make indentstring
51             for (i = 0; i < cur->depth(); i++)
52                 curIndent += "\"\",";
53
54             // Write heading
55             out += curIndent + "\"" + cur->getHeadingPlain() + "\"\n";
56         }
57
58         model->nextBranch(cur, prev);
59         curIndent = "";
60     }
61     QTextStream ts(&file);
62     ts.setCodec("UTF-8");
63     ts << out;
64     file.close();
65
66     QClipboard *clipboard = QGuiApplication::clipboard();
67     clipboard->setText(out);
68
69     displayedDestination = filePath;
70
71     result = ExportBase::Success;
72     completeExport();
73 }