[evolution-data-server/openismus-work-3-8: 83/116] EDataBook: Handle D-Bus calls for creating cursors and export new cursors on the bus.



commit 1e73abb402b433b7bb18a20daaecf4cffba59980
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Tue Jul 2 17:44:51 2013 +0900

    EDataBook: Handle D-Bus calls for creating cursors and export new cursors on the bus.

 addressbook/libedata-book/e-data-book.c |  178 +++++++++++++++++++++++++++++++
 1 files changed, 178 insertions(+), 0 deletions(-)
---
diff --git a/addressbook/libedata-book/e-data-book.c b/addressbook/libedata-book/e-data-book.c
index e4fe3c7..8bab109 100644
--- a/addressbook/libedata-book/e-data-book.c
+++ b/addressbook/libedata-book/e-data-book.c
@@ -471,6 +471,18 @@ op_complete (EDataBook *book,
        return direct_data;
 }
 
+static gchar *
+construct_bookcursor_path (void)
+{
+       static volatile gint counter = 1;
+
+       g_atomic_int_inc (&counter);
+
+       return g_strdup_printf (
+               "/org/gnome/evolution/dataserver/AddressBookCursor/%d/%d",
+               getpid (), counter);
+}
+
 static void
 data_book_convert_to_client_error (GError *error)
 {
@@ -1104,6 +1116,169 @@ data_book_handle_get_view_cb (EDBusAddressBook *interface,
 }
 
 static gboolean
+data_book_interpret_sort_keys (const gchar * const *in_sort_keys,
+                              const gchar * const *in_sort_types,
+                              EContactField **out_sort_keys,
+                              EBookSortType **out_sort_types,
+                              gint *n_fields,
+                              GError **error)
+{
+       gint i, key_count = 0, type_count = 0;
+       EContactField *sort_keys;
+       EBookSortType *sort_types;
+       gboolean success = TRUE;
+
+       if (!in_sort_keys || !in_sort_types) {
+               g_set_error (error,
+                            E_CLIENT_ERROR,
+                            E_CLIENT_ERROR_INVALID_ARG,
+                            "Missing sort keys while trying to create a Cursor");
+               return FALSE;
+       }
+
+       for (i = 0; in_sort_keys[i] != NULL; i++)
+               key_count++;
+       for (i = 0; in_sort_types[i] != NULL; i++)
+               type_count++;
+
+       if (key_count != type_count) {
+               g_set_error (error,
+                            E_CLIENT_ERROR,
+                            E_CLIENT_ERROR_INVALID_ARG,
+                            "Must specify the same amount of sort keys as sort types while creating a 
Cursor");
+               return FALSE;
+       }
+
+       sort_keys = g_new0 (EContactField, key_count);
+       sort_types = g_new0 (EBookSortType, type_count);
+
+       for (i = 0; success && i < key_count; i++) {
+
+               sort_keys[i] = e_contact_field_id (in_sort_keys[i]);
+
+               if (sort_keys[i] == 0) {
+                       g_set_error (error,
+                                    E_CLIENT_ERROR,
+                                    E_CLIENT_ERROR_INVALID_ARG,
+                                    "Invalid sort key '%s' specified when creating a Cursor",
+                                    in_sort_keys[i]);
+                       success = FALSE;
+               }
+       }
+
+       for (i = 0; success && i < type_count; i++) {
+               gint enum_value = 0;
+
+               if (!e_enum_from_string (E_TYPE_BOOK_SORT_TYPE,
+                                        in_sort_types[i],
+                                        &enum_value)) {
+                       g_set_error (error,
+                                    E_CLIENT_ERROR,
+                                    E_CLIENT_ERROR_INVALID_ARG,
+                                    "Invalid sort type '%s' specified when creating a Cursor",
+                                    in_sort_types[i]);
+                       success = FALSE;
+               }
+
+               sort_types[i] = enum_value;
+       }
+
+       if (!success) {
+               g_free (sort_keys);
+               g_free (sort_types);
+       } else {
+               *out_sort_keys = sort_keys;
+               *out_sort_types = sort_types;
+               *n_fields = key_count;
+       }
+
+       return success;
+}
+
+static gboolean
+data_book_handle_get_cursor_cb (EDBusAddressBook *interface,
+                               GDBusMethodInvocation *invocation,
+                               const gchar *in_query,
+                               const gchar * const *in_sort_keys,
+                               const gchar * const *in_sort_types,
+                               EDataBook *data_book)
+{
+       EBookBackend *backend;
+       EDataBookCursor *cursor;
+       GDBusConnection *connection;
+       EContactField *sort_keys = NULL;
+       EBookSortType *sort_types = NULL;
+       gint n_fields = 0;
+       gchar *object_path;
+       GError *error = NULL;
+
+       backend = e_data_book_get_backend (data_book);
+       g_return_val_if_fail (backend != NULL, FALSE);
+
+       /*
+        * Interpret arguments
+        */
+       if (!data_book_interpret_sort_keys (in_sort_keys,
+                                           in_sort_types,
+                                           &sort_keys,
+                                           &sort_types,
+                                           &n_fields,
+                                           &error)) {
+               g_dbus_method_invocation_take_error (invocation, error);
+               g_object_unref (backend);
+               return TRUE;
+       }
+
+       /*
+        * Create cursor
+        */
+       cursor = e_book_backend_create_cursor (backend,
+                                              sort_keys,
+                                              sort_types,
+                                              n_fields,
+                                              &error);
+       g_free (sort_keys);
+       g_free (sort_types);
+
+       if (!cursor) {
+               g_dbus_method_invocation_take_error (invocation, error);
+               return TRUE;
+       }
+
+       /*
+        * Set the query, if any (no query is allowed)
+        */
+       if (!e_data_book_cursor_set_sexp (cursor, in_query, &error)) {
+
+               e_book_backend_delete_cursor (backend, cursor);
+               g_dbus_method_invocation_take_error (invocation, error);
+               return TRUE;
+       }
+
+       object_path = construct_bookcursor_path ();
+       connection = g_dbus_method_invocation_get_connection (invocation);
+
+       /*
+        * Now export the object on the connection
+        */
+       if (!e_data_book_cursor_register_gdbus_object (cursor, connection, object_path, &error)) {
+               e_book_backend_delete_cursor (backend, cursor);
+               g_dbus_method_invocation_take_error (invocation, error);
+               g_free (object_path);
+               return TRUE;
+       }
+
+       /*
+        * All is good in the hood, complete the method call
+        */
+       e_dbus_address_book_complete_get_cursor (interface,
+                                                invocation,
+                                                object_path);
+       g_free (object_path);
+       return TRUE;
+}
+
+static gboolean
 data_book_handle_close_cb (EDBusAddressBook *interface,
                            GDBusMethodInvocation *invocation,
                            EDataBook *book)
@@ -2153,6 +2328,9 @@ data_book_initable_init (GInitable *initable,
                        book->priv->dbus_interface, "handle-get-view",
                        G_CALLBACK (data_book_handle_get_view_cb), book);
                g_signal_connect (
+                       book->priv->dbus_interface, "handle-get-cursor",
+                       G_CALLBACK (data_book_handle_get_cursor_cb), book);
+               g_signal_connect (
                        book->priv->dbus_interface, "handle-close",
                        G_CALLBACK (data_book_handle_close_cb), book);
 


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