addresseelineeditmanager.cpp 6.69 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2015-2020 Laurent Montel <montel@kde.org>
3
4

  This program is free software; you can redistribute it and/or modify it
5
6
7
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
8
9
10
11
12
13
14
15
16
17
18

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

Laurent Montel's avatar
Laurent Montel committed
19
#include "addresseelineeditmanager.h"
20
#include "addresseelineeditakonadi.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "addresseelineeditbaloo.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "addresseelineeditldap.h"
23
24
#include "kmailcompletion.h"

Laurent Montel's avatar
Laurent Montel committed
25
#include <KLDAP/LdapClient>
26
#include <QNetworkConfigurationManager>
27
#include <QTimer>
28
#include <QCoreApplication>
29
30
#include <KConfigGroup>
#include <KSharedConfig>
31
#include <KColorScheme>
32
#include <AkonadiCore/Session>
33

34
35
using namespace KPIM;

Laurent Montel's avatar
Laurent Montel committed
36
37
38
Q_GLOBAL_STATIC(AddresseeLineEditManager, sInstance)

AddresseeLineEditManager::AddresseeLineEditManager()
39
40
41
    : mShowOU(false)
    , mAutoGroupExpand(false)
    , mCompletion(new KMailCompletion)
Laurent Montel's avatar
Laurent Montel committed
42
    , mAddresseeLineEditAkonadi(new AddresseeLineEditAkonadi)
Laurent Montel's avatar
Laurent Montel committed
43
    , mAddressessLineEditLdap(new AddresseeLineEditLdap(this))
Laurent Montel's avatar
Laurent Montel committed
44
    , mAddressessLineEditBaloo(new AddresseeLineEditBaloo)
45
    , mNetworkConfigMgr(new QNetworkConfigurationManager(QCoreApplication::instance()))
46
{
47
48
49
    KConfigGroup group(KSharedConfig::openConfig(), "AddressLineEdit");
    mShowOU = group.readEntry("ShowOU", false);
    mAutoGroupExpand = group.readEntry("AutoGroupExpand", false);
50
51
}

Laurent Montel's avatar
Laurent Montel committed
52
AddresseeLineEditManager::~AddresseeLineEditManager()
53
{
Laurent Montel's avatar
Laurent Montel committed
54
    delete mCompletion;
55
56

    delete mAddresseeLineEditAkonadi;
Laurent Montel's avatar
Laurent Montel committed
57
    delete mAddressessLineEditLdap;
Laurent Montel's avatar
Laurent Montel committed
58
    delete mAddressessLineEditBaloo;
59
60
}

Laurent Montel's avatar
Laurent Montel committed
61
62
63
64
65
66
AddresseeLineEditManager *AddresseeLineEditManager::self()
{
    return sInstance;
}

void AddresseeLineEditManager::updateCompletionOrder()
67
68
69
70
71
{
    updateLDAPWeights();
    updateCollectionWeights();
}

Laurent Montel's avatar
Laurent Montel committed
72
void AddresseeLineEditManager::updateCollectionWeights()
73
74
75
76
{
    akonadiCollectionToCompletionSourceMap.clear();
}

Laurent Montel's avatar
Laurent Montel committed
77
void AddresseeLineEditManager::updateLDAPWeights()
78
{
Laurent Montel's avatar
Laurent Montel committed
79
    mAddressessLineEditLdap->updateLDAPWeights();
80
81
}

Laurent Montel's avatar
Laurent Montel committed
82
int AddresseeLineEditManager::addCompletionSource(const QString &source, int weight)
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
    QMap<QString, int>::iterator it = completionSourceWeights.find(source);
    if (it == completionSourceWeights.end()) {
        completionSourceWeights.insert(source, weight);
    } else {
        completionSourceWeights[source] = weight;
    }

    const int sourceIndex = completionSources.indexOf(source);
    if (sourceIndex == -1) {
        completionSources.append(source);
        return completionSources.size() - 1;
    } else {
        return sourceIndex;
    }
}

Laurent Montel's avatar
Laurent Montel committed
100
void AddresseeLineEditManager::removeCompletionSource(const QString &source)
101
102
103
104
{
    QMap<QString, int>::iterator it = completionSourceWeights.find(source);
    if (it != completionSourceWeights.end()) {
        completionSourceWeights.remove(source);
Laurent Montel's avatar
Laurent Montel committed
105
        mCompletion->clear();
106
107
    }
}
108

Laurent Montel's avatar
Laurent Montel committed
109
Akonadi::Session *AddresseeLineEditManager::akonadiSession() const
110
{
111
    return mAddresseeLineEditAkonadi->akonadiSession();
112
}
Laurent Montel's avatar
Laurent Montel committed
113

Laurent Montel's avatar
Laurent Montel committed
114
115
116
117
118
KMailCompletion *AddresseeLineEditManager::completion() const
{
    return mCompletion;
}

Laurent Montel's avatar
Laurent Montel committed
119
120
AddresseeLineEdit *AddresseeLineEditManager::addressLineEdit() const
{
Laurent Montel's avatar
Laurent Montel committed
121
    return mAddressessLineEditLdap->addressLineEdit();
Laurent Montel's avatar
Laurent Montel committed
122
123
124
125
}

