From 02980eb506e2b624d539a2dfb29bbe1834dd07a0 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 4 Sep 2017 16:20:09 +0200 Subject: Split off Bookmarks into static lib --- src/browser.cpp | 2 +- src/browser.h | 5 +- src/forms/bookmarksform.ui | 104 ------------------ src/forms/bookmarkswidget.cpp | 108 ------------------- src/forms/bookmarkswidget.h | 61 ----------- src/forms/profilesdialog.cpp | 2 + src/lib/bookmarks/bookmarks.qbs | 26 +++++ src/lib/bookmarks/bookmarksform.ui | 104 ++++++++++++++++++ src/lib/bookmarks/bookmarkswidget.cpp | 96 +++++++++++++++++ src/lib/bookmarks/bookmarkswidget.h | 59 ++++++++++ src/lib/bookmarks/xbel.cpp | 198 ++++++++++++++++++++++++++++++++++ src/lib/bookmarks/xbel.h | 57 ++++++++++ src/mainwindow.cpp | 6 ++ src/webengine/webengineprofile.cpp | 1 + src/widgets/mainwindowmenubar.cpp | 2 + src/widgets/webviewtabbar.cpp | 2 + src/xbel.cpp | 198 ---------------------------------- src/xbel.h | 57 ---------- 18 files changed, 558 insertions(+), 530 deletions(-) delete mode 100644 src/forms/bookmarksform.ui delete mode 100644 src/forms/bookmarkswidget.cpp delete mode 100644 src/forms/bookmarkswidget.h create mode 100644 src/lib/bookmarks/bookmarks.qbs create mode 100644 src/lib/bookmarks/bookmarksform.ui create mode 100644 src/lib/bookmarks/bookmarkswidget.cpp create mode 100644 src/lib/bookmarks/bookmarkswidget.h create mode 100644 src/lib/bookmarks/xbel.cpp create mode 100644 src/lib/bookmarks/xbel.h delete mode 100644 src/xbel.cpp delete mode 100644 src/xbel.h (limited to 'src') diff --git a/src/browser.cpp b/src/browser.cpp index 20a91dd..8db1980 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -139,7 +139,7 @@ QNetworkAccessManager *Browser::network() BookmarksWidget *Browser::bookmarks() { if(!m_bookmarksManager) { - m_bookmarksManager = new BookmarksWidget(); + m_bookmarksManager = new BookmarksWidget(settings()->value("bookmarks.path").toString()); } return m_bookmarksManager; } diff --git a/src/browser.h b/src/browser.h index 75102c6..7a5d300 100644 --- a/src/browser.h +++ b/src/browser.h @@ -23,12 +23,15 @@ #include "singleapplication.h" #include -#include "forms/bookmarkswidget.h" +#include #include "forms/downloadswidget.h" #include "settings.h" #include #include "webengine/webengineprofile.h" +#include "filter/blockermanager.h" +#include "webengine/urlinterceptor.h" + #ifdef qApp #undef qApp #define qApp Browser::instance() diff --git a/src/forms/bookmarksform.ui b/src/forms/bookmarksform.ui deleted file mode 100644 index 2df0c4b..0000000 --- a/src/forms/bookmarksform.ui +++ /dev/null @@ -1,104 +0,0 @@ - - - BookmarksDialog - - - - 0 - 0 - 420 - 600 - - - - - 420 - 600 - - - - Bookmarks - - - - - - - - ... - - - - - - - ... - - - - - - - ... - - - - - - - ... - - - - - - - - - true - - - QAbstractItemView::InternalMove - - - Qt::MoveAction - - - - Title - - - - - href - - - - - - - - Details - - - - - - href - - - - - - - - - - - - - - - - - diff --git a/src/forms/bookmarkswidget.cpp b/src/forms/bookmarkswidget.cpp deleted file mode 100644 index 9a43224..0000000 --- a/src/forms/bookmarkswidget.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "bookmarkswidget.h" -#include "ui_bookmarksform.h" -#include "browser.h" -#include - -BookmarksWidget::BookmarksWidget(QWidget *parent) : - DockingWidget(tr("Bookmarks"), parent), - ui(new Ui::BookmarksDialog) -{ - // make sure this dialog does not get deleted on close - setAttribute(Qt::WA_DeleteOnClose, false); - - 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->addSeparator_toolButton->setText("---"); - 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); - connect(ui->deleteItem_toolButton, SIGNAL(clicked(bool)), this, SLOT(deleteCurrentItem())); - - xbel = new Xbel(ui->treeWidget); - xbel->read(sSettings->value("bookmarks.path").toString()); - - 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->addSeparator_toolButton, &QToolButton::clicked, [this]() { - xbel->addSeparator(ui->treeWidget->currentItem()); - }); -} - -BookmarksWidget::~BookmarksWidget() -{ - qDebug("Destroying BookmarksManager"); - - if(ui->treeWidget->topLevelItemCount() > 0) { - xbel->write(sSettings->value("bookmarks.path").toString()); - } - - delete xbel; - delete ui; -} - -void BookmarksWidget::show() -{ - // show() gets called by a QAction in MainWindow - window = qobject_cast(sender()->parent()); - - DockingWidget::show(); -} - -void BookmarksWidget::deleteCurrentItem() -{ - delete ui->treeWidget->currentItem(); -} - -void BookmarksWidget::openItem(QTreeWidgetItem *item, int column) -{ - Q_UNUSED(column) - - if(window) { - window->newTab(QUrl::fromUserInput(item->text(1))); - } else { - qWarning("Trying to open a link without a MainWindow set"); - } -} - -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/src/forms/bookmarkswidget.h b/src/forms/bookmarkswidget.h deleted file mode 100644 index ba0b361..0000000 --- a/src/forms/bookmarkswidget.h +++ /dev/null @@ -1,61 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef BOOKMARKSDIALOG_H -#define BOOKMARKSDIALOG_H - -#include "widgets/dockingwidget.h" -#include -#include "xbel.h" -#include "mainwindow.h" - -namespace Ui { -class BookmarksDialog; -} - -class BookmarksWidget : public DockingWidget -{ - Q_OBJECT - -public: - explicit BookmarksWidget(QWidget *parent = 0); - ~BookmarksWidget(); - -signals: - void openUrl(const QUrl &url); - -public slots: - void show(); - // void addFolder(const QString &title); - // void addBookmark(const QString &title, const QString &href); - // void addSeparator(); - void deleteCurrentItem(); - -private slots: - void openItem(QTreeWidgetItem *item, int column); - void showItemDetails(); - -private: - MainWindow *window; - Ui::BookmarksDialog *ui; - Xbel *xbel; -}; - -#endif // BOOKMARKSDIALOG_H diff --git a/src/forms/profilesdialog.cpp b/src/forms/profilesdialog.cpp index 557d865..00e4920 100644 --- a/src/forms/profilesdialog.cpp +++ b/src/forms/profilesdialog.cpp @@ -28,6 +28,8 @@ #include #include +#include "mainwindow.h" + ProfilesDialog::ProfilesDialog(MainWindow *window, QWidget *parent) : QDialog(parent), ui(new Ui::ProfilesDialog) diff --git a/src/lib/bookmarks/bookmarks.qbs b/src/lib/bookmarks/bookmarks.qbs new file mode 100644 index 0000000..22971a4 --- /dev/null +++ b/src/lib/bookmarks/bookmarks.qbs @@ -0,0 +1,26 @@ +import qbs 1.0 + +Project { + name: "bookmarks" + + StaticLibrary { + id: bookmarks + name: "bookmarks" + + cpp.includePaths: ['../..'] + + Depends { + name: "Qt" + versionAtLeast: "5.9.0" + submodules: ["core", "widgets"] + } + + files: [ + "bookmarksform.ui", + "bookmarkswidget.cpp", + "bookmarkswidget.h", + "xbel.cpp", + "xbel.h", + ] + } +} diff --git a/src/lib/bookmarks/bookmarksform.ui b/src/lib/bookmarks/bookmarksform.ui new file mode 100644 index 0000000..2df0c4b --- /dev/null +++ b/src/lib/bookmarks/bookmarksform.ui @@ -0,0 +1,104 @@ + + + BookmarksDialog + + + + 0 + 0 + 420 + 600 + + + + + 420 + 600 + + + + Bookmarks + + + + + + + + ... + + + + + + + ... + + + + + + + ... + + + + + + + ... + + + + + + + + + true + + + QAbstractItemView::InternalMove + + + Qt::MoveAction + + + + Title + + + + + href + + + + + + + + Details + + + + + + href + + + + + + + + + + + + + + + + + diff --git a/src/lib/bookmarks/bookmarkswidget.cpp b/src/lib/bookmarks/bookmarkswidget.cpp new file mode 100644 index 0000000..38716ce --- /dev/null +++ b/src/lib/bookmarks/bookmarkswidget.cpp @@ -0,0 +1,96 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "bookmarkswidget.h" +#include "ui_bookmarksform.h" +#include +#include + +BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) : + DockingWidget(tr("Bookmarks"), parent), + ui(new Ui::BookmarksDialog) +{ + // make sure this dialog does not get deleted on close + setAttribute(Qt::WA_DeleteOnClose, false); + + 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->addSeparator_toolButton->setText("---"); + 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); + connect(ui->deleteItem_toolButton, SIGNAL(clicked(bool)), this, SLOT(deleteCurrentItem())); + + m_path = path; + xbel = new Xbel(ui->treeWidget); + xbel->read(m_path); + + 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->addSeparator_toolButton, &QToolButton::clicked, [this]() { + xbel->addSeparator(ui->treeWidget->currentItem()); + }); +} + +BookmarksWidget::~BookmarksWidget() +{ + qDebug("Destroying BookmarksManager"); + + if(ui->treeWidget->topLevelItemCount() > 0) { + xbel->write(m_path); + } + + delete xbel; + delete ui; +} + +void BookmarksWidget::deleteCurrentItem() +{ + delete ui->treeWidget->currentItem(); +} + +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/src/lib/bookmarks/bookmarkswidget.h b/src/lib/bookmarks/bookmarkswidget.h new file mode 100644 index 0000000..a31cfea --- /dev/null +++ b/src/lib/bookmarks/bookmarkswidget.h @@ -0,0 +1,59 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef BOOKMARKSDIALOG_H +#define BOOKMARKSDIALOG_H + +#include "widgets/dockingwidget.h" +#include +#include "xbel.h" + +namespace Ui { +class BookmarksDialog; +} + +class BookmarksWidget : public DockingWidget +{ + Q_OBJECT + +public: + explicit BookmarksWidget(const QString &path, QWidget *parent = 0); + ~BookmarksWidget(); + +signals: + void openUrl(const QUrl &url); + +public slots: + // void addFolder(const QString &title); + // void addBookmark(const QString &title, const QString &href); + // void addSeparator(); + void deleteCurrentItem(); + +private slots: + void openItem(QTreeWidgetItem *item, int column); + void showItemDetails(); + +private: + QString m_path; + Ui::BookmarksDialog *ui; + Xbel *xbel; +}; + +#endif // BOOKMARKSDIALOG_H diff --git a/src/lib/bookmarks/xbel.cpp b/src/lib/bookmarks/xbel.cpp new file mode 100644 index 0000000..7ba0cb4 --- /dev/null +++ b/src/lib/bookmarks/xbel.cpp @@ -0,0 +1,198 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "xbel.h" +#include + +#include + +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 + qDebug("Cannot open xbel: %s", qUtf8Printable(xbel)); + 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(""); + xmlWriter.writeStartElement("xbel"); + xmlWriter.writeAttribute("version", "1.0"); + + for(int i=0; itopLevelItemCount(); i++) { + writeItem(treeWidget->topLevelItem(i)); + } + + xmlWriter.writeEndDocument(); + return true; +} + +void Xbel::readChildElements(QTreeWidgetItem *parentItem) +{ + while(xmlReader.readNextStartElement()) { + if(xmlReader.name() == "title") { + readTitle(parentItem); + } else if(xmlReader.name() == "folder") { + readChildElements(addFolder(parentItem)); + } 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); + 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/src/lib/bookmarks/xbel.h b/src/lib/bookmarks/xbel.h new file mode 100644 index 0000000..0ff98fc --- /dev/null +++ b/src/lib/bookmarks/xbel.h @@ -0,0 +1,57 @@ +/******************************************************************************* + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef XBELREADER_H +#define XBELREADER_H + +#include +#include + +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 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index dd16172..be19021 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -108,6 +108,12 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : }); connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*))); + connect(qApp->bookmarks(), &BookmarksWidget::openUrl, [this](const QUrl &url) { + if(this->isActiveWindow()) { + this->newTab(url); + } + }); + // Load profile tabBar->setProfile(qApp->profile(sSettings->value("browser.profile.default").toString())); diff --git a/src/webengine/webengineprofile.cpp b/src/webengine/webengineprofile.cpp index 583be3e..9745949 100644 --- a/src/webengine/webengineprofile.cpp +++ b/src/webengine/webengineprofile.cpp @@ -22,6 +22,7 @@ #include "browser.h" #include #include +#include WebEngineProfile::WebEngineProfile(QObject *parent) : QWebEngineProfile(parent) diff --git a/src/widgets/mainwindowmenubar.cpp b/src/widgets/mainwindowmenubar.cpp index 8dd7fe6..e69a207 100644 --- a/src/widgets/mainwindowmenubar.cpp +++ b/src/widgets/mainwindowmenubar.cpp @@ -26,6 +26,8 @@ #include "interfaces.h" +#include "mainwindow.h" + MainWindowMenuBar::MainWindowMenuBar(MainWindow *parent) : QMenuBar(parent) { diff --git a/src/widgets/webviewtabbar.cpp b/src/widgets/webviewtabbar.cpp index eb2e463..7ec7f0b 100644 --- a/src/widgets/webviewtabbar.cpp +++ b/src/widgets/webviewtabbar.cpp @@ -21,6 +21,8 @@ #include "webviewtabbar.h" #include "browser.h" #include +#include +#include WebViewTabBar::WebViewTabBar(WebEngineProfile *profile, QWidget *parent) : QTabBar(parent) diff --git a/src/xbel.cpp b/src/xbel.cpp deleted file mode 100644 index 7ba0cb4..0000000 --- a/src/xbel.cpp +++ /dev/null @@ -1,198 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#include "xbel.h" -#include - -#include - -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 - qDebug("Cannot open xbel: %s", qUtf8Printable(xbel)); - 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(""); - xmlWriter.writeStartElement("xbel"); - xmlWriter.writeAttribute("version", "1.0"); - - for(int i=0; itopLevelItemCount(); i++) { - writeItem(treeWidget->topLevelItem(i)); - } - - xmlWriter.writeEndDocument(); - return true; -} - -void Xbel::readChildElements(QTreeWidgetItem *parentItem) -{ - while(xmlReader.readNextStartElement()) { - if(xmlReader.name() == "title") { - readTitle(parentItem); - } else if(xmlReader.name() == "folder") { - readChildElements(addFolder(parentItem)); - } 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); - 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/src/xbel.h b/src/xbel.h deleted file mode 100644 index 0ff98fc..0000000 --- a/src/xbel.h +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* - ** - ** smolbote: yet another qute browser - ** Copyright (C) 2017 Xian Nox - ** - ** This program is free software: you can redistribute it and/or modify - ** it under the terms of the GNU General Public License as published by - ** the Free Software Foundation, either version 3 of the License, or - ** (at your option) any later version. - ** - ** This program is distributed in the hope that it will be useful, - ** but WITHOUT ANY WARRANTY; without even the implied warranty of - ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - ** GNU General Public License for more details. - ** - ** You should have received a copy of the GNU General Public License - ** along with this program. If not, see . - ** - ******************************************************************************/ - -#ifndef XBELREADER_H -#define XBELREADER_H - -#include -#include - -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 -- cgit v1.2.1