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 "ldapcontrol.h"
22#include "ber.h"
23
24#include <QtCore/QSharedData>
25
26using namespace KLDAP;
27
28class LdapControl::Private : public QSharedData
29{
30 public:
31 Private()
32 {
33 mCritical = false;
34 }
35
36 Private( const Private &other )
37 : QSharedData( other )
38 {
39 mOid = other.mOid;
40 mValue = other.mValue;
41 mCritical = other.mCritical;
42 }
43
44 QString mOid;
45 QByteArray mValue;
46 bool mCritical;
47};
48
49LdapControl::LdapControl()
50 : d( new Private )
51{
52 setControl( QString(), QByteArray(), false );
53}
54
55LdapControl::LdapControl( QString &oid, QByteArray &value, bool critical )
56 : d( new Private )
57{
58 setControl( oid, value, critical );
59}
60
61LdapControl::LdapControl( const LdapControl &that )
62 : d( that.d )
63{
64 setControl( that.d->mOid, that.d->mValue, that.d->mCritical );
65}
66
67LdapControl &LdapControl::operator= ( const LdapControl &that )
68{
69 if ( this != &that ) {
70 d = that.d;
71 }
72
73 setControl( that.d->mOid, that.d->mValue, that.d->mCritical );
74
75 return *this;
76}
77
78LdapControl::~LdapControl()
79{
80}
81
82void LdapControl::setControl( const QString &oid, const QByteArray &value, bool critical )
83{
84 d->mOid = oid;
85 d->mValue = value;
86 d->mCritical = critical;
87}
88
89QString LdapControl::oid() const
90{
91 return d->mOid;
92}
93
94QByteArray LdapControl::value() const
95{
96 return d->mValue;
97}
98
99bool LdapControl::critical() const
100{
101 return d->mCritical;
102}
103
104void LdapControl::setOid( const QString &oid )
105{
106 d->mOid = oid;
107}
108
109void LdapControl::setValue( const QByteArray &value )
110{
111 d->mValue = value;
112}
113
114void LdapControl::setCritical( bool critical )
115{
116 d->mCritical = critical;
117}
118
119int LdapControl::parsePageControl( QByteArray &cookie ) const
120{
121 if ( d->mOid != QLatin1String("1.2.840.113556.1.4.319") ) {
122 return -1;
123 }
124
125 Ber ber( d->mValue );
126 int size;
127 if ( ber.scanf( QLatin1String("{iO}"), &size, &cookie ) == -1 ) {
128 return -1;
129 } else {
130 return size;
131 }
132}
133
134LdapControl LdapControl::createPageControl( int pagesize, const QByteArray &cookie )
135{
136 LdapControl control;
137 Ber ber;
138
139 ber.printf( QLatin1String("{iO}"), pagesize, &cookie );
140 control.setOid( QLatin1String("1.2.840.113556.1.4.319") );
141 control.setValue( ber.flatten() );
142 return control;
143}
144
145void LdapControl::insert( LdapControls &list, const LdapControl &ctrl )
146{
147 LdapControls::iterator it;
148 LdapControls::iterator endit = list.end();
149 const QString oid = ctrl.oid();
150
151 for ( it = list.begin(); it != endit; ++it ) {
152 if ( it->oid() == oid ) {
153 *it = ctrl;
154 return;
155 }
156 }
157 list.append( ctrl );
158}
159