1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* e-book-auth-util.c - Lame helper to load addressbooks with authentication.
5 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
21 * Authors: Hans Petter Jansson <hpj@novell.com>
23 * Mostly taken from Evolution's addressbook/gui/component/addressbook.c
32 #include <glib/gi18n-lib.h>
34 #include <libedataserverui/e-passwords.h>
36 #include "e-book-auth-util.h"
43 load_book_source_context_free (LoadContext *context)
45 if (context->book != NULL)
46 g_object_unref (context->book);
48 g_slice_free (LoadContext, context);
52 load_book_source_thread (GSimpleAsyncResult *simple,
54 GCancellable *cancellable)
60 context = g_simple_async_result_get_op_res_gpointer (simple);
62 book = e_book_new (source, &error);
64 g_simple_async_result_take_error (simple, error);
68 if (g_cancellable_set_error_if_cancelled (cancellable, &error)) {
69 g_simple_async_result_take_error (simple, error);
70 g_object_unref (book);
74 if (!e_book_open (book, FALSE, &error)) {
75 g_simple_async_result_take_error (simple, error);
76 g_object_unref (book);
84 * e_load_book_source_async:
85 * @source: an #ESource
86 * @parent: parent window for password dialogs, or %NULL
87 * @cancellable: optional #GCancellable object, %NULL to ignore
88 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
89 * @user_data: the data to pass to @callback
91 * Creates a new #EBook specified by @source and opens it, prompting the
92 * user for authentication if necessary.
94 * When the operation is finished, @callback will be called. You can
95 * then call e_load_book_source_finish() to obtain the resulting #EBook.
99 * Deprecated: 3.2: Use e_client_utils_open_new(), e_client_utils_open_new_finish() instead.
102 e_load_book_source_async (ESource *source,
104 GCancellable *cancellable,
105 GAsyncReadyCallback callback,
108 GSimpleAsyncResult *simple;
109 LoadContext *context;
111 g_return_if_fail (E_IS_SOURCE (source));
113 if (parent != NULL) {
114 g_return_if_fail (GTK_IS_WINDOW (parent));
115 g_object_ref (parent);
118 if (cancellable != NULL) {
119 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
120 g_object_ref (cancellable);
122 /* always provide cancellable, because the code depends on it */
123 cancellable = g_cancellable_new ();
126 context = g_slice_new0 (LoadContext);
128 simple = g_simple_async_result_new (
129 G_OBJECT (source), callback,
130 user_data, e_load_book_source_async);
132 g_simple_async_result_set_check_cancellable (simple, cancellable);
134 g_simple_async_result_set_op_res_gpointer (
135 simple, context, (GDestroyNotify)
136 load_book_source_context_free);
138 g_simple_async_result_run_in_thread (
139 simple, (GSimpleAsyncThreadFunc) load_book_source_thread,
140 G_PRIORITY_DEFAULT, cancellable);
142 g_object_unref (simple);
146 * e_load_book_source_finish:
147 * @source: an #ESource
148 * @result: a #GAsyncResult
149 * @error: return location for a #GError, or %NULL
151 * Finishes an asynchronous #EBook open operation started with
152 * e_load_book_source_async(). If an error occurred, or the user
153 * declined to authenticate, the function will return %NULL and
156 * Returns: a ready-to-use #EBook, or %NULL or error
160 * Deprecated: 3.2: Use e_client_utils_open_new(), e_client_utils_open_new_finish() instead.
163 e_load_book_source_finish (ESource *source,
164 GAsyncResult *result,
167 GSimpleAsyncResult *simple;
168 LoadContext *context;
170 g_return_val_if_fail (E_IS_SOURCE (source), NULL);
171 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
173 g_return_val_if_fail (
174 g_simple_async_result_is_valid (
175 result, G_OBJECT (source),
176 e_load_book_source_async), NULL);
178 simple = G_SIMPLE_ASYNC_RESULT (result);
180 if (g_simple_async_result_propagate_error (simple, error))
183 context = g_simple_async_result_get_op_res_gpointer (simple);
184 g_return_val_if_fail (context != NULL, NULL);
186 return g_object_ref (context->book);