From b8f8e3753f68dcbf5ac7134b90efb3596a42b0a9 Mon Sep 17 00:00:00 2001 From: nowrep Date: Mon, 17 Feb 2014 12:13:26 +0100 Subject: [PATCH] [Bookmarks] Added Export dialog + HTML Bookmarks Exporter --- .../bookmarksexport/bookmarksexportdialog.cpp | 80 ++++++++++++ .../bookmarksexport/bookmarksexportdialog.h | 52 ++++++++ .../bookmarksexport/bookmarksexportdialog.ui | 117 ++++++++++++++++++ .../bookmarksexport/bookmarksexporter.cpp | 42 +++++++ .../bookmarksexport/bookmarksexporter.h | 54 ++++++++ .../bookmarksexport/htmlexporter.cpp | 106 ++++++++++++++++ .../bookmarks/bookmarksexport/htmlexporter.h | 42 +++++++ .../bookmarksimport/bookmarksimportdialog.cpp | 10 +- src/lib/lib.pro | 13 +- src/lib/other/browsinglibrary.cpp | 6 +- 10 files changed, 512 insertions(+), 10 deletions(-) create mode 100644 src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.cpp create mode 100644 src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.h create mode 100644 src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.ui create mode 100644 src/lib/bookmarks/bookmarksexport/bookmarksexporter.cpp create mode 100644 src/lib/bookmarks/bookmarksexport/bookmarksexporter.h create mode 100644 src/lib/bookmarks/bookmarksexport/htmlexporter.cpp create mode 100644 src/lib/bookmarks/bookmarksexport/htmlexporter.h diff --git a/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.cpp b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.cpp new file mode 100644 index 000000000..d232967b1 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.cpp @@ -0,0 +1,80 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 "bookmarksexportdialog.h" +#include "ui_bookmarksexportdialog.h" +#include "htmlexporter.h" +#include "mainapplication.h" +#include "bookmarks.h" + +#include + +BookmarksExportDialog::BookmarksExportDialog(QWidget* parent) + : QDialog(parent) + , ui(new Ui::BookmarksExportDialog) + , m_currentExporter(0) +{ + setAttribute(Qt::WA_DeleteOnClose); + ui->setupUi(this); + + init(); + + connect(ui->chooseOutput, SIGNAL(clicked()), this, SLOT(setPath())); + connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(exportBookmarks())); + connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(close())); +} + +BookmarksExportDialog::~BookmarksExportDialog() +{ + delete ui; +} + +void BookmarksExportDialog::setPath() +{ + Q_ASSERT(m_currentExporter); + + ui->output->setText(m_currentExporter->getPath(this)); +} + +void BookmarksExportDialog::exportBookmarks() +{ + Q_ASSERT(m_currentExporter); + + if (ui->output->text().isEmpty()) { + return; + } + + bool ok = m_currentExporter->exportBookmarks(mApp->bookmarks()->rootItem()); + + if (!ok) { + QMessageBox::critical(this, tr("Error!"), m_currentExporter->errorString()); + } + else { + close(); + } +} + +void BookmarksExportDialog::init() +{ + m_exporters.append(new HtmlExporter(this)); + + foreach (BookmarksExporter* exporter, m_exporters) { + ui->format->addItem(exporter->name()); + } + + m_currentExporter = m_exporters.first(); +} diff --git a/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.h b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.h new file mode 100644 index 000000000..bc08d0ae5 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.h @@ -0,0 +1,52 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 BOOKMARKSEXPORTDIALOG_H +#define BOOKMARKSEXPORTDIALOG_H + +#include + +#include "qz_namespace.h" + +namespace Ui +{ +class BookmarksExportDialog; +} + +class BookmarksExporter; + +class QT_QUPZILLA_EXPORT BookmarksExportDialog : public QDialog +{ + Q_OBJECT + +public: + explicit BookmarksExportDialog(QWidget* parent = 0); + ~BookmarksExportDialog(); + +private slots: + void setPath(); + void exportBookmarks(); + +private: + void init(); + + Ui::BookmarksExportDialog* ui; + QList m_exporters; + BookmarksExporter* m_currentExporter; +}; + +#endif // BOOKMARKSEXPORTDIALOG_H diff --git a/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.ui b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.ui new file mode 100644 index 000000000..0018c9b25 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/bookmarksexportdialog.ui @@ -0,0 +1,117 @@ + + + BookmarksExportDialog + + + + 0 + 0 + 571 + 152 + + + + Export Bookmarks + + + + + + <b>Export Bookmarks</b> + + + + + + + Export options + + + + + + + + true + + + + + + + Choose... + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 40 + 20 + + + + + + + + + + + 100 + 0 + + + + Output File: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Format: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 150 + 0 + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/src/lib/bookmarks/bookmarksexport/bookmarksexporter.cpp b/src/lib/bookmarks/bookmarksexport/bookmarksexporter.cpp new file mode 100644 index 000000000..d7f0af281 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/bookmarksexporter.cpp @@ -0,0 +1,42 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 "bookmarksexporter.h" + +BookmarksExporter::BookmarksExporter(QObject* parent) + : QObject(parent) +{ +} + +BookmarksExporter::~BookmarksExporter() +{ +} + +bool BookmarksExporter::error() const +{ + return !m_error.isEmpty(); +} + +QString BookmarksExporter::errorString() const +{ + return m_error; +} + +void BookmarksExporter::setError(const QString &error) +{ + m_error = error; +} diff --git a/src/lib/bookmarks/bookmarksexport/bookmarksexporter.h b/src/lib/bookmarks/bookmarksexport/bookmarksexporter.h new file mode 100644 index 000000000..9ef7406b5 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/bookmarksexporter.h @@ -0,0 +1,54 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 BOOKMARKSEXPORTER_H +#define BOOKMARKSEXPORTER_H + +#include + +#include "qz_namespace.h" + +class BookmarkItem; + +class QT_QUPZILLA_EXPORT BookmarksExporter : public QObject +{ + Q_OBJECT + +public: + explicit BookmarksExporter(QObject* parent = 0); + virtual ~BookmarksExporter(); + + bool error() const; + QString errorString() const; + + virtual QString name() const = 0; + + // Get filename from user (or a directory) + virtual QString getPath(QWidget* parent) = 0; + + // Export bookmarks, return false on error + virtual bool exportBookmarks(BookmarkItem* root) = 0; + +protected: + // Empty error = no error + void setError(const QString &error); + +private: + QString m_error; +}; + +#endif // BOOKMARKSEXPORTER_H diff --git a/src/lib/bookmarks/bookmarksexport/htmlexporter.cpp b/src/lib/bookmarks/bookmarksexport/htmlexporter.cpp new file mode 100644 index 000000000..3e5d97047 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/htmlexporter.cpp @@ -0,0 +1,106 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 "htmlexporter.h" +#include "bookmarkitem.h" +#include "qztools.h" + +#include + +HtmlExporter::HtmlExporter(QObject* parent) + : BookmarksExporter(parent) +{ +} + +QString HtmlExporter::name() const +{ + return BookmarksExporter::tr("HTML File (bookmarks.html)"); +} + +QString HtmlExporter::getPath(QWidget* parent) +{ + const QString defaultPath = QDir::homePath() + QLatin1String("/bookmarks.html"); + const QString filter = BookmarksExporter::tr("HTML Bookmarks (.html)"); + m_path = QzTools::getSaveFileName("HtmlExporter", parent, BookmarksExporter::tr("Choose file..."), defaultPath, filter); + return m_path; +} + +bool HtmlExporter::exportBookmarks(BookmarkItem* root) +{ + QFile file(m_path); + + if (!file.open(QFile::WriteOnly | QFile::Truncate)) { + setError(BookmarksExporter::tr("Cannot open file for writing!")); + return false; + } + + QTextStream stream(&file); + stream.setCodec("UTF-8"); + + stream << "" << endl; + stream << "" << endl; + stream << "" << endl; + stream << "Bookmarks" << endl; + stream << "

