From 88bbb10880d65af417689c7ad3455acf39a33215 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 22 Feb 2016 20:21:26 +0100 Subject: Moved the mail parsing into it's own little corner --- framework/mail/CMakeLists.txt | 1 + framework/mail/maillistmodel.cpp | 38 -------------------- framework/mail/maillistmodel.h | 3 +- framework/mail/mailplugin.cpp | 2 ++ framework/mail/messageparser.cpp | 76 ++++++++++++++++++++++++++++++++++++++++ framework/mail/messageparser.h | 45 ++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 40 deletions(-) create mode 100644 framework/mail/messageparser.cpp create mode 100644 framework/mail/messageparser.h (limited to 'framework') diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index f83a3a9e..13788cfa 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt @@ -10,6 +10,7 @@ set(mailplugin_SRCS stringhtmlwriter.cpp csshelper.cpp composer.cpp + messageparser.cpp ) add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") diff --git a/framework/mail/maillistmodel.cpp b/framework/mail/maillistmodel.cpp index 6bd26759..0bed1305 100644 --- a/framework/mail/maillistmodel.cpp +++ b/framework/mail/maillistmodel.cpp @@ -20,13 +20,7 @@ #include "maillistmodel.h" -#include "stringhtmlwriter.h" -#include "objecttreesource.h" -#include "csshelper.h" - #include -#include -#include MailListModel::MailListModel(QObject *parent) @@ -53,7 +47,6 @@ QHash< int, QByteArray > MailListModel::roleNames() const roles[Important] = "important"; roles[Id] = "id"; roles[MimeMessage] = "mimeMessage"; - roles[RenderedMessage] = "renderedMessage"; roles[DomainObject] = "domainObject"; return roles; @@ -90,37 +83,6 @@ 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 - StringHtmlWriter htmlWriter; - QImage paintDevice; - 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.end(); - - return htmlWriter.html(); - } else { - qWarning() << "Failed to open the file"; - } - return "Failed to read mail."; - } } return QSortFilterProxyModel::data(idx, role); } diff --git a/framework/mail/maillistmodel.h b/framework/mail/maillistmodel.h index d179eb02..47a2a091 100644 --- a/framework/mail/maillistmodel.h +++ b/framework/mail/maillistmodel.h @@ -49,8 +49,7 @@ public: Important, Id, MimeMessage, - DomainObject, - RenderedMessage + DomainObject }; QHash roleNames() const; diff --git a/framework/mail/mailplugin.cpp b/framework/mail/mailplugin.cpp index 691d4a15..750c6f8e 100644 --- a/framework/mail/mailplugin.cpp +++ b/framework/mail/mailplugin.cpp @@ -23,6 +23,7 @@ #include "maillistmodel.h" #include "folderlistmodel.h" #include "composer.h" +#include "messageparser.h" #include @@ -33,4 +34,5 @@ void MailPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "FolderListModel"); qmlRegisterType(uri, 1, 0, "MailListModel"); qmlRegisterType(uri, 1, 0, "Composer"); + qmlRegisterType(uri, 1, 0, "MessageParser"); } diff --git a/framework/mail/messageparser.cpp b/framework/mail/messageparser.cpp new file mode 100644 index 00000000..2529a677 --- /dev/null +++ b/framework/mail/messageparser.cpp @@ -0,0 +1,76 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#include "messageparser.h" + +#include "stringhtmlwriter.h" +#include "objecttreesource.h" +#include "csshelper.h" + +#include +#include +#include +#include +#include + +MessageParser::MessageParser(QObject *parent) + : QObject(parent) +{ + +} + +QString MessageParser::html() const +{ + return mHtml; +} + +QVariant MessageParser::message() const +{ + return QVariant(); +} + +void MessageParser::setMessage(const QVariant &message) +{ + QTime time; + time.start(); + const auto mailData = KMime::CRLFtoLF(message.toByteArray()); + KMime::Message::Ptr msg(new KMime::Message); + msg->setContent(mailData); + msg->parse(); + qWarning() << "parsed: " << time.elapsed(); + qWarning() << "parsed: " << message.toByteArray(); + + // render the mail + StringHtmlWriter htmlWriter; + QImage paintDevice; + 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.end(); + + mHtml = htmlWriter.html(); + emit htmlChanged(); +} diff --git a/framework/mail/messageparser.h b/framework/mail/messageparser.h new file mode 100644 index 00000000..754ac2bd --- /dev/null +++ b/framework/mail/messageparser.h @@ -0,0 +1,45 @@ +/* + Copyright (c) 2016 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#pragma once + +#include +#include +#include + +class MessageParser : public QObject +{ + Q_OBJECT + Q_PROPERTY (QVariant message READ message WRITE setMessage) + Q_PROPERTY (QString html READ html NOTIFY htmlChanged) + +public: + explicit MessageParser(QObject *parent = Q_NULLPTR); + + QString html() const; + + QVariant message() const; + void setMessage(const QVariant &to); + +signals: + void htmlChanged(); + +private: + QString mHtml; +}; -- cgit v1.2.3