Mailcommon

mailutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2005 Till Adam <[email protected]>
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 <KIO/JobUiDelegate>
45 #include <KJob>
46 #include <collectionpage/attributes/expirecollectionattribute.h>
47 
48 OrgKdeAkonadiPOP3SettingsInterface *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 
54 bool MailCommon::Util::isVirtualCollection(const Akonadi::Collection &collection)
55 {
56  return collection.isVirtual() || MailCommon::Util::isVirtualCollection(collection.resource());
57 }
58 
59 bool MailCommon::Util::isVirtualCollection(const QString &resource)
60 {
61  if (resource == QLatin1String("akonadi_search_resource")) {
62  return true;
63  }
64 
65  const auto type = Akonadi::AgentManager::self()->type(resource);
66  return type.capabilities().contains(QLatin1String("Virtual"));
67 }
68 
69 bool 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 
75 QString 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 
103 bool MailCommon::Util::showJobErrorMessage(KJob *job)
104 {
105  if (job->error()) {
106  if (auto uidelegate = static_cast<KIO::Job *>(job)->uiDelegate()) {
107  uidelegate->showErrorMessage();
108  } else {
109  qCDebug(MAILCOMMON_LOG) << " job->errorString() :" << job->errorString();
110  }
111  return true;
112  }
113  return false;
114 }
115 
116 Akonadi::AgentInstance::List MailCommon::Util::agentInstances(bool excludeMailDispacher)
117 {
118  Akonadi::AgentInstance::List relevantInstances;
120  std::copy_if(agentList.cbegin(), agentList.cend(), std::back_inserter(relevantInstances), [excludeMailDispacher](const Akonadi::AgentInstance &instance) {
121  return isMailAgent(instance, excludeMailDispacher);
122  });
123  return relevantInstances;
124 }
125 
126 bool MailCommon::Util::isMailAgent(const Akonadi::AgentInstance &instance, bool excludeMailTransport)
127 {
128  if (!instance.type().mimeTypes().contains(KMime::Message::mimeType())) {
129  return false;
130  }
131 
132  const QStringList capabilities(instance.type().capabilities());
133  if (capabilities.contains(QLatin1String("Resource")) && !capabilities.contains(QLatin1String("Virtual"))
134  && !capabilities.contains(QLatin1String("MailTransport")) && !capabilities.contains(QLatin1String("Autostart"))) {
135  return true;
136  } else if (!excludeMailTransport && instance.identifier() == QLatin1String("akonadi_maildispatcher_agent")) {
137  return true;
138  }
139 
140  return false;
141 }
142 
143 bool MailCommon::Util::isUnifiedMailboxesAgent(const Akonadi::Collection &col)
144 {
145  return col.resource() == QLatin1String("akonadi_unifiedmailbox_agent");
146 }
147 
149 {
150  uint id = 0;
151  if (item.isValid() && item.parentCollection().isValid()) {
153  if (col.resource().isEmpty()) {
154  col = CommonKernel->collectionFromId(col.id());
155  }
156  const QSharedPointer<FolderSettings> fd = FolderSettings::forCollection(col, false);
157 
158  id = fd->identity();
159  }
160  return id;
161 }
162 
163 bool MailCommon::Util::ignoreNewMailInFolder(const Akonadi::Collection &collection)
164 {
165  if (collection.hasAttribute<Akonadi::NewMailNotifierAttribute>()) {
166  if (collection.attribute<Akonadi::NewMailNotifierAttribute>()->ignoreNewMail()) {
167  return true;
168  }
169  }
170  return false;
171 }
172 
173 QString MailCommon::Util::realFolderPath(const QString &path)
174 {
175  QString realPath(path);
176  realPath.remove(QStringLiteral(".directory"));
177  realPath.replace(QLatin1String("/."), QStringLiteral("/"));
178  if (!realPath.isEmpty() && (realPath.at(0) == QLatin1Char('.'))) {
179  realPath.remove(0, 1); // remove first "."
180  }
181  return realPath;
182 }
183 
184 void MailCommon::Util::expireOldMessages(const Akonadi::Collection &collection, bool immediate)
185 {
186  auto task = new ScheduledExpireTask(collection, immediate);
187  KernelIf->jobScheduler()->registerTask(task);
188 }
189 
190 Akonadi::Collection::Id MailCommon::Util::convertFolderPathToCollectionId(const QString &folder)
191 {
192  Akonadi::Collection::Id newFolderId = -1;
193  bool exactPath = false;
194  const Akonadi::Collection::List lst = FilterActionMissingFolderDialog::potentialCorrectFolders(folder, exactPath);
195  if (lst.count() == 1 && exactPath) {
196  newFolderId = lst.at(0).id();
197  } else {
198  QPointer<FilterActionMissingFolderDialog> dlg = new FilterActionMissingFolderDialog(lst, QString(), folder);
199  if (dlg->exec()) {
200  newFolderId = dlg->selectedCollection().id();
201  }
202  delete dlg;
203  }
204  return newFolderId;
205 }
206 
207 QString MailCommon::Util::convertFolderPathToCollectionStr(const QString &folder)
208 {
209  Akonadi::Collection::Id newFolderId = MailCommon::Util::convertFolderPathToCollectionId(folder);
210  if (newFolderId == -1) {
211  return {};
212  }
213  return QString::number(newFolderId);
214 }
215 
216 void MailCommon::Util::foundMailer(QStringList &lst, const QString &name)
217 {
218  if (!name.isEmpty()) {
219  lst.append(name);
220  }
221 }
222 
223 QStringList MailCommon::Util::foundMailer()
224 {
225  QStringList lst;
226  MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution::isMailerFound());
227  MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution_v2::isMailerFound());
228  MailCommon::Util::foundMailer(lst, MailImporter::FilterEvolution_v3::isMailerFound());
229  MailCommon::Util::foundMailer(lst, MailImporter::FilterBalsa::isMailerFound());
230  MailCommon::Util::foundMailer(lst, MailImporter::FilterClawsMail::isMailerFound());
231  MailCommon::Util::foundMailer(lst, MailImporter::FilterOpera::isMailerFound());
232  MailCommon::Util::foundMailer(lst, MailImporter::FilterSylpheed::isMailerFound());
233  MailCommon::Util::foundMailer(lst, MailImporter::FilterThunderbird::isMailerFound());
234  MailCommon::Util::foundMailer(lst, MailImporter::FilterIcedove::isMailerFound());
235  const QStringList otherMailer = MailImporter::OtherMailerUtil::isMailerFound();
236  if (!otherMailer.isEmpty()) {
237  lst << otherMailer;
238  }
239  return lst;
240 }
bool isValid() const
void append(const T &value)
QString identifier() const
QString number(int n, int base)
AgentType type() const
Type type(const QSqlDatabase &db)
QVector::const_iterator cend() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition: mailutil.cpp:148
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
bool hasAttribute() const
QStringList capabilities() const
QVariant data(int role) const const
static QString mimeType()
QDBusConnection sessionBus()
QVector::const_iterator cbegin() const const
const T & at(int i) const const
bool isEmpty() const const
const T * attribute() const
bool isEmpty() const const
bool isValid() const const
AgentType type(const QString &identifier) const
Collection & parentCollection()
Capabilities capabilities()
QString name(StandardShortcut id)
bool isVirtual() const
QString resource() const
static AgentManager * self()
int count(const T &value) const const
static QModelIndex modelIndexForCollection(const QAbstractItemModel *model, const Collection &collection)
QModelIndex parent() const const
bool isValid() const
AgentInstance::List instances() const
virtual QString errorString() const
int error() const
QStringList mimeTypes() const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.