Clazy fix++
[kdepim.git] / kmail / util.cpp
blob59df18670f3fc50d60c5953089fa16726604da5d
1 /*******************************************************************************
2 **
3 ** Filename : util
4 ** Created on : 03 April, 2005
5 ** Copyright : (c) 2005 Till Adam
6 ** Email : <adam@kde.org>
7 **
8 *******************************************************************************/
10 /*******************************************************************************
12 ** This program is free software; you can redistribute it and/or modify
13 ** it under the terms of the GNU General Public License as published by
14 ** the Free Software Foundation; either version 2 of the License, or
15 ** (at your option) any later version.
17 ** It is distributed in the hope that it will be useful, but
18 ** WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
22 ** You should have received a copy of the GNU General Public License
23 ** along with this program; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 ** In addition, as a special exception, the copyright holders give
27 ** permission to link the code of this program with any edition of
28 ** the Qt library by Trolltech AS, Norway (or with modified versions
29 ** of Qt that use the same license as Qt), and distribute linked
30 ** combinations including the two. You must obey the GNU General
31 ** Public License in all respects for all of the code used other than
32 ** Qt. If you modify this file, you may extend this exception to
33 ** your version of the file, but you are not obligated to do so. If
34 ** you do not wish to do so, delete this exception statement from
35 ** your version.
37 *******************************************************************************/
38 #include "util.h"
39 #include "kmkernel.h"
41 #include "MessageCore/StringUtil"
42 #include "MessageComposer/MessageHelper"
44 #include "TemplateParser/TemplateParser"
46 #include <kmime/kmime_message.h>
47 #include <kmessagebox.h>
48 #include <KLocalizedString>
49 #include <KProcess>
50 #include "kmail_debug.h"
52 #include <QProcess>
53 #include <QFileInfo>
54 #include <QAction>
55 #include <QStandardPaths>
57 #include "MailCommon/FolderCollection"
59 using namespace MailCommon;
61 KMime::Types::Mailbox::List KMail::Util::mailingListsFromMessage(const Akonadi::Item &item)
63 KMime::Types::Mailbox::List addresses;
64 // determine the mailing list posting address
65 Akonadi::Collection parentCollection = item.parentCollection();
66 if (parentCollection.isValid()) {
67 const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection(parentCollection, false);
68 if (fd->isMailingListEnabled() && !fd->mailingListPostAddress().isEmpty()) {
69 KMime::Types::Mailbox mailbox;
70 mailbox.fromUnicodeString(fd->mailingListPostAddress());
71 addresses << mailbox;
75 return addresses;
78 Akonadi::Item::Id KMail::Util::putRepliesInSameFolder(const Akonadi::Item &item)
80 Akonadi::Collection parentCollection = item.parentCollection();
81 if (parentCollection.isValid()) {
82 const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection(parentCollection, false);
83 if (fd->putRepliesInSameFolder()) {
84 return parentCollection.id();
87 return -1;
90 bool KMail::Util::handleClickedURL(const QUrl &url, const QSharedPointer<MailCommon::FolderCollection> &folder)
92 if (url.scheme() == QLatin1String("mailto")) {
93 KMime::Message::Ptr msg(new KMime::Message);
94 uint identity = !folder.isNull() ? folder->identity() : 0;
95 MessageHelper::initHeader(msg, KMKernel::self()->identityManager(), identity);
96 msg->contentType()->setCharset("utf-8");
98 QMap<QString, QString> fields = MessageCore::StringUtil::parseMailtoUrl(url);
100 msg->to()->fromUnicodeString(fields.value(QStringLiteral("to")), "utf-8");
101 if (!fields.value(QStringLiteral("subject")).isEmpty()) {
102 msg->subject()->fromUnicodeString(fields.value(QStringLiteral("subject")), "utf-8");
104 if (!fields.value(QStringLiteral("body")).isEmpty()) {
105 msg->setBody(fields.value(QStringLiteral("body")).toUtf8());
107 if (!fields.value(QStringLiteral("cc")).isEmpty()) {
108 msg->cc()->fromUnicodeString(fields.value(QStringLiteral("cc")), "utf-8");
111 if (!folder.isNull()) {
112 TemplateParser::TemplateParser parser(msg, TemplateParser::TemplateParser::NewMessage);
113 parser.setIdentityManager(KMKernel::self()->identityManager());
114 parser.process(msg, folder->collection());
117 KMail::Composer *win = KMail::makeComposer(msg, false, false, KMail::Composer::New, identity);
118 win->setFocusToSubject();
119 if (!folder.isNull()) {
120 win->setCollectionForNewMessage(folder->collection());
122 win->show();
123 return true;
124 } else {
125 qCWarning(KMAIL_LOG) << "Can't handle URL:" << url;
126 return false;
130 bool KMail::Util::mailingListsHandleURL(const QList<QUrl> &lst, const QSharedPointer<MailCommon::FolderCollection> &folder)
132 const QString handler = (folder->mailingList().handler() == MailingList::KMail)
133 ? QStringLiteral("mailto") : QStringLiteral("https");
135 QUrl urlToHandle;
136 QList<QUrl>::ConstIterator end(lst.constEnd());
137 for (QList<QUrl>::ConstIterator itr = lst.constBegin(); itr != end; ++itr) {
138 if (handler == (*itr).scheme()) {
139 urlToHandle = *itr;
140 break;
143 if (urlToHandle.isEmpty() && !lst.empty()) {
144 urlToHandle = lst.first();
147 if (!urlToHandle.isEmpty()) {
148 return KMail::Util::handleClickedURL(urlToHandle, folder);
149 } else {
150 qCWarning(KMAIL_LOG) << "Can't handle url";
151 return false;
155 bool KMail::Util::mailingListPost(const QSharedPointer<MailCommon::FolderCollection> &fd)
157 if (fd) {
158 return KMail::Util::mailingListsHandleURL(fd->mailingList().postUrls(), fd);
160 return false;
163 bool KMail::Util::mailingListSubscribe(const QSharedPointer<MailCommon::FolderCollection> &fd)
165 if (fd) {
166 return KMail::Util::mailingListsHandleURL(fd->mailingList().subscribeUrls(), fd);
168 return false;
171 bool KMail::Util::mailingListUnsubscribe(const QSharedPointer<MailCommon::FolderCollection> &fd)
173 if (fd) {
174 return KMail::Util::mailingListsHandleURL(fd->mailingList().unsubscribeUrls(), fd);
176 return false;
179 bool KMail::Util::mailingListArchives(const QSharedPointer<MailCommon::FolderCollection> &fd)
181 if (fd) {
182 return KMail::Util::mailingListsHandleURL(fd->mailingList().archiveUrls(), fd);
184 return false;
187 bool KMail::Util::mailingListHelp(const QSharedPointer<MailCommon::FolderCollection> &fd)
189 if (fd) {
190 return KMail::Util::mailingListsHandleURL(fd->mailingList().helpUrls(), fd);
192 return false;
195 void KMail::Util::lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr &msg)
197 lastSign = KMime::isSigned(msg.data());
198 lastEncrypt = KMime::isEncrypted(msg.data());
201 void KMail::Util::addQActionHelpText(QAction *action, const QString &text)
203 action->setStatusTip(text);
204 action->setToolTip(text);
205 if (action->whatsThis().isEmpty()) {
206 action->setWhatsThis(text);