/* gcc -Wall $(pkg-config --cflags --libs libebook-1.2) e-client-sync.c -o e-client-sync */

#include <stdlib.h>
#include <string.h>
#include <libedataserver/e-account.h>
#include <libedataserver/e-source.h>
#include <gio/gio.h>
#include <libebook/e-book-client.h>


EBookClient *addressbook; 

static ESource * get_source (void)
{
  ESourceGroup* group = NULL;
  ESource* source = NULL;
  GError *error = NULL;
  ESourceList* sl = NULL;

  group = e_source_group_new ("Test", "local:");
  source = e_source_new_with_absolute_uri ("Test", "local:///tmp/test");

  return source;
}

gboolean do_stuff (gpointer data)
{
  GError *error = NULL;
  ESource *source;
  source = get_source ();
  addressbook = e_book_client_new (source, &error);

  if (error == NULL) {
    e_client_open_sync ((EClient*) addressbook, TRUE, NULL, &error);

    if (error == NULL) {
      if (e_client_is_opened ((EClient *)addressbook)) {
	printf ("Yes\n");
      } else {
	printf ("No\n");
      }
    } else {
      printf ("Error (e_client_open_sync): %s\n", error->message);
    }

    exit (-1);
  } else {
    printf ("Error (e_book_client_new): %s\n", error->message);
  }

  return FALSE;
}

int main (int argc, char **argv)
{
  GMainLoop *loop;

  g_type_init ();

  g_idle_add (do_stuff, NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);
  g_main_loop_unref (loop);

  return 0;
}
