Akonadi Mime

specialmailcollections.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "specialmailcollections.h"
8 #include "specialmailcollectionssettings.h"
9 #include "akonadi_mime_debug.h"
10 
11 #include <specialcollectionattribute.h>
12 #include <entitydisplayattribute.h>
13 #include <collectionmodifyjob.h>
14 #include <agentinstance.h>
15 #include <servermanager.h>
16 
17 #include <KLocalizedString>
18 #include <Kdelibs4ConfigMigrator>
19 
20 using namespace Akonadi;
21 
22 class Akonadi::SpecialMailCollectionsPrivate
23 {
24 public:
25  SpecialMailCollectionsPrivate();
26  ~SpecialMailCollectionsPrivate();
27 
28  SpecialMailCollections *mInstance = nullptr;
29 };
30 
31 typedef SpecialMailCollectionsSettings Settings;
32 
33 Q_GLOBAL_STATIC(SpecialMailCollectionsPrivate, sInstance)
34 
35 static const char s_specialCollectionTypes[SpecialMailCollections::LastType][11] = {
36  "local-mail",
37  "inbox",
38  "outbox",
39  "sent-mail",
40  "trash",
41  "drafts",
42  "templates"
43 };
44 
45 static const int s_numTypes = sizeof s_specialCollectionTypes / sizeof *s_specialCollectionTypes;
46 
47 static inline QByteArray enumToType(SpecialMailCollections::Type value)
48 {
49  return s_specialCollectionTypes[value];
50 }
51 
52 static inline SpecialMailCollections::Type typeToEnum(const QByteArray &type)
53 {
54  for (int i = 0; i < s_numTypes; ++i) {
55  if (type == s_specialCollectionTypes[i]) {
56  return static_cast<SpecialMailCollections::Type>(i);
57  }
58  }
60 }
61 
62 SpecialMailCollectionsPrivate::SpecialMailCollectionsPrivate()
63  : mInstance(new SpecialMailCollections(this))
64 {
65 }
66 
67 SpecialMailCollectionsPrivate::~SpecialMailCollectionsPrivate()
68 {
69  delete mInstance;
70 }
71 
72 static KCoreConfigSkeleton *getConfig(const QString &filename)
73 {
74  Kdelibs4ConfigMigrator migrate(QStringLiteral("specialmailcollection"));
75  migrate.setConfigFiles(QStringList() << filename);
76  migrate.migrate();
77 
78  Settings::instance(ServerManager::addNamespace(filename));
79  return Settings::self();
80 }
81 
82 SpecialMailCollections::SpecialMailCollections(SpecialMailCollectionsPrivate *dd)
83  : SpecialCollections(getConfig(QStringLiteral("specialmailcollectionsrc")))
84  , d(dd)
85 {
86 }
87 
89 {
90  return sInstance->mInstance;
91 }
92 
93 bool SpecialMailCollections::hasCollection(Type type, const AgentInstance &instance) const
94 {
95  return SpecialCollections::hasCollection(enumToType(type), instance);
96 }
97 
99 {
100  return SpecialCollections::collection(enumToType(type), instance);
101 }
102 
104 {
105  return SpecialCollections::registerCollection(enumToType(type), collection);
106 }
107 
109 {
110  if (collection != Akonadi::SpecialMailCollections::self()->defaultCollection(Akonadi::SpecialMailCollections::Trash)) {
111  return SpecialCollections::unregisterCollection(collection);
112  } else {
113  return false;
114  }
115 }
116 
118 {
119  return SpecialCollections::hasDefaultCollection(enumToType(type));
120 }
121 
123 {
124  return SpecialCollections::defaultCollection(enumToType(type));
125 }
126 
127 void SpecialMailCollections::verifyI18nDefaultCollection(Type type)
128 {
129  Collection collection = defaultCollection(type);
130  QString defaultI18n;
131 
132  switch (type) {
134  defaultI18n = i18nc("local mail folder", "inbox");
135  break;
137  defaultI18n = i18nc("local mail folder", "outbox");
138  break;
140  defaultI18n = i18nc("local mail folder", "sent-mail");
141  break;
143  defaultI18n = i18nc("local mail folder", "trash");
144  break;
146  defaultI18n = i18nc("local mail folder", "drafts");
147  break;
149  defaultI18n = i18nc("local mail folder", "templates");
150  break;
151  default:
152  break;
153  }
154  if (!defaultI18n.isEmpty()) {
155  if (collection.hasAttribute<Akonadi::EntityDisplayAttribute>()) {
156  if (collection.attribute<Akonadi::EntityDisplayAttribute>()->displayName() != defaultI18n) {
157  collection.attribute<Akonadi::EntityDisplayAttribute>()->setDisplayName(defaultI18n);
158  Akonadi::CollectionModifyJob *job = new Akonadi::CollectionModifyJob(collection, this);
159  connect(job, &Akonadi::CollectionModifyJob::result, this, &SpecialMailCollections::slotCollectionModified);
160  }
161  }
162  }
163 }
164 
165 void SpecialMailCollections::slotCollectionModified(KJob *job)
166 {
167  if (job->error()) {
168  qCDebug(AKONADIMIME_LOG) << " Error when we modified collection";
169  return;
170  }
171 }
172 
174 {
175  if (!collection.hasAttribute<SpecialCollectionAttribute>()) {
176  return Invalid;
177  } else {
178  return typeToEnum(collection.attribute<SpecialCollectionAttribute>()->collectionType());
179  }
180 }
bool hasDefaultCollection(Type type) const
Returns whether the default resource has a special mail collection of the given type.
bool registerCollection(const QByteArray &type, const Akonadi::Collection &collection)
bool hasCollection(const QByteArray &type, const AgentInstance &instance) const
bool hasDefaultCollection(const QByteArray &type) const
Akonadi::Collection collection(Type type, const AgentInstance &instance) const
Returns the special mail collection of the given type in the given agent instance, or an invalid collection if such a collection is unknown.
Akonadi::Collection defaultCollection(const QByteArray &type) const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
Akonadi::Collection collection(const QByteArray &type, const AgentInstance &instance) const
Interface to special mail collections such as inbox, outbox etc.
Type
Describes the possible types of special mail collections.
Attribute * attribute(const QByteArray &name)
static Type specialCollectionType(const Akonadi::Collection &collection)
Returns the special collection type for a given collection, or empty if the collection doesn&#39;t have a...
static SpecialMailCollections * self()
Returns the global SpecialMailCollections instance.
bool registerCollection(Type type, const Akonadi::Collection &collection)
Registers the given collection as a special mail collection with the given type.
bool hasAttribute(const QByteArray &name) const
bool hasCollection(Type type, const AgentInstance &instance) const
Returns whether the given agent instance has a special collection of the given type.
bool unregisterCollection(const Collection &collection)
Unregisters the given collection as a special mail collection.
QByteArray collectionType() const
Akonadi::Collection defaultCollection(Type type) const
Returns the special mail collection of given type in the default resource, or an invalid collection i...
void result(KJob *job)
bool unregisterCollection(const Collection &collection)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QString addNamespace(const QString &string)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 7 2020 23:06:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.