]> git.0d.be Git - empathy.git/blob - libempathy/empathy-keyring.c
keyring: add set_password_{async,finish} functions
[empathy.git] / libempathy / empathy-keyring.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include "empathy-keyring.h"
22
23 #include <string.h>
24
25 #include <gnome-keyring.h>
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
28 #include "empathy-debug.h"
29
30 static GnomeKeyringPasswordSchema keyring_schema =
31   { GNOME_KEYRING_ITEM_GENERIC_SECRET,
32     { { "account", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
33       { "param", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
34       { NULL } } };
35
36 static void
37 find_items_cb (GnomeKeyringResult result,
38     GList *list,
39     gpointer user_data)
40 {
41   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
42
43   if (result != GNOME_KEYRING_RESULT_OK)
44     {
45       GError *error = g_error_new_literal (TP_ERROR,
46           TP_ERROR_DOES_NOT_EXIST,
47           gnome_keyring_result_to_message (result));
48       g_simple_async_result_set_from_error (simple, error);
49       g_clear_error (&error);
50     }
51
52   if (g_list_length (list) == 1)
53     {
54       GnomeKeyringFound *found = list->data;
55
56       DEBUG ("Got secret");
57
58       g_simple_async_result_set_op_res_gpointer (simple, found->secret, NULL);
59     }
60
61   g_simple_async_result_complete (simple);
62   g_object_unref (simple);
63 }
64
65 void
66 empathy_keyring_get_password_async (TpAccount *account,
67     GAsyncReadyCallback callback,
68     gpointer user_data)
69 {
70   GSimpleAsyncResult *simple;
71   GnomeKeyringAttributeList *match;
72   const gchar *account_id;
73
74   g_return_if_fail (TP_IS_ACCOUNT (account));
75   g_return_if_fail (callback != NULL);
76
77   simple = g_simple_async_result_new (G_OBJECT (account), callback,
78       user_data, empathy_keyring_get_password_async);
79
80   account_id = tp_proxy_get_object_path (account) +
81     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
82
83   DEBUG ("Trying to get password for: %s", account_id);
84
85   match = gnome_keyring_attribute_list_new ();
86   gnome_keyring_attribute_list_append_string (match, "account",
87       account_id);
88   gnome_keyring_attribute_list_append_string (match, "param", "password");
89
90   gnome_keyring_find_items (GNOME_KEYRING_ITEM_GENERIC_SECRET,
91       match, find_items_cb, simple, NULL);
92
93   gnome_keyring_attribute_list_free (match);
94 }
95
96 const gchar *
97 empathy_keyring_get_password_finish (TpAccount *account,
98     GAsyncResult *result,
99     GError **error)
100 {
101   GSimpleAsyncResult *simple;
102
103   g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
104   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
105
106   simple = G_SIMPLE_ASYNC_RESULT (result);
107
108   if (g_simple_async_result_propagate_error (simple, error))
109     return NULL;
110
111   g_return_val_if_fail (g_simple_async_result_is_valid (result,
112           G_OBJECT (account), empathy_keyring_get_password_async), NULL);
113
114   return g_simple_async_result_get_op_res_gpointer (simple);
115 }
116
117 static void
118 store_password_cb (GnomeKeyringResult result,
119     gpointer user_data)
120 {
121   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
122
123   if (result != GNOME_KEYRING_RESULT_OK)
124     {
125       GError *error = g_error_new_literal (TP_ERROR,
126           TP_ERROR_DOES_NOT_EXIST,
127           gnome_keyring_result_to_message (result));
128       g_simple_async_result_set_from_error (simple, error);
129       g_clear_error (&error);
130     }
131
132   g_simple_async_result_complete (simple);
133   g_object_unref (simple);
134 }
135
136 void
137 empathy_keyring_set_password_async (TpAccount *account,
138     const gchar *password,
139     GAsyncReadyCallback callback,
140     gpointer user_data)
141 {
142   GSimpleAsyncResult *simple;
143   const gchar *account_id;
144   gchar *name;
145
146   g_return_if_fail (TP_IS_ACCOUNT (account));
147   g_return_if_fail (password != NULL);
148   g_return_if_fail (callback != NULL);
149
150   simple = g_simple_async_result_new (G_OBJECT (account), callback,
151       user_data, empathy_keyring_set_password_async);
152
153   account_id = tp_proxy_get_object_path (account) +
154     strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
155
156   DEBUG ("Remembering password for %s", account_id);
157
158   name = g_strdup_printf ("account: %s; param: param-password", account_id);
159
160   gnome_keyring_store_password (&keyring_schema, NULL, name, password,
161       store_password_cb, simple, NULL,
162       "account", account_id,
163       "param", "password",
164       NULL);
165
166   g_free (name);
167 }
168
169 gboolean
170 empathy_keyring_set_password_finish (TpAccount *account,
171     GAsyncResult *result,
172     GError **error)
173 {
174   GSimpleAsyncResult *simple;
175
176   g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE);
177   g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
178
179   simple = G_SIMPLE_ASYNC_RESULT (result);
180
181   if (g_simple_async_result_propagate_error (simple, error))
182     return FALSE;
183
184   g_return_val_if_fail (g_simple_async_result_is_valid (result,
185           G_OBJECT (account), empathy_keyring_set_password_async), FALSE);
186
187   return TRUE;
188 }
189