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