From cf6873b653ee8fdc2b4c86167d2af499d35489da Mon Sep 17 00:00:00 2001 From: hiro Date: Thu, 12 Apr 2012 05:55:03 +0000 Subject: also use first and last name for address completion. git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@3039 ee746299-78ed-0310-b773-934348b2243d --- src/addr_compl.c | 96 ++++++++++++++++++++++--------------------------------- src/addressbook.c | 29 ++++++++++++++--- src/addressbook.h | 6 +++- 3 files changed, 69 insertions(+), 62 deletions(-) (limited to 'src') diff --git a/src/addr_compl.c b/src/addr_compl.c index 90a73428..b7c69950 100644 --- a/src/addr_compl.c +++ b/src/addr_compl.c @@ -109,35 +109,10 @@ static gchar *completion_func(gpointer data) return ((completion_entry *)data)->string; } -static gint compare_func(const gchar *s1, const gchar *s2, gsize n) -{ - const gchar *p = s2; - gint ret = 1; - - g_return_val_if_fail(s1 != NULL && s2 != NULL, 1); - - while (*p) { - ret = strncmp(s1, p, n); - if (ret == 0) - return ret; - while (*p && *p != '-' && *p != '.' && *p != '@' && - !g_ascii_isspace(*p)) - p++; - /* don't compare the domain part */ - if (*p == '@') - break; - while (*p == '-' || *p == '.' || g_ascii_isspace(*p)) - p++; - } - - return ret; -} - static void init_all(void) { completion = g_completion_new(completion_func); g_return_if_fail(completion != NULL); - g_completion_set_compare(completion, compare_func); } static void free_all(void) @@ -186,24 +161,34 @@ static gint address_entry_find_func(gconstpointer a, gconstpointer b) return 0; } +static void add_completion_entry(const gchar *str, address_entry *ae) +{ + completion_entry *ce; + + if (!str || *str == '\0') + return; + if (!ae) + return; + + ce = g_new0(completion_entry, 1); + /* GCompletion list is case sensitive */ + ce->string = g_utf8_strdown(str, -1); + ce->ref = ae; + completion_list = g_list_append(completion_list, ce); +} + /* add_address() - adds address to the completion list. this function looks * complicated, but it's only allocation checks. */ -static gint add_address(const gchar *name, const gchar *address, const gchar *nickname) +static gint add_address(const gchar *name, const gchar *firstname, const gchar *lastname, const gchar *nickname, const gchar *address) { - address_entry *ae; - completion_entry *name_ce = NULL; - completion_entry *nick_ce = NULL; - completion_entry *addr_ce; - GList *found; + address_entry *ae; + GList *found; - if (!address) return -1; + if (!address || *address == '\0') + return -1; - if (name && *name) - name_ce = g_new0(completion_entry, 1); - if (nickname && *nickname) - nick_ce = g_new0(completion_entry, 1); - addr_ce = g_new0(completion_entry, 1); + /* debugg_print("add_address: [%s] [%s] [%s] [%s] [%s]\n", name, firstname, lastname, nickname, address); */ ae = g_new0(address_entry, 1); ae->name = g_strdup(name ? name : ""); @@ -216,25 +201,22 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *ni ae = (address_entry *)found->data; } else address_list = g_list_append(address_list, ae); - - /* GCompletion list is case sensitive */ - if (name_ce) - name_ce->string = g_utf8_strdown(name, -1); - if (nick_ce) - nick_ce->string = g_utf8_strdown(nickname, -1); - addr_ce->string = g_utf8_strdown(address, -1); - - if (name_ce) - name_ce->ref = ae; - if (nick_ce) - nick_ce->ref = ae; - addr_ce->ref = ae; - - if (name_ce) - completion_list = g_list_append(completion_list, name_ce); - if (nick_ce) - completion_list = g_list_append(completion_list, nick_ce); - completion_list = g_list_append(completion_list, addr_ce); + + if (name) { + const gchar *p = name; + + while (*p != '\0') { + add_completion_entry(p, ae); + while (*p && *p != '-' && *p != '.' && !g_ascii_isspace(*p)) + p++; + while (*p == '-' || *p == '.' || g_ascii_isspace(*p)) + p++; + } + } + add_completion_entry(firstname, ae); + add_completion_entry(lastname, ae); + add_completion_entry(nickname, ae); + add_completion_entry(address, ae); return 0; } @@ -242,7 +224,7 @@ static gint add_address(const gchar *name, const gchar *address, const gchar *ni /* read_address_book() */ static void read_address_book(void) { - addressbook_load_completion( add_address ); + addressbook_load_completion_full( add_address ); } /* start_address_completion() - returns the number of addresses diff --git a/src/addressbook.c b/src/addressbook.c index bcd2b793..3e8d8bae 100644 --- a/src/addressbook.c +++ b/src/addressbook.c @@ -1,6 +1,6 @@ /* * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client - * Copyright (C) 1999-2011 Hiroyuki Yamamoto + * Copyright (C) 1999-2012 Hiroyuki Yamamoto * * 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 @@ -4521,7 +4521,7 @@ gboolean addressbook_add_contact_autoreg(const gchar *name, const gchar *address * to be loaded. * Return: TRUE if data loaded, FALSE if address index not loaded. */ -gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const gchar *, const gchar *)) +gboolean addressbook_load_completion_full(AddressBookCompletionFunc func) { /* AddressInterface *interface; */ AddressDataSource *ds; @@ -4529,6 +4529,7 @@ gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const g GList *listP, *nodeP; GList *nodeM; gchar *sName, *sAddress, *sAlias, *sNickName; + gchar *sFirstName, *sLastName; debug_print( "addressbook_load_completion\n" ); @@ -4560,6 +4561,8 @@ gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const g if (sNickName) sName = sNickName; } + sFirstName = person->firstName; + sLastName = person->lastName; /* Process each E-Mail address */ while( nodeM ) { @@ -4569,9 +4572,9 @@ gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const g if( sAddress && *sAddress != '\0' ) { sAlias = ADDRITEM_NAME(email); if( sAlias && *sAlias != '\0' ) { - ( callBackFunc ) ( sName, sAddress, sAlias ); + func( sName, sFirstName, sLastName, sAlias, sAddress ); } else { - ( callBackFunc ) ( sName, sAddress, sNickName ); + func( sName, sFirstName, sLastName, sNickName, sAddress ); } } nodeM = g_list_next( nodeM ); @@ -4590,6 +4593,24 @@ gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const g return TRUE; } +static gint (*real_func)(const gchar *, const gchar *, const gchar *); + +static gint wrapper_func(const gchar *name, const gchar *firstname, const gchar *lastname, const gchar *nickname, const gchar *address) +{ + return real_func(name, address, nickname); +} + +gboolean addressbook_load_completion(gint (*callBackFunc)(const gchar *, const gchar *, const gchar *)) +{ + gboolean ret; + + real_func = callBackFunc; + ret = addressbook_load_completion_full(wrapper_func); + real_func = NULL; + + return ret; +} + /* ********************************************************************** * Address Import. * *********************************************************************** diff --git a/src/addressbook.h b/src/addressbook.h index b1c61026..ee0b7446 100644 --- a/src/addressbook.h +++ b/src/addressbook.h @@ -1,6 +1,6 @@ /* * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client - * Copyright (C) 1999,2000 Hiroyuki Yamamoto + * Copyright (C) 1999-2012 Hiroyuki Yamamoto * * 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 @@ -25,6 +25,8 @@ #include "compose.h" +typedef gint (*AddressBookCompletionFunc) (const gchar *name, const gchar *firstname, const gchar *lastname, const gchar *nickname, const gchar *address); + void addressbook_open (Compose *target); void addressbook_set_target_compose (Compose *target); Compose *addressbook_get_target_compose (void); @@ -47,6 +49,8 @@ gboolean addressbook_add_contact_autoreg(const gchar *name, const gchar *address, const gchar *remarks); +gboolean addressbook_load_completion_full + (AddressBookCompletionFunc func); gboolean addressbook_load_completion ( gint (*callBackFunc) ( const gchar *, const gchar *, const gchar * ) ); gboolean addressbook_has_address (const gchar *address); -- cgit v1.2.3