summaryrefslogtreecommitdiffstats
path: root/conduits/vcalconduit/vcal-conduitbase.cc
blob: 15cfcdc867b9ff35ef3a047b66fdcb122960eddf (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/* KPilot
**
** Copyright (C) 2002-3 by Reinhold Kainhofer
** Copyright (C) 2001 by Dan Pilone
**
** Contributions:
**    Copyright (c) 2001 David Jarvie <software@astrojar.org.uk>
**    Copyright (C) 2006 by Bertjan Broeksema <b.broeksema@gmail.com>
**
** This file defines the vcal-conduit plugin.
*/

/*
** 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 in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include <options.h>

#include <tqtimer.h>
#include <tqfile.h>

#include <tdemessagebox.h>
#include <tdeio/netaccess.h>

#include "libkcal/calendar.h"
#include "libkcal/calendarlocal.h"
#include "libkcal/calendarresources.h"
#include <kstandarddirs.h>

#include "pilotSerialDatabase.h"
#include "pilotLocalDatabase.h"
#include "pilotDateEntry.h"

#include "vcal-conduitbase.moc"
#include "vcalconduitSettings.h"

#ifndef LIBKCAL_IS_VERSION
#warning "Using an old version of libkcal with timezone bug."
#define LIBKCAL_IS_VERSION(a,b,c) (0)
#endif

#include "conduitstate.h"
#include "initstate.h"


/****************************************************************************
 *                          VCalConduitBase class                           *
 ****************************************************************************/

VCalConduitBase::VCalConduitBase(KPilotLink *d,
	const char *n,
	const TQStringList &a) :
	ConduitAction(d,n,a),
	fCalendar(0L),
	fP(0L)
{
	FUNCTIONSETUP;

	fState = new InitState();
}

VCalConduitBase::~VCalConduitBase()
{
	FUNCTIONSETUP;

	KPILOT_DELETE(fP);
	KPILOT_DELETE(fState);
	KPILOT_DELETE(fCalendar);
	KPILOT_DELETE(fDatabase);
	KPILOT_DELETE(fLocalDatabase);
}


/*
	There are several different scenarios for a record on the Palm and its PC
	counterpart. N means a new record, M flags a modified record, D a deleted
	and - an unmodified record. First is the Palm record, second the
	corresponding PC record:
	(-,-) unchanged, just sync if first time or full sync
	(N,-) no rec matching the Palm ID in the backupDB/calendar yet => add
	      KCal::Event
	(M,-) record is in backupDB, unchanged in calendar => modify in calendar and
	      in backupDB
	(D,-) deleted on Palm, exists in backupDB and calendar => just delete from
	      calendar and backupDB
	(-,N) no or invalid pilotID set for the KCal::Event => just add to palm and
	      backupDB
	(-,M) valid PilotID set => just modify on Palm
	(-,D) Record in backupDB, but not in calendar => delete from Palm and
	      backupDB
	(N,N) Can't find out (the two records are not correlated in any way, they
	      just have the same data!!
	(M,M),(M,L),(L,M) (Record exists on Palm and the Event has the ID) CONFLICT,
	      ask the user what to do or use a config setting
	(L,L) already deleted on both, no need to do anything.

	The sync process is as follows (for a fast sync):
	1) HHToPCState goes through all records on Palm (just the modified one
	   are necessary), find it in the backupDB. The following handles ([NMD],*)
		a) if it doesn't exist and was not deleted, add it to the calendar and
		   the backupDB
		b) if it exists, is unchanged in the calendar and was not deleted,
		   just modify in the calendar
		c) if it exists and was deleted, delete it from the calendar if
		   necessary
	2) PCToHHState goes through all KCale::Events in the calendar (just
	   modified, this is the modification time is later than the last sync time
		). This handles (-,N),(-,M)
		a) if it does not have a pilotID, add it to the palm and backupDB,
		   store the PalmID
		b) if it has a valid pilotID, update the Palm record and the backup
	3) DeletedUnsyncedHHState goes through all palm records (which don't
	   have the deleted flag) of the palm db and if one does not exist in the
	   Calendar, it was deleted there, so delete it from the Palm and backup,
	   too. This handles the case of (-,D)
	4) DeletedUnsyncedPCState goes through all KCal::Events in the calendar and
	   looks for a corresponding event in the palm database. If it does not
	   exist, that means that it was deleted on the palm, so we need to also
	   delete it from the local calendar.  This handles the case of (D,-).

	In addition to the fast sync, where the last sync was done with this very
	PC and calendar file, there are two special cases: a full and a first sync.
	-) a full sync goes through all records, not just the modified ones. The
	   pilotID setting of the calendar records is used to determine if the
	   record already exists. if yes, the record is just modified.
	-) a first sync completely ignores the pilotID setting of the calendar
	   events. All records are added, so there might be duplicates. The add
	   function for the calendar should check if a similar record already
	   exists, but this is not done yet.

	-) a full sync is done if
	   a) there is a backupdb and a calendar, but the PC id number changed
	   b) it was explicitly requested by pressing the full sync button in KPilot
	   c) the setting "always full sync" was selected in the configuration dlg
	-) a first sync is done if
	   a) either the calendar or the backup DB does not exist.
	   b) the calendar and the backup DB exists, but the sync is done for a
	      different User name
	   c) it was explicitly requested in KPilot
*/

/* virtual */ bool VCalConduitBase::exec()
{
	FUNCTIONSETUP;

	readConfig();

	// don't do a first sync by default in any case, only when explicitly
	// requested, or the backup database or the alendar are empty.
	setFirstSync( false );

	// TODO: Check Full sync and First sync
	bool retrieved = false;
	if ( !openDatabases( dbname(), &retrieved ) ) goto error;
	setFirstSync( retrieved );

	// If we are in testmode we don't need the local calendar. Else a
	// calendar *must* be opened, we want to sync something don't we?
	if (!syncMode().isTest() && !openCalendar() ) goto error;

	// Start processing the sync
	TQTimer::singleShot(0, this, TQT_SLOT(slotProcess()));
	return true;

error:
	emit logError( i18n( "Could not open the calendar databases." ) );

	KPILOT_DELETE(fCalendar);
	KPILOT_DELETE(fP);
	KPILOT_DELETE(fState);
	return false;
}

void VCalConduitBase::slotProcess() {
	FUNCTIONSETUP;

	// start the current state if necessary
	if( fState && !fState->started() ) {
		fState->startSync( this );
	}

	// Process next record if applicable
	if( hasNextRecord )
	{
		fState->handleRecord( this );
		TQTimer::singleShot( 0, this, TQT_SLOT( slotProcess() ) );
	}
	// Else finish the current state if there is one
	else if( fState )
	{
		fState->finishSync( this );
		TQTimer::singleShot( 0, this, TQT_SLOT( slotProcess() ) );
	}
	// No state so sync is finished
	else
	{
		DEBUGKPILOT << fname << ": Sync finished." << endl;
		delayDone();
	}
}

/* virtual */ void VCalConduitBase::readConfig()
{
	config()->readConfig();
	SyncAction::ConflictResolution res = (SyncAction::ConflictResolution)
		(config()->conflictResolution());
	setConflictResolution( res );
}

static void listResources( KCal::CalendarResources *p )
{
	FUNCTIONSETUP;
	KCal::CalendarResourceManager *manager = p->resourceManager();

	DEBUGKPILOT << fname << ": Resources in calendar:" << endl;
	KCal::CalendarResourceManager::Iterator it;
	for( it = manager->begin(); it != manager->end(); ++it )
	{
		DEBUGKPILOT << fname << ": " << (*it)->resourceName() << endl;
	}
}

/* virtual */ bool VCalConduitBase::openCalendar()
{
	FUNCTIONSETUP;

	TDEConfig korgcfg( locate( "config", CSL1("korganizerrc") ) );

	// this part taken from adcalendarbase.cpp:
	korgcfg.setGroup( "Time & Date" );
	TQString tz(korgcfg.readEntry( "TimeZoneId" ) );

	DEBUGKPILOT << fname << ": KOrganizer's time zone = " << tz << endl;

	// Need a subclass ptr. for the ResourceCalendar methods
	KCal::CalendarResources *rescal = 0L;

	DEBUGKPILOT << fname << ": Got calendar type " << config()->calendarType()
		<< endl;

	switch(config()->calendarType())
	{
		case VCalConduitSettings::eCalendarLocal:
		{
			DEBUGKPILOT << fname << "Using CalendarLocal, file = "
				<< config()->calendarFile() << endl;

			if ( config()->calendarFile().isEmpty() )
			{
				DEBUGKPILOT << fname << "Empty calendar file name." << endl;

				emit logError( i18n( "You selected to sync with an iCalendar"
						" file, but did not give a filename. Please select a"
						" valid file name in the conduit's configuration"
						" dialog" ) );
				return false;
			}

			fCalendar = new KCal::CalendarLocal( tz );
			if ( !fCalendar )
			{
				WARNINGKPILOT
					<< "Cannot initialize calendar object for file "
					<< config()->calendarFile() << endl;
				return false;
			}

			DEBUGKPILOT << fname << "Calendar's timezone: "
				<< fCalendar->timeZoneId() << endl;
			DEBUGKPILOT << fname << "Calendar is local time: "
				<< fCalendar->isLocalTime() << endl;

			emit logMessage( fCalendar->isLocalTime() ?
				i18n( "Using local time zone: %1" ).arg( tz ) :
				i18n( "Using non-local time zone: %1" ).arg( tz ) );

			KURL kurl( config()->calendarFile() );
			if( !TDEIO::NetAccess::download( config()->calendarFile(),
				fCalendarFile, 0L ) && !kurl.isLocalFile() )
			{
				emit logError(i18n( "You chose to sync with the file \"%1\", which "
					"cannot be opened. Please make sure to supply a "
					"valid file name in the conduit's configuration dialog. "
					"Aborting the conduit." ).arg( config()->calendarFile() ) );
				TDEIO::NetAccess::removeTempFile( fCalendarFile );
				return false;
			}

			// if there is no calendar yet, use a first sync..
			// the calendar is initialized, so nothing more to do...
			if (!dynamic_cast<KCal::CalendarLocal*>(fCalendar)->load(fCalendarFile) )
			{
				DEBUGKPILOT << fname << "Calendar file " << fCalendarFile
					<< " could not be opened. Will create a new one" << endl;

				// Try to create empty file. if it fails,
				// no valid file name was given.
				TQFile fl(fCalendarFile);
				if (!fl.open(IO_WriteOnly | IO_Append))
				{
					DEBUGKPILOT << fname << "Invalid calendar file name "
						<< fCalendarFile << endl;

					emit logError( i18n( "You chose to sync with the file \"%1\", which "
						"cannot be opened or created. Please make sure to supply a "
						"valid file name in the conduit's configuration dialog. "
						"Aborting the conduit." ).arg( config()->calendarFile() ) );
					return false;
				}
				fl.close();
				setFirstSync( true );
			}
			addSyncLogEntry( i18n( "Syncing with file \"%1\"" )
				.arg( config()->calendarFile() ) );
			break;
		}

		case VCalConduitSettings::eCalendarResource:
			DEBUGKPILOT << "Using CalendarResource!" << endl;

			rescal = new KCal::CalendarResources( tz );
			listResources(rescal);
			fCalendar = rescal;
			if ( !fCalendar)
			{
				WARNINGKPILOT << "Cannot initialize calendar " <<
					"object for ResourceCalendar" << endl;
				return false;
			}

#if LIBKCAL_IS_VERSION(1,1,0)
			rescal->readConfig();
			rescal->load();
#else
#warning "Timezone bug is present."
#endif
			addSyncLogEntry( i18n( "Syncing with standard calendar resource." ) );
			emit logMessage( fCalendar->isLocalTime() ?
				i18n( "Using local time zone: %1" ).arg( tz ) :
				i18n( "Using non-local time zone: %1" ).arg( tz ) );
			break;
		default:
			break;
	}

	if ( !fCalendar )
	{
		WARNINGKPILOT << "Unable to initialize calendar object."
			<< " Please check the conduit's setup." << endl;
		emit logError( i18n( "Unable to initialize the calendar object. Please"
			" check the conduit's setup") );
		return false;
	}
	fP = createPrivateCalendarData( fCalendar );
	if ( !fP )
	{
		return false;
	}
	int rc = fP->updateIncidences();
	DEBUGKPILOT << fname << ": return from updateIncidences: [" << rc
		<< "]" << endl;

	if ( fP->count() < 1 )
	{
		setFirstSync( true );
	}

	return true;
}

KCal::Incidence* VCalConduitBase::addRecord( PilotRecord *r )
{
	FUNCTIONSETUP;

	recordid_t id = fLocalDatabase->writeRecord( r );
	DEBUGKPILOT<<fname<<": Pilot Record ID = " << r->id() << ", backup ID = "
		<< id << endl;

	PilotRecordBase *de = newPilotEntry( r );
	KCal::Incidence*e = 0L;

	if ( de )
	{
		e = fP->findIncidence( r->id() );
		if ( !e )
		{
			// no corresponding entry found, so create, copy and insert it.
			e = newIncidence();
			incidenceFromRecord( e, de );
			fP->addIncidence( e );
			fCtrPC->created();
		}
		else
		{
			// similar entry found, so just copy, no need to insert again
			incidenceFromRecord( e, de );
			fCtrPC->updated();
		}
	}
	KPILOT_DELETE( de );
	return e;
}

int VCalConduitBase::resolveConflict( KCal::Incidence *e, PilotRecordBase *de ) {
	if ( getConflictResolution() == SyncAction::eAskUser )
	{
		// TODO: This is messed up!!!
		TQString query = i18n( "The following item was modified "
			"both on the Handheld and on your PC:\nPC entry:\n\t" );
		query += e->summary();
		query += i18n( "\nHandheld entry:\n\t" );
		query += getTitle( de );
		query += i18n( "\n\nWhich entry do you want to keep? It will "
			"overwrite the other entry." );

		return KMessageBox::No == questionYesNo(
			query,
			i18n( "Conflicting Entries" ),
			TQString(),
			0 /* Never timeout */,
			i18n( "Handheld" ), i18n( "PC" ));
	}
	return getConflictResolution();
}

KCal::Incidence*VCalConduitBase::changeRecord(PilotRecord *r,PilotRecord *)
{
	FUNCTIONSETUP;

	PilotRecordBase *de = newPilotEntry( r );
	KCal::Incidence *e = fP->findIncidence( r->id() );

	DEBUGKPILOT << fname << ": Pilot Record ID: [" << r->id() << "]" << endl;

	if ( e && de )
	{
		// TODO: check for conflict, and if there is one, ask for resolution
		if ( ( e->syncStatus() != KCal::Incidence::SYNCNONE )
			&& r->isModified() )
		{
			// TODO: I have not yet found a way to complete ignore an item
			if (resolveConflict( e, de ) )
			{
				// PC record takes precedence:
				KPILOT_DELETE( de );
				return e;
			}
		}
		// no conflict or conflict resolution says, Palm overwrites, so do it:
		incidenceFromRecord( e, de );

		// NOTE: This MUST be done last, since every other set* call
		// calls updated(), which will trigger an
		// setSyncStatus(SYNCMOD)!!!
		e->setSyncStatus(KCal::Incidence::SYNCNONE);
		fLocalDatabase->writeRecord( r );
	}
	else
	{
		WARNINGKPILOT
			<< "While changing record -- not found in iCalendar" << endl;
		addRecord( r );
	}

	KPILOT_DELETE( de );
	return e;
}


KCal::Incidence*VCalConduitBase::deleteRecord( PilotRecord *r, PilotRecord * )
{
	FUNCTIONSETUP;

	KCal::Incidence *e = fP->findIncidence(r->id());
	if (e)
	{
		// RemoveEvent also takes it out of the calendar.
		fP->removeIncidence(e);
		fCtrPC->deleted();
	}
	fLocalDatabase->writeRecord( r );
	return NULL;
}


void VCalConduitBase::addPalmRecord( KCal::Incidence *e )
{
	FUNCTIONSETUP;

	PilotRecordBase *de = newPilotEntry( 0L );
	updateIncidenceOnPalm( e, de );
	fCtrHH->created();
	KPILOT_DELETE( de );
}


void VCalConduitBase::changePalmRecord(KCal::Incidence*e, PilotRecord*s)
{
	PilotRecordBase *de = newPilotEntry( s );
	updateIncidenceOnPalm( e, de );
	fCtrHH->updated();
	KPILOT_DELETE( de );
}


void VCalConduitBase::deletePalmRecord( KCal::Incidence *e, PilotRecord *s )
{
	FUNCTIONSETUP;
	if ( s )
	{
		DEBUGKPILOT << fname << ": deleting record " << s->id() << endl;
		s->setDeleted();
		fDatabase->writeRecord( s );
		fLocalDatabase->writeRecord( s );
		fCtrHH->deleted();
	}
	else
	{
		DEBUGKPILOT << fname << ": could not find record to delete (";
		DEBUGKPILOT << e->pilotId() << ")" << endl;
	}

	Q_UNUSED(e);
}

/* I have to use a pointer to an existing PilotDateEntry so that I can handle
   new records as well (and to prevent some crashes concerning the validity
   domain of the PilotRecord*r). In syncEvent this PilotDateEntry is created. */
void VCalConduitBase::updateIncidenceOnPalm( KCal::Incidence *e,
	PilotRecordBase *de )
{
	FUNCTIONSETUP;
	if ( !de || !e ) {
		DEBUGKPILOT << fname << ": NULL event given... Skipping it" << endl;
		return;
	}

	if ( e->syncStatus() == KCal::Incidence::SYNCDEL )
	{
		DEBUGKPILOT << fname << ": don't write deleted incidence "
			<< e->summary() << " to the palm" << endl;
		return;
	}

	PilotRecord *r = recordFromIncidence( de, e );

	// TODO: Check for conflict!
	if ( r )
	{
		recordid_t id=fDatabase->writeRecord(r);
		r->setID(id);
//		r->setAttrib(r->getAttrib() & ~dlpRecAttrDeleted);
		fLocalDatabase->writeRecord( r );
//		fDatabase->writeRecord(r);
		e->setPilotId( id );

		// NOTE: This MUST be done last, since every other set* call
		// calls updated(), which will trigger an
		// setSyncStatus(SYNCMOD)!!!
		e->setSyncStatus(KCal::Incidence::SYNCNONE);
		KPILOT_DELETE( r );
	}
}

const TQString VCalConduitBase::dbname()
{
    return TQString();
}

PilotRecord *VCalConduitBase::readRecordByIndex( int index )
{
	FUNCTIONSETUP;
	return fDatabase->readRecordByIndex( index );
}

KCal::Incidence *VCalConduitBase::incidenceFromRecord( PilotRecord *r )
{
	FUNCTIONSETUP;
	PilotRecordBase *pac = newPilotEntry( r );
	KCal::Incidence *i = newIncidence();
	incidenceFromRecord( i, pac );

	KPILOT_DELETE( pac );
	return i;
}

void VCalConduitBase::setState( ConduitState *s )
{
	KPILOT_DELETE( fState );
	fState = s;
}

/* virtual */ void VCalConduitBase::postSync( )
{
	FUNCTIONSETUP;
	if (fCtrPC && fP)
		fCtrPC->setEndCount(fP->count());
}

/* virtual */ void VCalConduitBase::preSync( )
{
	FUNCTIONSETUP;
	if (fCtrPC && fP)
		fCtrPC->setStartCount(fP->count());
}