summaryrefslogtreecommitdiffabout
path: root/kaddressbook/features
authorzautrix <zautrix>2004-06-26 19:01:18 (UTC)
committer zautrix <zautrix>2004-06-26 19:01:18 (UTC)
commitb9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff)
tree2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kaddressbook/features
downloadkdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2
Initial revision
Diffstat (limited to 'kaddressbook/features') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/features/distributionlistwidget.cpp501
-rw-r--r--kaddressbook/features/distributionlistwidget.h143
-rw-r--r--kaddressbook/features/mergewidget.cpp374
-rw-r--r--kaddressbook/features/mergewidget.h87
4 files changed, 1105 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
58class 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
72extern "C" {
73 void *init_libkaddrbk_distributionlist()
74 {
75 return ( new DistributionListFactory );
76 }
77}
78#endif //KAB_EMBEDDED
79
80class 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
120DistributionListWidget::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
197DistributionListWidget::~DistributionListWidget()
198{
199 delete mManager;
200}
201
202void DistributionListWidget::save()
203{
204qDebug("DistributionListWidget::save");
205 mManager->save();
206}
207
208void 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
218void 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
239void 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
263void 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
280void 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
296void 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
313void 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
334void 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
365void DistributionListWidget::updateNameCombo()
366{
367 mNameCombo->insertStringList( mManager->listNames() );
368
369 updateContactView();
370}
371
372void 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
398void DistributionListWidget::contactsSelectionChanged()
399{
400 mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 );
401}
402
403QString DistributionListWidget::title() const
404{
405 return i18n( "Distribution List Editor" );
406}
407
408QString DistributionListWidget::identifier() const
409{
410 return "distribution_list_editor";
411}
412
413void DistributionListWidget::dropped( QDropEvent *e, QListViewItem* )
414{
415 dropEvent( e );
416}
417
418void DistributionListWidget::changed()
419{
420 save();
421}
422
423
424DistributionListView::DistributionListView( QWidget *parent, const char* name )
425 : KListView( parent, name )
426{
427 setDragEnabled( true );
428 setAcceptDrops( true );
429 setAllColumnsShowFocus( true );
430}
431
432void 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
440void 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
448void DistributionListView::viewportDropEvent( QDropEvent *e )
449{
450 emit dropped( e, 0 );
451}
452
453void DistributionListView::dropEvent( QDropEvent *e )
454{
455 emit dropped( e, 0 );
456}
457
458
459EmailSelector::EmailSelector( const QStringList &emails,
460 const QString &current, 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
480QString EmailSelector::selected()
481{
482 QButton *button = mButtonGroup->selected();
483 if ( button )
484 return button->text();
485
486 return QString::null;
487}
488
489QString EmailSelector::getEmail( const QStringList &emails,
490 const QString &current, 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
diff --git a/kaddressbook/features/distributionlistwidget.h b/kaddressbook/features/distributionlistwidget.h
new file mode 100644
index 0000000..82bac3d
--- a/dev/null
+++ b/kaddressbook/features/distributionlistwidget.h
@@ -0,0 +1,143 @@
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#ifndef DISTRIBUTIONLISTWIDGET_H
25#define DISTRIBUTIONLISTWIDGET_H
26
27#include <kdialogbase.h>
28#include <klistview.h>
29
30#include "extensionwidget.h"
31
32class QButtonGroup;
33class QComboBox;
34class QLabel;
35class QListView;
36
37class DistributionListView;
38class KABCore;
39
40namespace KABC {
41class AddressBook;
42class DistributionListManager;
43}
44
45class DistributionListWidget : public ExtensionWidget
46{
47 Q_OBJECT
48
49 public:
50 DistributionListWidget( KABCore*, QWidget *parent, const char *name = 0 );
51 virtual ~DistributionListWidget();
52
53 void contactsSelectionChanged();
54
55 QString title() const;
56 QString identifier() const;
57
58 public slots:
59 void save();
60 void dropped( QDropEvent*, QListViewItem* );
61
62 private slots:
63 void createList();
64 void editList();
65 void removeList();
66 void addContact();
67 void removeContact();
68 void changeEmail();
69 void updateNameCombo();
70 void updateContactView();
71 void selectionContactViewChanged();
72 void changed();
73
74 protected:
75 void dropEvent( QDropEvent* );
76
77 private:
78 QComboBox *mNameCombo;
79 QLabel *mListLabel;
80 DistributionListView *mContactView;
81
82 KABC::DistributionListManager *mManager;
83 QPushButton *mCreateListButton;
84 QPushButton *mEditListButton;
85 QPushButton *mRemoveListButton;
86 QPushButton *mChangeEmailButton;
87 QPushButton *mAddContactButton;
88 QPushButton *mRemoveContactButton;
89};
90
91/**
92 @short Helper class
93*/
94class DistributionListView : public KListView
95{
96 Q_OBJECT
97
98 public:
99 DistributionListView( QWidget *parent, const char* name = 0 );
100
101 protected:
102 void dragEnterEvent( QDragEnterEvent *e );
103 void dropEvent( QDropEvent *e );
104 void viewportDragMoveEvent( QDragMoveEvent *e );
105 void viewportDropEvent( QDropEvent *e );
106};
107
108/**
109 @short Helper class
110*/
111class EmailSelector : public KDialogBase
112{
113 public:
114 EmailSelector( const QStringList &emails, const QString &current,
115 QWidget *parent );
116
117 QString selected();
118
119 static QString getEmail( const QStringList &emails, const QString &current,
120 QWidget *parent );
121
122 private:
123 QButtonGroup *mButtonGroup;
124};
125
126
127#ifdef KAB_EMBEDDED
128class DistributionListFactory : public ExtensionFactory
129{
130 public:
131 ExtensionWidget *extension( KABCore *core, QWidget *parent, const char *name )
132 {
133 return new DistributionListWidget( core, parent, name );
134 }
135
136 QString identifier() const
137 {
138 return "distribution_list_editor";
139 }
140};
141#endif //KAB_EMBEDDED
142
143#endif
diff --git a/kaddressbook/features/mergewidget.cpp b/kaddressbook/features/mergewidget.cpp
new file mode 100644
index 0000000..2476e42
--- a/dev/null
+++ b/kaddressbook/features/mergewidget.cpp
@@ -0,0 +1,374 @@
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2003 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 <qlayout.h>
25#include <qpushbutton.h>
26
27#ifndef KAB_EMBEDDED
28#include <kaccelmanager.h>
29#endif //KAB_EMBEDDED
30
31#include <kdebug.h>
32#include <klistview.h>
33#include <klocale.h>
34#include <kglobal.h>
35#include <kmessagebox.h>
36
37#include <kabc/addressbook.h>
38
39#include "kabcore.h"
40
41#include "mergewidget.h"
42
43#ifndef KAB_EMBEDDED
44class MergeFactory : public ExtensionFactory
45{
46 public:
47 ExtensionWidget *extension( KABCore *core, QWidget *parent, const char *name )
48 {
49 return new MergeWidget( core, parent, name );
50 }
51
52 QString identifier() const
53 {
54 return "merge";
55 }
56};
57
58extern "C" {
59 void *init_libkaddrbk_merge()
60 {
61 return ( new MergeFactory );
62 }
63}
64#endif //KAB_EMBEDDED
65
66class ContactItem : public QListViewItem
67{
68 public:
69 ContactItem( KListView *parent, const KABC::Addressee &addressee )
70 : QListViewItem( parent ), mAddressee( addressee )
71 {
72 KABC::Field::List fieldList = KABC::Field::defaultFields();
73 KABC::Field::List::ConstIterator it;
74
75 int i = 0;
76 for ( it = fieldList.begin(); it != fieldList.end(); ++it )
77 setText( i++, (*it)->value( mAddressee ) );
78 }
79
80 KABC::Addressee addressee() const
81 {
82 return mAddressee;
83 }
84
85 private:
86 KABC::Addressee mAddressee;
87};
88
89MergeWidget::MergeWidget( KABCore *core, QWidget *parent, const char *name )
90 : ExtensionWidget( core, parent, name ), mBlockUpdate( false )
91{
92#ifdef KAB_EMBEDDED
93 if (KGlobal::getOrientation() == KGlobal::Portrait)
94 parent->setMaximumSize( KGlobal::getDesktopWidth() , 180);
95#endif //KAB_EMBEDDED
96
97 QGridLayout *topLayout = new QGridLayout( this, 3, 2, KDialog::marginHint(),
98 KDialog::spacingHint() );
99
100 mContactView = new KListView( this );
101 KABC::Field::List fieldList = KABC::Field::defaultFields();
102 KABC::Field::List::ConstIterator it;
103
104 for ( it = fieldList.begin(); it != fieldList.end(); ++it )
105 mContactView->addColumn( (*it)->label() );
106
107 mContactView->setEnabled( false );
108 mContactView->setAllColumnsShowFocus( true );
109 topLayout->addMultiCellWidget( mContactView, 0, 2, 0, 0 );
110
111 connect( mContactView, SIGNAL( selectionChanged() ),
112 SLOT( selectionContactViewChanged() ) );
113
114 mMergeAndRemoveButton = new QPushButton( i18n( "Merge and Remove" ), this );
115 mMergeAndRemoveButton->setEnabled( false );
116 topLayout->addWidget( mMergeAndRemoveButton, 0, 1 );
117 connect( mMergeAndRemoveButton, SIGNAL( clicked() ), SLOT( mergeAndRemove() ) );
118
119 mMergeButton = new QPushButton( i18n( "Merge" ), this );
120 mMergeButton->setEnabled( false );
121 topLayout->addWidget( mMergeButton, 1, 1 );
122 connect( mMergeButton, SIGNAL( clicked() ), SLOT( merge() ) );
123
124#ifndef KAB_EMBEDDED
125 KAcceleratorManager::manage( this );
126#endif //KAB_EMBEDDED
127}
128
129MergeWidget::~MergeWidget()
130{
131}
132
133void MergeWidget::selectionContactViewChanged()
134{
135#ifndef KAB_EMBEDDED
136 ContactItem *contactItem =
137 dynamic_cast<ContactItem*>( mContactView->selectedItem() );
138#else //KAB_EMBEDDED
139 ContactItem *contactItem =(ContactItem*)( mContactView->selectedItem() );
140#endif //KAB_EMBEDDED
141
142 bool state = (contactItem != 0);
143
144 mMergeAndRemoveButton->setEnabled( state );
145 mMergeButton->setEnabled( state );
146}
147
148void MergeWidget::contactsSelectionChanged()
149{
150 if ( mBlockUpdate )
151 return;
152
153 if ( !contactsSelected() ) {
154 mContactView->setEnabled( false );
155 mContactView->clear();
156 mMergeAndRemoveButton->setEnabled( false );
157 mMergeButton->setEnabled( false );
158 } else {
159 KABC::Addressee::List list = selectedContacts();
160 if ( list.count() > 1 ) {
161 mContactView->setEnabled( false );
162 mContactView->clear();
163 mMergeAndRemoveButton->setEnabled( false );
164 mMergeButton->setEnabled( false );
165 return;
166 } else {
167 mContactView->setEnabled( true );
168 mMasterAddressee = list[ 0 ];
169 updateView();
170 }
171 }
172}
173
174void MergeWidget::updateView()
175{
176 mContactView->clear();
177
178 KABC::AddressBook::Iterator it;
179 KABC::AddressBook *ab = core()->addressBook();
180 if ( !ab )
181 return;
182
183 for ( it = ab->begin(); it != ab->end(); ++it )
184 if ( (*it).uid() != mMasterAddressee.uid() )
185 new ContactItem( mContactView, *it );
186}
187
188QString MergeWidget::title() const
189{
190 return i18n( "Merge Contacts Editor" );
191}
192
193QString MergeWidget::identifier() const
194{
195 return "merge";
196}
197
198void MergeWidget::mergeAndRemove()
199{
200#ifndef KAB_EMBEDDED
201 ContactItem *item = dynamic_cast<ContactItem*>( mContactView->currentItem() );
202#else //KAB_EMBEDDED
203 ContactItem *item = (ContactItem*)( mContactView->currentItem() );
204#endif //KAB_EMBEDDED
205 if ( !item )
206 return;
207
208 QString oldUID = item->addressee().uid();
209
210 doMerge( item->addressee() );
211
212 KABC::Addressee::List retval;
213 retval << mMasterAddressee;
214 emit modified( retval );
215
216 mBlockUpdate = true;
217 core()->deleteContacts( oldUID );
218 core()->setContactSelected( mMasterAddressee.uid() );
219 mBlockUpdate = false;
220
221 updateView();
222}
223
224void MergeWidget::merge()
225{
226#ifndef KAB_EMBEDDED
227 ContactItem *item = dynamic_cast<ContactItem*>( mContactView->currentItem() );
228#else //KAB_EMBEDDED
229 ContactItem *item = (ContactItem*)( mContactView->currentItem() );
230#endif //KAB_EMBEDDED
231 if ( !item )
232 return;
233
234 doMerge( item->addressee() );
235
236 KABC::Addressee::List retval;
237 retval << mMasterAddressee;
238 emit modified( retval );
239
240 mBlockUpdate = true;
241 core()->setContactSelected( mMasterAddressee.uid() );
242 mBlockUpdate = false;
243
244 updateView();
245}
246
247void MergeWidget::doMerge( const KABC::Addressee &addr )
248{
249 // ADR + LABEL
250 KABC::Address::List addresses = addr.addresses();
251 KABC::Address::List masterAddresses = mMasterAddressee.addresses();
252 KABC::Address::List::Iterator addrIt ;
253 for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) {
254 if ( !masterAddresses.contains( *addrIt ) )
255 mMasterAddressee.insertAddress( *addrIt );
256 }
257
258 if ( mMasterAddressee.birthday().isNull() && !addr.birthday().isNull() )
259 mMasterAddressee.setBirthday( addr.birthday() );
260
261
262 // CATEGORIES
263 QStringList::Iterator it;
264 QStringList categories = addr.categories();
265 QStringList masterCategories = mMasterAddressee.categories();
266 QStringList newCategories( masterCategories );
267 for ( it = categories.begin(); it != categories.end(); ++it )
268 if ( !masterCategories.contains( *it ) )
269 newCategories.append( *it );
270 mMasterAddressee.setCategories( newCategories );
271
272 // CLASS
273 if ( !mMasterAddressee.secrecy().isValid() && addr.secrecy().isValid() )
274 mMasterAddressee.setSecrecy( addr.secrecy() );
275
276 // EMAIL
277 QStringList emails = addr.emails();
278 QStringList masterEmails = mMasterAddressee.emails();
279 for ( it = emails.begin(); it != emails.end(); ++it )
280 if ( !masterEmails.contains( *it ) )
281 mMasterAddressee.insertEmail( *it, false );
282
283 // FN
284 if ( mMasterAddressee.formattedName().isEmpty() && !addr.formattedName().isEmpty() )
285 mMasterAddressee.setFormattedName( addr.formattedName() );
286
287 // GEO
288 if ( !mMasterAddressee.geo().isValid() && addr.geo().isValid() )
289 mMasterAddressee.setGeo( addr.geo() );
290
291/*
292 // KEY
293 // LOGO
294*/
295
296 // MAILER
297 if ( mMasterAddressee.mailer().isEmpty() && !addr.mailer().isEmpty() )
298 mMasterAddressee.setMailer( addr.mailer() );
299
300 // N
301 if ( mMasterAddressee.assembledName().isEmpty() && !addr.assembledName().isEmpty() )
302 mMasterAddressee.setNameFromString( addr.assembledName() );
303
304 // NICKNAME
305 if ( mMasterAddressee.nickName().isEmpty() && !addr.nickName().isEmpty() )
306 mMasterAddressee.setNickName( addr.nickName() );
307
308 // NOTE
309 if ( mMasterAddressee.note().isEmpty() && !addr.note().isEmpty() )
310 mMasterAddressee.setNote( addr.note() );
311
312 // ORG
313 if ( mMasterAddressee.organization().isEmpty() && !addr.organization().isEmpty() )
314 mMasterAddressee.setOrganization( addr.organization() );
315
316/*
317 // PHOTO
318*/
319
320 // PROID
321 if ( mMasterAddressee.productId().isEmpty() && !addr.productId().isEmpty() )
322 mMasterAddressee.setProductId( addr.productId() );
323
324 // REV
325 if ( mMasterAddressee.revision().isNull() && !addr.revision().isNull() )
326 mMasterAddressee.setRevision( addr.revision() );
327
328 // ROLE
329 if ( mMasterAddressee.role().isEmpty() && !addr.role().isEmpty() )
330 mMasterAddressee.setRole( addr.role() );
331
332 // SORT-STRING
333 if ( mMasterAddressee.sortString().isEmpty() && !addr.sortString().isEmpty() )
334 mMasterAddressee.setSortString( addr.sortString() );
335
336/*
337 // SOUND
338*/
339
340 // TEL
341 KABC::PhoneNumber::List phones = addr.phoneNumbers();
342 KABC::PhoneNumber::List masterPhones = mMasterAddressee.phoneNumbers();
343 KABC::PhoneNumber::List::ConstIterator phoneIt;
344 for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt )
345 if ( !masterPhones.contains( *it ) )
346 mMasterAddressee.insertPhoneNumber( *it );
347
348 // TITLE
349 if ( mMasterAddressee.title().isEmpty() && !addr.title().isEmpty() )
350 mMasterAddressee.setTitle( addr.title() );
351
352 // TZ
353 if ( !mMasterAddressee.timeZone().isValid() && addr.timeZone().isValid() )
354 mMasterAddressee.setTimeZone( addr.timeZone() );
355
356 // UID // ignore UID
357
358 // URL
359 if ( mMasterAddressee.url().isEmpty() && !addr.url().isEmpty() )
360 mMasterAddressee.setUrl( addr.url() );
361
362 // X-
363 QStringList customs = addr.customs();
364 QStringList masterCustoms = mMasterAddressee.customs();
365 QStringList newCustoms( masterCustoms );
366 for ( it = customs.begin(); it != customs.end(); ++it )
367 if ( !masterCustoms.contains( *it ) )
368 newCustoms.append( *it );
369 mMasterAddressee.setCustoms( newCustoms );
370}
371
372#ifndef KAB_EMBEDDED
373#include "mergewidget.moc"
374#endif //KAB_EMBEDDED
diff --git a/kaddressbook/features/mergewidget.h b/kaddressbook/features/mergewidget.h
new file mode 100644
index 0000000..1063c80
--- a/dev/null
+++ b/kaddressbook/features/mergewidget.h
@@ -0,0 +1,87 @@
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2003 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#ifndef MERGEWIDGET_H
25#define MERGEWIDGET_H
26
27#include <kdialogbase.h>
28#include <klistview.h>
29
30#include "extensionwidget.h"
31
32class QListView;
33
34class KABCore;
35
36namespace KABC {
37class AddressBook;
38}
39
40class MergeWidget : public ExtensionWidget
41{
42 Q_OBJECT
43
44 public:
45 MergeWidget( KABCore*, QWidget *parent, const char *name = 0 );
46 virtual ~MergeWidget();
47
48 void contactsSelectionChanged();
49
50 QString title() const;
51 QString identifier() const;
52
53 private slots:
54 void mergeAndRemove();
55 void merge();
56
57 void selectionContactViewChanged();
58
59 private:
60 void updateView();
61 void doMerge( const KABC::Addressee &addr );
62
63 KListView *mContactView;
64 QPushButton *mMergeAndRemoveButton;
65 QPushButton *mMergeButton;
66
67 KABC::Addressee mMasterAddressee;
68 bool mBlockUpdate;
69};
70
71#ifdef KAB_EMBEDDED
72class MergeFactory : public ExtensionFactory
73{
74 public:
75 ExtensionWidget *extension( KABCore *core, QWidget *parent, const char *name )
76 {
77 return new MergeWidget( core, parent, name );
78 }
79
80 QString identifier() const
81 {
82 return "merge";
83 }
84};
85#endif //KAB_EMBEDDED
86
87#endif