File indexing completed on 2022-11-29 20:24:09

0001 // -*- indent-tabs-mode:nil -*-
0002 // vim: set ts=4 sts=4 sw=4 et:
0003 /* This file is part of the KDE project
0004    Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License, or (at your option) version 3.
0010 
0011    This program is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014    GNU General Public License for more details.
0015 
0016    You should have received a copy of the GNU General Public License
0017    along with this program.  If not, see <http://www.gnu.org/licenses/>
0018 */
0019 
0020 #include "exporters.h"
0021 
0022 #include "keditbookmarks_debug.h"
0023 #include <KLocalizedString>
0024 
0025 #include <QFile>
0026 
0027 HTMLExporter::HTMLExporter()
0028     : m_out(&m_string, QIODevice::WriteOnly)
0029 {
0030 }
0031 
0032 void HTMLExporter::write(const KBookmarkGroup &grp, const QString &filename, bool showAddress)
0033 {
0034     QFile file(filename);
0035     if (!file.open(QIODevice::WriteOnly)) {
0036         qCCritical(KEDITBOOKMARKS_LOG) << "Can't write to file " << filename;
0037         return;
0038     }
0039     QTextStream tstream(&file);
0040 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0041     tstream.setCodec("UTF-8");
0042 #endif
0043     tstream << toString(grp, showAddress);
0044 }
0045 
0046 QString HTMLExporter::toString(const KBookmarkGroup &grp, bool showAddress)
0047 {
0048     m_showAddress = showAddress;
0049     traverse(grp);
0050     return QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n")
0051         + QStringLiteral("<html><head><title>") + i18n("My Bookmarks") + QStringLiteral("</title>\n")
0052         + QStringLiteral("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">") + QStringLiteral("</head>\n") + QStringLiteral("<body>\n")
0053         + QStringLiteral("<div>") + m_string + QStringLiteral("</div>\n") + QStringLiteral("</body>\n</html>\n");
0054 }
0055 
0056 void HTMLExporter::visit(const KBookmark &bk)
0057 {
0058     // //qCDebug(KEDITBOOKMARKS_LOG) << "visit(" << bk.text() << ")";
0059     if (bk.isSeparator()) {
0060         m_out << bk.fullText() << "<br>" << QLatin1Char('\n');
0061     } else {
0062         if (m_showAddress) {
0063             m_out << bk.fullText() << "<br>" << QLatin1Char('\n');
0064             m_out << "<i><div style =\"margin-left: 1em\">" << bk.url().url().toUtf8() << "</div></i>";
0065         } else {
0066             m_out << "<a href=\"" << bk.url().url().toUtf8() << "\">";
0067             m_out << bk.fullText() << "</a><br>" << QLatin1Char('\n');
0068         }
0069     }
0070     m_out.flush();
0071 }
0072 
0073 void HTMLExporter::visitEnter(const KBookmarkGroup &grp)
0074 {
0075     // //qCDebug(KEDITBOOKMARKS_LOG) << "visitEnter(" << grp.text() << ")";
0076     m_out << "<b>" << grp.fullText() << "</b><br>" << QLatin1Char('\n');
0077     m_out << "<div style=\"margin-left: 2em\">" << QLatin1Char('\n');
0078     m_out.flush();
0079 }
0080 
0081 void HTMLExporter::visitLeave(const KBookmarkGroup &)
0082 {
0083     // //qCDebug(KEDITBOOKMARKS_LOG) << "visitLeave()";
0084     m_out << "</div>" << QLatin1Char('\n');
0085     m_out.flush();
0086 }