00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <qbuttongroup.h>
00024 #include <qcombobox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qlistview.h>
00028 #include <qpushbutton.h>
00029 #include <qradiobutton.h>
00030
00031 #include <kaccelmanager.h>
00032 #include <kdebug.h>
00033 #include <kinputdialog.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036
00037 #include <kabc/addresseedialog.h>
00038 #include <kabc/distributionlist.h>
00039 #include <kabc/stdaddressbook.h>
00040 #include <kabc/vcardconverter.h>
00041 #include <libkdepim/kvcarddrag.h>
00042
00043 #include <core.h>
00044 #include "distributionlistwidget.h"
00045
00046 class DistributionListFactory : public KAB::ExtensionFactory
00047 {
00048 public:
00049 KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00050 {
00051 return new DistributionListWidget( core, parent, name );
00052 }
00053
00054 QString identifier() const
00055 {
00056 return "distribution_list_editor";
00057 }
00058 };
00059
00060 extern "C" {
00061 void *init_libkaddrbk_distributionlist()
00062 {
00063 return ( new DistributionListFactory );
00064 }
00065 }
00066
00067 class ContactItem : public QListViewItem
00068 {
00069 public:
00070 ContactItem( DistributionListView *parent, const KABC::Addressee &addressee,
00071 const QString &email = QString::null ) :
00072 QListViewItem( parent ),
00073 mAddressee( addressee ),
00074 mEmail( email )
00075 {
00076 setText( 0, addressee.realName() );
00077 if( email.isEmpty() ) {
00078 setText( 1, addressee.preferredEmail() );
00079 setText( 2, i18n( "Yes" ) );
00080 } else {
00081 setText( 1, email );
00082 setText( 2, i18n( "No" ) );
00083 }
00084 }
00085
00086 KABC::Addressee addressee() const
00087 {
00088 return mAddressee;
00089 }
00090
00091 QString email() const
00092 {
00093 return mEmail;
00094 }
00095
00096 protected:
00097 bool acceptDrop( const QMimeSource* )
00098 {
00099 return true;
00100 }
00101
00102 private:
00103 KABC::Addressee mAddressee;
00104 QString mEmail;
00105 };
00106
00107 DistributionListWidget::DistributionListWidget( KAB::Core *core, QWidget *parent,
00108 const char *name )
00109 : KAB::ExtensionWidget( core, parent, name )
00110 {
00111 QGridLayout *topLayout = new QGridLayout( this, 3, 4, KDialog::marginHint(),
00112 KDialog::spacingHint() );
00113
00114 mNameCombo = new QComboBox( this );
00115 topLayout->addWidget( mNameCombo, 0, 0 );
00116 connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateContactView() ) );
00117
00118 mCreateListButton = new QPushButton( i18n( "New List..." ), this );
00119 topLayout->addWidget( mCreateListButton, 0, 1 );
00120 connect( mCreateListButton, SIGNAL( clicked() ), SLOT( createList() ) );
00121
00122 mEditListButton = new QPushButton( i18n( "Rename List..." ), this );
00123 topLayout->addWidget( mEditListButton, 0, 2 );
00124 connect( mEditListButton, SIGNAL( clicked() ), SLOT( editList() ) );
00125
00126 mRemoveListButton = new QPushButton( i18n( "Remove List" ), this );
00127 topLayout->addWidget( mRemoveListButton, 0, 3 );
00128 connect( mRemoveListButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00129
00130 mContactView = new DistributionListView( this );
00131 mContactView->addColumn( i18n( "Name" ) );
00132 mContactView->addColumn( i18n( "Email" ) );
00133 mContactView->addColumn( i18n( "Use Preferred" ) );
00134 mContactView->setEnabled( false );
00135 mContactView->setAllColumnsShowFocus( true );
00136 mContactView->setFullWidth( true );
00137 topLayout->addMultiCellWidget( mContactView, 1, 1, 0, 3 );
00138 connect( mContactView, SIGNAL( selectionChanged() ),
00139 SLOT( selectionContactViewChanged() ) );
00140 connect( mContactView, SIGNAL( dropped( QDropEvent*, QListViewItem* ) ),
00141 SLOT( dropped( QDropEvent*, QListViewItem* ) ) );
00142
00143 mAddContactButton = new QPushButton( i18n( "Add Contact" ), this );
00144 mAddContactButton->setEnabled( false );
00145 topLayout->addWidget( mAddContactButton, 2, 0 );
00146 connect( mAddContactButton, SIGNAL( clicked() ), SLOT( addContact() ) );
00147
00148 mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this );
00149 topLayout->addWidget( mChangeEmailButton, 2, 2 );
00150 connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00151
00152 mRemoveContactButton = new QPushButton( i18n( "Remove Contact" ), this );
00153 topLayout->addWidget( mRemoveContactButton, 2, 3 );
00154 connect( mRemoveContactButton, SIGNAL( clicked() ), SLOT( removeContact() ) );
00155
00156 connect( core, SIGNAL( contactsUpdated() ),
00157 this, SLOT( updateNameCombo() ) );
00158
00159 updateNameCombo();
00160
00161 KAcceleratorManager::manage( this );
00162 }
00163
00164 DistributionListWidget::~DistributionListWidget()
00165 {
00166 }
00167
00168 void DistributionListWidget::selectionContactViewChanged()
00169 {
00170 ContactItem *contactItem =
00171 static_cast<ContactItem *>( mContactView->selectedItem() );
00172 bool state = contactItem;
00173
00174 mChangeEmailButton->setEnabled( state );
00175 mRemoveContactButton->setEnabled( state );
00176 }
00177
00178 void DistributionListWidget::createList()
00179 {
00180 QString newName = KInputDialog::getText( i18n( "New Distribution List" ),
00181 i18n( "Please enter name:" ),
00182 QString::null, 0, this );
00183
00184 if ( newName.isEmpty() ) return;
00185
00186 QStringList names = core()->distributionListNames();
00187 if ( names.contains( newName ) ) {
00188 KMessageBox::sorry( this, i18n( "The name already exists" ) );
00189 return;
00190 }
00191
00192 KABC::Resource* resource = core()->requestResource( this );
00193 if ( !resource )
00194 return;
00195
00196 KPIM::DistributionList dist;
00197 dist.setResource( resource );
00198 dist.setName( newName );
00199 core()->addressBook()->insertAddressee( dist );
00200
00201
00202
00203 changed( dist );
00204
00205
00206 mNameCombo->setCurrentText( newName );
00207
00208 updateContactView();
00209 }
00210
00211 void DistributionListWidget::editList()
00212 {
00213 QString oldName = mNameCombo->currentText();
00214
00215 QString newName = KInputDialog::getText( i18n( "Distribution List Editor" ),
00216 i18n( "Please enter name:" ),
00217 oldName, 0, this );
00218
00219 if ( newName.isEmpty() ) return;
00220
00221 QStringList names = core()->distributionListNames();
00222
00223 if ( names.contains( newName ) ) {
00224 KMessageBox::sorry( this, i18n( "The name already exists" ) );
00225 return;
00226 }
00227
00228 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00229 core()->addressBook(), mNameCombo->currentText() );
00230 if ( dist.isEmpty() )
00231 return;
00232
00233 dist.setFormattedName( newName );
00234 core()->addressBook()->insertAddressee( dist );
00235
00236 changed( dist );
00237
00238
00239 mNameCombo->setCurrentText( newName );
00240
00241 updateContactView();
00242 }
00243
00244 void DistributionListWidget::removeList()
00245 {
00246 int result = KMessageBox::warningContinueCancel( this,
00247 i18n( "<qt>Delete distribution list <b>%1</b>?</qt>" ) .arg( mNameCombo->currentText() ),
00248 QString::null, KGuiItem( i18n("Delete"), "editdelete") );
00249
00250 if ( result != KMessageBox::Continue )
00251 return;
00252
00253 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00254 core()->addressBook(), mNameCombo->currentText() );
00255 if ( dist.isEmpty() )
00256 return;
00257
00258 core()->addressBook()->removeAddressee( dist );
00259
00260 emit deleted( dist.uid() );
00261 }
00262
00263 void DistributionListWidget::addContact()
00264 {
00265 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00266 core()->addressBook(), mNameCombo->currentText() );
00267 if ( dist.isEmpty() ) {
00268 kdDebug(5720) << k_funcinfo << mNameCombo->currentText() << " not found" << endl;
00269 return;
00270 }
00271
00272 KABC::Addressee::List addrList = selectedContacts();
00273 KABC::Addressee::List::Iterator it;
00274 for ( it = addrList.begin(); it != addrList.end(); ++it )
00275 dist.insertEntry( *it );
00276
00277 core()->addressBook()->insertAddressee( dist );
00278
00279
00280
00281 changed( dist );
00282 }
00283
00284 void DistributionListWidget::removeContact()
00285 {
00286 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00287 core()->addressBook(), mNameCombo->currentText() );
00288 if ( dist.isEmpty() )
00289 return;
00290
00291 ContactItem *contactItem =
00292 static_cast<ContactItem *>( mContactView->selectedItem() );
00293 if ( !contactItem )
00294 return;
00295
00296 dist.removeEntry( contactItem->addressee(), contactItem->email() );
00297 core()->addressBook()->insertAddressee( dist );
00298 delete contactItem;
00299
00300 changed( dist );
00301 }
00302
00303 void DistributionListWidget::changeEmail()
00304 {
00305 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00306 core()->addressBook(), mNameCombo->currentText() );
00307 if ( dist.isEmpty() )
00308 return;
00309
00310 ContactItem *contactItem =
00311 static_cast<ContactItem *>( mContactView->selectedItem() );
00312 if ( !contactItem )
00313 return;
00314
00315 QString email = EmailSelector::getEmail( contactItem->addressee().emails(),
00316 contactItem->email(), this );
00317 dist.removeEntry( contactItem->addressee(), contactItem->email() );
00318 dist.insertEntry( contactItem->addressee(), email );
00319 core()->addressBook()->insertAddressee( dist );
00320
00321
00322
00323 changed( dist );
00324 }
00325
00326 void DistributionListWidget::updateContactView()
00327 {
00328 mContactView->clear();
00329
00330 KPIM::DistributionList dist;
00331 if ( mNameCombo->count() != 0 )
00332 dist = KPIM::DistributionList::findByName(
00333 core()->addressBook(), mNameCombo->currentText() );
00334
00335 if ( dist.isEmpty() ) {
00336 mEditListButton->setEnabled( false );
00337 mRemoveListButton->setEnabled( false );
00338 mChangeEmailButton->setEnabled( false );
00339 mRemoveContactButton->setEnabled( false );
00340 mContactView->setEnabled( false );
00341 return;
00342 } else {
00343 mEditListButton->setEnabled( true );
00344 mRemoveListButton->setEnabled( true );
00345 mContactView->setEnabled( true );
00346 }
00347
00348 KPIM::DistributionList::Entry::List entries = dist.entries( core()->addressBook() );
00349 KPIM::DistributionList::Entry::List::ConstIterator it;
00350 for( it = entries.begin(); it != entries.end(); ++it )
00351 new ContactItem( mContactView, (*it).addressee, (*it).email );
00352
00353 ContactItem *contactItem =
00354 static_cast<ContactItem *>( mContactView->selectedItem() );
00355 bool state = contactItem;
00356
00357 mChangeEmailButton->setEnabled( state );
00358 mRemoveContactButton->setEnabled( state );
00359 }
00360
00361 void DistributionListWidget::updateNameCombo()
00362 {
00363 int pos = mNameCombo->currentItem();
00364 mNameCombo->clear();
00365 const QStringList names = core()->distributionListNames();
00366 mNameCombo->insertStringList( names );
00367 mNameCombo->setCurrentItem( QMIN( pos, (int)names.count() - 1 ) );
00368
00369 updateContactView();
00370 }
00371
00372 void DistributionListWidget::dropEvent( QDropEvent *e )
00373 {
00374 if ( mNameCombo->count() == 0 )
00375 return;
00376 KPIM::DistributionList dist = KPIM::DistributionList::findByName(
00377 core()->addressBook(), mNameCombo->currentText() );
00378 if ( dist.isEmpty() )
00379 return;
00380
00381 QString vcards;
00382 if ( KVCardDrag::decode( e, vcards ) ) {
00383 KABC::VCardConverter converter;
00384 KABC::Addressee::List list = converter.parseVCards( vcards );
00385 KABC::Addressee::List::Iterator it;
00386 for ( it = list.begin(); it != list.end(); ++it )
00387 dist.insertEntry( *it );
00388
00389 core()->addressBook()->insertAddressee( dist );
00390
00391 changed( dist );
00392 }
00393 }
00394
00395 void DistributionListWidget::contactsSelectionChanged()
00396 {
00397 mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 );
00398 }
00399
00400 QString DistributionListWidget::title() const
00401 {
00402 return i18n( "Distribution List Editor" );
00403 }
00404
00405 QString DistributionListWidget::identifier() const
00406 {
00407 return "distribution_list_editor";
00408 }
00409
00410 void DistributionListWidget::dropped( QDropEvent *e, QListViewItem* )
00411 {
00412 dropEvent( e );
00413 }
00414
00415 void DistributionListWidget::changed( const KABC::Addressee& dist )
00416 {
00417 emit modified( KABC::Addressee::List() << dist );
00418 }
00419
00420
00421 DistributionListView::DistributionListView( QWidget *parent, const char* name )
00422 : KListView( parent, name )
00423 {
00424 setDragEnabled( true );
00425 setAcceptDrops( true );
00426 setAllColumnsShowFocus( true );
00427 }
00428
00429 void DistributionListView::dragEnterEvent( QDragEnterEvent* e )
00430 {
00431 bool canDecode = QTextDrag::canDecode( e );
00432 e->accept( canDecode );
00433 }
00434
00435 void DistributionListView::viewportDragMoveEvent( QDragMoveEvent *e )
00436 {
00437 bool canDecode = QTextDrag::canDecode( e );
00438 e->accept( canDecode );
00439 }
00440
00441 void DistributionListView::viewportDropEvent( QDropEvent *e )
00442 {
00443 emit dropped( e, 0 );
00444 }
00445
00446 void DistributionListView::dropEvent( QDropEvent *e )
00447 {
00448 emit dropped( e, 0 );
00449 }
00450
00451
00452 EmailSelector::EmailSelector( const QStringList &emails,
00453 const QString ¤t, QWidget *parent )
00454 : KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00455 parent )
00456 {
00457 QFrame *topFrame = plainPage();
00458 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00459
00460 mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00461 topFrame );
00462 topLayout->addWidget( mButtonGroup );
00463
00464 QRadioButton *button = new QRadioButton( i18n("Preferred address"), mButtonGroup );
00465 button->setDown( true );
00466 mEmailMap.insert( mButtonGroup->id( button ), "" );
00467
00468 QStringList::ConstIterator it;
00469 for ( it = emails.begin(); it != emails.end(); ++it ) {
00470 button = new QRadioButton( *it, mButtonGroup );
00471 mEmailMap.insert( mButtonGroup->id( button ), *it );
00472 if ( (*it) == current )
00473 button->setDown( true );
00474 }
00475 }
00476
00477 QString EmailSelector::selected() const
00478 {
00479 QButton *button = mButtonGroup->selected();
00480 if ( button )
00481 return mEmailMap[ mButtonGroup->id( button ) ];
00482
00483 return QString::null;
00484 }
00485
00486 QString EmailSelector::getEmail( const QStringList &emails,
00487 const QString ¤t, QWidget *parent )
00488 {
00489 EmailSelector dlg( emails, current, parent );
00490 dlg.exec();
00491
00492 return dlg.selected();
00493 }
00494
00495
00496 #include "distributionlistwidget.moc"