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