]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-firefox.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / export-firefox.cpp
1 #include <QMessageBox>
2
3 #include "export-firefox.h"
4
5 #include "attributeitem.h"
6 #include "mainwindow.h"
7
8 extern QString vymName;
9 extern Main *mainWindow;
10
11 ExportFirefox::ExportFirefox()
12 {
13     exportName = "Firefox";
14     filter = "JSON (*.json);;All (* *.*)";
15     caption = vymName + " -" + QObject::tr("Export as Firefox bookmarks");
16 }
17
18 QJsonObject ExportFirefox::buildList(BranchItem *bi)
19 {
20     // Loop over children branches
21     QJsonObject jsobj;
22     QJsonArray jarray;
23
24     if (bi->branchCount() > 0 ) {
25         for (int i = 0; i < bi->branchCount(); i++)
26             jarray.append(buildList(bi->getBranchNum(i)));
27
28         jsobj["children"] = jarray;
29     }
30
31     QString key;
32     AttributeItem *ai;
33     for (int i = 0; i < bi->attributeCount(); i++) {
34         ai =bi->getAttributeNum(i);
35         key = ai->getKey();
36
37         // Rewrite some values, which maybe have been modified in map
38         if (key == "index")
39             ai->setValue(bi->num());
40         else if (key == "uri" && !bi->getURL().isEmpty())
41             ai->setValue(bi->getURL());
42         else if (key == "title" && !bi->getHeadingPlain().isEmpty())
43             ai->setValue(bi->getHeadingPlain());
44
45         // Export values
46         if (key == "postData")
47             jsobj[key] = QJsonValue::Null; 
48         else if (ai->getAttributeType() == AttributeItem::DateTime) 
49             jsobj[key] = QJsonValue(ai->getValue().toDateTime().toMSecsSinceEpoch() * 1000);
50         else if (ai->getAttributeType() == AttributeItem::String)
51             jsobj[key] = ai->getValue().toString();
52         else if (ai->getAttributeType() == AttributeItem::Integer) 
53         {
54             jsobj[key] = QJsonValue(ai->getValue().toInt());
55         }
56         else
57             qWarning() << "ExportFirefox  Unknown attribute type in " << bi->getHeadingPlain() << "Key: " << key;
58     }
59
60     return jsobj;
61 }
62
63 void ExportFirefox::doExport()
64 {
65     QFile file(filePath);
66     if (!file.open(QIODevice::WriteOnly)) {
67         QMessageBox::critical(
68             0, QObject::tr("Critical Export Error"),
69             QObject::tr("Could not export as Firefox bookmarks to %1").arg(filePath));
70         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
71         return;
72     }
73
74     // Select bookmark main branch
75     model->select("mc:0,bo:0");
76
77     BranchItem *bi = model->getSelectedBranch();
78     if (!bi) return;
79
80     // Loop over all branches
81     QJsonObject jsobj;
82     QJsonArray jarray;
83
84     /*
85     for (int i = 0; i < bi->branchCount(); i++)
86         jarray.append(buildList(bi->getBranchNum(i)));
87
88     jsobj["children"] = jarray;
89     */
90     jsobj = buildList(bi);
91
92     file.write(QJsonDocument(jsobj).toJson());
93     file.close();
94
95     displayedDestination = filePath;
96
97     result = ExportBase::Success;
98     completeExport();
99 }