sync with sylpheed 0.7.0cvs38
[claws.git] / src / addr_compl.c
index a5c9f2dd9dd1e1cff6aaff2c2d0df0cde3b1c606..6cfafc5c2a8e2cfd0e4628106db67b674101b68a 100644 (file)
@@ -46,9 +46,6 @@
 #include "addressbook.h"
 #include "main.h"
 
-#define LOG_MESSAGE \
-       debug_mode == 0 ? (debug_mode == debug_mode) : debug_print 
-
 /* How it works:
  *
  * The address book is read into memory. We set up an address list
@@ -205,7 +202,7 @@ gint start_address_completion(void)
                        g_completion_add_items(g_completion, g_completion_list);
        }
        g_ref_count++;
-       LOG_MESSAGE("start_address_completion ref count %d\n", g_ref_count);
+       debug_print("start_address_completion ref count %d\n", g_ref_count);
 
        return g_list_length(g_completion_list);
 }
@@ -222,8 +219,15 @@ gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
        wchar_t *wtext;
        wchar_t *wp;
        wchar_t rfc_mail_sep;
+       wchar_t quote;
+       wchar_t lt;
+       gboolean in_quote = FALSE;
        gchar *str;
 
+       if (mbtowc(&rfc_mail_sep, ",", 1) < 0) return NULL;
+       if (mbtowc(&quote, "\"", 1) < 0) return NULL;
+       if (mbtowc(&lt, "<", 1) < 0) return NULL;
+
        edit_text = gtk_entry_get_text(entry);
        if (edit_text == NULL) return NULL;
 
@@ -232,14 +236,13 @@ gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
 
        cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
 
-       if (mbtowc(&rfc_mail_sep, ",", 1) < 0) {
-               g_free(wtext);
-               return NULL;
-       }
-
        /* scan for a separator. doesn't matter if walk points at null byte. */
-       for (wp = wtext + cur_pos; wp > wtext && *wp != rfc_mail_sep; wp--)
-               ;
+       for (wp = wtext + cur_pos; wp > wtext; wp--) {
+               if (!in_quote && *wp == rfc_mail_sep)
+                       break;
+               if (*wp == quote)
+                       in_quote ^= TRUE;
+       }
 
        /* have something valid */
        if (wcslen(wp) == 0) {
@@ -247,7 +250,8 @@ gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
                return NULL;
        }
 
-#define IS_VALID_CHAR(x)       (iswalnum(x) || ((x) > 0x7f))
+#define IS_VALID_CHAR(x) \
+       (iswalnum(x) || (x) == quote || (x) == lt || ((x) > 0x7f))
 
        /* now scan back until we hit a valid character */
        for (; *wp && !IS_VALID_CHAR(*wp); wp++)
@@ -334,22 +338,29 @@ guint complete_address(const gchar *str)
 gchar *get_complete_address(gint index)
 {
        const address_entry *p;
-       
+       gchar *address = NULL;
+
        if (index < g_completion_count) {
                if (index == 0)
-                       return g_strdup(g_completion_prefix);
+                       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)
-                               return NULL;
-                       else
-                               return g_strdup_printf
-                                       ("%s <%s>", p->name, p->address);
+                       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);
+                       }
                }
-       } else
-               return NULL;
+       }
+
+       return address;
 }
 
 gchar *get_next_complete_address(void)
@@ -422,7 +433,7 @@ gint invalidate_address_completion(void)
 {
        if (g_ref_count) {
                /* simply the same as start_address_completion() */
-               LOG_MESSAGE("Invalidation request for address completion\n");
+               debug_print("Invalidation request for address completion\n");
                free_all();
                init_all();
                read_address_book();
@@ -440,7 +451,7 @@ gint end_address_completion(void)
        if (0 == --g_ref_count)
                free_all();
 
-       LOG_MESSAGE("end_address_completion ref count %d\n", g_ref_count);
+       debug_print("end_address_completion ref count %d\n", g_ref_count);
 
        return g_ref_count; 
 }