Fix a few memory leaks when enumerating keys with GpgME.
[claws.git] / src / plugins / pgpcore / autocompletion.c
index c64c63a4966ed076c527cd265644ee284063b874..a4f15a6b159f0f6b807de0cefda575ad3485d1eb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * PGP/Core keyring autocompletion
  *
- * Copyright (C) 2014 Christian Hesse <mail@eworm.de>
+ * Copyright (C) 2014 Christian Hesse <mail@eworm.de> and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <errno.h>
 #include <inttypes.h>
 #include <stdlib.h>
 #include <gpgme.h>
 
 #include "autocompletion.h"
+#include "addr_compl.h"
+#include "prefs_gpg.h"
+#include "hooks.h"
+#include "utils.h"
 
 static guint autocompletion_hook_id = 0;
 
@@ -40,6 +49,7 @@ static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
        gpgme_user_id_t uid;
        address_entry *ae;
        GList *addr_list = NULL;
+       gint i;
 
        /* just return if autocompletion is disabled */
        if (!prefs_gpg_get_config()->autocompletion)
@@ -59,6 +69,7 @@ static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
                        /* skip keys that are revoked, expired, ... */
                        if ((key->revoked || key->expired || key->disabled || key->invalid) == FALSE) {
                                uid = key->uids;
+                               i = 0;
 
                                /* walk all user ids within a key */
                                while (uid != NULL) {
@@ -80,10 +91,16 @@ static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
 
                                                debug_print("%s <%s>\n", uid->name, uid->email);
                                        }
+
+                                       if (prefs_gpg_get_config()->autocompletion_limit > 0 &&
+                                                       prefs_gpg_get_config()->autocompletion_limit == i)
+                                               break;
+
                                        uid = uid->next;
+                                       i++;
                                }
                        }
-                       gpgme_key_release(key);
+                       gpgme_key_unref(key);
                }
                gpgme_release(ctx);
        }