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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
This file is part of kdepim.
Copyright (c) 2004 Cornelius Schumacher <schumacher@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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <qapplication.h>
#include <kabc/addressee.h>
#include <kabc/vcardconverter.h>
#include <kconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <libkdepim/groupwarejob.h>
#include "kabc_groupwareprefs.h"
#include "kabc_resourcegroupware.h"
using namespace KABC;
ResourceGroupware::ResourceGroupware( const KConfig *config )
: ResourceCached( config )
{
init();
mPrefs->addGroupPrefix( identifier() );
if ( config ) {
readConfig( config );
}
}
ResourceGroupware::ResourceGroupware( const KURL &url,
const QString &user,
const QString &password )
: ResourceCached( 0 )
{
init();
mPrefs->addGroupPrefix( identifier() );
mPrefs->setUrl( url.url() );
mPrefs->setUser( user );
mPrefs->setPassword( password );
}
void ResourceGroupware::init()
{
mDownloadJob = 0;
mProgress = 0;
mPrefs = new GroupwarePrefs;
setType( "groupware" );
}
ResourceGroupware::~ResourceGroupware()
{
delete mPrefs;
mPrefs = 0;
}
void ResourceGroupware::readConfig( const KConfig * )
{
mPrefs->readConfig();
readAddressBooks();
}
void ResourceGroupware::writeConfig( KConfig *config )
{
Resource::writeConfig( config );
writeAddressBooks();
mPrefs->writeConfig();
}
void ResourceGroupware::readAddressBooks()
{
#if 0
QStringList ids = prefs()->ids();
QStringList names = prefs()->names();
QStringList personals = prefs()->personals();
QStringList frequents = prefs()->frequents();
if ( ids.count() != names.count() || ids.count() != personals.count() ||
ids.count() != frequents.count() ) {
kdError() << "Corrupt addressbook configuration" << endl;
return;
}
mAddressBooks.clear();
for( uint i = 0; i < ids.count(); ++i ) {
Groupware::AddressBook ab;
ab.id = ids[ i ];
ab.name = names[ i ];
ab.isPersonal = personals[ i ] == "1";
ab.isFrequentContacts = frequents[ i ] == "1";
mAddressBooks.append( ab );
}
#endif
}
void ResourceGroupware::writeAddressBooks()
{
#if 0
QStringList ids;
QStringList names;
QStringList personals;
QStringList frequents;
Groupware::AddressBook::List::ConstIterator it;
for( it = mAddressBooks.begin(); it != mAddressBooks.end(); ++it ) {
ids.append( (*it).id );
names.append( (*it).name );
personals.append( (*it).isPersonal ? "1" : "0" );
frequents.append( (*it).isFrequentContacts ? "1" : "0" );
}
prefs()->setIds( ids );
prefs()->setNames( names );
prefs()->setPersonals( personals );
prefs()->setFrequents( frequents );
#endif
}
void ResourceGroupware::retrieveAddressBooks()
{
#if 0
bool firstRetrieve = mAddressBooks.isEmpty();
GroupwareServer server( prefs()->url(),
prefs()->user(),
prefs()->password(), this );
server.login();
mAddressBooks = server.addressBookList();
server.logout();
if ( firstRetrieve ) {
QStringList reads;
QString write;
Groupware::AddressBook::List::ConstIterator it;
for( it = mAddressBooks.begin(); it != mAddressBooks.end(); ++it ) {
if ( (*it).isPersonal ) {
reads.append( (*it).id );
if ( write.isEmpty() ) write = (*it).id;
}
}
prefs()->setReadAddressBooks( reads );
prefs()->setWriteAddressBook( write );
}
#endif
}
Ticket *ResourceGroupware::requestSaveTicket()
{
if ( !addressBook() ) {
kdDebug(5700) << "no addressbook" << endl;
return 0;
}
return createTicket( this );
}
void ResourceGroupware::releaseSaveTicket( Ticket *ticket )
{
delete ticket;
}
bool ResourceGroupware::doOpen()
{
return true;
}
void ResourceGroupware::doClose()
{
kdDebug() << "ResourceGroupware::doClose()" << endl;
cancelLoad();
}
bool ResourceGroupware::load()
{
return asyncLoad();
}
bool ResourceGroupware::asyncLoad()
{
if ( mDownloadJob ) {
kdWarning() << "Download still in progress" << endl;
return false;
}
mAddrMap.clear();
loadCache();
#if 0
if ( addressBooks().isEmpty() ) {
kdDebug() << "Retrieving default addressbook list." << endl;
retrieveAddressBooks();
writeAddressBooks();
}
#endif
KURL url( prefs()->url() );
url.setUser( prefs()->user() );
url.setPass( prefs()->password() );
#if 0
QString query = "?";
QStringList ids = mPrefs->readAddressBooks();
QStringList::ConstIterator it;
for( it = ids.begin(); it != ids.end(); ++it ) {
if ( it != ids.begin() ) query += "&";
query += "addressbookid=" + *it;
}
url.setQuery( query );
#endif
kdDebug() << "Download URL: " << url << endl;
mJobData = QString::null;
mDownloadJob = KPIM::GroupwareJob::getAddressBook( url );
connect( mDownloadJob, SIGNAL( result( KIO::Job * ) ),
SLOT( slotJobResult( KIO::Job * ) ) );
connect( mDownloadJob, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
SLOT( slotJobData( KIO::Job *, const QByteArray & ) ) );
connect( mDownloadJob, SIGNAL( percent( KIO::Job *, unsigned long ) ),
SLOT( slotJobPercent( KIO::Job *, unsigned long ) ) );
mProgress = KPIM::ProgressManager::instance()->createProgressItem(
KPIM::ProgressManager::getUniqueID(), i18n("Downloading addressbook") );
connect( mProgress,
SIGNAL( progressItemCanceled( KPIM::ProgressItem * ) ),
SLOT( cancelLoad() ) );
return true;
}
bool ResourceGroupware::save( Ticket *ticket )
{
return asyncSave( ticket );
}
bool ResourceGroupware::asyncSave( Ticket* )
{
#if 0
if ( !mServer->login() ) return false;
KABC::Addressee::List::Iterator it;
KABC::Addressee::List addedList = addedAddressees();
for ( it = addedList.begin(); it != addedList.end(); ++it ) {
if ( mServer->insertAddressee( mPrefs->writeAddressBook(), *it ) ) {
clearChange( *it );
idMapper().setRemoteId( (*it).uid(), (*it).custom( "GWRESOURCE", "UID" ) );
}
}
KABC::Addressee::List changedList = changedAddressees();
for ( it = changedList.begin(); it != changedList.end(); ++it ) {
if ( mServer->changeAddressee( *it ) )
clearChange( *it );
}
KABC::Addressee::List deletedList = deletedAddressees();
for ( it = deletedList.begin(); it != deletedList.end(); ++it ) {
if ( mServer->removeAddressee( *it ) )
clearChange( *it );
}
saveCache();
mServer->logout();
#endif
return true;
}
void ResourceGroupware::slotJobResult( KIO::Job *job )
{
kdDebug() << "ResourceGroupware::slotJobResult(): " << endl;
if ( job->error() ) {
kdError() << job->errorString() << endl;
emit loadingError( this, job->errorString() );
} else {
mAddrMap.clear();
KABC::VCardConverter conv;
Addressee::List addressees = conv.parseVCards( mJobData );
Addressee::List::ConstIterator it;
for( it = addressees.begin(); it != addressees.end(); ++it ) {
KABC::Addressee addr = *it;
if ( !addr.isEmpty() ) {
addr.setResource( this );
QString remote = addr.custom( "GWRESOURCE", "UID" );
QString local = idMapper().localId( remote );
if ( local.isEmpty() ) {
idMapper().setRemoteId( addr.uid(), remote );
} else {
addr.setUid( local );
}
insertAddressee( addr );
clearChange( addr );
}
}
}
saveCache();
emit loadingFinished( this );
mDownloadJob = 0;
if ( mProgress ) mProgress->setComplete();
mProgress = 0;
}
void ResourceGroupware::slotJobData( KIO::Job *, const QByteArray &data )
{
// kdDebug() << "ResourceGroupware::slotJobData()" << endl;
mJobData.append( data.data() );
}
void ResourceGroupware::slotJobPercent( KIO::Job *, unsigned long percent )
{
kdDebug() << "ResourceGroupware::slotJobPercent() " << percent << endl;
if ( mProgress ) mProgress->setProgress( percent );
}
void ResourceGroupware::cancelLoad()
{
if ( mDownloadJob ) mDownloadJob->kill();
mDownloadJob = 0;
if ( mProgress ) mProgress->setComplete();
mProgress = 0;
}
#include "kabc_resourcegroupware.moc"
|