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 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 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>
28 #include "e-data-server-marshal.h"
30 #include "e-source-group.h"
32 static GObjectClass *parent_class = NULL;
34 /* Private members. */
36 struct _ESourceGroupPrivate {
43 gboolean ignore_source_changed;
56 static unsigned int signals[LAST_SIGNAL] = { 0 };
62 source_changed_callback (ESource *source,
65 if (! group->priv->ignore_source_changed)
66 g_signal_emit (group, signals[CHANGED], 0);
70 /* GObject methods. */
73 impl_dispose (GObject *object)
75 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
77 if (priv->sources != NULL) {
80 for (p = priv->sources; p != NULL; p = p->next) {
81 ESource *source = E_SOURCE (p->data);
83 g_signal_handlers_disconnect_by_func (source,
84 G_CALLBACK (source_changed_callback),
86 g_object_unref (source);
89 g_slist_free (priv->sources);
93 (* G_OBJECT_CLASS (parent_class)->dispose) (object);
97 impl_finalize (GObject *object)
99 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
103 g_free (priv->base_uri);
106 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
110 /* Initialization. */
113 class_init (ESourceGroupClass *class)
115 GObjectClass *object_class = G_OBJECT_CLASS (class);
117 object_class->dispose = impl_dispose;
118 object_class->finalize = impl_finalize;
120 parent_class = g_type_class_peek_parent (class);
123 g_signal_new ("changed",
124 G_OBJECT_CLASS_TYPE (object_class),
126 G_STRUCT_OFFSET (ESourceGroupClass, changed),
128 e_data_server_marshal_VOID__VOID,
131 signals[SOURCE_ADDED] =
132 g_signal_new ("source_added",
133 G_OBJECT_CLASS_TYPE (object_class),
135 G_STRUCT_OFFSET (ESourceGroupClass, source_added),
137 e_data_server_marshal_VOID__OBJECT,
140 signals[SOURCE_REMOVED] =
141 g_signal_new ("source_removed",
142 G_OBJECT_CLASS_TYPE (object_class),
144 G_STRUCT_OFFSET (ESourceGroupClass, source_removed),
146 e_data_server_marshal_VOID__OBJECT,
152 init (ESourceGroup *source_group)
154 ESourceGroupPrivate *priv;
156 priv = g_new0 (ESourceGroupPrivate, 1);
157 source_group->priv = priv;
161 e_source_group_get_type (void)
163 static GType e_source_group_type = 0;
165 if (!e_source_group_type) {
166 static GTypeInfo info = {
167 sizeof (ESourceGroupClass),
168 (GBaseInitFunc) NULL,
169 (GBaseFinalizeFunc) NULL,
170 (GClassInitFunc) class_init,
172 sizeof (ESourceGroup),
174 (GInstanceInitFunc) init
176 e_source_group_type = g_type_register_static (G_TYPE_OBJECT, "ESourceGroup", &info, 0);
179 return e_source_group_type;
182 /* Public methods. */
185 e_source_group_new (const char *name,
186 const char *base_uri)
190 g_return_val_if_fail (name != NULL, NULL);
191 g_return_val_if_fail (base_uri != NULL, NULL);
193 new = g_object_new (e_source_group_get_type (), NULL);
194 new->priv->uid = e_uid_new ();
196 e_source_group_set_name (new, name);
197 e_source_group_set_base_uri (new, base_uri);
203 e_source_group_new_from_xml (const char *xml)
208 doc = xmlParseDoc ((char *) xml);
212 group = e_source_group_new_from_xmldoc (doc);
219 e_source_group_new_from_xmldoc (xmlDocPtr doc)
225 xmlChar *readonly_str;
226 ESourceGroup *new = NULL;
228 g_return_val_if_fail (doc != NULL, NULL);
230 root = doc->children;
231 if (strcmp (root->name, "group") != 0)
234 uid = xmlGetProp (root, "uid");
235 name = xmlGetProp (root, "name");
236 base_uri = xmlGetProp (root, "base_uri");
237 readonly_str = xmlGetProp (root, "readonly");
239 if (uid == NULL || name == NULL || base_uri == NULL)
242 new = g_object_new (e_source_group_get_type (), NULL);
243 new->priv->uid = g_strdup (uid);
245 e_source_group_set_name (new, name);
246 e_source_group_set_base_uri (new, base_uri);
248 for (p = root->children; p != NULL; p = p->next) {
249 ESource *new_source = e_source_new_from_xml_node (p);
251 if (new_source == NULL) {
252 g_object_unref (new);
255 e_source_group_add_source (new, new_source, -1);
258 e_source_group_set_readonly (new, readonly_str && !strcmp (readonly_str, "yes"));
266 if (base_uri != NULL)
268 if (readonly_str != NULL)
269 xmlFree (readonly_str);
274 e_source_group_update_from_xml (ESourceGroup *group,
276 gboolean *changed_return)
281 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
282 g_return_val_if_fail (xml != NULL, FALSE);
284 xmldoc = xmlParseDoc ((char *) xml);
286 success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return);
294 e_source_group_update_from_xmldoc (ESourceGroup *group,
296 gboolean *changed_return)
298 GHashTable *new_sources_hash;
299 GSList *new_sources_list = NULL;
300 xmlNodePtr root, nodep;
301 xmlChar *name, *base_uri, *readonly_str;
303 gboolean changed = FALSE;
306 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
307 g_return_val_if_fail (doc != NULL, FALSE);
309 *changed_return = FALSE;
311 root = doc->children;
312 if (strcmp (root->name, "group") != 0)
315 name = xmlGetProp (root, "name");
319 base_uri = xmlGetProp (root, "base_uri");
320 if (base_uri == NULL) {
325 if (strcmp (group->priv->name, name) != 0) {
326 g_free (group->priv->name);
327 group->priv->name = g_strdup (name);
332 if (strcmp (group->priv->base_uri, base_uri) != 0) {
333 g_free (group->priv->base_uri);
334 group->priv->base_uri = g_strdup (base_uri);
339 readonly_str = xmlGetProp (root, "readonly");
340 readonly = readonly_str && !strcmp (readonly_str, "yes");
341 if (readonly != group->priv->readonly) {
342 group->priv->readonly = readonly;
345 xmlFree (readonly_str);
347 new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
349 for (nodep = root->children; nodep != NULL; nodep = nodep->next) {
350 ESource *existing_source;
351 char *uid = e_source_uid_from_xml_node (nodep);
356 existing_source = e_source_group_peek_source_by_uid (group, uid);
357 if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL)
360 if (existing_source == NULL) {
361 ESource *new_source = e_source_new_from_xml_node (nodep);
363 if (new_source != NULL) {
364 e_source_set_group (new_source, group);
365 g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group);
366 new_sources_list = g_slist_prepend (new_sources_list, new_source);
368 g_hash_table_insert (new_sources_hash, new_source, new_source);
370 g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source);
374 gboolean source_changed;
376 group->priv->ignore_source_changed ++;
378 if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) {
379 new_sources_list = g_slist_prepend (new_sources_list, existing_source);
380 g_object_ref (existing_source);
381 g_hash_table_insert (new_sources_hash, existing_source, existing_source);
387 group->priv->ignore_source_changed --;
393 new_sources_list = g_slist_reverse (new_sources_list);
395 /* Emit "group_removed" and disconnect the "changed" signal for all the
396 groups that we haven't found in the new list. */
397 q = new_sources_list;
398 for (p = group->priv->sources; p != NULL; p = p->next) {
399 ESource *source = E_SOURCE (p->data);
401 if (g_hash_table_lookup (new_sources_hash, source) == NULL) {
404 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
405 g_signal_handlers_disconnect_by_func (source, source_changed_callback, group);
408 if (! changed && q != NULL) {
409 if (q->data != p->data)
415 g_hash_table_destroy (new_sources_hash);
417 /* Replace the original group list with the new one. */
418 g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL);
419 g_slist_free (group->priv->sources);
421 group->priv->sources = new_sources_list;
423 /* FIXME if the order changes, the function doesn't notice. */
426 g_signal_emit (group, signals[CHANGED], 0);
427 *changed_return = TRUE;
430 return TRUE; /* Success. */
434 e_source_group_uid_from_xmldoc (xmlDocPtr doc)
436 xmlNodePtr root = doc->children;
440 if (strcmp (root->name, "group") != 0)
443 name = xmlGetProp (root, "uid");
447 retval = g_strdup (name);
453 e_source_group_set_name (ESourceGroup *group,
456 g_return_if_fail (E_IS_SOURCE_GROUP (group));
457 g_return_if_fail (name != NULL);
459 if (group->priv->readonly)
462 if (group->priv->name == name)
465 g_free (group->priv->name);
466 group->priv->name = g_strdup (name);
468 g_signal_emit (group, signals[CHANGED], 0);
471 void e_source_group_set_base_uri (ESourceGroup *group,
472 const char *base_uri)
474 g_return_if_fail (E_IS_SOURCE_GROUP (group));
475 g_return_if_fail (base_uri != NULL);
477 if (group->priv->readonly)
480 if (group->priv->base_uri == base_uri)
483 g_free (group->priv->base_uri);
484 group->priv->base_uri = g_strdup (base_uri);
486 g_signal_emit (group, signals[CHANGED], 0);
489 void e_source_group_set_readonly (ESourceGroup *group,
494 g_return_if_fail (E_IS_SOURCE_GROUP (group));
496 if (group->priv->readonly)
499 if (group->priv->readonly == readonly)
502 group->priv->readonly = readonly;
503 for (i = group->priv->sources; i != NULL; i = i->next)
504 e_source_set_readonly (E_SOURCE (i->data), readonly);
506 g_signal_emit (group, signals[CHANGED], 0);
510 e_source_group_peek_uid (ESourceGroup *group)
512 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
514 return group->priv->uid;
518 e_source_group_peek_name (ESourceGroup *group)
520 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
522 return group->priv->name;
526 e_source_group_peek_base_uri (ESourceGroup *group)
528 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
530 return group->priv->base_uri;
534 e_source_group_get_readonly (ESourceGroup *group)
536 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
538 return group->priv->readonly;
542 e_source_group_peek_sources (ESourceGroup *group)
544 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
546 return group->priv->sources;
550 e_source_group_peek_source_by_uid (ESourceGroup *group,
555 for (p = group->priv->sources; p != NULL; p = p->next) {
556 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
557 return E_SOURCE (p->data);
564 e_source_group_peek_source_by_name (ESourceGroup *group,
569 for (p = group->priv->sources; p != NULL; p = p->next) {
570 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
571 return E_SOURCE (p->data);
578 e_source_group_add_source (ESourceGroup *group,
582 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
584 if (group->priv->readonly)
587 if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
590 e_source_set_group (source, group);
591 e_source_set_readonly (source, group->priv->readonly);
592 g_object_ref (source);
594 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
596 group->priv->sources = g_slist_insert (group->priv->sources, source, position);
597 g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
598 g_signal_emit (group, signals[CHANGED], 0);
604 e_source_group_remove_source (ESourceGroup *group,
609 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
610 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
612 if (group->priv->readonly)
615 for (p = group->priv->sources; p != NULL; p = p->next) {
616 if (E_SOURCE (p->data) == source) {
617 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
618 g_signal_handlers_disconnect_by_func (source,
619 G_CALLBACK (source_changed_callback),
621 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
622 g_signal_emit (group, signals[CHANGED], 0);
631 e_source_group_remove_source_by_uid (ESourceGroup *group,
636 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
637 g_return_val_if_fail (uid != NULL, FALSE);
639 if (group->priv->readonly)
642 for (p = group->priv->sources; p != NULL; p = p->next) {
643 ESource *source = E_SOURCE (p->data);
645 if (strcmp (e_source_peek_uid (source), uid) == 0) {
646 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
647 g_signal_handlers_disconnect_by_func (source,
648 G_CALLBACK (source_changed_callback),
650 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
651 g_signal_emit (group, signals[CHANGED], 0);
661 e_source_group_to_xml (ESourceGroup *group)
666 char *returned_buffer;
670 doc = xmlNewDoc ("1.0");
672 root = xmlNewDocNode (doc, NULL, "group", NULL);
673 xmlSetProp (root, "uid", e_source_group_peek_uid (group));
674 xmlSetProp (root, "name", e_source_group_peek_name (group));
675 xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group));
676 xmlSetProp (root, "readonly", group->priv->readonly ? "yes" : "no");
678 xmlDocSetRootElement (doc, root);
680 for (p = group->priv->sources; p != NULL; p = p->next)
681 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
683 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
686 returned_buffer = g_malloc (xml_buffer_size + 1);
687 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
688 returned_buffer [xml_buffer_size] = '\0';
689 xmlFree (xml_buffer);
691 return returned_buffer;