[evolution-data-server] Migrated tests/cursor-example to examples/cursor



commit 8cf788ba35fedc344d57b600197095b9001d4e7e
Author: Tristan Van Berkom <tristan upstairslabs com>
Date:   Fri Dec 6 15:08:15 2013 +0900

    Migrated tests/cursor-example to examples/cursor
    
    Examples can be added to the examples subdirectory which does
    not belong in 'tests'. Note that the new documentation contains
    an examples section, the file eds-cursor-example.sgml is manually
    written and includes source code inline, along with some commentary
    on the source code example.

 Makefile.am                                        |    4 +
 configure.ac                                       |   14 +-
 examples/Makefile.am                               |    1 +
 .../cursor-example => examples/cursor}/Makefile.am |    0
 .../cursor}/cursor-data.c                          |  239 +++++++++-----------
 .../cursor}/cursor-data.h                          |    0
 .../cursor}/cursor-example.c                       |    0
 .../cursor}/cursor-example.gresources.xml          |    0
 .../cursor}/cursor-example.h                       |    0
 .../cursor}/cursor-example.ui                      |    0
 .../cursor}/cursor-navigator.c                     |    0
 .../cursor}/cursor-navigator.h                     |    0
 .../cursor}/cursor-search.c                        |    0
 .../cursor}/cursor-search.h                        |    0
 .../cursor}/cursor-search.ui                       |    0
 .../cursor}/cursor-slot.c                          |    0
 .../cursor}/cursor-slot.h                          |    0
 .../cursor}/cursor-slot.ui                         |    0
 {tests/cursor-example => examples/cursor}/main.c   |    0
 tests/Makefile.am                                  |    4 -
 20 files changed, 115 insertions(+), 147 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 66bb725..93df215 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -25,6 +25,10 @@ SUBDIRS = \
        po \
        $(NULL)
 
+if BUILD_EXAMPLES
+SUBDIRS += examples
+endif
+
 if HAVE_INTROSPECTION
 if HAVE_VALA
 SUBDIRS += vala
diff --git a/configure.ac b/configure.ac
index 14a12fb..bc48122 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1796,20 +1796,14 @@ tests/test-server-utils/services/Makefile
 tests/test-server-utils/services/org.gnome.evolution.dataserver.AddressBook.service
 tests/test-server-utils/services/org.gnome.evolution.dataserver.Calendar.service
 tests/test-server-utils/services/org.gnome.evolution.dataserver.Sources.service
-tests/cursor-example/Makefile
+examples/Makefile
+examples/cursor/Makefile
 docs/Makefile
 docs/reference/Makefile
-docs/reference/addressbook/Makefile
-docs/reference/addressbook/libebook/Makefile
-docs/reference/addressbook/libebook-contacts/Makefile
-docs/reference/addressbook/libedata-book/Makefile
-docs/reference/calendar/Makefile
-docs/reference/calendar/libecal/Makefile
-docs/reference/calendar/libedata-cal/Makefile
 docs/reference/camel/Makefile
-docs/reference/libedataserver/Makefile
-docs/reference/libebackend/Makefile
 docs/reference/private/Makefile
+docs/reference/eds/Makefile
+docs/reference/eds/version.xml
 po/Makefile.in
 vala/Makefile
 ])
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 0000000..cacd1ba
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = cursor
diff --git a/tests/cursor-example/Makefile.am b/examples/cursor/Makefile.am
similarity index 100%
rename from tests/cursor-example/Makefile.am
rename to examples/cursor/Makefile.am
diff --git a/tests/cursor-example/cursor-data.c b/examples/cursor/cursor-data.c
similarity index 73%
rename from tests/cursor-example/cursor-data.c
rename to examples/cursor/cursor-data.c
index f7b0996..e24805f 100644
--- a/tests/cursor-example/cursor-data.c
+++ b/examples/cursor/cursor-data.c
@@ -22,14 +22,12 @@
 
 #define CURSOR_DATA_SOURCE_ID "cursor-example-book"
 
-static void                  load_contacts (EBookClient          *client,
-                                           const gchar          *vcard_directory);
-static EBookClientCursor    *get_cursor    (EBookClient          *book_client);
-
-/* Just an example, we need to spin the main loop
- * a bit to wait for the ESource to be created,
+/* We need to spin the main loop a bit to wait for the ESource
+ * to be created. This particular API is admittedly cumbersome,
+ * hopefully this will be fixed in a later version so that
+ * the ESource can be synchronously created.
  *
- * So lets just use some global variables here
+ * Just an example so lets just use some global variables here...
  */
 static EBookClient *address_book = NULL;
 static ESource     *address_book_source = NULL;
