diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-17 11:46:20 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-17 11:46:20 +0200 |
commit | d75794508e1c202d28bdf77c25afb85d80d42554 (patch) | |
tree | 79fd7363b0f5d6a999dfbeee85862a49b8b2001c /src/bookmarks | |
parent | BookmarkItem::DateAdded and LastModified fields (diff) | |
download | smolbote-d75794508e1c202d28bdf77c25afb85d80d42554.tar.xz |
Add Import option to BookmarksWidget
Diffstat (limited to 'src/bookmarks')
-rw-r--r-- | src/bookmarks/bookmarksform.ui | 13 | ||||
-rw-r--r-- | src/bookmarks/bookmarkswidget.cpp | 41 |
2 files changed, 43 insertions, 11 deletions
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() |