* src/addr_compl.c
[claws.git] / src / addr_compl.c
index 8a1ee9502203e2fa6ee39dcbedd64ec1404744c5..2260a7802e5d0ee03714c8f0ca8a928164564c43 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  *
- * Copyright (c) 2000-2003 by Alfons Hoogervorst <alfons@proteus.demon.nl>
+ * Copyright (C) 2000-2004 by Alfons Hoogervorst & The Sylpheed Claws 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
 #  include <wctype.h>
 #endif
 
-#include "addressbook.h"
+#include "addrindex.h"
 #include "addr_compl.h"
 #include "utils.h"
-#include "addritem.h"
-#include "addrquery.h"
 #include <pthread.h>
 
-/* How it works:
+/*
+ * How it works:
  *
  * The address book is read into memory. We set up an address list
  * containing all address book entries. Next we make the completion
  * After calling the g_completion_complete(), we get a reference
  * to a valid email address.  
  *
- * Completion is very simplified. We never complete on another searchTerm,
- * i.e. we neglect the next smallest possible searchTerm for the current
+ * Completion is very simplified. We never complete on another prefix,
+ * i.e. we neglect the next smallest possible prefix for the current
  * completion cache. This is simply done so we might break up the
  * addresses a little more (e.g. break up alfons@proteus.demon.nl into
  * something like alfons, proteus, demon, nl; and then completing on
  * any of those words).
+ */ 
+       
+/**
+ * address_entry - structure which refers to the original address entry in the
+ * address book .
  */
-
-typedef struct _CompletionWindow CompletionWindow;
-struct _CompletionWindow {
-       gint      listCount;
-       gchar     *searchTerm;
-       GtkWidget *window;
-       GtkWidget *entry;
-       GtkWidget *clist;
-};
+typedef struct
+{
+       gchar *name;
+       gchar *address;
+} address_entry;
 
 /**
- * Current query ID.
+ * completion_entry - structure used to complete addresses, with a reference
+ * the the real address information.
  */
-static gint _queryID_ = 0;
+typedef struct
+{
+       gchar           *string; /* string to complete */
+       address_entry   *ref;    /* address the string belongs to  */
+} completion_entry;
+
+/*******************************************************************************/
+
+static gint        g_ref_count;        /* list ref count */
+static GList      *g_completion_list;  /* list of strings to be checked */
+static GList      *g_address_list;     /* address storage */
+static GCompletion *g_completion;      /* completion object */
+
+/* To allow for continuing completion we have to keep track of the state
+ * using the following variables. No need to create a context object. */
+
+static gint        g_completion_count;         /* nr of addresses incl. the prefix */
+static gint        g_completion_next;          /* next prev address */
+static GSList     *g_completion_addresses;     /* unique addresses found in the
+                                                  completion cache. */
+static gchar      *g_completion_prefix;        /* last prefix. (this is cached here
+                                                * because the prefix passed to g_completion
+                                                * is g_strdown()'ed */
+
+/*******************************************************************************/
 
 /**
- * Completion idle ID.
+ * Function used by GTK to find the string data to be used for completion.
+ * \param data Pointer to data being processed.
  */
-static gint _completionIdleID_ = 0;
+static gchar *completion_func(gpointer data)
+{
+       g_return_val_if_fail(data != NULL, NULL);
+
+       return ((completion_entry *)data)->string;
+} 
 
 /**
- * Completion window.
+ * Initialize all completion index data.
  */
-static CompletionWindow *_compWindow_ = NULL;
+static void init_all(void)
+{
+       g_completion = g_completion_new(completion_func);
+       g_return_if_fail(g_completion != NULL);
+}
 
 /**
- * Mutex to protect callback from multiple threads.
+ * Free up all completion index data.
  */
-static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
+static void free_all(void)
+{
+       GList *walk;
+       
+       walk = g_list_first(g_completion_list);
+       for (; walk != NULL; walk = g_list_next(walk)) {
+               completion_entry *ce = (completion_entry *) walk->data;
+               g_free(ce->string);
+               g_free(walk->data);
+       }
+       g_list_free(g_completion_list);
+       g_completion_list = NULL;
+       
+       walk = g_address_list;
+       for (; walk != NULL; walk = g_list_next(walk)) {
+               address_entry *ae = (address_entry *) walk->data;
+               g_free(ae->name);
+               g_free(ae->address);
+               g_free(walk->data);
+       }
+       g_list_free(g_address_list);
+       g_address_list = NULL;
+       
+       g_completion_free(g_completion);
+       g_completion = NULL;
+}
 
 /**
- * Completion queue list.
+ * Append specified address entry to the index.
+ * \param str Index string value.
+ * \param ae  Entry containing address data.
  */
