diff options
Diffstat (limited to 'e-util')
-rw-r--r-- | e-util/.cvsignore | 1 | ||||
-rw-r--r-- | e-util/ChangeLog | 143 | ||||
-rw-r--r-- | e-util/Makefile.am | 14 | ||||
-rw-r--r-- | e-util/e-account-list.c | 3 | ||||
-rw-r--r-- | e-util/e-account-list.h | 1 | ||||
-rw-r--r-- | e-util/e-account.c | 29 | ||||
-rw-r--r-- | e-util/e-account.h | 1 | ||||
-rw-r--r-- | e-util/e-source-group.c | 605 | ||||
-rw-r--r-- | e-util/e-source-group.h | 102 | ||||
-rw-r--r-- | e-util/e-source-list.c | 524 | ||||
-rw-r--r-- | e-util/e-source-list.h | 88 | ||||
-rw-r--r-- | e-util/e-source.c | 458 | ||||
-rw-r--r-- | e-util/e-source.h | 91 | ||||
-rw-r--r-- | e-util/e-uid.c | 61 | ||||
-rw-r--r-- | e-util/e-uid.h | 28 | ||||
-rw-r--r-- | e-util/test-source-list.c | 524 |
16 files changed, 2646 insertions, 27 deletions
diff --git a/e-util/.cvsignore b/e-util/.cvsignore index ea3b8c5ad4..cfb049f0e3 100644 --- a/e-util/.cvsignore +++ b/e-util/.cvsignore @@ -6,3 +6,4 @@ Makefile.in *.la e-util-marshal.c e-util-marshal.h +test-source-list
\ No newline at end of file diff --git a/e-util/ChangeLog b/e-util/ChangeLog index fb28623bee..b6bdedd004 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -3,6 +3,21 @@ * Makefile.am (pilot_compile) [! ENABLE_PILOT_CONDUITS]: Add md5-utils.c so it compiles even if you have no Pilot support. +2003-10-16 Rodrigo Moya <rodrigo@ximian.com> + + * e-source-list.c (e_source_list_sync): use gconf_client_notify_remove + instead of g_source_remove for GConf notification IDs. + (impl_finalize): remove the GConf notification also here. + +2003-10-13 Rodrigo Moya <rodrigo@ximian.com> + + * e-source-group.[ch] (e_source_group_peek_source_by_name): added + new function. + +2003-10-10 Not Zed <NotZed@Ximian.com> + + * e-account-list.c (e_account_list_find): add FIND_UID find type. + 2003-09-26 Jeffrey Stedfast <fejj@ximian.com> * e-host-utils.c (e_gethostbyaddr_r): IPv6 implementation @@ -21,6 +36,11 @@ * e-host-utils.c (e_gethostbyaddr_r): Work around a bug in glibc 2.3.2's gethostbyaddr_r() implementation. +2003-09-15 Larry Ewing <lewing@ximian.com> + + * e-source.c (e_source_dump_to_xml_node): make sure declarations + precede the body. + 2003-09-11 Dan Winship <danw@ximian.com> * Makefile.am (noinst_LTLIBRARIES): Remove libeutil-static.la and @@ -47,6 +67,129 @@ Editor always gives time validation error for apptmnts in non UTF-8/non ASCII locales. +2003-08-17 Ettore Perazzoli <ettore@ximian.com> + + * test-source-list.c: No short letter for --key. + +2003-08-15 Ettore Perazzoli <ettore@ximian.com> + + * e-uid.c: #include <glib/gstrfuncs.h> + + * test-source-list.c: Add options to display, set and unset the + color as well. + + * e-source.c: New members has_color, color in struct + ESourcePrivate. + (e_source_update_from_xml_node): Parse a color property from the + XML node. Protect from NULL name and relative_uri members as + well. + (e_source_dump_to_xml_node): Set a color property on the XML node. + (e_source_get_color): New. + (e_source_set_color): New. + (e_source_unset_color): New. + (e_source_new_from_xml_node): Use e_source_update_from_xml_node() + instead of getting the data from the XML yourself. + +2003-08-14 Ettore Perazzoli <ettore@ximian.com> + + Add UIDs to ESource* items so we can distinguish renames from + removals/additions. + + * test-source-list.c: Made --source and --group get UIDs instead + of names. + (on_idle_do_stuff): Updated behavior accordingly. + (dump_list): Print "(No items)" if there are no groups. + (dump_group): Print the UID of the group. + (dump_source): Print the UID of the source. + + * e-source-list.c (load_from_gconf): Match with group UIDs instead + of group names. + (e_source_list_peek_source_by_uid): New. + (e_source_list_peek_source_by_name): Removed. + (e_source_list_peek_group_by_uid): New. + (e_source_list_peek_group_by_name): Removed. + (e_source_list_remove_group_by_uid): New. + (e_source_list_remove_group_by_name): Removed. + (e_source_list_remove_source_by_uid): New. + (e_source_list_remove_source_by_name): Removed. + + * e-source-group.c: New member uid in struct ESourceGroupPrivate. + (impl_finalize): Free it. + (e_source_group_new): Set the uid member using e_uid_new(). + (e_source_group_peek_source_by_uid): New. + (e_source_group_peek_source_by_name): Removed. + (e_source_group_add_source): Check that the UID is unique, instead + of the name. + (e_source_group_remove_source_by_uid): New. + (e_source_group_remove_source_by_name): Removed. + (e_source_group_update_from_xmldoc): Use the UID to figure out + which source has changed, instead of the name. + (e_source_group_uid_from_xmldoc): New. + (e_source_group_name_from_xmldoc): Removed. + (e_source_group_new_from_xmldoc): Set the UID in the new group + from the XML. + (e_source_group_to_xml): Set a UID property in the XML. + + * e-source.c: New member uid in struct ESourcePrivate. + (e_source_new): Initialize using e_uid_new(). + (impl_finalize): Free. + (e_source_peek_uid): New. + (e_source_new_from_xml_node): Set the UID from the XML node. + (e_source_name_from_xml_node): Removed. + (e_source_uid_from_xml_node): New. + (e_source_dump_to_xml_node): Set the "uid" property on the XML + node. + + * e-account.c (e_account_gen_uid): Removed. + (e_account_new): Use e_uid_new() instead of e_account_gen_uid(). + + * e-uid.c (e_uid_new): New file, new function. + +2003-08-13 Ettore Perazzoli <ettore@ximian.com> + + Fix up the semantics of "changed" signals on GConf changes. + + * e-source-group.c (e_source_group_update_from_xmldoc): Added new + member ignore_source_changed in struct _ESourceGroupPrivate. + (e_source_group_update_from_xmldoc): Increment + ignore_source_changed before calling + e_source_update_from_xml_node(), decrement afterwards. + (source_changed_callback): Only emit "changed" if + ignore_source_changed is zero. + (e_source_group_update_from_xmldoc): Properly emit the "changed" + signal when the base_uri or the name change. + + * e-source-list.c: Changed type of sync_idle_id from gboolean (!) + to int and added new member ignore_group_changed in struct + _ESourceListPrivate. + (load_from_gconf): Increment ignore_group_changed before calling + e_source_group_update_from_xmldoc() and decrement it afterwards. + (group_changed_callback): Only emit the signal if + ignore_group_changed is zero. + + * e-source.c (e_source_update_from_xml_node): Removed arg + emit_signals. Always emit signals. + + * e-source-group.c (e_source_group_update_from_xmldoc): Removed + arg emit_signals. Always emit signals. + (e_source_group_update_from_xml): Likewise. + +2003-08-11 Ettore Perazzoli <ettore@ximian.com> + + * e-source.c (e_source_set_group): Weak_unref the current group if + not NULL and properly handle the case where a NULL group is being + passed in. + +2003-08-11 Ettore Perazzoli <ettore@ximian.com> + + * e-source-group.c: New file. + * e-source-group.h: New file. + * e-source-list.h: New file. + * e-source-list.c: New file. + * e-source.c: New file. + * e-source.h: New file. + * test-source-list.c: New file to test the above. + 2003-08-11 Not Zed <NotZed@Ximian.com> * e-msgport.c (e_thread_put): check pthread_create return code diff --git a/e-util/Makefile.am b/e-util/Makefile.am index 2a134e14ed..f80e321f0e 100644 --- a/e-util/Makefile.am +++ b/e-util/Makefile.am @@ -44,8 +44,12 @@ eutilinclude_HEADERS = \ e-proxy.h \ e-request.h \ e-sexp.h \ + e-source-group.h \ + e-source-list.h \ + e-source.h \ e-time-utils.h \ e-trie.h \ + e-uid.h \ e-url.h \ e-xml-hash-utils.h \ md5-utils.h @@ -78,8 +82,12 @@ libeutil_la_SOURCES = \ e-proxy.c \ e-request.c \ e-sexp.c \ + e-source-group.c \ + e-source-list.c \ + e-source.c \ e-time-utils.c \ e-trie.c \ + e-uid.c \ e-url.c \ e-util-marshal.c \ e-xml-hash-utils.c \ @@ -87,6 +95,12 @@ libeutil_la_SOURCES = \ eggtrayicon.h \ md5-utils.c +noinst_PROGRAMS = \ + test-source-list + +test_source_list_SOURCES = test-source-list.c +test_source_list_LDADD = libeutil.la + MARSHAL_GENERATED = e-util-marshal.c e-util-marshal.h @EVO_MARSHAL_RULE@ diff --git a/e-util/e-account-list.c b/e-util/e-account-list.c index acb9093cb4..d0463a810b 100644 --- a/e-util/e-account-list.c +++ b/e-util/e-account-list.c @@ -447,6 +447,9 @@ e_account_list_find(EAccountList *accounts, e_account_find_t type, const char *k case E_ACCOUNT_FIND_NAME: found = strcmp(account->name, key) == 0; break; + case E_ACCOUNT_FIND_UID: + found = strcmp(account->uid, key) == 0; + break; case E_ACCOUNT_FIND_ID_NAME: if (account->id) found = strcmp(account->id->name, key) == 0; diff --git a/e-util/e-account-list.h b/e-util/e-account-list.h index 2ad0896307..b47efb42ae 100644 --- a/e-util/e-account-list.h +++ b/e-util/e-account-list.h @@ -35,6 +35,7 @@ typedef struct EAccountListPrivate EAccountListPrivate; /* search options for the find command */ typedef enum _e_account_find_t { E_ACCOUNT_FIND_NAME, + E_ACCOUNT_FIND_UID, E_ACCOUNT_FIND_ID_NAME, E_ACCOUNT_FIND_ID_ADDRESS, } e_account_find_t; diff --git a/e-util/e-account.c b/e-util/e-account.c index 510200b723..b6e3cdc57f 100644 --- a/e-util/e-account.c +++ b/e-util/e-account.c @@ -23,9 +23,9 @@ #include "e-account.h" +#include "e-uid.h" + #include <string.h> -#include <time.h> -#include <unistd.h> #include <gal/util/e-util.h> #include <libxml/parser.h> @@ -106,29 +106,6 @@ finalize (GObject *object) E_MAKE_TYPE (e_account, "EAccount", EAccount, class_init, init, PARENT_TYPE) -char * -e_account_gen_uid (void) -{ - static char *hostname; - static int serial; - - if (!hostname) { - static char buffer [512]; - - if ((gethostname (buffer, sizeof (buffer) - 1) == 0) && - (buffer [0] != 0)) - hostname = buffer; - else - hostname = "localhost"; - } - - return g_strdup_printf ("%lu.%lu.%d@%s", - (unsigned long) time (NULL), - (unsigned long) getpid (), - serial++, - hostname); -} - /** * e_account_new: * @@ -141,7 +118,7 @@ e_account_new (void) EAccount *account; account = g_object_new (E_TYPE_ACCOUNT, NULL); - account->uid = e_account_gen_uid (); + account->uid = e_uid_new (); return account; } diff --git a/e-util/e-account.h b/e-util/e-account.h index 78eb70b7a8..ff937b3905 100644 --- a/e-util/e-account.h +++ b/e-util/e-account.h @@ -100,6 +100,5 @@ char *e_account_to_xml (EAccount *account); char *e_account_uid_from_xml (const char *xml); -char *e_account_gen_uid (void); #endif /* __E_ACCOUNT__ */ diff --git a/e-util/e-source-group.c b/e-util/e-source-group.c new file mode 100644 index 0000000000..5fb5a97b23 --- /dev/null +++ b/e-util/e-source-group.c @@ -0,0 +1,605 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source-group.c + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#include <config.h> + +#include "e-source-group.h" + +#include "e-uid.h" +#include "e-util-marshal.h" + +#include <gal/util/e-util.h> + +#include <string.h> + +#define PARENT_TYPE G_TYPE_OBJECT +static GObjectClass *parent_class = NULL; + + +/* Private members. */ + +struct _ESourceGroupPrivate { + char *uid; + char *name; + char *base_uri; + + GSList *sources; + + gboolean ignore_source_changed; +}; + + +/* Signals. */ + +enum { + CHANGED, + SOURCE_REMOVED, + SOURCE_ADDED, + LAST_SIGNAL +}; +static unsigned int signals[LAST_SIGNAL] = { 0 }; + + +/* Callbacks. */ + +static void +source_changed_callback (ESource *source, + ESourceGroup *group) +{ + if (! group->priv->ignore_source_changed) + g_signal_emit (group, signals[CHANGED], 0); +} + + +/* GObject methods. */ + +static void +impl_dispose (GObject *object) +{ + ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv; + + if (priv->sources != NULL) { + GSList *p; + + for (p = priv->sources; p != NULL; p = p->next) { + ESource *source = E_SOURCE (p->data); + + g_signal_handlers_disconnect_by_func (source, + G_CALLBACK (source_changed_callback), + object); + g_object_unref (source); + } + + g_slist_free (priv->sources); + priv->sources = NULL; + } + + (* G_OBJECT_CLASS (parent_class)->dispose) (object); +} + +static void +impl_finalize (GObject *object) +{ + ESourceGroupPrivate *priv = E_SOURCE_GROUP (object)->priv; + + g_free (priv->uid); + g_free (priv->name); + g_free (priv->base_uri); + g_free (priv); + + (* G_OBJECT_CLASS (parent_class)->finalize) (object); +} + + +/* Initialization. */ + +static void +class_init (ESourceGroupClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->dispose = impl_dispose; + object_class->finalize = impl_finalize; + + parent_class = g_type_class_peek_parent (class); + + signals[CHANGED] = + g_signal_new ("changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceGroupClass, changed), + NULL, NULL, + e_util_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[SOURCE_ADDED] = + g_signal_new ("source_added", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceGroupClass, source_added), + NULL, NULL, + e_util_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); + signals[SOURCE_REMOVED] = + g_signal_new ("source_removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceGroupClass, source_removed), + NULL, NULL, + e_util_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_OBJECT); +} + +static void +init (ESourceGroup *source_group) +{ + ESourceGroupPrivate *priv; + + priv = g_new0 (ESourceGroupPrivate, 1); + source_group->priv = priv; +} + + +/* Public methods. */ + +ESourceGroup * +e_source_group_new (const char *name, + const char *base_uri) +{ + ESourceGroup *new; + + g_return_val_if_fail (name != NULL, NULL); + g_return_val_if_fail (base_uri != NULL, NULL); + + new = g_object_new (e_source_group_get_type (), NULL); + new->priv->uid = e_uid_new (); + + e_source_group_set_name (new, name); + e_source_group_set_base_uri (new, base_uri); + + return new; +} + +ESourceGroup * +e_source_group_new_from_xml (const char *xml) +{ + xmlDocPtr doc; + ESourceGroup *group; + + doc = xmlParseDoc ((char *) xml); + if (doc == NULL) + return NULL; + + group = e_source_group_new_from_xmldoc (doc); + xmlFreeDoc (doc); + + return group; +} + +ESourceGroup * +e_source_group_new_from_xmldoc (xmlDocPtr doc) +{ + xmlNodePtr root, p; + xmlChar *uid; + xmlChar *name; + xmlChar *base_uri; + ESourceGroup *new = NULL; + + g_return_val_if_fail (doc != NULL, NULL); + + root = doc->children; + if (strcmp (root->name, "group") != 0) + return NULL; + + uid = xmlGetProp (root, "uid"); + name = xmlGetProp (root, "name"); + base_uri = xmlGetProp (root, "base_uri"); + + if (uid == NULL || name == NULL || base_uri == NULL) + goto done; + + new = g_object_new (e_source_group_get_type (), NULL); + new->priv->uid = g_strdup (uid); + + e_source_group_set_name (new, name); + e_source_group_set_base_uri (new, base_uri); + + for (p = root->children; p != NULL; p = p->next) { + ESource *new_source = e_source_new_from_xml_node (p); + e_source_group_add_source (new, new_source, -1); + } + + done: + if (name != NULL) + xmlFree (name); + if (base_uri != NULL) + xmlFree (base_uri); + return new; +} + +gboolean +e_source_group_update_from_xml (ESourceGroup *group, + const char *xml, + gboolean *changed_return) +{ + xmlDocPtr xmldoc; + gboolean success; + + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + g_return_val_if_fail (xml != NULL, FALSE); + + xmldoc = xmlParseDoc ((char *) xml); + + success = e_source_group_update_from_xmldoc (group, xmldoc, changed_return); + + xmlFreeDoc (xmldoc); + + return success; +} + +gboolean +e_source_group_update_from_xmldoc (ESourceGroup *group, + xmlDocPtr doc, + gboolean *changed_return) +{ + GHashTable *new_sources_hash; + GSList *new_sources_list = NULL; + xmlNodePtr root, nodep; + xmlChar *name, *base_uri; + gboolean changed = FALSE; + GSList *p, *q; + + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + g_return_val_if_fail (doc != NULL, FALSE); + + *changed_return = FALSE; + + root = doc->children; + if (strcmp (root->name, "group") != 0) + return FALSE; + + name = xmlGetProp (root, "name"); + if (name == NULL) + return FALSE; + + base_uri = xmlGetProp (root, "base_uri"); + if (base_uri == NULL) { + xmlFree (name); + return FALSE; + } + + if (strcmp (group->priv->name, name) != 0) { + g_free (group->priv->name); + group->priv->name = g_strdup (name); + changed = TRUE; + } + xmlFree (name); + + if (strcmp (group->priv->base_uri, base_uri) != 0) { + g_free (group->priv->base_uri); + group->priv->base_uri = g_strdup (base_uri); + changed = TRUE; + } + xmlFree (base_uri); + + new_sources_hash = g_hash_table_new (g_direct_hash, g_direct_equal); + + for (nodep = root->children; nodep != NULL; nodep = nodep->next) { + ESource *existing_source; + char *uid = e_source_uid_from_xml_node (nodep); + + if (uid == NULL) + continue; + + existing_source = e_source_group_peek_source_by_uid (group, uid); + if (g_hash_table_lookup (new_sources_hash, existing_source) != NULL) + continue; + + if (existing_source == NULL) { + ESource *new_source = e_source_new_from_xml_node (nodep); + + if (new_source != NULL) { + e_source_set_group (new_source, group); + g_signal_connect (new_source, "changed", G_CALLBACK (source_changed_callback), group); + new_sources_list = g_slist_prepend (new_sources_list, new_source); + + g_hash_table_insert (new_sources_hash, new_source, new_source); + + g_signal_emit (group, signals[SOURCE_ADDED], 0, new_source); + changed = TRUE; + } + } else { + gboolean source_changed; + + group->priv->ignore_source_changed ++; + + if (e_source_update_from_xml_node (existing_source, nodep, &source_changed)) { + new_sources_list = g_slist_prepend (new_sources_list, existing_source); + g_object_ref (existing_source); + g_hash_table_insert (new_sources_hash, existing_source, existing_source); + + if (source_changed) + changed = TRUE; + } + + group->priv->ignore_source_changed --; + } + + g_free (uid); + } + + new_sources_list = g_slist_reverse (new_sources_list); + + /* Emit "group_removed" and disconnect the "changed" signal for all the + groups that we haven't found in the new list. */ + q = new_sources_list; + for (p = group->priv->sources; p != NULL; p = p->next) { + ESource *source = E_SOURCE (p->data); + + if (g_hash_table_lookup (new_sources_hash, source) == NULL) { + changed = TRUE; + + g_signal_emit (group, signals[SOURCE_REMOVED], 0, source); + g_signal_handlers_disconnect_by_func (source, source_changed_callback, group); + } + + if (! changed && q != NULL) { + if (q->data != p->data) + changed = TRUE; + q = q->next; + } + } + + g_hash_table_destroy (new_sources_hash); + + /* Replace the original group list with the new one. */ + g_slist_foreach (group->priv->sources, (GFunc) g_object_unref, NULL); + g_slist_free (group->priv->sources); + + group->priv->sources = new_sources_list; + + /* FIXME if the order changes, the function doesn't notice. */ + + if (changed) { + g_signal_emit (group, signals[CHANGED], 0); + *changed_return = TRUE; + } + + return TRUE; /* Success. */ +} + +char * +e_source_group_uid_from_xmldoc (xmlDocPtr doc) +{ + xmlNodePtr root = doc->children; + xmlChar *name; + char *retval; + + if (strcmp (root->name, "group") != 0) + return NULL; + + name = xmlGetProp (root, "uid"); + if (name == NULL) + return NULL; + + retval = g_strdup (name); + xmlFree (name); + return retval; +} + +void +e_source_group_set_name (ESourceGroup *group, + const char *name) +{ + g_return_if_fail (E_IS_SOURCE_GROUP (group)); + g_return_if_fail (name != NULL); + + if (group->priv->name == name) + return; + + g_free (group->priv->name); + group->priv->name = g_strdup (name); + + g_signal_emit (group, signals[CHANGED], 0); +} + +void e_source_group_set_base_uri (ESourceGroup *group, + const char *base_uri) +{ + g_return_if_fail (E_IS_SOURCE_GROUP (group)); + g_return_if_fail (base_uri != NULL); + + if (group->priv->base_uri == base_uri) + return; + + g_free (group->priv->base_uri); + group->priv->base_uri = g_strdup (base_uri); + + g_signal_emit (group, signals[CHANGED], 0); +} + + +const char * +e_source_group_peek_uid (ESourceGroup *group) +{ + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL); + + return group->priv->uid; +} + +const char * +e_source_group_peek_name (ESourceGroup *group) +{ + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL); + + return group->priv->name; +} + +const char * +e_source_group_peek_base_uri (ESourceGroup *group) +{ + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL); + + return group->priv->base_uri; +} + + +GSList * +e_source_group_peek_sources (ESourceGroup *group) +{ + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), NULL); + + return group->priv->sources; +} + +ESource * +e_source_group_peek_source_by_uid (ESourceGroup *group, + const char *uid) +{ + GSList *p; + + for (p = group->priv->sources; p != NULL; p = p->next) { + if (strcmp (e_source_peek_uid (E_SOURCE (p->data)), uid) == 0) + return E_SOURCE (p->data); + } + + return NULL; +} + +ESource * +e_source_group_peek_source_by_name (ESourceGroup *group, + const char *name) +{ + GSList *p; + + for (p = group->priv->sources; p != NULL; p = p->next) { + if (strcmp (e_source_peek_name (E_SOURCE (p->data)), name) == 0) + return E_SOURCE (p->data); + } + + return NULL; +} + +gboolean +e_source_group_add_source (ESourceGroup *group, + ESource *source, + int position) +{ + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + + if (e_source_group_peek_source_by_uid (group, e_source_peek_uid (source)) != NULL) + return FALSE; + + e_source_set_group (source, group); + g_object_ref (source); + + g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), group); + + group->priv->sources = g_slist_insert (group->priv->sources, source, position); + g_signal_emit (group, signals[SOURCE_ADDED], 0, source); + g_signal_emit (group, signals[CHANGED], 0); + + return TRUE; +} + +gboolean +e_source_group_remove_source (ESourceGroup *group, + ESource *source) +{ + GSList *p; + + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + g_return_val_if_fail (E_IS_SOURCE (source), FALSE); + + for (p = group->priv->sources; p != NULL; p = p->next) { + if (E_SOURCE (p->data) == source) { + group->priv->sources = g_slist_remove_link (group->priv->sources, p); + g_signal_emit (group, signals[SOURCE_REMOVED], 0, source); + g_signal_emit (group, signals[CHANGED], 0); + return TRUE; + } + } + + return FALSE; +} + +gboolean +e_source_group_remove_source_by_uid (ESourceGroup *group, + const char *uid) +{ + GSList *p; + + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + g_return_val_if_fail (uid != NULL, FALSE); + + for (p = group->priv->sources; p != NULL; p = p->next) { + ESource *source = E_SOURCE (p->data); + + if (strcmp (e_source_peek_uid (source), uid) == 0) { + group->priv->sources = g_slist_remove_link (group->priv->sources, p); + g_signal_emit (group, signals[SOURCE_REMOVED], 0, source); + g_signal_emit (group, signals[CHANGED], 0); + return TRUE; + } + } + + return FALSE; +} + + +char * +e_source_group_to_xml (ESourceGroup *group) +{ + xmlDocPtr doc; + xmlNodePtr root; + xmlChar *xml_buffer; + char *returned_buffer; + int xml_buffer_size; + GSList *p; + + doc = xmlNewDoc ("1.0"); + + root = xmlNewDocNode (doc, NULL, "group", NULL); + xmlSetProp (root, "uid", e_source_group_peek_uid (group)); + xmlSetProp (root, "name", e_source_group_peek_name (group)); + xmlSetProp (root, "base_uri", e_source_group_peek_base_uri (group)); + + xmlDocSetRootElement (doc, root); + + for (p = group->priv->sources; p != NULL; p = p->next) + e_source_dump_to_xml_node (E_SOURCE (p->data), root); + + xmlDocDumpMemory (doc, &xml_buffer, &xml_buffer_size); + xmlFreeDoc (doc); + + returned_buffer = g_malloc (xml_buffer_size + 1); + memcpy (returned_buffer, xml_buffer, xml_buffer_size); + returned_buffer [xml_buffer_size] = '\0'; + xmlFree (xml_buffer); + + return returned_buffer; +} + + +E_MAKE_TYPE (e_source_group, "ESourceGroup", ESourceGroup, class_init, init, PARENT_TYPE) diff --git a/e-util/e-source-group.h b/e-util/e-source-group.h new file mode 100644 index 0000000000..bebecba58f --- /dev/null +++ b/e-util/e-source-group.h @@ -0,0 +1,102 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source-group.h + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#ifndef _E_SOURCE_GROUP_H_ +#define _E_SOURCE_GROUP_H_ + +#include <glib-object.h> +#include <libxml/tree.h> + +#define E_TYPE_SOURCE_GROUP (e_source_group_get_type ()) +#define E_SOURCE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), E_TYPE_SOURCE_GROUP, ESourceGroup)) +#define E_SOURCE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), E_TYPE_SOURCE_GROUP, ESourceGroupClass)) +#define E_IS_SOURCE_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), E_TYPE_SOURCE_GROUP)) +#define E_IS_SOURCE_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), E_TYPE_SOURCE_GROUP)) + + +typedef struct _ESourceGroup ESourceGroup; +typedef struct _ESourceGroupPrivate ESourceGroupPrivate; +typedef struct _ESourceGroupClass ESourceGroupClass; + +#include "e-source.h" + +struct _ESourceGroup { + GObject parent; + + ESourceGroupPrivate *priv; +}; + +struct _ESourceGroupClass { + GObjectClass parent_class; + + /* Signals. */ + + void (* changed) (ESourceGroup *group); + + void (* source_removed) (ESourceGroup *source_list, ESource *source); + void (* source_added) (ESourceGroup *source_list, ESource *source); +}; + + +GType e_source_group_get_type (void); + +ESourceGroup *e_source_group_new (const char *name, + const char *base_uri); +ESourceGroup *e_source_group_new_from_xml (const char *xml); +ESourceGroup *e_source_group_new_from_xmldoc (xmlDocPtr doc); + +gboolean e_source_group_update_from_xml (ESourceGroup *group, + const char *xml, + gboolean *changed_return); +gboolean e_source_group_update_from_xmldoc (ESourceGroup *group, + xmlDocPtr doc, + gboolean *changed_return); + +char *e_source_group_uid_from_xmldoc (xmlDocPtr doc); + +void e_source_group_set_name (ESourceGroup *group, + const char *name); +void e_source_group_set_base_uri (ESourceGroup *group, + const char *base_uri); + +const char *e_source_group_peek_uid (ESourceGroup *group); +const char *e_source_group_peek_name (ESourceGroup *group); +const char *e_source_group_peek_base_uri (ESourceGroup *group); + +GSList *e_source_group_peek_sources (ESourceGroup *group); +ESource *e_source_group_peek_source_by_uid (ESourceGroup *group, + const char *source_uid); +ESource *e_source_group_peek_source_by_name (ESourceGroup *group, + const char *source_name); + +gboolean e_source_group_add_source (ESourceGroup *group, + ESource *source, + int position); +gboolean e_source_group_remove_source (ESourceGroup *group, + ESource *source); +gboolean e_source_group_remove_source_by_uid (ESourceGroup *group, + const char *uid); + +char *e_source_group_to_xml (ESourceGroup *group); + + +#endif /* _E_SOURCE_GROUP_H_ */ diff --git a/e-util/e-source-list.c b/e-util/e-source-list.c new file mode 100644 index 0000000000..565df8977e --- /dev/null +++ b/e-util/e-source-list.c @@ -0,0 +1,524 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source-list.c + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#include <config.h> + +#include "e-source-list.h" + +#include "e-util-marshal.h" + +#include <string.h> +#include <gal/util/e-util.h> + + +#define PARENT_TYPE G_TYPE_OBJECT +static GObjectClass *parent_class = NULL; + +struct _ESourceListPrivate { + GConfClient *gconf_client; + char *gconf_path; + + int gconf_notify_id; + + GSList *groups; + + gboolean ignore_group_changed; + int sync_idle_id; +}; + + +/* Signals. */ + +enum { + CHANGED, + GROUP_REMOVED, + GROUP_ADDED, + LAST_SIGNAL +}; +static unsigned int signals[LAST_SIGNAL] = { 0 }; + + +/* Forward declarations. */ + +static gboolean sync_idle_callback (ESourceList *list); +static void group_changed_callback (ESourceGroup *group, + ESourceList *list); +static void conf_changed_callback (GConfClient *client, + unsigned int connection_id, + GConfEntry *entry, + ESourceList *list); + + +/* Utility functions. */ + +static void +load_from_gconf (ESourceList *list) +{ + GSList *conf_list, *p, *q; + GSList *new_groups_list; + GHashTable *new_groups_hash; + gboolean changed = FALSE; + int pos; + + conf_list = gconf_client_get_list (list->priv->gconf_client, + list->priv->gconf_path, + GCONF_VALUE_STRING, NULL); + + new_groups_list = NULL; + new_groups_hash = g_hash_table_new (g_direct_hash, g_direct_equal); + + for (p = conf_list, pos = 0; p != NULL; p = p->next, pos++) { + const char *xml = p->data; + xmlDocPtr xmldoc = xmlParseDoc ((char *) xml); + char *group_uid = e_source_group_uid_from_xmldoc (xmldoc); + ESourceGroup *existing_group; + + if (group_uid == NULL) + continue; + + existing_group = e_source_list_peek_group_by_uid (list, group_uid); + if (g_hash_table_lookup (new_groups_hash, existing_group) != NULL) + continue; + + if (existing_group == NULL) { + ESourceGroup *new_group = e_source_group_new_from_xmldoc (xmldoc); + + if (new_group != NULL) { + g_signal_connect (new_group, "changed", G_CALLBACK (group_changed_callback), list); + new_groups_list = g_slist_prepend (new_groups_list, new_group); + + g_hash_table_insert (new_groups_hash, new_group, new_group); + g_signal_emit (list, signals[GROUP_ADDED], 0, new_group); + changed = TRUE; + } + } else { + gboolean group_changed; + + list->priv->ignore_group_changed ++; + + if (e_source_group_update_from_xmldoc (existing_group, xmldoc, &group_changed)) { + new_groups_list = g_slist_prepend (new_groups_list, existing_group); + g_object_ref (existing_group); + g_hash_table_insert (new_groups_hash, existing_group, existing_group); + + if (group_changed) + changed = TRUE; + } + + list->priv->ignore_group_changed --; + } + + g_free (group_uid); + } + + new_groups_list = g_slist_reverse (new_groups_list); + + g_slist_foreach (conf_list, (GFunc) g_free, NULL); + g_slist_free (conf_list); + + /* Emit "group_removed" and disconnect the "changed" signal for all the + groups that we haven't found in the new list. Also, check if the + order has changed. */ + q = new_groups_list; + for (p = list->priv->groups; p != NULL; p = p->next) { + ESourceGroup *group = E_SOURCE_GROUP (p->data); + + if (g_hash_table_lookup (new_groups_hash, group) == NULL) { + changed = TRUE; + g_signal_emit (list, signals[GROUP_REMOVED], 0, group); + g_signal_handlers_disconnect_by_func (group, group_changed_callback, list); + } + + if (! changed && q != NULL) { + if (q->data != p->data) + changed = TRUE; + q = q->next; + } + } + + g_hash_table_destroy (new_groups_hash); + + /* Replace the original group list with the new one. */ + + g_slist_foreach (list->priv->groups, (GFunc) g_object_unref, NULL); + g_slist_free (list->priv->groups); + + list->priv->groups = new_groups_list; + + /* FIXME if the order changes, the function doesn't notice. */ + + if (changed) + g_signal_emit (list, signals[CHANGED], 0); +} + +static void +remove_group (ESourceList *list, + ESourceGroup *group) +{ + list->priv->groups = g_slist_remove (list->priv->groups, group); + + g_signal_emit (list, signals[GROUP_REMOVED], 0, group); + g_object_unref (group); + + g_signal_emit (list, signals[CHANGED], 0); +} + + +/* Callbacks. */ + +static gboolean +sync_idle_callback (ESourceList *list) +{ + GError *error = NULL; + + if (! e_source_list_sync (list, &error)) { + g_warning ("Cannot update \"%s\": %s", list->priv->gconf_path, error->message); + g_error_free (error); + } + + return FALSE; +} + +static void +group_changed_callback (ESourceGroup *group, + ESourceList *list) +{ + if (! list->priv->ignore_group_changed) + g_signal_emit (list, signals[CHANGED], 0); + + if (list->priv->sync_idle_id == 0) + list->priv->sync_idle_id = g_idle_add ((GSourceFunc) sync_idle_callback, list); +} + +static void +conf_changed_callback (GConfClient *client, + unsigned int connection_id, + GConfEntry *entry, + ESourceList *list) +{ + load_from_gconf (list); +} + + +/* GObject methods. */ + +static void +impl_dispose (GObject *object) +{ + ESourceListPrivate *priv = E_SOURCE_LIST (object)->priv; + + if (priv->sync_idle_id != 0) { + GError *error = NULL; + + g_source_remove (priv->sync_idle_id); + priv->sync_idle_id = 0; + + if (! e_source_list_sync (E_SOURCE_LIST (object), &error)) + g_warning ("Could not update \"%s\": %s", + priv->gconf_path, error->message); + } + + if (priv->groups != NULL) { + GSList *p; + + for (p = priv->groups; p != NULL; p = p->next) + g_object_unref (p->data); + + g_slist_free (priv->groups); + priv->groups = NULL; + } + + if (priv->gconf_client != NULL) { + if (priv->gconf_notify_id != 0) { + gconf_client_notify_remove (priv->gconf_client, + priv->gconf_notify_id); + priv->gconf_notify_id = 0; + } + + g_object_unref (priv->gconf_client); + priv->gconf_client = NULL; + } + + (* G_OBJECT_CLASS (parent_class)->dispose) (object); +} + +static void +impl_finalize (GObject *object) +{ + ESourceListPrivate *priv = E_SOURCE_LIST (object)->priv; + + if (priv->gconf_notify_id != 0) { + gconf_client_notify_remove (priv->gconf_client, + priv->gconf_notify_id); + priv->gconf_notify_id = 0; + } + + g_free (priv->gconf_path); + g_free (priv); + + (* G_OBJECT_CLASS (parent_class)->finalize) (object); +} + + +/* Initialization. */ + +static void +class_init (ESourceListClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->dispose = impl_dispose; + object_class->finalize = impl_finalize; + + parent_class = g_type_class_peek_parent (class); + + signals[CHANGED] = + g_signal_new ("changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceListClass, changed), + NULL, NULL, + e_util_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[GROUP_REMOVED] = + g_signal_new ("group_removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceListClass, group_removed), + NULL, NULL, + e_util_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_POINTER); + + signals[GROUP_ADDED] = + g_signal_new ("group_added", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceListClass, group_added), + NULL, NULL, + e_util_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, + G_TYPE_POINTER); +} + +static void +init (ESourceList *source_list) +{ + ESourceListPrivate *priv; + + priv = g_new0 (ESourceListPrivate, 1); + + source_list->priv = priv; +} + + +/* Public methods. */ + +ESourceList * +e_source_list_new (void) +{ + ESourceList *list = g_object_new (e_source_list_get_type (), NULL); + + return list; +} + +ESourceList * +e_source_list_new_for_gconf (GConfClient *client, + const char *path) +{ + ESourceList *list; + + g_return_val_if_fail (GCONF_IS_CLIENT (client), NULL); + g_return_val_if_fail (path != NULL, NULL); + + list = g_object_new (e_source_list_get_type (), NULL); + + list->priv->gconf_path = g_strdup (path); + list->priv->gconf_client = client; + g_object_ref (client); + + gconf_client_add_dir (client, path, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL); + + list->priv->gconf_notify_id + = gconf_client_notify_add (client, path, + (GConfClientNotifyFunc) conf_changed_callback, list, + NULL, NULL); + load_from_gconf (list); + + return list; +} + + +GSList * +e_source_list_peek_groups (ESourceList *list) +{ + g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL); + + return list->priv->groups; +} + +ESourceGroup * +e_source_list_peek_group_by_uid (ESourceList *list, + const char *uid) +{ + GSList *p; + + g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL); + g_return_val_if_fail (uid != NULL, NULL); + + for (p = list->priv->groups; p != NULL; p = p->next) { + ESourceGroup *group = E_SOURCE_GROUP (p->data); + + if (strcmp (e_source_group_peek_uid (group), uid) == 0) + return group; + } + + return NULL; +} + +ESource * +e_source_list_peek_source_by_uid (ESourceList *list, + const char *group_uid, + const char *source_uid) +{ + ESourceGroup *group; + + g_return_val_if_fail (E_IS_SOURCE_LIST (list), NULL); + g_return_val_if_fail (group_uid != NULL, NULL); + g_return_val_if_fail (source_uid != NULL, NULL); + + group = e_source_list_peek_group_by_uid (list, group_uid); + if (group == NULL) + return NULL; + + return e_source_group_peek_source_by_uid (group, source_uid); +} + + +gboolean +e_source_list_add_group (ESourceList *list, + ESourceGroup *group, + int position) +{ + g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE); + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + + if (e_source_list_peek_group_by_uid (list, e_source_group_peek_uid (group)) != NULL) + return FALSE; + + list->priv->groups = g_slist_insert (list->priv->groups, group, position); + g_object_ref (group); + + g_signal_connect (group, "changed", G_CALLBACK (group_changed_callback), list); + + g_signal_emit (list, signals[GROUP_ADDED], 0, group); + g_signal_emit (list, signals[CHANGED], 0); + + return TRUE; +} + +gboolean +e_source_list_remove_group (ESourceList *list, + ESourceGroup *group) +{ + g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE); + g_return_val_if_fail (E_IS_SOURCE_GROUP (group), FALSE); + + if (e_source_list_peek_group_by_uid (list, e_source_group_peek_uid (group)) == NULL) + return FALSE; + + remove_group (list, group); + return TRUE; +} + +gboolean +e_source_list_remove_group_by_uid (ESourceList *list, + const char *uid) +{ + ESourceGroup *group; + + g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE); + g_return_val_if_fail (uid != NULL, FALSE); + + group = e_source_list_peek_group_by_uid (list, uid); + if (group== NULL) + return FALSE; + + remove_group (list, group); + return TRUE; +} + +gboolean +e_source_list_remove_source_by_uid (ESourceList *list, + const char *group_uid, + const char *source_uid) +{ + ESourceGroup *group; + + g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE); + g_return_val_if_fail (group_uid != NULL, FALSE); + g_return_val_if_fail (source_uid != NULL, FALSE); + + group = e_source_list_peek_group_by_uid (list, group_uid); + if (group== NULL) + return FALSE; + + return e_source_group_remove_source_by_uid (group, source_uid); +} + + +gboolean +e_source_list_sync (ESourceList *list, + GError **error) +{ + GSList *conf_list; + GSList *p; + gboolean retval; + + g_return_val_if_fail (E_IS_SOURCE_LIST (list), FALSE); + + conf_list = NULL; + for (p = list->priv->groups; p != NULL; p = p->next) + conf_list = g_slist_prepend (conf_list, e_source_group_to_xml (E_SOURCE_GROUP (p->data))); + conf_list = g_slist_reverse (conf_list); + + retval = gconf_client_set_list (list->priv->gconf_client, + list->priv->gconf_path, + GCONF_VALUE_STRING, + conf_list, + error); + + g_slist_foreach (conf_list, (GFunc) g_free, NULL); + g_slist_free (conf_list); + + if (list->priv->gconf_notify_id != 0) { + gconf_client_notify_remove (list->priv->gconf_client, + list->priv->gconf_notify_id); + list->priv->gconf_notify_id = 0; + } + + return retval; +} + + +E_MAKE_TYPE (e_source_list, "ESourceList", ESourceList, class_init, init, PARENT_TYPE) diff --git a/e-util/e-source-list.h b/e-util/e-source-list.h new file mode 100644 index 0000000000..1985b35d73 --- /dev/null +++ b/e-util/e-source-list.h @@ -0,0 +1,88 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source-list.h + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#ifndef _E_SOURCE_LIST_H_ +#define _E_SOURCE_LIST_H_ + +#include <libxml/tree.h> +#include <gconf/gconf-client.h> + +#include "e-source-group.h" + +#define E_TYPE_SOURCE_LIST (e_source_list_get_type ()) +#define E_SOURCE_LIST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), E_TYPE_SOURCE_LIST, ESourceList)) +#define E_SOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), E_TYPE_SOURCE_LIST, ESourceListClass)) +#define E_IS_SOURCE_LIST(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), E_TYPE_SOURCE_LIST)) +#define E_IS_SOURCE_LIST_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), E_TYPE_SOURCE_LIST)) + + +typedef struct _ESourceList ESourceList; +typedef struct _ESourceListPrivate ESourceListPrivate; +typedef struct _ESourceListClass ESourceListClass; + +struct _ESourceList { + GObject parent; + + ESourceListPrivate *priv; +}; + +struct _ESourceListClass { + GObjectClass parent_class; + + /* Signals. */ + + void (* changed) (ESourceList *source_list); + + void (* group_removed) (ESourceList *source_list, ESourceGroup *group); + void (* group_added) (ESourceList *source_list, ESourceGroup *group); +}; + + +GType e_source_list_get_type (void); + +ESourceList *e_source_list_new (void); +ESourceList *e_source_list_new_for_gconf (GConfClient *client, + const char *path); + +GSList *e_source_list_peek_groups (ESourceList *list); +ESourceGroup *e_source_list_peek_group_by_uid (ESourceList *list, + const char *source_group); +ESource *e_source_list_peek_source_by_uid (ESourceList *list, + const char *group_name, + const char *source_name); + +gboolean e_source_list_add_group (ESourceList *list, + ESourceGroup *group, + int position); +gboolean e_source_list_remove_group (ESourceList *list, + ESourceGroup *group); +gboolean e_source_list_remove_group_by_uid (ESourceList *list, + const char *name); +gboolean e_source_list_remove_source_by_uid (ESourceList *list, + const char *group_name, + const char *source_name); + +gboolean e_source_list_sync (ESourceList *list, + GError **error); + + +#endif /* _E_SOURCE_LIST_H_ */ diff --git a/e-util/e-source.c b/e-util/e-source.c new file mode 100644 index 0000000000..cc7742d924 --- /dev/null +++ b/e-util/e-source.c @@ -0,0 +1,458 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source.c + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#include <config.h> + +#include "e-source.h" + +#include "e-util-marshal.h" +#include "e-uid.h" + +#include <string.h> +#include <gal/util/e-util.h> + + +#define PARENT_TYPE G_TYPE_OBJECT +static GObjectClass *parent_class = NULL; + +#define ES_CLASS(obj) E_SOURCE_CLASS (G_OBJECT_GET_CLASS (obj)) + + +/* String used to put the color in the XML. */ +#define COLOR_FORMAT_STRING "%06x" + + +/* Private members. */ + +struct _ESourcePrivate { + ESourceGroup *group; + + char *uid; + char *name; + char *relative_uri; + + gboolean has_color; + guint32 color; +}; + + +/* Signals. */ + +enum { + CHANGED, + LAST_SIGNAL +}; +static unsigned int signals[LAST_SIGNAL] = { 0 }; + + +/* Callbacks. */ + +static void +group_weak_notify (ESource *source, + GObject **where_the_object_was) +{ + source->priv->group = NULL; + + g_signal_emit (source, signals[CHANGED], 0); +} + + +/* GObject methods. */ + +static void +impl_finalize (GObject *object) +{ + ESourcePrivate *priv = E_SOURCE (object)->priv; + + g_free (priv->uid); + g_free (priv->name); + g_free (priv->relative_uri); + g_free (priv); + + (* G_OBJECT_CLASS (parent_class)->finalize) (object); +} + +static void +impl_dispose (GObject *object) +{ + ESourcePrivate *priv = E_SOURCE (object)->priv; + + if (priv->group != NULL) { + g_object_weak_unref (G_OBJECT (priv->group), (GWeakNotify) group_weak_notify, object); + priv->group = NULL; + } + + (* G_OBJECT_CLASS (parent_class)->dispose) (object); +} + + +/* Initialization. */ + +static void +class_init (ESourceClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->dispose = impl_dispose; + object_class->finalize = impl_finalize; + + parent_class = g_type_class_peek_parent (class); + + signals[CHANGED] = + g_signal_new ("changed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (ESourceClass, changed), + NULL, NULL, + e_util_marshal_VOID__VOID, + G_TYPE_NONE, 0); +} + +static void +init (ESource *source) +{ + ESourcePrivate *priv; + + priv = g_new0 (ESourcePrivate, 1); + source->priv = priv; +} + + +/* Public methods. */ + +ESource * +e_source_new (const char *name, + const char *relative_uri) +{ + ESource *source; + + g_return_val_if_fail (name != NULL, NULL); + g_return_val_if_fail (relative_uri != NULL, NULL); + + source = g_object_new (e_source_get_type (), NULL); + source->priv->uid = e_uid_new (); + + e_source_set_name (source, name); + e_source_set_relative_uri (source, relative_uri); + return source; +} + +ESource * +e_source_new_from_xml_node (xmlNodePtr node) +{ + ESource *source; + xmlChar *uid; + + uid = xmlGetProp (node, "uid"); + if (uid == NULL) + return NULL; + + source = g_object_new (e_source_get_type (), NULL); + + source->priv->uid = g_strdup (uid); + xmlFree (uid); + + if (e_source_update_from_xml_node (source, node, NULL)) + return source; + + g_object_unref (source); + return NULL; +} + +/** + * e_source_update_from_xml_node: + * @source: An ESource. + * @node: A pointer to the node to parse. + * + * Update the ESource properties from @node. + * + * Return value: %TRUE if the data in @node was recognized and parsed into + * acceptable values for @source, %FALSE otherwise. + **/ +gboolean +e_source_update_from_xml_node (ESource *source, + xmlNodePtr node, + gboolean *changed_return) +{ + xmlChar *name; + xmlChar *relative_uri; + xmlChar *color_string; + gboolean retval; + gboolean changed = FALSE; + + name = xmlGetProp (node, "name"); + relative_uri = xmlGetProp (node, "relative_uri"); + color_string = xmlGetProp (node, "color"); + + if (name == NULL || relative_uri == NULL) { + retval = FALSE; + goto done; + } + + if (source->priv->name == NULL + || strcmp (name, source->priv->name) != 0 + || source->priv->relative_uri == NULL + || strcmp (relative_uri, source->priv->relative_uri) != 0) { + g_free (source->priv->name); + source->priv->name = g_strdup (name); + + g_free (source->priv->relative_uri); + source->priv->relative_uri = g_strdup (relative_uri); + + changed = TRUE; + } + + if (color_string == NULL) { + if (source->priv->has_color) { + source->priv->has_color = FALSE; + changed = TRUE; + } + } else { + guint32 color = 0; + + sscanf (color_string, COLOR_FORMAT_STRING, &color); + if (! source->priv->has_color || source->priv->color != color) { + source->priv->has_color = TRUE; + source->priv->color = color; + changed = TRUE; + } + } + + retval = TRUE; + + done: + if (changed) + g_signal_emit (source, signals[CHANGED], 0); + + if (changed_return != NULL) + *changed_return = changed; + + if (name != NULL) + xmlFree (name); + if (relative_uri != NULL) + xmlFree (relative_uri); + if (color_string != NULL) + xmlFree (color_string); + + return retval; +} + +/** + * e_source_name_from_xml_node: + * @node: A pointer to an XML node. + * + * Assuming that @node is a valid ESource specification, retrieve the name of + * the source from it. + * + * Return value: Name of the source in the specified @node. The caller must + * free the string. + **/ +char * +e_source_uid_from_xml_node (xmlNodePtr node) +{ + xmlChar *uid = xmlGetProp (node, "uid"); + char *retval; + + if (uid == NULL) + return NULL; + + retval = g_strdup (uid); + xmlFree (uid); + return retval; +} + +void +e_source_set_group (ESource *source, + ESourceGroup *group) +{ + g_return_if_fail (E_IS_SOURCE (source)); + g_return_if_fail (group == NULL || E_IS_SOURCE_GROUP (group)); + + if (source->priv->group == group) + return; + + if (source->priv->group != NULL) + g_object_weak_unref (G_OBJECT (source->priv->group), (GWeakNotify) group_weak_notify, source); + + source->priv->group = group; + if (group != NULL) + g_object_weak_ref (G_OBJECT (group), (GWeakNotify) group_weak_notify, source); + + g_signal_emit (source, signals[CHANGED], 0); +} + +void +e_source_set_name (ESource *source, + const char *name) +{ + g_return_if_fail (E_IS_SOURCE (source)); + + if (source->priv->name == name) + return; + + g_free (source->priv->name); + source->priv->name = g_strdup (name); + + g_signal_emit (source, signals[CHANGED], 0); +} + +void +e_source_set_relative_uri (ESource *source, + const char *relative_uri) +{ + g_return_if_fail (E_IS_SOURCE (source)); + + if (source->priv->relative_uri == relative_uri) + return; + + g_free (source->priv->relative_uri); + source->priv->relative_uri = g_strdup (relative_uri); + + g_signal_emit (source, signals[CHANGED], 0); +} + +void +e_source_set_color (ESource *source, + guint32 color) +{ + g_return_if_fail (E_IS_SOURCE (source)); + + if (source->priv->has_color && source->priv->color == color) + return; + + source->priv->has_color = TRUE; + source->priv->color = color; + + g_signal_emit (source, signals[CHANGED], 0); +} + +void +e_source_unset_color (ESource *source) +{ + g_return_if_fail (E_IS_SOURCE (source)); + + if (! source->priv->has_color) + return; + + source->priv->has_color = FALSE; + g_signal_emit (source, signals[CHANGED], 0); +} + + +ESourceGroup * +e_source_peek_group (ESource *source) +{ + g_return_val_if_fail (E_IS_SOURCE (source), NULL); + + return source->priv->group; +} + +const char * +e_source_peek_uid (ESource *source) +{ + g_return_val_if_fail (E_IS_SOURCE (source), NULL); + + return source->priv->uid; +} + +const char * +e_source_peek_name (ESource *source) +{ + g_return_val_if_fail (E_IS_SOURCE (source), NULL); + + return source->priv->name; +} + +const char * +e_source_peek_relative_uri (ESource *source) +{ + g_return_val_if_fail (E_IS_SOURCE (source), NULL); + + return source->priv->relative_uri; +} + +/** + * e_source_get_color: + * @source: An ESource + * @color_return: Pointer to a variable where the returned color will be + * stored. + * + * If @source has an associated color, return it in *@color_return. + * + * Return value: %TRUE if the @source has a defined color (and hence + * *@color_return was set), %FALSE otherwise. + **/ +gboolean +e_source_get_color (ESource *source, + guint32 *color_return) +{ + g_return_val_if_fail (E_IS_SOURCE (source), FALSE); + + if (! source->priv->has_color) + return FALSE; + + if (color_return != NULL) + *color_return = source->priv->color; + + return TRUE; +} + + +char * +e_source_get_uri (ESource *source) +{ + g_return_val_if_fail (E_IS_SOURCE (source), NULL); + + if (source->priv->group == NULL) + return NULL; + + return g_build_filename (e_source_group_peek_base_uri (source->priv->group), + source->priv->relative_uri, + NULL); +} + + +void +e_source_dump_to_xml_node (ESource *source, + xmlNodePtr parent_node) +{ + gboolean has_color; + guint32 color; + xmlNodePtr node = xmlNewChild (parent_node, NULL, "source", NULL); + + g_return_if_fail (E_IS_SOURCE (source)); + + + xmlSetProp (node, "uid", e_source_peek_uid (source)); + xmlSetProp (node, "name", e_source_peek_name (source)); + xmlSetProp (node, "relative_uri", e_source_peek_relative_uri (source)); + + has_color = e_source_get_color (source, &color); + if (has_color) { + char *color_string = g_strdup_printf (COLOR_FORMAT_STRING, color); + xmlSetProp (node, "color", color_string); + g_free (color_string); + } +} + + +E_MAKE_TYPE (e_source, "ESource", ESource, class_init, init, PARENT_TYPE) diff --git a/e-util/e-source.h b/e-util/e-source.h new file mode 100644 index 0000000000..6060b8b588 --- /dev/null +++ b/e-util/e-source.h @@ -0,0 +1,91 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-source.h + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#ifndef _E_SOURCE_H_ +#define _E_SOURCE_H_ + +#include <glib-object.h> +#include <libxml/tree.h> + +#define E_TYPE_SOURCE (e_source_get_type ()) +#define E_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), E_TYPE_SOURCE, ESource)) +#define E_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), E_TYPE_SOURCE, ESourceClass)) +#define E_IS_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), E_TYPE_SOURCE)) +#define E_IS_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), E_TYPE_SOURCE)) + + +typedef struct _ESource ESource; +typedef struct _ESourcePrivate ESourcePrivate; +typedef struct _ESourceClass ESourceClass; + +#include "e-source-group.h" + +struct _ESource { + GObject parent; + + ESourcePrivate *priv; +}; + +struct _ESourceClass { + GObjectClass parent_class; + + /* Signals. */ + void (* changed) (ESource *source); +}; + + +GType e_source_get_type (void); + +ESource *e_source_new (const char *name, + const char *relative_uri); +ESource *e_source_new_from_xml_node (xmlNodePtr node); + +gboolean e_source_update_from_xml_node (ESource *source, + xmlNodePtr node, + gboolean *changed_return); + +char *e_source_uid_from_xml_node (xmlNodePtr node); + +void e_source_set_group (ESource *source, + ESourceGroup *group); +void e_source_set_name (ESource *source, + const char *name); +void e_source_set_relative_uri (ESource *source, + const char *relative_uri); +void e_source_set_color (ESource *source, + guint32 color); +void e_source_unset_color (ESource *source); + +ESourceGroup *e_source_peek_group (ESource *source); +const char *e_source_peek_uid (ESource *source); +const char *e_source_peek_name (ESource *source); +const char *e_source_peek_relative_uri (ESource *source); +gboolean e_source_get_color (ESource *source, + guint32 *color_return); + +char *e_source_get_uri (ESource *source); + +void e_source_dump_to_xml_node (ESource *source, + xmlNodePtr parent_node); + + +#endif /* _E_SOURCE_H_ */ diff --git a/e-util/e-uid.c b/e-util/e-uid.c new file mode 100644 index 0000000000..90c036e0c1 --- /dev/null +++ b/e-util/e-uid.c @@ -0,0 +1,61 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-uid.c - Unique ID generator. + * + * Copyright (C) 2002 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Dan Winship <danw@ximian.com> + */ + +#include "e-uid.h" + +#include <glib/gstrfuncs.h> + +#include <string.h> +#include <time.h> +#include <unistd.h> + + +/** + * e_uid_new: + * + * Generate a new unique string for use e.g. in account lists. + * + * Return value: the newly generated UID. The caller should free the string + * when it's done with it. + **/ +char * +e_uid_new (void) +{ + static char *hostname; + static int serial; + + if (!hostname) { + static char buffer [512]; + + if ((gethostname (buffer, sizeof (buffer) - 1) == 0) && + (buffer [0] != 0)) + hostname = buffer; + else + hostname = "localhost"; + } + + return g_strdup_printf ("%lu.%lu.%d@%s", + (unsigned long) time (NULL), + (unsigned long) getpid (), + serial++, + hostname); +} diff --git a/e-util/e-uid.h b/e-util/e-uid.h new file mode 100644 index 0000000000..44ec8c0dd9 --- /dev/null +++ b/e-util/e-uid.h @@ -0,0 +1,28 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* e-uid.h - Unique ID generator. + * + * Copyright (C) 2002 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Dan Winship <danw@ximian.com> + */ + +#ifndef E_UID_H +#define E_UID_H + +char *e_uid_new (void); + +#endif /* E_UID_H */ diff --git a/e-util/test-source-list.c b/e-util/test-source-list.c new file mode 100644 index 0000000000..7c38436326 --- /dev/null +++ b/e-util/test-source-list.c @@ -0,0 +1,524 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ +/* test-source-list.c - Test for the ESourceList class. + * + * Copyright (C) 2002 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Ettore Perazzoli <ettore@ximian.com> + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "e-source-list.h" + +#include <libgnomeui/gnome-ui-init.h> +#include <glib/gmain.h> + + +/* Globals. */ + +static GMainLoop *main_loop = NULL; +static ESourceList *list = NULL; +static int idle_dump_id = 0; + + +/* Options. */ + +static gboolean listen = FALSE; +static gboolean dump = FALSE; +static char *key_arg = "/apps/evolution/test/source_list"; +static char *source_arg = NULL; +static char *group_arg = NULL; +static char *add_group_arg = NULL; +static char *add_source_arg = NULL; +static char *remove_group_arg = NULL; +static char *remove_source_arg = NULL; +static char *set_name_arg = NULL; +static char *set_base_uri_arg = NULL; +static char *set_relative_uri_arg = NULL; +static char *set_color_arg = NULL; +static gboolean unset_color = FALSE; + +static struct poptOption options[] = { + { "key", '\0', POPT_ARG_STRING, &key_arg, 0, + "Name of the GConf key to use", "PATH" }, + { "source", '\0', POPT_ARG_STRING, &source_arg, 0, + "UID of source to apply operation to", "UID" }, + { "group", '\0', POPT_ARG_STRING, &group_arg, 0, + "UID of group to apply operation to", "UID" }, + { "add-group", '\0', POPT_ARG_STRING, &add_group_arg, 0, + "Add group of specified name", "NAME" }, + { "add-source", '\0', POPT_ARG_STRING, &add_source_arg, 0, + "Add source of specified name", "NAME" }, + { "remove-group", '\0', POPT_ARG_STRING, &remove_group_arg, 0, + "Remove group of specified name", "NAME" }, + { "remove-source", '\0', POPT_ARG_STRING, &remove_source_arg, 0, + "Remove source of specified name", "NAME" }, + { "set-name", '\0', POPT_ARG_STRING, &set_name_arg, 0, + "Set name of source or group. When used with --group, it sets the name of a group. " + "When used with both --group and --source, it sets the name of a source.", "NAME" }, + { "set-relative-uri", '\0', POPT_ARG_STRING, &set_relative_uri_arg, 0, + "Set relative URI of a source. Use with --source or --add-source.", "NAME" }, + { "set-base-uri", '\0', POPT_ARG_STRING, &set_base_uri_arg, 0, + "Set base URI of a group. Use with --group or --add-group.", "NAME" }, + { "set-color", '\0', POPT_ARG_STRING, &set_color_arg, 0, + "Set the color of a source. Use with --source or --add-source.", "COLOR (rrggbb)" }, + { "unset-color", '\0', POPT_ARG_NONE, &unset_color, 0, + "Unset the color of a source. Use with --source or --add-source.", NULL }, + { "listen", '\0', POPT_ARG_NONE, &listen, 0, + "Wait and listen for changes.", "" }, + { "dump", '\0', POPT_ARG_NONE, &dump, 0, + "List the current configured sources.", "" }, + POPT_AUTOHELP + { NULL } +}; + + +/* Forward decls. */ +static void group_added_callback (ESourceList *list, ESourceGroup *group); +static void group_removed_callback (ESourceList *list, ESourceGroup *group); +static void source_added_callback (ESourceGroup *group, ESource *source); +static void source_removed_callback (ESourceGroup *group, ESource *source); + + +static void +dump_source (ESource *source) +{ + char *uri = e_source_get_uri (source); + gboolean has_color; + guint32 color; + + g_print ("\tSource %s\n", e_source_peek_uid (source)); + g_print ("\t\tname: %s\n", e_source_peek_name (source)); + g_print ("\t\trelative_uri: %s\n", e_source_peek_relative_uri (source)); + g_print ("\t\tabsolute_uri: %s\n", uri); + + has_color = e_source_get_color (source, &color); + if (has_color) + g_print ("\t\tcolor: %06x\n", color); + + g_free (uri); +} + +static void +dump_group (ESourceGroup *group) +{ + GSList *sources, *p; + + g_print ("Group %s\n", e_source_group_peek_uid (group)); + g_print ("\tname: %s\n", e_source_group_peek_name (group)); + g_print ("\tbase_uri: %s\n", e_source_group_peek_base_uri (group)); + + sources = e_source_group_peek_sources (group); + for (p = sources; p != NULL; p = p->next) { + ESource *source = E_SOURCE (p->data); + + dump_source (source); + + if (e_source_peek_group (source) != group) + g_warning ("\t\t** ERROR ** parent pointer is %p, should be %p", + e_source_peek_group (source), group); + } +} + +static void +dump_list (void) +{ + GSList *groups, *p; + + groups = e_source_list_peek_groups (list); + if (groups == NULL) { + g_print ("(No items)\n"); + return; + } + + for (p = groups; p != NULL; p = p->next) + dump_group (E_SOURCE_GROUP (p->data)); +} + + +static int +idle_dump_callback (void *unused_data) +{ + dump_list (); + idle_dump_id = 0; + + return FALSE; +} + +static void +dump_on_idle (void) +{ + if (idle_dump_id == 0) + idle_dump_id = g_idle_add (idle_dump_callback, NULL); +} + + +static void +source_changed_callback (ESource *source) +{ + static int count = 0; + + g_print ("** Event: source \"%s\" changed (%d)\n", e_source_peek_name (source), ++count); + + dump_on_idle (); +} + +static void +group_changed_callback (ESourceGroup *group) +{ + static int count = 0; + + g_print ("** Event: group \"%s\" changed (%d)\n", e_source_group_peek_name (group), ++count); + + dump_on_idle (); +} + +static void +list_changed_callback (ESourceGroup *group) +{ + static int count = 0; + + g_print ("** Event: list changed (%d)\n", ++count); + + dump_on_idle (); +} + + +static void +connect_source (ESource *source) +{ + g_object_ref (source); + g_signal_connect (source, "changed", G_CALLBACK (source_changed_callback), NULL); +} + +static void +connect_group (ESourceGroup *group) +{ + GSList *sources, *p; + + g_object_ref (group); + g_signal_connect (group, "changed", G_CALLBACK (group_changed_callback), NULL); + g_signal_connect (group, "source_added", G_CALLBACK (source_added_callback), NULL); + g_signal_connect (group, "source_removed", G_CALLBACK (source_removed_callback), NULL); + + sources = e_source_group_peek_sources (group); + for (p = sources; p != NULL; p = p->next) + connect_source (E_SOURCE (p->data)); +} + +static void +connect_list (void) +{ + GSList *groups, *p; + + g_signal_connect (list, "changed", G_CALLBACK (list_changed_callback), NULL); + g_signal_connect (list, "group_added", G_CALLBACK (group_added_callback), NULL); + g_signal_connect (list, "group_removed", G_CALLBACK (group_removed_callback), NULL); + + groups = e_source_list_peek_groups (list); + for (p = groups; p != NULL; p = p->next) + connect_group (E_SOURCE_GROUP (p->data)); +} + +static void +disconnect_group (ESourceGroup *group) +{ + g_signal_handlers_disconnect_by_func (group, G_CALLBACK (group_changed_callback), NULL); + g_signal_handlers_disconnect_by_func (group, G_CALLBACK (source_added_callback), NULL); + + g_object_unref (group); +} + +static void +disconnect_source (ESource *source) +{ + g_signal_handlers_disconnect_by_func (source, G_CALLBACK (source_changed_callback), NULL); + + g_object_unref (source); +} + + +static void +source_added_callback (ESourceGroup *group, + ESource *source) +{ + static int count = 0; + + g_print ("** Event: source \"%s\" added (%d)\n", e_source_peek_name (source), ++count); + + connect_source (source); + dump_on_idle (); +} + +static void +source_removed_callback (ESourceGroup *group, + ESource *source) +{ + static int count = 0; + + g_print ("** Event: source \"%s\" removed (%d)\n", e_source_peek_name (source), ++count); + + disconnect_source (source); + dump_on_idle (); +} + +static void +group_added_callback (ESourceList *list, + ESourceGroup *group) +{ + static int count = 0; + + g_print ("** Event: group \"%s\" added (%d)\n", e_source_group_peek_name (group), ++count); + + connect_group (group); + dump_on_idle (); +} + +static void +group_removed_callback (ESourceList *list, + ESourceGroup *group) +{ + static int count = 0; + + g_print ("** Event: group \"%s\" removed (%d)\n", e_source_group_peek_name (group), ++count); + + disconnect_group (group); + dump_on_idle (); +} + + +static int +on_idle_do_stuff (void *unused_data) +{ + GConfClient *client = gconf_client_get_default (); + ESourceGroup *new_group = NULL; + ESource *new_source = NULL; + + list = e_source_list_new_for_gconf (client, key_arg); + g_object_unref (client); + + if (add_group_arg != NULL) { + if (group_arg != NULL) { + fprintf (stderr, "--add-group and --group cannot be used at the same time.\n"); + exit (1); + } + if (set_base_uri_arg == NULL) { + fprintf (stderr, "When using --add-group, you need to specify a base URI using --set-base-uri.\n"); + exit (1); + } + + new_group = e_source_group_new (add_group_arg, set_base_uri_arg); + e_source_list_add_group (list, new_group, -1); + g_object_unref (new_group); + + e_source_list_sync (list, NULL); + } + + if (remove_group_arg != NULL) { + ESourceGroup *group; + + group = e_source_list_peek_group_by_uid (list, remove_group_arg); + if (group == NULL) { + fprintf (stderr, "No such group \"%s\".\n", remove_group_arg); + exit (1); + } + + e_source_list_remove_group (list, group); + e_source_list_sync (list, NULL); + } + + if (add_source_arg != NULL) { + ESourceGroup *group; + + if (group_arg == NULL && new_group == NULL) { + fprintf (stderr, + "When using --add-source, you need to specify a group using either --group\n" + "or --add-group.\n"); + exit (1); + } + if (set_relative_uri_arg == NULL) { + fprintf (stderr, + "When using --add-source, you need to specify a relative URI using\n" + "--set-relative-uri.\n"); + exit (1); + } + + if (group_arg == NULL) { + group = new_group; + } else { + group = e_source_list_peek_group_by_uid (list, group_arg); + if (group == NULL) { + fprintf (stderr, "No such group \"%s\".\n", group_arg == NULL ? add_group_arg : group_arg); + exit (1); + } + } + + new_source = e_source_new (add_source_arg, set_relative_uri_arg); + e_source_group_add_source (group, new_source, -1); + e_source_list_sync (list, NULL); + } + + if (remove_source_arg != NULL) { + ESource *source; + + if (group_arg == NULL) { + fprintf (stderr, "When using --remove-source, you need to specify a group using --group.\n"); + exit (1); + } + + source = e_source_list_peek_source_by_uid (list, group_arg, remove_source_arg); + if (source == NULL) { + fprintf (stderr, "No such source \"%s\" in group \"%s\".\n", remove_source_arg, group_arg); + exit (1); + } + + e_source_list_remove_source_by_uid (list, group_arg, remove_source_arg); + e_source_list_sync (list, NULL); + } + + if (set_name_arg != NULL) { + if (group_arg == NULL) { + fprintf (stderr, + "When using --set-name, you need to specify a source (using --group and\n" + "--source) or a group (using --group alone).\n"); + exit (1); + } + + if (source_arg != NULL) { + ESource *source = e_source_list_peek_source_by_uid (list, group_arg, source_arg); + + if (source != NULL) { + e_source_set_name (source, set_name_arg); + } else { + fprintf (stderr, "No such source \"%s\" in group \"%s\".\n", source_arg, group_arg); + exit (1); + } + } else { + ESourceGroup *group = e_source_list_peek_group_by_uid (list, group_arg); + + if (group != NULL) { + e_source_group_set_name (group, set_name_arg); + } else { + fprintf (stderr, "No such group \"%s\".\n", group_arg); + exit (1); + } + } + + e_source_list_sync (list, NULL); + } + + if (set_relative_uri_arg != NULL && add_source_arg == NULL) { + ESource *source; + + if (source_arg == NULL || group_arg == NULL) { + fprintf (stderr, + "When using --set-relative-uri, you need to specify a source using --group\n" + "and --source.\n"); + exit (1); + } + + source = e_source_list_peek_source_by_uid (list, group_arg, source_arg); + e_source_set_relative_uri (source, set_relative_uri_arg); + e_source_list_sync (list, NULL); + } + + if (set_color_arg != NULL) { + ESource *source; + guint32 color; + + if (add_source_arg == NULL && (source_arg == NULL || group_arg == NULL)) { + fprintf (stderr, + "When using --set-color, you need to specify a source using --group\n" + "and --source.\n"); + exit (1); + } + + if (add_source_arg != NULL) + source = new_source; + else + source = e_source_list_peek_source_by_uid (list, group_arg, source_arg); + + sscanf (set_color_arg, "%06x", &color); + e_source_set_color (source, color); + e_source_list_sync (list, NULL); + } + + if (unset_color) { + ESource *source; + + if (add_source_arg == NULL && (source_arg == NULL || group_arg == NULL)) { + fprintf (stderr, + "When using --unset-color, you need to specify a source using --group\n" + "and --source.\n"); + exit (1); + } + + if (add_source_arg != NULL) + source = new_source; + else + source = e_source_list_peek_source_by_uid (list, group_arg, source_arg); + + e_source_unset_color (source); + e_source_list_sync (list, NULL); + } + + if (set_base_uri_arg != NULL && add_group_arg == NULL) { + ESourceGroup *group; + + if (group_arg == NULL) { + fprintf (stderr, + "When using --set-base-uri, you need to specify a group using --group.\n"); + exit (1); + } + + group = e_source_list_peek_group_by_uid (list, group_arg); + e_source_group_set_base_uri (group, set_base_uri_arg); + e_source_list_sync (list, NULL); + } + + connect_list (); + + if (dump) + dump_list (); + + if (! listen) + g_main_loop_quit (main_loop); + + return FALSE; +} + + +int +main (int argc, + char **argv) +{ + GnomeProgram *program; + + program = gnome_program_init ("test-source-list", "0.0", + LIBGNOMEUI_MODULE, argc, argv, + GNOME_PARAM_POPT_TABLE, options, + NULL); + + g_idle_add (on_idle_do_stuff, NULL); + + main_loop = g_main_loop_new (NULL, TRUE); + g_main_loop_run (main_loop); + + return 0; +} |