Fix missing mutex accessing _displayQueue_
[claws.git] / src / addr_compl.c
index c0ded1fbe5351dfaed19ce59c0b1755780484b91..1d7d65d654767e1536a684e9c808cfb86361b9a9 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  *
- * Copyright (C) 2000-2011 by Alfons Hoogervorst & The Claws Mail Team.
+ * Copyright (C) 2000-2012 by Alfons Hoogervorst & 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
@@ -20,6 +20,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 #include "defs.h"
 
 #  include <wctype.h>
 #endif
 
-#include "addrindex.h"
 #include "addr_compl.h"
+#include "addritem.h"
 #include "utils.h"
 #include "prefs_common.h"
 #include "claws.h"
 #include "hooks.h"
 #include "gtkutils.h"
+#include "stock_pixmap.h"
 #include <pthread.h>
 
+#ifndef USE_NEW_ADDRBOOK
+       #include "addrindex.h"
+#else
+       #include "addressbook-dbus.h"
+#endif
+
 /*!
  *\brief       For the GtkListStore
  */
@@ -156,6 +164,15 @@ 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);
+}
+
 /**
  * Initialize all completion index data.
  */
@@ -165,6 +182,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;
@@ -246,35 +274,32 @@ void addr_compl_add_address1(const char *str, address_entry *ae)
 static gint add_address(const gchar *name, const gchar *address, 
                        const gchar *nick, const gchar *alias, GList *grp_emails)
 {
-       address_entry    *ae;
-       gboolean is_group = FALSE;
+       address_entry *ae;
 
-       if (!name || !address) {
-               if (!address && !nick && !alias && grp_emails) {
-                       is_group = TRUE;
-               } else
-                       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;
 }
@@ -286,8 +311,18 @@ static void read_address_book(gchar *folderpath) {
        free_all_addresses();
        free_completion_list();
 
+#ifndef USE_NEW_ADDRBOOK
        addrindex_load_completion( add_address, folderpath );
-
+#else
+       GError* error = NULL;
+       
+       addrcompl_initialize();
+       if (! addrindex_dbus_load_completion(add_address, &error)) {
+               g_warning("Failed to populate address completion list");
+        g_error_free(error);
+               return;
+       }
+#endif
        /* plugins may hook in here to modify/extend the completion list */
        hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, &g_address_list);
 
@@ -753,8 +788,9 @@ static CompletionWindow *addrcompl_create_window( void ) {
  */
 static void addrcompl_destroy_window( CompletionWindow *cw ) {
        /* Stop all searches currently in progress */
+#ifndef USE_NEW_ADDRBOOK
        addrindex_stop_search( _queryID_ );
-
+#endif
        /* Remove idler function... or application may not terminate */
        if( _completionIdleID_ != 0 ) {
                g_source_remove( _completionIdleID_ );
@@ -843,19 +879,35 @@ 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_size_request( cw->list_view, &r );
 
        /* Adjust window height to available screen space */
-       if( ( y + r.height ) > gdk_screen_height() ) {
-               gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
-               gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
-       } else
-               gtk_widget_set_size_request(cw->window, width, r.height);
+       if( y + r.height > gdk_screen_height())
+               r.height = gdk_screen_height() - y;
+
+       gtk_widget_set_size_request(cw->window, width, r.height);
+
+       gdk_pointer_grab(gtk_widget_get_window(cw->window), TRUE,
+                        GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
+                        GDK_BUTTON_RELEASE_MASK,
+                        NULL, NULL, GDK_CURRENT_TIME);
+       gdk_keyboard_grab(gtk_widget_get_window(cw->window), FALSE, GDK_CURRENT_TIME);
+       gtk_grab_add(cw->window);
+
 }
 
 static GdkPixbuf *group_pixbuf = NULL;
@@ -927,6 +979,17 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
 #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
@@ -968,6 +1031,7 @@ static gboolean addrcompl_idle( gpointer data ) {
  *                   criteria.
  * \param data       Query data.
  */
+#ifndef USE_NEW_ADDRBOOK
 static gint addrcompl_callback_entry(
        gpointer sender, gint queryID, GList *listEMail, gpointer data )
 {
@@ -993,6 +1057,7 @@ static gint addrcompl_callback_entry(
 
        return 0;
 }
+#endif
 
 /**
  * Clear the display queue.
@@ -1038,6 +1103,7 @@ static void addrcompl_load_local( void ) {
  * Start the search.
  */
 static void addrcompl_start_search( void ) {
+#ifndef USE_NEW_ADDRBOOK
        gchar *searchTerm;
 
        searchTerm = g_strdup( _compWindow_->searchTerm );
@@ -1046,6 +1112,7 @@ static void addrcompl_start_search( void ) {
        _queryID_ = addrindex_setup_search(
                searchTerm, NULL, addrcompl_callback_entry );
        g_free( searchTerm );
+#endif
        /* g_print( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
 
        /* Load local stuff */
@@ -1056,7 +1123,11 @@ 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
        addrindex_start_search( _queryID_ );
+#else
+       
+#endif
 }
 
 /**
@@ -1117,6 +1188,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",
@@ -1262,7 +1334,7 @@ static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
 
        cm_return_val_if_fail(entry != NULL, FALSE);
 
-       if (!gtkut_widget_has_focus(GTK_WIDGET(entry))) return FALSE;
+       if (!gtk_widget_has_focus(GTK_WIDGET(entry))) return FALSE;
 
        /* get an address component from the cursor */
        searchTerm = get_address_from_edit( entry, &cursor_pos );
@@ -1338,7 +1410,11 @@ 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);
@@ -1370,13 +1446,8 @@ static void address_completion_create_completion_window( GtkEntry *entry_ )
                         GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
                         GDK_BUTTON_RELEASE_MASK,
                         NULL, NULL, GDK_CURRENT_TIME);
+       gdk_keyboard_grab(gtk_widget_get_window(window), FALSE, GDK_CURRENT_TIME);
        gtk_grab_add( window );
-
-       /* XXX: GTK2 too??? 
-        *
-        * GTK1: this gets rid of the irritating focus rectangle that doesn't
-        * follow the selection */
-       gtkut_widget_set_can_focus(list_view, FALSE);
 }
 
 /**
@@ -1580,10 +1651,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" ); */
 }