2 * PGP/Core keyring autocompletion
4 * Copyright (C) 2014 Christian Hesse <mail@eworm.de> and the Claws Mail team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "claws-features.h"
32 #include <glib/gi18n.h>
36 #include "autocompletion.h"
37 #include "addr_compl.h"
38 #include "prefs_gpg.h"
42 static guint autocompletion_hook_id = 0;
44 static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
51 GList *addr_list = NULL;
54 /* just return if autocompletion is disabled */
55 if (!prefs_gpg_get_config()->autocompletion)
59 gpgme_check_version(NULL);
61 if ((err = gpgme_new(&ctx)) == 0) {
62 err = gpgme_op_keylist_start(ctx, NULL, 0);
64 /* walk the available keys */
66 if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
69 /* skip keys that are revoked, expired, ... */
70 if ((key->revoked || key->expired || key->disabled || key->invalid) == FALSE) {
74 /* walk all user ids within a key */
76 if (uid->email != NULL && *uid->email != 0) {
77 ae = g_new0(address_entry, 1);
79 ae->address = g_strdup(uid->email);
80 addr_compl_add_address1(ae->address, ae);
82 if (uid->name != NULL && *uid->name != 0) {
83 ae->name = g_strdup(uid->name);
84 addr_compl_add_address1(ae->name, ae);
88 ae->grp_emails = NULL;
90 addr_list = g_list_prepend(addr_list, ae);
92 debug_print("%s <%s>\n", uid->name, uid->email);
95 if (prefs_gpg_get_config()->autocompletion_limit > 0 &&
96 prefs_gpg_get_config()->autocompletion_limit == i)
103 gpgme_key_unref(key);
108 if (gpg_err_code(err) != GPG_ERR_EOF) {
109 debug_print("can not list keys: %s\n", gpgme_strerror(err));
112 *((GList **)source) = addr_list;
117 gboolean autocompletion_done(void)
119 if (autocompletion_hook_id > 0)
121 hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, autocompletion_hook_id);
123 debug_print("PGP address autocompletion hook unregistered\n");
129 gint autocompletion_init(gchar ** error)
131 if ((autocompletion_hook_id = hooks_register_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, pgp_autocompletion_hook, NULL)) == -1)
133 *error = g_strdup(_("Failed to register PGP address autocompletion hook"));
136 debug_print("PGP address autocompletion hook registered\n");