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>
28 #include "e-data-server-marshal.h"
30 #include "e-source-group.h"
32 /* Private members. */
34 struct _ESourceGroupPrivate {
41 gboolean ignore_source_changed;
54 static unsigned int signals[LAST_SIGNAL] = { 0 };
60 source_changed_callback (ESource *source,
63 if (! group->priv->ignore_source_changed)
64 g_signal_emit (group, signals[CHANGED], 0);
68 /* GObject methods. */
70 G_DEFINE_TYPE (ESourceGroup, e_source_group, G_TYPE_OBJECT);
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 (e_source_group_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 (e_source_group_parent_class)->finalize) (object);
110 /* Initialization. */
113 e_source_group_class_init (ESourceGroupClass *class)
115 GObjectClass *object_class = G_OBJECT_CLASS (class);
117 object_class->dispose = impl_dispose;
118 object_class->finalize = impl_finalize;
121 g_signal_new ("changed",
122 G_OBJECT_CLASS_TYPE (object_class),
124 G_STRUCT_OFFSET (ESourceGroupClass, changed),
126 e_data_server_marshal_VOID__VOID,
129 signals[SOURCE_ADDED] =
130 g_signal_new ("source_added",
131 G_OBJECT_CLASS_TYPE (object_class),
133 G_STRUCT_OFFSET (ESourceGroupClass, source_added),
135 e_data_server_marshal_VOID__OBJECT,
138 signals[SOURCE_REMOVED] =
139 g_signal_new ("source_removed",
140 G_OBJECT_CLASS_TYPE (object_class),
142 G_STRUCT_OFFSET (ESourceGroupClass, source_removed),
144 e_data_server_marshal_VOID__OBJECT,
150 e_source_group_init (ESourceGroup *source_group)
152 ESourceGroupPrivate *priv;
154 priv = g_new0 (ESourceGroupPrivate, 1);
155 source_group->priv = priv;
158 /* Public methods. */
161 e_source_group_new (const char *name,
162 const char *base_uri)
166 g_return_val_if_fail (name != NULL, NULL);
167 g_return_val_if_fail (base_uri != NULL, NULL);
169 new = g_object_new (e_source_group_get_type (), NULL);
170 new->priv->uid = e_uid_new ();
172 e_source_group_set_name (new, name);
173 e_source_group_set_base_uri (new, base_uri);
179 e_source_group_new_from_xml (const char *xml)
184 doc = xmlParseDoc ((char *) xml);
188 group = e_source_group_new_from_xmldoc (doc);
195 e_source_group_new_from_xmldoc (xmlDocPtr doc)
201 xmlChar *readonly_str;
202 ESourceGroup *new = NULL;
204 g_return_val_if_fail (doc != NULL, NULL);
206 root = doc->children;
207 if (strcmp (root->name, "group") != 0)
210 uid = xmlGetProp (root, "uid");
211 name = xmlGetProp (root, "name");
212 base_uri = xmlGetProp (root, "base_uri");
213 readonly_str = xmlGetProp (root, "readonly");
215 if (uid == NULL || name == NULL || base_uri == NULL)
218 new = g_object_new (e_source_group_get_type (), NULL);
219 new->priv->uid = g_strdup (uid);
221 e_source_group_set_name (new, name);
222 e_source_group_set_base_uri (new, base_uri);
224 for (p = root->children; p != NULL; p = p->next) {
225 ESource *new_source = e_source_new_from_xml_node (p);
227 if (new_source == NULL) {
228 g_object_unref (new);
231 e_source_group_add_source (new, new_source, -1);
234 e_source_group_set_readonly (new, readonly_str && !strcmp (readonly_str, "yes"));
242 if (base_uri != NULL)
244 if (readonly_str != NULL)
245 xmlFree (readonly_str);
250 e_source_group_update_from_xml (ESourceGroup *group,
252 gboolean *changed_return)
257 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
258 g_return_val_if_fail (xml != NULL, FALSE);
260 xmldoc = xmlParseDoc ((char *) xml);
262 success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return);
270 e_source_group_update_from_xmldoc (ESourceGroup *group,
272 gboolean *changed_return)
274 GHashTable *new_sources_hash;
275 GSList *new_sources_list = NULL;
276 xmlNodePtr root, nodep;
277 xmlChar *name, *base_uri, *readonly_str;
279 gboolean changed = FALSE;
282 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
283 g_return_val_if_fail (doc != NULL, FALSE);
285 *changed_return = FALSE;
287 root = doc->children;
288 if (strcmp (root->name, "group") != 0)
291 name = xmlGetProp (root, "name");
295 base_uri = xmlGetProp (root, "base_uri");
296 if (base_uri == NULL) {
301 if (strcmp (group->priv->name, name) != 0) {
302 g_free (group->priv->name);
303 group->priv->name = g_strdup (name);
308 if (strcmp (group->priv->base_uri, base_uri) != 0) {
309 g_free (group->priv->base_uri);
310 group->priv->base_uri = g_strdup (base_uri);
315 readonly_str = xmlGetProp (root, "readonly");
316 readonly = readonly_str && !strcmp (readonly_str, "yes");
317 if (readonly != group->priv->readonly) {
318 group->priv->readonly = readonly;
321 xmlFree (readonly_str);
323 new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
325 for (nodep = root->children; nodep != NULL; nodep = nodep->next) {
326 ESource *existing_source;
327 char *uid = e_source_uid_from_xml_node (nodep);
332 existing_source = e_source_group_peek_source_by_uid (group, uid);
333 if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL)
336 if (existing_source == NULL) {
337 ESource *new_source = e_source_new_from_xml_node (nodep);
339 if (new_source != NULL) {
340 e_source_set_group (new_source, group);
341 g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group);
342 new_sources_list = g_slist_prepend (new_sources_list, new_source);
344 g_hash_table_insert (new_sources_hash, new_source, new_source);
346 g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source);
350 gboolean source_changed;
352 group->priv->ignore_source_changed ++;
354 if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) {
355 new_sources_list = g_slist_prepend (new_sources_list, existing_source);
356 g_object_ref (existing_source);
357 g_hash_table_insert (new_sources_hash, existing_source, existing_source);
363 group->priv->ignore_source_changed --;
369 new_sources_list = g_slist_reverse (new_sources_list);
371 /* Emit "group_removed" and disconnect the "changed" signal for all the
372 groups that we haven't found in the new list. */
373 q = new_sources_list;
374 for (p = group->priv->sources; p != NULL; p = p->next) {
375 ESource *source = E_SOURCE (p->data);
377 if (g_hash_table_lookup (new_sources_hash, source) == NULL) {
380 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
381 g_signal_handlers_disconnect_by_func (source, source_changed_callback, group);
384 if (! changed && q != NULL) {
385 if (q->data != p->data)
391 g_hash_table_destroy (new_sources_hash);
393 /* Replace the original group list with the new one. */
394 g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL);
395 g_slist_free (group->priv->sources);
397 group->priv->sources = new_sources_list;
399 /* FIXME if the order changes, the function doesn't notice. */
402 g_signal_emit (group, signals[CHANGED], 0);
403 *changed_return = TRUE;
406 return TRUE; /* Success. */
410 e_source_group_uid_from_xmldoc (xmlDocPtr doc)
412 xmlNodePtr root = doc->children;
416 if (strcmp (root->name, "group") != 0)
419 name = xmlGetProp (root, "uid");
423 retval = g_strdup (name);
429 e_source_group_set_name (ESourceGroup *group,
432 g_return_if_fail (E_IS_SOURCE_GROUP (group));
433 g_return_if_fail (name != NULL);
435 if (group->priv->readonly)
438 if (group->priv->name == name)
441 g_free (group->priv->name);
442 group->priv->name = g_strdup (name);
444 g_signal_emit (group, signals[CHANGED], 0);
447 void e_source_group_set_base_uri (ESourceGroup *group,
448 const char *base_uri)
450 g_return_if_fail (E_IS_SOURCE_GROUP (group));
451 g_return_if_fail (base_uri != NULL);
453 if (group->priv->readonly)
456 if (group->priv->base_uri == base_uri)
459 g_free (group->priv->base_uri);
460 group->priv->base_uri = g_strdup (base_uri);
462 g_signal_emit (group, signals[CHANGED], 0);
465 void e_source_group_set_readonly (ESourceGroup *group,
470 g_return_if_fail (E_IS_SOURCE_GROUP (group));
472 if (group->priv->readonly)
475 if (group->priv->readonly == readonly)
478 group->priv->readonly = readonly;
479 for (i = group->priv->sources; i != NULL; i = i->next)
480 e_source_set_readonly (E_SOURCE (i->data), readonly);
482 g_signal_emit (group, signals[CHANGED], 0);
486 e_source_group_peek_uid (ESourceGroup *group)
488 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
490 return group->priv->uid;
494 e_source_group_peek_name (ESourceGroup *group)
496 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
498 return group->priv->name;
502 e_source_group_peek_base_uri (ESourceGroup *group)
504 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
506 return group->priv->base_uri;
510 e_source_group_get_readonly (ESourceGroup *group)
512 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
514 return group->priv->readonly;
518 e_source_group_peek_sources (ESourceGroup *group)
520 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
522 return group->priv->sources;
526 e_source_group_peek_source_by_uid (ESourceGroup *group,
531 for (p = group->priv->sources; p != NULL; p = p->next) {
532 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
533 return E_SOURCE (p->data);
540 e_source_group_peek_source_by_name (ESourceGroup *group,
545 for (p = group->priv->sources; p != NULL; p = p->next) {
546 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
547 return E_SOURCE (p->data);
554 e_source_group_add_source (ESourceGroup *group,
558 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
560 if (group->priv->readonly)
563 if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
566 e_source_set_group (source, group);
567 e_source_set_readonly (source, group->priv->readonly);
568 g_object_ref (source);
570 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
572 group->priv->sources = g_slist_insert (group->priv->sources, source, position);
573 g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
574 g_signal_emit (group, signals[CHANGED], 0);
580 e_source_group_remove_source (ESourceGroup *group,
585 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
586 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
588 if (group->priv->readonly)
591 for (p = group->priv->sources; p != NULL; p = p->next) {
592 if (E_SOURCE (p->data) == source) {
593 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
594 g_signal_handlers_disconnect_by_func (source,
595 G_CALLBACK (source_changed_callback),
597 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
598 g_signal_emit (group, signals[CHANGED], 0);
607 e_source_group_remove_source_by_uid (ESourceGroup *group,
612 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
613 g_return_val_if_fail (uid != NULL, FALSE);
615 if (group->priv->readonly)
618 for (p = group->priv->sources; p != NULL; p = p->next) {
619 ESource *source = E_SOURCE (p->data);
621 if (strcmp (e_source_peek_uid (source), uid) == 0) {
622 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
623 g_signal_handlers_disconnect_by_func (source,
624 G_CALLBACK (source_changed_callback),
626 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
627 g_signal_emit (group, signals[CHANGED], 0);
637 e_source_group_to_xml (ESourceGroup *group)
642 char *returned_buffer;
646 doc = xmlNewDoc ("1.0");
648 root = xmlNewDocNode (doc, NULL, "group", NULL);
649 xmlSetProp (root, "uid", e_source_group_peek_uid (group));
650 xmlSetProp (root, "name", e_source_group_peek_name (group));
651 xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group));
652 xmlSetProp (root, "readonly", group->priv->readonly ? "yes" : "no");
654 xmlDocSetRootElement (doc, root);
656 for (p = group->priv->sources; p != NULL; p = p->next)
657 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
659 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
662 returned_buffer = g_malloc (xml_buffer_size + 1);
663 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
664 returned_buffer [xml_buffer_size] = '\0';
665 xmlFree (xml_buffer);
667 return returned_buffer;