File indexing completed on 2024-05-12 05:20:45

0001 /*
0002  *
0003  *  This file is part of KMail, the KDE mail client.
0004  *  SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-only
0007  */
0008 
0009 #include "mailserviceimpl.h"
0010 #include "editor/composer.h"
0011 #include "kmkernel.h"
0012 #include <serviceadaptor.h>
0013 
0014 // kdepim includes
0015 #include <MessageComposer/MessageHelper>
0016 
0017 #include <QUrl>
0018 
0019 #include <QDBusConnection>
0020 
0021 using namespace KMail;
0022 MailServiceImpl::MailServiceImpl(QObject *parent)
0023     : QObject(parent)
0024 {
0025     new ServiceAdaptor(this);
0026     QDBusConnection::sessionBus().registerObject(QStringLiteral("/MailTransportService"), this);
0027 }
0028 
0029 bool MailServiceImpl::sendMessage(const QString &from,
0030                                   const QString &to,
0031                                   const QString &cc,
0032                                   const QString &bcc,
0033                                   const QString &subject,
0034                                   const QString &body,
0035                                   const QStringList &attachments)
0036 {
0037     if (to.isEmpty() && cc.isEmpty() && bcc.isEmpty()) {
0038         return false;
0039     }
0040 
0041     KMime::Message::Ptr msg(new KMime::Message);
0042     MessageHelper::initHeader(msg, KMKernel::self()->identityManager());
0043 
0044     msg->contentType()->setCharset("utf-8");
0045 
0046     if (!from.isEmpty()) {
0047         msg->from()->fromUnicodeString(from, "utf-8");
0048     }
0049     if (!to.isEmpty()) {
0050         msg->to()->fromUnicodeString(to, "utf-8");
0051     }
0052     if (!cc.isEmpty()) {
0053         msg->cc()->fromUnicodeString(cc, "utf-8");
0054     }
0055     if (!bcc.isEmpty()) {
0056         msg->bcc()->fromUnicodeString(bcc, "utf-8");
0057     }
0058     if (!subject.isEmpty()) {
0059         msg->subject()->fromUnicodeString(subject, "utf-8");
0060     }
0061     if (!body.isEmpty()) {
0062         msg->setBody(body.toUtf8());
0063     }
0064 
0065     KMail::Composer *cWin = KMail::makeComposer(msg);
0066 
0067     QList<QUrl> attachUrls;
0068     const int nbAttachments = attachments.count();
0069     attachUrls.reserve(nbAttachments);
0070     for (int i = 0; i < nbAttachments; ++i) {
0071         attachUrls += QUrl::fromLocalFile(attachments[i]);
0072     }
0073 
0074     cWin->addAttachmentsAndSend(attachUrls, QString(), 1); // send now
0075     return true;
0076 }
0077 
0078 bool MailServiceImpl::sendMessage(const QString &from,
0079                                   const QString &to,
0080                                   const QString &cc,
0081                                   const QString &bcc,
0082                                   const QString &subject,
0083                                   const QString &body,
0084                                   const QByteArray &attachment)
0085 {
0086     if (to.isEmpty() && cc.isEmpty() && bcc.isEmpty()) {
0087         return false;
0088     }
0089 
0090     KMime::Message::Ptr msg(new KMime::Message);
0091     MessageHelper::initHeader(msg, KMKernel::self()->identityManager());
0092 
0093     msg->contentType()->setCharset("utf-8");
0094 
0095     if (!from.isEmpty()) {
0096         msg->from()->fromUnicodeString(from, "utf-8");
0097     }
0098     if (!to.isEmpty()) {
0099         msg->to()->fromUnicodeString(to, "utf-8");
0100     }
0101     if (!cc.isEmpty()) {
0102         msg->cc()->fromUnicodeString(cc, "utf-8");
0103     }
0104     if (!bcc.isEmpty()) {
0105         msg->bcc()->fromUnicodeString(bcc, "utf-8");
0106     }
0107     if (!subject.isEmpty()) {
0108         msg->subject()->fromUnicodeString(subject, "utf-8");
0109     }
0110     if (!body.isEmpty()) {
0111         msg->setBody(body.toUtf8());
0112     }
0113 
0114     auto part = new KMime::Content;
0115     part->contentTransferEncoding()->setEncoding(KMime::Headers::CEbase64);
0116     part->setBody(attachment); // TODO: check it!
0117     msg->appendContent(part);
0118 
0119     KMail::makeComposer(msg, false, false);
0120     return true;
0121 }
0122 
0123 #include "moc_mailserviceimpl.cpp"