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

KDE's Doxygen guidelines are available online.