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