@@ -64,41 +62,100 @@ cursor_data_source_timeout (gpointer user_data)
        return FALSE;
 }
 
+/*
+ * Helper function to load a contact from a vcard file.
+ */
+static EContact *
+contact_from_file (const gchar *vcard_file)
+{
+       EContact *contact;
+       GError *error;
+       gchar *vcard = NULL;
+
+       if (!g_file_get_contents (vcard_file, &vcard, NULL, &error))
+               g_error ("Failed to load vcard: %s", error->message);
+
+       contact = e_contact_new_from_vcard (vcard);
+       g_free (vcard);
+
+       return contact;
+}
+
+/*
+ * Load all the contacts from 'vcard_directory', and add them to 'client'.
+ */
 static void
-setup_custom_book (ESource *scratch)
+load_contacts (EBookClient *client,
+              const gchar *vcard_directory)
+{
+       GDir *dir;
+       GError *error = NULL;
+       const gchar *filename;
+       GSList *contacts = NULL;
+
+       dir = g_dir_open (vcard_directory, 0, &error);
+       if (!dir)
+               g_error ("Failed to open vcard directory '%s': %s", vcard_directory, error->message);
+
+       while ((filename = g_dir_read_name (dir)) != NULL) {
+
+               if (g_str_has_suffix (filename, ".vcf")) {
+                       gchar *fullpath = g_build_filename (vcard_directory, filename, NULL);
+                       EContact *contact;
+
+                       contact = contact_from_file (fullpath);
+                       contacts = g_slist_prepend (contacts, contact);
+
+                       g_free (fullpath);
+               }
+       }
+
+       g_dir_close (dir);
+
+       if (contacts != NULL) {
+
+               if (!e_book_client_add_contacts_sync (client, contacts, NULL, NULL, &error)) {
+
+                       /* If they were already added, ignore the error */
+                       if (g_error_matches (error, E_BOOK_CLIENT_ERROR,
+                                            E_BOOK_CLIENT_ERROR_CONTACT_ID_ALREADY_EXISTS))
+                               g_clear_error (&error);
+                       else
+                               g_error ("Failed to add test contacts: %s", error->message);
+               }
+       }
+}
+
+/*
+ * Create an EBookClient cursor sorted by family name, and then by given name as
+ * the secondary sort key.
+ */
+static EBookClientCursor *
+get_cursor (EBookClient *book_client)
 {
-       ESourceBackendSummarySetup *setup;
-
-       g_type_ensure (E_TYPE_SOURCE_BACKEND_SUMMARY_SETUP);
-       setup = e_source_get_extension (scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
-       e_source_backend_summary_setup_set_summary_fields (
-               setup,
-               E_CONTACT_FULL_NAME,
-               E_CONTACT_FAMILY_NAME,
-               E_CONTACT_GIVEN_NAME,
-               E_CONTACT_NICKNAME,
-               E_CONTACT_TEL,
-               E_CONTACT_EMAIL,
-               0);
-       e_source_backend_summary_setup_set_indexed_fields (
-               setup,
-               E_CONTACT_FULL_NAME, E_BOOK_INDEX_PREFIX,
-               E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_PREFIX,
-               E_CONTACT_GIVEN_NAME, E_BOOK_INDEX_PREFIX,
-               E_CONTACT_NICKNAME, E_BOOK_INDEX_PREFIX,
-               E_CONTACT_TEL, E_BOOK_INDEX_PREFIX,
-               E_CONTACT_TEL, E_BOOK_INDEX_PHONE,
-               E_CONTACT_EMAIL, E_BOOK_INDEX_PREFIX,
-               0);
+  EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
+  EBookCursorSortType sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING, E_BOOK_CURSOR_SORT_ASCENDING };
+  EBookClientCursor *cursor = NULL;
+  GError *error = NULL;
+
+  if (!e_book_client_get_cursor_sync (book_client,
+                                     NULL,
+                                     sort_fields,
+                                     sort_types,
+                                     2,
+                                     &cursor,
+                                     NULL,
+                                     &error)) {
+         g_warning ("Unable to create cursor");
+         g_clear_error (&error);
+  }
+
+  return cursor;
 }
 
