From d75794508e1c202d28bdf77c25afb85d80d42554 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 17 Jan 2020 11:46:20 +0200 Subject: Add Import option to BookmarksWidget --- lib/bookmarks/bookmarkformat.h | 4 ---- src/bookmarks/bookmarksform.ui | 13 ++++++++++--- src/bookmarks/bookmarkswidget.cpp | 41 +++++++++++++++++++++++++++++++-------- 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 @@ -16,6 +16,13 @@ + + + + Import + + + @@ -66,12 +73,12 @@ Qt::MoveAction - - 200 - 50 + + 200 + 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 #include +#include 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(&f) >> model; + f.close(); + } + + } else if(path.endsWith(".json")) { + QFile f(path); + if(f.open(QIODevice::ReadOnly | QIODevice::Text)) { + BookmarkFormat(&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(&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() -- cgit v1.2.1