libtdepim

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 <tqobject.h>
00027 #include <tqstring.h>
00028 #include <tqcstring.h>
00029 #include <tqstringlist.h>
00030 #include <tqmemarray.h>
00031 #include <tqguardedptr.h>
00032 #include <tqtimer.h>
00033 
00034 #include <tdeio/job.h>
00035 #include <tdeabc/ldif.h>
00036 #include <tdeconfig.h>
00037 
00038 #include <tdepimmacros.h>
00039 
00040 namespace KPIM {
00041 
00042 class LdapClient;
00043 typedef TQValueList<TQByteArray> LdapAttrValue;
00044 typedef TQMap<TQString,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     TQString host() const { return mHost; }
00061     int port() const { return mPort; }
00062     const TQString &baseDN() const { return mBaseDN; }
00063     const TQString &user() const { return mUser; }
00064     const TQString &bindDN() const { return mBindDN; }
00065     const TQString &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 TQString &mech() const { return mMech; }
00072 
00073     void setHost( const TQString &host ) { mHost = host; }
00074     void setPort( int port ) { mPort = port; }
00075     void setBaseDN( const TQString &baseDN ) {  mBaseDN = baseDN; }
00076     void setUser( const TQString &user ) { mUser = user; }
00077     void setBindDN( const TQString &bindDN ) {  mBindDN = bindDN; }
00078     void setPwdBindDN( const TQString &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 TQString &mech ) { mMech = mech; }
00085 
00086   private:
00087     TQString mHost;
00088     int mPort;
00089     TQString mBaseDN;
00090     TQString mUser;
00091     TQString mBindDN;
00092     TQString mPwdBindDN;
00093     TQString mMech;
00094     int mTimeLimit, mSizeLimit, mVersion, mSecurity, mAuth;
00095 };
00096 
00097 
00105 class LdapObject
00106 {
00107   public:
00108     LdapObject()
00109       : dn( TQString() ), client( 0 ) {}
00110     explicit LdapObject( const TQString& _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     TQString toString() const;
00120 
00121     void clear();
00122 
00123     TQString dn;
00124     TQString 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 TQObject
00143 {
00144   Q_OBJECT
00145   
00146 
00147   public:
00148     LdapClient( int clientNumber, TQObject* parent = 0, const char* name = 0 );
00149     virtual ~LdapClient();
00150 
00152     bool isActive() const { return mActive; }
00153 
00154     int clientNumber() const;
00155     int completionWeight() const;
00156     void setCompletionWeight( int );
00157 
00158     const LdapServer& server() { return mServer; }
00159     void setServer( const LdapServer &server ) { mServer = server; }
00164     TQStringList attrs() const { return mAttrs; }
00165 
00166   signals:
00168     void done();
00169 
00171     void error( const TQString& );
00172 
00176     void result( const KPIM::LdapObject& );
00177 
00178   public slots: // why are those slots?
00183     void setAttrs( const TQStringList& attrs );
00184 
00185     void setScope( const TQString scope ) { mScope = scope; }
00186 
00190     void startQuery( const TQString& filter );
00191 
00195     void cancelQuery();
00196 
00197   protected slots:
00198     void slotData( TDEIO::Job*, const TQByteArray &data );
00199     void slotInfoMessage( TDEIO::Job*, const TQString &info );
00200     void slotDone();
00201 
00202   protected:
00203     void startParseLDIF();
00204     void parseLDIF( const TQByteArray& data );
00205     void endParseLDIF();
00206     void finishCurrentObject();
00207 
00208     LdapServer mServer;
00209     TQString mScope;
00210     TQStringList mAttrs;
00211 
00212     TQGuardedPtr<TDEIO::SimpleJob> mJob;
00213     bool mActive;
00214     bool mReportObjectClass;
00215 
00216     LdapObject mCurrentObject;
00217 
00218   private:
00219     TDEABC::LDIF mLdif;
00220     int mClientNumber;
00221     int mCompletionWeight;
00222 
00223 //    class LdapClientPrivate;
00224 //    LdapClientPrivate* d;
00225 };
00226 
00230 struct LdapResult {
00231   TQString name;     
00232   TQStringList email;    
00233   int clientNumber; 
00234   int completionWeight; 
00235 };
00236 typedef TQValueList<LdapResult> LdapResultList;
00237 
00238 
00246 class KDE_EXPORT LdapSearch : public TQObject
00247 {
00248   Q_OBJECT
00249   
00250 
00251   public:
00252     LdapSearch();
00253 
00254     static TDEConfig *config();
00255     static void readConfig( LdapServer &server, TDEConfig *config, int num, bool active );
00256     static void writeConfig( const LdapServer &server, TDEConfig *config, int j, bool active );
00257 
00258     void startSearch( const TQString& txt );
00259     void cancelSearch();
00260     bool isAvailable() const;
00261     void updateCompletionWeights();
00262 
00263     TQValueList< LdapClient* > clients() const { return mClients; }
00264 
00265   signals:
00268     void searchData( const TQStringList& );
00271     void searchData( const KPIM::LdapResultList& );
00272     void searchDone();
00273 
00274   private slots:
00275     void slotLDAPResult( const KPIM::LdapObject& );
00276     void slotLDAPError( const TQString& );
00277     void slotLDAPDone();
00278     void slotDataTimer();
00279     void slotFileChanged( const TQString& );
00280 
00281   private:
00282     void readWeighForClient( LdapClient *client, TDEConfig *config, int clientNumber );
00283     void readConfig();
00284     void finish();
00285     void makeSearchData( TQStringList& ret, LdapResultList& resList );
00286     TQValueList< LdapClient* > mClients;
00287     TQString mSearchText;
00288     TQTimer mDataTimer;
00289     int mActiveClients;
00290     bool mNoLDAPLookup;
00291     TQValueList< LdapObject > mResults;
00292     TQString mConfigFile;
00293 
00294   private:
00295     static TDEConfig *s_config;
00296     class LdapSearchPrivate* d;
00297 };
00298 
00299 }
00300 #endif // KPIM_LDAPCLIENT_H