-static GList *_displayQueue_ = NULL;
+static void add_address1(const char *str, address_entry *ae)
+{
+       completion_entry *ce1;
+       ce1 = g_new0(completion_entry, 1),
+       ce1->string = g_strdup(str);
+       /* GCompletion list is case sensitive */
+       g_strdown(ce1->string);
+       ce1->ref = ae;
+
+       g_completion_list = g_list_prepend(g_completion_list, ce1);
+}
 
 /**
- * Create a completion window object.
- * \return Initialized completion window.
+ * Adds address to the completion list. This function looks complicated, but
+ * it's only allocation checks. Each value will be included in the index.
+ * \param name    Recipient name.
+ * \param address EMail address.
+ * \param alias   Alias to append.
+ * \return <code>0</code> if entry appended successfully, or <code>-1</code>
+ *         if failure.
  */
-static CompletionWindow *addrcompl_create_window( void ) {
-       CompletionWindow *cw;
+static gint add_address(const gchar *name, const gchar *address, const gchar *alias)
+{
+       address_entry    *ae;
 
-       cw = g_new0( CompletionWindow, 1 );
-       cw->listCount = 0;
-       cw->searchTerm = NULL;
-       cw->window = NULL;
-       cw->entry = NULL;
-       cw->clist = NULL;
+       if (!name || !address) return -1;
 
-       return cw;      
+       ae = g_new0(address_entry, 1);
+
+       g_return_val_if_fail(ae != NULL, -1);
+
+       ae->name    = g_strdup(name);
+       ae->address = g_strdup(address);                
+
+       g_address_list = g_list_prepend(g_address_list, ae);
+
+       add_address1(name, ae);
+       add_address1(address, ae);
+       add_address1(alias, ae);
+
+       return 0;
 }
 
 /**
- * Destroy completion window.
- * \param cw Window to destroy.
+ * Read address book, creating all entries in the completion index.
+ */ 
+static void read_address_book(void) {  
+       addrindex_load_completion( add_address );
+       g_address_list = g_list_reverse(g_address_list);
+       g_completion_list = g_list_reverse(g_completion_list);
+}
+
+/**
+ * Test whether there is a completion pending.
+ * \return <code>TRUE</code> if pending.
  */
-static void addrcompl_destroy_window( CompletionWindow *cw ) {
-       /* Remove idler function... or application may not terminate */
-       if( _completionIdleID_ != 0 ) {
-               gtk_idle_remove( _completionIdleID_ );
-               _completionIdleID_ = 0;
-       }
+static gboolean is_completion_pending(void)
+{
+       /* check if completion pending, i.e. we might satisfy a request for the next
+        * or previous address */
+        return g_completion_count;
+}
 
-       /* Now destroy window */        
-       if( cw ) {
-               /* Clear references to widgets */
-               cw->entry = NULL;
-               cw->clist = NULL;
+/**
+ * Clear the completion cache.
+ */
+static void clear_completion_cache(void)
+{
+       if (is_completion_pending()) {
+               if (g_completion_prefix)
+                       g_free(g_completion_prefix);
 
-               /* Free objects */
-               if( cw->window ) {
-                       gtk_widget_hide( cw->window );
-                       gtk_widget_destroy( cw->window );
+               if (g_completion_addresses) {
+                       g_slist_free(g_completion_addresses);
+                       g_completion_addresses = NULL;
                }
-               cw->window = NULL;
+
+               g_completion_count = g_completion_next = 0;
        }
 }
 
 /**
- * Free up completion window.
- * \param cw Window to free.
+ * Prepare completion index. This function should be called prior to attempting
+ * address completion.
+ * \return The number of addresses in the completion list.
  */
-static void addrcompl_free_window( CompletionWindow *cw ) {
-       /* printf( "addrcompl_free_window...\n" ); */
-       if( cw ) {
-               addrcompl_destroy_window( cw );
-
-               g_free( cw->searchTerm );
-               cw->searchTerm = NULL;
-
-               /* Clear references */          
-               cw->listCount = 0;
-
-               /* Free object */               
-               g_free( cw );
+gint start_address_completion(void)
+{
+       clear_completion_cache();
+       if (!g_ref_count) {
+               init_all();
+               /* open the address book */
+               read_address_book();
+               /* merge the completion entry list into g_completion */
+               if (g_completion_list)
+                       g_completion_add_items(g_completion, g_completion_list);
        }
-       /* printf( "addrcompl_free_window...done\n" ); */
+       g_ref_count++;
+       debug_print("start_address_completion ref count %d\n", g_ref_count);
+
+       return g_list_length(g_completion_list);
 }
 
 /**
@@ -170,7 +269,7 @@ static void addrcompl_free_window( CompletionWindow *cw ) {
  * \param entry Address entry field.
  * \param start_pos Address of start position of address.
  * \return Possible address.
- */ 
+ */
 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
 {
        const gchar *edit_text;
@@ -238,10 +337,10 @@ static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
        g_free(wtext);
 
        return str;
-}
+} 
 
 /**
- * Replace text in entry field with specified address.
+ * Replace an incompleted address with a completed one.
  * \param entry     Address entry field.
  * \param newtext   New text.
  * \param start_pos Insertion point in entry field.
@@ -256,6 +355,350 @@ static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
        gtk_editable_set_position(GTK_EDITABLE(entry), -1);
 }
 
+/**
+ * Attempt to complete an address, and returns the number of addresses found.
+ * Use <code>get_complete_address()</code> to get an entry from the index.
+ *
+ * \param  str Search string to find.
+ * \return Zero if no match was found, otherwise the number of addresses; the
+ *         original prefix (search string) will appear at index 0. 
+ */
+guint complete_address(const gchar *str)
+{
+       GList *result;
+       gchar *d;
+       guint  count, cpl;
+       completion_entry *ce;
+
+       g_return_val_if_fail(str != NULL, 0);
+
+       Xstrdup_a(d, str, return 0);
+
+       clear_completion_cache();
+       g_completion_prefix = g_strdup(str);
+
+       /* g_completion is case sensitive */
+       g_strdown(d);
+       result = g_completion_complete(g_completion, d, NULL);
+
+       count = g_list_length(result);
+       if (count) {
+               /* create list with unique addresses  */
+               for (cpl = 0, result = g_list_first(result);
+                    result != NULL;
+                    result = g_list_next(result)) {
+                       ce = (completion_entry *)(result->data);
+                       if (NULL == g_slist_find(g_completion_addresses,
+                                                ce->ref)) {
+                               cpl++;
+                               g_completion_addresses =
+                                       g_slist_append(g_completion_addresses,
+                                                      ce->ref);
+                       }
+               }
+               count = cpl + 1;        /* index 0 is the original prefix */
+               g_completion_next = 1;  /* we start at the first completed one */
+       } else {
+               g_free(g_completion_prefix);
+               g_completion_prefix = NULL;
+       }
+
+       g_completion_count = count;
+       return count;
+}
+
+/**
+ * Return a complete address from the index.
+ * \param index Index of entry that was found (by the previous call to
+ *              <code>complete_address()</code>
+ * \return Completed address string; this should be freed when done.
+ */
+gchar *get_complete_address(gint index)
+{
+       const address_entry *p;
+       gchar *address = NULL;
+
+       if (index < g_completion_count) {
+               if (index == 0)
+                       address = g_strdup(g_completion_prefix);
+               else {
+                       /* get something from the unique addresses */
+                       p = (address_entry *)g_slist_nth_data
+                               (g_completion_addresses, index - 1);
+                       if (p != NULL) {
+                               if (!p->name || p->name[0] == '\0')
+                                       address = g_strdup_printf(p->address);
+                               else if (strchr_with_skip_quote(p->name, '"', ','))
+                                       address = g_strdup_printf
+                                               ("\"%s\" <%s>", p->name, p->address);
+                               else
+                                       address = g_strdup_printf
+                                               ("%s <%s>", p->name, p->address);
+                       }
+               }
+       }
+
+       return address;
+}
+
+/**
+ * Return the next complete address match from the completion index.
+ * \return Completed address string; this should be freed when done.
+ */
+static gchar *get_next_complete_address(void)
+{
+       if (is_completion_pending()) {
+               gchar *res;
+
+               res = get_complete_address(g_completion_next);
+               g_completion_next += 1;
+               if (g_completion_next >= g_completion_count)
+                       g_completion_next = 0;
+
+               return res;
+       } else
+               return NULL;
+}
+
+/**
+ * Return a count of the completed matches in the completion index.
+ * \return Number of matched entries.
+ */
+static guint get_completion_count(void)
+{
+       if (is_completion_pending())
+               return g_completion_count;
+       else
+               return 0;
+}
+
+/**
+ * Invalidate address completion index. This function should be called whenever
+ * the address book changes. This forces data to be read into the completion
+ * data.
+ * \return Number of entries in index.
+ */
+gint invalidate_address_completion(void)
+{
+       if (g_ref_count) {
+               /* simply the same as start_address_completion() */
+               debug_print("Invalidation request for address completion\n");
+               free_all();
+               init_all();
+               read_address_book();
+               g_completion_add_items(g_completion, g_completion_list);
+               clear_completion_cache();
+       }
+
+       return g_list_length(g_completion_list);
+}
+
+/**
+ * Finished with completion index. This function should be called after
+ * matching addresses.
+ * \return Reference count.
+ */
+gint end_address_completion(void)
+{
+       clear_completion_cache();
+
+       if (0 == --g_ref_count)
+               free_all();
+
+       debug_print("end_address_completion ref count %d\n", g_ref_count);
+
+       return g_ref_count; 
+}
+
+/*
+ * Define the structure of the completion window.
+ */
+typedef struct _CompletionWindow CompletionWindow;
+struct _CompletionWindow {
+       gint      listCount;
+       gchar     *searchTerm;
+       GtkWidget *window;
+       GtkWidget *entry;
+       GtkWidget *clist;
+};
+
+/**
+ * Completion window.
+ */
+static CompletionWindow *_compWindow_ = NULL;
+
+/**
+ * Mutex to protect callback from multiple threads.
+ */
+static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
+
+/**
+ * Completion queue list.
+ */
+static GList *_displayQueue_ = NULL;
+
+/**
+ * Current query ID.
+ */
+static gint _queryID_ = 0;
+
+/**
+ * Completion idle ID.
+ */
+static guint _completionIdleID_ = 0;
+
+/*
+ * address completion entry ui. the ui (completion list was inspired by galeon's
+ * auto completion list). remaining things powered by sylpheed's completion engine.
+ */
+
+#define ENTRY_DATA_TAB_HOOK    "tab_hook"      /* used to lookup entry */
+
+static void address_completion_mainwindow_set_focus    (GtkWindow   *window,
+                                                        GtkWidget   *widget,
+                                                        gpointer     data);
+static gboolean address_completion_entry_key_pressed   (GtkEntry    *entry,
+                                                        GdkEventKey *ev,
+                                                        gpointer     data);
+static gboolean address_completion_complete_address_in_entry
+                                                       (GtkEntry    *entry,
+                                                        gboolean     next);
+static void address_completion_create_completion_window        (GtkEntry    *entry);
+
+static void completion_window_select_row(GtkCList       *clist,
+                                        gint             row,
+                                        gint             col,
+                                        GdkEvent        *event,
+                                        CompletionWindow *compWin );
+
+static gboolean completion_window_button_press
+                                       (GtkWidget       *widget,
+                                        GdkEventButton  *event,
+                                        CompletionWindow *compWin );
+
+static gboolean completion_window_key_press
+                                       (GtkWidget       *widget,
+                                        GdkEventKey     *event,
+                                        CompletionWindow *compWin );
+static void address_completion_create_completion_window( GtkEntry *entry_ );
+
+/**
+ * Create a completion window object.
+ * \return Initialized completion window.
+ */
+static CompletionWindow *addrcompl_create_window( void ) {
+       CompletionWindow *cw;
+
+       cw = g_new0( CompletionWindow, 1 );
+       cw->listCount = 0;
+       cw->searchTerm = NULL;
+       cw->window = NULL;
+       cw->entry = NULL;
+       cw->clist = NULL;
+
+       return cw;      
+}
+
+/**
+ * Destroy completion window.
+ * \param cw Window to destroy.
+ */
+static void addrcompl_destroy_window( CompletionWindow *cw ) {
+       /* Stop all searches currently in progress */
+       addrindex_stop_search( _queryID_ );
+
+       /* Remove idler function... or application may not terminate */
+       if( _completionIdleID_ != 0 ) {
+               gtk_idle_remove( _completionIdleID_ );
+               _completionIdleID_ = 0;
+       }
+
+       /* Now destroy window */        
+       if( cw ) {
+               /* Clear references to widgets */
+               cw->entry = NULL;
+               cw->clist = NULL;
+
+               /* Free objects */
+               if( cw->window ) {
+                       gtk_widget_hide( cw->window );
+                       gtk_widget_destroy( cw->window );
+               }
+               cw->window = NULL;
+       }
+}
+
+/**
+ * Free up completion window.
+ * \param cw Window to free.
+ */
+static void addrcompl_free_window( CompletionWindow *cw ) {
+       if( cw ) {
+               addrcompl_destroy_window( cw );
+
+               g_free( cw->searchTerm );
+               cw->searchTerm = NULL;
+
+               /* Clear references */          
+               cw->listCount = 0;
+
+               /* Free object */               
+               g_free( cw );
+       }
+}
+
+/**
+ * Advance selection to previous/next item in list.
+ * \param clist   List to process.
+ * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
+ *                previous entry.
+ */
+static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
+{
+       int row;
+
+       g_return_if_fail(clist != NULL);
+
+       if( clist->selection ) {
+               row = GPOINTER_TO_INT(clist->selection->data);
+               row = forward ? ( row + 1 ) : ( row - 1 );
+               gtk_clist_freeze(clist);
+               gtk_clist_select_row(clist, row, 0);
+               gtk_clist_thaw(clist);
+       }
+}
+
+#if 0
+/* completion_window_accept_selection() - accepts the current selection in the
+ * clist, and destroys the window */
+static void completion_window_accept_selection(GtkWidget **window,
+                                              GtkCList *clist,
+                                              GtkEntry *entry)
+{
+       gchar *address = NULL, *text = NULL;
+       gint   cursor_pos, row;
+
+       g_return_if_fail(window != NULL);
+       g_return_if_fail(*window != NULL);
+       g_return_if_fail(clist != NULL);
+       g_return_if_fail(entry != NULL);
+       g_return_if_fail(clist->selection != NULL);
+
+       /* FIXME: I believe it's acceptable to access the selection member directly  */
+       row = GPOINTER_TO_INT(clist->selection->data);
+
+       /* we just need the cursor position */
+       address = get_address_from_edit(entry, &cursor_pos);
+       g_free(address);
+       gtk_clist_get_text(clist, row, 0, &text);
+       replace_address_in_edit(entry, text, cursor_pos);
+
+       clear_completion_cache();
+       gtk_widget_destroy(*window);
+       *window = NULL;
+}
+#endif
+
 /**
  * Resize window to accommodate maximum number of address entries.
  * \param cw Completion window.
@@ -281,7 +724,7 @@ static void addrcompl_resize_window( CompletionWindow *cw ) {
 
 /**
  * Add an address the completion window address list.
- * \param cw Completion window.
+ * \param cw      Completion window.
  * \param address Address to add.
  */
 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
@@ -296,7 +739,11 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
        addrcompl_resize_window( cw );
        gtk_grab_add( cw->window );
 
-       if( cw->listCount == 2 ) {
+       if( cw->listCount == 1 ) {
+               /* Select first row for now */
+               gtk_clist_select_row( GTK_CLIST(cw->clist), 0, 0);
+       }
+       else if( cw->listCount == 2 ) {
                /* Move off first row */
                gtk_clist_select_row( GTK_CLIST(cw->clist), 1, 0);
        }
@@ -313,19 +760,15 @@ static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
 static gboolean addrcompl_idle( gpointer data ) {
        GList *node;
        gchar *address;
-       CompletionWindow *cw;
 
        /* Process all entries in display queue */
        pthread_mutex_lock( & _completionMutex_ );
        if( _displayQueue_ ) {
-               cw = data;
                node = _displayQueue_;
                while( node ) {
-                       ItemEMail *email = node->data;
-                       /* printf( "email/address ::%s::\n", email->address ); */
-                       address = addritem_format_email( email );
+                       address = node->data;
                        /* printf( "address ::: %s :::\n", address ); */
-                       addrcompl_add_entry( cw, address );
+                       addrcompl_add_entry( _compWindow_, address );
                        g_free( address );
                        node = g_list_next( node );
                }
@@ -340,43 +783,42 @@ static gboolean addrcompl_idle( gpointer data ) {
 /**
  * Callback entry point. The background thread (if any) appends the address
  * list to the display queue.
+ * \param sender     Sender of query.
  * \param queryID    Query ID of search request.
  * \param listEMail  List of zero of more email objects that met search
  *                   criteria.
- * \param target     Target object to received data.
+ * \param data       Query data.
  */
-static gint addrcompl_callback(
-       gint queryID, GList *listEMail, gpointer target )
+static gint addrcompl_callback_entry(
+       gpointer sender, gint queryID, GList *listEMail, gpointer data )
 {
        GList *node;
        gchar *address;
 
+       /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
        pthread_mutex_lock( & _completionMutex_ );
        if( queryID == _queryID_ ) {
                /* Append contents to end of display queue */
                node = listEMail;
                while( node ) {
                        ItemEMail *email = node->data;
-                       if( target ) {
-                               /*
-                               printf( "\temail/address ::%s::\n", email->address );
-                               */
-                               _displayQueue_ = g_list_append( _displayQueue_, email );
-                       }
+
+                       address = addritem_format_email( email );
+                       /* printf( "\temail/address ::%s::\n", address ); */
+                       _displayQueue_ = g_list_append( _displayQueue_, address );
                        node = g_list_next( node );
                }
        }
+       g_list_free( listEMail );
        pthread_mutex_unlock( & _completionMutex_ );
+
+       return 0;
 }
 
 /**
- * Start the search.
+ * Clear the display queue.
  */
-static void addrcompl_start_search( void ) {
-       gchar *searchTerm;
-
-       searchTerm = g_strdup( _compWindow_->searchTerm );
-
+static void addrcompl_clear_queue( void ) {
        /* Clear out display queue */
        pthread_mutex_lock( & _completionMutex_ );
 
@@ -384,69 +826,58 @@ static void addrcompl_start_search( void ) {
        _displayQueue_ = NULL;
 
        pthread_mutex_unlock( & _completionMutex_ );
+}
 
-       /* Setup the search */
-       _queryID_ = addressbook_setup_search(
-               searchTerm, _compWindow_, addrcompl_callback );
-       g_free( searchTerm );
-
-       /* Sit back and wait until something happens */
-       _completionIdleID_ =
-               gtk_idle_add( ( GtkFunction ) addrcompl_idle, _compWindow_ );
-       addressbook_start_search( _queryID_ );
+/**
+ * Add a single address entry into the display queue.
+ * \param address Address to append.
+ */
+static void addrcompl_add_queue( gchar *address ) {
+       pthread_mutex_lock( & _completionMutex_ );
+       _displayQueue_ = g_list_append( _displayQueue_, address );
+       pthread_mutex_unlock( & _completionMutex_ );
 }
 
-/*
- * address completion entry ui. the ui (completion list was inspired by galeon's
- * auto completion list). remaining things powered by sylpheed's completion engine.
+/**
+ * Load list with entries from local completion index.
  */
+static void addrcompl_load_local( void ) {
+       guint count = 0;
 
-#define ENTRY_DATA_TAB_HOOK    "tab_hook"      /* used to lookup entry */
-
-static gboolean address_completion_entry_key_pressed   (GtkEntry    *entry,
-                                                        GdkEventKey *ev,
-                                                        gpointer     data);
-static gboolean address_completion_complete_address_in_entry
-                                                       (GtkEntry    *entry);
+       for (count = 0; count < get_completion_count(); count++) {
+               gchar *address;
 
-static void address_completion_create_completion_window        (GtkEntry    *entry);
+               address = get_complete_address( count );
+               /* printf( "\taddress ::%s::\n", address ); */
 
-static void completion_window_select_row(GtkCList       *clist,
-                                        gint             row,
-                                        gint             col,
-                                        GdkEvent        *event,
-                                        CompletionWindow *compWin );
+               /* Append contents to end of display queue */
+               addrcompl_add_queue( address );
+       }
+}
 
-static gboolean completion_window_button_press
-                                       (GtkWidget       *widget,
-                                        GdkEventButton  *event,
-                                        CompletionWindow *compWin );
+/**
+ * Start the search.
+ */
+static void addrcompl_start_search( void ) {
+       gchar *searchTerm;
 
-static gboolean completion_window_key_press
-                                       (GtkWidget       *widget,
-                                        GdkEventKey     *event,
-                                        CompletionWindow *compWin );
+       searchTerm = g_strdup( _compWindow_->searchTerm );
 
+       /* Setup the search */
+       _queryID_ = addrindex_setup_search(
+               searchTerm, NULL, addrcompl_callback_entry );
+       g_free( searchTerm );
+       /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
 
-/**
- * Advance selection to previous/next item in list.
- * \param clist   List to process.
- * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
- *                previous entry.
- */
-static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
-{
-       int row;
+       /* Load local stuff */
+       addrcompl_load_local();
 
-       g_return_if_fail(clist != NULL);
+       /* Sit back and wait until something happens */
+       _completionIdleID_ =
+               gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
+       /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
 
-       if( clist->selection ) {
-               row = GPOINTER_TO_INT(clist->selection->data);
-               row = forward ? ( row + 1 ) : ( row - 1 );
-               gtk_clist_freeze(clist);
-               gtk_clist_select_row(clist, row, 0);
-               gtk_clist_thaw(clist);
-       }
+       addrindex_start_search( _queryID_ );
 }
 
 /**
@@ -484,7 +915,95 @@ static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
        if( parent ) {
                gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_FORWARD );
        }
+}
+
+/**
+ * Start address completion. Should be called when creating the main window
+ * containing address completion entries.
+ * \param mainwindow Main window.
+ */
+void address_completion_start(GtkWidget *mainwindow)
+{
+       start_address_completion();
+
+       /* register focus change hook */
+       gtk_signal_connect(GTK_OBJECT(mainwindow), "set_focus",
+                          GTK_SIGNAL_FUNC(address_completion_mainwindow_set_focus),
+                          mainwindow);
+}
+
+/**
+ * Need unique data to make unregistering signal handler possible for the auto
+ * completed entry.
+ */
+#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
+
+/**
+ * Register specified entry widget for address completion.
+ * \param entry Address entry field.
+ */
+void address_completion_register_entry(GtkEntry *entry)
+{
+       g_return_if_fail(entry != NULL);
+       g_return_if_fail(GTK_IS_ENTRY(entry));
+
+       /* add hooked property */
+       gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
+
+       /* add keypress event */
+       gtk_signal_connect_full(GTK_OBJECT(entry), "key_press_event",
+                               GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
+                               NULL,
+                               COMPLETION_UNIQUE_DATA,
+                               NULL,
+                               0,
+                               0); /* magic */
+}
+
+/**
+ * Unregister specified entry widget from address completion operations.
+ * \param entry Address entry field.
+ */
+void address_completion_unregister_entry(GtkEntry *entry)
+{
+       GtkObject *entry_obj;
+
+       g_return_if_fail(entry != NULL);
+       g_return_if_fail(GTK_IS_ENTRY(entry));
+
+       entry_obj = gtk_object_get_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
+       g_return_if_fail(entry_obj);
+       g_return_if_fail(entry_obj == GTK_OBJECT(entry));
+
+       /* has the hooked property? */
+       gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
+
+       /* remove the hook */
+       gtk_signal_disconnect_by_func(GTK_OBJECT(entry), 
+               GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
+               COMPLETION_UNIQUE_DATA);
+}
+
+/**
+ * End address completion. Should be called when main window with address
+ * completion entries terminates. NOTE: this function assumes that it is
+ * called upon destruction of the window.
+ * \param mainwindow Main window.
+ */
+void address_completion_end(GtkWidget *mainwindow)
+{
+       /* if address_completion_end() is really called on closing the window,
+        * we don't need to unregister the set_focus_cb */
+       end_address_completion();
+}
 
+/* if focus changes to another entry, then clear completion cache */
+static void address_completion_mainwindow_set_focus(GtkWindow *window,
+                                                   GtkWidget *widget,
+                                                   gpointer   data)
+{
+       if (widget)
+               clear_completion_cache();
 }
 
 /**
@@ -499,7 +1018,9 @@ static gboolean address_completion_entry_key_pressed(GtkEntry    *entry,
                                                     gpointer     data)
 {
        if (ev->keyval == GDK_Tab) {
-               if (address_completion_complete_address_in_entry(entry)) {
+               addrcompl_clear_queue();
+
+               if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
                        /* route a void character to the default handler */
                        /* this is a dirty hack; we're actually changing a key
                         * reported by the system. */
@@ -508,23 +1029,40 @@ static gboolean address_completion_entry_key_pressed(GtkEntry    *entry,
                        gtk_signal_emit_stop_by_name(GTK_OBJECT(entry),
                                                     "key_press_event");
 
-                       /* printf( "entry_key_pressed::create window\n" ); */
-                       address_completion_create_completion_window( entry );
-                       /* printf( "entry_key_pressed::create window...done\n" ); */
+                       /* Create window */                     
+                       address_completion_create_completion_window(entry);
+
+                       /* Start remote queries */
                        addrcompl_start_search();
                }
-       }
+               else {
+                       /* old behaviour */
+               }
+       } else if (ev->keyval == GDK_Shift_L
+               || ev->keyval == GDK_Shift_R
+               || ev->keyval == GDK_Control_L
+               || ev->keyval == GDK_Control_R
+               || ev->keyval == GDK_Caps_Lock
+               || ev->keyval == GDK_Shift_Lock
+               || ev->keyval == GDK_Meta_L
+               || ev->keyval == GDK_Meta_R
+               || ev->keyval == GDK_Alt_L
+               || ev->keyval == GDK_Alt_R) {
+               /* these buttons should not clear the cache... */
+       } else
+               clear_completion_cache();
+
        return TRUE;
 }
-
 /**
  * Initialize search term for address completion.
  * \param entry Address entry field.
  */
-static gboolean address_completion_complete_address_in_entry( GtkEntry *entry )
+static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
+                                                            gboolean  next)
 {
-       gint cursor_pos;
-       gchar *searchTerm;
+       gint ncount, cursor_pos;
+       gchar *searchTerm, *new = NULL;
 
        g_return_val_if_fail(entry != NULL, FALSE);
 
@@ -532,15 +1070,27 @@ static gboolean address_completion_complete_address_in_entry( GtkEntry *entry )
 
        /* get an address component from the cursor */
        searchTerm = get_address_from_edit( entry, &cursor_pos );
-       if (! searchTerm ) return FALSE;
+       if( ! searchTerm ) return FALSE;
+       /* printf( "search for :::%s:::\n", searchTerm ); */
 
        /* Clear any existing search */
        if( _compWindow_->searchTerm ) {
                g_free( _compWindow_->searchTerm );
        }
+       _compWindow_->searchTerm = g_strdup( searchTerm );
+
+       /* Perform search on local completion index */
+       ncount = complete_address( searchTerm );
+       if( 0 < ncount ) {
+               new = get_next_complete_address();
+               g_free( new );
+       }
 
-       /* Replace with new search term */
-       _compWindow_->searchTerm = searchTerm;
+       /* Make sure that drop-down appears uniform! */
+       if( ncount == 0 ) {
+               addrcompl_add_queue( g_strdup( searchTerm ) );
+       }
+       g_free( searchTerm );
 
        return TRUE;
 }
@@ -556,7 +1106,6 @@ static void address_completion_create_completion_window( GtkEntry *entry_ )
        GtkRequisition r;
        GtkWidget *window;
        GtkWidget *entry = GTK_WIDGET(entry_);
-       gchar *searchTerm;
 
        /* Create new window and list */
        window = gtk_window_new(GTK_WINDOW_POPUP);
@@ -611,13 +1160,6 @@ static void address_completion_create_completion_window( GtkEntry *entry_ )
        /* this gets rid of the irritating focus rectangle that doesn't
         * follow the selection */
        GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
-
-       /* Add first entry into address list */
-       searchTerm = g_strdup( _compWindow_->searchTerm );
-       addrcompl_add_entry( _compWindow_, searchTerm );
-       gtk_clist_select_row(GTK_CLIST(clist), 0, 0);
-
-       g_free( searchTerm );
 }
 
 /**
@@ -646,6 +1188,7 @@ static void completion_window_select_row(GtkCList *clist, gint row, gint col,
        /* User selected address by releasing the mouse in drop-down list*/
        completion_window_apply_selection( clist, entry );
 
+       clear_completion_cache();
        addrcompl_destroy_window( _compWindow_ );
 }
 
@@ -661,7 +1204,6 @@ static void completion_window_select_row(GtkCList *clist, gint row, gint col,
 static gboolean completion_window_button_press(GtkWidget *widget,
                                               GdkEventButton *event,
                                               CompletionWindow *compWin )
-
 {
        GtkWidget *event_widget, *entry;
        gchar *searchTerm;
@@ -694,6 +1236,7 @@ static gboolean completion_window_button_press(GtkWidget *widget,
                replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
        }
 
+       clear_completion_cache();
        addrcompl_destroy_window( _compWindow_ );
 
        return TRUE;
@@ -714,6 +1257,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
        gchar *searchTerm;
        gint cursor_pos;
        GtkWidget *clist;
+       GtkWidget *parent;
 
        g_return_val_if_fail(compWin != NULL, FALSE);
 
@@ -731,6 +1275,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
                return FALSE;
        }               
 
+#if 0  
        /* also make tab / shift tab go to next previous completion entry. we're
         * changing the key value */
        if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
@@ -743,6 +1288,39 @@ static gboolean completion_window_key_press(GtkWidget *widget,
                        event->keyval == GDK_Down ? TRUE : FALSE);
                return FALSE;
        }
+#endif
+
+       /* make tab move to next field */
+       if( event->keyval == GDK_Tab ) {
+               /* Reference to parent */
+               parent = GTK_WIDGET(entry)->parent;
+
+               /* Discard the window */
+               clear_completion_cache();
+               addrcompl_destroy_window( _compWindow_ );
+
+               /* Move focus to next widget */
+               if( parent ) {
+                       gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_FORWARD );
+               }
+               return FALSE;
+       }
+
+       /* make backtab move to previous field */
+       if( event->keyval == GDK_ISO_Left_Tab ) {
+               /* Reference to parent */
+               parent = GTK_WIDGET(entry)->parent;
+
+               /* Discard the window */
+               clear_completion_cache();
+               addrcompl_destroy_window( _compWindow_ );
+
+               /* Move focus to previous widget */
+               if( parent ) {
+                       gtk_container_focus( GTK_CONTAINER(parent), GTK_DIR_TAB_BACKWARD );
+               }
+               return FALSE;
+       }
 
        /* look for presses that accept the selection */
        if (event->keyval == GDK_Return || event->keyval == GDK_space) {
@@ -753,6 +1331,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
                        GTK_CLIST(clist), GTK_ENTRY(entry) );
 
                /* Discard the window */
+               clear_completion_cache();
                addrcompl_destroy_window( _compWindow_ );
                return FALSE;
        }
@@ -788,6 +1367,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
        gtk_widget_event(entry, (GdkEvent *)&tmp_event);
 
        /* and close the completion window */
+       clear_completion_cache();
        addrcompl_destroy_window( _compWindow_ );
 
        return TRUE;
@@ -798,6 +1378,7 @@ static gboolean completion_window_key_press(GtkWidget *widget,
  * Publically accessible functions.
  * ============================================================================
  */
+
 /**
  * Setup completion object.
  */
@@ -826,100 +1407,6 @@ void addrcompl_teardown( void ) {
        /* printf( "addrcompl_teardown...done\n" ); */
 }
 
-/**
- * Start address completion operation.
- */
-gint start_address_completion(void)
-{
-       if( ! _compWindow_ ) {
-               addrcompl_initialize();
-       }
-       addressbook_read_all();
-       return 0;
-}
-
-/**
- * Terminate addess completion.
- */
-gint end_address_completion(void)
-{
-       return 0;
-}
-
-/**
- * Start address completion. Should be called when creating the main window
- * containing address completion entries. This originally cleared the cache.
- * Function no longer required?
- */
-void address_completion_start(GtkWidget *mainwindow)
-{
-       start_address_completion();
-}
-
-/**
- * Need unique data to make unregistering signal handler possible for the auto
- * completed entry.
- */
-#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
-
-/**
- * Register specified entry widget for address completion.
- * \param entry Address entry field.
- */
-void address_completion_register_entry(GtkEntry *entry)
-{
-       g_return_if_fail(entry != NULL);
-       g_return_if_fail(GTK_IS_ENTRY(entry));
-
-       /* add hooked property */
-       gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
-
-       /* add keypress event */
-       gtk_signal_connect_full(GTK_OBJECT(entry), "key_press_event",
-                               GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
-                               NULL,
-                               COMPLETION_UNIQUE_DATA,
-                               NULL,
-                               0,
-                               0); /* magic */
-}
-
-/**
- * Unregister specified entry widget from address completion operations.
- * \param entry Address entry field.
- */
-void address_completion_unregister_entry(GtkEntry *entry)
-{
-       GtkObject *entry_obj;
-
-       g_return_if_fail(entry != NULL);
-       g_return_if_fail(GTK_IS_ENTRY(entry));
-
-       entry_obj = gtk_object_get_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
-       g_return_if_fail(entry_obj);
-       g_return_if_fail(entry_obj == GTK_OBJECT(entry));
-
-       /* has the hooked property? */
-       gtk_object_set_data(GTK_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
-
-       /* remove the hook */
-       gtk_signal_disconnect_by_func(GTK_OBJECT(entry), 
-               GTK_SIGNAL_FUNC(address_completion_entry_key_pressed),
-               COMPLETION_UNIQUE_DATA);
-}
-
-/**
- * End address completion. Should be called when main window with address
- * completion entries terminates. NOTE: this function assumes that it is
- * called upon destruction of the window.
- */
-void address_completion_end(GtkWidget *mainwindow)
-{
-       /* if address_completion_end() is really called on closing the window,
-        * we don't need to unregister the set_focus_cb */
-       end_address_completion();
-}
-
 /*
  * End of Source.
  */