Bookmarks

" << endl; + + writeBookmark(root, stream, 0); + return true; +} + +void HtmlExporter::writeBookmark(BookmarkItem* item, QTextStream &stream, int level) +{ + Q_ASSERT(item); + + QString indent; + indent.fill(QLatin1Char(' '), level * 4); + + switch (item->type()) { + case BookmarkItem::Url: + stream << indent << "
urlString() << "\">" << item->title() << "" << endl; + break; + + case BookmarkItem::Separator: + stream << indent << "
" << endl; + break; + + case BookmarkItem::Folder: + stream << indent << "

" << item->title() << "

" << endl; + stream << indent << "

" << endl; + + foreach (BookmarkItem* child, item->children()) { + writeBookmark(child, stream, level + 1); + } + + stream << indent << "

" << endl; + break; + + case BookmarkItem::Root: + stream << indent << "

" << endl; + + foreach (BookmarkItem* child, item->children()) { + writeBookmark(child, stream, level + 1); + } + + stream << indent << "

" << endl; + break; + + default: + break; + } +} diff --git a/src/lib/bookmarks/bookmarksexport/htmlexporter.h b/src/lib/bookmarks/bookmarksexport/htmlexporter.h new file mode 100644 index 000000000..cc48bd301 --- /dev/null +++ b/src/lib/bookmarks/bookmarksexport/htmlexporter.h @@ -0,0 +1,42 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2014 David Rosca +* +* 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 HTMLEXPORTER_H +#define HTMLEXPORTER_H + +#include "bookmarksexporter.h" + +class QTextStream; + +class BookmarkItem; + +class HtmlExporter : public BookmarksExporter +{ +public: + explicit HtmlExporter(QObject* parent = 0); + + QString name() const; + QString getPath(QWidget* parent); + bool exportBookmarks(BookmarkItem* root); + +private: + void writeBookmark(BookmarkItem* item, QTextStream &stream, int level); + + QString m_path; +}; + +#endif // HTMLEXPORTER_H diff --git a/src/lib/bookmarks/bookmarksimport/bookmarksimportdialog.cpp b/src/lib/bookmarks/bookmarksimport/bookmarksimportdialog.cpp index 410e0b58c..d9e895ef2 100644 --- a/src/lib/bookmarks/bookmarksimport/bookmarksimportdialog.cpp +++ b/src/lib/bookmarks/bookmarksimport/bookmarksimportdialog.cpp @@ -17,11 +17,11 @@ * ============================================================ */ #include "bookmarksimportdialog.h" #include "ui_bookmarksimportdialog.h" -#include "bookmarksimport/firefoximporter.h" -#include "bookmarksimport/chromeimporter.h" -#include "bookmarksimport/operaimporter.h" -#include "bookmarksimport/htmlimporter.h" -#include "bookmarksimport/ieimporter.h" +#include "firefoximporter.h" +#include "chromeimporter.h" +#include "operaimporter.h" +#include "htmlimporter.h" +#include "ieimporter.h" #include "bookmarks.h" #include "bookmarkitem.h" #include "bookmarksmodel.h" diff --git a/src/lib/lib.pro b/src/lib/lib.pro index 206480f54..8adc01515 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -254,7 +254,10 @@ SOURCES += \ bookmarks/bookmarksmenu.cpp \ bookmarks/bookmarksicon.cpp \ bookmarks/bookmarksitemdelegate.cpp \ - bookmarks/bookmarkstoolbarbutton.cpp + bookmarks/bookmarkstoolbarbutton.cpp \ + bookmarks/bookmarksexport/bookmarksexporter.cpp \ + bookmarks/bookmarksexport/bookmarksexportdialog.cpp \ + bookmarks/bookmarksexport/htmlexporter.cpp HEADERS += \ @@ -449,7 +452,10 @@ HEADERS += \ bookmarks/bookmarksmenu.h \ bookmarks/bookmarksicon.h \ bookmarks/bookmarksitemdelegate.h \ - bookmarks/bookmarkstoolbarbutton.h + bookmarks/bookmarkstoolbarbutton.h \ + bookmarks/bookmarksexport/bookmarksexporter.h \ + bookmarks/bookmarksexport/bookmarksexportdialog.h \ + bookmarks/bookmarksexport/htmlexporter.h FORMS += \ preferences/autofillmanager.ui \ @@ -500,7 +506,8 @@ FORMS += \ tools/html5permissions/html5permissionsdialog.ui \ autofill/autofillwidget.ui \ autofill/passwordbackends/masterpassworddialog.ui \ - network/sslerrordialog.ui + network/sslerrordialog.ui \ + bookmarks/bookmarksexport/bookmarksexportdialog.ui RESOURCES += \ data/icons.qrc \ diff --git a/src/lib/other/browsinglibrary.cpp b/src/lib/other/browsinglibrary.cpp index c3f84b8e4..364d23c66 100644 --- a/src/lib/other/browsinglibrary.cpp +++ b/src/lib/other/browsinglibrary.cpp @@ -18,6 +18,7 @@ #include "browsinglibrary.h" #include "ui_browsinglibrary.h" #include "bookmarksimport/bookmarksimportdialog.h" +#include "bookmarksexport/bookmarksexportdialog.h" #include "historymanager.h" #include "bookmarksmanager.h" #include "rssmanager.h" @@ -54,7 +55,7 @@ BrowsingLibrary::BrowsingLibrary(QupZilla* mainClass, QWidget* parent) QMenu* m = new QMenu(this); m->addAction(tr("Import Bookmarks..."), this, SLOT(importBookmarks())); - m->addAction(tr("Export Bookmarks to HTML..."), this, SLOT(exportBookmarks())); + m->addAction(tr("Export Bookmarks..."), this, SLOT(exportBookmarks())); ui->importExport->setMenu(m); connect(ui->tabs, SIGNAL(CurrentChanged(int)), this, SLOT(currentIndexChanged(int))); @@ -107,7 +108,8 @@ void BrowsingLibrary::importBookmarks() void BrowsingLibrary::exportBookmarks() { - qDebug("BrowsingLibrary::exportBookmarks() NOT IMPLEMENTED"); + BookmarksExportDialog* d = new BookmarksExportDialog(this); + d->show(); } void BrowsingLibrary::showHistory(QupZilla* mainClass)