1/*
2 This file is part of libkldap.
3 Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "ldapobject.h"
22#include "ldif.h"
23
24#include <QtCore/QSharedData>
25
26using namespace KLDAP;
27
28class LdapObject::Private : public QSharedData
29{
30 public:
31 Private()
32 {
33 }
34
35 Private( const Private &other )
36 : QSharedData( other )
37 {
38 mDn = other.mDn;
39 mAttrs = other.mAttrs;
40 }
41
42 LdapDN mDn;
43 LdapAttrMap mAttrs;
44};
45
46LdapObject::LdapObject()
47 : d( new Private )
48{
49}
50
51LdapObject::LdapObject( const QString &dn )
52 : d( new Private )
53{
54 d->mDn = LdapDN( dn );
55}
56
57LdapObject::~LdapObject()
58{
59}
60
61LdapObject::LdapObject( const LdapObject &that )
62 : d( that.d )
63{
64}
65
66LdapObject &LdapObject::operator=( const LdapObject &that )
67{
68 if ( this != &that ) {
69 d = that.d;
70 }
71
72 return *this;
73}
74
75void LdapObject::setDn( const LdapDN &dn )
76{
77 d->mDn = dn;
78}
79
80void LdapObject::setDn( const QString &dn )
81{
82 d->mDn = LdapDN( dn );
83}
84
85void LdapObject::setAttributes( const LdapAttrMap &attrs )
86{
87 d->mAttrs = attrs;
88}
89
90LdapDN LdapObject::dn() const
91{
92 return d->mDn;
93}
94
95const LdapAttrMap &LdapObject::attributes() const
96{
97 return d->mAttrs;
98}
99
100QString LdapObject::toString() const
101{
102 QString result = QString::fromLatin1( "dn: %1\n" ).arg( d->mDn.toString() );
103 LdapAttrMap::ConstIterator end( d->mAttrs.constEnd() );
104 for ( LdapAttrMap::ConstIterator it = d->mAttrs.constBegin(); it != end; ++it ) {
105 const QString attr = it.key();
106 LdapAttrValue::ConstIterator end2( ( *it ).constEnd() );
107 for ( LdapAttrValue::ConstIterator it2 = ( *it ).constBegin(); it2 != end2; ++it2 ) {
108 result += QString::fromUtf8( Ldif::assembleLine( attr, *it2, 76 ) ) + QLatin1Char('\n');
109 }
110 }
111 return result;
112}
113
114void LdapObject::clear()
115{
116 d->mDn.clear();
117 d->mAttrs.clear();
118}
119
120void LdapObject::setValues( const QString &attributeName, const LdapAttrValue &values )
121{
122 d->mAttrs[ attributeName ] = values;
123}
124
125void LdapObject::addValue( const QString &attributeName, const QByteArray &value )
126{
127 d->mAttrs[ attributeName ].append( value );
128}
129
130LdapAttrValue LdapObject::values( const QString &attributeName ) const
131{
132 if ( hasAttribute( attributeName ) ) {
133 return d->mAttrs.value( attributeName );
134 } else {
135 return LdapAttrValue();
136 }
137}
138
139QByteArray LdapObject::value( const QString &attributeName ) const
140{
141 if ( hasAttribute( attributeName ) ) {
142 return d->mAttrs.value( attributeName ).first();
143 } else {
144 return QByteArray();
145 }
146}
147
148bool LdapObject::hasAttribute( const QString &attributeName ) const
149{
150 return d->mAttrs.contains( attributeName );
151}
152