1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
kopeteaddrbookexport.cpp - Kopete Online tqStatus
Logic for exporting data acquired from messaging systems to the
KDE address book
Copyright (c) 2004 by Will Stephenson <lists@stevello.free-online.co.uk>
Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
*************************************************************************
*/
#include <kabc/phonenumber.h>
#include <tqcombobox.h>
#include <tqlabel.h>
#include <kdialogbase.h>
#include <kiconloader.h>
#include <klistbox.h>
#include <klocale.h>
#include "kopeteaccount.h"
#include "kopeteglobal.h"
#include "kopetemetacontact.h"
#include "kopetecontact.h"
#include "kopeteaddrbookexport.h"
#include "kopeteaddrbookexportui.h"
KopeteAddressBookExport::KopeteAddressBookExport( TQWidget *tqparent, Kopete::MetaContact *mc ) : TQObject( tqparent )
{
// instantiate dialog and populate widgets
mParent = tqparent;
mAddressBook = KABC::StdAddressBook::self();
mMetaContact = mc;
}
KopeteAddressBookExport::~KopeteAddressBookExport()
{
}
void KopeteAddressBookExport::initLabels()
{
if ( !mAddressee.isEmpty() )
{
mUI->mLblFirstName->setText( mAddressee.givenNameLabel() );
mUI->mLblLastName->setText( mAddressee.familyNameLabel() );
mUI->mLblEmail->setText( mAddressee.emailLabel() );
mUI->mLblUrl->setText( mAddressee.urlLabel() );
mUI->mLblHomePhone->setText( mAddressee.homePhoneLabel() );
mUI->mLblWorkPhone->setText( mAddressee.businessPhoneLabel() );
mUI->mLblMobilePhone->setText( mAddressee.mobilePhoneLabel() );
}
}
void KopeteAddressBookExport::fetchKABCData()
{
if ( !mAddressee.isEmpty() )
{
mAddrBookIcon = SmallIcon( "kaddressbook" );
// given name
TQString given = mAddressee.givenName();
if ( !given.isEmpty() )
mUI->mFirstName->insertItem( mAddrBookIcon, given );
else
mUI->mFirstName->insertItem( mAddrBookIcon, i18n("<Not Set>") );
// family name
TQString family = mAddressee.familyName();
if ( !family.isEmpty() )
mUI->mLastName->insertItem( mAddrBookIcon, family );
else
mUI->mLastName->insertItem( mAddrBookIcon, i18n("<Not Set>") );
// url
TQString url = mAddressee.url().url();
if ( !url.isEmpty() )
mUI->mUrl->insertItem( mAddrBookIcon, url );
else
mUI->mUrl->insertItem( mAddrBookIcon, i18n("<Not Set>") );
// emails
TQStringList emails = mAddressee.emails();
numEmails = emails.count();
for ( TQStringList::Iterator it = emails.begin(); it != emails.end(); ++it )
mUI->mEmails->insertItem( mAddrBookIcon, *it );
if ( numEmails == 0 )
{
mUI->mEmails->insertItem( mAddrBookIcon, i18n("<Not Set>") );
numEmails = 1;
}
// phone numbers
fetchPhoneNumbers( mUI->mHomePhones, KABC::PhoneNumber::Home, numHomePhones );
fetchPhoneNumbers( mUI->mWorkPhones, KABC::PhoneNumber::Work, numWorkPhones );
fetchPhoneNumbers( mUI->mMobilePhones, KABC::PhoneNumber::Cell, numMobilePhones );
}
}
void KopeteAddressBookExport::fetchPhoneNumbers( KListBox * listBox, int type, uint& counter )
{
KABC::PhoneNumber::List phones = mAddressee.phoneNumbers( type );
counter = phones.count();
KABC::PhoneNumber::List::Iterator it;
for ( it = phones.begin(); it != phones.end(); ++it )
listBox->insertItem( mAddrBookIcon, (*it).number() );
if ( counter == 0 )
{
listBox->insertItem( mAddrBookIcon, i18n("<Not Set>") );
counter = 1;
}
}
void KopeteAddressBookExport::fetchIMData()
{
TQPtrList<Kopete::Contact> contacts = mMetaContact->contacts();
TQPtrListIterator<Kopete::Contact> cit( contacts );
for( ; cit.current(); ++cit )
{
// for each contact, get the property content
Kopete::Contact* c = cit.current();
TQPixmap contactIcon = c->account()->accountIcon( 16 );
// given name
populateIM( c, contactIcon, mUI->mFirstName, Kopete::Global::Properties::self()->firstName() );
// family name
populateIM( c, contactIcon, mUI->mLastName, Kopete::Global::Properties::self()->lastName() );
// url
// TODO: make URL/homepage a global template, currently only in IRC channel contact
// emails
populateIM( c, contactIcon, mUI->mEmails, Kopete::Global::Properties::self()->emailAddress() );
// home phone
populateIM( c, contactIcon, mUI->mHomePhones, Kopete::Global::Properties::self()->privatePhone() );
// work phone
populateIM( c, contactIcon, mUI->mWorkPhones, Kopete::Global::Properties::self()->workPhone() );
// mobile phone
populateIM( c, contactIcon, mUI->mMobilePhones, Kopete::Global::Properties::self()->privateMobilePhone() );
}
}
void KopeteAddressBookExport::populateIM( const Kopete::Contact *contact, const TQPixmap &icon, TQComboBox *combo, const Kopete::ContactPropertyTmpl &property )
{
Kopete::ContactProperty prop = contact->property( property );
if ( !prop.isNull() )
{
combo->insertItem( icon, prop.value().toString() );
}
}
void KopeteAddressBookExport::populateIM( const Kopete::Contact *contact, const TQPixmap &icon, KListBox *listBox, const Kopete::ContactPropertyTmpl &property )
{
Kopete::ContactProperty prop = contact->property( property );
if ( !prop.isNull() )
{
listBox->insertItem( icon, prop.value().toString() );
}
}
int KopeteAddressBookExport::showDialog()
{
mAddressee = mAddressBook->findByUid( mMetaContact->metaContactId() );
if ( !mAddressee.isEmpty() )
{
numEmails = 0;
numHomePhones = 0;
numWorkPhones = 0;
numMobilePhones = 0;
mDialog = new KDialogBase( mParent, "addressbookexportdialog", true, i18n("Export to Address Book"), KDialogBase::Ok|KDialogBase::Cancel );
mUI = new AddressBookExportUI( mDialog );
mDialog->setMainWidget( mUI );
mDialog->setButtonOK( KGuiItem( i18n( "Export" ),
TQString(), i18n( "Set address book fields using the selected data from Kopete" ) ) );
initLabels();
// fetch existing data from kabc
fetchKABCData();
// fetch data from contacts
fetchIMData();
return mDialog->exec();
}
else
return TQDialog::Rejected;
}
void KopeteAddressBookExport::exportData()
{
// write the data from the widget to KABC
// update the Addressee
// first name
bool dirty = false;
if ( newValue( mUI->mFirstName ) )
{
dirty = true;
mAddressee.setGivenName( mUI->mFirstName->currentText() );
}
// last name
if ( newValue( mUI->mLastName ) )
{
dirty = true;
mAddressee.setFamilyName( mUI->mLastName->currentText() );
}
// url
if ( newValue( mUI->mUrl ) )
{
dirty = true;
mAddressee.setUrl( KURL( mUI->mUrl->currentText() ) );
}
TQStringList newVals;
// email
newVals = newValues( mUI->mEmails, numEmails );
for ( TQStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
{
dirty = true;
mAddressee.insertEmail( *it );
}
// home phone
newVals = newValues( mUI->mHomePhones, numHomePhones );
for ( TQStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
{
dirty = true;
mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Home ) );
}
// work phone
newVals = newValues( mUI->mWorkPhones, numWorkPhones );
for ( TQStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
{
dirty = true;
mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Work ) );
}
// mobile
newVals = newValues( mUI->mMobilePhones, numMobilePhones );
for ( TQStringList::Iterator it = newVals.begin(); it != newVals.end(); ++it )
{
dirty = true;
mAddressee.insertPhoneNumber( KABC::PhoneNumber( *it, KABC::PhoneNumber::Cell ) );
}
if ( dirty )
{
// write the changed addressbook
mAddressBook->insertAddressee( mAddressee );
KABC::Ticket *ticket = mAddressBook->requestSaveTicket();
if ( !ticket )
kdWarning( 14000 ) << k_funcinfo << "WARNING: Resource is locked by other application!" << endl;
else
{
if ( !mAddressBook->save( ticket ) )
{
kdWarning( 14000 ) << k_funcinfo << "ERROR: Saving failed!" << endl;
mAddressBook->releaseSaveTicket( ticket );
}
}
kdDebug( 14000 ) << k_funcinfo << "Finished writing KABC" << endl;
}
}
bool KopeteAddressBookExport::newValue( TQComboBox *combo )
{
// all data in position 0 is from KABC, so if position 0 is selected,
// or if the selection is the same as the data at 0, return false
return !( combo->currentItem() == 0 ||
( combo->text( combo->currentItem() ) == combo->text( 0 ) ) );
}
TQStringList KopeteAddressBookExport::newValues( KListBox *listBox, uint counter )
{
TQStringList newValues;
// need to iterate all items except those from KABC and check if selected and not same as the first
// counter is the number of KABC items, and hence the index of the first non KABC item
for ( uint i = counter; i < listBox->count(); ++i )
{
if ( listBox->isSelected( i ) )
{
// check whether it matches any KABC item
bool duplicate = false;
for ( uint j = 0; j < counter; ++j )
{
if ( listBox->text( i ) == listBox->text( j ) )
duplicate = true;
}
if ( !duplicate )
newValues.append( listBox->text( i ) );
}
}
return newValues;
}
|