[evolution-data-server] e-book-client.c cleanups.
- From: Matthew Barnes <mbarnes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server] e-book-client.c cleanups.
- Date: Sun, 7 Oct 2012 22:40:02 +0000 (UTC)
commit 94a6891c577141058f115ccc54376f4e4c881555
Author: Matthew Barnes <mbarnes redhat com>
Date: Sat Oct 6 18:18:18 2012 -0400
e-book-client.c cleanups.
Mostly just code rearrangement.
addressbook/libebook/e-book-client.c | 1092 +++++++++++++++++-----------------
1 files changed, 558 insertions(+), 534 deletions(-)
---
diff --git a/addressbook/libebook/e-book-client.c b/addressbook/libebook/e-book-client.c
index 8143a50..352e03f 100644
--- a/addressbook/libebook/e-book-client.c
+++ b/addressbook/libebook/e-book-client.c
@@ -184,46 +184,46 @@ set_proxy_gone_error (GError **error)
}
static guint active_book_clients = 0, book_connection_closed_id = 0;
-static EGdbusBookFactory *book_factory_proxy = NULL;
-static GStaticRecMutex book_factory_proxy_lock = G_STATIC_REC_MUTEX_INIT;
-#define LOCK_FACTORY() g_static_rec_mutex_lock (&book_factory_proxy_lock)
-#define UNLOCK_FACTORY() g_static_rec_mutex_unlock (&book_factory_proxy_lock)
+static EGdbusBookFactory *book_factory = NULL;
+static GStaticRecMutex book_factory_lock = G_STATIC_REC_MUTEX_INIT;
+#define LOCK_FACTORY() g_static_rec_mutex_lock (&book_factory_lock)
+#define UNLOCK_FACTORY() g_static_rec_mutex_unlock (&book_factory_lock)
-static void gdbus_book_factory_proxy_closed_cb (GDBusConnection *connection, gboolean remote_peer_vanished, GError *error, gpointer user_data);
+static void gdbus_book_factory_closed_cb (GDBusConnection *connection, gboolean remote_peer_vanished, GError *error, gpointer user_data);
static void
-gdbus_book_factory_proxy_disconnect (GDBusConnection *connection)
+gdbus_book_factory_disconnect (GDBusConnection *connection)
{
LOCK_FACTORY ();
- if (!connection && book_factory_proxy)
- connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory_proxy));
+ if (!connection && book_factory)
+ connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory));
if (connection && book_connection_closed_id) {
g_dbus_connection_signal_unsubscribe (connection, book_connection_closed_id);
- g_signal_handlers_disconnect_by_func (connection, gdbus_book_factory_proxy_closed_cb, NULL);
+ g_signal_handlers_disconnect_by_func (connection, gdbus_book_factory_closed_cb, NULL);
}
- if (book_factory_proxy)
- g_object_unref (book_factory_proxy);
+ if (book_factory != NULL)
+ g_object_unref (book_factory);
book_connection_closed_id = 0;
- book_factory_proxy = NULL;
+ book_factory = NULL;
UNLOCK_FACTORY ();
}
static void
-gdbus_book_factory_proxy_closed_cb (GDBusConnection *connection,
- gboolean remote_peer_vanished,
- GError *error,
- gpointer user_data)
+gdbus_book_factory_closed_cb (GDBusConnection *connection,
+ gboolean remote_peer_vanished,
+ GError *error,
+ gpointer user_data)
{
GError *err = NULL;
LOCK_FACTORY ();
- gdbus_book_factory_proxy_disconnect (connection);
+ gdbus_book_factory_disconnect (connection);
if (error)
unwrap_dbus_error (g_error_copy (error), &err);
@@ -249,36 +249,37 @@ gdbus_book_factory_connection_gone_cb (GDBusConnection *connection,
{
/* signal subscription takes care of correct parameters,
* thus just do what is to be done here */
- gdbus_book_factory_proxy_closed_cb (connection, TRUE, NULL, user_data);
+ gdbus_book_factory_closed_cb (connection, TRUE, NULL, user_data);
}
static gboolean
-gdbus_book_factory_activate (GError **error)
+gdbus_book_factory_activate (GCancellable *cancellable,
+ GError **error)
{
GDBusConnection *connection;
LOCK_FACTORY ();
- if (G_LIKELY (book_factory_proxy)) {
+ if (G_LIKELY (book_factory != NULL)) {
UNLOCK_FACTORY ();
return TRUE;
}
- book_factory_proxy = e_gdbus_book_factory_proxy_new_for_bus_sync (
+ book_factory = e_gdbus_book_factory_proxy_new_for_bus_sync (
G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
ADDRESS_BOOK_DBUS_SERVICE_NAME,
"/org/gnome/evolution/dataserver/AddressBookFactory",
- NULL,
- error);
+ cancellable, error);
- if (!book_factory_proxy) {
+ if (book_factory == NULL) {
UNLOCK_FACTORY ();
return FALSE;
}
- connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory_proxy));
- book_connection_closed_id = g_dbus_connection_signal_subscribe (connection,
+ connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory));
+ book_connection_closed_id = g_dbus_connection_signal_subscribe (
+ connection,
NULL, /* sender */
"org.freedesktop.DBus", /* interface */
"NameOwnerChanged", /* member */
@@ -287,7 +288,9 @@ gdbus_book_factory_activate (GError **error)
G_DBUS_SIGNAL_FLAGS_NONE,
gdbus_book_factory_connection_gone_cb, NULL, NULL);
- g_signal_connect (connection, "closed", G_CALLBACK (gdbus_book_factory_proxy_closed_cb), NULL);
+ g_signal_connect (
+ connection, "closed",
+ G_CALLBACK (gdbus_book_factory_closed_cb), NULL);
UNLOCK_FACTORY ();
@@ -450,369 +453,316 @@ contact_slist_to_utf8_vcard_array (GSList *contacts)
return array;
}
-/**
- * e_book_client_new:
- * @source: An #ESource pointer
- * @error: A #GError pointer
- *
- * Creates a new #EBookClient corresponding to the given source. There are
- * only two operations that are valid on this book at this point:
- * e_client_open(), and e_client_remove().
- *
- * Returns: a new but unopened #EBookClient.
- *
- * Since: 3.2
- **/
-EBookClient *
-e_book_client_new (ESource *source,
- GError **error)
+static gboolean
+book_client_get_backend_property_from_cache_finish (EClient *client,
+ GAsyncResult *result,
+ gchar **prop_value,
+ GError **error)
{
- EBookClient *client;
- GError *err = NULL;
- GDBusConnection *connection;
- const gchar *uid;
- gchar *path = NULL;
+ GSimpleAsyncResult *simple;
+ GError *local_error = NULL;
- g_return_val_if_fail (source != NULL, NULL);
- g_return_val_if_fail (E_IS_SOURCE (source), NULL);
+ g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
+ g_return_val_if_fail (result != NULL, FALSE);
+ g_return_val_if_fail (prop_value != NULL, FALSE);
+ g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (client), book_client_get_backend_property_from_cache_finish), FALSE);
- LOCK_FACTORY ();
- if (!gdbus_book_factory_activate (&err)) {
- UNLOCK_FACTORY ();
- if (err) {
- unwrap_dbus_error (err, &err);
- g_warning ("%s: Failed to run book factory: %s", G_STRFUNC, err->message);
- g_propagate_error (error, err);
- } else {
- g_warning ("%s: Failed to run book factory: Unknown error", G_STRFUNC);
- g_set_error_literal (error, E_CLIENT_ERROR, E_CLIENT_ERROR_DBUS_ERROR, _("Failed to run book factory"));
- }
+ simple = G_SIMPLE_ASYNC_RESULT (result);
- return NULL;
+ if (g_simple_async_result_propagate_error (simple, &local_error)) {
+ e_client_unwrap_dbus_error (client, local_error, error);
+ return FALSE;
}
- uid = e_source_get_uid (source);
+ *prop_value = g_strdup (g_simple_async_result_get_op_res_gpointer (simple));
- client = g_object_new (E_TYPE_BOOK_CLIENT, "source", source, NULL);
- UNLOCK_FACTORY ();
+ return *prop_value != NULL;
+}
- if (!e_gdbus_book_factory_call_get_book_sync (G_DBUS_PROXY (book_factory_proxy), uid, &path, NULL, &err)) {
- unwrap_dbus_error (err, &err);
- g_warning ("%s: Cannot get book from factory: %s", G_STRFUNC, err ? err->message : "[no error]");
- if (err)
- g_propagate_error (error, err);
- g_object_unref (client);
+static void
+book_client_dispose (GObject *object)
+{
+ EClient *client;
- return NULL;
- }
+ client = E_CLIENT (object);
- client->priv->gdbus_book = G_DBUS_PROXY (e_gdbus_book_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory_proxy)),
- G_DBUS_PROXY_FLAGS_NONE,
- ADDRESS_BOOK_DBUS_SERVICE_NAME,
- path,
- NULL,
- &err));
+ e_client_cancel_all (client);
- if (!client->priv->gdbus_book) {
- g_free (path);
- unwrap_dbus_error (err, &err);
- g_warning ("%s: Cannot create cal proxy: %s", G_STRFUNC, err ? err->message : "Unknown error");
- if (err)
- g_propagate_error (error, err);
+ gdbus_book_client_disconnect (E_BOOK_CLIENT (client));
- g_object_unref (client);
+ /* Chain up to parent's dispose() method. */
+ G_OBJECT_CLASS (e_book_client_parent_class)->dispose (object);
+}
- return NULL;
- }
+static void
+book_client_finalize (GObject *object)
+{
+ /* Chain up to parent's finalize() method. */
+ G_OBJECT_CLASS (e_book_client_parent_class)->finalize (object);
- g_free (path);
+ LOCK_FACTORY ();
+ active_book_clients--;
+ if (!active_book_clients)
+ gdbus_book_factory_disconnect (NULL);
+ UNLOCK_FACTORY ();
+}
- connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (client->priv->gdbus_book));
- client->priv->gone_signal_id = g_dbus_connection_signal_subscribe (connection,
- "org.freedesktop.DBus", /* sender */
- "org.freedesktop.DBus", /* interface */
- "NameOwnerChanged", /* member */
- "/org/freedesktop/DBus", /* object_path */
- "org.gnome.evolution.dataserver.AddressBook", /* arg0 */
- G_DBUS_SIGNAL_FLAGS_NONE,
- gdbus_book_client_connection_gone_cb, client, NULL);
+static GDBusProxy *
+book_client_get_dbus_proxy (EClient *client)
+{
+ EBookClient *book_client;
- g_signal_connect (connection, "closed", G_CALLBACK (gdbus_book_client_closed_cb), client);
+ g_return_val_if_fail (E_IS_CLIENT (client), NULL);
- g_signal_connect (client->priv->gdbus_book, "backend_error", G_CALLBACK (backend_error_cb), client);
- g_signal_connect (client->priv->gdbus_book, "readonly", G_CALLBACK (readonly_cb), client);
- g_signal_connect (client->priv->gdbus_book, "online", G_CALLBACK (online_cb), client);
- g_signal_connect (client->priv->gdbus_book, "opened", G_CALLBACK (opened_cb), client);
- g_signal_connect (client->priv->gdbus_book, "backend-property-changed", G_CALLBACK (backend_property_changed_cb), client);
+ book_client = E_BOOK_CLIENT (client);
- return client;
+ return book_client->priv->gdbus_book;
}
-#define SELF_UID_PATH_ID "org.gnome.evolution-data-server.addressbook"
-#define SELF_UID_KEY "self-contact-uid"
+static void
+book_client_unwrap_dbus_error (EClient *client,
+ GError *dbus_error,
+ GError **out_error)
+{
+ unwrap_dbus_error (dbus_error, out_error);
+}
-static EContact *
-make_me_card (void)
+static void
+book_client_retrieve_capabilities (EClient *client,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
- GString *vcard;
- const gchar *s;
- EContact *contact;
+ g_return_if_fail (E_IS_BOOK_CLIENT (client));
- vcard = g_string_new ("BEGIN:VCARD\nVERSION:3.0\n");
+ e_client_get_backend_property (client, CLIENT_BACKEND_PROPERTY_CAPABILITIES, cancellable, callback, user_data);
+}
- s = g_get_user_name ();
- if (s)
- g_string_append_printf (vcard, "NICKNAME:%s\n", s);
+static gboolean
+book_client_retrieve_capabilities_finish (EClient *client,
+ GAsyncResult *result,
+ gchar **capabilities,
+ GError **error)
+{
+ g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- s = g_get_real_name ();
- if (s && strcmp (s, "Unknown") != 0) {
- ENameWestern *western;
+ return e_client_get_backend_property_finish (client, result, capabilities, error);
+}
- g_string_append_printf (vcard, "FN:%s\n", s);
+static gboolean
+book_client_retrieve_capabilities_sync (EClient *client,
+ gchar **capabilities,
+ GCancellable *cancellable,
+ GError **error)
+{
+ g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- western = e_name_western_parse (s);
- g_string_append_printf (vcard, "N:%s;%s;%s;%s;%s\n",
- western->last ? western->last : "",
- western->first ? western->first : "",
- western->middle ? western->middle : "",
- western->prefix ? western->prefix : "",
- western->suffix ? western->suffix : "");
- e_name_western_free (western);
+ return e_client_get_backend_property_sync (client, CLIENT_BACKEND_PROPERTY_CAPABILITIES, capabilities, cancellable, error);
+}
+
+static void
+book_client_get_backend_property (EClient *client,
+ const gchar *prop_name,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ gchar *prop_value;
+
+ prop_value = e_client_get_backend_property_from_cache (client, prop_name);
+ if (prop_value) {
+ e_client_finish_async_without_dbus (client, cancellable, callback, user_data, book_client_get_backend_property_from_cache_finish, prop_value, g_free);
+ } else {
+ e_client_proxy_call_string_with_res_op_data (client, prop_name, cancellable, callback, user_data, book_client_get_backend_property, prop_name,
+ e_gdbus_book_call_get_backend_property,
+ NULL, NULL, e_gdbus_book_call_get_backend_property_finish, NULL, NULL);
}
- g_string_append (vcard, "END:VCARD");
+}
- contact = e_contact_new_from_vcard (vcard->str);
+static gboolean
+book_client_get_backend_property_finish (EClient *client,
+ GAsyncResult *result,
+ gchar **prop_value,
+ GError **error)
+{
+ gchar *str = NULL;
+ gboolean res;
- g_string_free (vcard, TRUE);
+ g_return_val_if_fail (prop_value != NULL, FALSE);
- return contact;
+ if (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (result)) == book_client_get_backend_property_from_cache_finish) {
+ res = book_client_get_backend_property_from_cache_finish (client, result, &str, error);
+ } else {
+ res = e_client_proxy_call_finish_string (client, result, &str, error, book_client_get_backend_property);
+ if (res && str) {
+ const gchar *prop_name = g_object_get_data (G_OBJECT (result), "res-op-data");
+
+ if (prop_name && *prop_name)
+ e_client_update_backend_property_cache (client, prop_name, str);
+ }
+ }
+
+ *prop_value = str;
+
+ return res;
}
-/**
- * e_book_client_get_self:
- * @registry: an #ESourceRegistry
- * @contact: (out): an #EContact pointer to set
- * @client: (out): an #EBookClient pointer to set
- * @error: a #GError to set on failure
- *
- * Get the #EContact referring to the user of the address book
- * and set it in @contact and @client.
- *
- * Returns: %TRUE if successful, otherwise %FALSE.
- *
- * Since: 3.2
- **/
-gboolean
-e_book_client_get_self (ESourceRegistry *registry,
- EContact **contact,
- EBookClient **client,
- GError **error)
+static gboolean
+book_client_get_backend_property_sync (EClient *client,
+ const gchar *prop_name,
+ gchar **prop_value,
+ GCancellable *cancellable,
+ GError **error)
{
- ESource *source;
- GError *local_error = NULL;
- GSettings *settings;
- gchar *uid;
+ EBookClient *book_client;
+ gchar *prop_val;
+ gboolean res;
- g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
- g_return_val_if_fail (contact != NULL, FALSE);
g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- source = e_source_registry_ref_builtin_address_book (registry);
- *client = e_book_client_new (source, &local_error);
- g_object_unref (source);
+ book_client = E_BOOK_CLIENT (client);
- if (!*client) {
- g_propagate_error (error, local_error);
+ if (!book_client->priv->gdbus_book) {
+ set_proxy_gone_error (error);
return FALSE;
}
- if (!e_client_open_sync (E_CLIENT (*client), FALSE, NULL, &local_error)) {
- g_object_unref (*client);
- *client = NULL;
- g_propagate_error (error, local_error);
+ prop_val = e_client_get_backend_property_from_cache (client, prop_name);
+ if (prop_val) {
+ g_return_val_if_fail (prop_value != NULL, FALSE);
- return FALSE;
+ *prop_value = prop_val;
+
+ return TRUE;
}
- settings = g_settings_new (SELF_UID_PATH_ID);
- uid = g_settings_get_string (settings, SELF_UID_KEY);
- g_object_unref (settings);
-
- if (uid) {
- gboolean got;
-
- /* Don't care about errors because we'll create a new card on failure */
- got = e_book_client_get_contact_sync (*client, uid, contact, NULL, NULL);
- g_free (uid);
- if (got)
- return TRUE;
- }
-
- uid = NULL;
- *contact = make_me_card ();
- if (!e_book_client_add_contact_sync (*client, *contact, &uid, NULL, &local_error)) {
- g_object_unref (*client);
- *client = NULL;
- g_object_unref (*contact);
- *contact = NULL;
- g_propagate_error (error, local_error);
- return FALSE;
- }
-
- if (uid) {
- e_contact_set (*contact, E_CONTACT_UID, uid);
- g_free (uid);
- }
+ res = e_client_proxy_call_sync_string__string (client, prop_name, prop_value, cancellable, error, e_gdbus_book_call_get_backend_property_sync);
- e_book_client_set_self (*client, *contact, NULL);
+ if (res && prop_value)
+ e_client_update_backend_property_cache (client, prop_name, *prop_value);
- return TRUE;
+ return res;
}
-/**
- * e_book_client_set_self:
- * @client: an #EBookClient
- * @contact: an #EContact
- * @error: a #GError to set on failure
- *
- * Specify that @contact residing in @client is the #EContact that
- * refers to the user of the address book.
- *
- * Returns: %TRUE if successful, %FALSE otherwise.
- *
- * Since: 3.2
- **/
-gboolean
-e_book_client_set_self (EBookClient *client,
- EContact *contact,
- GError **error)
+static void
+book_client_set_backend_property (EClient *client,
+ const gchar *prop_name,
+ const gchar *prop_value,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
- GSettings *settings;
+ gchar **prop_name_value;
- g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- g_return_val_if_fail (contact != NULL, FALSE);
- g_return_val_if_fail (e_contact_get_const (contact, E_CONTACT_UID) != NULL, FALSE);
+ prop_name_value = e_gdbus_book_encode_set_backend_property (prop_name, prop_value);
- settings = g_settings_new (SELF_UID_PATH_ID);
- g_settings_set_string (settings, SELF_UID_KEY, e_contact_get_const (contact, E_CONTACT_UID));
- g_object_unref (settings);
+ e_client_proxy_call_strv (client, (const gchar * const *) prop_name_value, cancellable, callback, user_data, book_client_set_backend_property,
+ e_gdbus_book_call_set_backend_property,
+ e_gdbus_book_call_set_backend_property_finish, NULL, NULL, NULL, NULL);
- return TRUE;
+ g_strfreev (prop_name_value);
}
-/**
- * e_book_client_is_self:
- * @contact: an #EContact
- *
- * Check if @contact is the user of the address book.
- *
- * Returns: %TRUE if @contact is the user, %FALSE otherwise.
- *
- * Since: 3.2
- **/
-gboolean
-e_book_client_is_self (EContact *contact)
+static gboolean
+book_client_set_backend_property_finish (EClient *client,
+ GAsyncResult *result,
+ GError **error)
{
- GSettings *settings;
- gchar *uid;
- gboolean is_self;
-
- g_return_val_if_fail (contact && E_IS_CONTACT (contact), FALSE);
-
- settings = g_settings_new (SELF_UID_PATH_ID);
- uid = g_settings_get_string (settings, SELF_UID_KEY);
- g_object_unref (settings);
-
- is_self = uid && !g_strcmp0 (uid, e_contact_get_const (contact, E_CONTACT_UID));
-
- g_free (uid);
-
- return is_self;
+ return e_client_proxy_call_finish_void (client, result, error, book_client_set_backend_property);
}
static gboolean
-book_client_get_backend_property_from_cache_finish (EClient *client,
- GAsyncResult *result,
- gchar **prop_value,
- GError **error)
+book_client_set_backend_property_sync (EClient *client,
+ const gchar *prop_name,
+ const gchar *prop_value,
+ GCancellable *cancellable,
+ GError **error)
{
- GSimpleAsyncResult *simple;
- GError *local_error = NULL;
+ EBookClient *book_client;
+ gboolean res;
+ gchar **prop_name_value;
g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- g_return_val_if_fail (result != NULL, FALSE);
- g_return_val_if_fail (prop_value != NULL, FALSE);
- g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (client), book_client_get_backend_property_from_cache_finish), FALSE);
- simple = G_SIMPLE_ASYNC_RESULT (result);
+ book_client = E_BOOK_CLIENT (client);
- if (g_simple_async_result_propagate_error (simple, &local_error)) {
- e_client_unwrap_dbus_error (client, local_error, error);
+ if (!book_client->priv->gdbus_book) {
+ set_proxy_gone_error (error);
return FALSE;
}
- *prop_value = g_strdup (g_simple_async_result_get_op_res_gpointer (simple));
+ prop_name_value = e_gdbus_book_encode_set_backend_property (prop_name, prop_value);
+ res = e_client_proxy_call_sync_strv__void (client, (const gchar * const *) prop_name_value, cancellable, error, e_gdbus_book_call_set_backend_property_sync);
+ g_strfreev (prop_name_value);
- return *prop_value != NULL;
+ return res;
}
static void
-book_client_get_backend_property (EClient *client,
- const gchar *prop_name,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
+book_client_open (EClient *client,
+ gboolean only_if_exists,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
- gchar *prop_value;
+ e_client_proxy_call_boolean (client, only_if_exists, cancellable, callback, user_data, book_client_open,
+ e_gdbus_book_call_open,
+ e_gdbus_book_call_open_finish, NULL, NULL, NULL, NULL);
+}
- prop_value = e_client_get_backend_property_from_cache (client, prop_name);
- if (prop_value) {
- e_client_finish_async_without_dbus (client, cancellable, callback, user_data, book_client_get_backend_property_from_cache_finish, prop_value, g_free);
- } else {
- e_client_proxy_call_string_with_res_op_data (client, prop_name, cancellable, callback, user_data, book_client_get_backend_property, prop_name,
- e_gdbus_book_call_get_backend_property,
- NULL, NULL, e_gdbus_book_call_get_backend_property_finish, NULL, NULL);
- }
+static gboolean
+book_client_open_finish (EClient *client,
+ GAsyncResult *result,
+ GError **error)
+{
+ return e_client_proxy_call_finish_void (client, result, error, book_client_open);
}
static gboolean
-book_client_get_backend_property_finish (EClient *client,
- GAsyncResult *result,
- gchar **prop_value,
- GError **error)
+book_client_open_sync (EClient *client,
+ gboolean only_if_exists,
+ GCancellable *cancellable,
+ GError **error)
{
- gchar *str = NULL;
- gboolean res;
+ EBookClient *book_client;
- g_return_val_if_fail (prop_value != NULL, FALSE);
+ g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
- if (g_simple_async_result_get_source_tag (G_SIMPLE_ASYNC_RESULT (result)) == book_client_get_backend_property_from_cache_finish) {
- res = book_client_get_backend_property_from_cache_finish (client, result, &str, error);
- } else {
- res = e_client_proxy_call_finish_string (client, result, &str, error, book_client_get_backend_property);
- if (res && str) {
- const gchar *prop_name = g_object_get_data (G_OBJECT (result), "res-op-data");
+ book_client = E_BOOK_CLIENT (client);
- if (prop_name && *prop_name)
- e_client_update_backend_property_cache (client, prop_name, str);
- }
+ if (!book_client->priv->gdbus_book) {
+ set_proxy_gone_error (error);
+ return FALSE;
}
- *prop_value = str;
+ return e_client_proxy_call_sync_boolean__void (client, only_if_exists, cancellable, error, e_gdbus_book_call_open_sync);
+}
- return res;
+static void
+book_client_refresh (EClient *client,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ e_client_proxy_call_void (client, cancellable, callback, user_data, book_client_refresh,
+ e_gdbus_book_call_refresh,
+ e_gdbus_book_call_refresh_finish, NULL, NULL, NULL, NULL);
}
static gboolean
-book_client_get_backend_property_sync (EClient *client,
- const gchar *prop_name,
- gchar **prop_value,
- GCancellable *cancellable,
- GError **error)
+book_client_refresh_finish (EClient *client,
+ GAsyncResult *result,
+ GError **error)
+{
+ return e_client_proxy_call_finish_void (client, result, error, book_client_refresh);
+}
+
+static gboolean
+book_client_refresh_sync (EClient *client,
+ GCancellable *cancellable,
+ GError **error)
{
EBookClient *book_client;
- gchar *prop_val;
- gboolean res;
g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
@@ -823,153 +773,349 @@ book_client_get_backend_property_sync (EClient *client,
return FALSE;
}
- prop_val = e_client_get_backend_property_from_cache (client, prop_name);
- if (prop_val) {
- g_return_val_if_fail (prop_value != NULL, FALSE);
-
- *prop_value = prop_val;
+ return e_client_proxy_call_sync_void__void (client, cancellable, error, e_gdbus_book_call_refresh_sync);
+}
- return TRUE;
- }
+static void
+e_book_client_class_init (EBookClientClass *class)
+{
+ GObjectClass *object_class;
+ EClientClass *client_class;
- res = e_client_proxy_call_sync_string__string (client, prop_name, prop_value, cancellable, error, e_gdbus_book_call_get_backend_property_sync);
+ g_type_class_add_private (class, sizeof (EBookClientPrivate));
- if (res && prop_value)
- e_client_update_backend_property_cache (client, prop_name, *prop_value);
+ object_class = G_OBJECT_CLASS (class);
+ object_class->dispose = book_client_dispose;
+ object_class->finalize = book_client_finalize;
- return res;
+ client_class = E_CLIENT_CLASS (class);
+ client_class->get_dbus_proxy = book_client_get_dbus_proxy;
+ client_class->unwrap_dbus_error = book_client_unwrap_dbus_error;
+ client_class->retrieve_capabilities = book_client_retrieve_capabilities;
+ client_class->retrieve_capabilities_finish = book_client_retrieve_capabilities_finish;
+ client_class->retrieve_capabilities_sync = book_client_retrieve_capabilities_sync;
+ client_class->get_backend_property = book_client_get_backend_property;
+ client_class->get_backend_property_finish = book_client_get_backend_property_finish;
+ client_class->get_backend_property_sync = book_client_get_backend_property_sync;
+ client_class->set_backend_property = book_client_set_backend_property;
+ client_class->set_backend_property_finish = book_client_set_backend_property_finish;
+ client_class->set_backend_property_sync = book_client_set_backend_property_sync;
+ client_class->open = book_client_open;
+ client_class->open_finish = book_client_open_finish;
+ client_class->open_sync = book_client_open_sync;
+ client_class->refresh = book_client_refresh;
+ client_class->refresh_finish = book_client_refresh_finish;
+ client_class->refresh_sync = book_client_refresh_sync;
}
static void
-book_client_set_backend_property (EClient *client,
- const gchar *prop_name,
- const gchar *prop_value,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
+e_book_client_init (EBookClient *client)
{
- gchar **prop_name_value;
+ LOCK_FACTORY ();
+ active_book_clients++;
+ UNLOCK_FACTORY ();
- prop_name_value = e_gdbus_book_encode_set_backend_property (prop_name, prop_value);
+ client->priv = E_BOOK_CLIENT_GET_PRIVATE (client);
+}
- e_client_proxy_call_strv (client, (const gchar * const *) prop_name_value, cancellable, callback, user_data, book_client_set_backend_property,
- e_gdbus_book_call_set_backend_property,
- e_gdbus_book_call_set_backend_property_finish, NULL, NULL, NULL, NULL);
+/**
+ * e_book_client_new:
+ * @source: An #ESource pointer
+ * @error: A #GError pointer
+ *
+ * Creates a new #EBookClient corresponding to the given source. There are
+ * only two operations that are valid on this book at this point:
+ * e_client_open(), and e_client_remove().
+ *
+ * Returns: a new but unopened #EBookClient.
+ *
+ * Since: 3.2
+ **/
+EBookClient *
+e_book_client_new (ESource *source,
+ GError **error)
+{
+ EBookClient *client;
+ GError *err = NULL;
+ GDBusConnection *connection;
+ const gchar *uid;
+ gchar *object_path = NULL;
- g_strfreev (prop_name_value);
+ g_return_val_if_fail (source != NULL, NULL);
+ g_return_val_if_fail (E_IS_SOURCE (source), NULL);
+
+ LOCK_FACTORY ();
+ /* XXX Oops, e_book_client_new() forgot to take a GCancellable. */
+ if (!gdbus_book_factory_activate (NULL, &err)) {
+ UNLOCK_FACTORY ();
+ if (err) {
+ unwrap_dbus_error (err, &err);
+ g_warning ("%s: Failed to run book factory: %s", G_STRFUNC, err->message);
+ g_propagate_error (error, err);
+ } else {
+ g_warning ("%s: Failed to run book factory: Unknown error", G_STRFUNC);
+ g_set_error_literal (error, E_CLIENT_ERROR, E_CLIENT_ERROR_DBUS_ERROR, _("Failed to run book factory"));
+ }
+
+ return NULL;
+ }
+
+ uid = e_source_get_uid (source);
+
+ client = g_object_new (E_TYPE_BOOK_CLIENT, "source", source, NULL);
+ UNLOCK_FACTORY ();
+
+ e_gdbus_book_factory_call_get_book_sync (
+ G_DBUS_PROXY (book_factory), uid, &object_path, NULL, &err);
+
+ /* Sanity check. */
+ g_return_val_if_fail (
+ ((object_path != NULL) && (err == NULL)) ||
+ ((object_path == NULL) && (err != NULL)), NULL);
+
+ if (err != NULL) {
+ unwrap_dbus_error (err, &err);
+ g_propagate_error (error, err);
+ g_object_unref (client);
+ return NULL;
+ }
+
+ connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory));
+
+ client->priv->gdbus_book = G_DBUS_PROXY (e_gdbus_book_proxy_new_sync (
+ connection,
+ G_DBUS_PROXY_FLAGS_NONE,
+ ADDRESS_BOOK_DBUS_SERVICE_NAME,
+ object_path,
+ NULL, &err));
+
+ g_free (object_path);
+
+ /* Sanity check. */
+ g_return_val_if_fail (
+ ((client->priv->gdbus_book != NULL) && (err == NULL)) ||
+ ((client->priv->gdbus_book == NULL) && (err != NULL)), NULL);
+
+ if (err != NULL) {
+ unwrap_dbus_error (err, &err);
+ g_propagate_error (error, err);
+ g_object_unref (client);
+ return NULL;
+ }
+
+ client->priv->gone_signal_id = g_dbus_connection_signal_subscribe (
+ connection,
+ "org.freedesktop.DBus", /* sender */
+ "org.freedesktop.DBus", /* interface */
+ "NameOwnerChanged", /* member */
+ "/org/freedesktop/DBus", /* object_path */
+ "org.gnome.evolution.dataserver.AddressBook", /* arg0 */
+ G_DBUS_SIGNAL_FLAGS_NONE,
+ gdbus_book_client_connection_gone_cb, client, NULL);
+
+ g_signal_connect (
+ connection, "closed",
+ G_CALLBACK (gdbus_book_client_closed_cb), client);
+
+ g_signal_connect (
+ client->priv->gdbus_book, "backend_error",
+ G_CALLBACK (backend_error_cb), client);
+ g_signal_connect (
+ client->priv->gdbus_book, "readonly",
+ G_CALLBACK (readonly_cb), client);
+ g_signal_connect (
+ client->priv->gdbus_book, "online",
+ G_CALLBACK (online_cb), client);
+ g_signal_connect (
+ client->priv->gdbus_book, "opened",
+ G_CALLBACK (opened_cb), client);
+ g_signal_connect (
+ client->priv->gdbus_book, "backend-property-changed",
+ G_CALLBACK (backend_property_changed_cb), client);
+
+ return client;
}
-static gboolean
-book_client_set_backend_property_finish (EClient *client,
- GAsyncResult *result,
- GError **error)
+#define SELF_UID_PATH_ID "org.gnome.evolution-data-server.addressbook"
+#define SELF_UID_KEY "self-contact-uid"
+
+static EContact *
+make_me_card (void)
{
- return e_client_proxy_call_finish_void (client, result, error, book_client_set_backend_property);
+ GString *vcard;
+ const gchar *s;
+ EContact *contact;
+
+ vcard = g_string_new ("BEGIN:VCARD\nVERSION:3.0\n");
+
+ s = g_get_user_name ();
+ if (s)
+ g_string_append_printf (vcard, "NICKNAME:%s\n", s);
+
+ s = g_get_real_name ();
+ if (s && strcmp (s, "Unknown") != 0) {
+ ENameWestern *western;
+
+ g_string_append_printf (vcard, "FN:%s\n", s);
+
+ western = e_name_western_parse (s);
+ g_string_append_printf (vcard, "N:%s;%s;%s;%s;%s\n",
+ western->last ? western->last : "",
+ western->first ? western->first : "",
+ western->middle ? western->middle : "",
+ western->prefix ? western->prefix : "",
+ western->suffix ? western->suffix : "");
+ e_name_western_free (western);
+ }
+ g_string_append (vcard, "END:VCARD");
+
+ contact = e_contact_new_from_vcard (vcard->str);
+
+ g_string_free (vcard, TRUE);
+
+ return contact;
}
-static gboolean
-book_client_set_backend_property_sync (EClient *client,
- const gchar *prop_name,
- const gchar *prop_value,
- GCancellable *cancellable,
- GError **error)
+/**
+ * e_book_client_get_self:
+ * @registry: an #ESourceRegistry
+ * @contact: (out): an #EContact pointer to set
+ * @client: (out): an #EBookClient pointer to set
+ * @error: a #GError to set on failure
+ *
+ * Get the #EContact referring to the user of the address book
+ * and set it in @contact and @client.
+ *
+ * Returns: %TRUE if successful, otherwise %FALSE.
+ *
+ * Since: 3.2
+ **/
+gboolean
+e_book_client_get_self (ESourceRegistry *registry,
+ EContact **contact,
+ EBookClient **client,
+ GError **error)
{
- EBookClient *book_client;
- gboolean res;
- gchar **prop_name_value;
+ ESource *source;
+ GError *local_error = NULL;
+ GSettings *settings;
+ gchar *uid;
+
+ g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), FALSE);
+ g_return_val_if_fail (contact != NULL, FALSE);
+ g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
+
+ source = e_source_registry_ref_builtin_address_book (registry);
+ *client = e_book_client_new (source, &local_error);
+ g_object_unref (source);
+
+ if (!*client) {
+ g_propagate_error (error, local_error);
+ return FALSE;
+ }
+
+ if (!e_client_open_sync (E_CLIENT (*client), FALSE, NULL, &local_error)) {
+ g_object_unref (*client);
+ *client = NULL;
+ g_propagate_error (error, local_error);
+
+ return FALSE;
+ }
+
+ settings = g_settings_new (SELF_UID_PATH_ID);
+ uid = g_settings_get_string (settings, SELF_UID_KEY);
+ g_object_unref (settings);
- g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
+ if (uid) {
+ gboolean got;
- book_client = E_BOOK_CLIENT (client);
+ /* Don't care about errors because we'll create a new card on failure */
+ got = e_book_client_get_contact_sync (*client, uid, contact, NULL, NULL);
+ g_free (uid);
+ if (got)
+ return TRUE;
+ }
- if (!book_client->priv->gdbus_book) {
- set_proxy_gone_error (error);
+ uid = NULL;
+ *contact = make_me_card ();
+ if (!e_book_client_add_contact_sync (*client, *contact, &uid, NULL, &local_error)) {
+ g_object_unref (*client);
+ *client = NULL;
+ g_object_unref (*contact);
+ *contact = NULL;
+ g_propagate_error (error, local_error);
return FALSE;
}
- prop_name_value = e_gdbus_book_encode_set_backend_property (prop_name, prop_value);
- res = e_client_proxy_call_sync_strv__void (client, (const gchar * const *) prop_name_value, cancellable, error, e_gdbus_book_call_set_backend_property_sync);
- g_strfreev (prop_name_value);
-
- return res;
-}
+ if (uid) {
+ e_contact_set (*contact, E_CONTACT_UID, uid);
+ g_free (uid);
+ }
-static void
-book_client_open (EClient *client,
- gboolean only_if_exists,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
-{
- e_client_proxy_call_boolean (client, only_if_exists, cancellable, callback, user_data, book_client_open,
- e_gdbus_book_call_open,
- e_gdbus_book_call_open_finish, NULL, NULL, NULL, NULL);
-}
+ e_book_client_set_self (*client, *contact, NULL);
-static gboolean
-book_client_open_finish (EClient *client,
- GAsyncResult *result,
- GError **error)
-{
- return e_client_proxy_call_finish_void (client, result, error, book_client_open);
+ return TRUE;
}
-static gboolean
-book_client_open_sync (EClient *client,
- gboolean only_if_exists,
- GCancellable *cancellable,
- GError **error)
+/**
+ * e_book_client_set_self:
+ * @client: an #EBookClient
+ * @contact: an #EContact
+ * @error: a #GError to set on failure
+ *
+ * Specify that @contact residing in @client is the #EContact that
+ * refers to the user of the address book.
+ *
+ * Returns: %TRUE if successful, %FALSE otherwise.
+ *
+ * Since: 3.2
+ **/
+gboolean
+e_book_client_set_self (EBookClient *client,
+ EContact *contact,
+ GError **error)
{
- EBookClient *book_client;
+ GSettings *settings;
g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
+ g_return_val_if_fail (contact != NULL, FALSE);
+ g_return_val_if_fail (e_contact_get_const (contact, E_CONTACT_UID) != NULL, FALSE);
- book_client = E_BOOK_CLIENT (client);
-
- if (!book_client->priv->gdbus_book) {
- set_proxy_gone_error (error);
- return FALSE;
- }
-
- return e_client_proxy_call_sync_boolean__void (client, only_if_exists, cancellable, error, e_gdbus_book_call_open_sync);
-}
+ settings = g_settings_new (SELF_UID_PATH_ID);
+ g_settings_set_string (settings, SELF_UID_KEY, e_contact_get_const (contact, E_CONTACT_UID));
+ g_object_unref (settings);
-static void
-book_client_refresh (EClient *client,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
-{
- e_client_proxy_call_void (client, cancellable, callback, user_data, book_client_refresh,
- e_gdbus_book_call_refresh,
- e_gdbus_book_call_refresh_finish, NULL, NULL, NULL, NULL);
+ return TRUE;
}
-static gboolean
-book_client_refresh_finish (EClient *client,
- GAsyncResult *result,
- GError **error)
+/**
+ * e_book_client_is_self:
+ * @contact: an #EContact
+ *
+ * Check if @contact is the user of the address book.
+ *
+ * Returns: %TRUE if @contact is the user, %FALSE otherwise.
+ *
+ * Since: 3.2
+ **/
+gboolean
+e_book_client_is_self (EContact *contact)
{
- return e_client_proxy_call_finish_void (client, result, error, book_client_refresh);
-}
+ GSettings *settings;
+ gchar *uid;
+ gboolean is_self;
-static gboolean
-book_client_refresh_sync (EClient *client,
- GCancellable *cancellable,
- GError **error)
-{
- EBookClient *book_client;
+ g_return_val_if_fail (contact && E_IS_CONTACT (contact), FALSE);
- g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
+ settings = g_settings_new (SELF_UID_PATH_ID);
+ uid = g_settings_get_string (settings, SELF_UID_KEY);
+ g_object_unref (settings);
- book_client = E_BOOK_CLIENT (client);
+ is_self = uid && !g_strcmp0 (uid, e_contact_get_const (contact, E_CONTACT_UID));
- if (!book_client->priv->gdbus_book) {
- set_proxy_gone_error (error);
- return FALSE;
- }
+ g_free (uid);
- return e_client_proxy_call_sync_void__void (client, cancellable, error, e_gdbus_book_call_refresh_sync);
+ return is_self;
}
/**
@@ -2214,11 +2360,11 @@ complete_get_view (EBookClient *client,
{
g_return_val_if_fail (view != NULL, FALSE);
- if (view_path && res && book_factory_proxy) {
+ if (view_path && res && book_factory) {
GError *local_error = NULL;
EGdbusBookView *gdbus_bookview;
- gdbus_bookview = e_gdbus_book_view_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory_proxy)),
+ gdbus_bookview = e_gdbus_book_view_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (book_factory)),
G_DBUS_PROXY_FLAGS_NONE,
ADDRESS_BOOK_DBUS_SERVICE_NAME,
view_path,
@@ -2325,125 +2471,3 @@ e_book_client_get_view_sync (EBookClient *client,
return complete_get_view (client, res, view_path, view, error);
}
-static GDBusProxy *
-book_client_get_dbus_proxy (EClient *client)
-{
- EBookClient *book_client;
-
- g_return_val_if_fail (E_IS_CLIENT (client), NULL);
-
- book_client = E_BOOK_CLIENT (client);
-
- return book_client->priv->gdbus_book;
-}
-
-static void
-book_client_unwrap_dbus_error (EClient *client,
- GError *dbus_error,
- GError **out_error)
-{
- unwrap_dbus_error (dbus_error, out_error);
-}
-
-static void
-book_client_retrieve_capabilities (EClient *client,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
-{
- g_return_if_fail (E_IS_BOOK_CLIENT (client));
-
- book_client_get_backend_property (client, CLIENT_BACKEND_PROPERTY_CAPABILITIES, cancellable, callback, user_data);
-}
-
-static gboolean
-book_client_retrieve_capabilities_finish (EClient *client,
- GAsyncResult *result,
- gchar **capabilities,
- GError **error)
-{
- g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
-
- return book_client_get_backend_property_finish (client, result, capabilities, error);
-}
-
-static gboolean
-book_client_retrieve_capabilities_sync (EClient *client,
- gchar **capabilities,
- GCancellable *cancellable,
- GError **error)
-{
- g_return_val_if_fail (E_IS_BOOK_CLIENT (client), FALSE);
-
- return book_client_get_backend_property_sync (client, CLIENT_BACKEND_PROPERTY_CAPABILITIES, capabilities, cancellable, error);
-}
-
-static void
-e_book_client_init (EBookClient *client)
-{
- LOCK_FACTORY ();
- active_book_clients++;
- UNLOCK_FACTORY ();
-
- client->priv = E_BOOK_CLIENT_GET_PRIVATE (client);
-}
-
-static void
-book_client_dispose (GObject *object)
-{
- EClient *client;
-
- client = E_CLIENT (object);
-
- e_client_cancel_all (client);
-
- gdbus_book_client_disconnect (E_BOOK_CLIENT (client));
-
- /* Chain up to parent's dispose() method. */
- G_OBJECT_CLASS (e_book_client_parent_class)->dispose (object);
-}
-
-static void
-book_client_finalize (GObject *object)
-{
- /* Chain up to parent's finalize() method. */
- G_OBJECT_CLASS (e_book_client_parent_class)->finalize (object);
-
- LOCK_FACTORY ();
- active_book_clients--;
- if (!active_book_clients)
- gdbus_book_factory_proxy_disconnect (NULL);
- UNLOCK_FACTORY ();
-}
-
-static void
-e_book_client_class_init (EBookClientClass *class)
-{
- GObjectClass *object_class;
- EClientClass *client_class;
-
- g_type_class_add_private (class, sizeof (EBookClientPrivate));
-
- object_class = G_OBJECT_CLASS (class);
- object_class->dispose = book_client_dispose;
- object_class->finalize = book_client_finalize;
-
- client_class = E_CLIENT_CLASS (class);
- client_class->get_dbus_proxy = book_client_get_dbus_proxy;
- client_class->unwrap_dbus_error = book_client_unwrap_dbus_error;
- client_class->retrieve_capabilities = book_client_retrieve_capabilities;
- client_class->retrieve_capabilities_finish = book_client_retrieve_capabilities_finish;
- client_class->retrieve_capabilities_sync = book_client_retrieve_capabilities_sync;
- client_class->get_backend_property = book_client_get_backend_property;
- client_class->get_backend_property_finish = book_client_get_backend_property_finish;
- client_class->get_backend_property_sync = book_client_get_backend_property_sync;
- client_class->set_backend_property = book_client_set_backend_property;
- client_class->set_backend_property_finish = book_client_set_backend_property_finish;
- client_class->set_backend_property_sync = book_client_set_backend_property_sync;
- client_class->open = book_client_open;
- client_class->open_finish = book_client_open_finish;
- client_class->open_sync = book_client_open_sync;
- client_class->refresh = book_client_refresh;
- client_class->refresh_finish = book_client_refresh_finish;
- client_class->refresh_sync = book_client_refresh_sync;
-}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]