distributionlistdialog.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <tqlistview.h> 00022 #include <tqlayout.h> 00023 #include <tqlabel.h> 00024 #include <tqpushbutton.h> 00025 #include <tqcombobox.h> 00026 #include <kinputdialog.h> 00027 #include <tqbuttongroup.h> 00028 #include <tqradiobutton.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, TQWidget *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, TQT_SIGNAL( okClicked() ), mEditor, TQT_SLOT( save() ) ); 00050 } 00051 00052 DistributionListDialog::~DistributionListDialog() 00053 { 00054 } 00055 00056 // TODO KDE4: Add d-pointer to EmailSelector, make sEmailMap a member variable 00057 static TQMap<TQWidget*,TQString> *sEmailMap = 0; 00058 00059 EmailSelector::EmailSelector( const TQStringList &emails, const TQString ¤t, 00060 TQWidget *parent ) : 00061 KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok, 00062 parent ) 00063 { 00064 if (!sEmailMap) 00065 sEmailMap = new TQMap<TQWidget*,TQString>(); 00066 TQFrame *topFrame = plainPage(); 00067 TQBoxLayout *topLayout = new TQVBoxLayout( topFrame ); 00068 00069 mButtonGroup = new TQButtonGroup( 1, Qt::Horizontal, i18n("Email Addresses"), 00070 topFrame ); 00071 topLayout->addWidget( mButtonGroup ); 00072 00073 TQStringList::ConstIterator it; 00074 for( it = emails.begin(); it != emails.end(); ++it ) { 00075 TQRadioButton *button = new TQRadioButton( *it, mButtonGroup ); 00076 sEmailMap->insert( button, *it ); 00077 if ( (*it) == current ) { 00078 mButtonGroup->setButton(mButtonGroup->id(button)); 00079 } 00080 } 00081 } 00082 00083 TQString EmailSelector::selected() 00084 { 00085 TQButton *button = mButtonGroup->selected(); 00086 if ( button ) return (*sEmailMap)[button]; 00087 return TQString::null; 00088 } 00089 00090 TQString EmailSelector::getEmail( const TQStringList &emails, const TQString ¤t, 00091 TQWidget *parent ) 00092 { 00093 EmailSelector *dlg = new EmailSelector( emails, current, parent ); 00094 dlg->exec(); 00095 00096 TQString result = dlg->selected(); 00097 00098 delete dlg; 00099 00100 return result; 00101 } 00102 00103 class EntryItem : public TQListViewItem 00104 { 00105 public: 00106 EntryItem( TQListView *parent, const Addressee &addressee, 00107 const TQString &email=TQString::null ) : 00108 TQListViewItem( 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 TQString email() const 00128 { 00129 return mEmail; 00130 } 00131 00132 private: 00133 Addressee mAddressee; 00134 TQString mEmail; 00135 }; 00136 00137 DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook, TQWidget *parent) : 00138 TQWidget( parent ), 00139 mAddressBook( addressBook ) 00140 { 00141 kdDebug(5700) << "DistributionListEditor()" << endl; 00142 00143 TQBoxLayout *topLayout = new TQVBoxLayout( this ); 00144 topLayout->setSpacing( KDialog::spacingHint() ); 00145 00146 TQBoxLayout *nameLayout = new TQHBoxLayout( topLayout) ; 00147 00148 mNameCombo = new TQComboBox( this ); 00149 nameLayout->addWidget( mNameCombo ); 00150 connect( mNameCombo, TQT_SIGNAL( activated( int ) ), TQT_SLOT( updateEntryView() ) ); 00151 00152 mNewButton = new TQPushButton( i18n("New List..."), this ); 00153 nameLayout->addWidget( mNewButton ); 00154 connect( mNewButton, TQT_SIGNAL( clicked() ), TQT_SLOT( newList() ) ); 00155 00156 mEditButton = new TQPushButton( i18n("Rename List..."), this ); 00157 nameLayout->addWidget( mEditButton ); 00158 connect( mEditButton, TQT_SIGNAL( clicked() ), TQT_SLOT( editList() ) ); 00159 00160 mRemoveButton = new TQPushButton( i18n("Remove List"), this ); 00161 nameLayout->addWidget( mRemoveButton ); 00162 connect( mRemoveButton, TQT_SIGNAL( clicked() ), TQT_SLOT( removeList() ) ); 00163 00164 TQGridLayout *gridLayout = new TQGridLayout( topLayout, 3, 3 ); 00165 gridLayout->setColStretch(1, 1); 00166 00167 TQLabel *listLabel = new TQLabel( i18n("Available addresses:"), this ); 00168 gridLayout->addWidget( listLabel, 0, 0 ); 00169 00170 mListLabel = new TQLabel( this ); 00171 gridLayout->addMultiCellWidget( mListLabel, 0, 0, 1, 2 ); 00172 00173 mAddresseeView = new TQListView( 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, TQT_SIGNAL( selectionChanged() ), 00179 TQT_SLOT( slotSelectionAddresseeViewChanged() ) ); 00180 connect( mAddresseeView, TQT_SIGNAL( doubleClicked( TQListViewItem * ) ), 00181 TQT_SLOT( addEntry() ) ); 00182 00183 mAddEntryButton = new TQPushButton( i18n("Add Entry"), this ); 00184 mAddEntryButton->setEnabled(false); 00185 gridLayout->addWidget( mAddEntryButton, 2, 0 ); 00186 connect( mAddEntryButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addEntry() ) ); 00187 00188 mEntryView = new TQListView( 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, TQT_SIGNAL( selectionChanged() ), 00196 TQT_SLOT( slotSelectionEntryViewChanged() ) ); 00197 00198 mChangeEmailButton = new TQPushButton( i18n("Change Email..."), this ); 00199 gridLayout->addWidget( mChangeEmailButton, 2, 1 ); 00200 connect( mChangeEmailButton, TQT_SIGNAL( clicked() ), TQT_SLOT( changeEmail() ) ); 00201 00202 mRemoveEntryButton = new TQPushButton( i18n("Remove Entry"), this ); 00203 gridLayout->addWidget( mRemoveEntryButton, 2, 2 ); 00204 connect( mRemoveEntryButton, TQT_SIGNAL( clicked() ), TQT_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 TQString name = KInputDialog::getText( i18n( "New Distribution List" ), 00238 i18n( "Please enter &name:" ), TQString::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 TQString oldName = mNameCombo->currentText(); 00254 bool ok; 00255 TQString 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 TQString::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 TQString 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 }