From 7de9b40977b2b0cb51bac643f130399670dd50a3 Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Sun, 7 Oct 2012 17:41:41 -0400 Subject: [PATCH] Remove e_data_book_register_gdbus_object(). The first thing we do after creating a new EDataBook is export its D-Bus interface. This was a separate and failable operation. If the exporting fails, the EDataBook is useless. Perfect use case for GInitable. Now we pass the GDBusConnection and object path directly to e_data_book_new(), and if the exporting fails the function sets a GError and returns NULL. This also introduces a couple accessor functions: e_data_book_get_connection() e_data_book_get_object_path() --- addressbook/libedata-book/e-data-book-factory.c | 49 +++-- addressbook/libedata-book/e-data-book.c | 222 ++++++++++++++++++--- addressbook/libedata-book/e-data-book.h | 12 +- .../libedata-book/libedata-book-sections.txt | 3 +- 4 files changed, 229 insertions(+), 57 deletions(-) diff --git a/addressbook/libedata-book/e-data-book-factory.c b/addressbook/libedata-book/e-data-book-factory.c index f286604..cca4c02 100644 --- a/addressbook/libedata-book/e-data-book-factory.c +++ b/addressbook/libedata-book/e-data-book-factory.c @@ -302,7 +302,7 @@ impl_BookFactory_get_book (EGdbusBookFactory *object, GDBusConnection *connection; ESourceRegistry *registry; ESource *source; - gchar *path; + gchar *object_path; const gchar *sender; GList *list; GError *error = NULL; @@ -357,33 +357,42 @@ impl_BookFactory_get_book (EGdbusBookFactory *object, book_backend_factory_match_goa_object (factory, backend); #endif - path = construct_book_factory_path (); - book = e_data_book_new (E_BOOK_BACKEND (backend)); - g_hash_table_insert (priv->books, g_strdup (path), book); - e_book_backend_add_client (E_BOOK_BACKEND (backend), book); - e_data_book_register_gdbus_object (book, connection, path, &error); - g_object_weak_ref ( - G_OBJECT (book), (GWeakNotify) - book_freed_cb, factory); + object_path = construct_book_factory_path (); - g_object_unref (backend); + book = e_data_book_new ( + E_BOOK_BACKEND (backend), + connection, object_path, &error); - /* Update the hash of open connections. */ - g_mutex_lock (priv->connections_lock); - list = g_hash_table_lookup (priv->connections, sender); - list = g_list_prepend (list, book); - g_hash_table_insert (priv->connections, g_strdup (sender), list); - g_mutex_unlock (priv->connections_lock); + if (book != NULL) { + g_mutex_lock (priv->books_lock); + g_hash_table_insert ( + priv->books, g_strdup (object_path), book); + g_mutex_unlock (priv->books_lock); - g_mutex_unlock (priv->books_lock); + e_book_backend_add_client (E_BOOK_BACKEND (backend), book); + + g_object_weak_ref ( + G_OBJECT (book), (GWeakNotify) + book_freed_cb, factory); + + /* Update the hash of open connections. */ + g_mutex_lock (priv->connections_lock); + list = g_hash_table_lookup (priv->connections, sender); + list = g_list_prepend (list, book); + g_hash_table_insert ( + priv->connections, g_strdup (sender), list); + g_mutex_unlock (priv->connections_lock); + } + + g_object_unref (backend); e_gdbus_book_factory_complete_get_book ( - object, invocation, path, error); + object, invocation, object_path, error); - if (error) + if (error != NULL) g_error_free (error); - g_free (path); + g_free (object_path); return TRUE; } diff --git a/addressbook/libedata-book/e-data-book.c b/addressbook/libedata-book/e-data-book.c index d474736..ddb9261 100644 --- a/addressbook/libedata-book/e-data-book.c +++ b/addressbook/libedata-book/e-data-book.c @@ -39,11 +39,11 @@ (G_TYPE_INSTANCE_GET_PRIVATE \ ((obj), E_TYPE_DATA_BOOK, EDataBookPrivate)) -struct _EDataBookPrivate -{ +struct _EDataBookPrivate { + GDBusConnection *connection; EGdbusBook *gdbus_object; - EBookBackend *backend; + gchar *object_path; GStaticRecMutex pending_ops_lock; GHashTable *pending_ops; /* opid to GCancellable for still running operations */ @@ -51,7 +51,9 @@ struct _EDataBookPrivate enum { PROP_0, - PROP_BACKEND + PROP_BACKEND, + PROP_CONNECTION, + PROP_OBJECT_PATH }; static EOperationPool *ops_pool = NULL; @@ -109,7 +111,16 @@ typedef struct { } d; } OperationData; -G_DEFINE_TYPE (EDataBook, e_data_book, G_TYPE_OBJECT) +/* Forward Declarations */ +static void e_data_book_initable_init (GInitableIface *interface); + +G_DEFINE_TYPE_WITH_CODE ( + EDataBook, + e_data_book, + G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE ( + G_TYPE_INITABLE, + e_data_book_initable_init)) static gchar * construct_bookview_path (void) @@ -1143,27 +1154,6 @@ e_data_book_report_backend_property_changed (EDataBook *book, g_strfreev (strv); } -/** - * e_data_book_register_gdbus_object: - * - * Registers GDBus object of this EDataBook. - * - * Since: 2.32 - **/ -guint -e_data_book_register_gdbus_object (EDataBook *book, - GDBusConnection *connection, - const gchar *object_path, - GError **error) -{ - g_return_val_if_fail (book != NULL, 0); - g_return_val_if_fail (E_IS_DATA_BOOK (book), 0); - g_return_val_if_fail (connection != NULL, 0); - g_return_val_if_fail (object_path != NULL, 0); - - return e_gdbus_book_register_object (book->priv->gdbus_object, connection, object_path, error); -} - static void data_book_set_backend (EDataBook *book, EBookBackend *backend) @@ -1175,6 +1165,26 @@ data_book_set_backend (EDataBook *book, } static void +data_book_set_connection (EDataBook *book, + GDBusConnection *connection) +{ + g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); + g_return_if_fail (book->priv->connection == NULL); + + book->priv->connection = g_object_ref (connection); +} + +static void +data_book_set_object_path (EDataBook *book, + const gchar *object_path) +{ + g_return_if_fail (object_path != NULL); + g_return_if_fail (book->priv->object_path == NULL); + + book->priv->object_path = g_strdup (object_path); +} + +static void data_book_set_property (GObject *object, guint property_id, const GValue *value, @@ -1186,6 +1196,18 @@ data_book_set_property (GObject *object, E_DATA_BOOK (object), g_value_get_object (value)); return; + + case PROP_CONNECTION: + data_book_set_connection ( + E_DATA_BOOK (object), + g_value_get_object (value)); + return; + + case PROP_OBJECT_PATH: + data_book_set_object_path ( + E_DATA_BOOK (object), + g_value_get_string (value)); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -1204,6 +1226,20 @@ data_book_get_property (GObject *object, e_data_book_get_backend ( E_DATA_BOOK (object))); return; + + case PROP_CONNECTION: + g_value_set_object ( + value, + e_data_book_get_connection ( + E_DATA_BOOK (object))); + return; + + case PROP_OBJECT_PATH: + g_value_set_string ( + value, + e_data_book_get_object_path ( + E_DATA_BOOK (object))); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -1216,7 +1252,12 @@ data_book_dispose (GObject *object) priv = E_DATA_BOOK_GET_PRIVATE (object); - if (priv->backend) { + if (priv->connection != NULL) { + g_object_unref (priv->connection); + priv->connection = NULL; + } + + if (priv->backend != NULL) { g_object_unref (priv->backend); priv->backend = NULL; } @@ -1232,6 +1273,8 @@ data_book_finalize (GObject *object) priv = E_DATA_BOOK_GET_PRIVATE (object); + g_free (priv->object_path); + if (priv->pending_ops) { g_hash_table_destroy (priv->pending_ops); priv->pending_ops = NULL; @@ -1248,6 +1291,22 @@ data_book_finalize (GObject *object) G_OBJECT_CLASS (e_data_book_parent_class)->finalize (object); } +static gboolean +data_book_initable_init (GInitable *initable, + GCancellable *cancellable, + GError **error) +{ + EDataBook *book; + + book = E_DATA_BOOK (initable); + + return e_gdbus_book_register_object ( + book->priv->gdbus_object, + book->priv->connection, + book->priv->object_path, + error); +} + static void e_data_book_class_init (EDataBookClass *class) { @@ -1273,11 +1332,43 @@ e_data_book_class_init (EDataBookClass *class) G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property ( + object_class, + PROP_CONNECTION, + g_param_spec_object ( + "connection", + "Connection", + "The GDBusConnection on which to " + "export the address book interface", + G_TYPE_DBUS_CONNECTION, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property ( + object_class, + PROP_OBJECT_PATH, + g_param_spec_string ( + "object-path", + "Object Path", + "The object path at which to " + "export the address book interface", + NULL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + if (!ops_pool) ops_pool = e_operation_pool_new (10, operation_thread, NULL); } static void +e_data_book_initable_init (GInitableIface *interface) +{ + interface->init = data_book_initable_init; +} + +static void e_data_book_init (EDataBook *ebook) { EGdbusBook *gdbus_object; @@ -1334,14 +1425,47 @@ e_data_book_init (EDataBook *ebook) G_CALLBACK (impl_Book_close), ebook); } +/** + * e_data_book_new: + * @backend: an #EBookBackend + * @connection: a #GDBusConnection + * @object_path: object path for the D-Bus interface + * @error: return location for a #GError, or %NULL + * + * Creates a new #EDataBook and exports the AddressBook D-Bus interface + * on @connection at @object_path. The #EDataBook handles incoming remote + * method invocations and forwards them to the @backend. If the AddressBook + * interface fails to export, the function sets @error and returns %NULL. + * + * Returns: an #EDataBook, or %NULL on error + **/ EDataBook * -e_data_book_new (EBookBackend *backend) +e_data_book_new (EBookBackend *backend, + GDBusConnection *connection, + const gchar *object_path, + GError **error) { g_return_val_if_fail (E_IS_BOOK_BACKEND (backend), NULL); - - return g_object_new (E_TYPE_DATA_BOOK, "backend", backend, NULL); + g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); + g_return_val_if_fail (object_path != NULL, NULL); + + return g_initable_new ( + E_TYPE_DATA_BOOK, NULL, error, + "backend", backend, + "connection", connection, + "object-path", object_path, + NULL); } +/** + * e_data_book_get_backend: + * @book: an #EDataBook + * + * Returns the #EBookBackend to which incoming remote method invocations + * are being forwarded. + * + * Returns: the #EBookBackend + **/ EBookBackend * e_data_book_get_backend (EDataBook *book) { @@ -1350,3 +1474,41 @@ e_data_book_get_backend (EDataBook *book) return book->priv->backend; } +/** + * e_data_book_get_connection: + * @book: an #EDataBook + * + * Returns the #GDBusConnection on which the AddressBook D-Bus interface + * is exported. + * + * Returns: the #GDBusConnection + * + * Since: 3.8 + **/ +GDBusConnection * +e_data_book_get_connection (EDataBook *book) +{ + g_return_val_if_fail (E_IS_DATA_BOOK (book), NULL); + + return book->priv->connection; +} + +/** + * e_data_book_get_object_path: + * @book: an #EDataBook + * + * Returns the object path at which the AddressBook D-Bus interface is + * exported. + * + * Returns: the object path + * + * Since: 3.8 + **/ +const gchar * +e_data_book_get_object_path (EDataBook *book) +{ + g_return_val_if_fail (E_IS_DATA_BOOK (book), NULL); + + return book->priv->object_path; +} + diff --git a/addressbook/libedata-book/e-data-book.h b/addressbook/libedata-book/e-data-book.h index e434cdf..e0827b8 100644 --- a/addressbook/libedata-book/e-data-book.h +++ b/addressbook/libedata-book/e-data-book.h @@ -142,15 +142,15 @@ const gchar * e_data_book_status_to_string (EDataBookStatus status); } G_STMT_END GType e_data_book_get_type (void) G_GNUC_CONST; -EDataBook * e_data_book_new (struct _EBookBackend *backend); -struct _EBookBackend * - e_data_book_get_backend (EDataBook *book); - -guint e_data_book_register_gdbus_object - (EDataBook *cal, +EDataBook * e_data_book_new (struct _EBookBackend *backend, GDBusConnection *connection, const gchar *object_path, GError **error); +struct _EBookBackend * + e_data_book_get_backend (EDataBook *book); +GDBusConnection * + e_data_book_get_connection (EDataBook *book); +const gchar * e_data_book_get_object_path (EDataBook *book); void e_data_book_respond_open (EDataBook *book, guint32 opid, diff --git a/docs/reference/addressbook/libedata-book/libedata-book-sections.txt b/docs/reference/addressbook/libedata-book/libedata-book-sections.txt index 6089dee..7dfc7f9 100644 --- a/docs/reference/addressbook/libedata-book/libedata-book-sections.txt +++ b/docs/reference/addressbook/libedata-book/libedata-book-sections.txt @@ -265,7 +265,8 @@ e_return_data_book_error_if_fail e_return_data_book_error_val_if_fail e_data_book_new e_data_book_get_backend -e_data_book_register_gdbus_object +e_data_book_get_connection +e_data_book_get_object_path e_data_book_respond_open e_data_book_respond_refresh e_data_book_respond_get_backend_property -- 2.7.4