summaryrefslogtreecommitdiffstats
path: root/kresources/lib/kcal_resourcegroupwarebase.cpp
blob: 7499b224d4120fa9a49d6476c9057ac4a1fdcd75 (plain)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
    This file is part of kdepim.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2004 Till Adam <adam@kde.org>
    Copyright (c) 2005 Reinhold Kainhofer <reinhold@kainhofer.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kcal_resourcegroupwarebase.h"
#include "kresources_groupwareprefs.h"

#include "libkcal/confirmsavedialog.h"
#include "folderlister.h"
#include "calendaradaptor.h"
#include "groupwaredownloadjob.h"
#include "groupwareuploadjob.h"

#include <libkcal/icalformat.h>
#include <kio/job.h>
#include <klocale.h>

#include <tqapplication.h>

using namespace KCal;

ResourceGroupwareBase::ResourceGroupwareBase()
  : ResourceCached( 0 ), mPrefs(0), mFolderLister(0), 
    mLock( true ), mAdaptor(0), mDownloadJob(0), mUploadJob(0)
{
}

ResourceGroupwareBase::ResourceGroupwareBase( const KConfig *config )
  : ResourceCached( config ), mPrefs(0), mFolderLister(0), 
    mLock( true ), mAdaptor(0), mDownloadJob(0), mUploadJob(0)
{
  if ( config ) readConfig( config );
}

ResourceGroupwareBase::~ResourceGroupwareBase()
{
  disableChangeNotification();

  delete mPrefs;
  mPrefs = 0;
}

KPIM::GroupwarePrefsBase *ResourceGroupwareBase::createPrefs()
{
  return new KPIM::GroupwarePrefsBase();
}


bool ResourceGroupwareBase::addEvent( Event *event )
{
  if ( adaptor() && ( adaptor()->supports( KPIM::FolderLister::Event ) ||
                      adaptor()->supports( KPIM::FolderLister::All ) ) ) {
    return ResourceCached::addEvent( event );
  } else return false;
}

bool ResourceGroupwareBase::addTodo( Todo *todo )
{
  if ( adaptor() && ( adaptor()->supports( KPIM::FolderLister::Todo ) ||
                      adaptor()->supports( KPIM::FolderLister::All ) ) ) {
    return ResourceCached::addTodo( todo );
  } else return false;
}

bool ResourceGroupwareBase::addJournal( Journal *journal )
{
  if ( adaptor() && ( adaptor()->supports( KPIM::FolderLister::Journal ) ||
                      adaptor()->supports( KPIM::FolderLister::All ) ) ) {
    return ResourceCached::addJournal( journal );
  } else return false;
}


KPIM::GroupwareDownloadJob *ResourceGroupwareBase::createDownloadJob( 
                            CalendarAdaptor *adaptor )
{
  return new KPIM::GroupwareDownloadJob( adaptor );
}

KPIM::GroupwareUploadJob *ResourceGroupwareBase::createUploadJob( 
                          CalendarAdaptor *adaptor )
{
  return new KPIM::GroupwareUploadJob( adaptor );
}

void ResourceGroupwareBase::setPrefs( KPIM::GroupwarePrefsBase *newprefs ) 
{
  if ( !newprefs ) return;
  if ( mPrefs ) delete mPrefs;
  mPrefs = newprefs;
  mPrefs->addGroupPrefix( identifier() );
  
  mPrefs->readConfig();
  if ( mFolderLister ) mFolderLister->readConfig( mPrefs );
}

void ResourceGroupwareBase::setFolderLister( KPIM::FolderLister *folderLister )
{
  if ( !folderLister ) return;
  if ( mFolderLister ) delete mFolderLister;
  mFolderLister = folderLister;
  if ( mPrefs ) mFolderLister->readConfig( mPrefs );
  if ( adaptor() ) {
    adaptor()->setFolderLister( mFolderLister );
    mFolderLister->setAdaptor( adaptor() );
  }
}

void ResourceGroupwareBase::setAdaptor( CalendarAdaptor *adaptor )
{
  if ( !adaptor ) return;
  if ( mAdaptor ) delete mAdaptor;
  mAdaptor = adaptor;
  mAdaptor->setFolderLister( mFolderLister );
  if ( mFolderLister ) mFolderLister->setAdaptor( mAdaptor );
  // TODO: Set the prgreess status in the up/download jobs, and set
  //       the resource name as the label. This should be done in the
  //       up/download jobs, but these don't have access to the resource's
  //       name, do they?
/*  mAdaptor->setDownloadProgressMessage( i18n("Downloading calendar") );
  mAdaptor->setUploadProgressMessage( i18n("Uploading calendar") );*/
  if ( prefs() ) {
    mAdaptor->setBaseURL( prefs()->url() );
    mAdaptor->setUser( prefs()->user() );
    mAdaptor->setPassword( prefs()->password() );
  }
  mAdaptor->setIdMapper( &idMapper() );
  mAdaptor->setResource( this );
}

