File indexing completed on 2024-04-14 05:43: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     tstream << toString(grp, showAddress);
0041 }
0042 
0043 QString HTMLExporter::toString(const KBookmarkGroup &grp, bool showAddress)
0044 {
0045     m_showAddress = showAddress;
0046     traverse(grp);
0047     return QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n")
0048         + QStringLiteral("<html><head><title>") + i18n("My Bookmarks") + QStringLiteral("</title>\n")
0049         + QStringLiteral("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">") + QStringLiteral("</head>\n") + QStringLiteral("<body>\n")
0050         + QStringLiteral("<div>") + m_string + QStringLiteral("</div>\n") + QStringLiteral("</body>\n</html>\n");
0051 }
0052 
0053 void HTMLExporter::visit(const KBookmark &bk)
0054 {
0055     // //qCDebug(KEDITBOOKMARKS_LOG) << "visit(" << bk.text() << ")";
0056     if (bk.isSeparator()) {
0057         m_out << bk.fullText() << "<br>" << QLatin1Char('\n');
0058     } else {
0059         if (m_showAddress) {
0060             m_out << bk.fullText() << "<br>" << QLatin1Char('\n');
0061             m_out << "<i><div style =\"margin-left: 1em\">" << bk.url().url().toUtf8() << "</div></i>";
0062         } else {
0063             m_out << "<a href=\"" << bk.url().url().toUtf8() << "\">";
0064             m_out << bk.fullText() << "</a><br>" << QLatin1Char('\n');
0065         }
0066     }
0067     m_out.flush();
0068 }
0069 
0070 void HTMLExporter::visitEnter(const KBookmarkGroup &grp)
0071 {
0072     // //qCDebug(KEDITBOOKMARKS_LOG) << "visitEnter(" << grp.text() << ")";
0073     m_out << "<b>" << grp.fullText() << "</b><br>" << QLatin1Char('\n');
0074     m_out << "<div style=\"margin-left: 2em\">" << QLatin1Char('\n');
0075     m_out.flush();
0076 }
0077 
0078 void HTMLExporter::visitLeave(const KBookmarkGroup &)
0079 {
0080     // //qCDebug(KEDITBOOKMARKS_LOG) << "visitLeave()";
0081     m_out << "</div>" << QLatin1Char('\n');
0082     m_out.flush();
0083 }