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 != NULL &&
439 strcmp (group->priv->name, name) == 0)
442 g_free (group->priv->name);
443 group->priv->name = g_strdup (name);
445 g_signal_emit (group, signals[CHANGED], 0);
448 void e_source_group_set_base_uri (ESourceGroup *group,
449 const char *base_uri)
451 g_return_if_fail (E_IS_SOURCE_GROUP (group));
452 g_return_if_fail (base_uri != NULL);
454 if (group->priv->readonly)
457 if (group->priv->base_uri == base_uri)
460 g_free (group->priv->base_uri);
461 group->priv->base_uri = g_strdup (base_uri);
463 g_signal_emit (group, signals[CHANGED], 0);
466 void e_source_group_set_readonly (ESourceGroup *group,
471 g_return_if_fail (E_IS_SOURCE_GROUP (group));
473 if (group->priv->readonly)
476 if (group->priv->readonly == readonly)
479 group->priv->readonly = readonly;
480 for (i = group->priv->sources; i != NULL; i = i->next)
481 e_source_set_readonly (E_SOURCE (i->data), readonly);
483 g_signal_emit (group, signals[CHANGED], 0);
487 e_source_group_peek_uid (ESourceGroup *group)
489 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
491 return group->priv->uid;
495 e_source_group_peek_name (ESourceGroup *group)
497 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
499 return group->priv->name;
503 e_source_group_peek_base_uri (ESourceGroup *group)
505 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
507 return group->priv->base_uri;
511 e_source_group_get_readonly (ESourceGroup *group)
513 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
515 return group->priv->readonly;
519 e_source_group_peek_sources (ESourceGroup *group)
521 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL);
523 return group->priv->sources;
527 e_source_group_peek_source_by_uid (ESourceGroup *group,
532 for (p = group->priv->sources; p != NULL; p = p->next) {
533 if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0)
534 return E_SOURCE (p->data);
541 e_source_group_peek_source_by_name (ESourceGroup *group,
546 for (p = group->priv->sources; p != NULL; p = p->next) {
547 if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0)
548 return E_SOURCE (p->data);
555 e_source_group_add_source (ESourceGroup *group,
559 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
561 if (group->priv->readonly)
564 if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL)
567 e_source_set_group (source, group);
568 e_source_set_readonly (source, group->priv->readonly);
569 g_object_ref (source);
571 g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group);
573 group->priv->sources = g_slist_insert (group->priv->sources, source, position);
574 g_signal_emit (group, signals[SOURCE_ADDED], 0, source);
575 g_signal_emit (group, signals[CHANGED], 0);
581 e_source_group_remove_source (ESourceGroup *group,
586 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
587 g_return_val_if_fail (E_IS_SOURCE (source), FALSE);
589 if (group->priv->readonly)
592 for (p = group->priv->sources; p != NULL; p = p->next) {
593 if (E_SOURCE (p->data) == source) {
594 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
595 g_signal_handlers_disconnect_by_func (source,
596 G_CALLBACK (source_changed_callback),
598 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
599 g_signal_emit (group, signals[CHANGED], 0);
608 e_source_group_remove_source_by_uid (ESourceGroup *group,
613 g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE);
614 g_return_val_if_fail (uid != NULL, FALSE);
616 if (group->priv->readonly)
619 for (p = group->priv->sources; p != NULL; p = p->next) {
620 ESource *source = E_SOURCE (p->data);
622 if (strcmp (e_source_peek_uid (source), uid) == 0) {
623 group->priv->sources = g_slist_remove_link (group->priv->sources, p);
624 g_signal_handlers_disconnect_by_func (source,
625 G_CALLBACK (source_changed_callback),
627 g_signal_emit (group, signals[SOURCE_REMOVED], 0, source);
628 g_signal_emit (group, signals[CHANGED], 0);
638 e_source_group_to_xml (ESourceGroup *group)
643 char *returned_buffer;
647 doc = xmlNewDoc ("1.0");
649 root = xmlNewDocNode (doc, NULL, "group", NULL);
650 xmlSetProp (root, "uid", e_source_group_peek_uid (group));
651 xmlSetProp (root, "name", e_source_group_peek_name (group));
652 xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group));
653 xmlSetProp (root, "readonly", group->priv->readonly ? "yes" : "no");
655 xmlDocSetRootElement (doc, root);
657 for (p = group->priv->sources; p != NULL; p = p->next)
658 e_source_dump_to_xml_node (E_SOURCE (p->data), root);
660 xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size);
663 returned_buffer = g_malloc (xml_buffer_size + 1);
664 memcpy (returned_buffer, xml_buffer, xml_buffer_size);
665 returned_buffer [xml_buffer_size] = '\0';
666 xmlFree (xml_buffer);
668 return returned_buffer;