specialmailcollections.cpp 5.41 KB
Newer Older
1
/*
2
    SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3

4
    SPDX-License-Identifier: LGPL-2.0-or-later
5
6
7
*/

#include "specialmailcollections.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "akonadi_mime_debug.h"
Laurent Montel's avatar
Laurent Montel committed
9
#include "specialmailcollectionssettings.h"
10

11
#include <agentinstance.h>
Laurent Montel's avatar
Laurent Montel committed
12
13
#include <collectionmodifyjob.h>
#include <entitydisplayattribute.h>
14
#include <servermanager.h>
Laurent Montel's avatar
Laurent Montel committed
15
#include <specialcollectionattribute.h>
16

17
#include <KLocalizedString>
18
19
#include <kcoreaddons_version.h>
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(6, 0, 0)
20
#include <Kdelibs4ConfigMigrator>
21
#endif
22
23
24
25
26

using namespace Akonadi;

class Akonadi::SpecialMailCollectionsPrivate
{
Guy Maurel's avatar
Guy Maurel committed
27
public:
28
29
30
    SpecialMailCollectionsPrivate();
    ~SpecialMailCollectionsPrivate();

Laurent Montel's avatar
Laurent Montel committed
31
    SpecialMailCollections *mInstance = nullptr;
32
33
34
35
};

typedef SpecialMailCollectionsSettings Settings;

36
Q_GLOBAL_STATIC(SpecialMailCollectionsPrivate, sInstance)
37

Laurent Montel's avatar
Laurent Montel committed
38
39
static const char s_specialCollectionTypes[SpecialMailCollections::LastType][11] =
    {"local-mail", "inbox", "outbox", "sent-mail", "trash", "drafts", "templates"};
40

Laurent Montel's avatar
Laurent Montel committed
41
static const int s_numTypes = sizeof s_specialCollectionTypes / sizeof *s_specialCollectionTypes;
42

43
static inline QByteArray enumToByteArray(SpecialMailCollections::Type value)
44
{
45
46
47
48
49
50
51
52
53
54
55
    return s_specialCollectionTypes[value];
}

static inline SpecialMailCollections::Type typeToEnum(const QByteArray &type)
{
    for (int i = 0; i < s_numTypes; ++i) {
        if (type == s_specialCollectionTypes[i]) {
            return static_cast<SpecialMailCollections::Type>(i);
        }
    }
    return SpecialMailCollections::Invalid;
56
57
58
}

SpecialMailCollectionsPrivate::SpecialMailCollectionsPrivate()
Guy Maurel's avatar
Guy Maurel committed
59
    : mInstance(new SpecialMailCollections(this))
60
61
62
63
64
{
}

SpecialMailCollectionsPrivate::~SpecialMailCollectionsPrivate()
{
Guy Maurel's avatar
Guy Maurel committed
65
    delete mInstance;
66
67
}

Guy Maurel's avatar
Guy Maurel committed
68
static KCoreConfigSkeleton *getConfig(const QString &filename)
69
{
70
#if KCOREADDONS_VERSION < QT_VERSION_CHECK(6, 0, 0)
Laurent Montel's avatar
Laurent Montel committed
71
    Kdelibs4ConfigMigrator migrate(QStringLiteral("specialmailcollection"));
72
73
    migrate.setConfigFiles(QStringList() << filename);
    migrate.migrate();
74
#endif
Guy Maurel's avatar
Guy Maurel committed
75
76
    Settings::instance(ServerManager::addNamespace(filename));
    return Settings::self();
77
78
}

Guy Maurel's avatar
Guy Maurel committed
79
SpecialMailCollections::SpecialMailCollections(SpecialMailCollectionsPrivate *dd)
Laurent Montel's avatar
Laurent Montel committed
80
    : SpecialCollections(getConfig(QStringLiteral("specialmailcollectionsrc")))
Guy Maurel's avatar
Guy Maurel committed
81
    , d(dd)
82
83
84
85
86
{
}

SpecialMailCollections *SpecialMailCollections::self()
{
Guy Maurel's avatar
Guy Maurel committed
87
    return sInstance->mInstance;
88
89
}