void AddresseeLineEditManager::setAddressLineEdit(AddresseeLineEdit *addressLineEdit)
{
Laurent Montel's avatar
Laurent Montel committed
126
    mAddressessLineEditLdap->setAddressLineEdit(addressLineEdit);
Laurent Montel's avatar
Laurent Montel committed
127
128
}

129
130
131
132
133
bool AddresseeLineEditManager::isOnline() const
{
    return mNetworkConfigMgr->isOnline();
}

Laurent Montel's avatar
Laurent Montel committed
134
KLDAP::LdapClientSearch *AddresseeLineEditManager::ldapSearch() const
Laurent Montel's avatar
Laurent Montel committed
135
136
137
138
{
    return mAddressessLineEditLdap->ldapSearch();
}

Laurent Montel's avatar
Laurent Montel committed
139
QTimer *AddresseeLineEditManager::ldapTimer() const
Laurent Montel's avatar
Laurent Montel committed
140
141
142
143
{
    return mAddressessLineEditLdap->ldapTimer();
}

Laurent Montel's avatar
Laurent Montel committed
144
int AddresseeLineEditManager::ldapClientToCompletionSourceValue(int value) const
145
146
147
148
{
    return mAddressessLineEditLdap->ldapClientToCompletionSourceValue(value);
}

Laurent Montel's avatar
Laurent Montel committed
149
bool AddresseeLineEditManager::isLdapClientToCompletionSourceMapContains(int value) const
150
151
152
153
{
    return mAddressessLineEditLdap->isLdapClientToCompletionSourceMapContains(value);
}

Laurent Montel's avatar
Laurent Montel committed
154
int AddresseeLineEditManager::balooCompletionSource() const
Laurent Montel's avatar
Laurent Montel committed
155
156
157
158
{
    return mAddressessLineEditBaloo->balooCompletionSource();
}

Laurent Montel's avatar
Laurent Montel committed
159
void AddresseeLineEditManager::setBalooCompletionSource(int value)
Laurent Montel's avatar
Laurent Montel committed
160
161
162
163
{
    mAddressessLineEditBaloo->setBalooCompletionSource(value);
}

Laurent Montel's avatar
Laurent Montel committed
164
void AddresseeLineEditManager::initializeLdap()
Laurent Montel's avatar
Laurent Montel committed
165
166
167
{
    mAddressessLineEditLdap->init();
}
Laurent Montel's avatar
Laurent Montel committed
168

Laurent Montel's avatar
Laurent Montel committed
169
QString AddresseeLineEditManager::ldapText() const
Laurent Montel's avatar
Laurent Montel committed
170
171
172
173
{
    return mAddressessLineEditLdap->ldapText();
}

Laurent Montel's avatar
Laurent Montel committed
174
void AddresseeLineEditManager::setLdapText(const QString &ldapText)
Laurent Montel's avatar
Laurent Montel committed
175
176
177
178
{
    mAddressessLineEditLdap->setLdapText(ldapText);
}

179
180
void AddresseeLineEditManager::stopLDAPLookup()
{
Laurent Montel's avatar
Laurent Montel committed
181
    mAddressessLineEditLdap->stopLDAPLookup();
182
183
184
185
186
187
}

void AddresseeLineEditManager::startLoadingLDAPEntries()
{
    mAddressessLineEditLdap->startLoadingLDAPEntries();
}
Laurent Montel's avatar
Laurent Montel committed
188
189
190
191
192

void AddresseeLineEditManager::restartLdap(const QString &searchString, AddresseeLineEdit *addressLine)
{
    mAddressessLineEditLdap->restartLdap(searchString, addressLine);
}
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

void AddresseeLineEditManager::loadBalooBlackList()
{
    mAddressessLineEditBaloo->loadBalooBlackList();
}

QStringList AddresseeLineEditManager::cleanupEmailList(const QStringList &inputList)
{
    return mAddressessLineEditBaloo->cleanupEmailList(inputList);
}

QStringList AddresseeLineEditManager::balooBlackList() const
{
    return mAddressessLineEditBaloo->balooBlackList();
}
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

bool AddresseeLineEditManager::showOU() const
{
    return mShowOU;
}

void AddresseeLineEditManager::setShowOU(bool checked)
{
    if (checked != mShowOU) {
        KConfigGroup group(KSharedConfig::openConfig(), "AddressLineEdit");
        group.writeEntry("ShowOU", checked);
        mShowOU = checked;
    }
}

bool AddresseeLineEditManager::autoGroupExpand() const
{
    return mAutoGroupExpand;
}

228
void AddresseeLineEditManager::setAutoGroupExpand(bool checked)
229
{
230
231
232
233
234
    if (mAutoGroupExpand != checked) {
        mAutoGroupExpand = checked;
        KConfigGroup group(KSharedConfig::openConfig(), "AddressLineEdit");
        group.writeEntry("AutoGroupExpand", mAutoGroupExpand);
    }
235
}
236
237
238
239
240
241
242
243
244

QColor AddresseeLineEditManager::alternateColor() const
{
    if (!mAlternateColor.isValid()) {
        const KColorScheme colorScheme(QPalette::Active, KColorScheme::View);
        mAlternateColor = colorScheme.background(KColorScheme::AlternateBackground).color();
    }
    return mAlternateColor;
}