libkdepim

ldapclient.h

00001 /* kldapclient.h - LDAP access
00002  *      Copyright (C) 2002 Klar�vdalens 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 #include <kconfig.h>
00037 
00038 #include <kdepimmacros.h>
00039 
00040 namespace KPIM {
00041 
00042 class LdapClient;
00043 typedef QValueList<QByteArray> LdapAttrValue;
00044 typedef QMap<QString,LdapAttrValue > LdapAttrMap;
00045 
00046 class LdapServer
00047 {
00048   public:
00049     LdapServer()
00050     : mPort( 389 ),
00051       mTimeLimit(0),
00052       mSizeLimit(0),
00053       mVersion(2),
00054       mSecurity(Sec_None),
00055       mAuth( LdapServer::Anonymous )
00056     {}
00057 
00058     enum Security{ Sec_None, TLS, SSL };
00059     enum Auth{ Anonymous, Simple, SASL };
00060     QString host() const { return mHost; }
00061     int port() const { return mPort; }
00062     const QString &baseDN() const { return mBaseDN; }
00063     const QString &user() const { return mUser; }
00064     const QString &bindDN() const { return mBindDN; }
00065     const QString &pwdBindDN() const { return mPwdBindDN; }
00066     int timeLimit() const { return mTimeLimit; }
00067     int sizeLimit() const { return mSizeLimit; }
00068     int version() const { return mVersion; }
00069     int security() const { return mSecurity; }
00070     int auth() const { return mAuth; }
00071     const QString &mech() const { return mMech; }
00072 
00073     void setHost( const QString &host ) { mHost = host; }
00074     void setPort( int port ) { mPort = port; }
00075     void setBaseDN( const QString &baseDN ) {  mBaseDN = baseDN; }
00076     void setUser( const QString &user ) { mUser = user; }
00077     void setBindDN( const QString &bindDN ) {  mBindDN = bindDN; }
00078     void setPwdBindDN( const QString &pwdBindDN ) {  mPwdBindDN = pwdBindDN; }
00079     void setTimeLimit( int timelimit ) { mTimeLimit = timelimit; }
00080     void setSizeLimit( int sizelimit ) { mSizeLimit = sizelimit; }
00081     void setVersion( int version ) { mVersion = version; }
00082     void setSecurity( int security ) { mSecurity = security; } //0-No, 1-TLS, 2-SSL - KDE4: add an enum to Lda
00083     void setAuth( int auth ) { mAuth = auth; } //0-Anonymous, 1-simple, 2-SASL - KDE4: add an enum to LdapCon
00084     void setMech( const QString &mech ) { mMech = mech; }
00085 
00086   private:
00087     QString mHost;
00088     int mPort;
00089     QString mBaseDN;
00090     QString mUser;
00091     QString mBindDN;
00092     QString mPwdBindDN;
00093     QString mMech;
00094     int mTimeLimit, mSizeLimit, mVersion, mSecurity, mAuth;
00095 };
00096 
00097 
00105 class LdapObject
00106 {
00107   public:
00108     LdapObject()
00109       : dn( QString::null ), client( 0 ) {}
00110     explicit LdapObject( const QString& _dn, LdapClient* _cl ) : dn( _dn ), client( _cl ) {}
00111     LdapObject( const LdapObject& that ) { assign( that ); }
00112 
00113     LdapObject& operator=( const LdapObject& that )
00114     {
00115       assign( that );
00116       return *this;
00117     }
00118 
00119     QString toString() const;
00120 
00121     void clear();
00122 
00123     QString dn;
00124     QString objectClass;
00125     LdapAttrMap attrs;
00126     LdapClient* client;
00127 
00128   protected:
00129     void assign( const LdapObject& that );
00130 
00131   private:
00132     //class LdapObjectPrivate* d;
00133 };
00134 
00142 class KDE_EXPORT LdapClient : public QObject
00143 {
00144   Q_OBJECT
00145 
00146   public:
00147     LdapClient( int clientNumber, QObject* parent = 0, const char* name = 0 );
00148     virtual ~LdapClient();
00149 
00151     bool isActive() const { return mActive; }
00152 
00153     int clientNumber() const;
00154     int completionWeight() const;
00155     void setCompletionWeight( int );
00156 
00157     const LdapServer& server() { return mServer; }
00158     void setServer( const LdapServer &server ) { mServer = server; }
00163     QStringList attrs() const { return mAttrs; }
00164 
00165   signals:
00167     void done();
00168 
00170     void error( const QString& );
00171 
00175     void result( const KPIM::LdapObject& );
00176 
00177   public slots: // why are those slots?
00182     void setAttrs( const QStringList& attrs );
00183 
00184     void setScope( const QString scope ) { mScope = scope; }
00185 
00189     void startQuery( const QString& filter );
00190 
00194     void cancelQuery();
00195 
00196   protected slots:
00197     void slotData( KIO::Job*, const QByteArray &data );
00198     void slotInfoMessage( KIO::Job*, const QString &info );
00199     void slotDone();
00200 
00201   protected:
00202     void startParseLDIF();
00203     void parseLDIF( const QByteArray& data );
00204     void endParseLDIF();
00205     void finishCurrentObject();
00206 
00207     LdapServer mServer;
00208     QString mScope;
00209     QStringList mAttrs;
00210 
00211     QGuardedPtr<KIO::SimpleJob> mJob;
00212     bool mActive;
00213     bool mReportObjectClass;
00214 
00215     LdapObject mCurrentObject;
00216 
00217   private:
00218     KABC::LDIF mLdif;
00219     int mClientNumber;
00220     int mCompletionWeight;
00221 
00222 //    class LdapClientPrivate;
00223 //    LdapClientPrivate* d;
00224 };
00225 
00229 struct LdapResult {
00230   QString name;     
00231   QStringList email;    
00232   int clientNumber; 
00233   int completionWeight; 
00234 };
00235 typedef QValueList<LdapResult> LdapResultList;
00236 
00237 
00245 class KDE_EXPORT LdapSearch : public QObject
00246 {
00247   Q_OBJECT
00248 
00249   public:
00250     LdapSearch();
00251 
00252     static KConfig *config();
00253     static void readConfig( LdapServer &server, KConfig *config, int num, bool active );
00254     static void writeConfig( const LdapServer &server, KConfig *config, int j, bool active );
00255 
00256     void startSearch( const QString& txt );
00257     void cancelSearch();
00258     bool isAvailable() const;
00259 
00260     QValueList< LdapClient* > clients() const { return mClients; }
00261 
00262   signals:
00265     void searchData( const QStringList& );
00268     void searchData( const KPIM::LdapResultList& );
00269     void searchDone();
00270 
00271   private slots:
00272     void slotLDAPResult( const KPIM::LdapObject& );
00273     void slotLDAPError( const QString& );
00274     void slotLDAPDone();
00275     void slotDataTimer();
00276     void slotFileChanged( const QString& );
00277 
00278   private:
00279     void readConfig();
00280     void finish();
00281     void makeSearchData( QStringList& ret, LdapResultList& resList );
00282     QValueList< LdapClient* > mClients;
00283     QString mSearchText;
00284     QTimer mDataTimer;
00285     int mActiveClients;
00286     bool mNoLDAPLookup;
00287     QValueList< LdapObject > mResults;
00288     QString mConfigFile;
00289 
00290   private:
00291     static KConfig *s_config;
00292     class LdapSearchPrivate* d;
00293 };
00294 
00295 }
00296 #endif // KPIM_LDAPCLIENT_H
KDE Home | KDE Accessibility Home | Description of Access Keys