[evolution-data-server/account-mgmt: 1/37] Add various base classes for backends.



commit 804dc743c8e1c695335ec221d5e56491d86bbef1
Author: Matthew Barnes <mbarnes redhat com>
Date:   Thu Sep 8 09:01:59 2011 -0400

    Add various base classes for backends.
    
    EBackend is an abstract base class for address book and calendar
    backends.
    
    EBackendFactory is an abstract base class for creating address book and
    calendar backends.  It is also an EExtension that extends EDataFactory.
    
    EDataFactory is an abstract base class for a D-Bus service that exports
    address book and calendar objects.

 .../reference/libebackend/tmpl/e-dbus-service.sgml |   76 ++++++
 libebackend/e-dbus-service.c                       |  251 ++++++++++++++++++++
 libebackend/e-dbus-service.h                       |   86 +++++++
 3 files changed, 413 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/libebackend/tmpl/e-dbus-service.sgml b/docs/reference/libebackend/tmpl/e-dbus-service.sgml
new file mode 100644
index 0000000..1cbdafc
--- /dev/null
+++ b/docs/reference/libebackend/tmpl/e-dbus-service.sgml
@@ -0,0 +1,76 @@
+<!-- ##### SECTION Title ##### -->
+EDBusService
+
+<!-- ##### SECTION Short_Description ##### -->
+
+
+<!-- ##### SECTION Long_Description ##### -->
+<para>
+
+</para>
+
+<!-- ##### SECTION See_Also ##### -->
+<para>
+
+</para>
+
+<!-- ##### SECTION Stability_Level ##### -->
+
+
+<!-- ##### SECTION Image ##### -->
+
+
+<!-- ##### STRUCT EDBusService ##### -->
+<para>
+
+</para>
+
+
+<!-- ##### SIGNAL EDBusService::bus-acquired ##### -->
+<para>
+
+</para>
+
+ edbusservice: the object which received the signal.
+ arg1: 
+
+<!-- ##### SIGNAL EDBusService::bus-name-acquired ##### -->
+<para>
+
+</para>
+
+ edbusservice: the object which received the signal.
+ arg1: 
+
+<!-- ##### SIGNAL EDBusService::bus-name-lost ##### -->
+<para>
+
+</para>
+
+ edbusservice: the object which received the signal.
+ arg1: 
+
+<!-- ##### FUNCTION e_dbus_service_run ##### -->
+<para>
+
+</para>
+
+ service: 
+
+
+<!-- ##### FUNCTION e_dbus_service_quit ##### -->
+<para>
+
+</para>
+
+ service: 
+
+
+<!-- ##### FUNCTION e_dbus_service_load_modules ##### -->
+<para>
+
+</para>
+
+ service: 
+
+
diff --git a/libebackend/e-dbus-service.c b/libebackend/e-dbus-service.c
new file mode 100644
index 0000000..09eba0a
--- /dev/null
+++ b/libebackend/e-dbus-service.c
@@ -0,0 +1,251 @@
+/*
+ * e-dbus-service.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+/**
+ * SECTION: e-dbus-service
+ * @short_description: an abstract base class for a D-Bus service
+ * @include: libebackend/e-dbus-service
+ **/
+
+#include "e-dbus-service.h"
+
+#include <config.h>
+
+#ifdef G_OS_UNIX
+#include <glib-unix.h>
+#endif
+
+#include <libebackend/e-module.h>
+#include <libebackend/e-extensible.h>
+
+#define E_DBUS_SERVICE_GET_PRIVATE(obj) \
+	(G_TYPE_INSTANCE_GET_PRIVATE \
+	((obj), E_TYPE_DBUS_SERVICE, EDBusServicePrivate))
+
+struct _EDBusServicePrivate {
+	GMainLoop *main_loop;
+	guint bus_owner_id;
+	guint terminate_id;
+};
+
+enum {
+	BUS_ACQUIRED,
+	BUS_NAME_ACQUIRED,
+	BUS_NAME_LOST,
+	LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL];
+
+G_DEFINE_ABSTRACT_TYPE_WITH_CODE (
+	EDBusService, e_dbus_service, G_TYPE_OBJECT,
+	G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))
+
+static void
+dbus_service_bus_acquired_cb (GDBusConnection *connection,
+                              const gchar *bus_name,
+                              EDBusService *service)
+{
+	g_signal_emit (service, signals[BUS_ACQUIRED], 0, connection);
+}
+
+static void
+dbus_service_name_acquired_cb (GDBusConnection *connection,
+                               const gchar *bus_name,
+                               EDBusService *service)
+{
+	g_signal_emit (service, signals[BUS_NAME_ACQUIRED], 0, connection);
+}
+
+static void
+dbus_service_name_lost_cb (GDBusConnection *connection,
+                           const gchar *bus_name,
+                           EDBusService *service)
+{
+	g_signal_emit (service, signals[BUS_NAME_LOST], 0, connection);
+}
+
+#ifdef G_OS_UNIX
+static gboolean
+dbus_service_terminate_cb (EDBusService *service)
+{
+	g_print ("Received terminate signal.\n");
+	e_dbus_service_quit (service);
+
+	return FALSE;
+}
+#endif
+
+static void
+dbus_service_finalize (GObject *object)
+{
+	EDBusServicePrivate *priv;
+
+	priv = E_DBUS_SERVICE_GET_PRIVATE (object);
+
+	g_main_loop_unref (priv->main_loop);
+	g_bus_unown_name (priv->bus_owner_id);
+
+	if (priv->terminate_id > 0)
+		g_source_remove (priv->terminate_id);
+
+	/* Chain up to parent's finalize() method. */
+	G_OBJECT_CLASS (e_dbus_service_parent_class)->finalize (object);
+}
+
+static void
+dbus_service_bus_acquired (EDBusService *service,
+                           GDBusConnection *connection)
+{
+	/* Placeholder so subclasses can safely chain up. */
+}
+
+static void
+dbus_service_bus_name_acquired (EDBusService *service,
+                                GDBusConnection *connection)
+{
+	EDBusServiceClass *class;
+
+	class = E_DBUS_SERVICE_GET_CLASS (service);
+	g_return_if_fail (class->bus_name != NULL);
+
+	g_print ("Bus name '%s' acquired.\n", class->bus_name);
+}
+
+static void
+dbus_service_bus_name_lost (EDBusService *service,
+                            GDBusConnection *connection)
+{
+	EDBusServiceClass *class;
+
+	class = E_DBUS_SERVICE_GET_CLASS (service);
+	g_return_if_fail (class->bus_name != NULL);
+
+	g_print ("Bus name '%s' lost.\n", class->bus_name);
+
+	e_dbus_service_quit (service);
+}
+
+static void
+e_dbus_service_class_init (EDBusServiceClass *class)
+{
+	GObjectClass *object_class;
+
+	g_type_class_add_private (class, sizeof (EDBusServicePrivate));
+
+	object_class = G_OBJECT_CLASS (class);
+	object_class->finalize = dbus_service_finalize;
+
+	class->bus_acquired = dbus_service_bus_acquired;
+	class->bus_name_acquired = dbus_service_bus_name_acquired;
+	class->bus_name_lost = dbus_service_bus_name_lost;
+
+	signals[BUS_ACQUIRED] = g_signal_new (
+		"bus-acquired",
+		G_OBJECT_CLASS_TYPE (object_class),
+		G_SIGNAL_RUN_LAST,
+		G_STRUCT_OFFSET (EDBusServiceClass, bus_acquired),
+		NULL, NULL,
+		g_cclosure_marshal_VOID__OBJECT,
+		G_TYPE_NONE, 1,
+		G_TYPE_DBUS_CONNECTION);
+
+	signals[BUS_NAME_ACQUIRED] = g_signal_new (
+		"bus-name-acquired",
+		G_OBJECT_CLASS_TYPE (object_class),
+		G_SIGNAL_RUN_LAST,
+		G_STRUCT_OFFSET (EDBusServiceClass, bus_name_acquired),
+		NULL, NULL,
+		g_cclosure_marshal_VOID__OBJECT,
+		G_TYPE_NONE, 1,
+		G_TYPE_DBUS_CONNECTION);
+
+	signals[BUS_NAME_LOST] = g_signal_new (
+		"bus-name-lost",
+		G_OBJECT_CLASS_TYPE (object_class),
+		G_SIGNAL_RUN_LAST,
+		G_STRUCT_OFFSET (EDBusServiceClass, bus_name_lost),
+		NULL, NULL,
+		g_cclosure_marshal_VOID__OBJECT,
+		G_TYPE_NONE, 1,
+		G_TYPE_DBUS_CONNECTION);
+}
+
+static void
+e_dbus_service_init (EDBusService *service)
+{
+	service->priv = E_DBUS_SERVICE_GET_PRIVATE (service);
+	service->priv->main_loop = g_main_loop_new (NULL, FALSE);
+
+#ifdef G_OS_UNIX
+	service->priv->terminate_id = g_unix_signal_add (
+		SIGTERM, (GSourceFunc) dbus_service_terminate_cb, service);
+#endif
+}
+
+void
+e_dbus_service_run (EDBusService *service)
+{
+	EDBusServiceClass *class;
+
+	g_return_if_fail (E_IS_DBUS_SERVICE (service));
+
+	if (g_main_loop_is_running (service->priv->main_loop))
+		return;
+
+	/* Try to acquire the well-known bus name. */
+
+	class = E_DBUS_SERVICE_GET_CLASS (service);
+	g_return_if_fail (class->bus_name != NULL);
+
+	service->priv->bus_owner_id = g_bus_own_name (
+		G_BUS_TYPE_SESSION,
+		class->bus_name,
+		G_BUS_NAME_OWNER_FLAGS_REPLACE |
+		G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
+		(GBusAcquiredCallback) dbus_service_bus_acquired_cb,
+		(GBusNameAcquiredCallback) dbus_service_name_acquired_cb,
+		(GBusNameLostCallback) dbus_service_name_lost_cb,
+		g_object_ref (service),
+		(GDestroyNotify) g_object_unref);
+
+	g_main_loop_run (service->priv->main_loop);
+}
+
+void
+e_dbus_service_quit (EDBusService *service)
+{
+	g_return_if_fail (E_IS_DBUS_SERVICE (service));
+
+	g_main_loop_quit (service->priv->main_loop);
+}
+
+void
+e_dbus_service_load_modules (EDBusService *service)
+{
+	EDBusServiceClass *class;
+	GList *list;
+
+	g_return_if_fail (E_IS_DBUS_SERVICE (service));
+
+	class = E_DBUS_SERVICE_GET_CLASS (service);
+	g_return_if_fail (class->module_directory != NULL);
+
+	list = e_module_load_all_in_directory (class->module_directory);
+	g_list_free_full (list, (GDestroyNotify) g_type_module_unuse);
+}
diff --git a/libebackend/e-dbus-service.h b/libebackend/e-dbus-service.h
new file mode 100644
index 0000000..294317f
--- /dev/null
+++ b/libebackend/e-dbus-service.h
@@ -0,0 +1,86 @@
+/*
+ * e-dbus-service.h
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifndef E_DBUS_SERVICE_H
+#define E_DBUS_SERVICE_H
+
+#include <gio/gio.h>
+
+/* Standard GObject macros */
+#define E_TYPE_DBUS_SERVICE \
+	(e_dbus_service_get_type ())
+#define E_DBUS_SERVICE(obj) \
+	(G_TYPE_CHECK_INSTANCE_CAST \
+	((obj), E_TYPE_DBUS_SERVICE, EDBusService))
+#define E_DBUS_SERVICE_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_CAST \
+	((cls), E_TYPE_DBUS_SERVICE, EDBusServiceClass))
+#define E_IS_DBUS_SERVICE(obj) \
+	(G_TYPE_CHECK_INSTANCE_TYPE \
+	((obj), E_TYPE_DBUS_SERVICE))
+#define E_IS_DBUS_SERVICE_CLASS(cls) \
+	(G_TYPE_CHECK_CLASS_TYPE \
+	((cls), E_TYPE_DBUS_SERVICE))
+#define E_DBUS_SERVICE_GET_CLASS(obj) \
+	(G_TYPE_INSTANCE_GET_CLASS \
+	((obj), E_TYPE_DBUS_SERVICE, EDBusServiceClass))
+
+G_BEGIN_DECLS
+
+typedef struct _EDBusService EDBusService;
+typedef struct _EDBusServiceClass EDBusServiceClass;
+typedef struct _EDBusServicePrivate EDBusServicePrivate;
+
+/**
+ * EDBusService:
+ *
+ * Contains only private data that should be read and manipulated using the
+ * functions below.
+ *
+ * Since: 3.4
+ **/
+struct _EDBusService {
+	GObject parent;
+	EDBusServicePrivate *priv;
+};
+
+struct _EDBusServiceClass {
+	GObjectClass parent_class;
+
+	const gchar *bus_name;
+	const gchar *module_directory;
+
+	/* Signals */
+	void		(*bus_acquired)		(EDBusService *service,
+						 GDBusConnection *connection);
+	void		(*bus_name_acquired)	(EDBusService *service,
+						 GDBusConnection *connection);
+	void		(*bus_name_lost)	(EDBusService *service,
+						 GDBusConnection *connection);
+
+	gpointer reserved[16];
+};
+
+GType		e_dbus_service_get_type		(void) G_GNUC_CONST;
+void		e_dbus_service_run		(EDBusService *service);
+void		e_dbus_service_quit		(EDBusService *service);
+void		e_dbus_service_load_modules	(EDBusService *service);
+
+G_END_DECLS
+
+#endif /* E_DBUS_SERVICE_H */



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