Move away from KMime::Headers::Generic overloaded ctors.
[kdepim.git] / kmail / util.cpp
blob2ddcd728ae4449e20fca860be4589cfb27d0a509
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/utils/stringutil.h"
42 #include "messagecomposer/helper/messagehelper.h"
44 #include "templateparser/templateparser.h"
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 "foldercollection.h"
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 addresses << MessageCore::StringUtil::mailboxFromUnicodeString(fd->mailingListPostAddress());
73 return addresses;
76 Akonadi::Item::Id KMail::Util::putRepliesInSameFolder(const Akonadi::Item &item)
78 Akonadi::Collection parentCollection = item.parentCollection();
79 if (parentCollection.isValid()) {
80 const QSharedPointer<FolderCollection> fd = FolderCollection::forCollection(parentCollection, false);
81 if (fd->putRepliesInSameFolder()) {
82 return parentCollection.id();
85 return -1;
88 bool KMail::Util::handleClickedURL(const KUrl &url, const QSharedPointer<MailCommon::FolderCollection> &folder)
90 if (url.scheme() == QLatin1String("mailto")) {
91 KMime::Message::Ptr msg(new KMime::Message);
92 uint identity = !folder.isNull() ? folder->identity() : 0;
93 MessageHelper::initHeader(msg, KMKernel::self()->identityManager(), identity);
94 msg->contentType()->setCharset("utf-8");
96 QMap<QString, QString> fields = MessageCore::StringUtil::parseMailtoUrl(url);
98 msg->to()->fromUnicodeString(fields.value(QStringLiteral("to")), "utf-8");
99 if (!fields.value(QStringLiteral("subject")).isEmpty()) {
100 msg->subject()->fromUnicodeString(fields.value(QStringLiteral("subject")), "utf-8");
102 if (!fields.value(QStringLiteral("body")).isEmpty()) {
103 msg->setBody(fields.value(QStringLiteral("body")).toUtf8());
105 if (!fields.value(QStringLiteral("cc")).isEmpty()) {
106 msg->cc()->fromUnicodeString(fields.value(QStringLiteral("cc")), "utf-8");
109 if (!folder.isNull()) {
110 TemplateParser::TemplateParser parser(msg, TemplateParser::TemplateParser::NewMessage);
111 parser.setIdentityManager(KMKernel::self()->identityManager());
112 parser.process(msg, folder->collection());
115 KMail::Composer *win = KMail::makeComposer(msg, false, false, KMail::Composer::New, identity);
116 win->setFocusToSubject();
117 if (!folder.isNull()) {
118 win->setCollectionForNewMessage(folder->collection());
120 win->show();
121 return true;
122 } else {
123 qCWarning(KMAIL_LOG) << "Can't handle URL:" << url;
124 return false;
128 bool KMail::Util::mailingListsHandleURL(const QList<QUrl> &lst, const QSharedPointer<MailCommon::FolderCollection> &folder)
130 const QString handler = (folder->mailingList().handler() == MailingList::KMail)
131 ? QStringLiteral("mailto") : QStringLiteral("https");
133 QUrl urlToHandle;
134 QList<QUrl>::ConstIterator end(lst.constEnd());
135 for (QList<QUrl>::ConstIterator itr = lst.constBegin(); itr != end; ++itr) {
136 if (handler == (*itr).scheme()) {
137 urlToHandle = *itr;
138 break;
141 if (urlToHandle.isEmpty() && !lst.empty()) {
142 urlToHandle = lst.first();
145 if (!urlToHandle.isEmpty()) {
146 return KMail::Util::handleClickedURL(urlToHandle, folder);
147 } else {
148 qCWarning(KMAIL_LOG) << "Can't handle url";
149 return false;
153 bool KMail::Util::mailingListPost(const QSharedPointer<MailCommon::FolderCollection> &fd)
155 if (fd) {
156 return KMail::Util::mailingListsHandleURL(fd->mailingList().postUrls(), fd);
158 return false;
161 bool KMail::Util::mailingListSubscribe(const QSharedPointer<MailCommon::FolderCollection> &fd)
163 if (fd) {
164 return KMail::Util::mailingListsHandleURL(fd->mailingList().subscribeUrls(), fd);
166 return false;
169 bool KMail::Util::mailingListUnsubscribe(const QSharedPointer<MailCommon::FolderCollection> &fd)
171 if (fd) {
172 return KMail::Util::mailingListsHandleURL(fd->mailingList().unsubscribeUrls(), fd);
174 return false;
177 bool KMail::Util::mailingListArchives(const QSharedPointer<MailCommon::FolderCollection> &fd)
179 if (fd) {
180 return KMail::Util::mailingListsHandleURL(fd->mailingList().archiveUrls(), fd);
182 return false;
185 bool KMail::Util::mailingListHelp(const QSharedPointer<MailCommon::FolderCollection> &fd)
187 if (fd) {
188 return KMail::Util::mailingListsHandleURL(fd->mailingList().helpUrls(), fd);
190 return false;
193 void KMail::Util::lastEncryptAndSignState(bool &lastEncrypt, bool &lastSign, const KMime::Message::Ptr &msg)
195 lastSign = KMime::isSigned(msg.data());
196 lastEncrypt = KMime::isEncrypted(msg.data());
199 QColor KMail::Util::misspelledColor()
201 return QColor(Qt::red);
204 QColor KMail::Util::quoteL1Color()
206 return QColor(0x00, 0x80, 0x00);
209 QColor KMail::Util::quoteL2Color()
211 return QColor(0x00, 0x70, 0x00);
214 QColor KMail::Util::quoteL3Color()
216 return QColor(0x00, 0x60, 0x00);
219 void KMail::Util::addQActionHelpText(QAction *action, const QString &text)
221 action->setStatusTip(text);
222 action->setToolTip(text);
223 if (action->whatsThis().isEmpty()) {
224 action->setWhatsThis(text);