00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "distributionlistwidget.h"
00024
00025 #include <tqbuttongroup.h>
00026 #include <tqcombobox.h>
00027 #include <tqlabel.h>
00028 #include <tqlayout.h>
00029 #include <tqlistview.h>
00030 #include <tqpushbutton.h>
00031 #include <tqradiobutton.h>
00032
00033 #include <tdeaccelmanager.h>
00034 #include <tdeconfig.h>
00035 #include <kdebug.h>
00036 #include <tdeglobal.h>
00037 #include <kinputdialog.h>
00038 #include <tdelocale.h>
00039 #include <tdemessagebox.h>
00040
00041 #include <tdeabc/addresseedialog.h>
00042 #ifdef TDEPIM_NEW_DISTRLISTS
00043 #include <libtdepim/distributionlist.h>
00044 typedef KPIM::DistributionList DistributionList;
00045 #else
00046 #include <tdeabc/distributionlist.h>
00047 typedef TDEABC::DistributionList DistributionList;
00048 #endif
00049 #include <tdeabc/stdaddressbook.h>
00050 #include <tdeabc/vcardconverter.h>
00051 #include <libtdepim/kvcarddrag.h>
00052
00053 #include "core.h"
00054
00055 class DistributionListFactory : public KAB::ExtensionFactory
00056 {
00057 public:
00058 KAB::ExtensionWidget *extension( KAB::Core *core, TQWidget *parent, const char *name )
00059 {
00060 return new DistributionListWidget( core, parent, name );
00061 }
00062
00063 TQString identifier() const
00064 {
00065 return "distribution_list_editor";
00066 }
00067 };
00068
00069 extern "C" {
00070 void *init_libkaddrbk_distributionlist()
00071 {
00072 return ( new DistributionListFactory );
00073 }
00074 }
00075
00081 class DeletePressedCatcher : public TQObject
00082 {
00083 public:
00084 DeletePressedCatcher( DistributionListWidget *parent )
00085 : TQObject( parent, "DeletePressedCatcher" ), mWidget( parent )
00086 {
00087 }
00088
00089 protected:
00090 bool eventFilter( TQObject*, TQEvent *event )
00091 {
00092 if ( event->type() == TQEvent::AccelOverride ) {
00093 TQKeyEvent *keyEvent = (TQKeyEvent*)event;
00094 if ( keyEvent->key() == TQt::Key_Delete ) {
00095 keyEvent->accept();
00096 mWidget->removeContact();
00097 return true;
00098 } else
00099 return false;
00100 } else {
00101 return false;
00102 }
00103 }
00104
00105 private:
00106 DistributionListWidget *mWidget;
00107 };
00108
00109 class ContactItem : public TQListViewItem
00110 {
00111 public:
00112 ContactItem( DistributionListView *parent, const TDEABC::Addressee &addressee,
00113 const TQString &email = TQString() ) :
00114 TQListViewItem( parent ),
00115 mAddressee( addressee ),
00116 mEmail( email )
00117 {
00118 setText( 0, addressee.realName() );
00119 if ( email.isEmpty() ) {
00120 setText( 1, addressee.preferredEmail() );
00121 setText( 2, i18n( "Yes" ) );
00122 } else {
00123 setText( 1, email );
00124 setText( 2, i18n( "No" ) );
00125 }
00126 }
00127
00128 TDEABC::Addressee addressee() const
00129 {
00130 return mAddressee;
00131 }
00132
00133 TQString email() const
00134 {
00135 return mEmail;
00136 }
00137
00138 protected:
00139 bool acceptDrop( const TQMimeSource* )
00140 {
00141 return true;
00142 }
00143
00144 private:
00145 TDEABC::Addressee mAddressee;
00146 TQString mEmail;
00147 };
00148
00149 DistributionListWidget::DistributionListWidget( KAB::Core *core, TQWidget *parent,
00150 const char *name )
00151 : KAB::ExtensionWidget( core, parent, name )
00152 #ifndef TDEPIM_NEW_DISTRLISTS
00153 , mManager( 0 )
00154 #endif
00155 {
00156 TQGridLayout *topLayout = new TQGridLayout( this, 3, 4, KDialog::marginHint(),
00157 KDialog::spacingHint() );
00158
00159 mNameCombo = new TQComboBox( this );
00160 topLayout->addWidget( mNameCombo, 0, 0 );
00161 connect( mNameCombo, TQT_SIGNAL( activated( int ) ), TQT_SLOT( updateContactView() ) );
00162
00163 mCreateListButton = new TQPushButton( i18n( "New List..." ), this );
00164 topLayout->addWidget( mCreateListButton, 0, 1 );
00165 connect( mCreateListButton, TQT_SIGNAL( clicked() ), TQT_SLOT( createList() ) );
00166
00167 mEditListButton = new TQPushButton( i18n( "Rename List..." ), this );
00168 topLayout->addWidget( mEditListButton, 0, 2 );
00169 connect( mEditListButton, TQT_SIGNAL( clicked() ), TQT_SLOT( editList() ) );
00170
00171 mRemoveListButton = new TQPushButton( i18n( "Remove List" ), this );
00172 topLayout->addWidget( mRemoveListButton, 0, 3 );
00173 connect( mRemoveListButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeList() ) );
00174
00175 mContactView = new DistributionListView( this );
00176 mContactView->addColumn( i18n( "Name" ) );
00177 mContactView->addColumn( i18n( "Email" ) );
00178 mContactView->addColumn( i18n( "Use Preferred" ) );
00179 mContactView->setEnabled( false );
00180 mContactView->setAllColumnsShowFocus( true );
00181 mContactView->setFullWidth( true );
00182 topLayout->addMultiCellWidget( mContactView, 1, 1, 0, 3 );
00183 connect( mContactView, TQT_SIGNAL( selectionChanged() ),
00184 TQT_SLOT( selectionContactViewChanged() ) );
00185 connect( mContactView, TQT_SIGNAL( dropped( TQDropEvent*, TQListViewItem* ) ),
00186 TQT_SLOT( dropped( TQDropEvent*, TQListViewItem* ) ) );
00187
00188 mAddContactButton = new TQPushButton( i18n( "Add Contact" ), this );
00189 mAddContactButton->setEnabled( false );
00190 topLayout->addWidget( mAddContactButton, 2, 0 );
00191 connect( mAddContactButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addContact() ) );
00192
00193 mEntryCountLabel = new TQLabel( this );
00194 topLayout->addWidget( mEntryCountLabel, 2, 1 );
00195
00196 mChangeEmailButton = new TQPushButton( i18n( "Change Email..." ), this );
00197 topLayout->addWidget( mChangeEmailButton, 2, 2 );
00198 connect( mChangeEmailButton, TQT_SIGNAL( clicked() ), TQT_SLOT( changeEmail() ) );
00199
00200 mRemoveContactButton = new TQPushButton( i18n( "Remove Contact" ), this );
00201 topLayout->addWidget( mRemoveContactButton, 2, 3 );
00202 connect( mRemoveContactButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeContact() ) );
00203
00204 #ifdef TDEPIM_NEW_DISTRLISTS
00205
00206 connect( core, TQT_SIGNAL( contactsUpdated() ),
00207 this, TQT_SLOT( updateNameCombo() ) );
00208 #else
00209 mManager = new TDEABC::DistributionListManager( core->addressBook() );
00210
00211 connect( TDEABC::DistributionListWatcher::self(), TQT_SIGNAL( changed() ),
00212 this, TQT_SLOT( updateNameCombo() ) );
00213 #endif
00214
00215 connect( core->addressBook(), TQT_SIGNAL( addressBookChanged( AddressBook* ) ),
00216 this, TQT_SLOT( updateNameCombo() ) );
00217
00218 updateNameCombo();
00219
00220 TQObject *catcher = new DeletePressedCatcher( this );
00221 installEventFilter( catcher );
00222 mContactView->installEventFilter( catcher );
00223
00224 mContactView->restoreLayout( TDEGlobal::config(), "DistributionListViewColumns" );
00225
00226 TDEAcceleratorManager::manage( this );
00227 }
00228
00229 DistributionListWidget::~DistributionListWidget()
00230 {
00231 #ifndef TDEPIM_NEW_DISTRLISTS
00232 delete mManager;
00233 #endif
00234
00235 mContactView->saveLayout( TDEGlobal::config(), "DistributionListViewColumns" );
00236 }
00237
00238 void DistributionListWidget::save()
00239 {
00240 #ifndef TDEPIM_NEW_DISTRLISTS
00241 mManager->save();
00242 #endif
00243 }
00244
00245 void DistributionListWidget::selectionContactViewChanged()
00246 {
00247 ContactItem *contactItem =
00248 static_cast<ContactItem *>( mContactView->selectedItem() );
00249 bool state = contactItem;
00250
00251 mChangeEmailButton->setEnabled( state );
00252 mRemoveContactButton->setEnabled( state );
00253 }
00254
00255 bool DistributionListWidget::alreadyExists( const TQString& distrListName ) const
00256 {
00257 #ifdef TDEPIM_NEW_DISTRLISTS
00258 return core()->distributionListNames().contains( distrListName );
00259 #else
00260 return mManager->listNames().contains( distrListName );
00261 #endif
00262 }
00263
00264 void DistributionListWidget::createList()
00265 {
00266 TQString newName = KInputDialog::getText( i18n( "New Distribution List" ),
00267 i18n( "Please enter name:" ),
00268 TQString(), 0, this );
00269
00270 if ( newName.isEmpty() ) return;
00271
00272 if ( alreadyExists( newName ) ) {
00273 KMessageBox::sorry( this, i18n( "The name already exists" ) );
00274 return;
00275 }
00276 #ifdef TDEPIM_NEW_DISTRLISTS
00277 TDEABC::Resource* resource = core()->requestResource( this );
00278 if ( !resource )
00279 return;
00280
00281 KPIM::DistributionList dist;
00282 dist.setResource( resource );
00283 dist.setName( newName );
00284
00285
00286 changed( dist );
00287 core()->addressBook()->insertAddressee( dist );
00288
00289 #else
00290 new TDEABC::DistributionList( mManager, newName );
00291 changed();
00292
00293 updateNameCombo();
00294 #endif
00295
00296
00297 mNameCombo->setCurrentText( newName );
00298
00299 updateContactView();
00300 }
00301
00302 void DistributionListWidget::editList()
00303 {
00304 const TQString oldName = mNameCombo->currentText();
00305
00306 const TQString newName = KInputDialog::getText( i18n( "Rename Distribution List" ),
00307 i18n( "Please enter name:" ),
00308 oldName, 0, this );
00309
00310 if ( newName.isEmpty() ) return;
00311
00312 if ( alreadyExists( newName ) ) {
00313 KMessageBox::sorry( this, i18n( "The name already exists." ) );
00314 return;
00315 }
00316 #ifdef TDEPIM_NEW_DISTRLISTS
00317 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00318 core()->addressBook(), mNameCombo->currentText() );
00319 if ( dist.isEmpty() )
00320 return;
00321
00322 dist.setFormattedName( newName );
00323 core()->addressBook()->insertAddressee( dist );
00324
00325 changed( dist );
00326 #else
00327 TDEABC::DistributionList *list = mManager->list( oldName );
00328 list->setName( newName );
00329 mManager->save();
00330 updateNameCombo();
00331 #endif
00332
00333
00334 mNameCombo->setCurrentText( newName );
00335
00336 updateContactView();
00337
00338 #ifndef TDEPIM_NEW_DISTRLISTS
00339 changed();
00340 #endif
00341 }
00342
00343 void DistributionListWidget::removeList()
00344 {
00345 int result = KMessageBox::warningContinueCancel( this,
00346 i18n( "<qt>Delete distribution list <b>%1</b>?</qt>" ) .arg( mNameCombo->currentText() ),
00347 TQString(), KGuiItem( i18n("Delete"), "edit-delete") );
00348
00349 if ( result != KMessageBox::Continue )
00350 return;
00351
00352 #ifdef TDEPIM_NEW_DISTRLISTS
00353 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00354 core()->addressBook(), mNameCombo->currentText() );
00355 if ( dist.isEmpty() )
00356 return;
00357
00358 emit deleted( dist.uid() );
00359 core()->addressBook()->removeAddressee( dist );
00360 #else
00361 mManager->remove( mManager->list( mNameCombo->currentText() ) );
00362 mNameCombo->removeItem( mNameCombo->currentItem() );
00363
00364 updateContactView();
00365
00366 changed();
00367 #endif
00368 }
00369
00370 void DistributionListWidget::addContact()
00371 {
00372 #ifdef TDEPIM_NEW_DISTRLISTS
00373 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00374 core()->addressBook(), mNameCombo->currentText() );
00375 if ( dist.isEmpty() ) {
00376 kdDebug(5720) << k_funcinfo << mNameCombo->currentText() << " not found" << endl;
00377 return;
00378 }
00379 #else
00380 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00381 if ( !list )
00382 return;
00383 TDEABC::DistributionList& dist = *list;
00384 #endif
00385
00386 const TDEABC::Addressee::List addrList = selectedContacts();
00387 TDEABC::Addressee::List::ConstIterator it;
00388 for ( it = addrList.begin(); it != addrList.end(); ++it )
00389 dist.insertEntry( *it );
00390
00391 #ifdef TDEPIM_NEW_DISTRLISTS
00392 core()->addressBook()->insertAddressee( dist );
00393 changed( dist );
00394 #else
00395 updateContactView();
00396 changed();
00397 #endif
00398 }
00399
00400 void DistributionListWidget::removeContact()
00401 {
00402 #ifdef TDEPIM_NEW_DISTRLISTS
00403 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00404 core()->addressBook(), mNameCombo->currentText() );
00405 if ( dist.isEmpty() )
00406 return;
00407 #else
00408 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00409 if ( !list )
00410 return;
00411 TDEABC::DistributionList& dist = *list;
00412 #endif
00413
00414 ContactItem *contactItem =
00415 static_cast<ContactItem *>( mContactView->selectedItem() );
00416 if ( !contactItem )
00417 return;
00418
00419 dist.removeEntry( contactItem->addressee(), contactItem->email() );
00420 delete contactItem;
00421
00422 #ifdef TDEPIM_NEW_DISTRLISTS
00423 core()->addressBook()->insertAddressee( dist );
00424 changed( dist );
00425 #else
00426 changed();
00427 #endif
00428 }
00429
00430 void DistributionListWidget::changeEmail()
00431 {
00432 #ifdef TDEPIM_NEW_DISTRLISTS
00433 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00434 core()->addressBook(), mNameCombo->currentText() );
00435 if ( dist.isEmpty() )
00436 return;
00437 #else
00438 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00439 if ( !list )
00440 return;
00441 TDEABC::DistributionList& dist = *list;
00442 #endif
00443
00444 ContactItem *contactItem =
00445 static_cast<ContactItem *>( mContactView->selectedItem() );
00446 if ( !contactItem )
00447 return;
00448
00449 bool canceled = false;
00450 const TQString email = EmailSelector::getEmail( contactItem->addressee().emails(),
00451 contactItem->email(), this, canceled);
00452 if( canceled)
00453 return;
00454 dist.removeEntry( contactItem->addressee(), contactItem->email() );
00455 dist.insertEntry( contactItem->addressee(), email );
00456
00457 #ifdef TDEPIM_NEW_DISTRLISTS
00458 core()->addressBook()->insertAddressee( dist );
00459 changed( dist );
00460 #else
00461 updateContactView();
00462 changed();
00463 #endif
00464 }
00465
00466 void DistributionListWidget::updateContactView()
00467 {
00468 mContactView->clear();
00469
00470 bool isListSelected = false;
00471 #ifdef TDEPIM_NEW_DISTRLISTS
00472 KPIM::DistributionList dist;
00473 if ( mNameCombo->count() != 0 )
00474 dist = KPIM::DistributionList::findByName(
00475 core()->addressBook(), mNameCombo->currentText() );
00476 isListSelected = !dist.isEmpty();
00477 #else
00478 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00479 isListSelected = list != 0;
00480 #endif
00481 if ( !isListSelected ) {
00482 mEditListButton->setEnabled( false );
00483 mRemoveListButton->setEnabled( false );
00484 mChangeEmailButton->setEnabled( false );
00485 mRemoveContactButton->setEnabled( false );
00486 mContactView->setEnabled( false );
00487 return;
00488 }
00489 mEditListButton->setEnabled( true );
00490 mRemoveListButton->setEnabled( true );
00491 mContactView->setEnabled( true );
00492
00493 uint entryCount = 0;
00494 #ifdef TDEPIM_NEW_DISTRLISTS
00495 const KPIM::DistributionList::Entry::List entries = dist.entries( core()->addressBook() );
00496 KPIM::DistributionList::Entry::List::ConstIterator it;
00497 #else
00498 const TDEABC::DistributionList::Entry::List entries = list->entries();
00499 TDEABC::DistributionList::Entry::List::ConstIterator it;
00500 #endif
00501 for ( it = entries.begin(); it != entries.end(); ++it, ++entryCount )
00502 new ContactItem( mContactView, (*it).addressee, (*it).email );
00503
00504 bool state = mContactView->selectedItem() != 0;
00505 mChangeEmailButton->setEnabled( state );
00506 mRemoveContactButton->setEnabled( state );
00507
00508 mEntryCountLabel->setText( i18n( "Count: %n contact", "Count: %n contacts", entryCount ) );
00509 }
00510
00511 void DistributionListWidget::updateNameCombo()
00512 {
00513 int pos = mNameCombo->currentItem();
00514 mNameCombo->clear();
00515 #ifdef TDEPIM_NEW_DISTRLISTS
00516 const TQStringList names = core()->distributionListNames();
00517 #else
00518 mManager->load();
00519 const TQStringList names = mManager->listNames();
00520 #endif
00521 mNameCombo->insertStringList( names );
00522 mNameCombo->setCurrentItem( TQMIN( pos, (int)names.count() - 1 ) );
00523
00524 updateContactView();
00525 }
00526
00527 void DistributionListWidget::dropEvent( TQDropEvent *e )
00528 {
00529 if ( mNameCombo->count() == 0 )
00530 return;
00531
00532 #ifdef TDEPIM_NEW_DISTRLISTS
00533 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00534 core()->addressBook(), mNameCombo->currentText() );
00535 if ( dist.isEmpty() )
00536 return;
00537 #else
00538 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00539 if ( !list )
00540 return;
00541 TDEABC::DistributionList& dist = *list;
00542 #endif
00543
00544 TQString vcards;
00545 if ( KVCardDrag::decode( e, vcards ) ) {
00546 TDEABC::VCardConverter converter;
00547 const TDEABC::Addressee::List lst = converter.parseVCards( vcards );
00548 for ( TDEABC::Addressee::List::ConstIterator it = lst.begin(); it != lst.end(); ++it )
00549 dist.insertEntry( *it );
00550
00551 #ifdef TDEPIM_NEW_DISTRLISTS
00552 core()->addressBook()->insertAddressee( dist );
00553 changed( dist );
00554 #else
00555 changed();
00556 updateContactView();
00557 #endif
00558 }
00559 }
00560
00561 void DistributionListWidget::contactsSelectionChanged()
00562 {
00563 mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 );
00564 }
00565
00566 TQString DistributionListWidget::title() const
00567 {
00568 return i18n( "Distribution List Editor" );
00569 }
00570
00571 TQString DistributionListWidget::identifier() const
00572 {
00573 return "distribution_list_editor";
00574 }
00575
00576 void DistributionListWidget::dropped( TQDropEvent *e, TQListViewItem* )
00577 {
00578 dropEvent( e );
00579 }
00580
00581 #ifdef TDEPIM_NEW_DISTRLISTS
00582 void DistributionListWidget::changed( const TDEABC::Addressee& dist )
00583 {
00584 emit modified( TDEABC::Addressee::List() << dist );
00585 }
00586 #else
00587 void DistributionListWidget::changed()
00588 {
00589 save();
00590 }
00591 #endif
00592
00593 DistributionListView::DistributionListView( TQWidget *parent, const char* name )
00594 : TDEListView( parent, name )
00595 {
00596 setDragEnabled( true );
00597 setAcceptDrops( true );
00598 setAllColumnsShowFocus( true );
00599 }
00600
00601 void DistributionListView::dragEnterEvent( TQDragEnterEvent* e )
00602 {
00603 bool canDecode = TQTextDrag::canDecode( e );
00604 e->accept( canDecode );
00605 }
00606
00607 void DistributionListView::viewportDragMoveEvent( TQDragMoveEvent *e )
00608 {
00609 bool canDecode = TQTextDrag::canDecode( e );
00610 e->accept( canDecode );
00611 }
00612
00613 void DistributionListView::viewportDropEvent( TQDropEvent *e )
00614 {
00615 emit dropped( e, 0 );
00616 }
00617
00618 void DistributionListView::dropEvent( TQDropEvent *e )
00619 {
00620 emit dropped( e, 0 );
00621 }
00622
00623
00624 EmailSelector::EmailSelector( const TQStringList &emails,
00625 const TQString ¤t, TQWidget *parent )
00626 : KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok|Cancel, Ok,
00627 parent )
00628 {
00629 TQFrame *topFrame = plainPage();
00630 TQBoxLayout *topLayout = new TQVBoxLayout( topFrame );
00631
00632 mButtonGroup = new TQButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00633 topFrame );
00634 mButtonGroup->setRadioButtonExclusive( true );
00635 topLayout->addWidget( mButtonGroup );
00636
00637 TQRadioButton *button = new TQRadioButton( i18n("Preferred address"), mButtonGroup );
00638 button->setDown( true );
00639 mEmailMap.insert( mButtonGroup->id( button ), "" );
00640
00641 TQStringList::ConstIterator it;
00642 for ( it = emails.begin(); it != emails.end(); ++it ) {
00643 button = new TQRadioButton( *it, mButtonGroup );
00644 mEmailMap.insert( mButtonGroup->id( button ), *it );
00645 if ( (*it) == current )
00646 button->setDown( true );
00647 }
00648 }
00649
00650 TQString EmailSelector::selected() const
00651 {
00652 TQButton *button = mButtonGroup->selected();
00653 if ( button )
00654 return mEmailMap[ mButtonGroup->id( button ) ];
00655
00656 return TQString();
00657 }
00658
00659 TQString EmailSelector::getEmail( const TQStringList &emails,
00660 const TQString ¤t, TQWidget *parent, bool &canceled )
00661 {
00662 EmailSelector dlg( emails, current, parent );
00663 if(dlg.exec())
00664 {
00665 canceled = false;
00666 return dlg.selected();
00667 }
00668 canceled = true;
00669 return TQString();
00670 }
00671
00672
00673 #include "distributionlistwidget.moc"