Falkon Develop
Cross-platform Qt-based web browser
htmlexporter.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "htmlexporter.h"
19#include "bookmarkitem.h"
20#include "qztools.h"
21
22#include <QTextStream>
23
25 : BookmarksExporter(parent)
26{
27}
28
29QString HtmlExporter::name() const
30{
31 return BookmarksExporter::tr("HTML File") + QL1S(" (bookmarks.html)");
32}
33
34QString HtmlExporter::getPath(QWidget* parent)
35{
36 const QString defaultPath = QDir::homePath() + QLatin1String("/bookmarks.html");
37 const QString filter = BookmarksExporter::tr("HTML Bookmarks") + QL1S(" (.html)");
38 m_path = QzTools::getSaveFileName(QStringLiteral("HtmlExporter"), parent, BookmarksExporter::tr("Choose file..."), defaultPath, filter);
39 return m_path;
40}
41
43{
44 QFile file(m_path);
45
46 if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
47 setError(BookmarksExporter::tr("Cannot open file for writing!"));
48 return false;
49 }
50
51 QTextStream stream(&file);
52 stream.setEncoding(QStringConverter::Utf8);
53
54 stream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << Qt::endl;
55 stream << "<!-- This is an automatically generated file." << Qt::endl;
56 stream << " It will be read and overwritten." << Qt::endl;
57 stream << " DO NOT EDIT! -->" << Qt::endl;
58 stream << R"(<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">)" << Qt::endl;
59 stream << "<TITLE>Bookmarks</TITLE>" << Qt::endl;
60 stream << "<H1>Bookmarks</H1>" << Qt::endl;
61
62 writeBookmark(root, stream, 0);
63 return true;
64}
65
66void HtmlExporter::writeBookmark(BookmarkItem* item, QTextStream &stream, int level)
67{
68 Q_ASSERT(item);
69
70 QString indent;
71 indent.fill(QLatin1Char(' '), level * 4);
72
73 switch (item->type()) {
75 stream << indent << "<DT><A HREF=\"" << item->urlString() << "\">" << item->title() << "</A>" << Qt::endl;
76 break;
77
79 stream << indent << "<HR>" << Qt::endl;
80 break;
81
83 stream << indent << "<DT><H3>" << item->title() << "</H3>" << Qt::endl;
84 stream << indent << "<DL><p>" << Qt::endl;
85
86 const auto children = item->children();
87 for (BookmarkItem* child : children) {
88 writeBookmark(child, stream, level + 1);
89 }
90
91 stream << indent << "</DL><p>" << Qt::endl;
92 break;
93 }
94
95 case BookmarkItem::Root: {
96 stream << indent << "<DL><p>" << Qt::endl;
97
98 const auto children = item->children();
99 for (BookmarkItem* child : children) {
100 writeBookmark(child, stream, level + 1);
101 }
102
103 stream << indent << "</DL><p>" << Qt::endl;
104 break;
105 }
106
107 default:
108 break;
109 }
110}
QString urlString() const
QList< BookmarkItem * > children() const
QString title() const
Type type() const
void setError(const QString &error)
QString name() const override
HtmlExporter(QObject *parent=nullptr)
QString getPath(QWidget *parent) override
bool exportBookmarks(BookmarkItem *root) override
static QString getSaveFileName(const QString &name, QWidget *parent=nullptr, const QString &caption=QString(), const QString &dir=QString(), const QString &filter=QString(), QString *selectedFilter=nullptr, QFileDialog::Options options=QFileDialog::Options())
Definition: qztools.cpp:709
#define QL1S(x)
Definition: qzcommon.h:44