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 #pragma once
0008 
0009 #include <Akonadi/Collection>
0010 #include <QMap>
0011 
0012 #include <Akonadi/Item>
0013 #include <Akonadi/Session>
0014 #include <KLDAPWidgets/LdapClientSearch>
0015 #include <QList>
0016 #include <QPointer>
0017 
0018 #include <QColor>
0019 
0020 class QTimer;
0021 class QNetworkConfigurationManager;
0022 namespace PimCommon
0023 {
0024 class KMailCompletion;
0025 class AddresseeLineEdit;
0026 class AddresseeLineEditAkonadi;
0027 class AddresseeLineEditLdap;
0028 class AddresseeLineEditBaloo;
0029 class AddresseeLineEditManager
0030 {
0031 public:
0032     using CompletionItemsMap = QMap<QString, QPair<int, int>>;
0033 
0034     AddresseeLineEditManager();
0035 
0036     ~AddresseeLineEditManager();
0037 
0038     static AddresseeLineEditManager *self();
0039 
0040     void updateCollectionWeights();
0041 
0042     void updateLDAPWeights();
0043     void updateCompletionOrder();
0044 
0045     int addCompletionSource(const QString &source, int weight);
0046     void removeCompletionSource(const QString &source);
0047 
0048     CompletionItemsMap completionItemMap;
0049     QStringList completionSources;
0050 
0051     // The weights associated with the completion sources in s_static->completionSources.
0052     // Both are maintained by addCompletionSource(), don't attempt to modify those yourself.
0053     QMap<QString, int> completionSourceWeights;
0054 
0055     // holds the cached mapping from akonadi collection id to the completion source index
0056     struct collectionInfo {
0057         collectionInfo()
0058             : index(-1)
0059             , enabled(true)
0060         {
0061         }
0062 
0063         collectionInfo(int idx, bool _enabled)
0064             : index(idx)
0065             , enabled(_enabled)
0066         {
0067         }
0068 
0069         int index;
0070         bool enabled;
0071     };
0072 
0073     QMap<Akonadi::Collection::Id, collectionInfo> akonadiCollectionToCompletionSourceMap;
0074     // a list of akonadi items (contacts) that have not had their collection fetched yet
0075     Akonadi::Item::List akonadiPendingItems;
0076     QList<QPointer<Akonadi::Job>> akonadiJobsInFlight;
0077 
0078     // Ldap
0079     void initializeLdap();
0080     [[nodiscard]] QString ldapText() const;
0081     void setLdapText(const QString &ldapText);
0082     KLDAPWidgets::LdapClientSearch *ldapSearch() const;
0083     QTimer *ldapTimer() const;
0084     bool isLdapClientToCompletionSourceMapContains(int value) const;
0085     int ldapClientToCompletionSourceValue(int value) const;
0086     void stopLDAPLookup();
0087     void startLoadingLDAPEntries();
0088     void restartLdap(const QString &searchString, AddresseeLineEdit *addressLine);
0089 
0090     // Baloo
0091     [[nodiscard]] int balooCompletionSource() const;
0092     void setBalooCompletionSource(int value);
0093 
0094     // Akonadi
0095     Akonadi::Session *akonadiSession() const;
0096 
0097     KMailCompletion *completion() const;
0098 
0099     AddresseeLineEdit *addressLineEdit() const;
0100     void setAddressLineEdit(AddresseeLineEdit *addressLineEdit);
0101     [[nodiscard]] bool isOnline() const;
0102 
0103     void loadBalooBlackList();
0104     [[nodiscard]] QStringList cleanupEmailList(const QStringList &inputList);
0105     [[nodiscard]] QStringList balooBlackList() const;
0106 
0107     [[nodiscard]] bool showOU() const;
0108     void setShowOU(bool showOU);
0109 
0110     bool autoGroupExpand() const;
0111     void setAutoGroupExpand(bool checked);
0112 
0113     [[nodiscard]] QColor alternateColor() const;
0114 
0115     [[nodiscard]] QStringList cleanupRecentAddressEmailList(const QStringList &inputList);
0116 
0117 private:
0118     QStringList mRecentAddressEmailList;
0119     QStringList mRecentCleanupAddressEmailList;
0120     mutable QColor mAlternateColor;
0121     bool mShowOU = false;
0122     bool mAutoGroupExpand = false;
0123     KMailCompletion *const mCompletion;
0124     AddresseeLineEditAkonadi *const mAddresseeLineEditAkonadi;
0125     AddresseeLineEditLdap *const mAddressessLineEditLdap;
0126     AddresseeLineEditBaloo *const mAddressessLineEditBaloo;
0127 };
0128 }