summaryrefslogtreecommitdiffstats
path: root/sync/src/pokysync_main.c
blob: 7a78511c92bf60d17b439f17451e9e4b1b82ed10 (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

#include <string.h>
#include <math.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <opensync/opensync.h>
#include <osengine/engine.h>

#include "pokysync.h"


static OSyncGroup *
pokysync_get_backup_group (PokySyncData *data)
{
	OSyncGroup *group;
	OSyncMember *member;
	OSyncError *error = NULL;
	OSyncConfigurationTypes type = NO_CONFIGURATION;

	group = osync_env_find_group (data->osync, BACKUP_GROUP);
	if (group) return group;

	group = osync_group_new (data->osync);
	osync_group_set_name (group, BACKUP_GROUP);
	
	/* Setup Evolution2 sync member */
	member = osync_member_new (group);
	if (!osync_member_instance_plugin (member, EVO2_PLUGIN, &error)) {
		g_warning ("Error instancing Evolution2 plug-in: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		return NULL;
	}
	if (!osync_member_need_config (member, &type, &error)) {
		g_warning ("Error reading plug-in config requirements: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		return NULL;
	}
	if (type == NO_CONFIGURATION) {
		g_warning (
			"Evolution2 plug-in reports no need for configuration");
	} else {
		osync_member_set_config (
			member, EVO2_CONFIG, strlen (EVO2_CONFIG));
	}
	
	/* Setup File sync member */
	member = osync_member_new (group);
	if (!osync_member_instance_plugin (member, BACKUP_PLUGIN, &error)) {
		g_warning ("Error instancing file plug-in: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		return NULL;
	}
	if (!osync_member_need_config (member, &type, &error)) {
		g_warning ("Error reading plug-in config requirements: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		return NULL;
	}
	if (type == NO_CONFIGURATION) {
		g_warning (
			"File sync plug-in reports no need for configuration");
	} else {
		gchar *backup_config, *backup_path;
		
		backup_path = g_strdup_printf (BACKUP_PATH, g_getenv ("HOME"));
		if (g_mkdir_with_parents (backup_path, 0755) == -1) {
			g_warning ("Couldn't create backup directory");
			g_free (backup_path);
			return NULL;
		}

		backup_config = g_strdup_printf (
			BACKUP_CONFIG, backup_path);
		osync_member_set_config (
			member, backup_config, strlen (backup_config));
		g_free (backup_config);
		g_free (backup_path);
	}

	/* Save the group */
	if (!osync_group_save (group, &error)) {
		g_warning ("Error saving group: %s",
			osync_error_print (&error));
		osync_error_free (&error);
	}
	
	return group;
}

static gboolean
pokysync_sync_animate (PokySyncData *data)
{
	static gint direction = 1;
	gfloat x, y;
	GtkWidget *widget = glade_xml_get_widget (data->xml, "sync_image_mid");

	gtk_misc_get_alignment (GTK_MISC (widget), &x, &y);
	x += 0.05 * direction;
	if ((x > 1) || (x < 0)) {
		direction = -direction;
		direction = (gfloat)roundf (direction);
	}
	gtk_misc_set_alignment (GTK_MISC (widget), x, y);
	
	return data->syncing;
}

static const char *
OSyncChangeType2String (OSyncChangeType c)
{
	switch (c) {
		case CHANGE_ADDED: return "ADDED";
		case CHANGE_UNMODIFIED: return "UNMODIFIED";
		case CHANGE_DELETED: return "DELETED";
		case CHANGE_MODIFIED: return "MODIFIED";
		default:
		case CHANGE_UNKNOWN: return "?";
	}
}

static void
pokysync_conflict_cb (OSyncEngine *engine, OSyncMapping *mapping,
		      void *user_data)
{
	GtkWidget *widget;
	PokySyncData *data = (PokySyncData *)user_data;
		
	gdk_threads_enter ();
	widget = glade_xml_get_widget (data->xml, "main_notebook");
	gtk_notebook_set_current_page (GTK_NOTEBOOK (widget), TAB_CONFLICT);
	gdk_threads_leave ();
	
	osengine_mapping_ignore_conflict (engine, mapping);
}

static void
pokysync_changestatus_cb (OSyncEngine *engine, OSyncChangeUpdate *status,
			  void *user_data)
{
	PokySyncData *data = (PokySyncData *)user_data;

	switch (status->type) {
		case CHANGE_RECEIVED_INFO:
			g_debug ("Received a entry %s without data from member "
				"%i. Changetype %s",
				osync_change_get_uid (status->change),
				status->member_id,
				OSyncChangeType2String (
					osync_change_get_changetype (
						status->change)));
			break;
		case CHANGE_RECEIVED:
			g_debug ("Received a entry %s with data of size %i from "
				"member %i. Changetype %s",
				osync_change_get_uid (status->change),
				osync_change_get_datasize (status->change),
				status->member_id,
				OSyncChangeType2String (
					osync_change_get_changetype (
						status->change)));
			break;
		case CHANGE_SENT:
			g_debug ("Sent a entry %s of size %i to member %i. "
				"Changetype %s",
				osync_change_get_uid (status->change),
				osync_change_get_datasize (status->change),
				status->member_id,
				OSyncChangeType2String (
					osync_change_get_changetype (
						status->change)));
			break;
		case CHANGE_WRITE_ERROR:
			g_debug ("Error writing entry %s to member %i: %s",
				osync_change_get_uid (status->change),
				status->member_id,
				osync_error_print (&(status->error)));
			break;
		case CHANGE_RECV_ERROR:
			g_debug ("Error reading entry %s from member %i: %s",
				osync_change_get_uid (status->change),
				status->member_id,
				osync_error_print (&(status->error)));
			break;
	}
}

static void
pokysync_mappingstatus_cb (OSyncMappingUpdate *status, void *user_data)
{
	PokySyncData *data = (PokySyncData *)user_data;

	switch (status->type) {
		case MAPPING_SOLVED:
			g_debug ("Mapping solved");
			break;
		case MAPPING_SYNCED:
			g_debug ("Mapping Synced");
			break;
		case MAPPING_WRITE_ERROR:
			g_debug ("Mapping Write Error: %s",
				osync_error_print (&(status->error)));
			break;
	}
}

static void
pokysync_enginestatus_cb (OSyncEngine *engine, OSyncEngineUpdate *status,
			  void *user_data)
{
	GtkWidget *widget;
	PokySyncData *data = (PokySyncData *)user_data;

	switch (status->type) {
		case ENG_PREV_UNCLEAN:
			g_debug ("The previous synchronization was unclean. "
				"Slow-syncing");
			return;
		case ENG_ENDPHASE_CON:
			g_debug ("All clients connected or error");
			return;
		case ENG_END_CONFLICTS:
			g_debug ("All conflicts have been reported");
			return;
		case ENG_ENDPHASE_READ:
			g_debug ("All clients sent changes or error");
			return;
		case ENG_ENDPHASE_WRITE:
			g_debug ("All clients have written");
			return;
		case ENG_ENDPHASE_DISCON:
			g_debug ("All clients have disconnected");
			return;
		case ENG_SYNC_SUCCESSFULL:
			g_debug ("The sync was successful");
			gdk_threads_enter ();
			widget = glade_xml_get_widget (
				data->xml, "main_notebook");
			gtk_notebook_set_current_page (
				GTK_NOTEBOOK (widget), TAB_SUCCESS);
			gdk_threads_leave ();
			break;
		case ENG_ERROR:
			g_debug ("The sync failed: %s",
				osync_error_print (&(status->error)));
			gdk_threads_enter ();
			widget = glade_xml_get_widget (
				data->xml, "main_notebook");
			gtk_notebook_set_current_page (
				GTK_NOTEBOOK (widget), TAB_ERROR);
			gdk_threads_leave ();
/*			g_debug ("Aborting synchronisation...");
			osengine_abort (engine);*/
			break;
	}

	g_debug ("Cleaning up engine.");
	osengine_finalize (engine);
	osengine_free (engine);
}

static void
pokysync_memberstatus_cb (OSyncMemberUpdate *status, void *user_data)
{
	PokySyncData *data = (PokySyncData *)user_data;

	switch (status->type) {
		case MEMBER_CONNECTED:
			g_debug ("Member %lli of type %s just connected",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member));
			break;
		case MEMBER_DISCONNECTED:
			g_debug ("Member %lli of type %s just disconnected",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member));
			break;
		case MEMBER_SENT_CHANGES:
			g_debug ("Member %lli of type %s just sent all changes",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member));
			break;
		case MEMBER_COMMITTED_ALL:
			g_debug (
				"Member %lli of type %s committed all changes.",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member));
			break;
		case MEMBER_CONNECT_ERROR:
			g_debug ("Member %lli of type %s had an "
				"error while connecting: %s",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member),
				osync_error_print (&(status->error)));
			break;
		case MEMBER_GET_CHANGES_ERROR:
			g_debug ("Member %lli of type %s had an error while "
				"getting changes: %s",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member),
				osync_error_print (&(status->error)));
			break;
		case MEMBER_SYNC_DONE_ERROR:
			g_debug ("Member %lli of type %s had an error while "
				"calling sync done: %s",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member),
				osync_error_print (&(status->error)));
			break;
		case MEMBER_DISCONNECT_ERROR:
			g_debug ("Member %lli of type %s had an error while "
				"disconnecting: %s",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member),
				osync_error_print (&(status->error)));
			break;
		case MEMBER_COMMITTED_ALL_ERROR:
			g_debug ("Member %lli of type %s had an error while "
				"commiting changes: %s",
				osync_member_get_id (status->member),
				osync_member_get_pluginname (status->member),
				osync_error_print (&(status->error)));
			break;
	}
}

