aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 11:46:20 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 11:46:20 +0200
commitd75794508e1c202d28bdf77c25afb85d80d42554 (patch)
tree79fd7363b0f5d6a999dfbeee85862a49b8b2001c
parentBookmarkItem::DateAdded and LastModified fields (diff)
downloadsmolbote-d75794508e1c202d28bdf77c25afb85d80d42554.tar.xz
Add Import option to BookmarksWidget
-rw-r--r--lib/bookmarks/bookmarkformat.h4
-rw-r--r--src/bookmarks/bookmarksform.ui13
-rw-r--r--src/bookmarks/bookmarkswidget.cpp41
3 files changed, 43 insertions, 15 deletions
diff --git a/lib/bookmarks/bookmarkformat.h b/lib/bookmarks/bookmarkformat.h
index 8b64420..c886546 100644
--- a/lib/bookmarks/bookmarkformat.h
+++ b/lib/bookmarks/bookmarkformat.h
@@ -27,10 +27,6 @@ public:
Q_CHECK_PTR(device);
m_device = device;
}
- ~BookmarkFormat()
- {
- m_device->close();
- }
void read(BookmarkItem *root) const;
void write(BookmarkItem *root);
diff --git a/src/bookmarks/bookmarksform.ui b/src/bookmarks/bookmarksform.ui
index a003e53..2dfebf4 100644
--- a/src/bookmarks/bookmarksform.ui
+++ b/src/bookmarks/bookmarksform.ui
@@ -17,6 +17,13 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
+ <widget class="QToolButton" name="import_toolButton">
+ <property name="text">
+ <string>Import</string>
+ </property>
+ </widget>
+ </item>
+ <item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -66,12 +73,12 @@
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
- <attribute name="headerDefaultSectionSize">
- <number>200</number>
- </attribute>
<attribute name="headerMinimumSectionSize">
<number>50</number>
</attribute>
+ <attribute name="headerDefaultSectionSize">
+ <number>200</number>
+ </attribute>
</widget>
</item>
<item>
diff --git a/src/bookmarks/bookmarkswidget.cpp b/src/bookmarks/bookmarkswidget.cpp
index 26fdddf..bd2d607 100644
--- a/src/bookmarks/bookmarkswidget.cpp
+++ b/src/bookmarks/bookmarkswidget.cpp
@@ -12,6 +12,7 @@
#include "bookmarkformat.h"
#include <QTreeView>
#include <QUrl>
+#include <QFileDialog>
inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIndex &rootIndex)
{
@@ -26,6 +27,29 @@ inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIn
}
}
+inline void loadModel(const QString &path, BookmarkModel *model, QTreeView *view)
+{
+ view->setModel(nullptr);
+
+ if(path.endsWith(".xbel")) {
+ QFile f(path);
+ if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ BookmarkFormat<XbelFormat>(&f) >> model;
+ f.close();
+ }
+
+ } else if(path.endsWith(".json")) {
+ QFile f(path);
+ if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ BookmarkFormat<FirefoxJsonFormat>(&f) >> model;
+ f.close();
+ }
+ }
+
+ view->setModel(model);
+ expandChildren(view, model, QModelIndex());
+}
+
BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
: QWidget(parent)
, ui(new Ui::BookmarksDialog)
@@ -44,14 +68,7 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
model = new BookmarkModel(this);
m_bookmarksPath = path;
- QFile bookmarksFile(m_bookmarksPath);
- if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
- BookmarkFormat<XbelFormat>(&bookmarksFile) >> model;
- }
- model->resetModified();
-
- ui->treeView->setModel(model);
- expandChildren(ui->treeView, model, QModelIndex());
+ loadModel(path, model, ui->treeView);
// item activated
connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
@@ -100,6 +117,14 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent)
const QModelIndex idx = ui->treeView->currentIndex();
model->removeRow(idx.row(), idx.parent());
});
+
+ // import button
+ connect(ui->import_toolButton, &QToolButton::clicked, this, [this]() {
+ const auto path = QFileDialog::getOpenFileName(this, tr("Open bookmarks file"), QDir::homePath(), tr("Firefox bookmarks backup (*.json)"));
+ if(!path.isEmpty()) {
+ loadModel(path, model, ui->treeView);
+ }
+ });
}
BookmarksWidget::~BookmarksWidget()