From a97fd7275bbd949b0e62a689db3e0e10c515a9a4 Mon Sep 17 00:00:00 2001 From: Montel Laurent Date: Thu, 9 Feb 2017 07:22:34 +0100 Subject: [PATCH] Extract other job --- src/CMakeLists.txt | 1 + src/job/newmessagejob.cpp | 87 +++++++++++++++++++++++++++++++++++++++ src/job/newmessagejob.h | 77 ++++++++++++++++++++++++++++++++++ src/kmkernel.cpp | 6 +-- 4 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 src/job/newmessagejob.cpp create mode 100644 src/job/newmessagejob.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 28bdd7576..4c2a02681 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -131,6 +131,7 @@ set(kmailprivate_job_LIB_SRCS job/handleclickedurljob.cpp job/composenewmessagejob.cpp job/opencomposerjob.cpp + job/newmessagejob.cpp ) set(kmailprivate_widgets_LIB_SRCS diff --git a/src/job/newmessagejob.cpp b/src/job/newmessagejob.cpp new file mode 100644 index 000000000..a7fd3af7e --- /dev/null +++ b/src/job/newmessagejob.cpp @@ -0,0 +1,87 @@ +/* + Copyright (C) 2017 Laurent Montel + + 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 "newmessagejob.h" +#include "config-kmail.h" +#include "kmkernel.h" +#include "composer.h" +#include "editor/kmcomposerwin.h" + +#include +#include +//#ifdef KDEPIM_TEMPLATEPARSER_ASYNC_BUILD +//#include +//#else +#include +//#endif + +NewMessageJob::NewMessageJob(QObject *parent) + : QObject(parent), + mMsg(nullptr) +{ + +} + +NewMessageJob::~NewMessageJob() +{ + +} + +void NewMessageJob::start() +{ + QUrl attachURL = QUrl::fromLocalFile(mNewMessageJobSettings.mAttachURL); + mMsg = KMime::Message::Ptr(new KMime::Message); + MessageHelper::initHeader(mMsg, KMKernel::self()->identityManager(), mNewMessageJobSettings.mIdentity); + mMsg->contentType()->setCharset("utf-8"); + //set basic headers + if (!mNewMessageJobSettings.mCc.isEmpty()) { + mMsg->cc()->fromUnicodeString(mNewMessageJobSettings.mCc, "utf-8"); + } + if (!mNewMessageJobSettings.mBcc.isEmpty()) { + mMsg->bcc()->fromUnicodeString(mNewMessageJobSettings.mBcc, "utf-8"); + } + if (!mNewMessageJobSettings.mTo.isEmpty()) { + mMsg->to()->fromUnicodeString(mNewMessageJobSettings.mTo, "utf-8"); + } + + mMsg->assemble(); + TemplateParser::TemplateParser parser(mMsg, TemplateParser::TemplateParser::NewMessage); + parser.setIdentityManager(KMKernel::self()->identityManager()); + Akonadi::Collection col = mNewMessageJobSettings.mFolder ? mNewMessageJobSettings.mFolder->collection() : Akonadi::Collection(); + parser.process(mMsg, col); + + KMail::Composer *win = makeComposer(mMsg, false, false, KMail::Composer::New, mNewMessageJobSettings.mIdentity); + + win->setCollectionForNewMessage(col); + //Add the attachment if we have one + if (!attachURL.isEmpty() && attachURL.isValid()) { + win->addAttachment(attachURL, QString()); + } + + //only show window when required + if (!mNewMessageJobSettings.mHidden) { + win->show(); + } + deleteLater(); +} + +void NewMessageJob::setNewMessageJobSettings(const NewMessageJobSettings &newMessageJobSettings) +{ + mNewMessageJobSettings = newMessageJobSettings; +} diff --git a/src/job/newmessagejob.h b/src/job/newmessagejob.h new file mode 100644 index 000000000..06ecbbe25 --- /dev/null +++ b/src/job/newmessagejob.h @@ -0,0 +1,77 @@ +/* + Copyright (C) 2017 Laurent Montel + + 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. +*/ +#ifndef NEWMESSAGEJOB_H +#define NEWMESSAGEJOB_H + +#include +#include +#include + +struct NewMessageJobSettings +{ + NewMessageJobSettings() + : mFolder(nullptr), + mHidden(false), + mIdentity(0) + { + + } + NewMessageJobSettings(const QString &to, + const QString &cc, + const QString &bcc, + bool hidden, + const QString &attachURL, + const QSharedPointer &folder, + uint identity) + : mTo(to), + mCc(cc), + mBcc(bcc), + mAttachURL(attachURL), + mFolder(folder), + mHidden(hidden), + mIdentity(identity) + + { + + } + QString mTo; + QString mCc; + QString mBcc; + QString mAttachURL; + QSharedPointer mFolder; + bool mHidden; + uint mIdentity; +}; + +class NewMessageJob : public QObject +{ + Q_OBJECT +public: + explicit NewMessageJob(QObject *parent = nullptr); + ~NewMessageJob(); + void start(); + + void setNewMessageJobSettings(const NewMessageJobSettings &newMessageJobSettings); + +private: + NewMessageJobSettings mNewMessageJobSettings; + KMime::Message::Ptr mMsg; +}; + +#endif // NEWMESSAGEJOB_H diff --git a/src/kmkernel.cpp b/src/kmkernel.cpp index 5dcfc210b..8075c8f2f 100644 --- a/src/kmkernel.cpp +++ b/src/kmkernel.cpp @@ -796,16 +796,16 @@ void KMKernel::newMessage(const QString &to, const QString & /*messageFile*/, const QString &_attachURL) { - QUrl attachURL = QUrl::fromLocalFile(_attachURL); - KMime::Message::Ptr msg(new KMime::Message); QSharedPointer folder; uint id = 0; - if (useFolderId) { //create message with required folder identity folder = currentFolderCollection(); id = folder ? folder->identity() : 0; } + + QUrl attachURL = QUrl::fromLocalFile(_attachURL); + KMime::Message::Ptr msg(new KMime::Message); MessageHelper::initHeader(msg, identityManager(), id); msg->contentType()->setCharset("utf-8"); //set basic headers