summaryrefslogtreecommitdiffabout
path: root/kaddressbook/features/mergewidget.cpp
Unidiff
Diffstat (limited to 'kaddressbook/features/mergewidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/features/mergewidget.cpp374
1 files changed, 374 insertions, 0 deletions
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