Oops, added back one line, swallowed by 3.12.0-36, fixing address completion
[claws.git] / src / addr_compl.c
index d7467aad1804c0a3dd68d0dbee07646abb477947..ad1840b5e1d11d6b80d0d71c9ac7a76e70a55060 100644 (file)
@@ -164,6 +164,77 @@ static gchar *completion_func(gpointer data)
        return ((completion_entry *)data)->string;
 } 
 
+static gint addr_completion_func(const gchar *needle, const gchar *haystack,
+               gsize n)
+{
+       if (needle == NULL || haystack == NULL)
+               return 1;
+
+       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.
  */
@@ -173,6 +244,17 @@ static void init_all(void)
        cm_return_if_fail(g_completion != NULL);
 }
 
+/**
+ * set the compare function (default is strncmp)
+ */
+static void set_match_any_part(const gboolean any_part)
+{
+       if (any_part && prefs_common.address_search_wildcard)
+               g_completion_set_compare(g_completion, addr_completion_func);
+       else
+               g_completion_set_compare(g_completion, strncmp);
+}
+
 static void free_all_addresses(void)
 {
        GList *walk;
@@ -536,6 +618,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;
@@ -865,14 +950,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 */
@@ -952,13 +1030,25 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
        }
 #ifndef GENERIC_UMPC
        else if( cw->listCount == 2 ) {
-               gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
-               /* Move off first row */
-               gtk_tree_selection_select_iter(selection, &iter);
+               if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)) {
+                       /* Move off first row */
+                       gtk_tree_selection_select_iter(selection, &iter);
+               }
        }
 #endif
 }
 
+void addrcompl_reflect_prefs_pixmap_theme(void) {
+       if (group_pixbuf) {
+               g_object_unref(G_OBJECT(group_pixbuf));
+               group_pixbuf = NULL;
+       }
+       if (email_pixbuf) {
+               g_object_unref(G_OBJECT(email_pixbuf));
+               email_pixbuf = NULL;
+       }
+}
+
 /**
  * Completion idle function. This function is called by the main (UI) thread
  * during UI idle time while an address search is in progress. Items from the
@@ -1157,6 +1247,7 @@ static void completion_window_apply_selection(GtkTreeView *list_view,
 void address_completion_start(GtkWidget *mainwindow)
 {
        start_address_completion(NULL);
+       set_match_any_part(TRUE);
 
        /* register focus change hook */
        g_signal_connect(G_OBJECT(mainwindow), "set_focus",
@@ -1497,7 +1588,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 */
@@ -1619,10 +1710,9 @@ void addrcompl_teardown( void ) {
        /* g_print( "addrcompl_teardown...\n" ); */
        addrcompl_free_window( _compWindow_ );
        _compWindow_ = NULL;
-       if( _displayQueue_ ) {
-               g_list_free( _displayQueue_ );
-       }
-       _displayQueue_ = NULL;
+
+       addrcompl_clear_queue();
+
        _completionIdleID_ = 0;
        /* g_print( "addrcompl_teardown...done\n" ); */
 }