1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-source-list.c - Test for the ESourceList class.
4 * Copyright (C) 2002 Ximian, Inc.
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.
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.
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.
20 * Author: Ettore Perazzoli <ettore@ximian.com>
28 #include "e-source-list.h"
32 static GMainLoop *main_loop = NULL;
33 static ESourceList *list = NULL;
34 static int idle_dump_id = 0;
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;
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 },
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);
85 dump_property (const gchar *prop, const gchar *value)
87 g_print ("\t\t\t%s: %s\n", prop, value);
91 dump_source (ESource *source)
93 char *uri = e_source_get_uri (source);
94 const gchar *color_spec;
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);
101 color_spec = e_source_peek_color_spec (source);
102 if (color_spec != NULL)
103 g_print ("\t\tcolor: %s\n", color_spec);
105 g_print ("\t\tproperties:\n");
106 e_source_foreach_property (source, (GHFunc) dump_property, NULL);
112 dump_group (ESourceGroup *group)
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));
120 sources = e_source_group_peek_sources (group);
121 for (p = sources; p != NULL; p = p->next) {
122 ESource *source = E_SOURCE (p->data);
124 dump_source (source);
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);
137 groups = e_source_list_peek_groups (list);
138 if (groups == NULL) {
139 g_print ("(No items)\n");
143 for (p = groups; p != NULL; p = p->next)
144 dump_group (E_SOURCE_GROUP (p->data));
149 idle_dump_callback (void *unused_data)
160 if (idle_dump_id == 0)
161 idle_dump_id = g_idle_add (idle_dump_callback, NULL);
166 source_changed_callback (ESource *source)
168 static int count = 0;
170 g_print ("** Event: source \"%s\" changed (%d)\n", e_source_peek_name (source), ++count);
176 group_changed_callback (ESourceGroup *group)
178 static int count = 0;
180 g_print ("** Event: group \"%s\" changed (%d)\n", e_source_group_peek_name (group), ++count);
186 list_changed_callback (ESourceGroup *group)
188 static int count = 0;
190 g_print ("** Event: list changed (%d)\n", ++count);
197 connect_source (ESource *source)
199 g_object_ref (source);
200 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), NULL);
204 connect_group (ESourceGroup *group)
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);
213 sources = e_source_group_peek_sources (group);
214 for (p = sources; p != NULL; p = p->next)
215 connect_source (E_SOURCE (p->data));
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);
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));
233 disconnect_group (ESourceGroup *group)
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);
238 g_object_unref (group);
242 disconnect_source (ESource *source)
244 g_signal_handlers_disconnect_by_func (source, G_CALLBACK (source_changed_callback), NULL);
246 g_object_unref (source);
251 source_added_callback (ESourceGroup *group,
254 static int count = 0;
256 g_print ("** Event: source \"%s\" added (%d)\n", e_source_peek_name (source), ++count);
258 connect_source (source);
263 source_removed_callback (ESourceGroup *group,
266 static int count = 0;
268 g_print ("** Event: source \"%s\" removed (%d)\n", e_source_peek_name (source), ++count);
270 disconnect_source (source);
275 group_added_callback (ESourceList *list,
278 static int count = 0;
280 g_print ("** Event: group \"%s\" added (%d)\n", e_source_group_peek_name (group), ++count);
282 connect_group (group);
287 group_removed_callback (ESourceList *list,
290 static int count = 0;
292 g_print ("** Event: group \"%s\" removed (%d)\n", e_source_group_peek_name (group), ++count);
294 disconnect_group (group);
300 on_idle_do_stuff (void *unused_data)
302 GConfClient *client = gconf_client_get_default ();
303 ESourceGroup *new_group = NULL;
304 ESource *new_source = NULL;
306 list = e_source_list_new_for_gconf (client, key_arg);
307 g_object_unref (client);
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");
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");
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);
323 e_source_list_sync (list, NULL);
326 if (remove_group_arg != NULL) {
329 group = e_source_list_peek_group_by_uid (list, remove_group_arg);
331 fprintf (stderr, "No such group \"%s\".\n", remove_group_arg);
335 e_source_list_remove_group (list, group);
336 e_source_list_sync (list, NULL);
339 if (add_source_arg != NULL) {
342 if (group_arg == NULL && new_group == NULL) {
344 "When using --add-source, you need to specify a group using either --group\n"
345 "or --add-group.\n");
348 if (set_relative_uri_arg == NULL) {
350 "When using --add-source, you need to specify a relative URI using\n"
351 "--set-relative-uri.\n");
355 if (group_arg == NULL) {
358 group = e_source_list_peek_group_by_uid (list, group_arg);
360 fprintf (stderr, "No such group \"%s\".\n", group_arg == NULL ? add_group_arg : group_arg);
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);
370 if (remove_source_arg != NULL) {
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);
379 e_source_list_remove_source_by_uid (list, remove_source_arg);
380 e_source_list_sync (list, NULL);
383 if (set_name_arg != NULL) {
384 if (group_arg == NULL && source_arg == NULL) {
386 "When using --set-name, you need to specify a source (using --source"
387 "alone) or a group (using --group alone).\n");
391 if (source_arg != NULL) {
392 ESource *source = e_source_list_peek_source_by_uid (list, source_arg);
394 if (source != NULL) {
395 e_source_set_name (source, set_name_arg);
397 fprintf (stderr, "No such source \"%s\".\n", source_arg);
401 ESourceGroup *group = e_source_list_peek_group_by_uid (list, group_arg);
404 e_source_group_set_name (group, set_name_arg);
406 fprintf (stderr, "No such group \"%s\".\n", group_arg);
411 e_source_list_sync (list, NULL);
414 if (set_relative_uri_arg != NULL && add_source_arg == NULL) {
417 if (source_arg == NULL) {
419 "When using --set-relative-uri, you need to specify a source using "
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);
429 if (set_color_arg != NULL) {
432 if (add_source_arg == NULL && source_arg == NULL) {
434 "When using --set-color, you need to specify a source using --source\n");
438 if (add_source_arg != NULL)
441 source = e_source_list_peek_source_by_uid (list, source_arg);
443 e_source_set_color_spec (source, set_color_arg);
444 e_source_list_sync (list, NULL);
450 if (add_source_arg == NULL && source_arg == NULL) {
452 "When using --unset-color, you need to specify a source using --source\n");
456 if (add_source_arg != NULL)
459 source = e_source_list_peek_source_by_uid (list, source_arg);
461 e_source_set_color_spec (source, NULL);
462 e_source_list_sync (list, NULL);
465 if (set_base_uri_arg != NULL && add_group_arg == NULL) {
468 if (group_arg == NULL) {
470 "When using --set-base-uri, you need to specify a group using --group.\n");
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);
479 if (set_value_arg != NULL) {
482 if (add_source_arg == NULL && source_arg == NULL) {
484 "When using --set-value, you need to specify a source using --source\n");
488 if (property_arg == NULL) {
490 "When using --set-value, you need to specify a property using --property\n");
494 if (add_source_arg != NULL)
497 source = e_source_list_peek_source_by_uid (list, source_arg);
499 e_source_set_property (source, property_arg, set_value_arg);
500 e_source_list_sync (list, NULL);
506 if (add_source_arg == NULL && source_arg == NULL) {
508 "When using --unset-value, you need to specify a source using --source\n");
512 if (property_arg == NULL) {
514 "When using --unset-value, you need to specify a property using --property\n");
518 if (add_source_arg != NULL)
521 source = e_source_list_peek_source_by_uid (list, source_arg);
523 e_source_set_property (source, property_arg, NULL);
524 e_source_list_sync (list, NULL);
533 g_main_loop_quit (main_loop);
540 main (int argc, char **argv)
542 GOptionContext *context;
543 GError *error = NULL;
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);
549 g_idle_add (on_idle_do_stuff, NULL);
551 main_loop = g_main_loop_new (NULL, TRUE);
552 g_main_loop_run (main_loop);