void ResourceGroupwareBase::init()
{
  mDownloadJob = 0;
  mIsShowingError = false;

  enableChangeNotification();
}

KPIM::GroupwarePrefsBase *ResourceGroupwareBase::prefs()
{
  return mPrefs;
}

void ResourceGroupwareBase::readConfig( const KConfig *config )
{
  kdDebug(5800) << "KCal::ResourceGroupwareBase::readConfig()" << endl;
  ResourceCached::readConfig( config );
  if ( mPrefs ) {
    mPrefs->readConfig();
    if ( mFolderLister )
      mFolderLister->readConfig( mPrefs );
  }
}

void ResourceGroupwareBase::writeConfig( KConfig *config )
{
  kdDebug(5800) << "KCal::ResourceGroupwareBase::writeConfig()" << endl;

  ResourceCalendar::writeConfig( config );
  ResourceCached::writeConfig( config );

  if ( mPrefs ) {
    if ( mFolderLister )
      mFolderLister->writeConfig( mPrefs );
    mPrefs->writeConfig();
  }
}

bool ResourceGroupwareBase::doOpen()
{
  if ( !adaptor() )
    return false;
  if ( adaptor()->flags() & KPIM::GroupwareDataAdaptor::GWResNeedsLogon ) {
    KIO::Job *loginJob = adaptor()->createLoginJob( prefs()->url(), prefs()->user(), prefs()->password() );
    if ( !loginJob ) {
      return false;
    } else {
      mLoginFinished = false;
      connect( loginJob, TQT_SIGNAL( result( KIO::Job * ) ),
               TQT_SLOT( slotLoginJobResult( KIO::Job* ) ) );
      enter_loop();
      return mLoginFinished;
    }
  }
  return true;
}

// BEGIN:COPIED
// TODO: Get rid of this hack, which is copied from KIO::NetAccess, which is
// LGPL'ed and
//     Copyright (C) 1997 Torben Weis (weis@kde.org)
//     Copyright (C) 1998 Matthias Ettrich (ettrich@kde.org)
//     Copyright (C) 1999 David Faure (faure@kde.org)
// If a troll sees this, he kills me
void qt_enter_modal( TQWidget *widget );
void qt_leave_modal( TQWidget *widget );

void ResourceGroupwareBase::enter_loop()
{
  TQWidget dummy(0,0,WType_Dialog | WShowModal);
  dummy.setFocusPolicy( TQWidget::NoFocus );
  qt_enter_modal(&dummy);
  qApp->enter_loop();
  qt_leave_modal(&dummy);
}
// END:COPIED

void ResourceGroupwareBase::slotLoginJobResult( KIO::Job *job )
{
  if ( !adaptor() ) return;
  mLoginFinished = adaptor()->interpretLoginJobResult( job );
  qApp->exit_loop();
}

void ResourceGroupwareBase::doClose()
{
  ResourceCached::doClose();
  if ( mDownloadJob ) mDownloadJob->kill();

  if ( adaptor() && 
       adaptor()->flags() & KPIM::GroupwareDataAdaptor::GWResNeedsLogoff ) {
    KIO::Job *logoffJob = adaptor()->createLogoffJob( prefs()->url(), prefs()->user(), prefs()->password() );
    connect( logoffJob, TQT_SIGNAL( result( KIO::Job * ) ),
             TQT_SLOT( slotLogoffJobResult( KIO::Job* ) ) );
    // TODO: Do we really need to block while waiting for the job to return?
    enter_loop();
  }
}

void ResourceGroupwareBase::slotLogoffJobResult( KIO::Job *job )
{
  if ( !adaptor() ) return;
  adaptor()->interpretLogoffJobResult( job );
  // TODO: Do we really need to block while waiting for the job to return?
  qApp->exit_loop();
}

bool ResourceGroupwareBase::doLoad()
{
  kdDebug(5800) << "ResourceGroupwareBase::load()" << endl;

  if ( mIsShowingError ) {
    kdDebug(5800) << "Still showing error" << endl;
    return true;
  }

  if ( mDownloadJob ) {
    kdWarning() << "Download still in progress" << endl;
    return false;
  }
  
  mCalendar.close();
  clearChanges();
  disableChangeNotification();
  loadCache();
  enableChangeNotification();
  emit resourceChanged( this );

  mDownloadJob = createDownloadJob( adaptor() );
  connect( mDownloadJob, TQT_SIGNAL( result( KPIM::GroupwareJob * ) ),
    TQT_SLOT( slotDownloadJobResult( KPIM::GroupwareJob * ) ) );

  return true;
}

