diff options
author | Jonny Lamb <jonny.lamb@collabora.co.uk> | 2010-12-06 17:42:59 +0800 |
---|---|---|
committer | Jonny Lamb <jonny.lamb@collabora.co.uk> | 2010-12-06 17:42:59 +0800 |
commit | 4d487def1692d57aca987219fa1ff6f133c389f1 (patch) | |
tree | 3a2d2db0c5a1516820e09669f1fd6899f40470ae /libempathy | |
parent | 5c2ae48c27de994ba418bf759bbf011c8101943d (diff) | |
download | gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar.gz gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar.bz2 gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar.lz gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar.xz gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.tar.zst gsoc2013-empathy-4d487def1692d57aca987219fa1ff6f133c389f1.zip |
keyring: add simple keyring helper
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
Diffstat (limited to 'libempathy')
-rw-r--r-- | libempathy/Makefile.am | 2 | ||||
-rw-r--r-- | libempathy/empathy-keyring.c | 115 | ||||
-rw-r--r-- | libempathy/empathy-keyring.h | 37 |
3 files changed, 154 insertions, 0 deletions
diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index 5122e073d..ae9e2b5e9 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -47,6 +47,7 @@ libempathy_headers = \ empathy-irc-network-manager.h \ empathy-irc-network.h \ empathy-irc-server.h \ + empathy-keyring.h \ empathy-location.h \ empathy-message.h \ empathy-server-sasl-handler.h \ @@ -87,6 +88,7 @@ libempathy_la_SOURCES = \ empathy-irc-network-manager.c \ empathy-irc-network.c \ empathy-irc-server.c \ + empathy-keyring.c \ empathy-message.c \ empathy-server-sasl-handler.c \ empathy-server-tls-handler.c \ diff --git a/libempathy/empathy-keyring.c b/libempathy/empathy-keyring.c new file mode 100644 index 000000000..7f49acc6d --- /dev/null +++ b/libempathy/empathy-keyring.c @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2010 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "empathy-keyring.h" + +#include <string.h> + +#include <gnome-keyring.h> + +#define DEBUG_FLAG EMPATHY_DEBUG_OTHER +#include "empathy-debug.h" + +/* +GnomeKeyringPasswordSchema keyring_schema = + { GNOME_KEYRING_ITEM_GENERIC_SECRET, + { { "account", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, + { "param", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING }, + { NULL } } }; +*/ + +static void +find_items_cb (GnomeKeyringResult result, + GList *list, + gpointer user_data) +{ + GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data); + + if (result != GNOME_KEYRING_RESULT_OK) + { + GError error = { TP_ERROR, TP_ERROR_DOES_NOT_EXIST, + (gchar *) gnome_keyring_result_to_message (result) }; + g_simple_async_result_set_from_error (simple, &error); + } + + if (g_list_length (list) == 1) + { + GnomeKeyringFound *found = list->data; + + DEBUG ("Got secret"); + + g_simple_async_result_set_op_res_gpointer (simple, found->secret, NULL); + } + + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +void +empathy_keyring_get_password_async (TpAccount *account, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *simple; + GnomeKeyringAttributeList *match; + const gchar *account_id; + + g_return_if_fail (TP_IS_ACCOUNT (account)); + g_return_if_fail (callback != NULL); + + simple = g_simple_async_result_new (G_OBJECT (account), callback, + user_data, empathy_keyring_get_password_async); + + account_id = tp_proxy_get_object_path (account) + + strlen (TP_ACCOUNT_OBJECT_PATH_BASE); + + DEBUG ("Trying to get password for: %s", account_id); + + match = gnome_keyring_attribute_list_new (); + gnome_keyring_attribute_list_append_string (match, "account", + account_id); + gnome_keyring_attribute_list_append_string (match, "param", "password"); + + gnome_keyring_find_items (GNOME_KEYRING_ITEM_GENERIC_SECRET, + match, find_items_cb, simple, NULL); + + gnome_keyring_attribute_list_free (match); +} + +const gchar * +empathy_keyring_get_password_finish (TpAccount *account, + GAsyncResult *result, + GError **error) +{ + GSimpleAsyncResult *simple; + + g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL); + g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL); + + simple = G_SIMPLE_ASYNC_RESULT (result); + + if (g_simple_async_result_propagate_error (simple, error)) + return NULL; + + g_return_val_if_fail (g_simple_async_result_is_valid (result, + G_OBJECT (account), empathy_keyring_get_password_async), NULL); + + return g_simple_async_result_get_op_res_gpointer (simple); +} diff --git a/libempathy/empathy-keyring.h b/libempathy/empathy-keyring.h new file mode 100644 index 000000000..2c062e1cd --- /dev/null +++ b/libempathy/empathy-keyring.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2010 Collabora Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __EMPATHY_KEYRING_H__ +#define __EMPATHY_KEYRING_H__ + +#include <gio/gio.h> + +#include <telepathy-glib/account.h> + +G_BEGIN_DECLS + +void empathy_keyring_get_password_async (TpAccount *account, + GAsyncReadyCallback callback, gpointer user_data); + +const gchar * empathy_keyring_get_password_finish (TpAccount *account, + GAsyncResult *result, GError **error); + +G_END_DECLS + +#endif /* __EMPATHY_KEYRING_H__ */ + |