summaryrefslogtreecommitdiffabout
path: root/kabc/distributionlistdialog.cpp
authorzautrix <zautrix>2004-06-26 19:01:18 (UTC)
committer zautrix <zautrix>2004-06-26 19:01:18 (UTC)
commitb9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff)
tree2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kabc/distributionlistdialog.cpp
downloadkdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2
Initial revision
Diffstat (limited to 'kabc/distributionlistdialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/distributionlistdialog.cpp396
1 files changed, 396 insertions, 0 deletions
diff --git a/kabc/distributionlistdialog.cpp b/kabc/distributionlistdialog.cpp
new file mode 100644
index 0000000..31d5944
--- a/dev/null
+++ b/kabc/distributionlistdialog.cpp
@@ -0,0 +1,396 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21#include <qlistview.h>
22#include <qlayout.h>
23#include <qlabel.h>
24#include <qpushbutton.h>
25#include <qcombobox.h>
26#include <klineeditdlg.h>
27#include <qbuttongroup.h>
28#include <qradiobutton.h>
29
30#include <klocale.h>
31#include <kdebug.h>
32#include <kmessagebox.h>
33
34#include "addressbook.h"
35#include "addresseedialog.h"
36#include "distributionlist.h"
37
38#include "distributionlistdialog.h"
39
40#ifndef KAB_EMBEDDED
41#include "distributionlistdialog.moc"
42#endif //KAB_EMBEDDED
43
44using namespace KABC;
45
46DistributionListDialog::DistributionListDialog( AddressBook *addressBook, QWidget *parent)
47 : KDialogBase( parent, "", true, i18n("Configure Distribution Lists"), Ok, Ok, true)
48{
49 mEditor = new DistributionListEditorWidget( addressBook, this );
50 setMainWidget( mEditor );
51
52 connect( this, SIGNAL( okClicked() ), mEditor, SLOT( save() ) );
53}
54
55DistributionListDialog::~DistributionListDialog()
56{
57}
58
59
60EmailSelector::EmailSelector( const QStringList &emails, const QString &current,
61 QWidget *parent ) :
62 KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
63 parent )
64{
65 QFrame *topFrame = plainPage();
66 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
67
68 mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
69 topFrame );
70 topLayout->addWidget( mButtonGroup );
71
72 QStringList::ConstIterator it;
73 for( it = emails.begin(); it != emails.end(); ++it ) {
74 QRadioButton *button = new QRadioButton( *it, mButtonGroup );
75 if ( (*it) == current ) {
76 button->setDown( true );
77 }
78 }
79}
80
81QString EmailSelector::selected()
82{
83 QButton *button = mButtonGroup->selected();
84 if ( button ) return button->text();
85 return QString::null;
86}
87
88QString EmailSelector::getEmail( const QStringList &emails, const QString &current,
89 QWidget *parent )
90{
91 EmailSelector *dlg = new EmailSelector( emails, current, parent );
92 dlg->exec();
93
94 QString result = dlg->selected();
95
96 delete dlg;
97
98 return result;
99}
100
101class EntryItem : public QListViewItem
102{
103 public:
104 EntryItem( QListView *parent, const Addressee &addressee,
105 const QString &email=QString::null ) :
106 QListViewItem( parent ),
107 mAddressee( addressee ),
108 mEmail( email )
109 {
110 setText( 0, addressee.realName() );
111 if( email.isEmpty() ) {
112 setText( 1, addressee.preferredEmail() );
113 setText( 2, i18n("Yes") );
114 } else {
115 setText( 1, email );
116 setText( 2, i18n("No") );
117 }
118 }
119
120 Addressee addressee() const
121 {
122 return mAddressee;
123 }
124
125 QString email() const
126 {
127 return mEmail;
128 }
129
130 private:
131 Addressee mAddressee;
132 QString mEmail;
133};
134
135DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook, QWidget *parent) :
136 QWidget( parent ),
137 mAddressBook( addressBook )
138{
139 kdDebug(5700) << "DistributionListEditor()" << endl;
140
141 QBoxLayout *topLayout = new QVBoxLayout( this );
142 topLayout->setSpacing( KDialog::spacingHint() );
143
144 QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
145
146 mNameCombo = new QComboBox( this );
147 nameLayout->addWidget( mNameCombo );
148 connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
149
150 mNewButton = new QPushButton( i18n("New List..."), this );
151 nameLayout->addWidget( mNewButton );
152 connect( mNewButton, SIGNAL( clicked() ), SLOT( newList() ) );
153
154 mEditButton = new QPushButton( i18n("Rename List..."), this );
155 nameLayout->addWidget( mEditButton );
156 connect( mEditButton, SIGNAL( clicked() ), SLOT( editList() ) );
157
158 mRemoveButton = new QPushButton( i18n("Remove List"), this );
159 nameLayout->addWidget( mRemoveButton );
160 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeList() ) );
161
162 QGridLayout *gridLayout = new QGridLayout( topLayout, 3, 3 );
163 gridLayout->setColStretch(1, 1);
164
165 QLabel *listLabel = new QLabel( i18n("Available addresses:"), this );
166 gridLayout->addWidget( listLabel, 0, 0 );
167
168 mListLabel = new QLabel( this );
169 gridLayout->addMultiCellWidget( mListLabel, 0, 0, 1, 2 );
170
171 mAddresseeView = new QListView( this );
172 mAddresseeView->addColumn( i18n("Name") );
173 mAddresseeView->addColumn( i18n("Preferred Email") );
174 mAddresseeView->setAllColumnsShowFocus( true );
175 gridLayout->addWidget( mAddresseeView, 1, 0 );
176 connect( mAddresseeView, SIGNAL( selectionChanged() ),
177 SLOT( slotSelectionAddresseeViewChanged() ) );
178 connect( mAddresseeView, SIGNAL( doubleClicked( QListViewItem * ) ),
179 SLOT( addEntry() ) );
180
181 mAddEntryButton = new QPushButton( i18n("Add Entry"), this );
182 mAddEntryButton->setEnabled(false);
183 gridLayout->addWidget( mAddEntryButton, 2, 0 );
184 connect( mAddEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
185
186 mEntryView = new QListView( this );
187 mEntryView->addColumn( i18n("Name") );
188 mEntryView->addColumn( i18n("Email") );
189 mEntryView->addColumn( i18n("Use Preferred") );
190 mEntryView->setEnabled(false);
191 mEntryView->setAllColumnsShowFocus( true );
192 gridLayout->addMultiCellWidget( mEntryView, 1, 1, 1, 2 );
193 connect( mEntryView, SIGNAL( selectionChanged() ),
194 SLOT( slotSelectionEntryViewChanged() ) );
195
196 mChangeEmailButton = new QPushButton( i18n("Change Email..."), this );
197 gridLayout->addWidget( mChangeEmailButton, 2, 1 );
198 connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
199
200 mRemoveEntryButton = new QPushButton( i18n("Remove Entry"), this );
201 gridLayout->addWidget( mRemoveEntryButton, 2, 2 );
202 connect( mRemoveEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
203
204 mManager = new DistributionListManager( mAddressBook );
205 mManager->load();
206
207 updateAddresseeView();
208 updateNameCombo();
209}
210
211DistributionListEditorWidget::~DistributionListEditorWidget()
212{
213 kdDebug(5700) << "~DistributionListEditor()" << endl;
214
215 delete mManager;
216}
217
218void DistributionListEditorWidget::save()
219{
220 mManager->save();
221}
222
223void DistributionListEditorWidget::slotSelectionEntryViewChanged()
224{
225 EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
226 bool state=entryItem;
227
228 mChangeEmailButton->setEnabled(state);
229 mRemoveEntryButton->setEnabled(state);
230}
231
232void DistributionListEditorWidget::newList()
233{
234 KLineEditDlg dlg(i18n("Please enter name:"), QString::null, this);
235 dlg.setCaption(i18n("New Distribution List"));
236 if (!dlg.exec()) return;
237
238 new DistributionList( mManager, dlg.text() );
239
240 mNameCombo->clear();
241 mNameCombo->insertStringList( mManager->listNames() );
242 mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
243
244 updateEntryView();
245 slotSelectionAddresseeViewChanged();
246}
247
248void DistributionListEditorWidget::editList()
249{
250 QString oldName = mNameCombo->currentText();
251
252 KLineEditDlg dlg(i18n("Please change name:"), oldName, this);
253 dlg.setCaption(i18n("Distribution List"));
254 if (!dlg.exec()) return;
255
256 DistributionList *list = mManager->list( oldName );
257 list->setName( dlg.text() );
258
259 mNameCombo->clear();
260 mNameCombo->insertStringList( mManager->listNames() );
261 mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
262
263 updateEntryView();
264 slotSelectionAddresseeViewChanged();
265}
266
267void DistributionListEditorWidget::removeList()
268{
269 int result = KMessageBox::warningContinueCancel( this,
270 i18n("Delete distribution list '%1'?") .arg( mNameCombo->currentText() ),
271 QString::null, i18n("Delete") );
272
273 if ( result != KMessageBox::Continue ) return;
274
275 delete mManager->list( mNameCombo->currentText() );
276 mNameCombo->removeItem( mNameCombo->currentItem() );
277
278 updateEntryView();
279 slotSelectionAddresseeViewChanged();
280}
281
282void DistributionListEditorWidget::addEntry()
283{
284 AddresseeItem *addresseeItem =
285 static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
286
287 if( !addresseeItem ) {
288 kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
289 return;
290 }
291
292 DistributionList *list = mManager->list( mNameCombo->currentText() );
293 if ( !list ) {
294 kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
295 return;
296 }
297
298 list->insertEntry( addresseeItem->addressee() );
299 updateEntryView();
300 slotSelectionAddresseeViewChanged();
301}
302
303void DistributionListEditorWidget::removeEntry()
304{
305 DistributionList *list = mManager->list( mNameCombo->currentText() );
306 if ( !list ) return;
307
308 EntryItem *entryItem =
309 static_cast<EntryItem *>( mEntryView->selectedItem() );
310 if ( !entryItem ) return;
311
312 list->removeEntry( entryItem->addressee(), entryItem->email() );
313 delete entryItem;
314}
315
316void DistributionListEditorWidget::changeEmail()
317{
318 DistributionList *list = mManager->list( mNameCombo->currentText() );
319 if ( !list ) return;
320
321 EntryItem *entryItem =
322 static_cast<EntryItem *>( mEntryView->selectedItem() );
323 if ( !entryItem ) return;
324
325 QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
326 entryItem->email(), this );
327 list->removeEntry( entryItem->addressee(), entryItem->email() );
328 list->insertEntry( entryItem->addressee(), email );
329
330 updateEntryView();
331}
332
333void DistributionListEditorWidget::updateEntryView()
334{
335 if ( mNameCombo->currentText().isEmpty() ) {
336 mListLabel->setText( i18n("Selected addressees:") );
337 } else {
338 mListLabel->setText( i18n("Selected addresses in '%1':")
339 .arg( mNameCombo->currentText() ) );
340 }
341
342 mEntryView->clear();
343
344 DistributionList *list = mManager->list( mNameCombo->currentText() );
345 if ( !list ) {
346 mEditButton->setEnabled(false);
347 mRemoveButton->setEnabled(false);
348 mChangeEmailButton->setEnabled(false);
349 mRemoveEntryButton->setEnabled(false);
350 mAddresseeView->setEnabled(false);
351 mEntryView->setEnabled(false);
352 return;
353 } else {
354 mEditButton->setEnabled(true);
355 mRemoveButton->setEnabled(true);
356 mAddresseeView->setEnabled(true);
357 mEntryView->setEnabled(true);
358 }
359
360 DistributionList::Entry::List entries = list->entries();
361 DistributionList::Entry::List::ConstIterator it;
362 for( it = entries.begin(); it != entries.end(); ++it ) {
363 new EntryItem( mEntryView, (*it).addressee, (*it).email );
364 }
365
366 EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
367 bool state=entryItem;
368
369 mChangeEmailButton->setEnabled(state);
370 mRemoveEntryButton->setEnabled(state);
371}
372
373void DistributionListEditorWidget::updateAddresseeView()
374{
375 mAddresseeView->clear();
376
377 AddressBook::Iterator it;
378 for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
379 new AddresseeItem( mAddresseeView, *it );
380 }
381}
382
383void DistributionListEditorWidget::updateNameCombo()
384{
385 mNameCombo->insertStringList( mManager->listNames() );
386
387 updateEntryView();
388}
389
390void DistributionListEditorWidget::slotSelectionAddresseeViewChanged()
391{
392 AddresseeItem *addresseeItem =
393 static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
394 bool state=addresseeItem;
395 mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
396}