23 #include "distributionlistwidget.h"
25 #include <tqbuttongroup.h>
26 #include <tqcombobox.h>
29 #include <tqlistview.h>
30 #include <tqpushbutton.h>
31 #include <tqradiobutton.h>
33 #include <tdeaccelmanager.h>
34 #include <tdeconfig.h>
36 #include <tdeglobal.h>
37 #include <kinputdialog.h>
38 #include <tdelocale.h>
39 #include <tdemessagebox.h>
41 #include <tdeabc/addresseedialog.h>
42 #ifdef TDEPIM_NEW_DISTRLISTS
43 #include <libtdepim/distributionlist.h>
44 typedef KPIM::DistributionList DistributionList;
46 #include <tdeabc/distributionlist.h>
47 typedef TDEABC::DistributionList DistributionList;
49 #include <tdeabc/stdaddressbook.h>
50 #include <tdeabc/vcardconverter.h>
51 #include <libtdepim/kvcarddrag.h>
55 class DistributionListFactory :
public KAB::ExtensionFactory
58 KAB::ExtensionWidget *extension( KAB::Core *core, TQWidget *parent,
const char *name )
60 return new DistributionListWidget( core, parent, name );
63 TQString identifier()
const
65 return "distribution_list_editor";
70 void *init_libkaddrbk_distributionlist()
72 return (
new DistributionListFactory );
81 class DeletePressedCatcher :
public TQObject
84 DeletePressedCatcher( DistributionListWidget *parent )
85 : TQObject( parent,
"DeletePressedCatcher" ), mWidget( parent )
90 bool eventFilter( TQObject*, TQEvent *event )
92 if ( event->type() == TQEvent::AccelOverride ) {
93 TQKeyEvent *keyEvent = (TQKeyEvent*)event;
94 if ( keyEvent->key() == TQt::Key_Delete ) {
96 mWidget->removeContact();
106 DistributionListWidget *mWidget;
109 class ContactItem :
public TQListViewItem
113 const TQString &email = TQString() ) :
114 TQListViewItem( parent ),
115 mAddressee( addressee ),
118 setText( 0, addressee.realName() );
119 if ( email.isEmpty() ) {
120 setText( 1, addressee.preferredEmail() );
121 setText( 2, i18n(
"Yes" ) );
124 setText( 2, i18n(
"No" ) );
128 TDEABC::Addressee addressee()
const
133 TQString email()
const
139 bool acceptDrop(
const TQMimeSource* )
145 TDEABC::Addressee mAddressee;
149 DistributionListWidget::DistributionListWidget( KAB::Core *core, TQWidget *parent,
151 : KAB::ExtensionWidget( core, parent, name )
152 #ifndef TDEPIM_NEW_DISTRLISTS
156 TQGridLayout *topLayout =
new TQGridLayout(
this, 3, 4, KDialog::marginHint(),
157 KDialog::spacingHint() );
159 mNameCombo =
new TQComboBox(
this );
160 topLayout->addWidget( mNameCombo, 0, 0 );
161 connect( mNameCombo, TQ_SIGNAL( activated(
int ) ), TQ_SLOT( updateContactView() ) );
163 mCreateListButton =
new TQPushButton( i18n(
"New List..." ),
this );
164 topLayout->addWidget( mCreateListButton, 0, 1 );
165 connect( mCreateListButton, TQ_SIGNAL( clicked() ), TQ_SLOT( createList() ) );
167 mEditListButton =
new TQPushButton( i18n(
"Rename List..." ),
this );
168 topLayout->addWidget( mEditListButton, 0, 2 );
169 connect( mEditListButton, TQ_SIGNAL( clicked() ), TQ_SLOT( editList() ) );
171 mRemoveListButton =
new TQPushButton( i18n(
"Remove List" ),
this );
172 topLayout->addWidget( mRemoveListButton, 0, 3 );
173 connect( mRemoveListButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeList() ) );
176 mContactView->addColumn( i18n(
"Name" ) );
177 mContactView->addColumn( i18n(
"Email" ) );
178 mContactView->addColumn( i18n(
"Use Preferred" ) );
179 mContactView->setEnabled(
false );
180 mContactView->setAllColumnsShowFocus(
true );
181 mContactView->setFullWidth(
true );
182 topLayout->addMultiCellWidget( mContactView, 1, 1, 0, 3 );
183 connect( mContactView, TQ_SIGNAL( selectionChanged() ),
184 TQ_SLOT( selectionContactViewChanged() ) );
185 connect( mContactView, TQ_SIGNAL( dropped( TQDropEvent*, TQListViewItem* ) ),
186 TQ_SLOT( dropped( TQDropEvent*, TQListViewItem* ) ) );
188 mAddContactButton =
new TQPushButton( i18n(
"Add Contact" ),
this );
189 mAddContactButton->setEnabled(
false );
190 topLayout->addWidget( mAddContactButton, 2, 0 );
191 connect( mAddContactButton, TQ_SIGNAL( clicked() ), TQ_SLOT( addContact() ) );
193 mEntryCountLabel =
new TQLabel(
this );
194 topLayout->addWidget( mEntryCountLabel, 2, 1 );
196 mChangeEmailButton =
new TQPushButton( i18n(
"Change Email..." ),
this );
197 topLayout->addWidget( mChangeEmailButton, 2, 2 );
198 connect( mChangeEmailButton, TQ_SIGNAL( clicked() ), TQ_SLOT( changeEmail() ) );
200 mRemoveContactButton =
new TQPushButton( i18n(
"Remove Contact" ),
this );
201 topLayout->addWidget( mRemoveContactButton, 2, 3 );
202 connect( mRemoveContactButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeContact() ) );
204 #ifdef TDEPIM_NEW_DISTRLISTS
206 connect( core, TQ_SIGNAL( contactsUpdated() ),
207 this, TQ_SLOT( updateNameCombo() ) );
209 mManager =
new TDEABC::DistributionListManager( core->addressBook() );
211 connect( TDEABC::DistributionListWatcher::self(), TQ_SIGNAL( changed() ),
212 this, TQ_SLOT( updateNameCombo() ) );
215 connect( core->addressBook(), TQ_SIGNAL( addressBookChanged( AddressBook* ) ),
216 this, TQ_SLOT( updateNameCombo() ) );
220 TQObject *catcher =
new DeletePressedCatcher(
this );
221 installEventFilter( catcher );
222 mContactView->installEventFilter( catcher );
224 mContactView->restoreLayout( TDEGlobal::config(),
"DistributionListViewColumns" );
226 TDEAcceleratorManager::manage(
this );
229 DistributionListWidget::~DistributionListWidget()
231 #ifndef TDEPIM_NEW_DISTRLISTS
235 mContactView->saveLayout( TDEGlobal::config(),
"DistributionListViewColumns" );
238 void DistributionListWidget::save()
240 #ifndef TDEPIM_NEW_DISTRLISTS
245 void DistributionListWidget::selectionContactViewChanged()
247 ContactItem *contactItem =
248 static_cast<ContactItem *
>( mContactView->selectedItem() );
249 bool state = contactItem;
251 mChangeEmailButton->setEnabled( state );
252 mRemoveContactButton->setEnabled( state );
255 bool DistributionListWidget::alreadyExists(
const TQString& distrListName )
const
257 #ifdef TDEPIM_NEW_DISTRLISTS
258 return core()->distributionListNames().contains( distrListName );
260 return mManager->listNames().contains( distrListName );
264 void DistributionListWidget::createList()
266 TQString newName = KInputDialog::getText( i18n(
"New Distribution List" ),
267 i18n(
"Please enter name:" ),
268 TQString(), 0,
this );
270 if ( newName.isEmpty() )
return;
272 if ( alreadyExists( newName ) ) {
273 KMessageBox::sorry(
this, i18n(
"The name already exists" ) );
276 #ifdef TDEPIM_NEW_DISTRLISTS
277 TDEABC::Resource* resource = core()->requestResource(
this );
281 KPIM::DistributionList dist;
282 dist.setResource( resource );
283 dist.setName( newName );
287 core()->addressBook()->insertAddressee( dist );
290 new TDEABC::DistributionList( mManager, newName );
297 mNameCombo->setCurrentText( newName );
302 void DistributionListWidget::editList()
304 const TQString oldName = mNameCombo->currentText();
306 const TQString newName = KInputDialog::getText( i18n(
"Rename Distribution List" ),
307 i18n(
"Please enter name:" ),
310 if ( newName.isEmpty() )
return;
312 if ( alreadyExists( newName ) ) {
313 KMessageBox::sorry(
this, i18n(
"The name already exists." ) );
316 #ifdef TDEPIM_NEW_DISTRLISTS
317 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
318 core()->addressBook(), mNameCombo->currentText() );
319 if ( dist.isEmpty() )
322 dist.setFormattedName( newName );
323 core()->addressBook()->insertAddressee( dist );
327 TDEABC::DistributionList *list = mManager->list( oldName );
328 list->setName( newName );
334 mNameCombo->setCurrentText( newName );
338 #ifndef TDEPIM_NEW_DISTRLISTS
343 void DistributionListWidget::removeList()
345 int result = KMessageBox::warningContinueCancel(
this,
346 i18n(
"<qt>Delete distribution list <b>%1</b>?</qt>" ) .arg( mNameCombo->currentText() ),
347 TQString(), KGuiItem( i18n(
"Delete"),
"edit-delete") );
349 if ( result != KMessageBox::Continue )
352 #ifdef TDEPIM_NEW_DISTRLISTS
353 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
354 core()->addressBook(), mNameCombo->currentText() );
355 if ( dist.isEmpty() )
358 emit deleted( dist.uid() );
359 core()->addressBook()->removeAddressee( dist );
361 mManager->remove( mManager->list( mNameCombo->currentText() ) );
362 mNameCombo->removeItem( mNameCombo->currentItem() );
370 void DistributionListWidget::addContact()
372 #ifdef TDEPIM_NEW_DISTRLISTS
373 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
374 core()->addressBook(), mNameCombo->currentText() );
375 if ( dist.isEmpty() ) {
376 kdDebug(5720) << k_funcinfo << mNameCombo->currentText() <<
" not found" << endl;
380 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
383 TDEABC::DistributionList& dist = *list;
386 const TDEABC::Addressee::List addrList = selectedContacts();
387 TDEABC::Addressee::List::ConstIterator it;
388 for ( it = addrList.begin(); it != addrList.end(); ++it )
389 dist.insertEntry( *it );
391 #ifdef TDEPIM_NEW_DISTRLISTS
392 core()->addressBook()->insertAddressee( dist );
400 void DistributionListWidget::removeContact()
402 #ifdef TDEPIM_NEW_DISTRLISTS
403 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
404 core()->addressBook(), mNameCombo->currentText() );
405 if ( dist.isEmpty() )
408 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
411 TDEABC::DistributionList& dist = *list;
414 ContactItem *contactItem =
415 static_cast<ContactItem *
>( mContactView->selectedItem() );
419 dist.removeEntry( contactItem->addressee(), contactItem->email() );
422 #ifdef TDEPIM_NEW_DISTRLISTS
423 core()->addressBook()->insertAddressee( dist );
430 void DistributionListWidget::changeEmail()
432 #ifdef TDEPIM_NEW_DISTRLISTS
433 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
434 core()->addressBook(), mNameCombo->currentText() );
435 if ( dist.isEmpty() )
438 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
441 TDEABC::DistributionList& dist = *list;
444 ContactItem *contactItem =
445 static_cast<ContactItem *
>( mContactView->selectedItem() );
449 bool canceled =
false;
450 const TQString email = EmailSelector::getEmail( contactItem->addressee().emails(),
451 contactItem->email(),
this, canceled);
454 dist.removeEntry( contactItem->addressee(), contactItem->email() );
455 dist.insertEntry( contactItem->addressee(), email );
457 #ifdef TDEPIM_NEW_DISTRLISTS
458 core()->addressBook()->insertAddressee( dist );
466 void DistributionListWidget::updateContactView()
468 mContactView->clear();
470 bool isListSelected =
false;
471 #ifdef TDEPIM_NEW_DISTRLISTS
472 KPIM::DistributionList dist;
473 if ( mNameCombo->count() != 0 )
474 dist = KPIM::DistributionList::findByName(
475 core()->addressBook(), mNameCombo->currentText() );
476 isListSelected = !dist.isEmpty();
478 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
479 isListSelected = list != 0;
481 if ( !isListSelected ) {
482 mEditListButton->setEnabled(
false );
483 mRemoveListButton->setEnabled(
false );
484 mChangeEmailButton->setEnabled(
false );
485 mRemoveContactButton->setEnabled(
false );
486 mContactView->setEnabled(
false );
489 mEditListButton->setEnabled(
true );
490 mRemoveListButton->setEnabled(
true );
491 mContactView->setEnabled(
true );
494 #ifdef TDEPIM_NEW_DISTRLISTS
495 const KPIM::DistributionList::Entry::List entries = dist.entries( core()->addressBook() );
496 KPIM::DistributionList::Entry::List::ConstIterator it;
498 const TDEABC::DistributionList::Entry::List entries = list->entries();
499 TDEABC::DistributionList::Entry::List::ConstIterator it;
501 for ( it = entries.begin(); it != entries.end(); ++it, ++entryCount )
502 new ContactItem( mContactView, (*it).addressee, (*it).email );
504 bool state = mContactView->selectedItem() != 0;
505 mChangeEmailButton->setEnabled( state );
506 mRemoveContactButton->setEnabled( state );
508 mEntryCountLabel->setText( i18n(
"Count: %n contact",
"Count: %n contacts", entryCount ) );
511 void DistributionListWidget::updateNameCombo()
513 int pos = mNameCombo->currentItem();
515 #ifdef TDEPIM_NEW_DISTRLISTS
516 const TQStringList names = core()->distributionListNames();
519 const TQStringList names = mManager->listNames();
521 mNameCombo->insertStringList( names );
522 mNameCombo->setCurrentItem( TQMIN( pos, (
int)names.count() - 1 ) );
527 void DistributionListWidget::dropEvent( TQDropEvent *e )
529 if ( mNameCombo->count() == 0 )
532 #ifdef TDEPIM_NEW_DISTRLISTS
533 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
534 core()->addressBook(), mNameCombo->currentText() );
535 if ( dist.isEmpty() )
538 TDEABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
541 TDEABC::DistributionList& dist = *list;
545 if ( KVCardDrag::decode( e, vcards ) ) {
546 TDEABC::VCardConverter converter;
547 const TDEABC::Addressee::List lst = converter.parseVCards( vcards );
548 for ( TDEABC::Addressee::List::ConstIterator it = lst.begin(); it != lst.end(); ++it )
549 dist.insertEntry( *it );
551 #ifdef TDEPIM_NEW_DISTRLISTS
552 core()->addressBook()->insertAddressee( dist );
561 void DistributionListWidget::contactsSelectionChanged()
563 mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 );
566 TQString DistributionListWidget::title()
const
568 return i18n(
"Distribution List Editor" );
571 TQString DistributionListWidget::identifier()
const
573 return "distribution_list_editor";
576 void DistributionListWidget::dropped( TQDropEvent *e, TQListViewItem* )
581 #ifdef TDEPIM_NEW_DISTRLISTS
582 void DistributionListWidget::changed(
const TDEABC::Addressee& dist )
584 emit modified( TDEABC::Addressee::List() << dist );
587 void DistributionListWidget::changed()
593 DistributionListView::DistributionListView( TQWidget *parent,
const char* name )
594 : TDEListView( parent, name )
596 setDragEnabled(
true );
597 setAcceptDrops(
true );
598 setAllColumnsShowFocus(
true );
601 void DistributionListView::dragEnterEvent( TQDragEnterEvent* e )
603 bool canDecode = TQTextDrag::canDecode( e );
604 e->accept( canDecode );
607 void DistributionListView::viewportDragMoveEvent( TQDragMoveEvent *e )
609 bool canDecode = TQTextDrag::canDecode( e );
610 e->accept( canDecode );
613 void DistributionListView::viewportDropEvent( TQDropEvent *e )
615 emit dropped( e, 0 );
618 void DistributionListView::dropEvent( TQDropEvent *e )
620 emit dropped( e, 0 );
624 EmailSelector::EmailSelector(
const TQStringList &emails,
625 const TQString ¤t, TQWidget *parent )
626 : KDialogBase( KDialogBase::Plain, i18n(
"Select Email Address"), Ok|Cancel, Ok,
629 TQFrame *topFrame = plainPage();
630 TQBoxLayout *topLayout =
new TQVBoxLayout( topFrame );
632 mButtonGroup =
new TQButtonGroup( 1, Horizontal, i18n(
"Email Addresses"),
634 mButtonGroup->setRadioButtonExclusive(
true );
635 topLayout->addWidget( mButtonGroup );
637 TQRadioButton *button =
new TQRadioButton( i18n(
"Preferred address"), mButtonGroup );
638 button->setDown(
true );
639 mEmailMap.insert( mButtonGroup->id( button ),
"" );
641 TQStringList::ConstIterator it;
642 for ( it = emails.begin(); it != emails.end(); ++it ) {
643 button =
new TQRadioButton( *it, mButtonGroup );
644 mEmailMap.insert( mButtonGroup->id( button ), *it );
645 if ( (*it) == current )
646 button->setDown(
true );
650 TQString EmailSelector::selected()
const
652 TQButton *button = mButtonGroup->selected();
654 return mEmailMap[ mButtonGroup->id( button ) ];
659 TQString EmailSelector::getEmail(
const TQStringList &emails,
660 const TQString ¤t, TQWidget *parent,
bool &canceled )
666 return dlg.selected();
673 #include "distributionlistwidget.moc"