Incidenceeditor

resourceitem.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 *
6 */
7#include "resourceitem.h"
8
9#include <KLDAPCore/LdapServer>
10
11using namespace IncidenceEditorNG;
12
13ResourceItem::ResourceItem(const KLDAPCore::LdapDN &dn, const QStringList &attrs, const KLDAPCore::LdapClient &ldapClient, const ResourceItem::Ptr &parent)
14 : parentItem(parent)
15 , dn(dn)
16 , mAttrs(attrs)
17 , mLdapClient(0, this)
18{
19 if (!dn.isEmpty()) {
20 KLDAPCore::LdapServer server = ldapClient.server();
21
22 server.setScope(KLDAPCore::LdapUrl::Base);
23 server.setBaseDn(dn);
24 mLdapClient.setServer(server);
25
26 connect(&mLdapClient, &KLDAPCore::LdapClient::result, this, &ResourceItem::slotLDAPResult);
27
28 mAttrs << QStringLiteral("uniqueMember");
29 mLdapClient.setAttributes(attrs);
30 } else {
31 itemData.reserve(mAttrs.count());
32 for (const QString &header : std::as_const(mAttrs)) {
33 itemData << header;
34 }
35 }
36}
37
38ResourceItem::~ResourceItem() = default;
39
40ResourceItem::Ptr ResourceItem::child(int number)
41{
42 return childItems.value(number);
43}
44
45int ResourceItem::childCount() const
46{
47 return childItems.count();
48}
49
50int ResourceItem::childNumber() const
51{
52 if (parentItem) {
53 int i = 0;
54 for (const ResourceItem::Ptr &child : std::as_const(parentItem->childItems)) {
55 if (child == this) {
56 return i;
57 }
58 i++;
59 }
60 }
61
62 return 0;
63}
64
65int ResourceItem::columnCount() const
66{
67 return itemData.count();
68}
69
70QVariant ResourceItem::data(int column) const
71{
72 return itemData.value(column);
73}
74
75QVariant ResourceItem::data(const QString &column) const
76{
77 if (!mLdapObject.attributes()[column].isEmpty()) {
78 return QString::fromUtf8(mLdapObject.attributes()[column][0]);
79 }
80 return {};
81}
82
83bool ResourceItem::insertChild(int position, const ResourceItem::Ptr &item)
84{
85 if (position < 0 || position > childItems.size()) {
86 return false;
87 }
88
89 childItems.insert(position, item);
90
91 return true;
92}
93
94ResourceItem::Ptr ResourceItem::parent()
95{
96 return parentItem;
97}
98
99bool ResourceItem::removeChildren(int position, int count)
100{
101 if (position < 0 || position + count > childItems.size()) {
102 return false;
103 }
104
105 for (int row = 0; row < count; ++row) {
106 childItems.removeAt(position);
107 }
108
109 return true;
110}
111
112const QStringList &ResourceItem::attributes() const
113{
114 return mAttrs;
115}
116
117const KLDAPCore::LdapObject &ResourceItem::ldapObject() const
118{
119 return mLdapObject;
120}
121
122void ResourceItem::startSearch()
123{
124 mLdapClient.startQuery(QStringLiteral("objectclass=*"));
125}
126
127void ResourceItem::setLdapObject(const KLDAPCore::LdapObject &obj)
128{
129 slotLDAPResult(mLdapClient, obj);
130}
131
132const KLDAPCore::LdapClient &ResourceItem::ldapClient() const
133{
134 return mLdapClient;
135}
136
137void ResourceItem::slotLDAPResult(const KLDAPCore::LdapClient &client, const KLDAPCore::LdapObject &obj)
138{
139 Q_UNUSED(client)
140 mLdapObject = obj;
141 for (const QString &header : std::as_const(mAttrs)) {
142 if (!obj.attributes()[header].isEmpty()) {
143 itemData << QString::fromUtf8(obj.attributes()[header][0]);
144 } else {
145 itemData << QString();
146 }
147 }
148 Q_EMIT searchFinished();
149}
150
151#include "moc_resourceitem.cpp"
void startQuery(const QString &filter)
const LdapAttrMap & attributes() const
qsizetype count() const const
T value(qsizetype i) const const
Q_EMITQ_EMIT
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.