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 <qpushbutton.h>
00024 #include <qcombobox.h>
00025 #include <qbuttongroup.h>
00026 #include <qradiobutton.h>
00027
00028 #include <kinputdialog.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031
00032 #include "addressbook.h"
00033 #include "addresseedialog.h"
00034 #include "distributionlist.h"
00035
00036 #include "distributionlisteditor.h"
00037 #include "distributionlisteditor.moc"
00038
00039 using namespace KABC;
00040
00041 EmailSelectDialog::EmailSelectDialog( const QStringList &emails, const QString ¤t,
00042 QWidget *parent ) :
00043 KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00044 parent )
00045 {
00046 QFrame *topFrame = plainPage();
00047 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00048
00049 mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00050 topFrame );
00051 mButtonGroup->setRadioButtonExclusive( true );
00052 topLayout->addWidget( mButtonGroup );
00053
00054 QStringList::ConstIterator it;
00055 for( it = emails.begin(); it != emails.end(); ++it ) {
00056 QRadioButton *button = new QRadioButton( *it, mButtonGroup );
00057 if ( (*it) == current ) {
00058 button->setDown( true );
00059 }
00060 }
00061 }
00062
00063 QString EmailSelectDialog::selected()
00064 {
00065 QButton *button = mButtonGroup->selected();
00066 if ( button ) return button->text();
00067 return QString::null;
00068 }
00069
00070 QString EmailSelectDialog::getEmail( const QStringList &emails, const QString ¤t,
00071 QWidget *parent )
00072 {
00073 EmailSelectDialog *dlg = new EmailSelectDialog( emails, current, parent );
00074 dlg->exec();
00075
00076 QString result = dlg->selected();
00077
00078 delete dlg;
00079
00080 return result;
00081 }
00082
00083 class EditEntryItem : public QListViewItem
00084 {
00085 public:
00086 EditEntryItem( QListView *parent, const Addressee &addressee,
00087 const QString &email=QString::null ) :
00088 QListViewItem( parent ),
00089 mAddressee( addressee ),
00090 mEmail( email )
00091 {
00092 setText( 0, addressee.realName() );
00093 if( email.isEmpty() ) {
00094 setText( 1, addressee.preferredEmail() );
00095 setText( 2, i18n("Yes") );
00096 } else {
00097 setText( 1, email );
00098 setText( 2, i18n("No") );
00099 }
00100 }
00101
00102 Addressee addressee() const
00103 {
00104 return mAddressee;
00105 }
00106
00107 QString email() const
00108 {
00109 return mEmail;
00110 }
00111
00112 private:
00113 Addressee mAddressee;
00114 QString mEmail;
00115 };
00116
00117 DistributionListEditor::DistributionListEditor( AddressBook *addressBook, QWidget *parent) :
00118 QWidget( parent ),
00119 mAddressBook( addressBook )
00120 {
00121 kdDebug(5700) << "DistributionListEditor()" << endl;
00122
00123 QBoxLayout *topLayout = new QVBoxLayout( this );
00124 topLayout->setMargin( KDialog::marginHint() );
00125 topLayout->setSpacing( KDialog::spacingHint() );
00126
00127 QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
00128
00129 mNameCombo = new QComboBox( this );
00130 nameLayout->addWidget( mNameCombo );
00131 connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
00132
00133 newButton = new QPushButton( i18n("New List"), this );
00134 nameLayout->addWidget( newButton );
00135 connect( newButton, SIGNAL( clicked() ), SLOT( newList() ) );
00136
00137 removeButton = new QPushButton( i18n("Remove List"), this );
00138 nameLayout->addWidget( removeButton );
00139 connect( removeButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00140
00141 mEntryView = new QListView( this );
00142 mEntryView->addColumn( i18n("Name") );
00143 mEntryView->addColumn( i18n("Email") );
00144 mEntryView->addColumn( i18n("Use Preferred") );
00145 topLayout->addWidget( mEntryView );
00146 connect(mEntryView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionEntryViewChanged()));
00147
00148 changeEmailButton = new QPushButton( i18n("Change Email"), this );
00149 topLayout->addWidget( changeEmailButton );
00150 connect( changeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00151
00152 removeEntryButton = new QPushButton( i18n("Remove Entry"), this );
00153 topLayout->addWidget( removeEntryButton );
00154 connect( removeEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00155
00156 addEntryButton = new QPushButton( i18n("Add Entry"), this );
00157 topLayout->addWidget( addEntryButton );
00158 connect( addEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00159
00160 mAddresseeView = new QListView( this );
00161 mAddresseeView->addColumn( i18n("Name") );
00162 mAddresseeView->addColumn( i18n("Preferred Email") );
00163 topLayout->addWidget( mAddresseeView );
00164
00165
00166 connect(mAddresseeView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionAddresseeViewChanged()));
00167
00168 mManager = new DistributionListManager( mAddressBook );
00169 mManager->load();
00170
00171 updateAddresseeView();
00172 updateNameCombo();
00173 removeButton->setEnabled(!mManager->listNames().isEmpty());
00174 }
00175
00176 DistributionListEditor::~DistributionListEditor()
00177 {
00178 kdDebug(5700) << "~DistributionListEditor()" << endl;
00179
00180 mManager->save();
00181 delete mManager;
00182 }
00183
00184 void DistributionListEditor::slotSelectionEntryViewChanged()
00185 {
00186 EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00187 bool state = (entryItem != 0L);
00188
00189 changeEmailButton->setEnabled(state);
00190 removeEntryButton->setEnabled(state);
00191 }
00192
00193 void DistributionListEditor::newList()
00194 {
00195 bool ok = false;
00196 QString name = KInputDialog::getText( i18n("New Distribution List"),
00197 i18n("Please enter name:"),
00198 QString::null, &ok, this );
00199 if ( !ok )
00200 return;
00201
00202 new DistributionList( mManager, name );
00203
00204 mNameCombo->insertItem( name );
00205 removeButton->setEnabled(true);
00206 updateEntryView();
00207 }
00208
00209 void DistributionListEditor::removeList()
00210 {
00211 mManager->remove( mManager->list( mNameCombo->currentText() ) );
00212 mNameCombo->removeItem( mNameCombo->currentItem() );
00213 removeButton->setEnabled(!mManager->listNames().isEmpty());
00214 addEntryButton->setEnabled( !mNameCombo->currentText().isEmpty());
00215 updateEntryView();
00216 }
00217
00218 void DistributionListEditor::addEntry()
00219 {
00220 AddresseeItem *addresseeItem =
00221 dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00222
00223 if( !addresseeItem ) {
00224 kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
00225 return;
00226 }
00227
00228 DistributionList *list = mManager->list( mNameCombo->currentText() );
00229 if ( !list ) {
00230 kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
00231 return;
00232 }
00233
00234 list->insertEntry( addresseeItem->addressee() );
00235 updateEntryView();
00236 slotSelectionAddresseeViewChanged();
00237 }
00238
00239 void DistributionListEditor::removeEntry()
00240 {
00241 DistributionList *list = mManager->list( mNameCombo->currentText() );
00242 if ( !list ) return;
00243
00244 EditEntryItem *entryItem =
00245 dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00246 if ( !entryItem ) return;
00247
00248 list->removeEntry( entryItem->addressee(), entryItem->email() );
00249 delete entryItem;
00250 }
00251
00252 void DistributionListEditor::changeEmail()
00253 {
00254 DistributionList *list = mManager->list( mNameCombo->currentText() );
00255 if ( !list ) return;
00256
00257 EditEntryItem *entryItem =
00258 dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00259 if ( !entryItem ) return;
00260
00261 QString email = EmailSelectDialog::getEmail( entryItem->addressee().emails(),
00262 entryItem->email(), this );
00263 list->removeEntry( entryItem->addressee(), entryItem->email() );
00264 list->insertEntry( entryItem->addressee(), email );
00265
00266 updateEntryView();
00267 }
00268
00269 void DistributionListEditor::updateEntryView()
00270 {
00271 DistributionList *list = mManager->list( mNameCombo->currentText() );
00272 if ( !list ) return;
00273
00274 mEntryView->clear();
00275 DistributionList::Entry::List entries = list->entries();
00276 DistributionList::Entry::List::ConstIterator it;
00277 for( it = entries.begin(); it != entries.end(); ++it ) {
00278 new EditEntryItem( mEntryView, (*it).addressee, (*it).email );
00279 }
00280 EditEntryItem *entryItem = dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00281 bool state = (entryItem != 0L);
00282
00283 changeEmailButton->setEnabled(state);
00284 removeEntryButton->setEnabled(state);
00285 }
00286
00287 void DistributionListEditor::updateAddresseeView()
00288 {
00289 mAddresseeView->clear();
00290
00291 AddressBook::Iterator it;
00292 for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00293 new AddresseeItem( mAddresseeView, *it );
00294 }
00295 }
00296
00297 void DistributionListEditor::updateNameCombo()
00298 {
00299 mNameCombo->insertStringList( mManager->listNames() );
00300
00301 updateEntryView();
00302 }
00303
00304 void DistributionListEditor::slotSelectionAddresseeViewChanged()
00305 {
00306 AddresseeItem *addresseeItem =
00307 dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00308 bool state = (addresseeItem != 0L);
00309 addEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
00310 }