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>
25 #include "e-source-group.h"
28 #include "e-util-marshal.h"
30 #include <gal/util/e-util.h>
34 #define PARENT_TYPE G_TYPE_OBJECT
35 static GObjectClass *parent_class = NULL;
38 /* Private members. */
40 struct _ESourceGroupPrivate {
47 gboolean ignore_source_changed;
59 static unsigned int signals[LAST_SIGNAL] = { 0 };
65 source_changed_callback (ESource *source,
68 if (! group->priv->ignore_source_changed)
69 g_signal_emit (group, signals[CHANGED], 0);
73 /* GObject methods. */
76 impl_dispose (GObject *object)
78 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
80 if (priv->sources != NULL) {
83 for (p = priv->sources; p != NULL; p = p->next) {
84 ESource *source = E_SOURCE (p->data);
86 g_signal_handlers_disconnect_by_func (source,
87 G_CALLBACK (source_changed_callback),
89 g_object_unref (source);
92 g_slist_free (priv->sources);
96 (* G_OBJECT_CLASS (parent_class)->dispose) (object);
100 impl_finalize (GObject *object)
102 ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv;
106 g_free (priv->base_uri);
109 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
113 /* Initialization. */
116 class_init (ESourceGroupClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
120 object_class->dispose = impl_dispose;
121 object_class->finalize = impl_finalize;
123 parent_class = g_type_class_peek_parent (class);
126 g_signal_new ("changed",
127 G_OBJECT_CLASS_TYPE (object_class),
129 G_STRUCT_OFFSET (ESourceGroupClass, changed),
131 e_util_marshal_VOID__VOID,
134 signals[SOURCE_ADDED] =
135 g_signal_new ("source_added",
136 G_OBJECT_CLASS_TYPE (object_class),
138 G_STRUCT_OFFSET (ESourceGroupClass, source_added),
140 e_util_marshal_VOID__OBJECT,
143 signals[SOURCE_REMOVED] =
144 g_signal_new ("source_removed",
145 G_OBJECT_CLASS_TYPE (object_class),
147 G_STRUCT_OFFSET (ESourceGroupClass, source_removed),
149 e_util_marshal_VOID__OBJECT,
155 init (ESourceGroup *source_group)
157 ESourceGroupPrivate *priv;
159 priv = g_new0 (ESourceGroupPrivate, 1);
160 source_group->priv = priv;
164 /* Public methods. */
167 e_source_group_new (const char *name,
168 const char *base_uri)
172 g_return_val_if_fail (name != NULL, NULL);
173 g_return_val_if_fail (base_uri != NULL, NULL);
175 new = g_object_new (e_source_group_get_type (), NULL);
176 new->priv->uid = e_uid_new ();
178 e_source_group_set_name (new, name);
179 e_source_group_set_base_uri (new, base_uri);
185 e_source_group_new_from_xml (const char *xml)
190 doc = xmlParseDoc ((char *) xml);
194 group = e_source_group_new_from_xmldoc (doc);
201 e_source_group_new_from_xmldoc (xmlDocPtr doc)
207 ESourceGroup *new = NULL;
209 g_return_val_if_fail (doc != NULL, NULL);
211 root = doc->children;
212 if (strcmp (root->name, "group") != 0)
215 uid = xmlGetProp (root, "uid");
216 name = xmlGetProp (root, "name");
217 base_uri = xmlGetProp (root, "base_uri");
219 if (uid == NULL || name == NULL || base_uri == NULL)
222 new = g_object_new (e_source_group_get_type (), NULL);
223 new->priv->uid = g_strdup (uid);
225 e_source_group_set_name (new, name);
226 e_source_group_set_base_uri (new, base_uri);
228 for (p = root->children; p != NULL; p = p->next) {
229 ESource *new_source = e_source_new_from_xml_node (p);
230 e_source_group_add_source (new, new_source, -1);
236 if (base_uri != NULL)
242 e_source_group_update_from_xml (ESourceGroup *group,
244 gboolean *changed_return)
249 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
250 g_return_val_if_fail (xml != NULL, FALSE);
252 xmldoc = xmlParseDoc ((char *) xml);
254 success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return);
262 e_source_group_update_from_xmldoc (ESourceGroup *group,
264 gboolean *changed_return)
266 GHashTable *new_sources_hash;
267 GSList *new_sources_list = NULL;
268 xmlNodePtr root, nodep;
269 xmlChar *name, *base_uri;
270 gboolean changed = FALSE;
273 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
274 g_return_val_if_fail (doc != NULL, FALSE);
276 *changed_return = FALSE;
278 root = doc->children;
279 if (strcmp (root->name, "group") != 0)
282 name = xmlGetProp (root, "name");
286 base_uri = xmlGetProp (root, "base_uri");
287 if (base_uri == NULL) {
292 if (strcmp (group->priv->name, name) != 0) {
293 g_free (group->priv->name);
294 group->priv->name = g_strdup (name);
299 if (strcmp (group->priv->base_uri, base_uri) != 0) {
300 g_free (group->priv->base_uri);
301 group->priv->base_uri = g_strdup (base_uri);
306 new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
308 for (nodep = root->children; nodep != NULL; nodep = nodep->next) {
309 ESource *existing_source;
310 char *uid = e_source_uid_from_xml_node (nodep);
315 existing_source = e_source_group_peek_source_by_uid (group, uid);
316 if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL)
319 if (existing_source == NULL) {
320 ESource *new_source = e_source_new_from_xml_node (nodep);
322 if (new_source != NULL) {
323 e_source_set_group (new_source, group);
324 g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group);
325 new_sources_list = g_slist_prepend (new_sources_list, new_source);
327 g_hash_table_insert (new_sources_hash, new_source, new_source);
329 g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source);
333 gboolean source_changed;
335 group->priv->ignore_source_changed ++;
337 if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) {
338 new_sources_list = g_slist_prepend (new_sources_list, existing_source);
339 g_object_ref (existing_source);
340 g_hash_table_insert (new_sources_hash, existing_source, existing_source);
346 group->priv->ignore_source_changed --;
352 new_sources_list = g_slist_reverse (new_sources_list);
354 /* Emit "group_removed" and disconnect the "changed" signal for all the
355 groups that we haven't found in the new list. */
356 q = new_sources_list;
357 for (p = group->priv->sources; p != NULL; p = p->next) {
358 ESource *source = E_SOURCE (p->data);
360 if (g_hash_table_lookup (new_sources_hash, source) == NULL) {
363 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
364 g_signal_handlers_disconnect_by_func (source, source_changed_callback, group);
367 if (! changed && q != NULL) {
368 if (q->data != p->data)
374 g_hash_table_destroy (new_sources_hash);
376 /* Replace the original group list with the new one. */
377 g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL);
378 g_slist_free (group->priv->sources);
380 group->priv->sources = new_sources_list;
382 /* FIXME if the order changes, the function doesn't notice. */
385 g_signal_emit (group, signals[CHANGED], 0);
386 *changed_return = TRUE;
389 return TRUE; /* Success. */
393 e_source_group_uid_from_xmldoc (xmlDocPtr doc)
395 xmlNodePtr root = doc->children;
399 if (strcmp (root->name, "group") != 0)
402 name = xmlGetProp (root, "uid");
406 retval = g_strdup (name);
412 e_source_group_set_name (ESourceGroup *group,
415 g_return_if_fail (E_IS_SOURCE_GROUP (group));
416 g_return_if_fail (name != NULL);
418 if (group->priv->name == name)
421 g_free (group->priv->name);
422 group->priv->name = g_strdup (name);
424 g_signal_emit (group, signals[CHANGED], 0);
427 void e_source_group_set_base_uri (ESourceGroup *group,
428 const char *base_uri)
430 g_return_if_fail (E_IS_SOURCE_GROUP (group));
431 g_return_if_fail (base_uri != NULL);
433 if (group->priv->base_uri == base_uri)
436 g_free (group->priv->base_uri);
437 group->priv->base_uri = g_strdup (base_uri);
439 g_signal_emit (group, signals[CHANGED], 0);
444 e_source_group_peek_uid (ESourceGroup *group)
446 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
448 return group->priv->uid;
452 e_source_group_peek_name (ESourceGroup *group)
454 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
456 return group->priv->name;
460 e_source_group_peek_base_uri (ESourceGroup *group)
462 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
464 return group->priv->base_uri;
469 e_source_group_peek_sources (ESourceGroup *group)
471 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
473 return group->priv->sources;
477 e_source_group_peek_source_by_uid (ESourceGroup *group,
482 for (p = group->priv->sources; p != NULL; p = p->next) {
483 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
484 return E_SOURCE (p->data);
491 e_source_group_peek_source_by_name (ESourceGroup *group,
496 for (p = group->priv->sources; p != NULL; p = p->next) {
497 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
498 return E_SOURCE (p->data);
505 e_source_group_add_source (ESourceGroup *group,
509 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
511 if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
514 e_source_set_group (source, group);
515 g_object_ref (source);
517 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
519 group->priv->sources = g_slist_insert (group->priv->sources, source, position);
520 g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
521 g_signal_emit (group, signals[CHANGED], 0);
527 e_source_group_remove_source (ESourceGroup *group,
532 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
533 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
535 for (p = group->priv->sources; p != NULL; p = p->next) {
536 if (E_SOURCE (p->data) == source) {
537 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
538 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
539 g_signal_emit (group, signals[CHANGED], 0);
548 e_source_group_remove_source_by_uid (ESourceGroup *group,
553 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
554 g_return_val_if_fail (uid != NULL, FALSE);
556 for (p = group->priv->sources; p != NULL; p = p->next) {
557 ESource *source = E_SOURCE (p->data);
559 if (strcmp (e_source_peek_uid (source), uid) == 0) {
560 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
561 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
562 g_signal_emit (group, signals[CHANGED], 0);
572 e_source_group_to_xml (ESourceGroup *group)
577 char *returned_buffer;
581 doc = xmlNewDoc ("1.0");
583 root = xmlNewDocNode (doc, NULL, "group", NULL);
584 xmlSetProp (root, "uid", e_source_group_peek_uid (group));
585 xmlSetProp (root, "name", e_source_group_peek_name (group));
586 xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group));
588 xmlDocSetRootElement (doc, root);
590 for (p = group->priv->sources; p != NULL; p = p->next)
591 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
593 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
596 returned_buffer = g_malloc (xml_buffer_size + 1);
597 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
598 returned_buffer [xml_buffer_size] = '\0';
599 xmlFree (xml_buffer);
601 return returned_buffer;
605 E_MAKE_TYPE (e_source_group, "ESourceGroup", ESourceGroup, class_init, init, PARENT_TYPE)