7#include "resourcemodel.h"
10#include <KEmailAddress>
13using namespace IncidenceEditorNG;
18 this->mHeaders = headers;
19 mRootItem = ResourceItem::Ptr(
new ResourceItem(KLDAPCore::LdapDN(), headers,
KLDAPCore::LdapClient(0)));
20 const QStringList attrs =
QStringList() << KLDAPCore::LdapClientSearch::defaultAttributes() << QStringLiteral(
"uniqueMember");
24 mLdapSearchCollections->setFilter(
25 QStringLiteral(
"&(ou=Resources,*)(objectClass=kolabGroupOfUniqueNames)(objectclass=groupofurls)(!(objectclass=nstombstone))(mail=*)"
27 mLdapSearch->setFilter(
28 QStringLiteral(
"&(objectClass=kolabSharedFolder)(kolabFolderType=event)(mail=*)"
29 "(|(cn=%1)(description=%1)(kolabDescAttribute=%1))"));
32 qOverload<const KLDAPCore::LdapResultObject::List &>(&KLDAPCore::LdapClientSearch ::searchData),
34 &ResourceModel::slotLDAPCollectionData);
38 &ResourceModel::slotLDAPSearchData);
40 mLdapSearchCollections->startSearch(QStringLiteral(
"*"));
43ResourceModel::~ResourceModel() =
default;
45int ResourceModel::columnCount(
const QModelIndex & )
const
47 return mRootItem->columnCount();
50QVariant ResourceModel::data(
const QModelIndex &index,
int role)
const
52 if (!index.isValid()) {
57 return getItem(index)->data(index.column());
58 }
else if (role == Resource) {
59 ResourceItem *p = getItem(
parent(index));
61 }
else if (role == FullName) {
62 ResourceItem *item = getItem(index);
69Qt::ItemFlags ResourceModel::flags(
const QModelIndex &index)
const
71 if (!index.isValid()) {
78ResourceItem *ResourceModel::getItem(
const QModelIndex &index)
const
80 if (index.isValid()) {
81 auto item =
static_cast<ResourceItem *
>(index.internalPointer());
86 return mRootItem.data();
89QVariant ResourceModel::headerData(
int section,
Qt::Orientation orientation,
int role)
const
92 return translateLDAPAttributeForDisplay(mRootItem->data(section).toString());
98QModelIndex ResourceModel::index(
int row,
int column,
const QModelIndex &parent)
const
100 ResourceItem *parentItem = getItem(parent);
102 ResourceItem::Ptr childItem = parentItem->child(row);
103 if (row < parentItem->childCount() && childItem) {
112 if (!index.isValid()) {
115 ResourceItem *childItem = getItem(index);
116 ResourceItem::Ptr parentItem = childItem->parent();
118 if (parentItem == mRootItem) {
122 return createIndex(parentItem->childNumber(), index.column(), parentItem.
data());
125bool ResourceModel::removeRows(
int position,
int rows,
const QModelIndex &parent)
127 ResourceItem *parentItem = getItem(parent);
130 bool success = parentItem->removeChildren(position, rows);
136int ResourceModel::rowCount(
const QModelIndex &parent)
const
138 ResourceItem *parentItem = getItem(parent);
140 return parentItem->childCount();
143void ResourceModel::startSearch(
const QString &query)
145 mSearchString =
query;
147 if (mFoundCollection) {
152void ResourceModel::startSearch()
155 for (
int i = 0; i < mRootItem->childCount(); ++i) {
156 if (mLdapCollections.contains(mRootItem->child(i))) {
157 QModelIndex parentIndex = index(i, 0, QModelIndex());
158 beginRemoveRows(parentIndex, 0, mRootItem->child(i)->childCount() - 1);
159 (void)mRootItem->child(i)->removeChildren(0, mRootItem->child(i)->childCount());
163 (void)mRootItem->removeChildren(i, 1);
168 if (mSearchString.isEmpty()) {
169 mLdapSearch->startSearch(QStringLiteral(
"*"));
171 mLdapSearch->startSearch(QLatin1Char(
'*') + mSearchString + QLatin1Char(
'*'));
175void ResourceModel::slotLDAPCollectionData(
const KLDAPCore::LdapResultObject::List &results)
179 mFoundCollection =
true;
180 mLdapCollectionsMap.clear();
181 mLdapCollections.clear();
185 for (
const KLDAPCore::LdapResultObject &result : std::as_const(results)) {
186 ResourceItem::Ptr item(
new ResourceItem(result.object.dn(), mHeaders, *result.client, mRootItem));
187 item->setLdapObject(result.object);
189 (void)mRootItem->insertChild(mRootItem->childCount(), item);
190 mLdapCollections.insert(item);
193 const auto members = result.object.attributes()[QStringLiteral(
"uniqueMember")];
194 for (
const QByteArray &member : members) {
204void ResourceModel::slotLDAPSearchData(
const KLDAPCore::LdapResultObject::List &results)
206 for (
const KLDAPCore::LdapResultObject &result : std::as_const(results)) {
208 QList<ResourceItem::Ptr> parents = mLdapCollectionsMap.values(result.object.dn().toString());
210 parents << mRootItem;
213 for (
const ResourceItem::Ptr &parent : std::as_const(parents)) {
214 ResourceItem::Ptr item(
new ResourceItem(result.object.dn(), mHeaders, *result.client, parent));
215 item->setLdapObject(result.object);
217 QModelIndex parentIndex;
218 if (parent != mRootItem) {
219 parentIndex = index(parent->childNumber(), 0, parentIndex);
221 beginInsertRows(parentIndex, parent->childCount(), parent->childCount());
222 (void)parent->insertChild(parent->childCount(), item);
228#include "moc_resourcemodel.cpp"
void searchData(const KLDAPCore::LdapResult::List &results)
char * toString(const EngineQuery &query)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void layoutAboutToBeChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
bool isEmpty() const const
QObject * parent() const const
QString fromLatin1(QByteArrayView str)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)