static void
pokysync_backup (PokySyncData *data)
{
	OSyncGroup *group;
	OSyncEngine *engine;
	GtkWidget *widget;
	OSyncError *error = NULL;
	
	widget = glade_xml_get_widget (data->xml, "main_notebook");

	if (!(group = pokysync_get_backup_group (data))) {
		g_warning ("Unable to retrieve back-up group");
		gtk_notebook_set_current_page (
			GTK_NOTEBOOK (widget), TAB_ERROR);
		return;
	}
	
	if (!(engine = osengine_new (group, &error))) {
		g_warning ("Error while creating syncengine: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		gtk_notebook_set_current_page (
			GTK_NOTEBOOK (widget), TAB_ERROR);
		return;
	}

/*	osengine_set_message_callback (engine, NULL, NULL);*/
	osengine_set_conflict_callback (
		engine, pokysync_conflict_cb, data);
	osengine_set_changestatus_callback (
		engine, pokysync_changestatus_cb, data);
	osengine_set_mappingstatus_callback (
		engine, pokysync_mappingstatus_cb, data);
	osengine_set_enginestatus_callback (
		engine, pokysync_enginestatus_cb, data);
	osengine_set_memberstatus_callback (
		engine, pokysync_memberstatus_cb, data);

	if (!osengine_init (engine, &error)) {
		g_warning ("Error while initializing syncengine: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		gtk_notebook_set_current_page (
			GTK_NOTEBOOK (widget), TAB_ERROR);
		return;
	}
	
	if (!osengine_synchronize (engine, &error)) {
		g_warning ("Error while starting synchronization: %s",
			osync_error_print (&error));
		osync_error_free (&error);
		gtk_notebook_set_current_page (
			GTK_NOTEBOOK (widget), TAB_ERROR);
		return;
	}

	/* Switch to sync tab */
	gtk_notebook_set_current_page (GTK_NOTEBOOK (widget), TAB_SYNC);
	/* Start sync animation */
	g_debug ("Started synchronisation...");
	data->syncing = TRUE;
	data->animate_id = g_timeout_add (
		100, (GSourceFunc)pokysync_sync_animate, data);
}

