File indexing completed on 2024-04-28 05:11:38

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  *
0006  */
0007 
0008 #pragma once
0009 
0010 #include "resourceitem.h"
0011 
0012 #include <KLDAPWidgets/LdapClientSearch>
0013 
0014 #include <QAbstractItemModel>
0015 #include <QModelIndex>
0016 #include <QSet>
0017 
0018 namespace IncidenceEditorNG
0019 {
0020 class ResourceModel : public QAbstractItemModel
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     /* Copied from https://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html:
0026      * Editable Tree Model Example
0027      */
0028     enum Roles { Resource = Qt::UserRole, FullName };
0029 
0030     explicit ResourceModel(const QStringList &headers, QObject *parent = nullptr);
0031     ~ResourceModel() override;
0032 
0033     [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
0034     [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0035 
0036     [[nodiscard]] QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0037     [[nodiscard]] QModelIndex parent(const QModelIndex &index) const override;
0038 
0039     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0040     [[nodiscard]] int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0041 
0042     [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
0043 
0044     [[nodiscard]] bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
0045 
0046 private:
0047     ResourceItem *getItem(const QModelIndex &index) const;
0048 
0049     ResourceItem::Ptr mRootItem;
0050 
0051 public:
0052     /* Start search on LDAP Server with the given string.
0053      * If the model is not ready to search, the string is cached and is executed afterwards.
0054      */
0055     void startSearch(const QString &);
0056 
0057 private:
0058     /* Start search with cached string (stored in searchString)
0059      *
0060      */
0061     void startSearch();
0062 
0063     /* Search for collections of resources
0064      *
0065      */
0066     KLDAPWidgets::LdapClientSearch *mLdapSearchCollections = nullptr;
0067 
0068     /* Search for matching resources
0069      *
0070      */
0071     KLDAPWidgets::LdapClientSearch *mLdapSearch = nullptr;
0072 
0073     /* Map from dn of resource -> collectionItem
0074      * A Resource can be part of different collection, so a QMuliMap is needed
0075      *
0076      */
0077     QMultiMap<QString, ResourceItem::Ptr> mLdapCollectionsMap;
0078 
0079     /* A Set of all collection ResourceItems
0080      *
0081      */
0082     QSet<ResourceItem::Ptr> mLdapCollections;
0083 
0084     /* Cached searchString (set by startSearch(QString))
0085      *
0086      */
0087     QString mSearchString;
0088 
0089     /* Is the search of collections ended
0090      *
0091      */
0092     bool mFoundCollection = false;
0093 
0094     /* List of all attributes in LDAP an the headers of the model
0095      *
0096      */
0097     QStringList mHeaders;
0098 
0099 private:
0100     /* Slot for founded collections
0101      *
0102      */
0103     void slotLDAPCollectionData(const KLDAPWidgets::LdapResultObject::List &);
0104 
0105     /* Slot for matching resources
0106      *
0107      */
0108     void slotLDAPSearchData(const KLDAPWidgets::LdapResultObject::List &);
0109 };
0110 }