gint
main (gint argc,
      gchar **argv)
{
	EBookClient *book_client;
	const gchar *query_string;
	EBookQuery *query;
	gchar *sexp;
	GSList *c, *contacts;
	GError *error = NULL;

	main_initialize ();

	if (argc != 2) {
		query_string = "contains \"full_name\" \"a\"";
		printf ("usage: test-search <query>\n");
		printf ("   using default query \"%s\"\n", query_string);
	} else {
		query_string = argv[1];
	}

	query = e_book_query_from_string (query_string);
	if (!query) {
		fprintf (stderr, " * Failed to parse query string '%s'\n", query_string);
		return 1;
	}

	sexp = e_book_query_to_string (query);
	e_book_query_unref (query);

	book_client = open_system_book (FALSE);
	if (!book_client) {
		g_free (sexp);
		return 1;
	}

	if (!e_book_client_get_contacts_sync (book_client, sexp, &contacts, NULL, &error)) {
		report_error ("get contacts sync", &error);
		g_free (sexp);
		g_object_unref (book_client);
		return 1;
	}

	for (c = contacts; c; c = c->next) {
		EContact *contact = E_CONTACT (c->data);
		gchar *vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);

		printf ("%s\n\n", vcard);

		g_free (vcard);
	}

	g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
	g_slist_free (contacts);

	g_free (sexp);
	g_object_unref (book_client);

	return 0;
}
Ejemplo n.º 2
0
static void
action_address_book_save_as_cb (GtkAction *action,
                                EBookShellView *book_shell_view)
{
	EShell *shell;
	EShellView *shell_view;
	EShellWindow *shell_window;
	EShellBackend *shell_backend;
	EBookShellContent *book_shell_content;
	EAddressbookModel *model;
	EAddressbookView *view;
	EActivity *activity;
	EBookQuery *query;
	EBookClient *book;
	GSList *list = NULL;
	GFile *file;
	gchar *string;

	shell_view = E_SHELL_VIEW (book_shell_view);
	shell_window = e_shell_view_get_shell_window (shell_view);
	shell_backend = e_shell_view_get_shell_backend (shell_view);
	shell = e_shell_window_get_shell (shell_window);

	book_shell_content = book_shell_view->priv->book_shell_content;
	view = e_book_shell_content_get_current_view (book_shell_content);
	g_return_if_fail (view != NULL);

	model = e_addressbook_view_get_model (view);
	book = e_addressbook_model_get_client (model);

	query = e_book_query_any_field_contains ("");
	string = e_book_query_to_string (query);
	e_book_query_unref (query);

	e_book_client_get_contacts_sync (book, string, &list, NULL, NULL);
	g_free (string);

	if (list == NULL)
		goto exit;

	string = eab_suggest_filename (list);
	file = e_shell_run_save_dialog (
		/* Translators: This is a save dialog title */
		shell, _("Save as vCard"), string,
		"*.vcf:text/x-vcard,text/directory", NULL, NULL);
	g_free (string);

	if (file == NULL)
		goto exit;

	string = eab_contact_list_to_string (list);
	if (string == NULL) {
		g_warning ("Could not convert contact list to a string");
		g_object_unref (file);
		goto exit;
	}

	/* XXX No callback means errors are discarded.
	 *
	 *     There's an EAlert for this which I'm not using
	 *     until I figure out a better way to display errors:
	 *
	 *     "addressbook:save-error"
	 */
	activity = e_file_replace_contents_async (
		file, string, strlen (string), NULL, FALSE,
		G_FILE_CREATE_NONE, (GAsyncReadyCallback) NULL, NULL);
	e_shell_backend_add_activity (shell_backend, activity);

	/* Free the string when the activity is finalized. */
	g_object_set_data_full (
		G_OBJECT (activity),
		"file-content", string,
		(GDestroyNotify) g_free);

	g_object_unref (file);

exit:
	e_client_util_free_object_slist (list);
}