From b1da23ca72fce0713c22630771eb0e917231446d Mon Sep 17 00:00:00 2001 From: nowrep Date: Mon, 30 Dec 2013 00:35:01 +0100 Subject: [PATCH] [Bookmarks] Added option to export bookmarks to HTML file. --- src/lib/bookmarks/bookmarksmanager.cpp | 16 ++++- src/lib/bookmarks/bookmarksmanager.h | 2 + src/lib/bookmarks/bookmarksmanager.ui | 40 +++++-------- src/lib/bookmarks/bookmarksmodel.cpp | 81 ++++++++++++++++++++++++++ src/lib/bookmarks/bookmarksmodel.h | 2 + 5 files changed, 116 insertions(+), 25 deletions(-) diff --git a/src/lib/bookmarks/bookmarksmanager.cpp b/src/lib/bookmarks/bookmarksmanager.cpp index 8da7d4b80..49ecd1670 100644 --- a/src/lib/bookmarks/bookmarksmanager.cpp +++ b/src/lib/bookmarks/bookmarksmanager.cpp @@ -37,6 +37,7 @@ #include #include #include +#include BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) : QWidget(parent) @@ -70,7 +71,11 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); connect(m_bookmarksModel, SIGNAL(bookmarkParentChanged(QString,QByteArray,int,QUrl,QString,QString)), this, SLOT(changeBookmarkParent(QString,QByteArray,int,QUrl,QString,QString))); - connect(ui->importBookmarks, SIGNAL(clicked(QPoint)), this, SLOT(importBookmarks())); + QMenu* menu = new QMenu; + menu->addAction(tr("Import Bookmarks..."), this, SLOT(importBookmarks())); + menu->addAction(tr("Export Bookmarks to HTML..."), this, SLOT(exportBookmarks())); + + ui->importExport->setMenu(menu); QShortcut* deleteAction = new QShortcut(QKeySequence("Del"), ui->bookmarksTree); connect(deleteAction, SIGNAL(activated()), this, SLOT(deleteItem())); @@ -85,6 +90,15 @@ void BookmarksManager::importBookmarks() b->show(); } +void BookmarksManager::exportBookmarks() +{ + QString file = QzTools::getSaveFileName("BookmarksManager-Export", this, tr("Export to HTML..."), QDir::homePath() + "/bookmarks.html"); + + if (!file.isEmpty()) { + m_bookmarksModel->exportToHtml(file); + } +} + void BookmarksManager::search(const QString &string) { ui->bookmarksTree->filterString(string); diff --git a/src/lib/bookmarks/bookmarksmanager.h b/src/lib/bookmarks/bookmarksmanager.h index 1e7d6cf4a..008123403 100644 --- a/src/lib/bookmarks/bookmarksmanager.h +++ b/src/lib/bookmarks/bookmarksmanager.h @@ -64,7 +64,9 @@ private slots: void moveBookmark(); void renameBookmark(); void changeIcon(); + void importBookmarks(); + void exportBookmarks(); void addFolder(const QString &name); void addSubfolder(const QString &name); diff --git a/src/lib/bookmarks/bookmarksmanager.ui b/src/lib/bookmarks/bookmarksmanager.ui index 98b1d2816..d0cfa93ce 100644 --- a/src/lib/bookmarks/bookmarksmanager.ui +++ b/src/lib/bookmarks/bookmarksmanager.ui @@ -14,16 +14,17 @@ Bookmarks - - - - PointingHandCursor - + + - Import Bookmarks + Add Folder - - true + + + + + + Collapse All @@ -40,14 +41,7 @@ - - - - Add Folder - - - - + Qt::CustomContextMenu @@ -83,21 +77,19 @@ - - + + - Collapse All + Import and Export + + + QToolButton::InstantPopup - - ClickableLabel - QLabel -
clickablelabel.h
-
BookmarksTree QTreeWidget diff --git a/src/lib/bookmarks/bookmarksmodel.cpp b/src/lib/bookmarks/bookmarksmodel.cpp index b147eb0f3..4134d76f9 100644 --- a/src/lib/bookmarks/bookmarksmodel.cpp +++ b/src/lib/bookmarks/bookmarksmodel.cpp @@ -22,7 +22,10 @@ #include "mainapplication.h" #include "settings.h" +#include #include +#include +#include // SQLite DB -> table bookmarks + folders // Unique in bookmarks table is id @@ -447,6 +450,84 @@ bool BookmarksModel::renameFolder(const QString &before, const QString &after) return true; } +void BookmarksModel::exportToHtml(const QString &fileName) +{ + QFile file(fileName); + + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { + qWarning() << "BookmarksModel::exportHtml Cannot open file for writing!" << file.errorString(); + } + + QTextStream out(&file); + + out << "" << endl; + out << "" << endl; + out << "" << endl; + out << "Bookmarks" << endl; + out << "

Bookmarks

" << endl; + + out << "

" << endl; + + QString indent = " "; + QList > allFolders; + + QPair menu; + menu.first = "bookmarksMenu"; + menu.second = false; + + QPair toolbar; + toolbar.first = "bookmarksToolbar"; + toolbar.second = false; + + allFolders.append(menu); + allFolders.append(toolbar); + + QSqlQuery query; + query.exec("SELECT name, subfolder FROM folders"); + + while (query.next()) { + QPair pair; + pair.first = query.value(0).toString(); + pair.second = query.value(1).toString() == QLatin1String("yes"); + + allFolders.append(pair); + } + + for (int i = 0; i < allFolders.size(); ++i) { + QPair pair = allFolders.at(i); + + out << indent << "

" << pair.first << "

" << endl; + out << indent << "

" << endl; + + QSqlQuery q; + q.prepare("SELECT title, url FROM bookmarks WHERE folder = ?"); + q.addBindValue(pair.first); + q.exec(); + + while (q.next()) { + QString title = q.value(0).toString(); + QString url = q.value(1).toString(); + + out << indent << indent << "

" << title << "" << endl; + } + + out << indent << "

" << endl; + } + + query.exec("SELECT title, url FROM bookmarks WHERE folder='' OR folder='unsorted'"); + + while (query.next()) { + QString title = query.value(0).toString(); + QString url = query.value(1).toString(); + + out << indent << "

" << title << "" << endl; + } + + out << "

" << endl; +} + QVector BookmarksModel::folderBookmarks(const QString &name) { QVector list; diff --git a/src/lib/bookmarks/bookmarksmodel.h b/src/lib/bookmarks/bookmarksmodel.h index 5b9ee9dd9..960e5906a 100644 --- a/src/lib/bookmarks/bookmarksmodel.h +++ b/src/lib/bookmarks/bookmarksmodel.h @@ -98,6 +98,8 @@ public: bool renameFolder(const QString &before, const QString &after); + void exportToHtml(const QString &fileName); + static bool bookmarksEqual(const Bookmark &one, const Bookmark &two); static QString toTranslatedFolder(const QString &name); static QString fromTranslatedFolder(const QString &name);