static gboolean
pokysync_activate_cursor_item_cb (GtkIconView *iconview, PokySyncData *data)
{
	return FALSE;
}

static void
pokysync_item_activated_cb (GtkIconView *iconview, GtkTreePath *arg1,
	PokySyncData *data)
{
	GtkTreeIter iter;
	
	if (gtk_tree_model_get_iter (
	    GTK_TREE_MODEL (data->model), &iter, arg1)) {
		gint id;
		gtk_tree_model_get (GTK_TREE_MODEL (data->model),
			&iter, COL_ID, &id, -1);

		/* Special case for back-up item */
		if (id == 0) {
			g_debug ("Performing backup");
			pokysync_backup (data);
			
			return;
		}
	}
}

static void
pokysync_sync_button_cb (GtkWidget *source, PokySyncData *data)
{
	GtkWidget *widget = glade_xml_get_widget (data->xml, "main_iconview");
	GList *list = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (widget));
	if (list) {
		pokysync_item_activated_cb (GTK_ICON_VIEW (widget),
			list->data, data);
		g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
		g_list_free (list);
	}
}

static void
pokysync_chooser_cb (GtkWidget *source, PokySyncData *data)
{
	GtkWidget *widget = glade_xml_get_widget (data->xml, "main_notebook");
	gtk_notebook_set_current_page (GTK_NOTEBOOK (widget), TAB_CHOOSE);
}