-/* This ensures that all of the test contacts are
- * installed in the CURSOR_DATA_SOURCE_ID test book.
- *
- * Then it opens an EBookClientCursor.
- *
- * The cursor has no filter on the results and is
- * ordered by family name (ascending) and then given name (ascending).
+/* Entry point for this file, here we take care of
+ * creating the addressbook if it doesnt exist,
+ * getting an EBookClient, and creating our EBookClientCursor.
  */
 EBookClient *
 cursor_load_data (const gchar        *vcard_path,
@@ -109,12 +166,13 @@ cursor_load_data (const gchar        *vcard_path,
        ESourceBackend *backend = NULL;
        GMainLoop *loop;
        GError  *error = NULL;
-       GSList *contacts = NULL;
        EBookClient *ret_book;
 
        g_return_val_if_fail (vcard_path != NULL, NULL);
        g_return_val_if_fail (ret_cursor != NULL, NULL);
 
+       g_print ("Cursor loading data from %s\n", vcard_path);
+
        loop = g_main_loop_new (NULL, FALSE);
 
        registry = e_source_registry_new_sync (NULL, &error);
@@ -132,8 +190,10 @@ cursor_load_data (const gchar        *vcard_path,
        backend = e_source_get_extension (scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
        e_source_backend_set_backend_name (backend, "local");
 
-       /* Setup custom summary fields, so that we can use those fields with the cursor */
-       setup_custom_book (scratch);
+       /* Now is the right time to use the ESourceBackendSummarySetup to configure
+        * your newly created addressbook. This configuration should happen on the
+        * scratch source before calling e_source_registry_commit_source_sync().
+        */
 
        /* Commit the source to the registry */
        if (!e_source_registry_commit_source_sync (registry, scratch, NULL, &error)) {
@@ -155,6 +215,10 @@ cursor_load_data (const gchar        *vcard_path,
 
        g_object_unref (scratch);
 
+       /* Give EDS a little time to actually create the ESource remotely and
+        * also have a copy if it cached locally, wait for the "source-added"
+        * signal.
+        */
        if (address_book == NULL) {
                g_timeout_add_seconds (20, cursor_data_source_timeout, NULL);
                g_main_loop_run (loop);
@@ -166,21 +230,7 @@ cursor_load_data (const gchar        *vcard_path,
        /**********************************************************
         * Ok, done with creating an addressbook, let's add data  *
         **********************************************************/
-
-       /* First check if there are already some contacts, if so then
-        * avoid adding them again
-        */
-       if (!e_book_client_get_contacts_uids_sync (address_book,
-                                                  "", &contacts, NULL, &error))
-               g_error ("Failed to query addressbook for existing contacts");
-
-       if (contacts != NULL) {
-               /* We already have contacts, no need to add them */
-               g_slist_free_full (contacts, (GDestroyNotify)g_free);
-               contacts = NULL;
-       } else {
-               load_contacts (address_book, vcard_path);
-       }
+       load_contacts (address_book, vcard_path);
 
        /* Addressbook should have contacts now, let's create the cursor */
        *ret_cursor = get_cursor (address_book);
@@ -199,80 +249,3 @@ cursor_load_data (const gchar        *vcard_path,
        /* Return the addressbook */
        return ret_book;
 }
-
-static EContact *
-contact_from_file (const gchar *vcard_file)
-{
-       EContact *contact;
-       GError *error;
-       gchar *vcard = NULL;
-
-       if (!g_file_get_contents (vcard_file, &vcard, NULL, &error))
-               g_error ("Failed to load vcard: %s", error->message);
-
-       contact = e_contact_new_from_vcard (vcard);
-       g_free (vcard);
-
-       return contact;
-}
-
-static void
-load_contacts (EBookClient *client,
-              const gchar *vcard_directory)
-{
-       GDir *dir;
-       GError *error = NULL;
-       const gchar *filename;
-       GSList *contacts = NULL;
-
-       dir = g_dir_open (vcard_directory, 0, &error);
-       if (!dir)
-               g_error ("Failed to open vcard directory '%s': %s", vcard_directory, error->message);
-
-       while ((filename = g_dir_read_name (dir)) != NULL) {
-
-               if (g_str_has_suffix (filename, ".vcf")) {
-                       gchar *fullpath = g_build_filename (vcard_directory, filename, NULL);
-                       EContact *contact;
-
-                       g_print ("Loading contact from: %s\n", fullpath);
-
-                       contact = contact_from_file (fullpath);
-                       contacts = g_slist_prepend (contacts, contact);
-
-                       g_free (fullpath);
-               }
-       }
-
-       g_dir_close (dir);
-
-       if (contacts != NULL) {
-
-               if (!e_book_client_add_contacts_sync (client, contacts, NULL, NULL, &error))
-                       g_error ("Failed to add contacts");
-       } else
-               g_error ("No contacts found in vcard directory: %s", vcard_directory);
-}
-
-static EBookClientCursor *
-get_cursor (EBookClient *book_client)
-{
-  EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
-  EBookCursorSortType sort_types[] = { E_BOOK_CURSOR_SORT_ASCENDING, E_BOOK_CURSOR_SORT_ASCENDING };
-  EBookClientCursor *cursor = NULL;
-  GError *error = NULL;
-
-  if (!e_book_client_get_cursor_sync (book_client,
-                                     NULL,
-                                     sort_fields,
-                                     sort_types,
-                                     2,
-                                     &cursor,
-                                     NULL,
-                                     &error)) {
-         g_warning ("Unable to create cursor");
-         g_clear_error (&error);
-  }
-
-  return cursor;
-}
diff --git a/tests/cursor-example/cursor-data.h b/examples/cursor/cursor-data.h
similarity index 100%
rename from tests/cursor-example/cursor-data.h
rename to examples/cursor/cursor-data.h
diff --git a/tests/cursor-example/cursor-example.c b/examples/cursor/cursor-example.c
similarity index 100%
rename from tests/cursor-example/cursor-example.c
rename to examples/cursor/cursor-example.c
diff --git a/tests/cursor-example/cursor-example.gresources.xml 
b/examples/cursor/cursor-example.gresources.xml
similarity index 100%
rename from tests/cursor-example/cursor-example.gresources.xml
rename to examples/cursor/cursor-example.gresources.xml
diff --git a/tests/cursor-example/cursor-example.h b/examples/cursor/cursor-example.h
similarity index 100%
rename from tests/cursor-example/cursor-example.h
rename to examples/cursor/cursor-example.h
diff --git a/tests/cursor-example/cursor-example.ui b/examples/cursor/cursor-example.ui
similarity index 100%
rename from tests/cursor-example/cursor-example.ui
rename to examples/cursor/cursor-example.ui
diff --git a/tests/cursor-example/cursor-navigator.c b/examples/cursor/cursor-navigator.c
similarity index 100%
rename from tests/cursor-example/cursor-navigator.c
rename to examples/cursor/cursor-navigator.c
diff --git a/tests/cursor-example/cursor-navigator.h b/examples/cursor/cursor-navigator.h
similarity index 100%
rename from tests/cursor-example/cursor-navigator.h
rename to examples/cursor/cursor-navigator.h
diff --git a/tests/cursor-example/cursor-search.c b/examples/cursor/cursor-search.c
similarity index 100%
rename from tests/cursor-example/cursor-search.c
rename to examples/cursor/cursor-search.c
diff --git a/tests/cursor-example/cursor-search.h b/examples/cursor/cursor-search.h
similarity index 100%
rename from tests/cursor-example/cursor-search.h
rename to examples/cursor/cursor-search.h
diff --git a/tests/cursor-example/cursor-search.ui b/examples/cursor/cursor-search.ui
similarity index 100%
rename from tests/cursor-example/cursor-search.ui
rename to examples/cursor/cursor-search.ui
diff --git a/tests/cursor-example/cursor-slot.c b/examples/cursor/cursor-slot.c
similarity index 100%
rename from tests/cursor-example/cursor-slot.c
rename to examples/cursor/cursor-slot.c
diff --git a/tests/cursor-example/cursor-slot.h b/examples/cursor/cursor-slot.h
similarity index 100%
rename from tests/cursor-example/cursor-slot.h
rename to examples/cursor/cursor-slot.h
diff --git a/tests/cursor-example/cursor-slot.ui b/examples/cursor/cursor-slot.ui
similarity index 100%
rename from tests/cursor-example/cursor-slot.ui
rename to examples/cursor/cursor-slot.ui
diff --git a/tests/cursor-example/main.c b/examples/cursor/main.c
similarity index 100%
rename from tests/cursor-example/main.c
rename to examples/cursor/main.c
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5196e41..7e2369b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -9,10 +9,6 @@ SUBDIRS = \
        libedata-cal \
        $(NULL)
 
-if BUILD_EXAMPLES
-SUBDIRS += cursor-example
-endif
-
 @GNOME_CODE_COVERAGE_RULES@
 
 -include $(top_srcdir)/git.mk


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