[evolution-kolab] ECalBackendKolab: new util function for getting mail address via backend



commit 5e2c5d45eacb3c4e825e3339086ea18611159891
Author: Christian Hilberg <hilberg kernelconcepts de>
Date:   Wed Oct 24 15:29:51 2012 +0200

    ECalBackendKolab: new util function for getting mail address via backend
    
    * added util function to search the ESourceRegistry for
      an ESource which
      (a) matches our own toplevel source identifyer
      (b) has an ESourceMailIdentity extension
    * from the ESource which matches these criteria,
      we can retrieve the Kolab email address associated
      with our backend instance (this works since Kolab
      backends always have an email address associated
      with them)

 src/calendar/kolab-util-calendar.c |  109 ++++++++++++++++++++++++++++++++++++
 src/calendar/kolab-util-calendar.h |    3 +
 2 files changed, 112 insertions(+), 0 deletions(-)
---
diff --git a/src/calendar/kolab-util-calendar.c b/src/calendar/kolab-util-calendar.c
index 1e6a459..3822b03 100644
--- a/src/calendar/kolab-util-calendar.c
+++ b/src/calendar/kolab-util-calendar.c
@@ -34,6 +34,30 @@
 /*----------------------------------------------------------------------------*/
 /* internal statics */
 
+static ESource*
+util_calendar_ref_top_source (ESourceRegistry *registry,
+                              ESource *source)
+{
+	ESource *top_source = NULL;
+	gchar *top_uid = NULL;
+
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
+	g_return_val_if_fail (E_IS_SOURCE (source), NULL);
+
+	top_source = g_object_ref (source);
+	while (TRUE) {
+		top_uid = e_source_dup_parent (top_source);
+		if (top_uid == NULL)
+			break;
+		g_object_unref (top_source);
+		top_source = e_source_registry_ref_source (registry,
+		                                           top_uid);
+		g_free (top_uid);
+	}
+
+	return top_source;
+}
+
 static KolabUtilHttpJob*
 util_calendar_create_http_request (KolabSettingsHandler *ksettings,
                                    const gchar *path,
@@ -149,6 +173,91 @@ util_calendar_create_xfb_request (KolabSettingsHandler *ksettings,
 /* public API */
 
 /**
+ * kolab_util_calendar_dup_email_address:
+ * @backend: the cal backend via which we get to the source registry
+ *
+ * Retrieves the email address associated with the @backend
+ * from the source registry. The caller needs to free the
+ * result, if non-NULL.
+ *
+ * Returns: An email address or NULL if none found.
+ * Since: 3.6
+ */
+gchar*
+kolab_util_calendar_dup_email_address (ECalBackend *backend)
+{
+	ESourceRegistry *registry = NULL;
+	ESourceMailIdentity *extension = NULL;
+	ESource *esource = NULL;
+	ESource *top_source = NULL;
+	GList *sources = NULL;
+	GList *sources_ptr = NULL;
+	gchar *mail_addr = NULL;
+	gchar *my_top_uid = NULL;
+	const gchar *top_uid = NULL;
+
+	g_return_val_if_fail (E_IS_CAL_BACKEND (backend), NULL);
+
+	registry = e_cal_backend_get_registry (backend);
+	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
+
+	esource = e_backend_get_source (E_BACKEND (backend));
+	top_source = util_calendar_ref_top_source (registry, esource);
+	my_top_uid = e_source_dup_uid (top_source);
+	g_object_unref (top_source);
+	g_return_val_if_fail (my_top_uid != NULL, NULL);
+
+	sources = e_source_registry_list_sources (registry,
+	                                          E_SOURCE_EXTENSION_MAIL_IDENTITY);
+	sources_ptr = sources;
+	while (sources_ptr != NULL) {
+		esource = E_SOURCE (sources_ptr->data);
+		if (esource == NULL) {
+			/* should not normally happen */
+			g_warning ("%s()[%u] Got NULL ESource, skipping",
+			           __func__, __LINE__);
+			goto source_skip;
+		}
+
+		top_source = util_calendar_ref_top_source (registry,
+		                                           esource);
+		if (top_source == NULL) {
+			/* should not normally happen */
+			g_warning ("%s()[%u] Got NULL top ESource, skipping",
+			           __func__, __LINE__);
+			goto source_skip;
+		}
+
+		top_uid = e_source_get_uid (top_source);
+		g_object_unref (top_source);
+
+		if (g_strcmp0 (my_top_uid, top_uid) == 0) {
+			/* be sure the esource has the mail identity extension */
+			if (! e_source_has_extension (esource, E_SOURCE_EXTENSION_MAIL_IDENTITY)) {
+				g_warning ("%s()[%u] ESource %s has no mail identity extension, skipping.",
+				           __func__, __LINE__, e_source_get_uid (esource));
+				goto source_skip;
+			}
+			extension = e_source_get_extension (esource,
+			                                    E_SOURCE_EXTENSION_MAIL_IDENTITY);
+			mail_addr = e_source_mail_identity_dup_address (extension);
+		}
+
+		if (mail_addr != NULL)
+			break;
+
+	source_skip:
+
+		sources_ptr = g_list_next (sources_ptr);
+	}
+
+	if (sources != NULL)
+		g_list_free_full (sources, g_object_unref);
+
+	return mail_addr;
+}
+
+/**
  * kolab_util_calendar_get_tzid:
  * @comp: An ECalComponent to derive the tzid from.
  * @from: field to get the tzid from.
diff --git a/src/calendar/kolab-util-calendar.h b/src/calendar/kolab-util-calendar.h
index d682540..99dd3d7 100644
--- a/src/calendar/kolab-util-calendar.h
+++ b/src/calendar/kolab-util-calendar.h
@@ -53,6 +53,9 @@
 /*----------------------------------------------------------------------------*/
 
 gchar*
+kolab_util_calendar_dup_email_address (ECalBackend *backend);
+
+gchar*
 kolab_util_calendar_get_tzid (ECalComponent *comp,
                               ECalComponentField from);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]