xldaplite.h
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 2005 Willem Verschuur <willverschuur@yahoo.com> 00004 Copyright (c) 2006 Helio Chissini de Castro <helio@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 /* 00023 * Lightweight openldap api wrappers, just makes the 00024 * whole memory management schlep so much easier 00025 * 00026 * test: exception handling.. try to break it 00027 * 00028 * note: dont pass these classes as results for functions.. 00029 * for that you need to pull the variables into a counter 00030 * referenced core structure. 00031 * 00032 * todo: ldapnode class for a truer in-memory representation 00033 * of LDAP tree.. and easier query coding 00034 */ 00035 00036 #ifndef _XLDAPLITE_H 00037 #define _XLDAPLITE_H 00038 00039 00040 00041 #include <ldap.h> 00042 #include <qstring.h> 00043 #include <qstringlist.h> 00044 #include <qptrlist.h> 00045 00046 00047 00048 class xLDAPquery; 00049 class xLDAPiterator; 00050 class xLDAPattribute; 00051 00052 00053 00054 class xLDAPconnection 00055 { 00056 00057 00058 friend class xLDAPquery; 00059 friend class xLDAPiterator; 00060 friend class xLDAPattribute; 00061 00062 00063 public: 00064 00065 00066 // read /etc/ldap.conf 00067 xLDAPconnection(); 00068 00069 00070 // free resources on object destruction 00071 ~xLDAPconnection(); 00072 00073 00074 // connect to ldap server 00075 bool connect(); 00076 00077 00078 // free resources 00079 void disconnect(); 00080 00081 00082 // accessor 00083 const LDAP* connection() { return m_Connection; } 00084 const QString& base() { return m_Base; } 00085 const QString& password() { return m_RootPassword; } 00086 00087 00088 protected: 00089 00090 00091 LDAP* m_Connection; 00092 QString m_RootPassword; 00093 ber_int_t m_Scope; 00094 QString m_Base; 00095 00096 00097 }; 00098 00099 00100 00101 class xLDAPquery 00102 { 00103 00104 00105 // iterator should have access to members 00106 friend class xLDAPiterator; 00107 friend class xLDAPattribute; 00108 00109 00110 public: 00111 00112 00113 // execute a query 00114 xLDAPquery( xLDAPconnection&, 00115 const QString& filter, 00116 const QString& base = 0, 00117 char** attrs = 0 ); 00118 00119 00120 // free resources on object destruction 00121 ~xLDAPquery(); 00122 00123 00124 protected: 00125 00126 00127 xLDAPconnection* m_Connection; 00128 LDAPMessage* m_Query; 00129 int m_Result; 00130 00131 00132 }; 00133 00134 00135 00136 class xLDAPiterator 00137 { 00138 00139 00140 friend class xLDAPattribute; 00141 00142 00143 public: 00144 00145 00146 xLDAPiterator(xLDAPquery&); 00147 00148 00149 ~xLDAPiterator(); 00150 00151 00152 operator bool(void); 00153 00154 00155 void operator ++ (void); 00156 00157 00158 protected: 00159 00160 00161 void clearElement(); 00162 00163 00164 xLDAPquery* m_Query; 00165 LDAPMessage* m_Entry; 00166 BerElement* m_Element; // used by attribute functions 00167 00168 00169 }; 00170 00171 00172 00173 class xLDAPattribute 00174 { 00175 00176 00177 public: 00178 00179 00180 xLDAPattribute(xLDAPiterator&); 00181 00182 00183 ~xLDAPattribute(); 00184 00185 00186 operator bool(void); 00187 00188 00189 void operator ++ (void); 00190 00191 00192 const char* attribute() { return m_Attribute; } 00193 00194 00195 const char* firstValue(); 00196 const char* nextValue(); 00197 00198 00199 protected: 00200 00201 00202 void clearAttribute(); 00203 void clearValues(); 00204 00205 00206 xLDAPiterator* m_Iterator; 00207 char* m_Attribute; // attribute 00208 char** m_Values; 00209 char** m_CurrentValue; 00210 00211 00212 }; 00213 00214 00215 00216 #endif 00217