void ResourceGroupwareBase::slotDownloadJobResult( KPIM::GroupwareJob *job )
{
  kdDebug(5800) << "ResourceGroupwareBase::slotJobResult(): " << endl;

  if ( job->error() ) {
    mIsShowingError = true;
    loadError( job->errorString() );
    mIsShowingError = false;
  } else {
    kdDebug(5800) << "Successfully downloaded data" << endl;
  
    clearChanges();
    saveCache();
    enableChangeNotification();

    emit resourceChanged( this );
    emit resourceLoaded( this );
  }

  mDownloadJob = 0;
}

bool ResourceGroupwareBase::doSave()
{
  kdDebug(5800) << "KCal::ResourceGroupwareBase::doSave()" << endl;

  saveCache();

  if ( !hasChanges() ) {
    kdDebug(5800) << "No changes" << endl;
    return true;
  }
  // TODO: Implement confirming of single changes i.e. it should be possible
  //       to upload only certain changes and discard the rest. This is
  //       particularly important for resources like the blogging resource,
  //       where uploading would mean a republication of the blog, not only
  //       a modifications. 
  if ( !confirmSave() ) return false;
  
  mUploadJob = createUploadJob( adaptor() );
  connect( mUploadJob, TQT_SIGNAL( result( KPIM::GroupwareJob * ) ),
    TQT_SLOT( slotUploadJobResult( KPIM::GroupwareJob * ) ) );

  Incidence::List inc;
  Incidence::List::Iterator it;
  KPIM::GroupwareUploadItem::List addedItems, changedItems, deletedItems;

  inc = addedIncidences();
  for( it = inc.begin(); it != inc.end(); ++it ) {
    addedItems.append( adaptor()->newUploadItem( *it, 
                                           KPIM::GroupwareUploadItem::Added ) );
  }
  // TODO: Check if the item has changed on the server...
  // In particular, check if the version we based our change on is still current 
  // on the server
  inc = changedIncidences();
  for( it = inc.begin(); it != inc.end(); ++it ) {
    changedItems.append( adaptor()->newUploadItem( *it, 
                                         KPIM::GroupwareUploadItem::Changed ) );
  }
  inc = deletedIncidences();
  for( it = inc.begin(); it != inc.end(); ++it ) {
    deletedItems.append( adaptor()->newUploadItem( *it, 
                                         KPIM::GroupwareUploadItem::Deleted ) );
  }

  mUploadJob->setAddedItems( addedItems );
  mUploadJob->setChangedItems( changedItems );
  mUploadJob->setDeletedItems( deletedItems );

  // FIXME: Calling clearChanges() here is not the ideal way since the
  // upload might fail, but there is no other place to call it...
  clearChanges();
  return true;
}

void ResourceGroupwareBase::slotUploadJobResult( KPIM::GroupwareJob *job )
{
  kdDebug(5800) << "ResourceGroupwareBase::slotUploadJobResult(): " << endl;

  if ( job->error() ) {
    mIsShowingError = true;
    loadError( job->errorString() );
    mIsShowingError = false;
  } else {
    kdDebug(5800) << "Successfully uploaded data" << endl;
    /* 
     * After the put the server might have expanded recurring events and will
     * also change the uids of the uploaded events. Remove them from the cache
     * and get the fresh delta and download. 
     */

    if ( !mDownloadJob ) {
      mDownloadJob = createDownloadJob( adaptor() );
      connect( mDownloadJob, TQT_SIGNAL( result( KPIM::GroupwareJob * ) ),
          TQT_SLOT( slotDownloadJobResult( KPIM::GroupwareJob * ) ) );
    } else {
      kdWarning() << k_funcinfo << "Download still in progress. "
                                   "Can't happen. (TM)" << endl;
    }
  }

  mDownloadJob = 0;
}

// FIXME: Put this into ResourceCached
bool ResourceGroupwareBase::confirmSave()
{
  if ( !hasChanges() ) return true;

  ConfirmSaveDialog dlg( resourceName(), 0 );

  dlg.addIncidences( addedIncidences(), i18n("Added") );
  dlg.addIncidences( changedIncidences(), i18n("Changed") );
  dlg.addIncidences( deletedIncidences(), i18n("Deleted") );

  int result = dlg.exec();
  return result == TQDialog::Accepted;
}

KABC::Lock *ResourceGroupwareBase::lock()
{
  return &mLock;
}

#include "kcal_resourcegroupwarebase.moc"