File indexing completed on 2025-01-05 04:58:19

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "addresseelineeditmanager.h"
0008 #include "addresseelineeditakonadi.h"
0009 #include "addresseelineeditbaloo.h"
0010 #include "addresseelineeditldap.h"
0011 #include "kmailcompletion.h"
0012 
0013 #include "pimcommonakonadi_debug.h"
0014 #include <KColorScheme>
0015 #include <KConfigGroup>
0016 #include <KLDAPWidgets/LdapClient>
0017 #include <KSharedConfig>
0018 #include <QCoreApplication>
0019 #include <QNetworkInformation>
0020 #include <QTimer>
0021 
0022 using namespace PimCommon;
0023 
0024 Q_GLOBAL_STATIC(AddresseeLineEditManager, sInstance)
0025 
0026 AddresseeLineEditManager::AddresseeLineEditManager()
0027     : mCompletion(new KMailCompletion)
0028     , mAddresseeLineEditAkonadi(new AddresseeLineEditAkonadi)
0029     , mAddressessLineEditLdap(new AddresseeLineEditLdap(this))
0030     , mAddressessLineEditBaloo(new AddresseeLineEditBaloo)
0031 {
0032     KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("AddressLineEdit"));
0033     mShowOU = group.readEntry("ShowOU", false);
0034     mAutoGroupExpand = group.readEntry("AutoGroupExpand", false);
0035 }
0036 
0037 AddresseeLineEditManager::~AddresseeLineEditManager()
0038 {
0039     delete mCompletion;
0040 
0041     delete mAddresseeLineEditAkonadi;
0042     delete mAddressessLineEditLdap;
0043     delete mAddressessLineEditBaloo;
0044 }
0045 
0046 AddresseeLineEditManager *AddresseeLineEditManager::self()
0047 {
0048     return sInstance;
0049 }
0050 
0051 void AddresseeLineEditManager::updateCompletionOrder()
0052 {
0053     updateLDAPWeights();
0054     updateCollectionWeights();
0055 }
0056 
0057 void AddresseeLineEditManager::updateCollectionWeights()
0058 {
0059     akonadiCollectionToCompletionSourceMap.clear();
0060 }
0061 
0062 void AddresseeLineEditManager::updateLDAPWeights()
0063 {
0064     mAddressessLineEditLdap->updateLDAPWeights();
0065 }
0066 
0067 int AddresseeLineEditManager::addCompletionSource(const QString &source, int weight)
0068 {
0069     QMap<QString, int>::iterator it = completionSourceWeights.find(source);
0070     if (it == completionSourceWeights.end()) {
0071         completionSourceWeights.insert(source, weight);
0072     } else {
0073         completionSourceWeights[source] = weight;
0074     }
0075 
0076     const int sourceIndex = completionSources.indexOf(source);
0077     if (sourceIndex == -1) {
0078         completionSources.append(source);
0079         return completionSources.size() - 1;
0080     } else {
0081         return sourceIndex;
0082     }
0083 }
0084 
0085 void AddresseeLineEditManager::removeCompletionSource(const QString &source)
0086 {
0087     QMap<QString, int>::iterator it = completionSourceWeights.find(source);
0088     if (it != completionSourceWeights.end()) {
0089         completionSourceWeights.remove(source);
0090         mCompletion->clear();
0091     }
0092 }
0093 
0094 Akonadi::Session *AddresseeLineEditManager::akonadiSession() const
0095 {
0096     return mAddresseeLineEditAkonadi->akonadiSession();
0097 }
0098 
0099 KMailCompletion *AddresseeLineEditManager::completion() const
0100 {
0101     return mCompletion;
0102 }
0103 
0104 AddresseeLineEdit *AddresseeLineEditManager::addressLineEdit() const
0105 {
0106     return mAddressessLineEditLdap->addressLineEdit();
0107 }
0108 
0109 void AddresseeLineEditManager::setAddressLineEdit(AddresseeLineEdit *addressLineEdit)
0110 {
0111     mAddressessLineEditLdap->setAddressLineEdit(addressLineEdit);
0112 }
0113 
0114 bool AddresseeLineEditManager::isOnline() const
0115 {
0116     if (QNetworkInformation::loadBackendByFeatures(QNetworkInformation::Feature::Reachability)) {
0117         return QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online;
0118     } else {
0119         qCWarning(PIMCOMMONAKONADI_LOG) << "Couldn't find a working backend for QNetworkInformation";
0120         return false;
0121     }
0122 }
0123 
0124 KLDAPWidgets::LdapClientSearch *AddresseeLineEditManager::ldapSearch() const
0125 {
0126     return mAddressessLineEditLdap->ldapSearch();
0127 }
0128 
0129 QTimer *AddresseeLineEditManager::ldapTimer() const
0130 {
0131     return mAddressessLineEditLdap->ldapTimer();
0132 }
0133 
0134 int AddresseeLineEditManager::ldapClientToCompletionSourceValue(int value) const
0135 {
0136     return mAddressessLineEditLdap->ldapClientToCompletionSourceValue(value);
0137 }
0138 
0139 bool AddresseeLineEditManager::isLdapClientToCompletionSourceMapContains(int value) const
0140 {
0141     return mAddressessLineEditLdap->isLdapClientToCompletionSourceMapContains(value);
0142 }
0143 
0144 int AddresseeLineEditManager::balooCompletionSource() const
0145 {
0146     return mAddressessLineEditBaloo->balooCompletionSource();
0147 }
0148 
0149 void AddresseeLineEditManager::setBalooCompletionSource(int value)
0150 {
0151     mAddressessLineEditBaloo->setBalooCompletionSource(value);
0152 }
0153 
0154 void AddresseeLineEditManager::initializeLdap()
0155 {
0156     mAddressessLineEditLdap->init();
0157 }
0158 
0159 QString AddresseeLineEditManager::ldapText() const
0160 {
0161     return mAddressessLineEditLdap->ldapText();
0162 }
0163 
0164 void AddresseeLineEditManager::setLdapText(const QString &ldapText)
0165 {
0166     mAddressessLineEditLdap->setLdapText(ldapText);
0167 }
0168 
0169 void AddresseeLineEditManager::stopLDAPLookup()
0170 {
0171     mAddressessLineEditLdap->stopLDAPLookup();
0172 }
0173 
0174 void AddresseeLineEditManager::startLoadingLDAPEntries()
0175 {
0176     mAddressessLineEditLdap->startLoadingLDAPEntries();
0177 }
0178 
0179 void AddresseeLineEditManager::restartLdap(const QString &searchString, AddresseeLineEdit *addressLine)
0180 {
0181     mAddressessLineEditLdap->restartLdap(searchString, addressLine);
0182 }
0183 
0184 void AddresseeLineEditManager::loadBalooBlackList()
0185 {
0186     mAddressessLineEditBaloo->loadBalooBlackList();
0187 }
0188 
0189 QStringList AddresseeLineEditManager::cleanupRecentAddressEmailList(const QStringList &inputList)
0190 {
0191     if (inputList != mRecentAddressEmailList) {
0192         mRecentCleanupAddressEmailList = mAddressessLineEditBaloo->cleanupEmailList(inputList);
0193         mRecentAddressEmailList = inputList;
0194     }
0195     return mRecentCleanupAddressEmailList;
0196 }
0197 
0198 QStringList AddresseeLineEditManager::cleanupEmailList(const QStringList &inputList)
0199 {
0200     return mAddressessLineEditBaloo->cleanupEmailList(inputList);
0201 }
0202 
0203 QStringList AddresseeLineEditManager::balooBlackList() const
0204 {
0205     return mAddressessLineEditBaloo->balooBlackList();
0206 }
0207 
0208 bool AddresseeLineEditManager::showOU() const
0209 {
0210     return mShowOU;
0211 }
0212 
0213 void AddresseeLineEditManager::setShowOU(bool checked)
0214 {
0215     if (checked != mShowOU) {
0216         KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("AddressLineEdit"));
0217         group.writeEntry("ShowOU", checked);
0218         mShowOU = checked;
0219     }
0220 }
0221 
0222 bool AddresseeLineEditManager::autoGroupExpand() const
0223 {
0224     return mAutoGroupExpand;
0225 }
0226 
0227 void AddresseeLineEditManager::setAutoGroupExpand(bool checked)
0228 {
0229     if (mAutoGroupExpand != checked) {
0230         mAutoGroupExpand = checked;
0231         KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("AddressLineEdit"));
0232         group.writeEntry("AutoGroupExpand", mAutoGroupExpand);
0233     }
0234 }
0235 
0236 QColor AddresseeLineEditManager::alternateColor() const
0237 {
0238     if (!mAlternateColor.isValid()) {
0239         const KColorScheme colorScheme(QPalette::Active, KColorScheme::View);
0240         mAlternateColor = colorScheme.background(KColorScheme::AlternateBackground).color();
0241     }
0242     return mAlternateColor;
0243 }