int
main (int argc, char **argv)
{
	GtkWidget *widget;
	GtkTreeIter iter;
	GtkTreePath *path;
	GtkIconTheme *icon_theme;
	gint width = 0, height = 0;
	OSyncError *error = NULL;
	PokySyncData data;

	gtk_init (&argc, &argv);
	glade_init ();
	g_thread_init (NULL);
	gdk_threads_init ();
	/* Must be called due to buggy OpenSync */
	gconf_client_get_default ();

	data.xml = glade_xml_new (XML_FILE, NULL, NULL);
	if (!data.xml)
		g_error ("Could not find interface XML file '%s'", XML_FILE);

	/* Load icons */
	icon_theme = gtk_icon_theme_get_default ();
	gtk_icon_size_lookup (GTK_ICON_SIZE_DIALOG, &width, &height);
	data.backup_icon = gtk_icon_theme_load_icon (icon_theme,
		GTK_STOCK_FLOPPY, MIN (width, height), 0, NULL);
	data.network_icon = gtk_icon_theme_load_icon (icon_theme,
		GTK_STOCK_NETWORK, MIN (width, height), 0, NULL);

	/* Setup icon-view */
	widget = glade_xml_get_widget (data.xml, "main_iconview");
	data.model = gtk_list_store_new (
		3, G_TYPE_INT, G_TYPE_STRING, GDK_TYPE_PIXBUF);
	gtk_icon_view_set_model (GTK_ICON_VIEW (widget),
				 GTK_TREE_MODEL (data.model));
	gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (widget), COL_PIXBUF);
	gtk_icon_view_set_text_column (GTK_ICON_VIEW (widget), COL_NAME);
	
	/* Insert backup item in icon-view */
	gtk_list_store_insert_with_values (GTK_LIST_STORE (data.model), &iter,
		0, COL_ID, 0, COL_NAME, "Backup",
		COL_PIXBUF, data.backup_icon, -1);
	path = gtk_tree_path_new_first ();
	gtk_icon_view_select_path (GTK_ICON_VIEW (widget), path);
	gtk_tree_path_free (path);
	
	/* Create OpenSync environment */
	data.osync = osync_env_new ();
	osync_env_set_option (data.osync,
		"GROUPS_DIRECTORY", (const char *)NULL);
	osync_env_set_option (data.osync, "LOAD_GROUPS", "TRUE");
	osync_env_set_option (data.osync, "LOAD_PLUGINS", "TRUE");
	osync_env_set_option (data.osync, "LOAD_FORMATS", "TRUE");
	if (!osync_env_initialize (data.osync, &error)) {
		g_error ("Unable to initialise OpenSync environment: %s",
			osync_error_print (&error));
	}

	widget = glade_xml_get_widget (data.xml, "quit_menuitem");
	g_signal_connect (G_OBJECT (widget), "activate",
			  G_CALLBACK (gtk_main_quit), NULL);
	widget = glade_xml_get_widget (data.xml, "main_window");
	g_signal_connect (G_OBJECT (widget), "destroy",
			  G_CALLBACK (gtk_main_quit), NULL);
	widget = glade_xml_get_widget (data.xml, "main_iconview");
	g_signal_connect (G_OBJECT (widget), "activate-cursor-item",
			  G_CALLBACK (pokysync_activate_cursor_item_cb), &data);
	g_signal_connect (G_OBJECT (widget), "item-activated",
			  G_CALLBACK (pokysync_item_activated_cb), &data);
	widget = glade_xml_get_widget (data.xml, "sync_button");
	g_signal_connect (G_OBJECT (widget), "clicked",
			  G_CALLBACK (pokysync_sync_button_cb), &data);
	
	widget = glade_xml_get_widget (data.xml, "success_close_button");
	g_signal_connect (G_OBJECT (widget), "clicked",
			  G_CALLBACK (pokysync_chooser_cb), &data);

	widget = glade_xml_get_widget (data.xml, "error_close_button");
	g_signal_connect (G_OBJECT (widget), "clicked",
			  G_CALLBACK (pokysync_chooser_cb), &data);

	gtk_main ();

	return 0;
}