Continue to implement fullsync
[kdepim.git] / kmail / mailserviceimpl.cpp
blob6952b95d279b210a1c75334d429f9f63fd33299a
1 /*
3 * This file is part of KMail, the KDE mail client.
4 * Copyright (c) 2003 Zack Rusin <zack@kde.org>
6 * KMail is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * KMail is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of this program with any edition of
21 * the Qt library by Trolltech AS, Norway (or with modified versions
22 * of Qt that use the same license as Qt), and distribute linked
23 * combinations including the two. You must obey the GNU General
24 * Public License in all respects for all of the code used other than
25 * Qt. If you modify this file, you may extend this exception to
26 * your version of the file, but you are not obligated to do so. If
27 * you do not wish to do so, delete this exception statement from
28 * your version.
31 #include "mailserviceimpl.h"
32 #include <serviceadaptor.h>
33 #include "editor/composer.h"
34 #include "kmkernel.h"
36 // kdepim includes
37 #include "MessageComposer/MessageHelper"
39 #include <qurl.h>
40 #include "kmail_debug.h"
42 #include <QDBusConnection>
44 namespace KMail
47 MailServiceImpl::MailServiceImpl()
49 new ServiceAdaptor(this);
50 QDBusConnection::sessionBus().registerObject(QStringLiteral("/MailTransportService"), this);
53 bool MailServiceImpl::sendMessage(const QString &from, const QString &to,
54 const QString &cc, const QString &bcc,
55 const QString &subject, const QString &body,
56 const QStringList &attachments)
58 if (to.isEmpty() && cc.isEmpty() && bcc.isEmpty()) {
59 return false;
62 KMime::Message::Ptr msg(new KMime::Message);
63 MessageHelper::initHeader(msg, KMKernel::self()->identityManager());
65 msg->contentType()->setCharset("utf-8");
67 if (!from.isEmpty()) {
68 msg->from()->fromUnicodeString(from, "utf-8");
70 if (!to.isEmpty()) {
71 msg->to()->fromUnicodeString(to, "utf-8");
73 if (!cc.isEmpty()) {
74 msg->cc()->fromUnicodeString(cc, "utf-8");
76 if (!bcc.isEmpty()) {
77 msg->bcc()->fromUnicodeString(bcc, "utf-8");
79 if (!subject.isEmpty()) {
80 msg->subject()->fromUnicodeString(subject, "utf-8");
82 if (!body.isEmpty()) {
83 msg->setBody(body.toUtf8());
86 KMail::Composer *cWin = KMail::makeComposer(msg);
88 QList<QUrl> attachUrls;
89 const int nbAttachments = attachments.count();
90 attachUrls.reserve(nbAttachments);
91 for (int i = 0; i < nbAttachments; ++i) {
92 attachUrls += QUrl::fromLocalFile(attachments[i]);
95 cWin->addAttachmentsAndSend(attachUrls, QString(), 1); //send now
96 return true;
99 bool MailServiceImpl::sendMessage(const QString &from, const QString &to,
100 const QString &cc, const QString &bcc,
101 const QString &subject, const QString &body,
102 const QByteArray &attachment)
104 if (to.isEmpty() && cc.isEmpty() && bcc.isEmpty()) {
105 return false;
108 KMime::Message::Ptr msg(new KMime::Message);
109 MessageHelper::initHeader(msg, KMKernel::self()->identityManager());
111 msg->contentType()->setCharset("utf-8");
113 if (!from.isEmpty()) {
114 msg->from()->fromUnicodeString(from, "utf-8");
116 if (!to.isEmpty()) {
117 msg->to()->fromUnicodeString(to, "utf-8");
119 if (!cc.isEmpty()) {
120 msg->cc()->fromUnicodeString(cc, "utf-8");
122 if (!bcc.isEmpty()) {
123 msg->bcc()->fromUnicodeString(bcc, "utf-8");
125 if (!subject.isEmpty()) {
126 msg->subject()->fromUnicodeString(subject, "utf-8");
128 if (!body.isEmpty()) {
129 msg->setBody(body.toUtf8());
132 KMime::Content *part = new KMime::Content;
133 part->contentTransferEncoding()->setEncoding(KMime::Headers::CEbase64);
134 part->setBody(attachment); //TODO: check it!
135 msg->addContent(part);
137 KMail::makeComposer(msg, false, false);
138 return true;
141 }//end namespace KMail