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

[Bookmarks] Added option to export bookmarks to HTML file.

This commit is contained in:
nowrep 2013-12-30 00:35:01 +01:00
parent 4265b90643
commit b1da23ca72
5 changed files with 116 additions and 25 deletions

View File

@ -37,6 +37,7 @@
#include <QShortcut> #include <QShortcut>
#include <QMenu> #include <QMenu>
#include <QSqlQuery> #include <QSqlQuery>
#include <QLabel>
BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent)
: 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(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(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); QShortcut* deleteAction = new QShortcut(QKeySequence("Del"), ui->bookmarksTree);
connect(deleteAction, SIGNAL(activated()), this, SLOT(deleteItem())); connect(deleteAction, SIGNAL(activated()), this, SLOT(deleteItem()));
@ -85,6 +90,15 @@ void BookmarksManager::importBookmarks()
b->show(); 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) void BookmarksManager::search(const QString &string)
{ {
ui->bookmarksTree->filterString(string); ui->bookmarksTree->filterString(string);

View File

@ -64,7 +64,9 @@ private slots:
void moveBookmark(); void moveBookmark();
void renameBookmark(); void renameBookmark();
void changeIcon(); void changeIcon();
void importBookmarks(); void importBookmarks();
void exportBookmarks();
void addFolder(const QString &name); void addFolder(const QString &name);
void addSubfolder(const QString &name); void addSubfolder(const QString &name);

View File

@ -14,16 +14,17 @@
<string>Bookmarks</string> <string>Bookmarks</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="1" column="7"> <item row="1" column="0" colspan="4">
<widget class="ClickableLabel" name="importBookmarks"> <widget class="QPushButton" name="addFolder">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text"> <property name="text">
<string>Import Bookmarks</string> <string>Add Folder</string>
</property> </property>
<property name="html-link-look" stdset="0"> </widget>
<bool>true</bool> </item>
<item row="1" column="5">
<widget class="QPushButton" name="collapseAll">
<property name="text">
<string>Collapse All</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -40,14 +41,7 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="1" column="0" colspan="4"> <item row="0" column="0" colspan="8">
<widget class="QPushButton" name="addFolder">
<property name="text">
<string>Add Folder</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="9">
<widget class="BookmarksTree" name="bookmarksTree"> <widget class="BookmarksTree" name="bookmarksTree">
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum> <enum>Qt::CustomContextMenu</enum>
@ -83,21 +77,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="5"> <item row="1" column="7">
<widget class="QPushButton" name="collapseAll"> <widget class="QToolButton" name="importExport">
<property name="text"> <property name="text">
<string>Collapse All</string> <string>Import and Export</string>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property> </property>
</widget> </widget>
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget>
<class>ClickableLabel</class>
<extends>QLabel</extends>
<header>clickablelabel.h</header>
</customwidget>
<customwidget> <customwidget>
<class>BookmarksTree</class> <class>BookmarksTree</class>
<extends>QTreeWidget</extends> <extends>QTreeWidget</extends>

View File

@ -22,7 +22,10 @@
#include "mainapplication.h" #include "mainapplication.h"
#include "settings.h" #include "settings.h"
#include <QTextStream>
#include <QBuffer> #include <QBuffer>
#include <QDebug>
#include <QFile>
// SQLite DB -> table bookmarks + folders // SQLite DB -> table bookmarks + folders
// Unique in bookmarks table is id // Unique in bookmarks table is id
@ -447,6 +450,84 @@ bool BookmarksModel::renameFolder(const QString &before, const QString &after)
return true; 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 << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
out << "<!-- This is an automatically generated file." << endl;
out << " It will be read and overwritten." << endl;
out << " DO NOT EDIT! -->" << endl;
out << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" << endl;
out << "<TITLE>Bookmarks</TITLE>" << endl;
out << "<H1>Bookmarks</H1>" << endl;
out << "<DL><p>" << endl;
QString indent = " ";
QList<QPair<QString, bool> > allFolders;
QPair<QString, bool> menu;
menu.first = "bookmarksMenu";
menu.second = false;
QPair<QString, bool> 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<QString, bool> 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<QString, bool> pair = allFolders.at(i);
out << indent << "<DT><H3 TOOLBAR_SUBFOLDER=\"" << (pair.second ? "yes" : "no") << "\">" << pair.first << "</H3>" << endl;
out << indent << "<DL><p>" << 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 << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << indent << "</DL><p>" << 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 << "<DT><A HREF=\"" << url << "\">" << title << "</A>" << endl;
}
out << "</DL><p>" << endl;
}
QVector<Bookmark> BookmarksModel::folderBookmarks(const QString &name) QVector<Bookmark> BookmarksModel::folderBookmarks(const QString &name)
{ {
QVector<Bookmark> list; QVector<Bookmark> list;

View File

@ -98,6 +98,8 @@ public:
bool renameFolder(const QString &before, const QString &after); bool renameFolder(const QString &before, const QString &after);
void exportToHtml(const QString &fileName);
static bool bookmarksEqual(const Bookmark &one, const Bookmark &two); static bool bookmarksEqual(const Bookmark &one, const Bookmark &two);
static QString toTranslatedFolder(const QString &name); static QString toTranslatedFolder(const QString &name);
static QString fromTranslatedFolder(const QString &name); static QString fromTranslatedFolder(const QString &name);