From 06201e4d405cef119207b528f76f8f993c09e527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Tue, 12 Jan 2016 16:55:57 +0100 Subject: render mail to html --- framework/CMakeLists.txt | 7 ++ framework/mail/CMakeLists.txt | 2 +- framework/mail/filehtmlwriter.cpp | 111 +++++++++++++++++++++++++++++++ framework/mail/filehtmlwriter.h | 64 ++++++++++++++++++ framework/mail/maillistmodel.cpp | 46 +++++++++++++ framework/mail/maillistmodel.h | 3 +- framework/mail/objecttreesource.cpp | 129 ++++++++++++++++++++++++++++++++++++ framework/mail/objecttreesource.h | 55 +++++++++++++++ 8 files changed, 415 insertions(+), 2 deletions(-) create mode 100644 framework/mail/filehtmlwriter.cpp create mode 100644 framework/mail/filehtmlwriter.h create mode 100644 framework/mail/objecttreesource.cpp create mode 100644 framework/mail/objecttreesource.h (limited to 'framework') diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index c91ee0d1..8301392a 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -16,14 +16,21 @@ include(KDEInstallDirs) find_package(Qt5 COMPONENTS REQUIRED Core Qml) +find_package(KF5Otp "5.1.42" CONFIG REQUIRED) +find_package(KF5Mime "4.87.0" CONFIG REQUIRED) find_package(Akonadi2Common CONFIG REQUIRED) find_package(KF5Async) +find_package(KF5Libkleo) set(CMAKE_AUTOMOC ON) add_definitions("-Wall -std=c++0x -g") include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/common) include_directories(SYSTEM /work/install/include/) include_directories(SYSTEM /work/install/include/KF5/) +include_directories(SYSTEM /work/install/include/KF5/KIconThemes) +include_directories(SYSTEM /work/install/include/KF5/KMime) +include_directories(SYSTEM /work/install/include/KF5/KService) +include_directories(SYSTEM /work/install/include/KF5/KCoreAddons) enable_testing() diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index a8a4b3e8..e7286763 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt @@ -13,7 +13,7 @@ add_library(mailplugin SHARED ${mailplugin_SRCS}) qt5_use_modules(mailplugin Core Quick Qml) -target_link_libraries(mailplugin KF5::akonadi2common) +target_link_libraries(mailplugin KF5::akonadi2common KF5::Otp) install(TARGETS mailplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) diff --git a/framework/mail/filehtmlwriter.cpp b/framework/mail/filehtmlwriter.cpp new file mode 100644 index 00000000..e435e1be --- /dev/null +++ b/framework/mail/filehtmlwriter.cpp @@ -0,0 +1,111 @@ +/* -*- c++ -*- + filehtmlwriter.cpp + + This file is part of KMail, the KDE mail client. + Copyright (c) 2003 Marc Mutz + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#include "filehtmlwriter.h" + +#include + +FileHtmlWriter::FileHtmlWriter(const QString &filename) + : MessageViewer::HtmlWriter(), + mFile(filename.isEmpty() ? QStringLiteral("filehtmlwriter.out") : filename) +{ + mStream.setCodec("UTF-8"); +} + +FileHtmlWriter::~FileHtmlWriter() +{ + if (mFile.isOpen()) { + mStream.setDevice(0); + mFile.close(); + } +} + +void FileHtmlWriter::begin(const QString &css) +{ + openOrWarn(); + if (!css.isEmpty()) { + write(QLatin1String("\n")); + } +} + +void FileHtmlWriter::end() +{ + flush(); + mStream.setDevice(0); + mFile.close(); +} + +void FileHtmlWriter::reset() +{ + if (mFile.isOpen()) { + mStream.setDevice(0); + mFile.close(); + } +} + +void FileHtmlWriter::write(const QString &str) +{ + mStream << str.toUtf8(); + flush(); +} + +void FileHtmlWriter::queue(const QString &str) +{ + write(str); +} + +void FileHtmlWriter::flush() +{ + mStream.flush(); + mFile.flush(); +} + +void FileHtmlWriter::openOrWarn() +{ + if (mFile.isOpen()) { + mStream.setDevice(0); + mFile.close(); + } + if (!mFile.open(QIODevice::WriteOnly)) { + } else { + mStream.setDevice(&mFile); + } +} + +void FileHtmlWriter::embedPart(const QByteArray &contentId, const QString &url) +{ + mStream << "" << endl; + flush(); +} +void FileHtmlWriter::extraHead(const QString &) +{ + +} \ No newline at end of file diff --git a/framework/mail/filehtmlwriter.h b/framework/mail/filehtmlwriter.h new file mode 100644 index 00000000..8bda39f8 --- /dev/null +++ b/framework/mail/filehtmlwriter.h @@ -0,0 +1,64 @@ +/* -*- c++ -*- + filehtmlwriter.h + + This file is part of KMail, the KDE mail client. + Copyright (c) 2003 Marc Mutz + + KMail is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License, version 2, as + published by the Free Software Foundation. + + KMail is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + In addition, as a special exception, the copyright holders give + permission to link the code of this program with any edition of + the Qt library by Trolltech AS, Norway (or with modified versions + of Qt that use the same license as Qt), and distribute linked + combinations including the two. You must obey the GNU General + Public License in all respects for all of the code used other than + Qt. If you modify this file, you may extend this exception to + your version of the file, but you are not obligated to do so. If + you do not wish to do so, delete this exception statement from + your version. +*/ + +#ifndef __MESSAGEVIEWER_FILEHTMLWRITER_H__ +#define __MESSAGEVIEWER_FILEHTMLWRITER_H__ + +#include + +#include +#include + +class QString; + +class FileHtmlWriter : public MessageViewer::HtmlWriter +{ +public: + explicit FileHtmlWriter(const QString &filename); + virtual ~FileHtmlWriter(); + + void begin(const QString &cssDefs) Q_DECL_OVERRIDE; + void end() Q_DECL_OVERRIDE; + void reset() Q_DECL_OVERRIDE; + void write(const QString &str) Q_DECL_OVERRIDE; + void queue(const QString &str) Q_DECL_OVERRIDE; + void flush() Q_DECL_OVERRIDE; + void embedPart(const QByteArray &contentId, const QString &url) Q_DECL_OVERRIDE; + void extraHead(const QString &str) Q_DECL_OVERRIDE; +private: + void openOrWarn(); + +private: + QFile mFile; + QTextStream mStream; +}; + +#endif // __MESSAGEVIEWER_FILEHTMLWRITER_H__ diff --git a/framework/mail/maillistmodel.cpp b/framework/mail/maillistmodel.cpp index e6a9c218..c37e2be4 100644 --- a/framework/mail/maillistmodel.cpp +++ b/framework/mail/maillistmodel.cpp @@ -1,6 +1,12 @@ #include "maillistmodel.h" +#include "filehtmlwriter.h" +#include "objecttreesource.h" + #include +#include +#include +#include MailListModel::MailListModel(QObject *parent) : QIdentityProxyModel() @@ -25,6 +31,7 @@ QHash< int, QByteArray > MailListModel::roleNames() const roles[Important] = "important"; roles[Id] = "id"; roles[MimeMessage] = "mimeMessage"; + roles[RenderedMessage] = "renderedMessage"; roles[DomainObject] = "domainObject"; return roles; @@ -61,6 +68,45 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const } return "Failed to read mail."; } + case RenderedMessage: { + auto filename = srcIdx.sibling(srcIdx.row(), 6).data(Qt::DisplayRole).toString(); + QFile file(filename); + if (file.open(QFile::ReadOnly)) { + const auto mailData = KMime::CRLFtoLF(file.readAll()); + KMime::Message::Ptr msg(new KMime::Message); + msg->setContent(mailData); + msg->parse(); + + // render the mail + const QString fname("/tmp/test.html"); + FileHtmlWriter htmlWriter(fname); + QImage paintDevice; + MessageViewer::CSSHelper cssHelper(&paintDevice); + MessageViewer::NodeHelper nodeHelper; + ObjectTreeSource source(&htmlWriter, &cssHelper); + MessageViewer::ObjectTreeParser otp(&source, &nodeHelper); + + htmlWriter.begin(QString()); + htmlWriter.queue(cssHelper.htmlHead(false)); + + otp.parseObjectTree(msg.data()); + + htmlWriter.queue(QStringLiteral("")); + htmlWriter.flush(); + htmlWriter.end(); + + QFile file(fname); + if (file.open(QFile::ReadOnly)) { + const auto content = file.readAll(); + return content; + } else { + qWarning() << "Failed to open the file"; + } + } else { + qWarning() << "Failed to open the file"; + } + return "Failed to read mail."; + } } return QIdentityProxyModel::data(idx, role); } diff --git a/framework/mail/maillistmodel.h b/framework/mail/maillistmodel.h index 7718477c..7eb55ffd 100644 --- a/framework/mail/maillistmodel.h +++ b/framework/mail/maillistmodel.h @@ -27,7 +27,8 @@ public: Important, Id, MimeMessage, - DomainObject + DomainObject, + RenderedMessage }; QHash roleNames() const; diff --git a/framework/mail/objecttreesource.cpp b/framework/mail/objecttreesource.cpp new file mode 100644 index 00000000..cde4775a --- /dev/null +++ b/framework/mail/objecttreesource.cpp @@ -0,0 +1,129 @@ +/* + Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net + Copyright (c) 2009 Andras Mantia + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "objecttreesource.h" + +#include + +class ObjectSourcePrivate +{ +public: + ObjectSourcePrivate() + : mWriter(0) + , mCSSHelper(0) + , mAllowDecryption(false) + , mHtmlLoadExternal(false) + , mHtmlMail(false) + { + + } + MessageViewer::HtmlWriter *mWriter; + MessageViewer::CSSHelper *mCSSHelper; + bool mAllowDecryption; + bool mHtmlLoadExternal; + bool mHtmlMail; +}; + +ObjectTreeSource::ObjectTreeSource(MessageViewer::HtmlWriter *writer, + MessageViewer::CSSHelper *cssHelper) + : MessageViewer::ObjectTreeSourceIf() + , d(new ObjectSourcePrivate) + { + d->mWriter = writer; + d->mCSSHelper = cssHelper; + } + +ObjectTreeSource::~ObjectTreeSource() +{ + delete d; +} + +void ObjectTreeSource::setAllowDecryption(bool allowDecryption) +{ + d->mAllowDecryption = allowDecryption; +} + +MessageViewer::HtmlWriter *ObjectTreeSource::htmlWriter() +{ + return d->mWriter; +} +MessageViewer::CSSHelper *ObjectTreeSource::cssHelper() +{ + return d->mCSSHelper; +} + +bool ObjectTreeSource::htmlLoadExternal() +{ + return d->mHtmlLoadExternal; +} + +void ObjectTreeSource::setHtmlLoadExternal(bool loadExternal) +{ + d->mHtmlLoadExternal = loadExternal; +} + +bool ObjectTreeSource::htmlMail() +{ + return d->mHtmlMail; +} + +void ObjectTreeSource::setHtmlMail(bool htmlMail) +{ + d->mHtmlMail = htmlMail; +} + +bool ObjectTreeSource::decryptMessage() +{ + return d->mAllowDecryption; +} + +bool ObjectTreeSource::showSignatureDetails() +{ + return true; +} + +int ObjectTreeSource::levelQuote() +{ + return 1; +} + +const QTextCodec *ObjectTreeSource::overrideCodec() +{ + return Q_NULLPTR; +} + +QString ObjectTreeSource::createMessageHeader(KMime::Message *message) +{ + return QString(); +} + +const MessageViewer::AttachmentStrategy *ObjectTreeSource::attachmentStrategy() +{ + return MessageViewer::AttachmentStrategy::smart(); +} + +QObject *ObjectTreeSource::sourceObject() +{ + return Q_NULLPTR; +} + +void ObjectTreeSource::setHtmlMode(MessageViewer::Util::HtmlMode mode) +{ + Q_UNUSED(mode); +} \ No newline at end of file diff --git a/framework/mail/objecttreesource.h b/framework/mail/objecttreesource.h new file mode 100644 index 00000000..c61ba715 --- /dev/null +++ b/framework/mail/objecttreesource.h @@ -0,0 +1,55 @@ +/* + Copyright (C) 2009 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net + Copyright (c) 2009 Andras Mantia + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef MAILVIEWER_OBJECTTREEEMPTYSOURCE_H +#define MAILVIEWER_OBJECTTREEEMPTYSOURCE_H + +#include + +class QString; + +class ObjectSourcePrivate; +class ObjectTreeSource : public MessageViewer::ObjectTreeSourceIf +{ +public: + ObjectTreeSource(MessageViewer::HtmlWriter *writer, + MessageViewer::CSSHelper *cssHelper); + virtual ~ObjectTreeSource(); + void setHtmlLoadExternal(bool loadExternal); + void setHtmlMail(bool htmlMail); + bool htmlMail() Q_DECL_OVERRIDE; + bool decryptMessage() Q_DECL_OVERRIDE; + bool htmlLoadExternal() Q_DECL_OVERRIDE; + bool showSignatureDetails() Q_DECL_OVERRIDE; + void setHtmlMode(MessageViewer::Util::HtmlMode mode) Q_DECL_OVERRIDE; + void setAllowDecryption(bool allowDecryption); + int levelQuote() Q_DECL_OVERRIDE; + const QTextCodec *overrideCodec() Q_DECL_OVERRIDE; + QString createMessageHeader(KMime::Message *message) Q_DECL_OVERRIDE; + const MessageViewer::AttachmentStrategy *attachmentStrategy() Q_DECL_OVERRIDE; + MessageViewer::HtmlWriter *htmlWriter() Q_DECL_OVERRIDE; + MessageViewer::CSSHelper *cssHelper() Q_DECL_OVERRIDE; + QObject *sourceObject() Q_DECL_OVERRIDE; + +private: + ObjectSourcePrivate *const d; +}; + +#endif + -- cgit v1.2.3