1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

[Bookmarks] Added Export dialog + HTML Bookmarks Exporter

This commit is contained in:
nowrep 2014-02-17 12:13:26 +01:00
parent daa3f07426
commit b8f8e3753f
10 changed files with 512 additions and 10 deletions

View File

@ -0,0 +1,80 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarksexportdialog.h"
#include "ui_bookmarksexportdialog.h"
#include "htmlexporter.h"
#include "mainapplication.h"
#include "bookmarks.h"
#include <QMessageBox>
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();
}

View File

@ -0,0 +1,52 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef BOOKMARKSEXPORTDIALOG_H
#define BOOKMARKSEXPORTDIALOG_H
#include <QDialog>
#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<BookmarksExporter*> m_exporters;
BookmarksExporter* m_currentExporter;
};
#endif // BOOKMARKSEXPORTDIALOG_H

View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>BookmarksExportDialog</class>
<widget class="QDialog" name="BookmarksExportDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>571</width>
<height>152</height>
</rect>
</property>
<property name="windowTitle">
<string>Export Bookmarks</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>&lt;b&gt;Export Bookmarks&lt;/b&gt;</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Export options</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="output">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="chooseOutput">
<property name="text">
<string>Choose...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Output File:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Format:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="format">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,42 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#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;
}

View File

@ -0,0 +1,54 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#ifndef BOOKMARKSEXPORTER_H
#define BOOKMARKSEXPORTER_H
#include <QObject>
#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

View File

@ -0,0 +1,106 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "htmlexporter.h"
#include "bookmarkitem.h"
#include "qztools.h"
#include <QTextStream>
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 << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
stream << "<!-- This is an automatically generated file." << endl;
stream << " It will be read and overwritten." << endl;
stream << " DO NOT EDIT! -->" << endl;
stream << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" << endl;
stream << "<TITLE>Bookmarks</TITLE>" << endl;
stream << "<H1>Bookmarks</H1>" << 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 << "<DT><A HREF=\"" << item->urlString() << "\">" << item->title() << "</A>" << endl;
break;
case BookmarkItem::Separator:
stream << indent << "<HR>" << endl;
break;
case BookmarkItem::Folder:
stream << indent << "<DT><H3>" << item->title() << "</H3>" << endl;
stream << indent << "<DL><p>" << endl;
foreach (BookmarkItem* child, item->children()) {
writeBookmark(child, stream, level + 1);
}
stream << indent << "</DL><p>" << endl;
break;
case BookmarkItem::Root:
stream << indent << "<DL><p>" << endl;
foreach (BookmarkItem* child, item->children()) {
writeBookmark(child, stream, level + 1);
}
stream << indent << "</DL><p>" << endl;
break;
default:
break;
}
}

View File

@ -0,0 +1,42 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#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

View File

@ -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"

View File

@ -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 \

View File

@ -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)