Assamese translation updated
[platform/upstream/evolution-data-server.git] / libedataserverui / e-book-auth-util.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* e-book-auth-util.c - Lame helper to load addressbooks with authentication.
4  *
5  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Hans Petter Jansson <hpj@novell.com>
22  *
23  * Mostly taken from Evolution's addressbook/gui/component/addressbook.c
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <string.h>
31 #include <gtk/gtk.h>
32 #include <glib/gi18n-lib.h>
33
34 #include <libedataserverui/e-passwords.h>
35
36 #include "e-book-auth-util.h"
37
38 typedef struct {
39         EBook *book;
40 } LoadContext;
41
42 static void
43 load_book_source_context_free (LoadContext *context)
44 {
45         if (context->book != NULL)
46                 g_object_unref (context->book);
47
48         g_slice_free (LoadContext, context);
49 }
50
51 static void
52 load_book_source_thread (GSimpleAsyncResult *simple,
53                          ESource *source,
54                          GCancellable *cancellable)
55 {
56         EBook *book;
57         LoadContext *context;
58         GError *error = NULL;
59
60         context = g_simple_async_result_get_op_res_gpointer (simple);
61
62         book = e_book_new (source, &error);
63         if (book == NULL) {
64                 g_simple_async_result_take_error (simple, error);
65                 return;
66         }
67
68         if (g_cancellable_set_error_if_cancelled (cancellable, &error)) {
69                 g_simple_async_result_take_error (simple, error);
70                 g_object_unref (book);
71                 return;
72         }
73
74         if (!e_book_open (book, FALSE, &error)) {
75                 g_simple_async_result_take_error (simple, error);
76                 g_object_unref (book);
77                 return;
78         }
79
80         context->book = book;
81 }
82
83 /**
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
90  *
91  * Creates a new #EBook specified by @source and opens it, prompting the
92  * user for authentication if necessary.
93  *
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.
96  *
97  * Since: 2.32
98  *
99  * Deprecated: 3.2: Use e_client_utils_open_new(), e_client_utils_open_new_finish() instead.
100  **/
101 void
102 e_load_book_source_async (ESource *source,
103                           GtkWindow *parent,
104                           GCancellable *cancellable,
105                           GAsyncReadyCallback callback,
106                           gpointer user_data)
107 {
108         GSimpleAsyncResult *simple;
109         LoadContext *context;
110
111         g_return_if_fail (E_IS_SOURCE (source));
112
113         if (parent != NULL) {
114                 g_return_if_fail (GTK_IS_WINDOW (parent));
115                 g_object_ref (parent);
116         }
117
118         if (cancellable != NULL) {
119                 g_return_if_fail (G_IS_CANCELLABLE (cancellable));
120                 g_object_ref (cancellable);
121         } else {
122                 /* always provide cancellable, because the code depends on it */
123                 cancellable = g_cancellable_new ();
124         }
125
126         context = g_slice_new0 (LoadContext);
127
128         simple = g_simple_async_result_new (
129                 G_OBJECT (source), callback,
130                 user_data, e_load_book_source_async);
131
132         g_simple_async_result_set_check_cancellable (simple, cancellable);
133
134         g_simple_async_result_set_op_res_gpointer (
135                 simple, context, (GDestroyNotify)
136                 load_book_source_context_free);
137
138         g_simple_async_result_run_in_thread (
139                 simple, (GSimpleAsyncThreadFunc) load_book_source_thread,
140                 G_PRIORITY_DEFAULT, cancellable);
141
142         g_object_unref (simple);
143 }
144
145 /**
146  * e_load_book_source_finish:
147  * @source: an #ESource
148  * @result: a #GAsyncResult
149  * @error: return location for a #GError, or %NULL
150  *
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
154  * set @error.
155  *
156  * Returns: a ready-to-use #EBook, or %NULL or error
157  *
158  * Since: 2.32
159  *
160  * Deprecated: 3.2: Use e_client_utils_open_new(), e_client_utils_open_new_finish() instead.
161  **/
162 EBook *
163 e_load_book_source_finish (ESource *source,
164                            GAsyncResult *result,
165                            GError **error)
166 {
167         GSimpleAsyncResult *simple;
168         LoadContext *context;
169
170         g_return_val_if_fail (E_IS_SOURCE (source), NULL);
171         g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
172
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);
177
178         simple = G_SIMPLE_ASYNC_RESULT (result);
179
180         if (g_simple_async_result_propagate_error (simple, error))
181                 return NULL;
182
183         context = g_simple_async_result_get_op_res_gpointer (simple);
184         g_return_val_if_fail (context != NULL, NULL);
185
186         return g_object_ref (context->book);
187 }