21 #include "ldapsearchdialog.h"
22 #include "ldapclient.h"
24 #include <libemailfunctions/email.h>
26 #include <tqcheckbox.h>
27 #include <tqgroupbox.h>
31 #include <tqlistview.h>
32 #include <tqpushbutton.h>
34 #include <tdeabc/addresslineedit.h>
35 #include <tdeapplication.h>
36 #include <kcombobox.h>
37 #include <tdeconfig.h>
38 #include <klineedit.h>
39 #include <tdelocale.h>
40 #include <tdemessagebox.h>
44 static TQString asUtf8(
const TQByteArray &val )
49 const char *data = val.data();
52 if ( data[ val.size() - 1 ] ==
'\0' )
53 return TQString::fromUtf8( data, val.size() - 1 );
55 return TQString::fromUtf8( data, val.size() );
58 static TQString join(
const KPIM::LdapAttrValue& lst,
const TQString& sep )
62 for ( KPIM::LdapAttrValue::ConstIterator it = lst.begin(); it != lst.end(); ++it ) {
71 static TQMap<TQString, TQString>& adrbookattr2ldap()
73 static TQMap<TQString, TQString> keys;
75 if ( keys.isEmpty() ) {
76 keys[ i18n(
"Title" ) ] =
"title";
77 keys[ i18n(
"Full Name" ) ] =
"cn";
78 keys[ i18n(
"Email" ) ] =
"mail";
79 keys[ i18n(
"Home Number" ) ] =
"homePhone";
80 keys[ i18n(
"Work Number" ) ] =
"telephoneNumber";
81 keys[ i18n(
"Mobile Number" ) ] =
"mobile";
82 keys[ i18n(
"Fax Number" ) ] =
"facsimileTelephoneNumber";
83 keys[ i18n(
"Pager" ) ] =
"pager";
84 keys[ i18n(
"Street") ] =
"street";
85 keys[ i18n(
"State" ) ] =
"st";
86 keys[ i18n(
"Country" ) ] =
"co";
87 keys[ i18n(
"City" ) ] =
"l";
88 keys[ i18n(
"Organization" ) ] =
"o";
89 keys[ i18n(
"Company" ) ] =
"Company";
90 keys[ i18n(
"Department" ) ] =
"department";
91 keys[ i18n(
"Zip Code" ) ] =
"postalCode";
92 keys[ i18n(
"Postal Address" ) ] =
"postalAddress";
93 keys[ i18n(
"Description" ) ] =
"description";
94 keys[ i18n(
"User ID" ) ] =
"uid";
101 class ContactListItem :
public TQListViewItem
104 ContactListItem( TQListView* parent,
const KPIM::LdapAttrMap& attrs )
105 : TQListViewItem( parent ), mAttrs( attrs )
107 const KPIM::LdapAttrValue &mailAttrs = attrs[
"mail" ];
108 if ( mailAttrs.isEmpty() ) {
109 setSelectable(
false );
114 KPIM::LdapAttrMap mAttrs;
116 virtual TQString text(
int col )
const
119 const TQString colName = listView()->columnText( col );
120 const TQString ldapAttrName = adrbookattr2ldap()[ colName ];
121 return join( mAttrs[ ldapAttrName ],
", " );
127 LDAPSearchDialog::LDAPSearchDialog( TQWidget* parent,
const char* name )
128 : KDialogBase( Plain, i18n(
"Search for Addresses in Directory" ), Help | User1 |
129 User2 | User3 | Cancel, Default, parent, name, false, true )
131 setButtonCancel( KStdGuiItem::close() );
132 TQFrame *page = plainPage();
133 TQVBoxLayout *topLayout =
new TQVBoxLayout( page, marginHint(), spacingHint() );
135 TQGroupBox *groupBox =
new TQGroupBox( i18n(
"Search for Addresses in Directory" ),
137 groupBox->setFrameShape( TQGroupBox::Box );
138 groupBox->setFrameShadow( TQGroupBox::Sunken );
139 groupBox->setColumnLayout( 0, TQt::Vertical );
140 TQGridLayout *boxLayout =
new TQGridLayout( groupBox->layout(), 2,
142 boxLayout->setColStretch( 1, 1 );
144 TQLabel *label =
new TQLabel( i18n(
"Search for:" ), groupBox );
145 boxLayout->addWidget( label, 0, 0 );
147 mSearchEdit =
new KLineEdit( groupBox );
148 boxLayout->addWidget( mSearchEdit, 0, 1 );
149 label->setBuddy( mSearchEdit );
151 label =
new TQLabel( i18n(
"in" ), groupBox );
152 boxLayout->addWidget( label, 0, 2 );
154 mFilterCombo =
new KComboBox( groupBox );
155 mFilterCombo->insertItem( i18n(
"Name" ) );
156 mFilterCombo->insertItem( i18n(
"Email" ) );
157 mFilterCombo->insertItem( i18n(
"Home Number" ) );
158 mFilterCombo->insertItem( i18n(
"Work Number" ) );
159 boxLayout->addWidget( mFilterCombo, 0, 3 );
162 mSearchButton =
new TQPushButton( i18n(
"Stop" ), groupBox );
163 buttonSize = mSearchButton->sizeHint();
164 mSearchButton->setText( i18n(
"Search" ) );
165 if ( buttonSize.width() < mSearchButton->sizeHint().width() )
166 buttonSize = mSearchButton->sizeHint();
167 mSearchButton->setFixedWidth( buttonSize.width() );
169 mSearchButton->setDefault(
true );
170 boxLayout->addWidget( mSearchButton, 0, 4 );
172 mRecursiveCheckbox =
new TQCheckBox( i18n(
"Recursive search" ), groupBox );
173 mRecursiveCheckbox->setChecked(
true );
174 boxLayout->addMultiCellWidget( mRecursiveCheckbox, 1, 1, 0, 4 );
176 mSearchType =
new KComboBox( groupBox );
177 mSearchType->insertItem( i18n(
"Contains" ) );
178 mSearchType->insertItem( i18n(
"Starts With" ) );
179 boxLayout->addMultiCellWidget( mSearchType, 1, 1, 3, 4 );
181 topLayout->addWidget( groupBox );
183 mResultListView =
new TQListView( page );
184 mResultListView->setSelectionMode( TQListView::Multi );
185 mResultListView->setAllColumnsShowFocus(
true );
186 mResultListView->setShowSortIndicator(
true );
187 topLayout->addWidget( mResultListView );
189 resize( TQSize( 600, 400).expandedTo( minimumSizeHint() ) );
191 setButtonText( User1, i18n(
"Unselect All" ) );
192 setButtonText( User2, i18n(
"Select All" ) );
193 setButtonText( User3, i18n(
"Add Selected" ) );
198 connect( mRecursiveCheckbox, TQ_SIGNAL( toggled(
bool ) ),
199 this, TQ_SLOT( slotSetScope(
bool ) ) );
200 connect( mSearchButton, TQ_SIGNAL( clicked() ),
201 this, TQ_SLOT( slotStartSearch() ) );
203 setTabOrder(mSearchEdit, mFilterCombo);
204 setTabOrder(mFilterCombo, mSearchButton);
205 mSearchEdit->setFocus();
210 LDAPSearchDialog::~LDAPSearchDialog()
215 void LDAPSearchDialog::restoreSettings()
221 mLdapClientList.setAutoDelete(
true );
222 mLdapClientList.clear();
224 TDEConfig kabConfig(
"kaddressbookrc" );
225 kabConfig.setGroup(
"LDAPSearch" );
226 mSearchType->setCurrentItem( kabConfig.readNumEntry(
"SearchType", 0 ) );
230 TDEConfig* config = TDEABC::AddressLineEdit::config();
231 TDEConfigGroupSaver saver( config,
"LDAP" );
232 mNumHosts = config->readUnsignedNumEntry(
"NumSelectedHosts" );
234 KMessageBox::error(
this, i18n(
"You must select a LDAP server before searching.\nYou can do this from the menu Settings/Configure KAddressBook." ) );
238 for (
int j = 0; j < mNumHosts; ++j ) {
239 KPIM::LdapServer ldapServer;
241 TQString host = config->readEntry( TQString(
"SelectedHost%1" ).arg( j ),
"" );
242 if ( !host.isEmpty() )
243 ldapServer.setHost( host );
245 int port = config->readUnsignedNumEntry( TQString(
"SelectedPort%1" ).arg( j ) );
247 ldapServer.setPort( port );
249 TQString base = config->readEntry( TQString(
"SelectedBase%1" ).arg( j ),
"" );
250 if ( !base.isEmpty() )
251 ldapServer.setBaseDN( base );
253 TQString bindDN = config->readEntry( TQString(
"SelectedBind%1" ).arg( j ),
"" );
254 if ( !bindDN.isEmpty() )
255 ldapServer.setBindDN( bindDN );
257 TQString pwdBindDN = config->readEntry( TQString(
"SelectedPwdBind%1" ).arg( j ),
"" );
258 if ( !pwdBindDN.isEmpty() )
259 ldapServer.setPwdBindDN( pwdBindDN );
262 ldapClient->setServer( ldapServer );
266 for ( TQMap<TQString,TQString>::Iterator it = adrbookattr2ldap().begin(); it != adrbookattr2ldap().end(); ++it )
273 connect( ldapClient, TQ_SIGNAL( done() ),
274 this, TQ_SLOT( slotSearchDone() ) );
275 connect( ldapClient, TQ_SIGNAL( error(
const TQString& ) ),
276 this, TQ_SLOT( slotError(
const TQString& ) ) );
278 mLdapClientList.append( ldapClient );
282 while ( mResultListView->header()->count() > 0 ) {
283 mResultListView->removeColumn(0);
286 mResultListView->addColumn( i18n(
"Full Name" ) );
287 mResultListView->addColumn( i18n(
"Email" ) );
288 mResultListView->addColumn( i18n(
"Home Number" ) );
289 mResultListView->addColumn( i18n(
"Work Number" ) );
290 mResultListView->addColumn( i18n(
"Mobile Number" ) );
291 mResultListView->addColumn( i18n(
"Fax Number" ) );
292 mResultListView->addColumn( i18n(
"Company" ) );
293 mResultListView->addColumn( i18n(
"Organization" ) );
294 mResultListView->addColumn( i18n(
"Street" ) );
295 mResultListView->addColumn( i18n(
"State" ) );
296 mResultListView->addColumn( i18n(
"Country" ) );
297 mResultListView->addColumn( i18n(
"Zip Code" ) );
298 mResultListView->addColumn( i18n(
"Postal Address" ) );
299 mResultListView->addColumn( i18n(
"City" ) );
300 mResultListView->addColumn( i18n(
"Department" ) );
301 mResultListView->addColumn( i18n(
"Description" ) );
302 mResultListView->addColumn( i18n(
"User ID" ) );
303 mResultListView->addColumn( i18n(
"Title" ) );
305 mResultListView->clear();
309 void LDAPSearchDialog::saveSettings()
311 TDEConfig config(
"kaddressbookrc" );
312 config.setGroup(
"LDAPSearch" );
313 config.writeEntry(
"SearchType", mSearchType->currentItem() );
317 void LDAPSearchDialog::cancelQuery()
319 for (
KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
320 client->cancelQuery();
326 new ContactListItem( mResultListView, obj.attrs );
329 void LDAPSearchDialog::slotSetScope(
bool rec )
331 for (
KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
333 client->setScope(
"sub" );
335 client->setScope(
"one" );
339 TQString LDAPSearchDialog::makeFilter(
const TQString& query,
const TQString& attr,
348 TQString result(
"&(|(objectclass=person)(objectclass=groupofnames)(mail=*))(" );
349 if( query.isEmpty() )
351 return result +
"|(cn=*)(sn=*)" +
")";
353 if ( attr == i18n(
"Name" ) ) {
354 result += startsWith ?
"|(cn=%1*)(sn=%2*)" :
"|(cn=*%1*)(sn=*%2*)";
355 result = result.arg( query ).arg( query );
357 result += (startsWith ?
"%1=%2*" :
"%1=*%2*");
358 if ( attr == i18n(
"Email" ) ) {
359 result = result.arg(
"mail" ).arg( query );
360 }
else if ( attr == i18n(
"Home Number" ) ) {
361 result = result.arg(
"homePhone" ).arg( query );
362 }
else if ( attr == i18n(
"Work Number" ) ) {
363 result = result.arg(
"telephoneNumber" ).arg( query );
374 void LDAPSearchDialog::slotStartSearch()
378 TQApplication::setOverrideCursor( TQt::waitCursor );
379 mSearchButton->setText( i18n(
"Stop" ) );
381 disconnect( mSearchButton, TQ_SIGNAL( clicked() ),
382 this, TQ_SLOT( slotStartSearch() ) );
383 connect( mSearchButton, TQ_SIGNAL( clicked() ),
384 this, TQ_SLOT( slotStopSearch() ) );
386 bool startsWith = (mSearchType->currentItem() == 1);
388 TQString filter = makeFilter( mSearchEdit->text().stripWhiteSpace(), mFilterCombo->currentText(), startsWith );
391 mResultListView->clear();
392 for(
KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
393 client->startQuery( filter );
399 void LDAPSearchDialog::slotStopSearch()
405 void LDAPSearchDialog::slotSearchDone()
408 for (
KPIM::LdapClient* client = mLdapClientList.first(); client; client = mLdapClientList.next() ) {
409 if ( client->isActive() )
413 disconnect( mSearchButton, TQ_SIGNAL( clicked() ),
414 this, TQ_SLOT( slotStopSearch() ) );
415 connect( mSearchButton, TQ_SIGNAL( clicked() ),
416 this, TQ_SLOT( slotStartSearch() ) );
418 mSearchButton->setText( i18n(
"Search" ) );
419 TQApplication::restoreOverrideCursor();
422 void LDAPSearchDialog::slotError(
const TQString& error )
424 TQApplication::restoreOverrideCursor();
425 KMessageBox::error(
this, error );
428 void LDAPSearchDialog::closeEvent( TQCloseEvent* e )
438 TQString LDAPSearchDialog::selectedEMails()
const
441 ContactListItem* cli =
static_cast<ContactListItem*
>( mResultListView->firstChild() );
443 if ( cli->isSelected() ) {
444 TQString email = asUtf8( cli->mAttrs[
"mail" ].first() ).stripWhiteSpace();
445 if ( !email.isEmpty() ) {
446 TQString name = asUtf8( cli->mAttrs[
"cn" ].first() ).stripWhiteSpace();
447 if ( name.isEmpty() ) {
450 result << KPIM::quoteNameIfNecessary( name ) +
" <" + email +
">";
454 cli =
static_cast<ContactListItem*
>( cli->nextSibling() );
457 return result.join(
", " );
460 void LDAPSearchDialog::slotHelp()
462 kapp->invokeHelp(
"ldap-queries" );
465 void LDAPSearchDialog::slotUser1()
467 mResultListView->selectAll(
false );
470 void LDAPSearchDialog::slotUser2()
472 mResultListView->selectAll(
true );
475 void LDAPSearchDialog::slotUser3()
477 emit addresseesAdded();
480 #include "ldapsearchdialog.moc"
void setAttrs(const TQStringList &attrs)
TDEPIM classes for drag and drop of mails.