00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qlistview.h>
00022 #include <qlayout.h>
00023 #include <qlabel.h>
00024 #include <qpushbutton.h>
00025 #include <qcombobox.h>
00026 #include <kinputdialog.h>
00027 #include <qbuttongroup.h>
00028 #include <qradiobutton.h>
00029
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kmessagebox.h>
00033
00034 #include "addressbook.h"
00035 #include "addresseedialog.h"
00036 #include "distributionlist.h"
00037
00038 #include "distributionlistdialog.h"
00039 #include "distributionlistdialog.moc"
00040
00041 using namespace KABC;
00042
00043 DistributionListDialog::DistributionListDialog( AddressBook *addressBook, QWidget *parent)
00044 : KDialogBase( parent, "", true, i18n("Configure Distribution Lists"), Ok, Ok, true)
00045 {
00046 mEditor = new DistributionListEditorWidget( addressBook, this );
00047 setMainWidget( mEditor );
00048
00049 connect( this, SIGNAL( okClicked() ), mEditor, SLOT( save() ) );
00050 }
00051
00052 DistributionListDialog::~DistributionListDialog()
00053 {
00054 }
00055
00056 static QMap<QWidget*,QString> *sEmailMap = 0;
00057
00058 EmailSelector::EmailSelector( const QStringList &emails, const QString ¤t,
00059 QWidget *parent ) :
00060 KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00061 parent )
00062 {
00063 if (!sEmailMap)
00064 sEmailMap = new QMap<QWidget*,QString>();
00065 QFrame *topFrame = plainPage();
00066 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00067
00068 mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00069 topFrame );
00070 topLayout->addWidget( mButtonGroup );
00071
00072 QStringList::ConstIterator it;
00073 for( it = emails.begin(); it != emails.end(); ++it ) {
00074 QRadioButton *button = new QRadioButton( *it, mButtonGroup );
00075 sEmailMap->insert( button, *it );
00076 if ( (*it) == current ) {
00077 mButtonGroup->setButton(mButtonGroup->id(button));
00078 }
00079 }
00080 }
00081
00082 QString EmailSelector::selected()
00083 {
00084 QButton *button = mButtonGroup->selected();
00085 if ( button ) return (*sEmailMap)[button];
00086 return QString::null;
00087 }
00088
00089 QString EmailSelector::getEmail( const QStringList &emails, const QString ¤t,
00090 QWidget *parent )
00091 {
00092 EmailSelector *dlg = new EmailSelector( emails, current, parent );
00093 dlg->exec();
00094
00095 QString result = dlg->selected();
00096
00097 delete dlg;
00098
00099 return result;
00100 }
00101
00102 class EntryItem : public QListViewItem
00103 {
00104 public:
00105 EntryItem( QListView *parent, const Addressee &addressee,
00106 const QString &email=QString::null ) :
00107 QListViewItem( parent ),
00108 mAddressee( addressee ),
00109 mEmail( email )
00110 {
00111 setText( 0, addressee.realName() );
00112 if( email.isEmpty() ) {
00113 setText( 1, addressee.preferredEmail() );
00114 setText( 2, i18n("Yes") );
00115 } else {
00116 setText( 1, email );
00117 setText( 2, i18n("No") );
00118 }
00119 }
00120
00121 Addressee addressee() const
00122 {
00123 return mAddressee;
00124 }
00125
00126 QString email() const
00127 {
00128 return mEmail;
00129 }
00130
00131 private:
00132 Addressee mAddressee;
00133 QString mEmail;
00134 };
00135
00136 DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook, QWidget *parent) :
00137 QWidget( parent ),
00138 mAddressBook( addressBook )
00139 {
00140 kdDebug(5700) << "DistributionListEditor()" << endl;
00141
00142 QBoxLayout *topLayout = new QVBoxLayout( this );
00143 topLayout->setSpacing( KDialog::spacingHint() );
00144
00145 QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
00146
00147 mNameCombo = new QComboBox( this );
00148 nameLayout->addWidget( mNameCombo );
00149 connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
00150
00151 mNewButton = new QPushButton( i18n("New List..."), this );
00152 nameLayout->addWidget( mNewButton );
00153 connect( mNewButton, SIGNAL( clicked() ), SLOT( newList() ) );
00154
00155 mEditButton = new QPushButton( i18n("Rename List..."), this );
00156 nameLayout->addWidget( mEditButton );
00157 connect( mEditButton, SIGNAL( clicked() ), SLOT( editList() ) );
00158
00159 mRemoveButton = new QPushButton( i18n("Remove List"), this );
00160 nameLayout->addWidget( mRemoveButton );
00161 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00162
00163 QGridLayout *gridLayout = new QGridLayout( topLayout, 3, 3 );
00164 gridLayout->setColStretch(1, 1);
00165
00166 QLabel *listLabel = new QLabel( i18n("Available addresses:"), this );
00167 gridLayout->addWidget( listLabel, 0, 0 );
00168
00169 mListLabel = new QLabel( this );
00170 gridLayout->addMultiCellWidget( mListLabel, 0, 0, 1, 2 );
00171
00172 mAddresseeView = new QListView( this );
00173 mAddresseeView->addColumn( i18n("Name") );
00174 mAddresseeView->addColumn( i18n("Preferred Email") );
00175 mAddresseeView->setAllColumnsShowFocus( true );
00176 gridLayout->addWidget( mAddresseeView, 1, 0 );
00177 connect( mAddresseeView, SIGNAL( selectionChanged() ),
00178 SLOT( slotSelectionAddresseeViewChanged() ) );
00179 connect( mAddresseeView, SIGNAL( doubleClicked( QListViewItem * ) ),
00180 SLOT( addEntry() ) );
00181
00182 mAddEntryButton = new QPushButton( i18n("Add Entry"), this );
00183 mAddEntryButton->setEnabled(false);
00184 gridLayout->addWidget( mAddEntryButton, 2, 0 );
00185 connect( mAddEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00186
00187 mEntryView = new QListView( this );
00188 mEntryView->addColumn( i18n("Name") );
00189 mEntryView->addColumn( i18n("Email") );
00190 mEntryView->addColumn( i18n("Use Preferred") );
00191 mEntryView->setEnabled(false);
00192 mEntryView->setAllColumnsShowFocus( true );
00193 gridLayout->addMultiCellWidget( mEntryView, 1, 1, 1, 2 );
00194 connect( mEntryView, SIGNAL( selectionChanged() ),
00195 SLOT( slotSelectionEntryViewChanged() ) );
00196
00197 mChangeEmailButton = new QPushButton( i18n("Change Email..."), this );
00198 gridLayout->addWidget( mChangeEmailButton, 2, 1 );
00199 connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00200
00201 mRemoveEntryButton = new QPushButton( i18n("Remove Entry"), this );
00202 gridLayout->addWidget( mRemoveEntryButton, 2, 2 );
00203 connect( mRemoveEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00204
00205 mManager = new DistributionListManager( mAddressBook );
00206 mManager->load();
00207
00208 updateAddresseeView();
00209 updateNameCombo();
00210 }
00211
00212 DistributionListEditorWidget::~DistributionListEditorWidget()
00213 {
00214 kdDebug(5700) << "~DistributionListEditor()" << endl;
00215
00216 delete mManager;
00217 }
00218
00219 void DistributionListEditorWidget::save()
00220 {
00221 mManager->save();
00222 }
00223
00224 void DistributionListEditorWidget::slotSelectionEntryViewChanged()
00225 {
00226 EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
00227 bool state=entryItem;
00228
00229 mChangeEmailButton->setEnabled(state);
00230 mRemoveEntryButton->setEnabled(state);
00231 }
00232
00233 void DistributionListEditorWidget::newList()
00234 {
00235 bool ok;
00236 QString name = KInputDialog::getText( i18n( "New Distribution List" ),
00237 i18n( "Please enter &name:" ), QString::null, &ok );
00238 if (!ok) return;
00239
00240 new DistributionList( mManager, name );
00241
00242 mNameCombo->clear();
00243 mNameCombo->insertStringList( mManager->listNames() );
00244 mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00245
00246 updateEntryView();
00247 slotSelectionAddresseeViewChanged();
00248 }
00249
00250 void DistributionListEditorWidget::editList()
00251 {
00252 QString oldName = mNameCombo->currentText();
00253 bool ok;
00254 QString name = KInputDialog::getText( i18n( "Distribution List" ),
00255 i18n( "Please change &name:" ), oldName, &ok );
00256 if (!ok) return;
00257
00258 DistributionList *list = mManager->list( oldName );
00259 list->setName( name );
00260
00261 mNameCombo->clear();
00262 mNameCombo->insertStringList( mManager->listNames() );
00263 mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00264
00265 updateEntryView();
00266 slotSelectionAddresseeViewChanged();
00267 }
00268
00269 void DistributionListEditorWidget::removeList()
00270 {
00271 int result = KMessageBox::warningContinueCancel( this,
00272 i18n("Delete distribution list '%1'?") .arg( mNameCombo->currentText() ),
00273 QString::null, KStdGuiItem::del() );
00274
00275 if ( result != KMessageBox::Continue ) return;
00276
00277 mManager->remove( mManager->list( mNameCombo->currentText() ) );
00278 mNameCombo->removeItem( mNameCombo->currentItem() );
00279
00280 updateEntryView();
00281 slotSelectionAddresseeViewChanged();
00282 }
00283
00284 void DistributionListEditorWidget::addEntry()
00285 {
00286 AddresseeItem *addresseeItem =
00287 static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00288
00289 if( !addresseeItem ) {
00290 kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
00291 return;
00292 }
00293
00294 DistributionList *list = mManager->list( mNameCombo->currentText() );
00295 if ( !list ) {
00296 kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
00297 return;
00298 }
00299
00300 list->insertEntry( addresseeItem->addressee() );
00301 updateEntryView();
00302 slotSelectionAddresseeViewChanged();
00303 }
00304
00305 void DistributionListEditorWidget::removeEntry()
00306 {
00307 DistributionList *list = mManager->list( mNameCombo->currentText() );
00308 if ( !list ) return;
00309
00310 EntryItem *entryItem =
00311 static_cast<EntryItem *>( mEntryView->selectedItem() );
00312 if ( !entryItem ) return;
00313
00314 list->removeEntry( entryItem->addressee(), entryItem->email() );
00315 delete entryItem;
00316 }
00317
00318 void DistributionListEditorWidget::changeEmail()
00319 {
00320 DistributionList *list = mManager->list( mNameCombo->currentText() );
00321 if ( !list ) return;
00322
00323 EntryItem *entryItem =
00324 static_cast<EntryItem *>( mEntryView->selectedItem() );
00325 if ( !entryItem ) return;
00326
00327 QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
00328 entryItem->email(), this );
00329 list->removeEntry( entryItem->addressee(), entryItem->email() );
00330 list->insertEntry( entryItem->addressee(), email );
00331
00332 updateEntryView();
00333 }
00334
00335 void DistributionListEditorWidget::updateEntryView()
00336 {
00337 if ( mNameCombo->currentText().isEmpty() ) {
00338 mListLabel->setText( i18n("Selected addressees:") );
00339 } else {
00340 mListLabel->setText( i18n("Selected addresses in '%1':")
00341 .arg( mNameCombo->currentText() ) );
00342 }
00343
00344 mEntryView->clear();
00345
00346 DistributionList *list = mManager->list( mNameCombo->currentText() );
00347 if ( !list ) {
00348 mEditButton->setEnabled(false);
00349 mRemoveButton->setEnabled(false);
00350 mChangeEmailButton->setEnabled(false);
00351 mRemoveEntryButton->setEnabled(false);
00352 mAddresseeView->setEnabled(false);
00353 mEntryView->setEnabled(false);
00354 return;
00355 } else {
00356 mEditButton->setEnabled(true);
00357 mRemoveButton->setEnabled(true);
00358 mAddresseeView->setEnabled(true);
00359 mEntryView->setEnabled(true);
00360 }
00361
00362 DistributionList::Entry::List entries = list->entries();
00363 DistributionList::Entry::List::ConstIterator it;
00364 for( it = entries.begin(); it != entries.end(); ++it ) {
00365 new EntryItem( mEntryView, (*it).addressee, (*it).email );
00366 }
00367
00368 EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
00369 bool state=entryItem;
00370
00371 mChangeEmailButton->setEnabled(state);
00372 mRemoveEntryButton->setEnabled(state);
00373 }
00374
00375 void DistributionListEditorWidget::updateAddresseeView()
00376 {
00377 mAddresseeView->clear();
00378
00379 AddressBook::Iterator it;
00380 for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00381 new AddresseeItem( mAddresseeView, *it );
00382 }
00383 }
00384
00385 void DistributionListEditorWidget::updateNameCombo()
00386 {
00387 mNameCombo->insertStringList( mManager->listNames() );
00388
00389 updateEntryView();
00390 }
00391
00392 void DistributionListEditorWidget::slotSelectionAddresseeViewChanged()
00393 {
00394 AddresseeItem *addresseeItem =
00395 static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00396 bool state=addresseeItem;
00397 mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
00398 }