aboutsummaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkswidget.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 13:39:59 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-01-17 13:39:59 +0200
commit4c73f86fc6cbdaaee90f9f8e40af36c1ab84c374 (patch)
tree63b42986ffccb3c0fdb3b978f3c78b56ca5a03a8 /src/bookmarks/bookmarkswidget.cpp
parentCreate default profile when the one set doesn't exist (diff)
parentParse Tags and Descriptions in FFJson (diff)
downloadsmolbote-4c73f86fc6cbdaaee90f9f8e40af36c1ab84c374.tar.xz
Merge branch 'firefox-bookmarks-json-importer'
Diffstat (limited to 'src/bookmarks/bookmarkswidget.cpp')
-rw-r--r--src/bookmarks/bookmarkswidget.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/bookmarks/bookmarkswidget.cpp b/src/bookmarks/bookmarkswidget.cpp
new file mode 100644
index 0000000..bd2d607
--- /dev/null
+++ b/src/bookmarks/bookmarkswidget.cpp
@@ -0,0 +1,163 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "bookmarkswidget.h"
+#include "editbookmarkdialog.h"
+#include "ui_bookmarksform.h"
+#include "bookmarkformat.h"
+#include <QTreeView>
+#include <QUrl>
+#include <QFileDialog>
+
+inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIndex &rootIndex)
+{
+ for(int i = 0; i < model->rowCount(rootIndex); ++i) {
+ QModelIndex idx = model->index(i, 0, rootIndex);
+ if(model->isItemExpanded(idx))
+ view->expand(idx);
+
+ // check if index has children
+ if(model->rowCount(idx) > 0)
+ expandChildren(view, model, idx);
+ }
+}
+
+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)
+{
+ // make sure this dialog does not get deleted on close
+ setAttribute(Qt::WA_DeleteOnClose, false);
+
+ ui->setupUi(this);
+ ui->bookmark_groupBox->setVisible(false);
+ ui->folder_groupBox->setVisible(false);
+
+ ui->addFolder_toolButton->setIcon(style()->standardPixmap(QStyle::SP_DirIcon));
+ ui->addBookmark_toolButton->setIcon(style()->standardPixmap(QStyle::SP_FileIcon));
+ ui->deleteItem_toolButton->setIcon(style()->standardPixmap(QStyle::SP_TrashIcon));
+ ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete);
+
+ model = new BookmarkModel(this);
+ m_bookmarksPath = path;
+ loadModel(path, model, ui->treeView);
+
+ // item activated
+ connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) {
+ if(index.column() == 1)
+ emit openUrl(index.data(Qt::DisplayRole).toUrl());
+ else
+ editBookmark(index);
+ });
+
+ connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) {
+ model->setItemExpanded(index, true);
+ });
+ connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) {
+ model->setItemExpanded(index, false);
+ });
+
+ ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
+ connect(ui->treeView, &QTreeView::customContextMenuRequested, this, [this](const QPoint &pos) {
+ const QModelIndex idx = ui->treeView->indexAt(pos);
+ if(idx.isValid()) {
+ const QUrl url = model->data(idx, 1, Qt::DisplayRole).toUrl();
+ const QPoint _pos = ui->treeView->viewport()->mapToGlobal(pos);
+
+ emit showContextMenu(url, _pos);
+ }
+ });
+
+ // addBookmark
+ connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() {
+ const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
+ const QModelIndex childIdx = model->appendBookmark(tr("Title"), QString(), idx);
+ ui->treeView->setCurrentIndex(childIdx);
+ editBookmark(childIdx);
+ });
+
+ // addFolder
+ connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() {
+ const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex());
+ const QModelIndex childIdx = model->appendFolder(tr("Title"), idx);
+ ui->treeView->setCurrentIndex(childIdx);
+ editBookmark(childIdx);
+ });
+
+ // deleteItem
+ connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() {
+ 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()
+{
+ delete ui;
+}
+
+void BookmarksWidget::editBookmark(const QModelIndex &index)
+{
+ auto *dlg = new EditBookmarkDialog(model, index, this);
+ dlg->exec();
+}
+
+void BookmarksWidget::save()
+{
+ if(!model->isModified())
+ return;
+
+ QFile bookmarksFile(m_bookmarksPath);
+ if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ BookmarkFormat<XbelFormat> f(&bookmarksFile);
+ model >> f;
+ bookmarksFile.flush();
+ }
+}
+
+void BookmarksWidget::addBookmark(const QString &title, const QString &url)
+{
+ model->appendBookmark(title, url, QModelIndex());
+}
+
+void BookmarksWidget::search(const QString &term, const std::function<void(QStringList &)> &callback) const
+{
+ QStringList ret = model->search(term);
+ callback(ret);
+}