File indexing completed on 2024-12-22 04:40:57

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "htmlexporter.h"
0019 #include "bookmarkitem.h"
0020 #include "qztools.h"
0021 
0022 #include <QTextStream>
0023 
0024 HtmlExporter::HtmlExporter(QObject* parent)
0025     : BookmarksExporter(parent)
0026 {
0027 }
0028 
0029 QString HtmlExporter::name() const
0030 {
0031     return BookmarksExporter::tr("HTML File") + QL1S(" (bookmarks.html)");
0032 }
0033 
0034 QString HtmlExporter::getPath(QWidget* parent)
0035 {
0036     const QString defaultPath = QDir::homePath() + QLatin1String("/bookmarks.html");
0037     const QString filter = BookmarksExporter::tr("HTML Bookmarks") + QL1S(" (.html)");
0038     m_path = QzTools::getSaveFileName(QStringLiteral("HtmlExporter"), parent, BookmarksExporter::tr("Choose file..."), defaultPath, filter);
0039     return m_path;
0040 }
0041 
0042 bool HtmlExporter::exportBookmarks(BookmarkItem* root)
0043 {
0044     QFile file(m_path);
0045 
0046     if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
0047         setError(BookmarksExporter::tr("Cannot open file for writing!"));
0048         return false;
0049     }
0050 
0051     QTextStream stream(&file);
0052     stream.setEncoding(QStringConverter::Utf8);
0053 
0054     stream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << Qt::endl;
0055     stream << "<!-- This is an automatically generated file." << Qt::endl;
0056     stream << "     It will be read and overwritten." << Qt::endl;
0057     stream << "     DO NOT EDIT! -->" << Qt::endl;
0058     stream << R"(<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">)" << Qt::endl;
0059     stream << "<TITLE>Bookmarks</TITLE>" << Qt::endl;
0060     stream << "<H1>Bookmarks</H1>" << Qt::endl;
0061 
0062     writeBookmark(root, stream, 0);
0063     return true;
0064 }
0065 
0066 void HtmlExporter::writeBookmark(BookmarkItem* item, QTextStream &stream, int level)
0067 {
0068     Q_ASSERT(item);
0069 
0070     QString indent;
0071     indent.fill(QLatin1Char(' '), level * 4);
0072 
0073     switch (item->type()) {
0074     case BookmarkItem::Url:
0075         stream << indent << "<DT><A HREF=\"" << item->urlString() << "\">" << item->title() << "</A>" << Qt::endl;
0076         break;
0077 
0078     case BookmarkItem::Separator:
0079         stream << indent << "<HR>" << Qt::endl;
0080         break;
0081 
0082     case BookmarkItem::Folder: {
0083         stream << indent << "<DT><H3>" << item->title() << "</H3>" << Qt::endl;
0084         stream << indent << "<DL><p>" << Qt::endl;
0085 
0086         const auto children = item->children();
0087         for (BookmarkItem* child : children) {
0088             writeBookmark(child, stream, level + 1);
0089         }
0090 
0091         stream << indent << "</DL><p>" << Qt::endl;
0092         break;
0093     }
0094 
0095     case BookmarkItem::Root: {
0096         stream << indent << "<DL><p>" << Qt::endl;
0097 
0098         const auto children = item->children();
0099         for (BookmarkItem* child : children) {
0100             writeBookmark(child, stream, level + 1);
0101         }
0102 
0103         stream << indent << "</DL><p>" << Qt::endl;
0104         break;
0105     }
0106 
0107     default:
0108         break;
0109     }
0110 }