aboutsummaryrefslogtreecommitdiff
path: root/lib/bookmarks
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-03 16:39:32 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-03 16:39:32 +0100
commit85d9f14aa8bac16ab341662c770b64a15c21628b (patch)
tree422db18e798646cd4bc98abf342f25c09c0bdf1d /lib/bookmarks
parentDownload items' width should no longer exceed the list width (diff)
downloadsmolbote-85d9f14aa8bac16ab341662c770b64a15c21628b.tar.xz
Changed from qbs to cmake
Diffstat (limited to 'lib/bookmarks')
-rw-r--r--lib/bookmarks/CMakeLists.txt10
-rw-r--r--lib/bookmarks/bookmarksform.ui104
-rw-r--r--lib/bookmarks/bookmarkswidget.cpp112
-rw-r--r--lib/bookmarks/bookmarkswidget.h49
-rw-r--r--lib/bookmarks/xbel.cpp193
-rw-r--r--lib/bookmarks/xbel.h45
6 files changed, 513 insertions, 0 deletions
diff --git a/lib/bookmarks/CMakeLists.txt b/lib/bookmarks/CMakeLists.txt
new file mode 100644
index 0000000..8fb7f45
--- /dev/null
+++ b/lib/bookmarks/CMakeLists.txt
@@ -0,0 +1,10 @@
+cmake_minimum_required(VERSION 3.1.0)
+
+add_library(bookmarks
+ bookmarksform.ui
+ bookmarkswidget.cpp
+ bookmarkswidget.h
+ xbel.cpp
+ xbel.h)
+
+target_link_libraries(bookmarks Qt5::Widgets) \ No newline at end of file
diff --git a/lib/bookmarks/bookmarksform.ui b/lib/bookmarks/bookmarksform.ui
new file mode 100644
index 0000000..2df0c4b
--- /dev/null
+++ b/lib/bookmarks/bookmarksform.ui
@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>BookmarksDialog</class>
+ <widget class="QWidget" name="BookmarksDialog">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>420</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="minimumSize">
+ <size>
+ <width>420</width>
+ <height>600</height>
+ </size>
+ </property>
+ <property name="windowTitle">
+ <string>Bookmarks</string>
+ </property>
+ <layout class="QVBoxLayout" name="verticalLayout">
+ <item>
+ <layout class="QHBoxLayout" name="horizontalLayout_2">
+ <item>
+ <widget class="QToolButton" name="addFolder_toolButton">
+ <property name="text">
+ <string>...</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QToolButton" name="addBookmark_toolButton">
+ <property name="text">
+ <string>...</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QToolButton" name="addSeparator_toolButton">
+ <property name="text">
+ <string>...</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QToolButton" name="deleteItem_toolButton">
+ <property name="text">
+ <string>...</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ <item>
+ <widget class="QTreeWidget" name="treeWidget">
+ <property name="dragEnabled">
+ <bool>true</bool>
+ </property>
+ <property name="dragDropMode">
+ <enum>QAbstractItemView::InternalMove</enum>
+ </property>
+ <property name="defaultDropAction">
+ <enum>Qt::MoveAction</enum>
+ </property>
+ <column>
+ <property name="text">
+ <string notr="true">Title</string>
+ </property>
+ </column>
+ <column>
+ <property name="text">
+ <string>href</string>
+ </property>
+ </column>
+ </widget>
+ </item>
+ <item>
+ <widget class="QGroupBox" name="groupBox">
+ <property name="title">
+ <string>Details</string>
+ </property>
+ <layout class="QFormLayout" name="formLayout">
+ <item row="1" column="0">
+ <widget class="QLabel" name="href_label">
+ <property name="text">
+ <string>href</string>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="1">
+ <widget class="QLineEdit" name="href_lineEdit"/>
+ </item>
+ <item row="0" column="1">
+ <widget class="QLineEdit" name="title_lineEdit"/>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp
new file mode 100644
index 0000000..cb191c7
--- /dev/null
+++ b/lib/bookmarks/bookmarkswidget.cpp
@@ -0,0 +1,112 @@
+/*
+ * 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: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "bookmarkswidget.h"
+#include "ui_bookmarksform.h"
+#include <QTreeWidget>
+#include <QUrl>
+
+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);
+ setWindowTitle(tr("Bookmarks"));
+
+ ui->setupUi(this);
+ ui->treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
+
+ QStyle *style = ui->treeWidget->style();
+ 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));
+
+ connect(ui->treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(showItemDetails()));
+ connect(ui->treeWidget, SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(openItem(QTreeWidgetItem*,int)));
+
+ ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete);
+
+ m_path = path;
+ xbel = new Xbel(ui->treeWidget);
+ qDebug("Reading bookmarks [%s] %s", qUtf8Printable(m_path), xbel->read(m_path) ? "ok" : "failed");
+
+ connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [&]() {
+ xbel->addFolder(ui->treeWidget->currentItem());
+ });
+ connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [&]() {
+ xbel->addBookmark(ui->treeWidget->currentItem());
+ });
+ connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [&]() {
+ delete ui->treeWidget->currentItem();
+ });
+
+ // editing bookmarks
+ connect(ui->title_lineEdit, &QLineEdit::returnPressed, this, [&]() {
+ ui->treeWidget->currentItem()->setText(0, ui->title_lineEdit->text());
+ });
+ connect(ui->href_lineEdit, &QLineEdit::returnPressed, this, [&]() {
+ ui->treeWidget->currentItem()->setText(1, ui->href_lineEdit->text());
+ });
+}
+
+BookmarksWidget::~BookmarksWidget()
+{
+ delete xbel;
+ delete ui;
+}
+
+void BookmarksWidget::save()
+{
+ qDebug("Writing bookmarks [%s] %s", qUtf8Printable(m_path), xbel->write(m_path) ? "ok" : "failed");
+}
+
+QStringList BookmarksWidget::bookmarksFor(const QString &term)
+{
+ QStringList ret;
+ for(int i = 0; i < ui->treeWidget->topLevelItemCount(); ++i) {
+ ret += searchItem(ui->treeWidget->topLevelItem(i), term);
+ }
+ return ret;
+}
+
+QAbstractItemModel *BookmarksWidget::model() const
+{
+ return ui->treeWidget->model();
+}
+
+QStringList BookmarksWidget::searchItem(QTreeWidgetItem *item, const QString &term)
+{
+ if(item->text(1).contains(term)) {
+ return { item->text(1) };
+ }
+
+ QStringList ret;
+ for(int i = 0; i < item->childCount(); ++i) {
+ ret += searchItem(item->child(i), term);
+ }
+ return ret;
+}
+
+void BookmarksWidget::openItem(QTreeWidgetItem *item, int column)
+{
+ Q_UNUSED(column)
+ emit openUrl(QUrl::fromUserInput(item->text(1)));
+}
+
+void BookmarksWidget::showItemDetails()
+{
+ QTreeWidgetItem *item = ui->treeWidget->currentItem();
+ if(!item) {
+ ui->title_lineEdit->setText("");
+ ui->href_label->setText("");
+ return;
+ }
+ ui->title_lineEdit->setText(item->text(0));
+ ui->href_lineEdit->setText(item->text(1));
+}
diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h
new file mode 100644
index 0000000..aa188b2
--- /dev/null
+++ b/lib/bookmarks/bookmarkswidget.h
@@ -0,0 +1,49 @@
+/*
+ * 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: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef BOOKMARKSDIALOG_H
+#define BOOKMARKSDIALOG_H
+
+#include <QWidget>
+#include "xbel.h"
+#include <QAbstractItemModel>
+
+namespace Ui {
+class BookmarksDialog;
+}
+
+class BookmarksWidget : public QWidget
+{
+ Q_OBJECT
+
+public:
+ explicit BookmarksWidget(const QString &path, QWidget *parent = 0);
+ ~BookmarksWidget();
+
+ void save();
+
+signals:
+ void openUrl(const QUrl &url);
+
+public slots:
+ QStringList bookmarksFor(const QString &term);
+ QAbstractItemModel *model() const;
+
+private slots:
+ void openItem(QTreeWidgetItem *item, int column);
+ void showItemDetails();
+
+private:
+ QStringList searchItem(QTreeWidgetItem *item, const QString &term);
+
+ QString m_path;
+ Ui::BookmarksDialog *ui;
+ Xbel *xbel;
+};
+
+#endif // BOOKMARKSDIALOG_H
diff --git a/lib/bookmarks/xbel.cpp b/lib/bookmarks/xbel.cpp
new file mode 100644
index 0000000..523ed99
--- /dev/null
+++ b/lib/bookmarks/xbel.cpp
@@ -0,0 +1,193 @@
+/*
+ * 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: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "xbel.h"
+#include <QFile>
+
+#include <QTreeWidgetItem>
+
+Xbel::Xbel(QTreeWidget *widget)
+{
+ treeWidget = widget;
+
+ QStyle *style = treeWidget->style();
+ folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirOpenIcon), QIcon::Normal, QIcon::On);
+ folderIcon.addPixmap(style->standardPixmap(QStyle::SP_DirClosedIcon), QIcon::Normal, QIcon::Off);
+ bookmarkIcon.addPixmap(style->standardPixmap(QStyle::SP_FileIcon));
+}
+
+bool Xbel::read(const QString &xbel)
+{
+ // no file specified
+ if(xbel.isEmpty()) {
+ return false;
+ }
+
+ QFile file(xbel);
+ if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ // file cannot be opened
+ return false;
+ }
+
+ xmlReader.setDevice(&file);
+
+ if(xmlReader.readNextStartElement()) {
+ if(xmlReader.name() == "xbel" && xmlReader.attributes().value("version") == "1.0") {
+ qDebug("valid xbel");
+ readChildElements(0);
+ } else {
+ qDebug("invalid xbel");
+ return false;
+ }
+
+ }
+ return true;
+}
+
+bool Xbel::write(const QString &xbel)
+{
+ // no file specified
+ if(xbel.isEmpty()) {
+ return false;
+ }
+
+ QFile file(xbel);
+ if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ // file cannot be opened
+ qDebug("Cannot open xbel: %s", qUtf8Printable(xbel));
+ return false;
+ }
+
+ xmlWriter.setAutoFormatting(true);
+ xmlWriter.setDevice(&file);
+
+ xmlWriter.writeStartDocument();
+ xmlWriter.writeDTD("<!DOCTYPE xbel>");
+ xmlWriter.writeStartElement("xbel");
+ xmlWriter.writeAttribute("version", "1.0");
+
+ for(int i=0; i<treeWidget->topLevelItemCount(); i++) {
+ writeItem(treeWidget->topLevelItem(i));
+ }
+
+ xmlWriter.writeEndDocument();
+
+ file.flush();
+ file.close();
+ return true;
+}
+
+void Xbel::readChildElements(QTreeWidgetItem *parentItem)
+{
+ while(xmlReader.readNextStartElement()) {
+ if(xmlReader.name() == "title") {
+ readTitle(parentItem);
+ } else if(xmlReader.name() == "folder") {
+ QTreeWidgetItem *item = addFolder(parentItem);
+ item->setText(1, xmlReader.attributes().value("href").toString());
+ readChildElements(item);
+ } else if(xmlReader.name() == "bookmark") {
+ QTreeWidgetItem *item = addBookmark(parentItem);
+ item->setText(1, xmlReader.attributes().value("href").toString());
+ readChildElements(item);
+ } else if(xmlReader.name() == "separator") {
+ addSeparator(parentItem);
+ xmlReader.skipCurrentElement();
+ } else {
+ xmlReader.skipCurrentElement();
+ }
+ }
+}
+
+void Xbel::readTitle(QTreeWidgetItem *item)
+{
+ item->setText(0, xmlReader.readElementText());
+}
+
+QTreeWidgetItem *Xbel::addFolder(QTreeWidgetItem *parentItem)
+{
+ QTreeWidgetItem *folderItem = createChildItem(parentFolder(parentItem), "folder");
+ //folderItem->setExpanded(xmlReader.attributes().value("folded") != "no");
+ treeWidget->setItemExpanded(folderItem, xmlReader.attributes().value("folded") != "yes");
+ folderItem->setFlags(folderItem->flags() | Qt::ItemIsEditable);
+ folderItem->setIcon(0, folderIcon);
+
+ return folderItem;
+}
+
+QTreeWidgetItem *Xbel::addBookmark(QTreeWidgetItem *parentItem)
+{
+ QTreeWidgetItem *bookmarkItem = createChildItem(parentFolder(parentItem), "bookmark");
+ bookmarkItem->setFlags((bookmarkItem->flags() | Qt::ItemIsEditable) & ~Qt::ItemIsDropEnabled);
+ bookmarkItem->setIcon(0, bookmarkIcon);
+ bookmarkItem->setText(0, "Unknown Title");
+ bookmarkItem->setText(1, "Unknown Address");
+
+ return bookmarkItem;
+}
+
+void Xbel::addSeparator(QTreeWidgetItem *parentItem)
+{
+ QTreeWidgetItem *separatorItem = createChildItem(parentFolder(parentItem), "separator");
+ separatorItem->setFlags(separatorItem->flags() & ~Qt::ItemIsDropEnabled);
+ separatorItem->setText(0, "-----");
+}
+
+QTreeWidgetItem *Xbel::parentFolder(QTreeWidgetItem *item)
+{
+ QTreeWidgetItem *parentItem = item;
+
+ if(parentItem) {
+ while(parentItem->data(0, Qt::UserRole) != "folder") {
+ parentItem = parentItem->parent();
+ if(parentItem == 0) {
+ break;
+ }
+ }
+ }
+
+ return parentItem;
+}
+
+QTreeWidgetItem *Xbel::createChildItem(QTreeWidgetItem *item, const QString &type)
+{
+ QTreeWidgetItem *childItem;
+ if(item) {
+ childItem = new QTreeWidgetItem(item);
+ } else {
+ childItem = new QTreeWidgetItem(treeWidget);
+ }
+ childItem->setData(0, Qt::UserRole, type);
+ return childItem;
+}
+
+void Xbel::writeItem(QTreeWidgetItem *item)
+{
+ QString tagName = item->data(0, Qt::UserRole).toString();
+ if (tagName == "folder") {
+ xmlWriter.writeStartElement(tagName);
+ if(!item->text(1).isEmpty()) {
+ xmlWriter.writeAttribute("href", item->text(1));
+ }
+ xmlWriter.writeAttribute("folded", treeWidget->isItemExpanded(item) ? "no" : "yes");
+ xmlWriter.writeTextElement("title", item->text(0));
+ for (int i = 0; i < item->childCount(); ++i) {
+ writeItem(item->child(i));
+ }
+ xmlWriter.writeEndElement();
+ } else if (tagName == "bookmark") {
+ xmlWriter.writeStartElement(tagName);
+ if (!item->text(1).isEmpty()) {
+ xmlWriter.writeAttribute("href", item->text(1));
+ }
+ xmlWriter.writeTextElement("title", item->text(0));
+ xmlWriter.writeEndElement();
+ } else if (tagName == "separator") {
+ xmlWriter.writeEmptyElement(tagName);
+ }
+}
diff --git a/lib/bookmarks/xbel.h b/lib/bookmarks/xbel.h
new file mode 100644
index 0000000..ed0a256
--- /dev/null
+++ b/lib/bookmarks/xbel.h
@@ -0,0 +1,45 @@
+/*
+ * 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: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef XBELREADER_H
+#define XBELREADER_H
+
+#include <QXmlStreamReader>
+#include <QIcon>
+
+class QTreeWidget;
+class QTreeWidgetItem;
+class Xbel
+{
+public:
+ explicit Xbel(QTreeWidget *widget);
+ bool read(const QString &xbel);
+ bool write(const QString &xbel);
+
+ QTreeWidgetItem *addFolder(QTreeWidgetItem *parentItem);
+ QTreeWidgetItem *addBookmark(QTreeWidgetItem *parentItem);
+ void addSeparator(QTreeWidgetItem *parentItem);
+
+private:
+ void readChildElements(QTreeWidgetItem *parentItem);
+ void readTitle(QTreeWidgetItem *item);
+
+ QTreeWidgetItem *parentFolder(QTreeWidgetItem *item);
+ QTreeWidgetItem *createChildItem(QTreeWidgetItem *item, const QString &type);
+
+ void writeItem(QTreeWidgetItem *item);
+
+ QIcon folderIcon;
+ QIcon bookmarkIcon;
+
+ QTreeWidget *treeWidget;
+ QXmlStreamReader xmlReader;
+ QXmlStreamWriter xmlWriter;
+};
+
+#endif // XBELREADER_H