Mailcommon

mailutil.cpp
1/*
2 SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "mailutil.h"
8#include "mailutil_p.h"
9
10#include "calendarinterface.h"
11#include "filter/dialog/filteractionmissingfolderdialog.h"
12#include "folder/foldersettings.h"
13#include "job/expirejob.h"
14#include "kernel/mailkernel.h"
15#include "mailcommon_debug.h"
16#include "pop3settings.h"
17
18#include <MailImporter/FilterBalsa>
19#include <MailImporter/FilterClawsMail>
20#include <MailImporter/FilterEvolution>
21#include <MailImporter/FilterEvolution_v2>
22#include <MailImporter/FilterEvolution_v3>
23#include <MailImporter/FilterIcedove>
24#include <MailImporter/FilterOpera>
25#include <MailImporter/FilterSylpheed>
26#include <MailImporter/FilterThunderbird>
27#include <MailImporter/OtherMailerUtil>
28
29#include <MessageCore/StringUtil>
30
31#include <MessageComposer/MessageHelper>
32
33#include <Akonadi/AgentManager>
34#include <Akonadi/EntityMimeTypeFilterModel>
35#include <Akonadi/EntityTreeModel>
36#include <Akonadi/ItemFetchJob>
37#include <Akonadi/ItemFetchScope>
38#include <Akonadi/MessageParts>
39#include <Akonadi/NewMailNotifierAttribute>
40#include <Akonadi/ServerManager>
41
42#include <KMime/Message>
43
44#include <KIO/Job>
45#include <KIO/JobUiDelegate>
46#include <KJob>
47
48OrgKdeAkonadiPOP3SettingsInterface *MailCommon::Util::createPop3SettingsInterface(const QString &ident)
49{
50 const auto service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, ident);
51 return new OrgKdeAkonadiPOP3SettingsInterface(service, QStringLiteral("/Settings"), QDBusConnection::sessionBus());
52}
53
54bool MailCommon::Util::isVirtualCollection(const Akonadi::Collection &collection)
55{
56 return collection.isVirtual() || MailCommon::Util::isVirtualCollection(collection.resource());
57}
58
59bool MailCommon::Util::isVirtualCollection(const QString &resource)
60{
61 if (resource == QLatin1StringView("akonadi_search_resource")) {
62 return true;
63 }
64
65 const auto type = Akonadi::AgentManager::self()->type(resource);
66 return type.capabilities().contains(QLatin1StringView("Virtual"));
67}
68
69bool MailCommon::Util::isLocalCollection(const QString &resource)
70{
71 auto type = Akonadi::AgentManager::self()->type(resource);
72 return type.customProperties().value(QStringLiteral("HasLocalStorage"), false).toBool();
73}
74
75QString MailCommon::Util::fullCollectionPath(const Akonadi::Collection &collection, bool addAccountName)
76{
77 QString fullPath;
78
79 QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(KernelIf->collectionModel(), collection);
80 if (!idx.isValid()) {
81 return fullPath;
82 }
83
84 fullPath = idx.data().toString();
85 idx = idx.parent();
86 while (idx != QModelIndex()) {
87 const QString tmp = idx.data().toString() + QLatin1Char('/') + fullPath;
88 idx = idx.parent();
89 if (idx != QModelIndex()) {
90 fullPath = tmp;
91 } else {
92 if (!addAccountName) {
93 break;
94 } else {
95 fullPath = tmp;
96 break;
97 }
98 }
99 }
100 return fullPath;
101}
102
103QString MailCommon::Util::fullCollectionRemoveIdPath(const Akonadi::Collection &collection, bool addAccountName)
104{
105 QString fullPath;
106
107 QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(KernelIf->collectionModel(), collection);
108 if (!idx.isValid()) {
109 return fullPath;
110 }
111
112 fullPath = idx.data().toString();
113 idx = idx.parent();
114 while (idx != QModelIndex()) {
115 const QString tmp = idx.data(Akonadi::EntityTreeModel::RemoteIdRole).toString() + QLatin1Char('/') + fullPath;
116 idx = idx.parent();
117 if (idx != QModelIndex()) {
118 fullPath = tmp;
119 } else {
120 if (!addAccountName) {
121 break;
122 } else {
123 fullPath = tmp;
124 break;
125 }
126 }
127 }
128 return fullPath;
129}
130
131bool MailCommon::Util::showJobErrorMessage(KJob *job)
132{
133 if (job->error()) {
134 if (auto uidelegate = static_cast<KIO::Job *>(job)->uiDelegate()) {
135 uidelegate->showErrorMessage();
136 } else {
137 qCDebug(MAILCOMMON_LOG) << " job->errorString() :" << job->errorString();
138 }
139 return true;
140 }
141 return false;
142}
143
144Akonadi::AgentInstance::List MailCommon::Util::agentInstances(bool excludeMailDispacher)
145{
146 Akonadi::AgentInstance::List relevantInstances;
148 std::copy_if(agentList.cbegin(), agentList.cend(), std::back_inserter(relevantInstances), [excludeMailDispacher](const Akonadi::AgentInstance &instance) {
149 return isMailAgent(instance, excludeMailDispacher);
150 });
151 return relevantInstances;
152}
153
154bool MailCommon::Util::isMailAgent(const Akonadi::AgentInstance &instance, bool excludeMailTransport)
155{
156 if (!instance.type().mimeTypes().contains(KMime::Message::mimeType())) {
157 return false;
158 }
159
160 const QStringList capabilities(instance.type().capabilities());
161 if (capabilities.contains(QLatin1StringView("Resource")) && !capabilities.contains(QLatin1StringView("Virtual"))
162 && !capabilities.contains(QLatin1StringView("MailTransport")) && !capabilities.contains(QLatin1StringView("Autostart"))) {
163 return true;
164 } else if (!excludeMailTransport && instance.identifier() == QLatin1StringView("akonadi_maildispatcher_agent")) {
165 return true;
166 }
167
168 return false;
169}
170
171bool MailCommon::Util::isUnifiedMailboxesAgent(const Akonadi::Collection &col)
172{
173 return col.resource() == QLatin1StringView("akonadi_unifiedmailbox_agent");
174}
175
177{
178 uint id = 0;
179 if (item.isValid() && item.parentCollection().isValid()) {
181 if (col.resource().isEmpty()) {
182 col = CommonKernel->collectionFromId(col.id());
183 }
184 const QSharedPointer<FolderSettings> fd = FolderSettings::forCollection(col, false);
185
186 id = fd->identity();
187 }
188 return id;
189}
190
191bool MailCommon::Util::ignoreNewMailInFolder(const Akonadi::Collection &collection)
192{
193 if (collection.hasAttribute<Akonadi::NewMailNotifierAttribute>()) {
194 if (collection.attribute<Akonadi::NewMailNotifierAttribute>()->ignoreNewMail()) {
195 return true;
196 }
197 }
198 return false;
199}
200
201QString MailCommon::Util::realFolderPath(const QString &path)
202{
203 QString realPath(path);
204 realPath.remove(QStringLiteral(".directory"));
205 realPath.replace(QLatin1StringView("/."), QStringLiteral("/"));
206 if (!realPath.isEmpty() && (realPath.at(0) == QLatin1Char('.'))) {
207 realPath.remove(0, 1); // remove first "."
208 }
209 return realPath;
210}
211
212void MailCommon::Util::expireOldMessages(const Akonadi::Collection &collection, bool immediate)
213{
214 auto task = new ScheduledExpireTask(collection, immediate);
215 KernelIf->jobScheduler()->registerTask(task);
216}
217
218Akonadi::Collection::Id MailCommon::Util::convertFolderPathToCollectionId(const QString &folder)
219{
220 Akonadi::Collection::Id newFolderId = -1;
221 bool exactPath = false;
222 const Akonadi::Collection::List lst = FilterActionMissingFolderDialog::potentialCorrectFolders(folder, exactPath);
223 if (lst.count() == 1 && exactPath) {
224 newFolderId = lst.at(0).id();
225 } else {
226 QPointer<FilterActionMissingFolderDialog> dlg = new FilterActionMissingFolderDialog(lst, QString(), folder);
227 if (dlg->exec()) {
228 newFolderId = dlg->selectedCollection().id();
229 }
230 delete dlg;
231 }
232 return newFolderId;
233}
234
235QString MailCommon::Util::convertFolderPathToCollectionStr(const QString &folder)
236{
237 Akonadi::Collection::Id newFolderId = MailCommon::Util::convertFolderPathToCollectionId(folder);
238 if (newFolderId == -1) {
239 return {};
240 }
241 return QString::number(newFolderId);
242}
243
244void MailCommon::Util::foundMailer(QStringList &lst, const QString &name)
245{
246 if (!name.isEmpty()) {
247 lst.append(name);
248 }
249}
250
251QStringList MailCommon::Util::foundMailer()
252{
253 QStringList lst;
254 MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution::isMailerFound());
255 MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution_v2::isMailerFound());
256 MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution_v3::isMailerFound());
257 MailCommon::Util::foundMailer(lst, MailImporter::FilterBalsa::isMailerFound());
258 MailCommon::Util::foundMailer(lst, MailImporter::FilterClawsMail::isMailerFound());
259 MailCommon::Util::foundMailer(lst, MailImporter::FilterOpera::isMailerFound());
260 MailCommon::Util::foundMailer(lst, MailImporter::FilterSylpheed::isMailerFound());
261 MailCommon::Util::foundMailer(lst, MailImporter::FilterThunderbird::isMailerFound());
262 MailCommon::Util::foundMailer(lst, MailImporter::FilterIcedove::isMailerFound());
263 const QStringList otherMailer = MailImporter::OtherMailerUtil::isMailerFound();
264 if (!otherMailer.isEmpty()) {
265 lst << otherMailer;
266 }
267 return lst;
268}
QString identifier() const
AgentType type() const
AgentType type(const QString &identifier) const
static AgentManager * self()
AgentInstance::List instances() const
QStringList mimeTypes() const
QStringList capabilities() const
QString resource() const
bool isValid() const
const T * attribute() const
bool hasAttribute() const
bool isVirtual() const
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
Collection & parentCollection()
bool isValid() const
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
virtual QString errorString() const
int error() const
static QString mimeType()
Type type(const QSqlDatabase &db)
Capabilities capabilities()
QString name(StandardAction id)
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition mailutil.cpp:176
QDBusConnection sessionBus()
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
const_iterator cbegin() const const
const_iterator cend() const const
qsizetype count() const const
bool isEmpty() const const
QVariant data(int role) const const
bool isValid() const const
QModelIndex parent() const const
bool isEmpty() const const
QString number(double n, char format, int precision)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QTaskBuilder< Task > task(Task &&task)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.