KMail: Fix scrolling up/down on the message viewer
[kdepim.git] / incidenceeditor-ng / resourceitem.cpp
blob6f28e7c4bb2db14bd96ff543799c7e761eabf15e
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 "resourceitem.h"
23 #include <kldap/ldapserver.h>
25 using namespace IncidenceEditorNG;
27 ResourceItem::ResourceItem(const KLDAP::LdapDN &dn, const QStringList &attrs, const KLDAP::LdapClient &ldapClient, const ResourceItem::Ptr &parent)
28 : dn(dn)
29 , mAttrs(attrs)
30 , mLdapClient(0, this)
31 , parentItem(parent)
33 if (!dn.isEmpty()) {
34 KLDAP::LdapServer server = ldapClient.server();
36 server.setScope(KLDAP::LdapUrl::Base);
37 server.setBaseDn(dn);
38 mLdapClient.setServer(server);
40 connect(&mLdapClient, &KLDAP::LdapClient::result, this, &ResourceItem::slotLDAPResult);
42 mAttrs << QStringLiteral("uniqueMember");
43 mLdapClient.setAttributes(attrs);
44 } else {
45 foreach (const QString &header, mAttrs) {
46 itemData << header;
51 ResourceItem::~ResourceItem()
55 ResourceItem::Ptr ResourceItem::child(int number)
57 return childItems.value(number);
60 int ResourceItem::childCount() const
62 return childItems.count();
65 int ResourceItem::childNumber() const
67 if (parentItem) {
68 int i = 0;
69 foreach (const ResourceItem::Ptr &child, parentItem->childItems) {
70 if (child == this) {
71 return i;
73 i++;
77 return 0;
80 int ResourceItem::columnCount() const
82 return itemData.count();
85 QVariant ResourceItem::data(int column) const
87 return itemData.value(column);
90 QVariant ResourceItem::data(const QString &column) const
92 if (!mLdapObject.attributes()[column].isEmpty()) {
93 return QString::fromUtf8(mLdapObject.attributes()[column][0]);
95 return QVariant();
98 bool ResourceItem::insertChild(int position, const ResourceItem::Ptr &item)
100 if (position < 0 || position > childItems.size()) {
101 return false;
104 childItems.insert(position, item);
106 return true;
109 ResourceItem::Ptr ResourceItem::parent()
111 return parentItem;
114 bool ResourceItem::removeChildren(int position, int count)
116 if (position < 0 || position + count > childItems.size()) {
117 return false;
120 for (int row = 0; row < count; ++row) {
121 childItems.removeAt(position);
124 return true;
127 const QStringList &ResourceItem::attributes() const
129 return mAttrs;
132 const KLDAP::LdapObject &ResourceItem::ldapObject() const
134 return mLdapObject;
137 void ResourceItem::startSearch()
139 mLdapClient.startQuery(QStringLiteral("objectclass=*"));
142 void ResourceItem::setLdapObject(const KLDAP::LdapObject &obj)
144 slotLDAPResult(mLdapClient, obj);
147 const KLDAP::LdapClient &ResourceItem::ldapClient() const
149 return mLdapClient;
152 void ResourceItem::slotLDAPResult(const KLDAP::LdapClient &/*client*/,
153 const KLDAP::LdapObject &obj)
155 mLdapObject = obj;
156 foreach (const QString &header, mAttrs) {
157 if (!obj.attributes()[header].isEmpty()) {
158 itemData << QString::fromUtf8(obj.attributes()[header][0]);
159 } else {
160 itemData << "";
163 emit searchFinished();