libkdepim

ldapclient.h

00001 /* kldapclient.h - LDAP access
00002  *      Copyright (C) 2002 Klarälvdalens Datakonsult AB
00003  *
00004  *      Author: Steffen Hansen <hansen@kde.org>
00005  *
00006  * This file is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This file 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
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00019  */
00020 
00021 
00022 #ifndef KPIM_LDAPCLIENT_H
00023 #define KPIM_LDAPCLIENT_H
00024 
00025 
00026 #include <qobject.h>
00027 #include <qstring.h>
00028 #include <qcstring.h>
00029 #include <qstringlist.h>
00030 #include <qmemarray.h>
00031 #include <qguardedptr.h>
00032 #include <qtimer.h>
00033 
00034 #include <kio/job.h>
00035 #include <kabc/ldif.h>
00036 
00037 #include <kdepimmacros.h>
00038 
00039 namespace KPIM {
00040 
00041 class LdapClient;
00042 typedef QValueList<QByteArray> LdapAttrValue;
00043 typedef QMap<QString,LdapAttrValue > LdapAttrMap;
00044 
00045 class LdapServer
00046 {
00047   public:
00048     LdapServer() : mPort( 389 ) {}
00049 
00050     enum Security{ Sec_None, TLS, SSL };
00051     enum Auth{ Anonymous, Simple, SASL };
00052     QString host() const { return mHost; }
00053     int port() const { return mPort; }
00054     const QString &baseDN() const { return mBaseDN; }
00055     const QString &user() const { return mUser; }
00056     const QString &bindDN() const { return mBindDN; }
00057     const QString &pwdBindDN() const { return mPwdBindDN; }
00058     int timeLimit() const { return mTimeLimit; }
00059     int sizeLimit() const { return mSizeLimit; }
00060     int version() const { return mVersion; }
00061     int security() const { return mSecurity; }
00062     int auth() const { return mAuth; }
00063     const QString &mech() const { return mMech; }
00064 
00065     void setHost( const QString &host ) { mHost = host; }
00066     void setPort( int port ) { mPort = port; }
00067     void setBaseDN( const QString &baseDN ) {  mBaseDN = baseDN; }
00068     void setUser( const QString &user ) { mUser = user; }
00069     void setBindDN( const QString &bindDN ) {  mBindDN = bindDN; }
00070     void setPwdBindDN( const QString &pwdBindDN ) {  mPwdBindDN = pwdBindDN; }
00071     void setTimeLimit( int timelimit ) { mTimeLimit = timelimit; }
00072     void setSizeLimit( int sizelimit ) { mSizeLimit = sizelimit; }
00073     void setVersion( int version ) { mVersion = version; }
00074     void setSecurity( int security ) { mSecurity = security; } //0-No, 1-TLS, 2-SSL - KDE4: add an enum to Lda
00075     void setAuth( int auth ) { mAuth = auth; }; //0-Anonymous, 1-simple, 2-SASL - KDE4: add an enum to LdapCon
00076     void setMech( const QString &mech ) { mMech = mech; }
00077 
00078   private:
00079     QString mHost;
00080     int mPort;
00081     QString mBaseDN;
00082     QString mUser;
00083     QString mBindDN;
00084     QString mPwdBindDN;
00085     QString mMech;
00086     int mTimeLimit, mSizeLimit, mVersion, mSecurity, mAuth;
00087 };
00088 
00089 
00097 class LdapObject
00098 {
00099   public:
00100     LdapObject()
00101       : dn( QString::null ), client( 0 ) {}
00102     explicit LdapObject( const QString& _dn, LdapClient* _cl ) : dn( _dn ), client( _cl ) {}
00103     LdapObject( const LdapObject& that ) { assign( that ); }
00104 
00105     LdapObject& operator=( const LdapObject& that )
00106     {
00107       assign( that );
00108       return *this;
00109     }
00110 
00111     QString toString() const;
00112 
00113     void clear();
00114 
00115     QString dn;
00116     QString objectClass;
00117     LdapAttrMap attrs;
00118     LdapClient* client;
00119 
00120   protected:
00121     void assign( const LdapObject& that );
00122 
00123   private:
00124     //class LdapObjectPrivate* d;
00125 };
00126 
00134 class KDE_EXPORT LdapClient : public QObject
00135 {
00136   Q_OBJECT
00137 
00138   public:
00139     LdapClient( int clientNumber, QObject* parent = 0, const char* name = 0 );
00140     virtual ~LdapClient();
00141 
00143     bool isActive() const { return mActive; }
00144 
00145     int clientNumber() const;
00146     int completionWeight() const;
00147     void setCompletionWeight( int );
00148 
00149     const LdapServer& server() { return mServer; }
00150     void setServer( const LdapServer &server ) { mServer = server; }
00155     QStringList attrs() const { return mAttrs; }
00156 
00157   signals:
00159     void done();
00160 
00162     void error( const QString& );
00163 
00167     void result( const KPIM::LdapObject& );
00168 
00169   public slots: // why are those slots?
00174     void setAttrs( const QStringList& attrs );
00175 
00176     void setScope( const QString scope ) { mScope = scope; }
00177 
00181     void startQuery( const QString& filter );
00182 
00186     void cancelQuery();
00187 
00188   protected slots:
00189     void slotData( KIO::Job*, const QByteArray &data );
00190     void slotInfoMessage( KIO::Job*, const QString &info );
00191     void slotDone();
00192 
00193   protected:
00194     void startParseLDIF();
00195     void parseLDIF( const QByteArray& data );
00196     void endParseLDIF();
00197     void finishCurrentObject();
00198 
00199     LdapServer mServer;
00200     QString mScope;
00201     QStringList mAttrs;
00202 
00203     QGuardedPtr<KIO::SimpleJob> mJob;
00204     bool mActive;
00205     bool mReportObjectClass;
00206 
00207     LdapObject mCurrentObject;
00208 
00209   private:
00210     KABC::LDIF mLdif;
00211     int mClientNumber;
00212     int mCompletionWeight;
00213 
00214 //    class LdapClientPrivate;
00215 //    LdapClientPrivate* d;
00216 };
00217 
00221 struct LdapResult {
00222   QString name;     
00223   QStringList email;    
00224   int clientNumber; 
00225   int completionWeight; 
00226 };
00227 typedef QValueList<LdapResult> LdapResultList;
00228 
00229 
00237 class KDE_EXPORT LdapSearch : public QObject
00238 {
00239   Q_OBJECT
00240 
00241   public:
00242     LdapSearch();
00243 
00244     static KConfig *config();
00245     static void readConfig( LdapServer &server, KConfig *config, int num, bool active );
00246     static void writeConfig( const LdapServer &server, KConfig *config, int j, bool active );
00247 
00248     void startSearch( const QString& txt );
00249     void cancelSearch();
00250     bool isAvailable() const;
00251 
00252     QValueList< LdapClient* > clients() const { return mClients; }
00253 
00254   signals:
00257     void searchData( const QStringList& );
00260     void searchData( const KPIM::LdapResultList& );
00261     void searchDone();
00262 
00263   private slots:
00264     void slotLDAPResult( const KPIM::LdapObject& );
00265     void slotLDAPError( const QString& );
00266     void slotLDAPDone();
00267     void slotDataTimer();
00268     void slotFileChanged( const QString& );
00269 
00270   private:
00271     void readConfig();
00272     void finish();
00273     void makeSearchData( QStringList& ret, LdapResultList& resList );
00274     QValueList< LdapClient* > mClients;
00275     QString mSearchText;
00276     QTimer mDataTimer;
00277     int mActiveClients;
00278     bool mNoLDAPLookup;
00279     QValueList< LdapObject > mResults;
00280     QString mConfigFile;
00281 
00282   private:
00283     static KConfig *s_config;
00284     class LdapSearchPrivate* d;
00285 };
00286 
00287 }
00288 #endif // KPIM_LDAPCLIENT_H
KDE Home | KDE Accessibility Home | Description of Access Keys