replace deprecated gtk_window_set_policy
[claws.git] / src / addr_compl.c
index cf094ffe718fb35d7c5bb9355c5ea20c578506c9..b29c87849538b9bab69b17175bb6e26882ac21ec 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
@@ -40,7 +40,7 @@
 #  include <wctype.h>
 #endif
 
-#include "addressbook.h"
+#include "addrindex.h"
 #include "addr_compl.h"
 #include "utils.h"
 #include <pthread.h>
  * any of those words).
  */ 
        
-/**
- * Reference to address index.
- */
-static AddressIndex *_addressIndex_ = NULL;
-
 /**
  * address_entry - structure which refers to the original address entry in the
  * address book .
@@ -184,7 +179,8 @@ static void add_address1(const char *str, address_entry *ae)
  * \return <code>0</code> if entry appended successfully, or <code>-1</code>
  *         if failure.
  */
-static gint add_address(const gchar *name, const gchar *address, const gchar *alias)
+static gint add_address(const gchar *name, const gchar *address, 
+                       const gchar *nick, const gchar *alias)
 {
        address_entry    *ae;
 
@@ -201,7 +197,13 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *al
 
        add_address1(name, ae);
        add_address1(address, ae);
-       add_address1(alias, ae);
+       
+       if (nick != NULL)
+               add_address1(nick, ae);
+       
+       if ( alias != NULL ) {
+               add_address1(alias, ae);
+       }
 
        return 0;
 }
@@ -210,7 +212,7 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *al
  * Read address book, creating all entries in the completion index.
  */ 
 static void read_address_book(void) {  
-       addrindex_load_completion( _addressIndex_, add_address );
+       addrindex_load_completion( add_address );
        g_address_list = g_list_reverse(g_address_list);
        g_completion_list = g_list_reverse(g_completion_list);
 }
@@ -594,6 +596,9 @@ static CompletionWindow *addrcompl_create_window( void ) {
  * \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_ );
@@ -697,16 +702,16 @@ static void addrcompl_resize_window( CompletionWindow *cw ) {
        /* Get current geometry of window */
        gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
 
-       gtk_widget_size_request( cw->clist, &r );
-       gtk_widget_set_size_request( cw->window, width, r.height );
+       gtk_widget_hide_all( cw->window );
        gtk_widget_show_all( cw->window );
        gtk_widget_size_request( cw->clist, &r );
 
        /* Adjust window height to available screen space */
        if( ( y + r.height ) > gdk_screen_height() ) {
-               gtk_window_set_policy( GTK_WINDOW( cw->window ), TRUE, FALSE, FALSE );
+               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);
 }
 
 /**
@@ -747,17 +752,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 ) {
                        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 );
                }
@@ -772,35 +775,34 @@ 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::queryID=%d\n", queryID ); */
+       /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
        pthread_mutex_lock( & _completionMutex_ );
-       if( target ) {
-               if( queryID == _queryID_ ) {
-                       /* Append contents to end of display queue */
-                       node = listEMail;
-                       while( node ) {
-                               ItemEMail *email = node->data;
-
-                               address = addritem_format_email( email );
-                               /* printf( "\temail/address ::%s::\n", address ); */
-                               _displayQueue_ = g_list_append( _displayQueue_, address );
-                               node = g_list_next( node );
-                       }
+       if( queryID == _queryID_ ) {
+               /* Append contents to end of display queue */
+               node = listEMail;
+               while( node ) {
+                       ItemEMail *email = node->data;
+
+                       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_ );
-       /* printf( "addrcompl_callback...done\n" ); */
 
        return 0;
 }
@@ -830,9 +832,8 @@ static void addrcompl_add_queue( gchar *address ) {
 
 /**
  * Load list with entries from local completion index.
- * \param cw Completion window.
  */
-static void addrcompl_load_local( CompletionWindow *cw ) {
+static void addrcompl_load_local( void ) {
        guint count = 0;
 
        for (count = 0; count < get_completion_count(); count++) {
@@ -856,19 +857,19 @@ static void addrcompl_start_search( void ) {
 
        /* Setup the search */
        _queryID_ = addrindex_setup_search(
-               _addressIndex_, searchTerm, _compWindow_, addrcompl_callback );
+               searchTerm, NULL, addrcompl_callback_entry );
        g_free( searchTerm );
        /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
 
        /* Load local stuff */
-       addrcompl_load_local( _compWindow_ );
+       addrcompl_load_local();
 
        /* Sit back and wait until something happens */
        _completionIdleID_ =
-               gtk_idle_add( ( GtkFunction ) addrcompl_idle, _compWindow_ );
+               gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
        /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
 
-       addrindex_start_search( _addressIndex_, _queryID_ );
+       addrindex_start_search( _queryID_ );
 }
 
 /**
@@ -943,7 +944,7 @@ void address_completion_register_entry(GtkEntry *entry)
 
        /* add keypress event */
        g_signal_connect_closure
-               (GTK_OBJECT(entry), "key_press_event",
+               (G_OBJECT(entry), "key_press_event",
                 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
                                COMPLETION_UNIQUE_DATA,
                                NULL),
@@ -963,7 +964,7 @@ void address_completion_unregister_entry(GtkEntry *entry)
 
        entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
        g_return_if_fail(entry_obj);
-       g_return_if_fail(entry_obj == GTK_OBJECT(entry));
+       g_return_if_fail(entry_obj == G_OBJECT(entry));
 
        /* has the hooked property? */
        g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
@@ -1079,8 +1080,21 @@ static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
                g_free( new );
        }
 
+       /* Select the address if there is only one match */
+       if (ncount == 2) {
+               /* Display selected address in entry field */           
+               gchar *addr = get_complete_address(1);
+
+               if (addr) {
+                       replace_address_in_edit(entry, addr, cursor_pos);
+                       g_free(addr);
+               }
+
+               /* Discard the window */
+               clear_completion_cache();
+       }
        /* Make sure that drop-down appears uniform! */
-       if( ncount == 0 ) {
+       else if( ncount == 0 ) {
                addrcompl_add_queue( g_strdup( searchTerm ) );
        }
        g_free( searchTerm );
@@ -1374,12 +1388,8 @@ static gboolean completion_window_key_press(GtkWidget *widget,
 
 /**
  * Setup completion object.
- * \param addrIndex Address index object.
  */
-void addrcompl_initialize( AddressIndex *addrIndex ) {
-       g_return_if_fail( addrIndex != NULL );
-       _addressIndex_ = addrIndex;
-
+void addrcompl_initialize( void ) {
        /* printf( "addrcompl_initialize...\n" ); */
        if( ! _compWindow_ ) {
                _compWindow_ = addrcompl_create_window();
@@ -1401,7 +1411,6 @@ void addrcompl_teardown( void ) {
        }
        _displayQueue_ = NULL;
        _completionIdleID_ = 0;
-       _addressIndex_ = NULL;
        /* printf( "addrcompl_teardown...done\n" ); */
 }