From e8ce86ccdf23fad155cf4888cb4db657b99c8bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20Knau=C3=9F?= Date: Fri, 15 Jan 2016 13:02:24 +0100 Subject: Render HTML directly to string and not via indirecton of a file --- framework/mail/CMakeLists.txt | 2 +- framework/mail/filehtmlwriter.cpp | 111 ----------------------------- framework/mail/filehtmlwriter.h | 64 ----------------- framework/mail/maillistmodel.cpp | 14 +--- framework/mail/stringhtmlwriter.cpp | 134 ++++++++++++++++++++++++++++++++++++ framework/mail/stringhtmlwriter.h | 68 ++++++++++++++++++ 6 files changed, 206 insertions(+), 187 deletions(-) delete mode 100644 framework/mail/filehtmlwriter.cpp delete mode 100644 framework/mail/filehtmlwriter.h create mode 100644 framework/mail/stringhtmlwriter.cpp create mode 100644 framework/mail/stringhtmlwriter.h (limited to 'framework/mail') diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt index e7286763..0abc6880 100644 --- a/framework/mail/CMakeLists.txt +++ b/framework/mail/CMakeLists.txt @@ -5,8 +5,8 @@ set(mailplugin_SRCS singlemailcontroller.cpp folderlistmodel.cpp folderlistcontroller.cpp - filehtmlwriter.cpp objecttreesource.cpp + stringhtmlwriter.cpp ) add_library(mailplugin SHARED ${mailplugin_SRCS}) diff --git a/framework/mail/filehtmlwriter.cpp b/framework/mail/filehtmlwriter.cpp deleted file mode 100644 index e435e1be..00000000 --- a/framework/mail/filehtmlwriter.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* -*- 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 deleted file mode 100644 index 8bda39f8..00000000 --- a/framework/mail/filehtmlwriter.h +++ /dev/null @@ -1,64 +0,0 @@ -/* -*- 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 c37e2be4..d3b60187 100644 --- a/framework/mail/maillistmodel.cpp +++ b/framework/mail/maillistmodel.cpp @@ -1,6 +1,6 @@ #include "maillistmodel.h" -#include "filehtmlwriter.h" +#include "stringhtmlwriter.h" #include "objecttreesource.h" #include @@ -78,8 +78,7 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const msg->parse(); // render the mail - const QString fname("/tmp/test.html"); - FileHtmlWriter htmlWriter(fname); + StringHtmlWriter htmlWriter; QImage paintDevice; MessageViewer::CSSHelper cssHelper(&paintDevice); MessageViewer::NodeHelper nodeHelper; @@ -92,16 +91,9 @@ QVariant MailListModel::data(const QModelIndex &idx, int role) const 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"; - } + return htmlWriter.html(); } else { qWarning() << "Failed to open the file"; } diff --git a/framework/mail/stringhtmlwriter.cpp b/framework/mail/stringhtmlwriter.cpp new file mode 100644 index 00000000..2c84dc6f --- /dev/null +++ b/framework/mail/stringhtmlwriter.cpp @@ -0,0 +1,134 @@ +/* -*- 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 "stringhtmlwriter.h" + +#include +#include + +StringHtmlWriter::StringHtmlWriter() + : MessageViewer::HtmlWriter() + , mState(Ended) +{ +} + +StringHtmlWriter::~StringHtmlWriter() +{ +} + +void StringHtmlWriter::begin(const QString &css) +{ + if (mState != Ended) { + qWarning() << "begin() called on non-ended session!"; + reset(); + } + + mState = Begun; + mExtraHead.clear(); + mHtml.clear(); + + if (!css.isEmpty()) { + write(QLatin1String("\n")); + } +} + +void StringHtmlWriter::end() +{ + if (mState != Begun) { + qWarning() << "Called on non-begun or queued session!"; + } + + if (!mExtraHead.isEmpty()) { + insertExtraHead(); + mExtraHead.clear(); + } + mState = Ended; +} + +void StringHtmlWriter::reset() +{ + if (mState != Ended) { + mHtml.clear(); + mExtraHead.clear(); + mState = Begun; // don't run into end()'s warning + end(); + mState = Ended; + } +} + +void StringHtmlWriter::write(const QString &str) +{ + if (mState != Begun) { + qWarning() << "Called in Ended or Queued state!"; + } + mHtml.append(str); +} + +void StringHtmlWriter::queue(const QString &str) +{ + write(str); +} + +void StringHtmlWriter::flush() +{ + mState = Begun; // don't run into end()'s warning + end(); +} + +void StringHtmlWriter::embedPart(const QByteArray &contentId, const QString &url) +{ + write("\n"); +} +void StringHtmlWriter::extraHead(const QString &extraHead) +{ + if (mState != Ended) { + qWarning() << "Called on non-started session!"; + } + mExtraHead.append(extraHead); +} + + +void StringHtmlWriter::insertExtraHead() +{ + const QString headTag(QStringLiteral("")); + const int index = mHtml.indexOf(headTag); + if (index != -1) { + mHtml.insert(index + headTag.length(), mExtraHead); + } +} + +QString StringHtmlWriter::html() const +{ + if (mState != Ended) { + qWarning() << "Called on non-ended session!"; + } + return mHtml; +} diff --git a/framework/mail/stringhtmlwriter.h b/framework/mail/stringhtmlwriter.h new file mode 100644 index 00000000..0805d027 --- /dev/null +++ b/framework/mail/stringhtmlwriter.h @@ -0,0 +1,68 @@ +/* -*- c++ -*- + + Copyright (c) 2016 Sandro Knauß + + Kube 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. + + Kube 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 __KUBE_FRAMEWORK_MAIL_STRINGHTMLWRITER_H__ +#define __KUBE_FRAMEWORK_MAIL_STRINGHTMLWRITER_H__ + +#include + +#include +#include + +class QString; + +class StringHtmlWriter : public MessageViewer::HtmlWriter +{ +public: + explicit StringHtmlWriter(); + virtual ~StringHtmlWriter(); + + 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; + + QString html() const; +private: + void insertExtraHead(); + + QString mHtml; + QString mExtraHead; + enum State { + Begun, + Queued, + Ended + } mState; +}; + +#endif // __MESSAGEVIEWER_FILEHTMLWRITER_H__ -- cgit v1.2.3