Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / libedataserver / test-source-list.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-source-list.c - Test for the ESourceList class.
3  *
4  * Copyright (C) 2002 Ximian, Inc.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * Author: Ettore Perazzoli <ettore@ximian.com>
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <glib.h>
28 #include "e-source-list.h"
29
30 /* Globals. */
31
32 static GMainLoop *main_loop = NULL;
33 static ESourceList *list = NULL;
34 static int idle_dump_id = 0;
35
36
37 /* Options.  */
38
39 static gboolean listen = FALSE;
40 static gboolean dump = FALSE;
41 static char *key_arg = "/apps/evolution/test/source_list";
42 static char *source_arg = NULL;
43 static char *group_arg = NULL;
44 static char *add_group_arg = NULL;
45 static char *add_source_arg = NULL;
46 static char *remove_group_arg = NULL;
47 static char *remove_source_arg = NULL;
48 static char *property_arg = NULL;
49 static char *set_name_arg = NULL;
50 static char *set_base_uri_arg = NULL;
51 static char *set_relative_uri_arg = NULL;
52 static char *set_color_arg = NULL;
53 static char *set_value_arg = NULL;
54 static gboolean unset_value = FALSE;
55 static gboolean unset_color = FALSE;
56
57 static GOptionEntry entries[] = {
58         { "key", '\0', 0, G_OPTION_ARG_STRING, &key_arg, "Name of the GConf key to use", "PATH" },
59         { "source", '\0', 0, G_OPTION_ARG_STRING, &source_arg, "UID of source to apply operations too", "UID"},
60         { "group", '\0', 0, G_OPTION_ARG_STRING, &group_arg, "UID of group to apply operations too", "UID" },
61         { "add-group", '\0', 0, G_OPTION_ARG_STRING, &add_group_arg, "Add group of specified name", "GROUP" },
62         { "add-source", '\0', 0, G_OPTION_ARG_STRING, &add_source_arg, "Add source of specified name", "SOURCE" },
63         { "remove-group", '\0', 0, G_OPTION_ARG_STRING, &remove_group_arg, "Remove group of specified name", "GROUP" },
64         { "remove-source", '\0', 0, G_OPTION_ARG_STRING, &remove_source_arg, "Remove source of specified name", "SOURCE" },
65         { "property", '\0', 0, G_OPTION_ARG_STRING, &property_arg, "Name of source property to apply operation to", "PROPERTY" },
66         { "set-name", '\0', 0, G_OPTION_ARG_STRING, &set_name_arg, "Set name of source or group.  When used with --group, it sets the name of a group. When used with both --group and --source, it sets the name of a source.", "NAME" },
67         { "set-relative-uri", '\0', 0, G_OPTION_ARG_STRING, &set_relative_uri_arg, "Set relative URI of a source.  Use with --source or --add-source.", "NAME" },
68         { "set-base-uri", '\0', 0, G_OPTION_ARG_STRING, &set_base_uri_arg, "Set base URI of a group.  Use with --group or --add-group.", "NAME" },
69         { "set-color", '\0', 0, G_OPTION_ARG_STRING, &set_color_arg, "Set the color of a source.  Use with --source or --add-source.", "COLOR (rrggbb)" },
70         { "unset-color", '\0', 0, G_OPTION_ARG_NONE, &unset_color, "Unset the color of a source.  Use with --source or --add-source.", NULL },
71         { "set-value", '\0', 0, G_OPTION_ARG_STRING, &set_value_arg, "Set a property on a source.  Use with --source and --property.", NULL },
72         { "unset-value", '\0', 0, G_OPTION_ARG_NONE, &unset_value, "Unset a property on a source.  Use with --source and --property.", NULL },
73         { "listen", '\0', 0, G_OPTION_ARG_NONE, &listen, "Wait and listen for changes.", NULL },
74         { "dump", '\0', 0, G_OPTION_ARG_NONE, &dump, "List the current configured sources.", NULL },
75         { NULL }
76 };
77
78 /* Forward decls.  */
79 static void group_added_callback (ESourceList *list, ESourceGroup *group);
80 static void group_removed_callback (ESourceList *list, ESourceGroup *group);
81 static void source_added_callback (ESourceGroup *group, ESource *source);
82 static void source_removed_callback (ESourceGroup *group, ESource *source);
83
84 static void
85 dump_property (const gchar *prop, const gchar *value)
86 {
87         g_print ("\t\t\t%s: %s\n", prop, value);
88 }
89
90 static void
91 dump_source (ESource *source)
92 {
93         char *uri = e_source_get_uri (source);
94         const gchar *color_spec;
95
96         g_print ("\tSource %s\n", e_source_peek_uid (source));
97         g_print ("\t\tname: %s\n", e_source_peek_name (source));
98         g_print ("\t\trelative_uri: %s\n", e_source_peek_relative_uri (source));
99         g_print ("\t\tabsolute_uri: %s\n", uri);
100
101         color_spec = e_source_peek_color_spec (source);
102         if (color_spec != NULL)
103                 g_print ("\t\tcolor: %s\n", color_spec);
104
105         g_print ("\t\tproperties:\n");
106         e_source_foreach_property (source, (GHFunc) dump_property, NULL);
107
108         g_free (uri);
109 }
110
111 static void
112 dump_group (ESourceGroup *group)
113 {
114         GSList *sources, *p;
115
116         g_print ("Group %s\n", e_source_group_peek_uid (group));
117         g_print ("\tname: %s\n", e_source_group_peek_name (group));
118         g_print ("\tbase_uri: %s\n", e_source_group_peek_base_uri (group));
119
120         sources = e_source_group_peek_sources (group);
121         for (p = sources; p != NULL; p = p->next) {
122                 ESource *source = E_SOURCE (p->data);
123
124                 dump_source (source);
125
126                 if (e_source_peek_group (source) != group)
127                         g_warning ("\t\t** ERROR ** parent pointer is %p, should be %p",
128                                    e_source_peek_group (source), group);
129         }
130 }
131
132 static void
133 dump_list (void)
134 {
135         GSList *groups, *p;
136
137         groups = e_source_list_peek_groups (list);
138         if (groups == NULL) {
139                 g_print ("(No items)\n");
140                 return;
141         }
142
143         for (p = groups; p != NULL; p = p->next)
144                 dump_group (E_SOURCE_GROUP (p->data));
145 }
146
147
148 static int
149 idle_dump_callback (void *unused_data)
150 {
151         dump_list ();
152         idle_dump_id = 0;
153
154         return FALSE;
155 }
156
157 static void
158 dump_on_idle (void)
159 {
160         if (idle_dump_id == 0)
161                 idle_dump_id = g_idle_add (idle_dump_callback, NULL);
162 }
163
164
165 static void
166 source_changed_callback (ESource *source)
167 {
168         static int count = 0;
169
170         g_print ("** Event: source \"%s\" changed (%d)\n", e_source_peek_name (source), ++count);
171
172         dump_on_idle ();
173 }
174
175 static void
176 group_changed_callback (ESourceGroup *group)
177 {
178         static int count = 0;
179
180         g_print ("** Event: group \"%s\" changed (%d)\n", e_source_group_peek_name (group), ++count);
181
182         dump_on_idle ();
183 }
184
185 static void
186 list_changed_callback (ESourceGroup *group)
187 {
188         static int count = 0;
189
190         g_print ("** Event: list changed (%d)\n", ++count);
191
192         dump_on_idle ();
193 }
194
195
196 static void
197 connect_source (ESource *source)
198 {
199         g_object_ref (source);
200         g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), NULL);
201 }
202
203 static void
204 connect_group (ESourceGroup *group)
205 {
206         GSList *sources, *p;
207
208         g_object_ref (group);
209         g_signal_connect (group, "changed", G_CALLBACK (group_changed_callback), NULL);
210         g_signal_connect (group, "source_added", G_CALLBACK (source_added_callback), NULL);
211         g_signal_connect (group, "source_removed", G_CALLBACK (source_removed_callback), NULL);
212
213         sources = e_source_group_peek_sources (group);
214         for (p = sources; p != NULL; p = p->next)
215                 connect_source (E_SOURCE (p->data));
216 }
217
218 static void
219 connect_list (void)
220 {
221         GSList *groups, *p;
222
223         g_signal_connect (list, "changed", G_CALLBACK (list_changed_callback), NULL);
224         g_signal_connect (list, "group_added", G_CALLBACK (group_added_callback), NULL);
225         g_signal_connect (list, "group_removed", G_CALLBACK (group_removed_callback), NULL);
226
227         groups = e_source_list_peek_groups (list);
228         for (p = groups; p != NULL; p = p->next)
229                 connect_group (E_SOURCE_GROUP (p->data));
230 }
231
232 static void
233 disconnect_group (ESourceGroup *group)
234 {
235         g_signal_handlers_disconnect_by_func (group, G_CALLBACK (group_changed_callback), NULL);
236         g_signal_handlers_disconnect_by_func (group, G_CALLBACK (source_added_callback), NULL);
237
238         g_object_unref (group);
239 }
240
241 static void
242 disconnect_source (ESource *source)
243 {
244         g_signal_handlers_disconnect_by_func (source, G_CALLBACK (source_changed_callback), NULL);
245
246         g_object_unref (source);
247 }
248
249
250 static void
251 source_added_callback (ESourceGroup *group,
252                        ESource *source)
253 {
254         static int count = 0;
255
256         g_print ("** Event: source \"%s\" added (%d)\n", e_source_peek_name (source), ++count);
257
258         connect_source (source);
259         dump_on_idle ();
260 }
261
262 static void
263 source_removed_callback (ESourceGroup *group,
264                          ESource *source)
265 {
266         static int count = 0;
267
268         g_print ("** Event: source \"%s\" removed (%d)\n", e_source_peek_name (source), ++count);
269
270         disconnect_source (source);
271         dump_on_idle ();
272 }
273
274 static void
275 group_added_callback (ESourceList *list,
276                       ESourceGroup *group)
277 {
278         static int count = 0;
279
280         g_print ("** Event: group \"%s\" added (%d)\n", e_source_group_peek_name (group), ++count);
281
282         connect_group (group);
283         dump_on_idle ();
284 }
285
286 static void
287 group_removed_callback (ESourceList *list,
288                         ESourceGroup *group)
289 {
290         static int count = 0;
291
292         g_print ("** Event: group \"%s\" removed (%d)\n", e_source_group_peek_name (group), ++count);
293
294         disconnect_group (group);
295         dump_on_idle ();
296 }
297
298
299 static int
300 on_idle_do_stuff (void *unused_data)
301 {
302         GConfClient *client = gconf_client_get_default ();
303         ESourceGroup *new_group = NULL;
304         ESource *new_source = NULL;
305
306         list = e_source_list_new_for_gconf (client, key_arg);
307         g_object_unref (client);
308
309         if (add_group_arg != NULL) {
310                 if (group_arg != NULL) {
311                         fprintf (stderr, "--add-group and --group cannot be used at the same time.\n");
312                         exit (1);
313                 }
314                 if (set_base_uri_arg == NULL) {
315                         fprintf (stderr, "When using --add-group, you need to specify a base URI using --set-base-uri.\n");
316                         exit (1);
317                 }
318
319                 new_group = e_source_group_new (add_group_arg, set_base_uri_arg);
320                 e_source_list_add_group (list, new_group, -1);
321                 g_object_unref (new_group);
322
323                 e_source_list_sync (list, NULL);
324         }
325
326         if (remove_group_arg != NULL) {
327                 ESourceGroup *group;
328
329                 group = e_source_list_peek_group_by_uid (list, remove_group_arg);
330                 if (group == NULL) {
331                         fprintf (stderr, "No such group \"%s\".\n", remove_group_arg);
332                         exit (1);
333                 }
334
335                 e_source_list_remove_group (list, group);
336                 e_source_list_sync (list, NULL);
337         }
338
339         if (add_source_arg != NULL) {
340                 ESourceGroup *group;
341
342                 if (group_arg == NULL && new_group == NULL) {
343                         fprintf (stderr,
344                                  "When using --add-source, you need to specify a group using either --group\n"
345                                  "or --add-group.\n");
346                         exit (1);
347                 }
348                 if (set_relative_uri_arg == NULL) {
349                         fprintf (stderr,
350                                  "When using --add-source, you need to specify a relative URI using\n"
351                                  "--set-relative-uri.\n");
352                         exit (1);
353                 }
354
355                 if (group_arg == NULL) {
356                         group = new_group;
357                 } else {
358                         group = e_source_list_peek_group_by_uid (list, group_arg);
359                         if (group == NULL) {
360                                 fprintf (stderr, "No such group \"%s\".\n", group_arg == NULL ? add_group_arg : group_arg);
361                                 exit (1);
362                         }
363                 }
364
365                 new_source = e_source_new (add_source_arg, set_relative_uri_arg);
366                 e_source_group_add_source (group, new_source, -1);
367                 e_source_list_sync (list, NULL);
368         }
369
370         if (remove_source_arg != NULL) {
371                 ESource *source;
372
373                 source = e_source_list_peek_source_by_uid (list, remove_source_arg);
374                 if (source == NULL) {
375                         fprintf (stderr, "No such source \"%s\".\n", remove_source_arg);
376                         exit (1);
377                 }
378
379                 e_source_list_remove_source_by_uid (list, remove_source_arg);
380                 e_source_list_sync (list, NULL);
381         }
382
383         if (set_name_arg != NULL) {
384                 if (group_arg == NULL && source_arg == NULL) {
385                         fprintf (stderr,
386                                  "When using --set-name, you need to specify a source (using --source"
387                                  "alone) or a group (using --group alone).\n");
388                         exit (1);
389                 }
390
391                 if (source_arg != NULL) {
392                         ESource *source = e_source_list_peek_source_by_uid (list, source_arg);
393
394                         if (source != NULL) {
395                                 e_source_set_name (source, set_name_arg);
396                         } else {
397                                 fprintf (stderr, "No such source \"%s\".\n", source_arg);
398                                 exit (1);
399                         }
400                 } else {
401                         ESourceGroup *group = e_source_list_peek_group_by_uid (list, group_arg);
402
403                         if (group != NULL) {
404                                 e_source_group_set_name (group, set_name_arg);
405                         } else {
406                                 fprintf (stderr, "No such group \"%s\".\n", group_arg);
407                                 exit (1);
408                         }
409                 } 
410
411                 e_source_list_sync (list, NULL);
412         }
413
414         if (set_relative_uri_arg != NULL && add_source_arg == NULL) {
415                 ESource *source;
416
417                 if (source_arg == NULL) {
418                         fprintf (stderr,
419                                  "When using --set-relative-uri, you need to specify a source using " 
420                                  "--source.\n");
421                         exit (1);
422                 }
423
424                 source = e_source_list_peek_source_by_uid (list, source_arg);
425                 e_source_set_relative_uri (source, set_relative_uri_arg);
426                 e_source_list_sync (list, NULL);
427         }
428
429         if (set_color_arg != NULL) {
430                 ESource *source;
431
432                 if (add_source_arg == NULL && source_arg == NULL) {
433                         fprintf (stderr,
434                                  "When using --set-color, you need to specify a source using --source\n");
435                         exit (1);
436                 }
437
438                 if (add_source_arg != NULL)
439                         source = new_source;
440                 else
441                         source = e_source_list_peek_source_by_uid (list, source_arg);
442
443                 e_source_set_color_spec (source, set_color_arg);
444                 e_source_list_sync (list, NULL);
445         }
446
447         if (unset_color) {
448                 ESource *source;
449
450                 if (add_source_arg == NULL && source_arg == NULL) {
451                         fprintf (stderr,
452                                  "When using --unset-color, you need to specify a source using --source\n");                    
453                         exit (1);
454                 }
455
456                 if (add_source_arg != NULL)
457                         source = new_source;
458                 else
459                         source = e_source_list_peek_source_by_uid (list, source_arg);
460
461                 e_source_set_color_spec (source, NULL);
462                 e_source_list_sync (list, NULL);
463         }
464
465         if (set_base_uri_arg != NULL && add_group_arg == NULL) {
466                 ESourceGroup *group;
467
468                 if (group_arg == NULL) {
469                         fprintf (stderr,
470                                  "When using --set-base-uri, you need to specify a group using --group.\n");
471                         exit (1);
472                 }
473
474                 group = e_source_list_peek_group_by_uid (list, group_arg);
475                 e_source_group_set_base_uri (group, set_base_uri_arg);
476                 e_source_list_sync (list, NULL);
477         }
478
479         if (set_value_arg != NULL) {
480                 ESource *source;
481
482                 if (add_source_arg == NULL && source_arg == NULL) {
483                         fprintf (stderr,
484                                  "When using --set-value, you need to specify a source using --source\n");
485                         exit (1);
486                 }
487
488                 if (property_arg == NULL) {
489                         fprintf (stderr,
490                                  "When using --set-value, you need to specify a property using --property\n");
491                         exit (1);
492                 }
493
494                 if (add_source_arg != NULL)
495                         source = new_source;
496                 else
497                         source = e_source_list_peek_source_by_uid (list, source_arg);
498
499                 e_source_set_property (source, property_arg, set_value_arg);
500                 e_source_list_sync (list, NULL);
501         }
502
503         if (unset_value) {
504                 ESource *source;
505
506                 if (add_source_arg == NULL && source_arg == NULL) {
507                         fprintf (stderr,
508                                  "When using --unset-value, you need to specify a source using --source\n");
509                         exit (1);
510                 }
511
512                 if (property_arg == NULL) {
513                         fprintf (stderr,
514                                  "When using --unset-value, you need to specify a property using --property\n");
515                         exit (1);
516                 }
517
518                 if (add_source_arg != NULL)
519                         source = new_source;
520                 else
521                         source = e_source_list_peek_source_by_uid (list, source_arg);
522
523                 e_source_set_property (source, property_arg, NULL);
524                 e_source_list_sync (list, NULL);
525         }
526
527         connect_list ();
528
529         if (dump)
530                 dump_list ();
531
532         if (! listen)
533                 g_main_loop_quit (main_loop);
534
535         return FALSE;
536 }
537
538
539 int
540 main (int argc, char **argv)
541 {
542         GOptionContext *context;
543         GError *error = NULL;
544
545         context = g_option_context_new ("- test source lists");
546         g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
547         g_option_context_parse (context, &argc, &argv, &error);
548
549         g_idle_add (on_idle_do_stuff, NULL);
550
551         main_loop = g_main_loop_new (NULL, TRUE);
552         g_main_loop_run (main_loop);
553
554         return 0;
555 }