sync with latest 0.9.0pre1
[claws.git] / src / addr_compl.c
index 00549890a03f2b3b76fc94e381d3242722739d4e..f11f4b929c4192ac7dec82c3db7f7f514e20231b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  *
- * Copyright (c) 2000-2001 by Alfons Hoogervorst <alfons@proteus.demon.nl>
+ * Copyright (c) 2000-2003 by Alfons Hoogervorst <alfons@proteus.demon.nl>
  *
  * 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 "xml.h"
+#include "addressbook.h"
 #include "addr_compl.h"
 #include "utils.h"
-#include "addressbook.h"
-#include "main.h"
 
 /* How it works:
  *
@@ -144,7 +142,7 @@ static void free_all(void)
        g_completion = NULL;
 }
 
-static gint add_address1(const char *str, address_entry *ae)
+static void add_address1(const char *str, address_entry *ae)
 {
        completion_entry *ce1;
        ce1 = g_new0(completion_entry, 1),
@@ -153,7 +151,7 @@ static gint add_address1(const char *str, address_entry *ae)
        g_strdown(ce1->string);
        ce1->ref = ae;
 
-       g_completion_list = g_list_append(g_completion_list, ce1);
+       g_completion_list = g_list_prepend(g_completion_list, ce1);
 }
 
 /* add_address() - adds address to the completion list. this function looks
@@ -165,8 +163,6 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *al
 
        if (!name || !address) return -1;
 
-       debug_print( "completion: add_address: %s - %s\n", name, address );
-
        ae = g_new0(address_entry, 1);
 
        g_return_val_if_fail(ae != NULL, -1);
@@ -174,7 +170,7 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *al
        ae->name    = g_strdup(name);
        ae->address = g_strdup(address);                
 
-       g_address_list    = g_list_append(g_address_list,    ae);
+       g_address_list    = g_list_prepend(g_address_list,    ae);
 
        add_address1(name, ae);
        add_address1(address, ae);
@@ -187,6 +183,24 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *al
  */ 
 static void read_address_book(void) {  
        addressbook_load_completion( add_address );
+       g_address_list = g_list_reverse(g_address_list);
+       g_completion_list = g_list_reverse(g_completion_list);
+}
+
+/* should clear up anything after complete_address() */
+void clear_completion_cache(void)
+{
+       if (is_completion_pending()) {
+               if (g_completion_prefix)
+                       g_free(g_completion_prefix);
+
+               if (g_completion_addresses) {
+                       g_slist_free(g_completion_addresses);
+                       g_completion_addresses = NULL;
+               }
+
+               g_completion_count = g_completion_next = 0;
+       }
 }
 
 /* start_address_completion() - returns the number of addresses 
@@ -288,6 +302,7 @@ gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
 void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
                             gint start_pos)
 {
+       if (!newtext) return;
        gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
        gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
                                 &start_pos);
@@ -361,7 +376,8 @@ gchar *get_complete_address(gint index)
                        if (p != NULL) {
                                if (!p->name || p->name[0] == '\0')
                                        address = g_strdup_printf(p->address);
-                               else if (strchr_with_skip_quote(p->name, '"', ','))
+                               else if (p->name[0] != '"' &&
+                                        strpbrk(p->name, ",.[]<>") != NULL)
                                        address = g_strdup_printf
                                                ("\"%s\" <%s>", p->name, p->address);
                                else
@@ -414,22 +430,6 @@ guint get_completion_count(void)
                return 0;
 }
 
-/* should clear up anything after complete_address() */
-void clear_completion_cache(void)
-{
-       if (is_completion_pending()) {
-               if (g_completion_prefix)
-                       g_free(g_completion_prefix);
-
-               if (g_completion_addresses) {
-                       g_slist_free(g_completion_addresses);
-                       g_completion_addresses = NULL;
-               }
-
-               g_completion_count = g_completion_next = 0;
-       }
-}
-
 gboolean is_completion_pending(void)
 {
        /* check if completion pending, i.e. we might satisfy a request for the next
@@ -869,6 +869,7 @@ static gboolean completion_window_button_press(GtkWidget *widget,
                prefix = get_complete_address(0);
                g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
                replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
+               g_free(prefix);
        }
 
        clear_completion_cache();