1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4 * Copyright (C) 2003 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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ettore Perazzoli <ettore@ximian.com>
29 #include "e-source-group.h"
31 #define XC (const xmlChar *)
32 #define GC (const gchar *)
34 /* Private members. */
36 struct _ESourceGroupPrivate {
43 gboolean ignore_source_changed;
46 GHashTable *properties;
58 static unsigned int signals[LAST_SIGNAL] = { 0 };
64 source_changed_callback (ESource *source,
67 if (! group->priv->ignore_source_changed)
68 g_signal_emit (group, signals[CHANGED], 0);
72 /* GObject methods. */
74 G_DEFINE_TYPE (ESourceGroup, e_source_group, G_TYPE_OBJECT);
77 impl_dispose (GObject *object)
79 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
81 if (priv->sources != NULL) {
84 for (p = priv->sources; p != NULL; p = p->next) {
85 ESource *source = E_SOURCE (p->data);
87 g_signal_handlers_disconnect_by_func (source,
88 G_CALLBACK (source_changed_callback),
90 g_object_unref (source);
93 g_slist_free (priv->sources);
97 (* G_OBJECT_CLASS (e_source_group_parent_class)->dispose) (object);
101 impl_finalize (GObject *object)
103 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
107 g_free (priv->base_uri);
109 g_hash_table_destroy (priv->properties);
113 (* G_OBJECT_CLASS (e_source_group_parent_class)->finalize) (object);
117 /* Initialization. */
120 e_source_group_class_init (ESourceGroupClass *class)
122 GObjectClass *object_class = G_OBJECT_CLASS (class);
124 object_class->dispose = impl_dispose;
125 object_class->finalize = impl_finalize;
128 g_signal_new ("changed",
129 G_OBJECT_CLASS_TYPE (object_class),
131 G_STRUCT_OFFSET (ESourceGroupClass, changed),
133 g_cclosure_marshal_VOID__VOID,
136 signals[SOURCE_ADDED] =
137 g_signal_new ("source_added",
138 G_OBJECT_CLASS_TYPE (object_class),
140 G_STRUCT_OFFSET (ESourceGroupClass, source_added),
142 g_cclosure_marshal_VOID__OBJECT,
145 signals[SOURCE_REMOVED] =
146 g_signal_new ("source_removed",
147 G_OBJECT_CLASS_TYPE (object_class),
149 G_STRUCT_OFFSET (ESourceGroupClass, source_removed),
151 g_cclosure_marshal_VOID__OBJECT,
157 e_source_group_init (ESourceGroup *source_group)
159 ESourceGroupPrivate *priv;
161 priv = g_new0 (ESourceGroupPrivate, 1);
162 source_group->priv = priv;
164 priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
169 import_properties (ESourceGroup *source_group,
170 xmlNodePtr prop_root)
172 ESourceGroupPrivate *priv = source_group->priv;
173 xmlNodePtr prop_node;
175 for (prop_node = prop_root->children; prop_node; prop_node = prop_node->next) {
176 xmlChar *name, *value;
178 if (!prop_node->name || strcmp (GC prop_node->name, "property"))
181 name = xmlGetProp (prop_node, XC "name");
182 value = xmlGetProp (prop_node, XC "value");
185 g_hash_table_insert (priv->properties, g_strdup (GC name), g_strdup (GC value));
202 compare_str_hash (gpointer key, gpointer value, hash_compare_data *cd)
204 gpointer value2 = g_hash_table_lookup (cd->table2, key);
205 if (value2 == NULL || g_str_equal (value, value2) == FALSE)
210 compare_str_hashes (GHashTable *table1, GHashTable *table2)
212 hash_compare_data cd;
214 if (g_hash_table_size (table1) != g_hash_table_size (table2))
219 g_hash_table_foreach (table1, (GHFunc) compare_str_hash, &cd);
224 property_dump_cb (const gchar *key, const gchar *value, xmlNodePtr root)
228 node = xmlNewChild (root, NULL, XC "property", NULL);
229 xmlSetProp (node, XC "name", XC key);
230 xmlSetProp (node, XC "value", XC value);
233 /* Public methods. */
236 e_source_group_new (const char *name,
237 const char *base_uri)
241 g_return_val_if_fail (name != NULL, NULL);
242 g_return_val_if_fail (base_uri != NULL, NULL);
244 new = g_object_new (e_source_group_get_type (), NULL);
245 new->priv->uid = e_uid_new ();
247 e_source_group_set_name (new, name);
248 e_source_group_set_base_uri (new, base_uri);
254 e_source_group_new_from_xml (const char *xml)
259 doc = xmlParseDoc (XC xml);
263 group = e_source_group_new_from_xmldoc (doc);
270 e_source_group_new_from_xmldoc (xmlDocPtr doc)
276 xmlChar *readonly_str;
277 ESourceGroup *new = NULL;
279 g_return_val_if_fail (doc != NULL, NULL);
281 root = doc->children;
282 if (strcmp (GC root->name, "group") != 0)
285 uid = xmlGetProp (root, XC "uid");
286 name = xmlGetProp (root, XC "name");
287 base_uri = xmlGetProp (root, XC "base_uri");
288 readonly_str = xmlGetProp (root, XC "readonly");
290 if (uid == NULL || name == NULL || base_uri == NULL)
293 new = g_object_new (e_source_group_get_type (), NULL);
298 new->priv->uid = g_strdup (GC uid);
300 e_source_group_set_name (new, GC name);
301 e_source_group_set_base_uri (new, GC base_uri);
303 for (p = root->children; p != NULL; p = p->next) {
306 if (p->name && !strcmp (GC p->name, "properties")) {
307 import_properties (new, p);
311 new_source = e_source_new_from_xml_node (p);
313 if (new_source == NULL) {
314 g_object_unref (new);
318 e_source_group_add_source (new, new_source, -1);
321 e_source_group_set_readonly (new, readonly_str && !strcmp (GC readonly_str, "yes"));
329 if (base_uri != NULL)
331 if (readonly_str != NULL)
332 xmlFree (readonly_str);
337 e_source_group_update_from_xml (ESourceGroup *group,
339 gboolean *changed_return)
344 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
345 g_return_val_if_fail (xml != NULL, FALSE);
348 xmldoc = xmlParseDoc (XC xml);
350 success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return);
358 e_source_group_update_from_xmldoc (ESourceGroup *group,
360 gboolean *changed_return)
362 GHashTable *new_sources_hash;
363 GSList *new_sources_list = NULL;
364 xmlNodePtr root, nodep;
365 xmlChar *name, *base_uri, *readonly_str;
367 gboolean changed = FALSE;
370 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
371 g_return_val_if_fail (doc != NULL, FALSE);
373 *changed_return = FALSE;
375 root = doc->children;
376 if (strcmp (GC root->name, "group") != 0)
379 name = xmlGetProp (root, XC "name");
383 base_uri = xmlGetProp (root, XC "base_uri");
384 if (base_uri == NULL) {
389 if (strcmp (group->priv->name, GC name) != 0) {
390 g_free (group->priv->name);
391 group->priv->name = g_strdup (GC name);
396 if (strcmp (group->priv->base_uri, GC base_uri) != 0) {
397 g_free (group->priv->base_uri);
398 group->priv->base_uri = g_strdup (GC base_uri);
403 readonly_str = xmlGetProp (root, XC "readonly");
404 readonly = readonly_str && !strcmp (GC readonly_str, "yes");
405 if (readonly != group->priv->readonly) {
406 group->priv->readonly = readonly;
409 xmlFree (readonly_str);
411 if (g_hash_table_size (group->priv->properties) && !root->children) {
412 g_hash_table_destroy (group->priv->properties);
413 group->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
418 new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
420 for (nodep = root->children; nodep != NULL; nodep = nodep->next) {
421 ESource *existing_source;
427 if (!strcmp (GC nodep->name, "properties")) {
428 GHashTable *temp = group->priv->properties;
429 group->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
431 import_properties (group, nodep);
432 if (!compare_str_hashes (temp, group->priv->properties))
434 g_hash_table_destroy (temp);
438 uid = e_source_uid_from_xml_node (nodep);
442 existing_source = e_source_group_peek_source_by_uid (group, uid);
443 if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL) {
448 if (existing_source == NULL) {
449 ESource *new_source = e_source_new_from_xml_node (nodep);
451 if (new_source != NULL) {
452 e_source_set_group (new_source, group);
453 g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group);
454 new_sources_list = g_slist_prepend (new_sources_list, new_source);
456 g_hash_table_insert (new_sources_hash, new_source, new_source);
458 g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source);
462 gboolean source_changed;
464 group->priv->ignore_source_changed ++;
466 if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) {
467 new_sources_list = g_slist_prepend (new_sources_list, existing_source);
468 g_object_ref (existing_source);
469 g_hash_table_insert (new_sources_hash, existing_source, existing_source);
475 group->priv->ignore_source_changed --;
481 new_sources_list = g_slist_reverse (new_sources_list);
483 /* Emit "group_removed" and disconnect the "changed" signal for all the
484 groups that we haven't found in the new list. */
485 q = new_sources_list;
486 for (p = group->priv->sources; p != NULL; p = p->next) {
487 ESource *source = E_SOURCE (p->data);
489 if (g_hash_table_lookup (new_sources_hash, source) == NULL) {
492 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
493 g_signal_handlers_disconnect_by_func (source, source_changed_callback, group);
496 if (! changed && q != NULL) {
497 if (q->data != p->data)
503 g_hash_table_destroy (new_sources_hash);
505 /* Replace the original group list with the new one. */
506 g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL);
507 g_slist_free (group->priv->sources);
509 group->priv->sources = new_sources_list;
511 /* FIXME if the order changes, the function doesn't notice. */
514 g_signal_emit (group, signals[CHANGED], 0);
515 *changed_return = TRUE;
518 return TRUE; /* Success. */
522 e_source_group_uid_from_xmldoc (xmlDocPtr doc)
524 xmlNodePtr root = doc->children;
528 if (root && root->name) {
529 if (strcmp (GC root->name, "group") != 0)
535 name = xmlGetProp (root, XC "uid");
539 retval = g_strdup (GC name);
545 e_source_group_set_name (ESourceGroup *group,
548 g_return_if_fail (E_IS_SOURCE_GROUP (group));
549 g_return_if_fail (name != NULL);
551 if (group->priv->readonly)
554 if (group->priv->name != NULL &&
555 strcmp (group->priv->name, name) == 0)
558 g_free (group->priv->name);
559 group->priv->name = g_strdup (name);
561 g_signal_emit (group, signals[CHANGED], 0);
564 void e_source_group_set_base_uri (ESourceGroup *group,
565 const char *base_uri)
567 g_return_if_fail (E_IS_SOURCE_GROUP (group));
568 g_return_if_fail (base_uri != NULL);
570 if (group->priv->readonly)
573 if (group->priv->base_uri == base_uri)
576 g_free (group->priv->base_uri);
577 group->priv->base_uri = g_strdup (base_uri);
579 g_signal_emit (group, signals[CHANGED], 0);
582 void e_source_group_set_readonly (ESourceGroup *group,
587 g_return_if_fail (E_IS_SOURCE_GROUP (group));
589 if (group->priv->readonly)
592 if (group->priv->readonly == readonly)
595 group->priv->readonly = readonly;
596 for (i = group->priv->sources; i != NULL; i = i->next)
597 e_source_set_readonly (E_SOURCE (i->data), readonly);
599 g_signal_emit (group, signals[CHANGED], 0);
603 e_source_group_peek_uid (ESourceGroup *group)
605 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
607 return group->priv->uid;
611 e_source_group_peek_name (ESourceGroup *group)
613 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
615 return group->priv->name;
619 e_source_group_peek_base_uri (ESourceGroup *group)
621 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
623 return group->priv->base_uri;
627 e_source_group_get_readonly (ESourceGroup *group)
629 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
631 return group->priv->readonly;
635 e_source_group_peek_sources (ESourceGroup *group)
637 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
639 return group->priv->sources;
643 e_source_group_peek_source_by_uid (ESourceGroup *group,
648 for (p = group->priv->sources; p != NULL; p = p->next) {
649 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
650 return E_SOURCE (p->data);
657 e_source_group_peek_source_by_name (ESourceGroup *group,
662 for (p = group->priv->sources; p != NULL; p = p->next) {
663 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
664 return E_SOURCE (p->data);
671 e_source_group_add_source (ESourceGroup *group,
675 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
677 if (group->priv->readonly)
680 if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
683 e_source_set_group (source, group);
684 e_source_set_readonly (source, group->priv->readonly);
685 g_object_ref (source);
687 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
689 group->priv->sources = g_slist_insert (group->priv->sources, source, position);
690 g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
691 g_signal_emit (group, signals[CHANGED], 0);
697 e_source_group_remove_source (ESourceGroup *group,
702 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
703 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
705 if (group->priv->readonly)
708 for (p = group->priv->sources; p != NULL; p = p->next) {
709 if (E_SOURCE (p->data) == source) {
710 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
711 g_signal_handlers_disconnect_by_func (source,
712 G_CALLBACK (source_changed_callback),
714 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
715 g_signal_emit (group, signals[CHANGED], 0);
724 e_source_group_remove_source_by_uid (ESourceGroup *group,
729 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
730 g_return_val_if_fail (uid != NULL, FALSE);
732 if (group->priv->readonly)
735 for (p = group->priv->sources; p != NULL; p = p->next) {
736 ESource *source = E_SOURCE (p->data);
738 if (strcmp (e_source_peek_uid (source), uid) == 0) {
739 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
740 g_signal_handlers_disconnect_by_func (source,
741 G_CALLBACK (source_changed_callback),
743 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
744 g_signal_emit (group, signals[CHANGED], 0);
754 e_source_group_to_xml (ESourceGroup *group)
759 char *returned_buffer;
763 doc = xmlNewDoc (XC "1.0");
765 root = xmlNewDocNode (doc, NULL, XC "group", NULL);
766 xmlSetProp (root, XC "uid", XC e_source_group_peek_uid (group));
767 xmlSetProp (root, XC "name", XC e_source_group_peek_name (group));
768 xmlSetProp (root, XC "base_uri", XC e_source_group_peek_base_uri (group));
769 xmlSetProp (root, XC "readonly", XC (group->priv->readonly ? "yes" : "no"));
771 if (g_hash_table_size (group->priv->properties) != 0) {
772 xmlNodePtr properties_node;
774 properties_node = xmlNewChild (root, NULL, XC "properties", NULL);
775 g_hash_table_foreach (group->priv->properties, (GHFunc) property_dump_cb, properties_node);
778 xmlDocSetRootElement (doc, root);
780 for (p = group->priv->sources; p != NULL; p = p->next)
781 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
783 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
786 returned_buffer = g_malloc (xml_buffer_size + 1);
787 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
788 returned_buffer [xml_buffer_size] = '\0';
789 xmlFree (xml_buffer);
791 return returned_buffer;
795 e_source_group_get_property (ESourceGroup *source_group,
796 const gchar *property)
798 ESourceGroupPrivate *priv;
800 g_return_val_if_fail (E_IS_SOURCE_GROUP (source_group), NULL);
801 priv = source_group->priv;
803 return g_strdup (g_hash_table_lookup (priv->properties, property));
807 e_source_group_set_property (ESourceGroup *source_group,
808 const gchar *property,
811 ESourceGroupPrivate *priv;
813 g_return_if_fail (E_IS_SOURCE_GROUP (source_group));
814 priv = source_group->priv;
817 g_hash_table_replace (priv->properties, g_strdup (property), g_strdup (value));
819 g_hash_table_remove (priv->properties, property);
821 g_signal_emit (source_group, signals[CHANGED], 0);
825 e_source_group_foreach_property (ESourceGroup *source_group, GHFunc func, gpointer data)
827 ESourceGroupPrivate *priv;
829 g_return_if_fail (E_IS_SOURCE_GROUP (source_group));
830 priv = source_group->priv;
832 g_hash_table_foreach (priv->properties, func, data);