Make the KOrganizer plugin loading work again.
[kdepim.git] / incidenceeditor-ng / src / resourcemodel.cpp
blobed660ccdb963bb1751f5b642cbfa48d097f86a6e
1 /*
2 * Copyright 2014 Sandro Knauß <knauss@kolabsys.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "resourcemodel.h"
22 #include "ldaputils.h"
24 #include <KEmailAddress>
25 #include <QDebug>
27 using namespace IncidenceEditorNG;
29 ResourceModel::ResourceModel(const QStringList &headers,
30 QObject *parent)
31 : QAbstractItemModel(parent)
32 , foundCollection(false)
35 this->headers = headers;
36 rootItem = ResourceItem::Ptr(new ResourceItem(KLDAP::LdapDN(), headers, KLDAP::LdapClient(0)));
38 ldapSearchCollections.setFilter(QStringLiteral("&(ou=Resources,*)(objectClass=kolabGroupOfUniqueNames)(objectclass=groupofurls)(!(objectclass=nstombstone))(mail=*)"
39 "(cn=%1)"));
40 ldapSearch.setFilter(QStringLiteral("&(objectClass=kolabSharedFolder)(kolabFolderType=event)(mail=*)"
41 "(|(cn=%1)(description=%1)(kolabDescAttribute=%1))"));
43 QStringList attrs = ldapSearchCollections.attributes();
44 attrs << QStringLiteral("uniqueMember");
45 ldapSearchCollections.setAttributes(attrs);
46 ldapSearch.setAttributes(headers);
48 connect(&ldapSearchCollections, static_cast<void (KLDAP::LdapClientSearch::*)(const KLDAP::LdapResultObject::List &)>(&KLDAP::LdapClientSearch::searchData), this, &ResourceModel::slotLDAPCollectionData);
49 connect(&ldapSearch, static_cast<void (KLDAP::LdapClientSearch::*)(const KLDAP::LdapResultObject::List &)>(&KLDAP::LdapClientSearch::searchData), this, &ResourceModel::slotLDAPSearchData);
51 ldapSearchCollections.startSearch(QStringLiteral("*"));
54 ResourceModel::~ResourceModel()
58 int ResourceModel::columnCount(const QModelIndex & /* parent */) const
60 return rootItem->columnCount();
63 QVariant ResourceModel::data(const QModelIndex &index, int role) const
65 if (!index.isValid()) {
66 return QVariant();
69 if (role == Qt::EditRole || role == Qt::DisplayRole) {
70 return getItem(index)->data(index.column());
71 } else if (role == Resource) {
72 ResourceItem *p = getItem(parent(index));
73 return QVariant::fromValue(p->child(index.row()));
74 } else if (role == FullName) {
75 ResourceItem *item = getItem(index);
76 return KEmailAddress::normalizedAddress(item->data(QStringLiteral("cn")).toString(), item->data(QStringLiteral("mail")).toString());
80 return QVariant();
83 Qt::ItemFlags ResourceModel::flags(const QModelIndex &index) const
85 if (!index.isValid()) {
86 return 0;
89 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
92 ResourceItem *ResourceModel::getItem(const QModelIndex &index) const
94 if (index.isValid()) {
95 ResourceItem *item = static_cast<ResourceItem *>(index.internalPointer());
96 if (item) {
97 return item;
100 return rootItem.data();
103 QVariant ResourceModel::headerData(int section, Qt::Orientation orientation,
104 int role) const
106 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
107 return translateLDAPAttributeForDisplay(rootItem->data(section).toString());
110 return QVariant();
113 QModelIndex ResourceModel::index(int row, int column, const QModelIndex &parent) const
115 ResourceItem *parentItem = getItem(parent);
117 ResourceItem::Ptr childItem = parentItem->child(row);
118 if (row < parentItem->childCount() && childItem) {
119 return createIndex(row, column, childItem.data());
120 } else {
121 return QModelIndex();
125 QModelIndex ResourceModel::parent(const QModelIndex &index) const
127 if (!index.isValid()) {
128 return QModelIndex();
130 ResourceItem *childItem = getItem(index);
131 ResourceItem::Ptr parentItem = childItem->parent();
133 if (parentItem == rootItem) {
134 return QModelIndex();
137 return createIndex(parentItem->childNumber(), index.column(), parentItem.data());
140 bool ResourceModel::removeRows(int position, int rows, const QModelIndex &parent)
142 ResourceItem *parentItem = getItem(parent);
143 bool success = true;
145 beginRemoveRows(parent, position, position + rows - 1);
146 success = parentItem->removeChildren(position, rows);
147 endRemoveRows();
149 return success;
152 int ResourceModel::rowCount(const QModelIndex &parent) const
154 ResourceItem *parentItem = getItem(parent);
156 return parentItem->childCount();
159 void ResourceModel::startSearch(const QString &query)
161 searchString = query;
163 if (foundCollection) {
164 startSearch();
168 void ResourceModel::startSearch()
170 // Delete all resources -> only collection elements are shown
171 for (int i = 0; i < rootItem->childCount(); ++i) {
172 if (ldapCollections.contains(rootItem->child(i))) {
173 QModelIndex parentIndex = index(i, 0, QModelIndex());
174 beginRemoveRows(parentIndex, 0, rootItem->child(i)->childCount() - 1);
175 rootItem->child(i)->removeChildren(0, rootItem->child(i)->childCount());
176 endRemoveRows();
177 } else {
178 beginRemoveRows(QModelIndex(), i, i);
179 rootItem->removeChildren(i, 1);
180 endRemoveRows();
184 if (searchString.isEmpty()) {
185 ldapSearch.startSearch(QStringLiteral("*"));
186 } else {
187 ldapSearch.startSearch("*" + searchString + "*");
191 void ResourceModel::slotLDAPCollectionData(const KLDAP::LdapResultObject::List &results)
193 Q_EMIT layoutAboutToBeChanged();
195 foundCollection = true;
196 ldapCollectionsMap.clear();
197 ldapCollections.clear();
199 //qDebug() << "Found ldapCollections";
201 foreach (const KLDAP::LdapResultObject &result, results) {
202 ResourceItem::Ptr item(new ResourceItem(result.object.dn(), headers, *result.client, rootItem));
203 item->setLdapObject(result.object);
205 rootItem->insertChild(rootItem->childCount(), item);
206 ldapCollections.insert(item);
208 // Resources in a collection add this link into ldapCollectionsMap
209 foreach (const QByteArray &member, result.object.attributes()["uniqueMember"]) {
210 ldapCollectionsMap.insert(QString::fromLatin1(member), item);
214 Q_EMIT layoutChanged();
216 startSearch();
219 void ResourceModel::slotLDAPSearchData(const KLDAP::LdapResultObject::List &results)
221 foreach (const KLDAP::LdapResultObject &result, results) {
222 //Add the found items to all collections, where it is member
223 QList<ResourceItem::Ptr> parents = ldapCollectionsMap.values(result.object.dn().toString());
224 if (parents.isEmpty()) {
225 parents << rootItem;
228 foreach (const ResourceItem::Ptr &parent, parents) {
229 ResourceItem::Ptr item(new ResourceItem(result.object.dn(), headers, *result.client, parent));
230 item->setLdapObject(result.object);
232 QModelIndex parentIndex;
233 if (parent != rootItem) {
234 parentIndex = index(parent->childNumber(), 0, parentIndex);
236 beginInsertRows(parentIndex, parent->childCount(), parent->childCount());
237 parent->insertChild(parent->childCount(), item);
238 endInsertRows();