]> git.sven.stormbind.net Git - sven/vym.git/blobdiff - src/export-firefox.cpp
New upstream version 2.9.22
[sven/vym.git] / src / export-firefox.cpp
diff --git a/src/export-firefox.cpp b/src/export-firefox.cpp
new file mode 100644 (file)
index 0000000..d01f0c0
--- /dev/null
@@ -0,0 +1,99 @@
+#include <QMessageBox>
+
+#include "export-firefox.h"
+
+#include "attributeitem.h"
+#include "mainwindow.h"
+
+extern QString vymName;
+extern Main *mainWindow;
+
+ExportFirefox::ExportFirefox()
+{
+    exportName = "Firefox";
+    filter = "JSON (*.json);;All (* *.*)";
+    caption = vymName + " -" + QObject::tr("Export as Firefox bookmarks");
+}
+
+QJsonObject ExportFirefox::buildList(BranchItem *bi)
+{
+    // Loop over children branches
+    QJsonObject jsobj;
+    QJsonArray jarray;
+
+    if (bi->branchCount() > 0 ) {
+        for (int i = 0; i < bi->branchCount(); i++)
+            jarray.append(buildList(bi->getBranchNum(i)));
+
+        jsobj["children"] = jarray;
+    }
+
+    QString key;
+    AttributeItem *ai;
+    for (int i = 0; i < bi->attributeCount(); i++) {
+        ai =bi->getAttributeNum(i);
+        key = ai->getKey();
+
+        // Rewrite some values, which maybe have been modified in map
+        if (key == "index")
+            ai->setValue(bi->num());
+        else if (key == "uri" && !bi->getURL().isEmpty())
+            ai->setValue(bi->getURL());
+        else if (key == "title" && !bi->getHeadingPlain().isEmpty())
+            ai->setValue(bi->getHeadingPlain());
+
+        // Export values
+        if (key == "postData")
+            jsobj[key] = QJsonValue::Null; 
+        else if (ai->getAttributeType() == AttributeItem::DateTime) 
+            jsobj[key] = QJsonValue(ai->getValue().toDateTime().toMSecsSinceEpoch() * 1000);
+        else if (ai->getAttributeType() == AttributeItem::String)
+            jsobj[key] = ai->getValue().toString();
+        else if (ai->getAttributeType() == AttributeItem::Integer) 
+        {
+            jsobj[key] = QJsonValue(ai->getValue().toInt());
+        }
+        else
+            qWarning() << "ExportFirefox  Unknown attribute type in " << bi->getHeadingPlain() << "Key: " << key;
+    }
+
+    return jsobj;
+}
+
+void ExportFirefox::doExport()
+{
+    QFile file(filePath);
+    if (!file.open(QIODevice::WriteOnly)) {
+        QMessageBox::critical(
+            0, QObject::tr("Critical Export Error"),
+            QObject::tr("Could not export as Firefox bookmarks to %1").arg(filePath));
+        mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
+        return;
+    }
+
+    // Select bookmark main branch
+    model->select("mc:0,bo:0");
+
+    BranchItem *bi = model->getSelectedBranch();
+    if (!bi) return;
+
+    // Loop over all branches
+    QJsonObject jsobj;
+    QJsonArray jarray;
+
+    /*
+    for (int i = 0; i < bi->branchCount(); i++)
+        jarray.append(buildList(bi->getBranchNum(i)));
+
+    jsobj["children"] = jarray;
+    */
+    jsobj = buildList(bi);
+
+    file.write(QJsonDocument(jsobj).toJson());
+    file.close();
+
+    displayedDestination = filePath;
+
+    result = ExportBase::Success;
+    completeExport();
+}