1/*
2 A temporary copy to break dependency to KLDAP
3
4 This file is part of libkldap.
5 Copyright (c) 2004-2006 Szombathelyi György <gyurco@freemail.hu>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library 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 GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#ifndef LDIF_P_H
24#define LDIF_P_H
25
26#include <QtCore/QString>
27#include <QtCore/QByteArray>
28
29#include "ldapdn_p.h"
30
31/**
32 * Ldif
33 *
34 * Ldif implements an RFC 2849 compliant Ldif parser. Ldif files are used to
35 * represent directory information on LDAP-based servers, or to describe a set
36 * of changes which are to be applied to a directory.
37 */
38class Ldif
39{
40 public:
41
42 typedef enum {
43 None, NewEntry, EndEntry, Item, Control, Err, MoreData
44 } ParseValue;
45
46 typedef enum {
47 Entry_None, Entry_Add, Entry_Del, Entry_Mod, Entry_Modrdn
48 } EntryType;
49
50 typedef enum {
51 Mod_None, Mod_Add, Mod_Replace, Mod_Del
52 } ModType;
53
54 Ldif();
55
56 Ldif( const Ldif &that );
57 Ldif &operator=( const Ldif &that );
58
59 virtual ~Ldif();
60
61 /**
62 * Assembles fieldname and value into a valid Ldif line, BASE64 encodes the
63 * value if necessary and optionally splits into more lines.
64 * @param fieldname The name of the entry.
65 * @param value The value of the entry.
66 * @param linelen Maximum length of the lines in the result.
67 * @param url If true, encode value as url ( use :< ).
68 */
69 static QByteArray assembleLine( const QString &fieldname,
70 const QByteArray &value, uint linelen=0,
71 bool url=false );
72 /**
73 * This is the same as the above function, the only difference that
74 * this accepts QString as the value.
75 */
76 static QByteArray assembleLine( const QString &fieldname,
77 const QString &value, uint linelen=0,
78 bool url=false );
79
80 /**
81 * Splits one line from an Ldif file to attribute and value components.
82 * @return true if value is an URL, false otherwise
83 */
84 static bool splitLine( const QByteArray &line, QString &fieldname,
85 QByteArray &value );
86
87 /**
88 * Splits a control specification (without the "control:" directive)
89 * @param line is the control directive
90 * @param oid will contain the OID
91 * @param critical will contain the criticality of control
92 * @param value is the control value
93 */
94 static bool splitControl( const QByteArray &line, QString &oid,
95 bool &critical, QByteArray &value );
96
97 /**
98 * Starts the parsing of a new Ldif
99 */
100 void startParsing();
101
102 /**
103 * Process one Ldif line
104 */
105 ParseValue processLine();
106
107 /**
108 * Process the Ldif until a complete item can be returned
109 * @return NewEntry if a new DN encountered, Item if a new item returned,
110 * Err if the Ldif contains error, EndEntry if the parser reached the end
111 * of the current entry and MoreData if the parser encountered the end of
112 * the current chunk of the Ldif.
113 *
114 * If you want to finish the parsing after receiving MoreData, then call
115 * endLdif(), so the parser can safely flush the current entry.
116 */
117 ParseValue nextItem();
118
119 /**
120 * Sets a chunk of Ldif. Call before startParsing(), or if nextItem()
121 * returned MoreData.
122 */
123 void setLdif( const QByteArray &ldif );
124
125 /**
126 * Indicates the end of the Ldif file/stream. Call if nextItem() returned
127 * MoreData, but actually you don't have more data.
128 */
129 void endLdif();
130
131 /**
132 * Returns the requested LDAP operation extracted from the current entry.
133 */
134 EntryType entryType() const;
135
136 /**
137 * Returns the LDAP modify request type if entryType() returned Entry_Mod.
138 */
139 int modType() const;
140
141 /**
142 * Returns the Distinguished Name of the current entry.
143 */
144 LdapDN dn() const;
145
146 /**
147 * Returns the new Relative Distinguished Name if modType() returned
148 * Entry_Modrdn.
149 */
150 QString newRdn() const;
151
152 /**
153 * Returns the new parent of the entry if modType() returned Entry_Modrdn.
154 */
155 QString newSuperior() const;
156
157 /**
158 * Returns if the delete of the old RDN is required.
159 */
160 bool delOldRdn() const;
161
162 /**
163 * Returns the attribute name.
164 */
165 QString attr() const;
166
167 /**
168 * Returns the attribute value.
169 */
170 QByteArray value() const;
171
172 /**
173 * Returns if val() is an url
174 */
175 bool isUrl() const;
176
177 /**
178 * Returns the criticality level when modType() returned Control.
179 */
180 bool isCritical() const;
181
182 /**
183 * Returns the OID when modType() returned Control.
184 */
185 QString oid() const;
186
187 /**
188 * Returns the line number which the parser processes.
189 */
190 uint lineNumber() const;
191
192 private:
193 class LdifPrivate;
194 LdifPrivate *const d;
195};
196
197#endif
198