Guy Maurel's avatar
Guy Maurel committed
90
bool SpecialMailCollections::hasCollection(Type type, const AgentInstance &instance) const
91
{
92
    return SpecialCollections::hasCollection(enumToByteArray(type), instance);
93
94
}

Guy Maurel's avatar
Guy Maurel committed
95
Collection SpecialMailCollections::collection(Type type, const AgentInstance &instance) const
96
{
97
    return SpecialCollections::collection(enumToByteArray(type), instance);
98
99
}

Guy Maurel's avatar
Guy Maurel committed
100
bool SpecialMailCollections::registerCollection(Type type, const Collection &collection)
101
{
102
    return SpecialCollections::registerCollection(enumToByteArray(type), collection);
103
104
}

Guy Maurel's avatar
Guy Maurel committed
105
bool SpecialMailCollections::unregisterCollection(const Collection &collection)
106
{
Guy Maurel's avatar
Guy Maurel committed
107
108
109
110
111
    if (collection != Akonadi::SpecialMailCollections::self()->defaultCollection(Akonadi::SpecialMailCollections::Trash)) {
        return SpecialCollections::unregisterCollection(collection);
    } else {
        return false;
    }
112
113
}

Guy Maurel's avatar
Guy Maurel committed
114
bool SpecialMailCollections::hasDefaultCollection(Type type) const
115
{
116
    return SpecialCollections::hasDefaultCollection(enumToByteArray(type));
117
118
}

Guy Maurel's avatar
Guy Maurel committed
119
Collection SpecialMailCollections::defaultCollection(Type type) const
120
{
121
    return SpecialCollections::defaultCollection(enumToByteArray(type));
122
123
}

Guy Maurel's avatar
Guy Maurel committed
124
void SpecialMailCollections::verifyI18nDefaultCollection(Type type)
125
{
Guy Maurel's avatar
Guy Maurel committed
126
127
    Collection collection = defaultCollection(type);
    QString defaultI18n;
128

Guy Maurel's avatar
Guy Maurel committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    switch (type) {
    case SpecialMailCollections::Inbox:
        defaultI18n = i18nc("local mail folder", "inbox");
        break;
    case SpecialMailCollections::Outbox:
        defaultI18n = i18nc("local mail folder", "outbox");
        break;
    case SpecialMailCollections::SentMail:
        defaultI18n = i18nc("local mail folder", "sent-mail");
        break;
    case SpecialMailCollections::Trash:
        defaultI18n = i18nc("local mail folder", "trash");
        break;
    case SpecialMailCollections::Drafts:
        defaultI18n = i18nc("local mail folder", "drafts");
        break;
    case SpecialMailCollections::Templates:
        defaultI18n = i18nc("local mail folder", "templates");
        break;
    default:
        break;
    }
    if (!defaultI18n.isEmpty()) {
        if (collection.hasAttribute<Akonadi::EntityDisplayAttribute>()) {
            if (collection.attribute<Akonadi::EntityDisplayAttribute>()->displayName() != defaultI18n) {
                collection.attribute<Akonadi::EntityDisplayAttribute>()->setDisplayName(defaultI18n);
155
                auto job = new Akonadi::CollectionModifyJob(collection, this);
Laurent Montel's avatar
Laurent Montel committed
156
                connect(job, &Akonadi::CollectionModifyJob::result, this, &SpecialMailCollections::slotCollectionModified);
Guy Maurel's avatar
Guy Maurel committed
157
158
            }
        }
159
160
161
    }
}

Guy Maurel's avatar
Guy Maurel committed
162
void SpecialMailCollections::slotCollectionModified(KJob *job)
163
{
Guy Maurel's avatar
Guy Maurel committed
164
    if (job->error()) {
Laurent Montel's avatar
Laurent Montel committed
165
        qCDebug(AKONADIMIME_LOG) << " Error when we modified collection";
Guy Maurel's avatar
Guy Maurel committed
166
167
        return;
    }
168
169
}

170
171
172
173
174
175
176
177
SpecialMailCollections::Type SpecialMailCollections::specialCollectionType(const Akonadi::Collection &collection)
{
    if (!collection.hasAttribute<SpecialCollectionAttribute>()) {
        return Invalid;
    } else {
        return typeToEnum(collection.attribute<SpecialCollectionAttribute>()->collectionType());
    }
}