Diffstat (limited to 'kaddressbook/features/distributionlistwidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/features/distributionlistwidget.cpp | 501 |
1 files changed, 501 insertions, 0 deletions
diff --git a/kaddressbook/features/distributionlistwidget.cpp b/kaddressbook/features/distributionlistwidget.cpp new file mode 100644 index 0000000..bfcb121 --- a/dev/null +++ b/kaddressbook/features/distributionlistwidget.cpp | |||
@@ -0,0 +1,501 @@ | |||
1 | /* | ||
2 | This file is part of KAddressBook. | ||
3 | Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | |||
19 | As a special exception, permission is given to link this program | ||
20 | with any edition of Qt, and distribute the resulting executable, | ||
21 | without including the source code for Qt in the source distribution. | ||
22 | */ | ||
23 | |||
24 | #include <qbuttongroup.h> | ||
25 | #include <qcombobox.h> | ||
26 | #include <qlabel.h> | ||
27 | #include <qlayout.h> | ||
28 | #include <qlistview.h> | ||
29 | #include <qpushbutton.h> | ||
30 | #include <qradiobutton.h> | ||
31 | |||
32 | #ifndef KAB_EMBEDDED | ||
33 | #include <kaccelmanager.h> | ||
34 | #endif //KAB_EMBEDDED | ||
35 | |||
36 | |||
37 | #include <kdebug.h> | ||
38 | #include <klineeditdlg.h> | ||
39 | #include <klocale.h> | ||
40 | #include <kglobal.h> | ||
41 | #include <kmessagebox.h> | ||
42 | |||
43 | #include <kabc/addressbook.h> | ||
44 | #include <kabc/addresseedialog.h> | ||
45 | #include <kabc/distributionlist.h> | ||
46 | #include <kabc/vcardconverter.h> | ||
47 | |||
48 | #ifndef KAB_EMBEDDED | ||
49 | #include <libkdepim/kvcarddrag.h> | ||
50 | #endif //KAB_EMBEDDED | ||
51 | |||
52 | #include "kabcore.h" | ||
53 | |||
54 | #include "distributionlistwidget.h" | ||
55 | |||
56 | #ifndef KAB_EMBEDDED | ||
57 | |||
58 | class DistributionListFactory : public ExtensionFactory | ||
59 | { | ||
60 | public: | ||
61 | ExtensionWidget *extension( KABCore *core, QWidget *parent, const char *name ) | ||
62 | { | ||
63 | return new DistributionListWidget( core, parent, name ); | ||
64 | } | ||
65 | |||
66 | QString identifier() const | ||
67 | { | ||
68 | return "distribution_list_editor"; | ||
69 | } | ||
70 | }; | ||
71 | |||
72 | extern "C" { | ||
73 | void *init_libkaddrbk_distributionlist() | ||
74 | { | ||
75 | return ( new DistributionListFactory ); | ||
76 | } | ||
77 | } | ||
78 | #endif //KAB_EMBEDDED | ||
79 | |||
80 | class ContactItem : public QListViewItem | ||
81 | { | ||
82 | public: | ||
83 | ContactItem( DistributionListView *parent, const KABC::Addressee &addressee, | ||
84 | const QString &email = QString::null ) : | ||
85 | QListViewItem( parent ), | ||
86 | mAddressee( addressee ), | ||
87 | mEmail( email ) | ||
88 | { | ||
89 | setText( 0, addressee.realName() ); | ||
90 | if( email.isEmpty() ) { | ||
91 | setText( 1, addressee.preferredEmail() ); | ||
92 | setText( 2, i18n( "Yes" ) ); | ||
93 | } else { | ||
94 | setText( 1, email ); | ||
95 | setText( 2, i18n( "No" ) ); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | KABC::Addressee addressee() const | ||
100 | { | ||
101 | return mAddressee; | ||
102 | } | ||
103 | |||
104 | QString email() const | ||
105 | { | ||
106 | return mEmail; | ||
107 | } | ||
108 | |||
109 | protected: | ||
110 | bool acceptDrop( const QMimeSource* ) | ||
111 | { | ||
112 | return true; | ||
113 | } | ||
114 | |||
115 | private: | ||
116 | KABC::Addressee mAddressee; | ||
117 | QString mEmail; | ||
118 | }; | ||
119 | |||
120 | DistributionListWidget::DistributionListWidget( KABCore *core, QWidget *parent, | ||
121 | const char *name ) | ||
122 | : ExtensionWidget( core, parent, name ), mManager( 0 ) | ||
123 | { | ||
124 | QGridLayout *topLayout = new QGridLayout( this, 3, 4, KDialog::marginHint(), | ||
125 | KDialog::spacingHint() ); | ||
126 | |||
127 | if (KGlobal::getOrientation() == KGlobal::Portrait) | ||
128 | { | ||
129 | mCreateListButton = new QPushButton( i18n( "New List" ), this ); | ||
130 | mEditListButton = new QPushButton( i18n( "Ren List" ), this ); | ||
131 | mRemoveListButton = new QPushButton( i18n( "Del List" ), this ); | ||
132 | mAddContactButton = new QPushButton( i18n( "Add Cont." ), this ); | ||
133 | mChangeEmailButton = new QPushButton( i18n( "Chge Email" ), this ); | ||
134 | mRemoveContactButton = new QPushButton( i18n( "Del Cont." ), this ); | ||
135 | } | ||
136 | else | ||
137 | { | ||
138 | mCreateListButton = new QPushButton( i18n( "New List..." ), this ); | ||
139 | mEditListButton = new QPushButton( i18n( "Rename List..." ), this ); | ||
140 | mRemoveListButton = new QPushButton( i18n( "Remove List" ), this ); | ||
141 | mAddContactButton = new QPushButton( i18n( "Add Contact" ), this ); | ||
142 | mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this ); | ||
143 | mRemoveContactButton = new QPushButton( i18n( "Remove Contact" ), this ); | ||
144 | } | ||
145 | mNameCombo = new QComboBox( this ); | ||
146 | topLayout->addWidget( mNameCombo, 0, 0 ); | ||
147 | connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateContactView() ) ); | ||
148 | |||
149 | topLayout->addWidget( mCreateListButton, 0, 1 ); | ||
150 | connect( mCreateListButton, SIGNAL( clicked() ), SLOT( createList() ) ); | ||
151 | |||
152 | topLayout->addWidget( mEditListButton, 0, 2 ); | ||
153 | connect( mEditListButton, SIGNAL( clicked() ), SLOT( editList() ) ); | ||
154 | |||
155 | topLayout->addWidget( mRemoveListButton, 0, 3 ); | ||
156 | connect( mRemoveListButton, SIGNAL( clicked() ), SLOT( removeList() ) ); | ||
157 | |||
158 | mContactView = new DistributionListView( this ); | ||
159 | mContactView->addColumn( i18n( "Name" ) ); | ||
160 | mContactView->addColumn( i18n( "Email" ) ); | ||
161 | mContactView->addColumn( i18n( "Use Preferred" ) ); | ||
162 | mContactView->setEnabled( false ); | ||
163 | mContactView->setAllColumnsShowFocus( true ); | ||
164 | mContactView->setMinimumHeight( 30 ); | ||
165 | |||
166 | topLayout->addMultiCellWidget( mContactView, 1, 1, 0, 3 ); | ||
167 | connect( mContactView, SIGNAL( selectionChanged() ), | ||
168 | SLOT( selectionContactViewChanged() ) ); | ||
169 | connect( mContactView, SIGNAL( dropped( QDropEvent*, QListViewItem* ) ), | ||
170 | SLOT( dropped( QDropEvent*, QListViewItem* ) ) ); | ||
171 | |||
172 | mAddContactButton->setEnabled( false ); | ||
173 | topLayout->addWidget( mAddContactButton, 2, 0 ); | ||
174 | connect( mAddContactButton, SIGNAL( clicked() ), SLOT( addContact() ) ); | ||
175 | |||
176 | topLayout->addWidget( mChangeEmailButton, 2, 2 ); | ||
177 | connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) ); | ||
178 | |||
179 | topLayout->addWidget( mRemoveContactButton, 2, 3 ); | ||
180 | connect( mRemoveContactButton, SIGNAL( clicked() ), SLOT( removeContact() ) ); | ||
181 | |||
182 | mManager = new KABC::DistributionListManager( core->addressBook() ); | ||
183 | mManager->load(); | ||
184 | |||
185 | updateNameCombo(); | ||
186 | |||
187 | #ifdef KAB_EMBEDDED | ||
188 | // if (KGlobal::getOrientation() == KGlobal::Portrait) | ||
189 | // parent->setMaximumSize( KGlobal::getDesktopWidth() , 150); | ||
190 | #endif //KAB_EMBEDDED | ||
191 | |||
192 | #ifndef KAB_EMBEDDED | ||
193 | KAcceleratorManager::manage( this ); | ||
194 | #endif //KAB_EMBEDDED | ||
195 | } | ||
196 | |||
197 | DistributionListWidget::~DistributionListWidget() | ||
198 | { | ||
199 | delete mManager; | ||
200 | } | ||
201 | |||
202 | void DistributionListWidget::save() | ||
203 | { | ||
204 | qDebug("DistributionListWidget::save"); | ||
205 | mManager->save(); | ||
206 | } | ||
207 | |||
208 | void DistributionListWidget::selectionContactViewChanged() | ||
209 | { | ||
210 | ContactItem *contactItem = | ||
211 | static_cast<ContactItem *>( mContactView->selectedItem() ); | ||
212 | bool state = contactItem; | ||
213 | |||
214 | mChangeEmailButton->setEnabled( state ); | ||
215 | mRemoveContactButton->setEnabled( state ); | ||
216 | } | ||
217 | |||
218 | void DistributionListWidget::createList() | ||
219 | { | ||
220 | KLineEditDlg dlg( i18n( "Please enter name:" ), QString::null, this ); | ||
221 | #ifdef KAB_EMBEDDED | ||
222 | dlg.setFixedSize(200, 50); | ||
223 | #endif //KAB_EMBEDDED | ||
224 | dlg.setCaption( i18n( "New Distribution List" ) ); | ||
225 | if ( !dlg.exec() ) | ||
226 | return; | ||
227 | |||
228 | new KABC::DistributionList( mManager, dlg.text() ); | ||
229 | |||
230 | mNameCombo->clear(); | ||
231 | mNameCombo->insertStringList( mManager->listNames() ); | ||
232 | mNameCombo->setCurrentItem( mNameCombo->count() - 1 ); | ||
233 | |||
234 | updateContactView(); | ||
235 | |||
236 | changed(); | ||
237 | } | ||
238 | |||
239 | void DistributionListWidget::editList() | ||
240 | { | ||
241 | QString oldName = mNameCombo->currentText(); | ||
242 | |||
243 | KLineEditDlg dlg( i18n( "Please change name:" ), oldName, this ); | ||
244 | #ifdef KAB_EMBEDDED | ||
245 | dlg.setFixedSize(200, 50); | ||
246 | #endif //KAB_EMBEDDED | ||
247 | dlg.setCaption( i18n("Distribution List") ); | ||
248 | if ( !dlg.exec() ) | ||
249 | return; | ||
250 | |||
251 | KABC::DistributionList *list = mManager->list( oldName ); | ||
252 | list->setName( dlg.text() ); | ||
253 | |||
254 | mNameCombo->clear(); | ||
255 | mNameCombo->insertStringList( mManager->listNames() ); | ||
256 | mNameCombo->setCurrentItem( mNameCombo->count() - 1 ); | ||
257 | |||
258 | updateContactView(); | ||
259 | |||
260 | changed(); | ||
261 | } | ||
262 | |||
263 | void DistributionListWidget::removeList() | ||
264 | { | ||
265 | int result = KMessageBox::warningContinueCancel( this, | ||
266 | i18n( "<qt>Delete distribution list <b>%1</b>?</qt>" ) .arg( mNameCombo->currentText() ), | ||
267 | QString::null, i18n( "Delete" ) ); | ||
268 | |||
269 | if ( result != KMessageBox::Continue ) | ||
270 | return; | ||
271 | |||
272 | delete mManager->list( mNameCombo->currentText() ); | ||
273 | mNameCombo->removeItem( mNameCombo->currentItem() ); | ||
274 | |||
275 | updateContactView(); | ||
276 | |||
277 | changed(); | ||
278 | } | ||
279 | |||
280 | void DistributionListWidget::addContact() | ||
281 | { | ||
282 | KABC::DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
283 | if ( !list ) | ||
284 | return; | ||
285 | |||
286 | KABC::Addressee::List addrList = selectedContacts(); | ||
287 | KABC::Addressee::List::Iterator it; | ||
288 | for ( it = addrList.begin(); it != addrList.end(); ++it ) | ||
289 | list->insertEntry( *it ); | ||
290 | |||
291 | updateContactView(); | ||
292 | |||
293 | changed(); | ||
294 | } | ||
295 | |||
296 | void DistributionListWidget::removeContact() | ||
297 | { | ||
298 | KABC::DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
299 | if ( !list ) | ||
300 | return; | ||
301 | |||
302 | ContactItem *contactItem = | ||
303 | static_cast<ContactItem *>( mContactView->selectedItem() ); | ||
304 | if ( !contactItem ) | ||
305 | return; | ||
306 | |||
307 | list->removeEntry( contactItem->addressee(), contactItem->email() ); | ||
308 | delete contactItem; | ||
309 | |||
310 | changed(); | ||
311 | } | ||
312 | |||
313 | void DistributionListWidget::changeEmail() | ||
314 | { | ||
315 | KABC::DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
316 | if ( !list ) | ||
317 | return; | ||
318 | |||
319 | ContactItem *contactItem = | ||
320 | static_cast<ContactItem *>( mContactView->selectedItem() ); | ||
321 | if ( !contactItem ) | ||
322 | return; | ||
323 | |||
324 | QString email = EmailSelector::getEmail( contactItem->addressee().emails(), | ||
325 | contactItem->email(), this ); | ||
326 | list->removeEntry( contactItem->addressee(), contactItem->email() ); | ||
327 | list->insertEntry( contactItem->addressee(), email ); | ||
328 | |||
329 | updateContactView(); | ||
330 | |||
331 | changed(); | ||
332 | } | ||
333 | |||
334 | void DistributionListWidget::updateContactView() | ||
335 | { | ||
336 | mContactView->clear(); | ||
337 | |||
338 | KABC::DistributionList *list = mManager->list( mNameCombo->currentText() ); | ||
339 | if ( !list ) { | ||
340 | mEditListButton->setEnabled( false ); | ||
341 | mRemoveListButton->setEnabled( false ); | ||
342 | mChangeEmailButton->setEnabled( false ); | ||
343 | mRemoveContactButton->setEnabled( false ); | ||
344 | mContactView->setEnabled( false ); | ||
345 | return; | ||
346 | } else { | ||
347 | mEditListButton->setEnabled( true ); | ||
348 | mRemoveListButton->setEnabled( true ); | ||
349 | mContactView->setEnabled( true ); | ||
350 | } | ||
351 | |||
352 | KABC::DistributionList::Entry::List entries = list->entries(); | ||
353 | KABC::DistributionList::Entry::List::ConstIterator it; | ||
354 | for( it = entries.begin(); it != entries.end(); ++it ) | ||
355 | new ContactItem( mContactView, (*it).addressee, (*it).email ); | ||
356 | |||
357 | ContactItem *contactItem = | ||
358 | static_cast<ContactItem *>( mContactView->selectedItem() ); | ||
359 | bool state = contactItem; | ||
360 | |||
361 | mChangeEmailButton->setEnabled( state ); | ||
362 | mRemoveContactButton->setEnabled( state ); | ||
363 | } | ||
364 | |||
365 | void DistributionListWidget::updateNameCombo() | ||
366 | { | ||
367 | mNameCombo->insertStringList( mManager->listNames() ); | ||
368 | |||
369 | updateContactView(); | ||
370 | } | ||
371 | |||
372 | void DistributionListWidget::dropEvent( QDropEvent *e ) | ||
373 | { | ||
374 | KABC::DistributionList *distributionList = mManager->list( mNameCombo->currentText() ); | ||
375 | if ( !distributionList ) | ||
376 | return; | ||
377 | |||
378 | QString vcards; | ||
379 | #ifndef KAB_EMBEDDED | ||
380 | if ( KVCardDrag::decode( e, vcards ) ) { | ||
381 | #endif //KAB_EMBEDDED | ||
382 | QStringList list = QStringList::split( "\r\n\r\n", vcards ); | ||
383 | QStringList::Iterator it; | ||
384 | KABC::VCardConverter converter; | ||
385 | for ( it = list.begin(); it != list.end(); ++it ) { | ||
386 | KABC::Addressee addr; | ||
387 | if ( converter.vCardToAddressee( (*it).stripWhiteSpace(), addr ) ) | ||
388 | distributionList->insertEntry( addr ); | ||
389 | } | ||
390 | |||
391 | changed(); | ||
392 | updateContactView(); | ||
393 | #ifndef KAB_EMBEDDED | ||
394 | } | ||
395 | #endif //KAB_EMBEDDED | ||
396 | } | ||
397 | |||
398 | void DistributionListWidget::contactsSelectionChanged() | ||
399 | { | ||
400 | mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 ); | ||
401 | } | ||
402 | |||
403 | QString DistributionListWidget::title() const | ||
404 | { | ||
405 | return i18n( "Distribution List Editor" ); | ||
406 | } | ||
407 | |||
408 | QString DistributionListWidget::identifier() const | ||
409 | { | ||
410 | return "distribution_list_editor"; | ||
411 | } | ||
412 | |||
413 | void DistributionListWidget::dropped( QDropEvent *e, QListViewItem* ) | ||
414 | { | ||
415 | dropEvent( e ); | ||
416 | } | ||
417 | |||
418 | void DistributionListWidget::changed() | ||
419 | { | ||
420 | save(); | ||
421 | } | ||
422 | |||
423 | |||
424 | DistributionListView::DistributionListView( QWidget *parent, const char* name ) | ||
425 | : KListView( parent, name ) | ||
426 | { | ||
427 | setDragEnabled( true ); | ||
428 | setAcceptDrops( true ); | ||
429 | setAllColumnsShowFocus( true ); | ||
430 | } | ||
431 | |||
432 | void DistributionListView::dragEnterEvent( QDragEnterEvent* e ) | ||
433 | { | ||
434 | #ifndef KAB_EMBEDDED | ||
435 | bool canDecode = QTextDrag::canDecode( e ); | ||
436 | e->accept( canDecode ); | ||
437 | #endif //KAB_EMBEDDED | ||
438 | } | ||
439 | |||
440 | void DistributionListView::viewportDragMoveEvent( QDragMoveEvent *e ) | ||
441 | { | ||
442 | #ifndef KAB_EMBEDDED | ||
443 | bool canDecode = QTextDrag::canDecode( e ); | ||
444 | e->accept( canDecode ); | ||
445 | #endif //KAB_EMBEDDED | ||
446 | } | ||
447 | |||
448 | void DistributionListView::viewportDropEvent( QDropEvent *e ) | ||
449 | { | ||
450 | emit dropped( e, 0 ); | ||
451 | } | ||
452 | |||
453 | void DistributionListView::dropEvent( QDropEvent *e ) | ||
454 | { | ||
455 | emit dropped( e, 0 ); | ||
456 | } | ||
457 | |||
458 | |||
459 | EmailSelector::EmailSelector( const QStringList &emails, | ||
460 | const QString ¤t, QWidget *parent ) | ||
461 | : KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok, | ||
462 | parent ) | ||
463 | { | ||
464 | QFrame *topFrame = plainPage(); | ||
465 | QBoxLayout *topLayout = new QVBoxLayout( topFrame ); | ||
466 | |||
467 | mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"), | ||
468 | topFrame ); | ||
469 | topLayout->addWidget( mButtonGroup ); | ||
470 | |||
471 | QStringList::ConstIterator it; | ||
472 | for( it = emails.begin(); it != emails.end(); ++it ) { | ||
473 | QRadioButton *button = new QRadioButton( *it, mButtonGroup ); | ||
474 | if ( (*it) == current ) { | ||
475 | button->setDown( true ); | ||
476 | } | ||
477 | } | ||
478 | } | ||
479 | |||
480 | QString EmailSelector::selected() | ||
481 | { | ||
482 | QButton *button = mButtonGroup->selected(); | ||
483 | if ( button ) | ||
484 | return button->text(); | ||
485 | |||
486 | return QString::null; | ||
487 | } | ||
488 | |||
489 | QString EmailSelector::getEmail( const QStringList &emails, | ||
490 | const QString ¤t, QWidget *parent ) | ||
491 | { | ||
492 | EmailSelector dlg( emails, current, parent ); | ||
493 | dlg.exec(); | ||
494 | |||
495 | return dlg.selected(); | ||
496 | } | ||
497 | |||
498 | |||
499 | #ifndef KAB_EMBEDDED | ||
500 | #include "distributionlistwidget.moc" | ||
501 | #endif //KAB_EMBEDDED | ||