2 * Copyright (C) 2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <glib/gi18n-lib.h>
23 #include "empathy-keyring.h"
27 #include <libsecret/secret.h>
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
32 #include "empathy-debug.h"
34 static const SecretSchema account_keyring_schema =
35 { "org.gnome.Empathy.Account", SECRET_SCHEMA_DONT_MATCH_NAME,
36 { { "account-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
37 { "param-name", SECRET_SCHEMA_ATTRIBUTE_STRING },
40 static const SecretSchema room_keyring_schema =
41 { "org.gnome.Empathy.Room", SECRET_SCHEMA_DONT_MATCH_NAME,
42 { { "account-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
43 { "room-id", SECRET_SCHEMA_ATTRIBUTE_STRING },
47 empathy_keyring_is_available (void)
55 lookup_item_cb (GObject *source,
59 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
63 password = secret_password_lookup_finish (result, &error);
66 g_simple_async_result_set_error (simple, TP_ERROR,
67 TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
68 g_clear_error (&error);
74 g_simple_async_result_set_error (simple, TP_ERROR,
75 TP_ERROR_DOES_NOT_EXIST, _("Password not found"));
79 g_simple_async_result_set_op_res_gpointer (simple, password,
80 (GDestroyNotify) secret_password_free);
83 g_simple_async_result_complete (simple);
84 g_object_unref (simple);
88 empathy_keyring_get_account_password_async (TpAccount *account,
89 GAsyncReadyCallback callback,
92 GSimpleAsyncResult *simple;
93 const gchar *account_id;
95 g_return_if_fail (TP_IS_ACCOUNT (account));
96 g_return_if_fail (callback != NULL);
98 simple = g_simple_async_result_new (G_OBJECT (account), callback,
99 user_data, empathy_keyring_get_account_password_async);
101 account_id = tp_proxy_get_object_path (account) +
102 strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
104 DEBUG ("Trying to get password for: %s", account_id);
106 secret_password_lookup (&account_keyring_schema, NULL,
107 lookup_item_cb, simple,
108 "account-id", account_id,
109 "param-name", "password",
114 empathy_keyring_get_room_password_async (TpAccount *account,
116 GAsyncReadyCallback callback,
119 GSimpleAsyncResult *simple;
120 const gchar *account_id;
122 g_return_if_fail (TP_IS_ACCOUNT (account));
123 g_return_if_fail (id != NULL);
124 g_return_if_fail (callback != NULL);
126 simple = g_simple_async_result_new (G_OBJECT (account), callback,
127 user_data, empathy_keyring_get_room_password_async);
129 account_id = tp_proxy_get_object_path (account) +
130 strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
132 DEBUG ("Trying to get password for room '%s' on account '%s'",
135 secret_password_lookup (&room_keyring_schema, NULL,
136 lookup_item_cb, simple,
137 "account-id", account_id,
143 empathy_keyring_get_account_password_finish (TpAccount *account,
144 GAsyncResult *result,
147 empathy_implement_finish_return_pointer (account,
148 empathy_keyring_get_account_password_async);
152 empathy_keyring_get_room_password_finish (TpAccount *account,
153 GAsyncResult *result,
156 empathy_implement_finish_return_pointer (account,
157 empathy_keyring_get_room_password_async);
163 store_password_cb (GObject *source,
164 GAsyncResult *result,
167 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
168 GError *error = NULL;
170 if (!secret_password_store_finish (result, &error))
172 g_simple_async_result_set_error (simple, TP_ERROR,
173 TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
174 g_error_free (error);
177 g_simple_async_result_complete (simple);
178 g_object_unref (simple);
182 empathy_keyring_set_account_password_async (TpAccount *account,
183 const gchar *password,
184 GAsyncReadyCallback callback,
187 GSimpleAsyncResult *simple;
188 const gchar *account_id;
191 g_return_if_fail (TP_IS_ACCOUNT (account));
192 g_return_if_fail (password != NULL);
194 simple = g_simple_async_result_new (G_OBJECT (account), callback,
195 user_data, empathy_keyring_set_account_password_async);
197 account_id = tp_proxy_get_object_path (account) +
198 strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
200 DEBUG ("Remembering password for %s", account_id);
202 name = g_strdup_printf (_("IM account password for %s (%s)"),
203 tp_account_get_display_name (account), account_id);
205 secret_password_store (&account_keyring_schema, NULL, name, password,
206 NULL, store_password_cb, simple,
207 "account-id", account_id,
208 "param-name", "password",
215 empathy_keyring_set_room_password_async (TpAccount *account,
217 const gchar *password,
218 GAsyncReadyCallback callback,
221 GSimpleAsyncResult *simple;
222 const gchar *account_id;
225 g_return_if_fail (TP_IS_ACCOUNT (account));
226 g_return_if_fail (id != NULL);
227 g_return_if_fail (password != NULL);
229 simple = g_simple_async_result_new (G_OBJECT (account), callback,
230 user_data, empathy_keyring_set_room_password_async);
232 account_id = tp_proxy_get_object_path (account) +
233 strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
235 DEBUG ("Remembering password for room '%s' on account '%s'", id, account_id);
237 name = g_strdup_printf (_("Password for chatroom '%s' on account %s (%s)"),
238 id, tp_account_get_display_name (account), account_id);
240 secret_password_store (&room_keyring_schema, NULL, name, password,
241 NULL, store_password_cb, simple,
242 "account-id", account_id,
250 empathy_keyring_set_account_password_finish (TpAccount *account,
251 GAsyncResult *result,
254 empathy_implement_finish_void (account, empathy_keyring_set_account_password_async);
258 empathy_keyring_set_room_password_finish (TpAccount *account,
259 GAsyncResult *result,
262 empathy_implement_finish_void (account, empathy_keyring_set_room_password_async);
268 items_delete_cb (GObject *source,
269 GAsyncResult *result,
272 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
273 GError *error = NULL;
275 if (!secret_password_clear_finish (result, &error))
277 g_simple_async_result_set_error (simple, TP_ERROR,
278 TP_ERROR_DOES_NOT_EXIST, "%s", error->message);
279 g_error_free (error);
282 g_simple_async_result_complete (simple);
283 g_object_unref (simple);
287 empathy_keyring_delete_account_password_async (TpAccount *account,
288 GAsyncReadyCallback callback,
291 GSimpleAsyncResult *simple;
292 const gchar *account_id;
294 g_return_if_fail (TP_IS_ACCOUNT (account));
296 simple = g_simple_async_result_new (G_OBJECT (account), callback,
297 user_data, empathy_keyring_delete_account_password_async);
299 account_id = tp_proxy_get_object_path (account) +
300 strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
302 secret_password_clear (&account_keyring_schema, NULL,
303 items_delete_cb, simple,
304 "account-id", account_id,
305 "param-name", "password",
310 empathy_keyring_delete_account_password_finish (TpAccount *account,
311 GAsyncResult *result,
314 empathy_implement_finish_void (account, empathy_keyring_delete_account_password_async);