summaryrefslogtreecommitdiffstats
path: root/conduits/vcalconduit/todo-conduit.cc
blob: 8a04f152aa262498f3cc0255b64b3cf5baa444fd (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
/* Todo-Conduit for syncing KPilot and KOrganizer
**
** Copyright (C) 2002-2003 Reinhold Kainhofer
** Copyright (C) 1998-2001 Dan Pilone
** Copyright (C) 1998-2000 Preston Brown <[email protected]>
** Copyright (C) 1998 Herwin-Jan Steehouwer
** Copyright (C) 2001 Cornelius Schumacher
**
** This file is part of the todo conduit, a conduit for KPilot that
** synchronises the Pilot's todo application with the outside world,
** which currently means KOrganizer.
*/

/*
** 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 [email protected]
*/

#include "options.h"

#include <tqdatetime.h>
#include <tqtextcodec.h>

#include <libkcal/calendar.h>
#include <libkcal/todo.h>

#include <pilotLocalDatabase.h>

#include "todo-conduit.moc"
#include "vcalconduitSettings.h"
#include "todo-factory.h"

#include "kcalRecord.h"
#include "todoRecord.h"

// define conduit versions, one for the version when categories were synced for the first time, and the current version number
#define CONDUIT_VERSION_CATEGORYSYNC 10
#define CONDUIT_VERSION 10

extern "C"
{
unsigned long version_conduit_todo = Pilot::PLUGIN_API;
}


TodoConduitPrivate::TodoConduitPrivate(KCal::Calendar *b) :
	VCalConduitPrivateBase(b)
{
	fAllTodos.setAutoDelete(false);
}

void TodoConduitPrivate::addIncidence(KCal::Incidence*e)
{
	fAllTodos.append(static_cast<KCal::Todo*>(e));
	fCalendar->addTodo(static_cast<KCal::Todo*>(e));
}

int TodoConduitPrivate::updateIncidences()
{
	fAllTodos = fCalendar->todos();
	fAllTodos.setAutoDelete(false);
	return fAllTodos.count();
}


void TodoConduitPrivate::removeIncidence(KCal::Incidence *e)
{
	fAllTodos.remove(static_cast<KCal::Todo*>(e));
	if (!fCalendar) return;
	fCalendar->deleteTodo(static_cast<KCal::Todo*>(e));
	// now just in case we're in the middle of reading through our list
	// and we delete something, set reading to false so we start at the
	// top again next time and don't have problems with our iterator
	reading = false;
}



KCal::Incidence *TodoConduitPrivate::findIncidence(recordid_t id)
{
	KCal::Todo::List::ConstIterator it;
        for( it = fAllTodos.begin(); it != fAllTodos.end(); ++it ) {
                KCal::Todo *todo = *it;
		if ((recordid_t)(todo->pilotId()) == id) return todo;
	}

	return 0L;
}



KCal::Incidence *TodoConduitPrivate::findIncidence(PilotRecordBase *tosearch)
{
	PilotTodoEntry*entry=dynamic_cast<PilotTodoEntry*>(tosearch);
	if (!entry) return 0L;

	TQString title=entry->getDescription();
	TQDateTime dt=readTm( entry->getDueDate() );

	KCal::Todo::List::ConstIterator it;
        for( it = fAllTodos.begin(); it != fAllTodos.end(); ++it ) {
                KCal::Todo *event = *it;
		if ( (event->dtDue().date() == dt.date()) && (event->summary() == title) ) return event;
	}
	return 0L;
}



KCal::Incidence *TodoConduitPrivate::getNextIncidence()
{
	FUNCTIONSETUP;
	if (reading) {
		++fAllTodosIterator;
	}
	else {
		reading=true;
		fAllTodosIterator = fAllTodos.begin();
	}

	return(fAllTodosIterator == fAllTodos.end()) ? 0L : *fAllTodosIterator;
}



KCal::Incidence *TodoConduitPrivate::getNextModifiedIncidence()
{
	FUNCTIONSETUP;
	KCal::Todo*e=0L;
	if (!reading)
	{
		reading=true;
		fAllTodosIterator = fAllTodos.begin();
	}
	else
	{
		++fAllTodosIterator;
	}
	if ( fAllTodosIterator != fAllTodos.end() ) e=*fAllTodosIterator;
	while (fAllTodosIterator != fAllTodos.end() &&
		e && e->synctqStatus()!=KCal::Incidence::SYNCMOD && e->pilotId())
	{
		e = (++fAllTodosIterator != fAllTodos.end()) ? *fAllTodosIterator : 0L;

#ifdef DEBUG
	if(e)
		DEBUGKPILOT<< e->summary()<<" had SyncStatus="<<e->synctqStatus()<<endl;
#endif

	}

	return (fAllTodosIterator == fAllTodos.end()) ? 0L : *fAllTodosIterator;
}



/****************************************************************************
 *                          TodoConduit class                               *
 ****************************************************************************/

TodoConduit::TodoConduit(KPilotLink *d,
	const char *n,
	const TQStringList &a) : VCalConduitBase(d,n,a),
	fTodoAppInfo( 0L )
{
	FUNCTIONSETUP;
	fConduitName=i18n("To-do");
}



TodoConduit::~TodoConduit()
{
//	FUNCTIONSETUP;
}



void TodoConduit::_setAppInfo()
{
	FUNCTIONSETUP;
	// get the address application header information

	if( !fTodoAppInfo )
	{
		DEBUGKPILOT << fname << ": fTodoAppInfo is NULL" << endl;
		return;
	}
	if( !fDatabase )
	{
		DEBUGKPILOT << fname << ": fDatabase is NULL" << endl;
		return;
	}

	fTodoAppInfo->writeTo(fDatabase);
}

void TodoConduit::_getAppInfo()
{
	FUNCTIONSETUP;
	// get the address application header information

	KPILOT_DELETE( fTodoAppInfo );
	fTodoAppInfo = new PilotToDoInfo(fDatabase);
	fTodoAppInfo->dump();
}



const TQString TodoConduit::getTitle(PilotRecordBase *de)
{
	PilotTodoEntry*d=dynamic_cast<PilotTodoEntry*>(de);
	if (d)
	{
		return TQString(d->getDescription());
	}
	return TQString();
}



void TodoConduit::readConfig()
{
	FUNCTIONSETUP;
	VCalConduitBase::readConfig();
	// determine if the categories have ever been synce. Needed to prevent loosing
	// the categories on the desktop. Also use a full sync for the first time to
	// make sure the palm categories are really transferred to the desktop.
	//
	categoriesSynced = config()->conduitVersion()>=CONDUIT_VERSION_CATEGORYSYNC;
	if (!categoriesSynced && !isFullSync() )
	{
		changeSync(SyncMode::eFullSync);
	}
	DEBUGKPILOT<<"categoriesSynced=" << categoriesSynced << endl;
}

void TodoConduit::preSync()
{
	FUNCTIONSETUP;
	VCalConduitBase::preSync();
	_getAppInfo();
}

void TodoConduit::postSync()
{
	FUNCTIONSETUP;
	VCalConduitBase::postSync();
	// after this successful sync the categories have been synced for sure
	config()->setConduitVersion( CONDUIT_VERSION );
	config()->writeConfig();
	_setAppInfo();
}



PilotRecord *TodoConduit::recordFromIncidence(PilotRecordBase *de, const KCal::Incidence *e)
{
	FUNCTIONSETUP;

	if (!de || !e)
	{
		DEBUGKPILOT << fname
			<< ": got NULL entry or NULL incidence." << endl;
		return 0L;
	}

	PilotTodoEntry *todoEntry = dynamic_cast<PilotTodoEntry*>(de);
	if (!todoEntry)
	{
		// Secretly wasn't a todo entry after all
		return 0L;
	}

	const KCal::Todo *todo = dynamic_cast<const KCal::Todo *>(e);
	if (!todo)
	{
		DEBUGKPILOT << fname << ": Incidence is not a todo." << endl;
		return 0L;
	}

	// don't need to check for null pointers here, the recordFromIncidence(PTE*, KCal::Todo*) will do that.
	if (KCalSync::setTodoEntry(todoEntry,todo,*fTodoAppInfo->categoryInfo()))
	{
		return todoEntry->pack();
	}
	else
	{
		return 0L;
	}
}

KCal::Incidence *TodoConduit::incidenceFromRecord(KCal::Incidence *e, const PilotRecordBase *de)
{
	FUNCTIONSETUP;

	if (!de || !e)
	{
		DEBUGKPILOT << fname
			<< ": Got NULL entry or NULL incidence." << endl;
		return 0L;
	}

	const PilotTodoEntry *todoEntry = dynamic_cast<const PilotTodoEntry *>(de);
	if (!todoEntry)
	{
		DEBUGKPILOT << fname << ": HH record not a todo entry." << endl;
		return 0L;
	}

	KCal::Todo *todo = dynamic_cast<KCal::Todo *>(e);
	if (!todo)
	{
		DEBUGKPILOT << fname << ": Incidence is not a todo." << endl;
		return 0L;
	}

	KCalSync::setTodo(todo, todoEntry,*fTodoAppInfo->categoryInfo());
	return e;
}





void TodoConduit::preRecord(PilotRecord*r)
{
	FUNCTIONSETUP;
	if (!categoriesSynced && r)
	{
		const PilotRecordBase *de = newPilotEntry(r);
		KCal::Incidence *e = fP->findIncidence(r->id());
		KCalSync::setCategory(dynamic_cast<KCal::Todo*>(e),
			dynamic_cast<const PilotTodoEntry*>(de),
			*fTodoAppInfo->categoryInfo());
	}
}






static VCalConduitSettings *config_vcal = 0L;

VCalConduitSettings *TodoConduit::theConfig() {
	if (!config_vcal)
	{
		config_vcal = new VCalConduitSettings(CSL1("Calendar"));
	}

	return config_vcal;
}

VCalConduitSettings *TodoConduit::config() {
	return theConfig();
}