00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qlistview.h>
00022 #include <qheader.h>
00023 #include <qlineedit.h>
00024 #include <qcheckbox.h>
00025 #include <qcombobox.h>
00026 #include <klocale.h>
00027 #include <kapplication.h>
00028 #include <kconfig.h>
00029 #include <kpushbutton.h>
00030 #include <kmessagebox.h>
00031 #include <qvaluelist.h>
00032 #include <qapplication.h>
00033
00034 #include "ldapsearchdialogimpl.h"
00035
00036 static QString join( const KABC::LdapAttrValue& lst, const QString& sep )
00037 {
00038 QString res;
00039 bool alredy = false;
00040 for ( KABC::LdapAttrValue::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
00041 if ( alredy )
00042 res += sep;
00043 alredy = TRUE;
00044 res += QString::fromUtf8( *it );
00045 }
00046 return res;
00047 }
00048
00049 static QMap<QString, QString>& adrbookattr2ldap()
00050 {
00051 static QMap<QString, QString> keys;
00052
00053 if ( keys.isEmpty() ) {
00054 keys[ i18n( "Title" ) ] = "title";
00055 keys[ i18n( "Full Name" ) ] = "cn";
00056 keys[ i18n( "Email" ) ] = "mail";
00057 keys[ i18n( "Home Number" ) ] = "homePhone";
00058 keys[ i18n( "Work Number" ) ] = "telephoneNumber";
00059 keys[ i18n( "Mobile Number" ) ] = "mobile";
00060 keys[ i18n( "Fax Number" ) ] = "facsimileTelephoneNumber";
00061 keys[ i18n( "Pager" ) ] = "pager";
00062 keys[ i18n( "Street") ] = "street";
00063 keys[ i18n( "State" ) ] = "st";
00064 keys[ i18n( "Country" ) ] = "co";
00065 keys[ i18n( "Locality" ) ] = "l";
00066 keys[ i18n( "Organization" ) ] = "o";
00067 keys[ i18n( "Company" ) ] = "Company";
00068 keys[ i18n( "Department" ) ] = "department";
00069 keys[ i18n( "Postal Code" ) ] = "postalCode";
00070 keys[ i18n( "Postal Address" ) ] = "postalAddress";
00071 keys[ i18n( "Description" ) ] = "description";
00072 keys[ i18n( "User ID" ) ] = "uid";
00073 }
00074 return keys;
00075 }
00076
00077 class ContactListItem : public QListViewItem
00078 {
00079 public:
00080 ContactListItem( QListView* parent, const KABC::LdapAttrMap& attrs )
00081 : QListViewItem( parent ), mAttrs( attrs )
00082 { }
00083
00084 KABC::LdapAttrMap mAttrs;
00085
00086 virtual QString text( int col ) const
00087 {
00088
00089 QString colName = listView()->columnText( col );
00090 return join( mAttrs[ adrbookattr2ldap()[ colName ] ], ", " );
00091 }
00092 };
00093
00094 LDAPSearchDialogImpl::LDAPSearchDialogImpl( KABC::AddressBook *ab, QWidget* parent, const char* name, bool modal, WFlags fl )
00095 : LDAPSearchDialog( parent, name, modal, fl ), mAddressBook( ab )
00096 {
00097 mNumHosts = 0;
00098 mIsOK = false;
00099
00100 filterCombo->insertItem( i18n( "Name" ) );
00101 filterCombo->insertItem( i18n( "Email" ) );
00102 filterCombo->insertItem( i18n( "Home Number" ) );
00103 filterCombo->insertItem( i18n( "Work Number" ) );
00104
00105 resultListView->setSelectionMode( QListView::Multi );
00106 resultListView->setAllColumnsShowFocus( true );
00107 resultListView->setShowSortIndicator( true );
00108
00109 connect( recursiveCheckbox, SIGNAL( toggled( bool ) ),
00110 this, SLOT( slotSetScope( bool ) ) );
00111 connect( addSelectedButton, SIGNAL( clicked() ),
00112 this, SLOT( slotAddSelectedContacts() ) );
00113 connect( selectAllButton, SIGNAL( clicked() ),
00114 this, SLOT( slotSelectAll() ) );
00115 connect( unselectAllButton, SIGNAL( clicked() ),
00116 this, SLOT( slotUnSelectAll() ) );
00117 connect( mailToButton, SIGNAL( clicked() ),
00118 this, SLOT( slotSendMail() ) );
00119 connect( searchButton, SIGNAL( clicked() ),
00120 this, SLOT( slotStartSearch() ) );
00121
00122 rereadConfig();
00123 }
00124
00125 void LDAPSearchDialogImpl::rereadConfig()
00126 {
00127
00128
00129
00130
00131 mLdapClientList.setAutoDelete( true );
00132 mLdapClientList.clear();
00133
00134
00135
00136 KConfig* config = kapp->config();
00137 config->setGroup( "LDAP" );
00138 mNumHosts = config->readUnsignedNumEntry( "NumSelectedHosts" );
00139 if ( !mNumHosts ) {
00140 KMessageBox::error( this, i18n( "You must select a LDAP server before searching.\nYou can do this from the menu Settings/Configure KAddressBook." ) );
00141 mIsOK = false;
00142 } else {
00143 mIsOK = true;
00144 for ( int j = 0; j < mNumHosts; ++j ) {
00145 KABC::LdapClient* ldapClient = new KABC::LdapClient( this, "ldapclient" );
00146
00147 QString host = config->readEntry( QString( "SelectedHost%1" ).arg( j ), "" );
00148 if ( !host.isEmpty() )
00149 ldapClient->setHost( host );
00150
00151 QString port = QString::number( config->readUnsignedNumEntry( QString( "SelectedPort%1" ).arg( j ) ) );
00152 if ( !port.isEmpty() )
00153 ldapClient->setPort( port );
00154
00155 QString base = config->readEntry( QString( "SelectedBase%1" ).arg( j ), "" );
00156 if ( !base.isEmpty() )
00157 ldapClient->setBase( base );
00158
00159 QStringList attrs;
00160
00161 for ( QMap<QString,QString>::Iterator it = adrbookattr2ldap().begin(); it != adrbookattr2ldap().end(); ++it )
00162 attrs << *it;
00163
00164 ldapClient->setAttrs( attrs );
00165
00166 connect( ldapClient, SIGNAL( result( const KABC::LdapObject& ) ),
00167 this, SLOT( slotAddResult( const KABC::LdapObject& ) ) );
00168 connect( ldapClient, SIGNAL( done() ),
00169 this, SLOT( slotSearchDone() ) );
00170 connect( ldapClient, SIGNAL( error( const QString& ) ),
00171 this, SLOT( slotError( const QString& ) ) );
00172
00173 mLdapClientList.append( ldapClient );
00174 }
00175
00177 while ( resultListView->header()->count() > 0 ) {
00178 resultListView->removeColumn(0);
00179 }
00180
00181 resultListView->addColumn( i18n( "Full Name" ) );
00182 resultListView->addColumn( i18n( "Email" ) );
00183 resultListView->addColumn( i18n( "Home Number" ) );
00184 resultListView->addColumn( i18n( "Work Number" ) );
00185 resultListView->addColumn( i18n( "Mobile Number" ) );
00186 resultListView->addColumn( i18n( "Fax Number" ) );
00187 resultListView->addColumn( i18n( "Company" ) );
00188 resultListView->addColumn( i18n( "Organization" ) );
00189 resultListView->addColumn( i18n( "Street" ) );
00190 resultListView->addColumn( i18n( "State" ) );
00191 resultListView->addColumn( i18n( "Country" ) );
00192 resultListView->addColumn( i18n( "Postal Code" ) );
00193 resultListView->addColumn( i18n( "Postal Address" ) );
00194 resultListView->addColumn( i18n( "Locality" ) );
00195 resultListView->addColumn( i18n( "Department" ) );
00196 resultListView->addColumn( i18n( "Description" ) );
00197 resultListView->addColumn( i18n( "User ID" ) );
00198 resultListView->addColumn( i18n( "Title" ) );
00199
00200 resultListView->clear();
00201 }
00202 }
00203
00204 LDAPSearchDialogImpl::~LDAPSearchDialogImpl()
00205 {
00206 }
00207
00208 void LDAPSearchDialogImpl::cancelQuery()
00209 {
00210 for ( KABC::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
00211 client->cancelQuery();
00212 }
00213 }
00214
00215 void LDAPSearchDialogImpl::slotAddResult( const KABC::LdapObject& obj )
00216 {
00217 new ContactListItem( resultListView, obj.attrs );
00218 }
00219
00220 void LDAPSearchDialogImpl::slotSetScope( bool rec )
00221 {
00222 for ( KABC::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
00223 if ( rec )
00224 client->setScope( "sub" );
00225 else
00226 client->setScope( "one" );
00227 }
00228 }
00229
00230 QString LDAPSearchDialogImpl::makeFilter( const QString& query, const QString& attr )
00231 {
00232 QString result;
00233
00234 if ( query.isEmpty() )
00235 result = "%1=*%2";
00236 else
00237 result = "%1=*%2*";
00238
00239 if ( attr == i18n( "Name" ) ) {
00240 result = result.arg( "cn" ).arg( query );
00241 } else if ( attr == i18n( "Email" ) ) {
00242 result = result.arg( "mail" ).arg( query );
00243 } else if ( attr == i18n( "Home Number" ) ) {
00244 result = result.arg( "homePhone" ).arg( query );
00245 } else if ( attr == i18n( "Work Number" ) ) {
00246 result = result.arg( "telephoneNumber" ).arg( query );
00247 } else {
00248
00249 result = QString::null;
00250 }
00251 return result;
00252 }
00253
00254 void LDAPSearchDialogImpl::slotStartSearch()
00255 {
00256 cancelQuery();
00257
00258 QApplication::setOverrideCursor( Qt::waitCursor );
00259 searchButton->setText( i18n( "Stop" ) );
00260
00261 disconnect( searchButton, SIGNAL( clicked() ),
00262 this, SLOT( slotStartSearch() ) );
00263 connect( searchButton, SIGNAL( clicked() ),
00264 this, SLOT( slotStopSearch() ) );
00265
00266 QString filter = makeFilter( searchEdit->text().stripWhiteSpace(), filterCombo->currentText() );
00267
00268
00269 resultListView->clear();
00270 for( KABC::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
00271 client->startQuery( filter );
00272 }
00273 }
00274
00275 void LDAPSearchDialogImpl::slotStopSearch()
00276 {
00277 cancelQuery();
00278 slotSearchDone();
00279 }
00280
00281 void LDAPSearchDialogImpl::slotSearchDone()
00282 {
00283
00284 for ( KABC::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
00285 if ( client->isActive() )
00286 return;
00287 }
00288
00289 disconnect( searchButton, SIGNAL( clicked() ),
00290 this, SLOT( slotStopSearch() ) );
00291 connect( searchButton, SIGNAL( clicked() ),
00292 this, SLOT( slotStartSearch() ) );
00293
00294 searchButton->setText( i18n( "Search" ) );
00295 QApplication::restoreOverrideCursor();
00296 }
00297
00298 void LDAPSearchDialogImpl::slotError( const QString& error )
00299 {
00300 QApplication::restoreOverrideCursor();
00301 KMessageBox::error( this, error );
00302 }
00303
00304 void LDAPSearchDialogImpl::closeEvent( QCloseEvent* e )
00305 {
00306 slotStopSearch();
00307 e->accept();
00308 }
00309
00310 void LDAPSearchDialogImpl::slotAddSelectedContacts()
00311 {
00312 ContactListItem* cli = static_cast<ContactListItem*>( resultListView->firstChild() );
00313 while ( cli ) {
00314 if ( cli->isSelected() ) {
00315 KABC::Addressee addr;
00316
00317
00318 addr.setNameFromString( QString::fromUtf8( cli->mAttrs["cn"].first() ) );
00319
00320
00321 KABC::LdapAttrValue lst = cli->mAttrs["mail"];
00322 KABC::LdapAttrValue::ConstIterator it = lst.begin();
00323 bool pref = true;
00324 if ( it != lst.end() ) {
00325 addr.insertEmail( QString::fromUtf8( *it ), pref );
00326 pref = false;
00327 ++it;
00328 }
00329
00330 addr.setOrganization(QString::fromUtf8( cli->mAttrs[ "o" ].first() ) );
00331 if (addr.organization().isEmpty())
00332 addr.setOrganization(QString::fromUtf8( cli->mAttrs[ "Company" ].first() ) );
00333
00334 addr.insertCustom("KADDRESSBOOK", "X-Department", QString::fromUtf8( cli->mAttrs[ "department" ].first() ) );
00335
00336
00337 KABC::Address workAddr(KABC::Address::Work);
00338
00339 workAddr.setStreet(QString::fromUtf8( cli->mAttrs[ "street" ].first()) );
00340 workAddr.setLocality(QString::fromUtf8( cli->mAttrs[ "l" ].first()) );
00341 workAddr.setRegion(QString::fromUtf8( cli->mAttrs[ "st" ].first()));
00342 workAddr.setPostalCode(QString::fromUtf8( cli->mAttrs[ "postalCode" ].first()) );
00343 workAddr.setCountry(QString::fromUtf8( cli->mAttrs[ "co" ].first()) );
00344
00345 addr.insertAddress( workAddr );
00346
00347
00348 KABC::PhoneNumber homeNr = QString::fromUtf8( cli->mAttrs[ "homePhone" ].first() );
00349 homeNr.setType(KABC::PhoneNumber::Home);
00350 addr.insertPhoneNumber(homeNr);
00351
00352 KABC::PhoneNumber workNr = QString::fromUtf8( cli->mAttrs[ "telephoneNumber" ].first() );
00353 workNr.setType(KABC::PhoneNumber::Work);
00354 addr.insertPhoneNumber(workNr);
00355
00356 KABC::PhoneNumber faxNr = QString::fromUtf8( cli->mAttrs[ "facsimileTelephoneNumber" ].first() );
00357 faxNr.setType(KABC::PhoneNumber::Fax);
00358 addr.insertPhoneNumber(faxNr);
00359
00360 KABC::PhoneNumber cellNr = QString::fromUtf8( cli->mAttrs[ "mobile" ].first() );
00361 cellNr.setType(KABC::PhoneNumber::Cell);
00362 addr.insertPhoneNumber(cellNr);
00363
00364 KABC::PhoneNumber pagerNr = QString::fromUtf8( cli->mAttrs[ "pager" ].first() );
00365 pagerNr.setType(KABC::PhoneNumber::Pager);
00366 addr.insertPhoneNumber(pagerNr);
00367
00368 if ( mAddressBook )
00369 mAddressBook->insertAddressee( addr );
00370 }
00371
00372 cli = static_cast<ContactListItem*>( cli->nextSibling() );
00373 }
00374
00375 emit addresseesAdded();
00376 }
00377
00382 QString LDAPSearchDialogImpl::selectedEMails() const
00383 {
00384 QStringList result;
00385 ContactListItem* cli = static_cast<ContactListItem*>( resultListView->firstChild() );
00386 while ( cli ) {
00387 if ( cli->isSelected() ) {
00388 QString email = QString::fromUtf8( cli->mAttrs[ "mail" ].first() ).stripWhiteSpace();
00389 if ( !email.isEmpty() ) {
00390 QString name = QString::fromUtf8( cli->mAttrs[ "cn" ].first() ).stripWhiteSpace();
00391 if ( name.isEmpty() ) {
00392 result << email;
00393 } else {
00394 result << name + " <" + email + ">";
00395 }
00396 }
00397 }
00398 cli = static_cast<ContactListItem*>( cli->nextSibling() );
00399 }
00400
00401 return result.join( ", " );
00402 }
00403
00404 void LDAPSearchDialogImpl::slotSendMail()
00405 {
00406 kapp->invokeMailer( selectedEMails(), "" );
00407 }
00408
00409 void LDAPSearchDialogImpl::slotSelectAll()
00410 {
00411 resultListView->selectAll( true );
00412 }
00413
00414 void LDAPSearchDialogImpl::slotUnSelectAll()
00415 {
00416 resultListView->selectAll( false );
00417 }
00418
00419 #include "ldapsearchdialogimpl.moc"