aboutsummaryrefslogtreecommitdiff
path: root/src/bookmarks/bookmarkswidget.cpp
diff options
context:
space:
mode:
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);
+}