enum EDSContactsStorageItemIterator::eFetchContacts EDSContactsStorageItemIterator::fetchContacts(int fetchsize)
{
  GError * gerror   = NULL;

  FREE_CONTACTS(contacts);

  LOG_VERBOSE() << "Cursor Position: " << e_book_client_cursor_get_position(cursor)<<std::endl;

  if (e_book_client_cursor_get_position(cursor) > total){
    FREE_CONTACTS(contacts);
    return eFetchContactsEND;
  }

  int num = e_book_client_cursor_step_sync(cursor,
                                           E_BOOK_CURSOR_STEP_FETCH ,
                                           E_BOOK_CURSOR_ORIGIN_CURRENT,
                                           fetchsize, &contacts, NULL, &gerror);
  if (-1 == num){
    LOG_ERROR() << "Error e_book_client_cursor_step_sync results: " << GERROR_MESSAGE(gerror)<<std::endl ;
    GERROR_FREE(gerror);
    FREE_CONTACTS(contacts);
    return eFetchContactsFail;
  }
  if (0 == num){
    GERROR_FREE(gerror);
    FREE_CONTACTS(contacts);
    return eFetchContactsEND;
  }

  GERROR_FREE(gerror);
  e_book_client_cursor_step_sync(cursor,
                                 E_BOOK_CURSOR_STEP_MOVE,
                                 E_BOOK_CURSOR_ORIGIN_CURRENT,
                                 fetchsize, NULL, NULL,
                                 &gerror);
  if (NULL != gerror){
    LOG_ERROR() << "Error e_book_client_cursor_step_sync results: " << GERROR_MESSAGE(gerror) <<std::endl;
    GERROR_FREE(gerror);
    FREE_CONTACTS(contacts);
    return eFetchContactsFail;
  }
  GERROR_FREE(gerror);
  return eFetchContactsOK;
}
static gboolean
cursor_example_move_cursor (CursorExample *example,
                            EBookCursorOrigin origin,
                            gint count)
{
	CursorExamplePrivate *priv = example->priv;
	GError               *error = NULL;
	gint                  n_results;

	n_results = e_book_client_cursor_step_sync (
		priv->cursor,
		E_BOOK_CURSOR_STEP_MOVE,
		origin,
		count,
		NULL, /* Result list */
		NULL, /* GCancellable */
		&error);

	if (n_results < 0) {

		if (g_error_matches (error,
				     E_CLIENT_ERROR,
				     E_CLIENT_ERROR_OUT_OF_SYNC)) {

			/* The addressbook has very recently been modified,
			 * very soon we will receive a "refresh" signal and
			 * automatically reload the current page position.
			 */
			d (g_print ("Cursor was temporarily out of sync while moving\n"));

		} else if (g_error_matches (error,
					    E_CLIENT_ERROR,
					    E_CLIENT_ERROR_QUERY_REFUSED)) {

			d (g_print ("End of list was reached\n"));

		} else
			g_warning ("Failed to move the cursor: %s", error->message);

		g_clear_error (&error);

	}

	return n_results >= 0;
}
/* Loads a page at the current cursor position, returns
 * FALSE if there was an error.
 */
static gboolean
cursor_example_load_page (CursorExample *example,
                          gboolean *full_results)
{
	CursorExamplePrivate *priv = example->priv;
	GError               *error = NULL;
	GSList               *results = NULL;
	gint                  n_results;

	/* Fetch N_SLOTS contacts after the current cursor position,
	 * without modifying the current cursor position
	 */
	n_results = e_book_client_cursor_step_sync (
		priv->cursor,
		E_BOOK_CURSOR_STEP_FETCH,
		E_BOOK_CURSOR_ORIGIN_CURRENT,
		N_SLOTS,
		&results,
		NULL, /* GCancellable */
		&error);

	if (n_results < 0) {
		if (g_error_matches (error,
				     E_CLIENT_ERROR,
				     E_CLIENT_ERROR_OUT_OF_SYNC)) {

			/* The addressbook has very recently been modified,
			 * very soon we will receive a "refresh" signal and
			 * automatically reload the current page position.
			 */
			d (g_print ("Cursor was temporarily out of sync while loading page\n"));

		} else if (g_error_matches (error,
					    E_CLIENT_ERROR,
					    E_CLIENT_ERROR_QUERY_REFUSED)) {

			d (g_print ("End of list was reached\n"));

		} else
			g_warning ("Failed to move the cursor: %s", error->message);

		g_clear_error (&error);

	} else {
		/* Display the results */
		EContact             *contact;
		gint                  i;

		/* Fill the page with results for the current cursor position
		 */
		for (i = 0; i < N_SLOTS; i++) {
			contact = g_slist_nth_data (results, i);

			/* For the first contact, give some visual feedback about where we
			 * are in the list, which alphabet character we're browsing right now.
			 */
			if (i == 0 && contact)
				cursor_example_update_current_index (example, contact);

			cursor_slot_set_from_contact (CURSOR_SLOT (priv->slots[i]), contact);
		}
	}

	if (full_results)
		*full_results = (n_results == N_SLOTS);

	g_slist_free_full (results, (GDestroyNotify) g_object_unref);

	return n_results >= 0;
}