Fix bug #3352, "found_in_addressbook matches entries from GPG keyring"
[claws.git] / src / addr_compl.c
index adcb7ccaeb9142450bb70c717224fcfeb19feaad..44303a6a2b2fa0f70240a664b3a327b8d6daa31e 100644 (file)
@@ -173,6 +173,68 @@ static gint addr_completion_func(const gchar *needle, const gchar *haystack,
        return (strcasestr(haystack, needle) != NULL ? 0 : 1);
 }
 
+/**
+ * Function used by GTK to compare elements for sorting
+ * name match beginning > name match after space > email address
+ *   match beginning and full match before @ > email adress
+ *   match beginning. Otherwise match position in string.
+ * \param a first element in comparsion
+ * \param b second element in comparison
+ */
+static gint weight_addr_match(const address_entry* addr)
+{
+       gint    n_weight = addr->name ? strlen(addr->name): 0;
+       gint    a_weight = addr->address ? strlen(addr->address) : n_weight;
+       gchar*  match = NULL;
+
+       match = strcasestr(addr->name, g_completion_prefix);
+       if (match != NULL) {
+               if (match == addr->name)
+                       n_weight = -4;
+               else if (match > addr->name && *(match - 1) == ' ')
+                       n_weight = -3;
+               else
+                       n_weight = match - addr->name;
+       }
+
+       if (addr->address) {
+               match = strcasestr(addr->address, g_completion_prefix);
+               if (match != NULL) {
+                       if (match == addr->address)
+                               a_weight = -1;
+                       else
+                               a_weight = match - addr->address;
+
+                       if (strlen(match) > strlen(g_completion_prefix)
+                        && *(match + strlen(g_completion_prefix)) == '@')
+                               a_weight--;
+               }
+       }
+
+       if (n_weight == -4 && a_weight < 0)
+               n_weight = -5;
+
+       return MIN(a_weight, n_weight);
+}
+
+static gint addr_comparison_func(gconstpointer a, gconstpointer b)
+{
+       const address_entry*    a_ref = (const address_entry*)a;
+       const address_entry*    b_ref = (const address_entry*)b;
+       gint                    a_weight = weight_addr_match(a_ref);
+       gint                    b_weight = weight_addr_match(b_ref);
+       gint                    cmp;
+
+       if (a_weight < b_weight)
+               return -1;
+       else if (a_weight > b_weight)
+               return 1;
+       else {
+               cmp = strcmp(a_ref->name, b_ref->name);
+               return cmp ? cmp : g_strcmp0(a_ref->address, b_ref->address);
+       }
+}
+
 /**
  * Initialize all completion index data.
  */
@@ -324,7 +386,9 @@ static void read_address_book(gchar *folderpath) {
        }
 #endif
        /* plugins may hook in here to modify/extend the completion list */
-       hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, &g_address_list);
+       if(!folderpath) {
+               hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, &g_address_list);
+       }
 
        g_address_list = g_list_reverse(g_address_list);
        g_completion_list = g_list_reverse(g_completion_list);
@@ -556,6 +620,9 @@ guint complete_address(const gchar *str)
                }
                count = cpl + 1;        /* index 0 is the original prefix */
                g_completion_next = 1;  /* we start at the first completed one */
+               if (prefs_common.address_search_wildcard)
+                   g_completion_addresses = g_slist_sort(g_completion_addresses,
+                                                         addr_comparison_func);
        } else {
                g_free(g_completion_prefix);
                g_completion_prefix = NULL;
@@ -885,14 +952,7 @@ static void addrcompl_resize_window( CompletionWindow *cw ) {
        gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height );
 #endif
 
-       /* simple _hide breaks size requisition !? */
-#if !GTK_CHECK_VERSION(3, 0, 0)
-       gtk_widget_hide_all( cw->window );
-       gtk_widget_show_all( cw->window );
-#else
-       gtk_widget_hide( cw->window );
-       gtk_widget_show( cw->window );
-#endif
+       gtk_widget_queue_resize_no_redraw(cw->list_view);
        gtk_widget_size_request( cw->list_view, &r );
 
        /* Adjust window height to available screen space */
@@ -1530,7 +1590,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
                        (GTK_TREE_VIEW(list_view),
                         event->keyval == GDK_KEY_Down ||
                         event->keyval == GDK_KEY_Page_Down ? TRUE : FALSE);
-               return FALSE;
+               return TRUE;
        }               
 
        /* make tab move to next field */