summaryrefslogtreecommitdiffstats
path: root/kpilot/tests/mergecalendars.cc
blob: 898eb8c060cad830b70df2421e8df5e19b60a33d (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
/* mergecalendars			KPilot
**
** Copyright (C) 2007 by Jason 'vanRijn' Kasper <vR@movingparts.net)
**
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser 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 <kaboutdata.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include <kconfigskeleton.h>

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

#include "options.h"

static const KCmdLineOptions options[] =
{
	{"korgfile <path>","KOrganizer master file", 0},
	{"newfile <path>","Calendar file to merge into korganizer", 0},
	{"category <string>","Category to remove from 'korgfile' and to add to events in 'newfile' for synch purposes", 0},
	{"verbose", "Verbose debugging", 0},
	KCmdLineLastOption
};



int main(int argc, char **argv)
{

	KApplication::disableAutoDcopRegistration();

	KAboutData aboutData("mergecalendars","Merge libkcal Calendars","0.1");
	KCmdLineArgs::init(argc,argv,&aboutData);
	KCmdLineArgs::addCmdLineOptions( options );

	KApplication app( false, false );

	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

	int debug_level= (args->isSet("verbose")) ? 4 : 0;

	TQString korgfile = args->getOption("korgfile");
	TQString newfile = args->getOption("newfile");
	TQString category = args->getOption("category");

	if (korgfile.isEmpty())
	{
		WARNINGKPILOT << "! Must provide a korganizer file." << endl;
	}
	if (newfile.isEmpty())
	{
		WARNINGKPILOT << "! Must provide a newfile file." << endl;
	}
	if (category.isEmpty())
	{
		WARNINGKPILOT << "! Must provide a category to use." << endl;
	}
	if (korgfile.isEmpty() || newfile.isEmpty() || category.isEmpty())
	{
		return 1;
	}

	TQString korgsave = TQString("%1.updated").arg(korgfile);
	TQString newfilesave = TQString("%1.updated").arg(newfile);

	DEBUGKPILOT << "Using korgfile: [" << korgfile 
		<< "]" << endl;
	DEBUGKPILOT << "Using newfile: [" << newfile
		<< "]" << endl;
	DEBUGKPILOT << "Will save korgfile to: [" << korgsave 
		<< "]" << endl;
	DEBUGKPILOT << "Will save newfile to: [" << newfilesave
		<< "]" << endl << endl;

	KCal::CalendarLocal *calkorg = new KCal::CalendarLocal( TQString::fromLatin1("UTC") );
	KCal::CalendarLocal *calnew = new KCal::CalendarLocal( TQString::fromLatin1("UTC") );
	if (!calkorg || !calnew)
	{
		WARNINGKPILOT << "Unable to create base calendar objects." << endl;
		return 1;
	}

	if (!calkorg->load(korgfile) || !calnew->load(newfile))
	{
		WARNINGKPILOT << "Unable to load calendar files." << endl;
		return 1;
	}

	int numkorgstart = calkorg->incidences().count();
	int numnewstart = calnew->incidences().count();

	DEBUGKPILOT << "  - Opened korganizer calendar with: [" 
		<< numkorgstart << "] incidences." << endl;
	DEBUGKPILOT << "  - Opened newfile calendar with: [" 
		<< numnewstart << "] incidences." << endl;

	KCal::Event::List korgEvents;
	KCal::Event::List::ConstIterator korgIt;
	korgEvents = calkorg->events();
	korgEvents.setAutoDelete(false);

	KCal::Event::List newEvents;
	KCal::Event::List::ConstIterator newIt;
	newEvents = calnew->events();
	newEvents.setAutoDelete(false);

	DEBUGKPILOT << "Looking for previous pilot ids for exchange events..." << endl;

	// iterate through all events and try to find a korganizer event
	// that matches up with this external event's UID
	unsigned int numkorgpilotids = 0;
	KCal::Event *ev = 0;
	for (newIt = newEvents.begin(); newIt != newEvents.end(); ++newIt ) 
	{
		ev = *newIt;
		TQString uid = ev->uid();
		if (debug_level)
			DEBUGKPILOT << "  - Looking at event: [" 
			<< ev->summary() << "], uid: ["
			<< uid << "]" << endl;

		KCal::Event * evkorg = calkorg->event(uid);
		if ( evkorg && (evkorg->pilotId() > 0) )
		{
			unsigned long pilotId = evkorg->pilotId();

			if (debug_level)
				DEBUGKPILOT << "Found korg event for uid: ["
				<< uid << "], pilotId: [" 
				<< pilotId << "]" << endl;

			ev->setPilotId(pilotId);
			ev->setSyncStatus(KCal::Incidence::SYNCMOD);

			++numkorgpilotids;
		}
	}

	DEBUGKPILOT << "Matched: [" << numkorgpilotids << "] events."<< endl;

	DEBUGKPILOT << "Now searching for previous events of category: [" << category << "] in korganizer's calendar." << endl;

	// iterate through all events and try to find a korganizer event
	// that matches up with this external event's UID
	unsigned int numkorgremoved = 0;

	TQString categoryToken = category;

	// careful iterating and removing...
	KCal::Event *next = 0;

	korgIt = korgEvents.begin();
	for ( ev = *korgIt; ev != 0; ev = next )
	{
		if (++korgIt == korgEvents.end())
		{
			next = 0;
		}
		else
		{
			next = *korgIt;
		}

		if (ev->categoriesStr().contains(categoryToken))
		{
			if (debug_level)
				DEBUGKPILOT << "  - Found matching event: [" 
				<< ev->summary() << "], uid: ["
				<< ev->uid() << "]. Removing." << endl;

			korgEvents.remove(ev);
			calkorg->deleteEvent(ev);

			++numkorgremoved;
		}
	}

	DEBUGKPILOT << "  - Found: [" << numkorgremoved
		<< "] prior: [" << categoryToken 
		<< "] category events." << endl;
	
	DEBUGKPILOT << "Merging new events into korganizer calendar..." 
		<< endl;

	for (newIt = newEvents.begin(); newIt != newEvents.end(); ++newIt ) 
	{
		ev = *newIt;
		ev->setCategories(category);
		korgEvents.append(ev);
		calkorg->addEvent(ev);
	}

	DEBUGKPILOT << "Ended up with: [" << korgEvents.count() 
		<< "] events in korganizer calendar." << endl;

	DEBUGKPILOT << "Saving updated korganizer file..." << endl;
	calkorg->save(korgsave);

	DEBUGKPILOT << "Saving updated newfile file..." << endl;
	calnew->save(newfilesave);

	return 0;
}