remove all gtk3 conditionals
[claws.git] / src / addr_compl.c
index de6731eb5ac7ff731e4a757f0b78ec7aba191b15..08534085b22161f6ba4ae00b5fa34f13679d2b6a 100644 (file)
@@ -46,7 +46,7 @@
 #include "stock_pixmap.h"
 #include <pthread.h>
 
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        #include "addrindex.h"
 #else
        #include "addressbook-dbus.h"
@@ -164,6 +164,79 @@ 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;
+
+       if (addr->name)
+               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 +246,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;
@@ -256,30 +340,30 @@ static gint add_address(const gchar *name, const gchar *address,
 {
        address_entry *ae;
 
-       if (!name || !address) {
-               if (address || nick || alias || !grp_emails) 
-                       return -1;
-       }
+       if (!address && !grp_emails)
+               return -1;
 
-       ae = g_new0(address_entry, 1);
+       if (!name)
+               name = "";
 
+       ae = g_new0(address_entry, 1);
        cm_return_val_if_fail(ae != NULL, -1);
 
-       ae->name    = g_strdup(name);
+       ae->name = g_strdup(name);
        ae->address = g_strdup(address);
        ae->grp_emails = grp_emails;
        g_address_list = g_list_prepend(g_address_list, ae);
 
        addr_compl_add_address1(name, ae);
+
        if (address != NULL && *address != '\0')
                addr_compl_add_address1(address, ae);
 
        if (nick != NULL && *nick != '\0')
                addr_compl_add_address1(nick, ae);
 
-       if ( alias != NULL && *alias != '\0') {
+       if (alias != NULL && *alias != '\0')
                addr_compl_add_address1(alias, ae);
-       }
 
        return 0;
 }
@@ -291,7 +375,7 @@ static void read_address_book(gchar *folderpath) {
        free_all_addresses();
        free_completion_list();
 
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        addrindex_load_completion( add_address, folderpath );
 #else
        GError* error = NULL;
@@ -304,7 +388,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);
@@ -351,7 +437,7 @@ static void clear_completion_cache(void)
  * address completion.
  * \return The number of addresses in the completion list.
  */
-gint start_address_completion(gchar *folderpath)
+guint start_address_completion(gchar *folderpath)
 {
        gboolean different_book = FALSE;
        clear_completion_cache();
@@ -536,6 +622,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;
@@ -768,7 +857,7 @@ static CompletionWindow *addrcompl_create_window( void ) {
  */
 static void addrcompl_destroy_window( CompletionWindow *cw ) {
        /* Stop all searches currently in progress */
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        addrindex_stop_search( _queryID_ );
 #endif
        /* Remove idler function... or application may not terminate */
@@ -859,20 +948,9 @@ static void addrcompl_resize_window( CompletionWindow *cw ) {
        gint x, y, width, height, depth;
 
        /* Get current geometry of window */
-#if !GTK_CHECK_VERSION(3, 0, 0)
        gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height, &depth );
-#else
-       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 */
@@ -908,11 +986,11 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
        GdkPixbuf *pixbuf;
        
        if (!group_pixbuf) {
-               stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
+               stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
                g_object_ref(G_OBJECT(group_pixbuf));
        }
        if (!email_pixbuf) {
-               stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
+               stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
                g_object_ref(G_OBJECT(email_pixbuf));
        }
        /* g_print( "\t\tAdding :%s\n", address ); */
@@ -944,21 +1022,34 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
        gtk_grab_add( cw->window );
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
-       gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
+       if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
+               return;
 
-       if( cw->listCount == 1 ) {
+       if (cw->listCount == 1) {
                /* Select first row for now */
                gtk_tree_selection_select_iter(selection, &iter);
        }
 #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);
+       else if (cw->listCount == 2) {
+               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
@@ -1000,7 +1091,7 @@ static gboolean addrcompl_idle( gpointer data ) {
  *                   criteria.
  * \param data       Query data.
  */
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
 static gint addrcompl_callback_entry(
        gpointer sender, gint queryID, GList *listEMail, gpointer data )
 {
@@ -1035,7 +1126,7 @@ static void addrcompl_clear_queue( void ) {
        /* Clear out display queue */
        pthread_mutex_lock( & _completionMutex_ );
 
-       g_list_free( _displayQueue_ );
+       g_list_free_full( _displayQueue_, g_free );
        _displayQueue_ = NULL;
 
        pthread_mutex_unlock( & _completionMutex_ );
@@ -1072,7 +1163,7 @@ static void addrcompl_load_local( void ) {
  * Start the search.
  */
 static void addrcompl_start_search( void ) {
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        gchar *searchTerm;
 
        searchTerm = g_strdup( _compWindow_->searchTerm );
@@ -1092,7 +1183,7 @@ static void addrcompl_start_search( void ) {
                g_idle_add( (GSourceFunc) addrcompl_idle, NULL );
        /* g_print( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
 
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        addrindex_start_search( _queryID_ );
 #else
        
@@ -1122,7 +1213,7 @@ static void completion_window_apply_selection(GtkTreeView *list_view,
        GList *grp_emails = NULL;
 
        selection = gtk_tree_view_get_selection(list_view);
-       if (! gtk_tree_selection_get_selected(selection, &model, &iter))
+       if (!gtk_tree_selection_get_selected(selection, &model, &iter))
                return;
 
        /* First remove the idler */
@@ -1157,6 +1248,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",
@@ -1335,9 +1427,10 @@ static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
        else 
 #endif
        if( ncount == 0 ) {
-               addrcompl_add_queue( g_strdup( searchTerm ) );
+               addrcompl_add_queue( searchTerm );
+       } else {
+               g_free( searchTerm );
        }
-       g_free( searchTerm );
 
        return TRUE;
 }
@@ -1378,11 +1471,7 @@ static void address_completion_create_completion_window( GtkEntry *entry_ )
                GTK_SHADOW_OUT);
        /* Use entry widget to create initial window */
        gdkwin = gtk_widget_get_window(entry),
-#if !GTK_CHECK_VERSION(3, 0, 0)
        gdk_window_get_geometry(gdkwin, &x, &y, &width, &height, &depth);
-#else
-       gdk_window_get_geometry(gdkwin, &x, &y, &width, &height);
-#endif
        gdk_window_get_origin (gdkwin, &x, &y);
        y += height;
        gtk_window_move(GTK_WINDOW(window), x, y);
@@ -1497,7 +1586,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 +1708,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" ); */
 }
@@ -1739,8 +1827,33 @@ static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
        return FALSE;
 }
 
+gboolean found_in_addressbook(const gchar *address)
+{
+       gchar *addr = NULL;
+       gboolean found = FALSE;
+       gint num_addr = 0;
+
+       if (!address)
+               return FALSE;
+
+       addr = g_strdup(address);
+       extract_address(addr);
+       num_addr = complete_address(addr);
+       if (num_addr > 1) {
+               /* skip first item (this is the search string itself) */
+               int i = 1;
+               for (; i < num_addr && !found; i++) {
+                       gchar *caddr = get_complete_address(i);
+                       extract_address(caddr);
+                       if (strcasecmp(caddr, addr) == 0)
+                               found = TRUE;
+                       g_free(caddr);
+               }
+       }
+       g_free(addr);
+       return found;
+}
 
 /*
  * End of Source.
  */
-