2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2012 by Alfons Hoogervorst & The Claws Mail Team.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
34 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
39 #include "addr_compl.h"
42 #include "prefs_common.h"
46 #include "stock_pixmap.h"
49 #ifndef USE_ALT_ADDRBOOK
50 #include "addrindex.h"
52 #include "addressbook-dbus.h"
56 *\brief For the GtkListStore
69 * The address book is read into memory. We set up an address list
70 * containing all address book entries. Next we make the completion
71 * list, which contains all the completable strings, and store a
72 * reference to the address entry it belongs to.
73 * After calling the g_completion_complete(), we get a reference
74 * to a valid email address.
76 * Completion is very simplified. We never complete on another prefix,
77 * i.e. we neglect the next smallest possible prefix for the current
78 * completion cache. This is simply done so we might break up the
79 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
80 * something like alfons, proteus, demon, nl; and then completing on
81 * any of those words).
85 * completion_entry - structure used to complete addresses, with a reference
86 * the the real address information.
90 gchar *string; /* string to complete */
91 address_entry *ref; /* address the string belongs to */
94 /*******************************************************************************/
96 static gint g_ref_count; /* list ref count */
97 static GList *g_completion_list = NULL; /* list of strings to be checked */
98 static GList *g_address_list = NULL; /* address storage */
99 static GCompletion *g_completion; /* completion object */
101 static GHashTable *_groupAddresses_ = NULL;
102 static gboolean _allowCommas_ = TRUE;
104 /* To allow for continuing completion we have to keep track of the state
105 * using the following variables. No need to create a context object. */
107 static gint g_completion_count; /* nr of addresses incl. the prefix */
108 static gint g_completion_next; /* next prev address */
109 static GSList *g_completion_addresses; /* unique addresses found in the
111 static gchar *g_completion_prefix; /* last prefix. (this is cached here
112 * because the prefix passed to g_completion
113 * is g_utf8_strdown()'ed */
115 static gchar *completion_folder_path = NULL;
117 /*******************************************************************************/
120 * Define the structure of the completion window.
122 typedef struct _CompletionWindow CompletionWindow;
123 struct _CompletionWindow {
128 GtkWidget *list_view;
130 gboolean in_mouse; /*!< mouse press pending... */
131 gboolean destroying; /*!< destruction in progress */
134 static GtkListStore *addr_compl_create_store (void);
136 static GtkWidget *addr_compl_list_view_create (CompletionWindow *window);
138 static void addr_compl_create_list_view_columns (GtkWidget *list_view);
140 static gboolean list_view_button_press (GtkWidget *widget,
141 GdkEventButton *event,
142 CompletionWindow *window);
144 static gboolean list_view_button_release (GtkWidget *widget,
145 GdkEventButton *event,
146 CompletionWindow *window);
148 static gboolean addr_compl_selected (GtkTreeSelection *selector,
151 gboolean currently_selected,
154 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window);
157 * Function used by GTK to find the string data to be used for completion.
158 * \param data Pointer to data being processed.
160 static gchar *completion_func(gpointer data)
162 cm_return_val_if_fail(data != NULL, NULL);
164 return ((completion_entry *)data)->string;
167 static gint addr_completion_func(const gchar *needle, const gchar *haystack,
170 if (needle == NULL || haystack == NULL)
173 return (strcasestr(haystack, needle) != NULL ? 0 : 1);
177 * Function used by GTK to compare elements for sorting
178 * name match beginning > name match after space > email address
179 * match beginning and full match before @ > email adress
180 * match beginning. Otherwise match position in string.
181 * \param a first element in comparsion
182 * \param b second element in comparison
184 static gint weight_addr_match(const address_entry* addr)
186 gint n_weight = addr->name ? strlen(addr->name): 0;
187 gint a_weight = addr->address ? strlen(addr->address) : n_weight;
191 match = strcasestr(addr->name, g_completion_prefix);
194 if (match == addr->name)
196 else if (match > addr->name && *(match - 1) == ' ')
199 n_weight = match - addr->name;
203 match = strcasestr(addr->address, g_completion_prefix);
205 if (match == addr->address)
208 a_weight = match - addr->address;
210 if (strlen(match) > strlen(g_completion_prefix)
211 && *(match + strlen(g_completion_prefix)) == '@')
216 if (n_weight == -4 && a_weight < 0)
219 return MIN(a_weight, n_weight);
222 static gint addr_comparison_func(gconstpointer a, gconstpointer b)
224 const address_entry* a_ref = (const address_entry*)a;
225 const address_entry* b_ref = (const address_entry*)b;
226 gint a_weight = weight_addr_match(a_ref);
227 gint b_weight = weight_addr_match(b_ref);
230 if (a_weight < b_weight)
232 else if (a_weight > b_weight)
235 cmp = strcmp(a_ref->name, b_ref->name);
236 return cmp ? cmp : g_strcmp0(a_ref->address, b_ref->address);
241 * Initialize all completion index data.
243 static void init_all(void)
245 g_completion = g_completion_new(completion_func);
246 cm_return_if_fail(g_completion != NULL);
250 * set the compare function (default is strncmp)
252 static void set_match_any_part(const gboolean any_part)
254 if (any_part && prefs_common.address_search_wildcard)
255 g_completion_set_compare(g_completion, addr_completion_func);
257 g_completion_set_compare(g_completion, strncmp);
260 static void free_all_addresses(void)
265 walk = g_address_list;
266 for (; walk != NULL; walk = g_list_next(walk)) {
267 address_entry *ae = (address_entry *) walk->data;
270 g_list_free(ae->grp_emails);
273 g_list_free(g_address_list);
274 g_address_list = NULL;
275 if (_groupAddresses_)
276 g_hash_table_destroy(_groupAddresses_);
277 _groupAddresses_ = NULL;
280 static void clear_completion_cache(void);
281 static void free_completion_list(void)
284 if (!g_completion_list)
287 clear_completion_cache();
289 g_completion_clear_items(g_completion);
291 walk = g_list_first(g_completion_list);
292 for (; walk != NULL; walk = g_list_next(walk)) {
293 completion_entry *ce = (completion_entry *) walk->data;
297 g_list_free(g_completion_list);
298 g_completion_list = NULL;
301 * Free up all completion index data.
303 static void free_all(void)
305 free_completion_list();
306 free_all_addresses();
307 g_completion_free(g_completion);
312 * Append specified address entry to the index.
313 * \param str Index string value.
314 * \param ae Entry containing address data.
316 void addr_compl_add_address1(const char *str, address_entry *ae)
318 completion_entry *ce1;
319 ce1 = g_new0(completion_entry, 1),
320 /* GCompletion list is case sensitive */
321 ce1->string = g_utf8_strdown(str, -1);
324 g_completion_list = g_list_prepend(g_completion_list, ce1);
328 * Adds address to the completion list. This function looks complicated, but
329 * it's only allocation checks. Each value will be included in the index.
330 * \param name Recipient name.
331 * \param address EMail address.
332 * \param alias Alias to append.
333 * \param grp_emails the emails in case of a group. List should be freed later,
334 * but not its strings
335 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
338 static gint add_address(const gchar *name, const gchar *address,
339 const gchar *nick, const gchar *alias, GList *grp_emails)
343 if (!address && !grp_emails)
349 ae = g_new0(address_entry, 1);
350 cm_return_val_if_fail(ae != NULL, -1);
352 ae->name = g_strdup(name);
353 ae->address = g_strdup(address);
354 ae->grp_emails = grp_emails;
355 g_address_list = g_list_prepend(g_address_list, ae);
357 addr_compl_add_address1(name, ae);
359 if (address != NULL && *address != '\0')
360 addr_compl_add_address1(address, ae);
362 if (nick != NULL && *nick != '\0')
363 addr_compl_add_address1(nick, ae);
365 if (alias != NULL && *alias != '\0')
366 addr_compl_add_address1(alias, ae);
372 * Read address book, creating all entries in the completion index.
374 static void read_address_book(gchar *folderpath) {
375 free_all_addresses();
376 free_completion_list();
378 #ifndef USE_ALT_ADDRBOOK
379 addrindex_load_completion( add_address, folderpath );
381 GError* error = NULL;
383 addrcompl_initialize();
384 if (! addrindex_dbus_load_completion(add_address, &error)) {
385 g_warning("Failed to populate address completion list");
390 /* plugins may hook in here to modify/extend the completion list */
392 hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, &g_address_list);
395 g_address_list = g_list_reverse(g_address_list);
396 g_completion_list = g_list_reverse(g_completion_list);
397 /* merge the completion entry list into g_completion */
398 if (g_completion_list) {
399 g_completion_add_items(g_completion, g_completion_list);
400 if (debug_get_mode())
401 debug_print("read %d items in %s\n",
402 g_list_length(g_completion_list),
403 folderpath?folderpath:"(null)");
408 * Test whether there is a completion pending.
409 * \return <code>TRUE</code> if pending.
411 static gboolean is_completion_pending(void)
413 /* check if completion pending, i.e. we might satisfy a request for the next
414 * or previous address */
415 return g_completion_count;
419 * Clear the completion cache.
421 static void clear_completion_cache(void)
423 if (is_completion_pending()) {
424 g_free(g_completion_prefix);
426 if (g_completion_addresses) {
427 g_slist_free(g_completion_addresses);
428 g_completion_addresses = NULL;
431 g_completion_count = g_completion_next = 0;
436 * Prepare completion index. This function should be called prior to attempting
437 * address completion.
438 * \return The number of addresses in the completion list.
440 guint start_address_completion(gchar *folderpath)
442 gboolean different_book = FALSE;
443 clear_completion_cache();
445 if (strcmp2(completion_folder_path,folderpath))
446 different_book = TRUE;
448 g_free(completion_folder_path);
449 if (folderpath != NULL)
450 completion_folder_path = g_strdup(folderpath);
452 completion_folder_path = NULL;
456 /* open the address book */
457 read_address_book(folderpath);
458 } else if (different_book)
459 read_address_book(folderpath);
462 debug_print("start_address_completion(%s) ref count %d\n",
463 folderpath?folderpath:"(null)", g_ref_count);
465 return g_list_length(g_completion_list);
469 * Retrieve a possible address (or a part) from an entry box. To make life
470 * easier, we only look at the last valid address component; address
471 * completion only works at the last string component in the entry box.
473 * \param entry Address entry field.
474 * \param start_pos Address of start position of address.
475 * \return Possible address.
477 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
479 const gchar *edit_text, *p;
481 gboolean in_quote = FALSE;
482 gboolean in_bracket = FALSE;
485 edit_text = gtk_entry_get_text(entry);
486 if (edit_text == NULL) return NULL;
488 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
490 /* scan for a separator. doesn't matter if walk points at null byte. */
491 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
493 p = g_utf8_prev_char(p)) {
496 } else if (!in_quote) {
497 if (!in_bracket && *p == ',') {
499 } else if (*p == '<')
506 /* have something valid */
507 if (g_utf8_strlen(p, -1) == 0)
510 #define IS_VALID_CHAR(x) \
511 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
513 /* now scan back until we hit a valid character */
514 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
519 if (g_utf8_strlen(p, -1) == 0)
522 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
529 static gchar *get_complete_address_from_name_email(const gchar *name, const gchar *email)
531 gchar *address = NULL;
532 if (!name || name[0] == '\0')
533 address = g_strdup_printf("<%s>", email);
534 else if (strchr_with_skip_quote(name, '"', ','))
535 address = g_strdup_printf
536 ("\"%s\" <%s>", name, email);
538 address = g_strdup_printf
539 ("%s <%s>", name, email);
544 * Replace an incompleted address with a completed one.
545 * \param entry Address entry field.
546 * \param newtext New text.
547 * \param start_pos Insertion point in entry field.
549 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
550 gint start_pos, gboolean is_group, GList *grp_emails)
552 if (!newtext) return;
553 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
555 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
558 gchar *addresses = NULL;
559 GList *cur = grp_emails;
560 for (; cur; cur = cur->next) {
562 ItemEMail *email = (ItemEMail *)cur->data;
563 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
565 gchar *addr = get_complete_address_from_name_email(
566 ADDRITEM_NAME(person), email->address);
568 tmp = g_strdup_printf("%s, %s", addresses, addr);
570 tmp = g_strdup_printf("%s", addr);
575 gtk_editable_insert_text(GTK_EDITABLE(entry), addresses, strlen(addresses),
579 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
583 * Attempt to complete an address, and returns the number of addresses found.
584 * Use <code>get_complete_address()</code> to get an entry from the index.
586 * \param str Search string to find.
587 * \return Zero if no match was found, otherwise the number of addresses; the
588 * original prefix (search string) will appear at index 0.
590 guint complete_address(const gchar *str)
592 GList *result = NULL;
596 completion_entry *ce = NULL;
598 cm_return_val_if_fail(str != NULL, 0);
600 /* g_completion is case sensitive */
601 d = g_utf8_strdown(str, -1);
603 clear_completion_cache();
604 g_completion_prefix = g_strdup(str);
606 result = g_completion_complete(g_completion, d, NULL);
608 count = g_list_length(result);
610 /* create list with unique addresses */
611 for (cpl = 0, result = g_list_first(result);
613 result = g_list_next(result)) {
614 ce = (completion_entry *)(result->data);
615 if (NULL == g_slist_find(g_completion_addresses,
618 g_completion_addresses =
619 g_slist_append(g_completion_addresses,
623 count = cpl + 1; /* index 0 is the original prefix */
624 g_completion_next = 1; /* we start at the first completed one */
625 if (prefs_common.address_search_wildcard)
626 g_completion_addresses = g_slist_sort(g_completion_addresses,
627 addr_comparison_func);
629 g_free(g_completion_prefix);
630 g_completion_prefix = NULL;
633 g_completion_count = count;
641 * complete_matches_found() returns the number of matched addresses according
642 * to the completion mechanism. Unlike complete_address(), the returned value
643 * doesn't count str itself. If there's no match, it returns 0.
644 * To get a list of completion matches, see complete_address() instead.
646 guint complete_matches_found(const gchar *str)
648 GList *result = NULL;
651 cm_return_val_if_fail(str != NULL, 0);
653 /* g_completion is case sensitive */
654 d = g_utf8_strdown(str, -1);
656 clear_completion_cache();
657 g_completion_prefix = g_strdup(str);
659 result = g_completion_complete(g_completion, d, NULL);
661 g_free(g_completion_prefix);
664 return g_list_length(result);
668 * Return a complete address from the index.
669 * \param index Index of entry that was found (by the previous call to
670 * <code>complete_address()</code>
671 * \return Completed address string; this should be freed when done.
673 gchar *get_complete_address(gint index)
675 const address_entry *p;
676 gchar *address = NULL;
678 if (index < g_completion_count) {
680 address = g_strdup(g_completion_prefix);
682 /* get something from the unique addresses */
683 p = (address_entry *)g_slist_nth_data
684 (g_completion_addresses, index - 1);
685 if (p != NULL && p->address != NULL) {
686 address = get_complete_address_from_name_email(p->name, p->address);
687 } else if (p != NULL && p->address == NULL && p->name != NULL) {
689 address = g_strdup_printf("%s (%s) <!--___group___-->", p->name, _("Group"));
690 if (!_groupAddresses_) {
691 _groupAddresses_ = g_hash_table_new(NULL, g_direct_equal);
693 if (!g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)))) {
694 g_hash_table_insert(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)), p->grp_emails);
705 * Return the next complete address match from the completion index.
706 * \return Completed address string; this should be freed when done.
708 static gchar *get_next_complete_address(void)
710 if (is_completion_pending()) {
713 res = get_complete_address(g_completion_next);
714 g_completion_next += 1;
715 if (g_completion_next >= g_completion_count)
716 g_completion_next = 0;
724 * Return a count of the completed matches in the completion index.
725 * \return Number of matched entries.
727 static guint get_completion_count(void)
729 if (is_completion_pending())
730 return g_completion_count;
736 * Invalidate address completion index. This function should be called whenever
737 * the address book changes. This forces data to be read into the completion
739 * \return Number of entries in index.
741 gint invalidate_address_completion(void)
744 /* simply the same as start_address_completion() */
745 debug_print("Invalidation request for address completion\n");
746 read_address_book(completion_folder_path);
747 clear_completion_cache();
750 return g_list_length(g_completion_list);
754 * Finished with completion index. This function should be called after
755 * matching addresses.
756 * \return Reference count.
758 gint end_address_completion(void)
760 gboolean different_folder = FALSE;
761 clear_completion_cache();
763 /* reset the folderpath to NULL */
764 if (completion_folder_path) {
765 g_free(completion_folder_path);
766 completion_folder_path = NULL;
767 different_folder = TRUE;
769 if (0 == --g_ref_count)
772 debug_print("end_address_completion ref count %d\n", g_ref_count);
773 if (g_ref_count && different_folder) {
774 debug_print("still ref'd, different folder\n");
775 invalidate_address_completion();
784 static CompletionWindow *_compWindow_ = NULL;
787 * Mutex to protect callback from multiple threads.
789 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
792 * Completion queue list.
794 static GList *_displayQueue_ = NULL;
798 static gint _queryID_ = 0;
801 * Completion idle ID.
803 static guint _completionIdleID_ = 0;
806 * address completion entry ui. the ui (completion list was inspired by galeon's
807 * auto completion list). remaining things powered by claws's completion engine.
810 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
811 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
813 static void address_completion_mainwindow_set_focus (GtkWindow *window,
816 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
819 static gboolean address_completion_complete_address_in_entry
822 static void address_completion_create_completion_window (GtkEntry *entry);
824 static gboolean completion_window_button_press
826 GdkEventButton *event,
827 CompletionWindow *compWin );
829 static gboolean completion_window_key_press
832 CompletionWindow *compWin );
833 static void address_completion_create_completion_window( GtkEntry *entry_ );
836 * Create a completion window object.
837 * \return Initialized completion window.
839 static CompletionWindow *addrcompl_create_window( void ) {
840 CompletionWindow *cw;
842 cw = g_new0( CompletionWindow, 1 );
844 cw->searchTerm = NULL;
847 cw->list_view = NULL;
848 cw->in_mouse = FALSE;
849 cw->destroying = FALSE;
855 * Destroy completion window.
856 * \param cw Window to destroy.
858 static void addrcompl_destroy_window( CompletionWindow *cw ) {
859 /* Stop all searches currently in progress */
860 #ifndef USE_ALT_ADDRBOOK
861 addrindex_stop_search( _queryID_ );
863 /* Remove idler function... or application may not terminate */
864 if( _completionIdleID_ != 0 ) {
865 g_source_remove( _completionIdleID_ );
866 _completionIdleID_ = 0;
869 /* Now destroy window */
871 /* Clear references to widgets */
873 cw->list_view = NULL;
877 gtk_widget_hide( cw->window );
878 gtk_widget_destroy( cw->window );
881 cw->destroying = FALSE;
882 cw->in_mouse = FALSE;
888 * Free up completion window.
889 * \param cw Window to free.
891 static void addrcompl_free_window( CompletionWindow *cw ) {
893 addrcompl_destroy_window( cw );
895 g_free( cw->searchTerm );
896 cw->searchTerm = NULL;
898 /* Clear references */
907 * Advance selection to previous/next item in list.
908 * \param list_view List to process.
909 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
912 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
914 GtkTreeSelection *selection;
918 cm_return_if_fail(list_view != NULL);
920 selection = gtk_tree_view_get_selection(list_view);
921 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
925 forward = gtk_tree_model_iter_next(model, &iter);
927 gtk_tree_selection_select_iter(selection, &iter);
931 prev = gtk_tree_model_get_path(model, &iter);
935 if (gtk_tree_path_prev(prev))
936 gtk_tree_selection_select_path(selection, prev);
938 gtk_tree_path_free(prev);
943 * Resize window to accommodate maximum number of address entries.
944 * \param cw Completion window.
946 static void addrcompl_resize_window( CompletionWindow *cw ) {
948 gint x, y, width, height, depth;
950 /* Get current geometry of window */
951 #if !GTK_CHECK_VERSION(3, 0, 0)
952 gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height, &depth );
954 gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height );
957 gtk_widget_queue_resize_no_redraw(cw->list_view);
958 gtk_widget_size_request( cw->list_view, &r );
960 /* Adjust window height to available screen space */
961 if( y + r.height > gdk_screen_height())
962 r.height = gdk_screen_height() - y;
964 gtk_widget_set_size_request(cw->window, width, r.height);
966 gdk_pointer_grab(gtk_widget_get_window(cw->window), TRUE,
967 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
968 GDK_BUTTON_RELEASE_MASK,
969 NULL, NULL, GDK_CURRENT_TIME);
970 gdk_keyboard_grab(gtk_widget_get_window(cw->window), FALSE, GDK_CURRENT_TIME);
971 gtk_grab_add(cw->window);
975 static GdkPixbuf *group_pixbuf = NULL;
976 static GdkPixbuf *email_pixbuf = NULL;
979 * Add an address the completion window address list.
980 * \param cw Completion window.
981 * \param address Address to add.
983 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
986 GtkTreeSelection *selection;
987 gboolean is_group = FALSE;
988 GList *grp_emails = NULL;
989 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
993 stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
994 g_object_ref(G_OBJECT(group_pixbuf));
997 stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
998 g_object_ref(G_OBJECT(email_pixbuf));
1000 /* g_print( "\t\tAdding :%s\n", address ); */
1001 if (strstr(address, " <!--___group___-->")) {
1003 if (_groupAddresses_)
1004 grp_emails = g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)));
1005 *(strstr(address, " <!--___group___-->")) = '\0';
1006 pixbuf = group_pixbuf;
1007 } else if (strchr(address, '@') && strchr(address, '<') &&
1008 strchr(address, '>')) {
1009 pixbuf = email_pixbuf;
1013 if (is_group && !_allowCommas_)
1015 gtk_list_store_append(store, &iter);
1016 gtk_list_store_set(store, &iter,
1017 ADDR_COMPL_ICON, pixbuf,
1018 ADDR_COMPL_ADDRESS, address,
1019 ADDR_COMPL_ISGROUP, is_group,
1020 ADDR_COMPL_GROUPLIST, grp_emails,
1025 addrcompl_resize_window( cw );
1026 gtk_grab_add( cw->window );
1028 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
1029 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1032 if (cw->listCount == 1) {
1033 /* Select first row for now */
1034 gtk_tree_selection_select_iter(selection, &iter);
1036 #ifndef GENERIC_UMPC
1037 else if (cw->listCount == 2) {
1038 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)) {
1039 /* Move off first row */
1040 gtk_tree_selection_select_iter(selection, &iter);
1046 void addrcompl_reflect_prefs_pixmap_theme(void) {
1048 g_object_unref(G_OBJECT(group_pixbuf));
1049 group_pixbuf = NULL;
1052 g_object_unref(G_OBJECT(email_pixbuf));
1053 email_pixbuf = NULL;
1058 * Completion idle function. This function is called by the main (UI) thread
1059 * during UI idle time while an address search is in progress. Items from the
1060 * display queue are processed and appended to the address list.
1062 * \param data Target completion window to receive email addresses.
1063 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
1065 static gboolean addrcompl_idle( gpointer data ) {
1069 /* Process all entries in display queue */
1070 pthread_mutex_lock( & _completionMutex_ );
1071 if( _displayQueue_ ) {
1072 node = _displayQueue_;
1074 address = node->data;
1075 /* g_print( "address ::: %s :::\n", address ); */
1076 addrcompl_add_entry( _compWindow_, address );
1078 node = g_list_next( node );
1080 g_list_free( _displayQueue_ );
1081 _displayQueue_ = NULL;
1083 pthread_mutex_unlock( & _completionMutex_ );
1090 * Callback entry point. The background thread (if any) appends the address
1091 * list to the display queue.
1092 * \param sender Sender of query.
1093 * \param queryID Query ID of search request.
1094 * \param listEMail List of zero of more email objects that met search
1096 * \param data Query data.
1098 #ifndef USE_ALT_ADDRBOOK
1099 static gint addrcompl_callback_entry(
1100 gpointer sender, gint queryID, GList *listEMail, gpointer data )
1105 /* g_print( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
1106 pthread_mutex_lock( & _completionMutex_ );
1107 if( queryID == _queryID_ ) {
1108 /* Append contents to end of display queue */
1111 ItemEMail *email = node->data;
1113 address = addritem_format_email( email );
1114 /* g_print( "\temail/address ::%s::\n", address ); */
1115 _displayQueue_ = g_list_append( _displayQueue_, address );
1116 node = g_list_next( node );
1119 g_list_free( listEMail );
1120 pthread_mutex_unlock( & _completionMutex_ );
1127 * Clear the display queue.
1129 static void addrcompl_clear_queue( void ) {
1130 /* Clear out display queue */
1131 pthread_mutex_lock( & _completionMutex_ );
1133 g_list_free_full( _displayQueue_, g_free );
1134 _displayQueue_ = NULL;
1136 pthread_mutex_unlock( & _completionMutex_ );
1140 * Add a single address entry into the display queue.
1141 * \param address Address to append.
1143 static void addrcompl_add_queue( gchar *address ) {
1144 pthread_mutex_lock( & _completionMutex_ );
1145 _displayQueue_ = g_list_append( _displayQueue_, address );
1146 pthread_mutex_unlock( & _completionMutex_ );
1150 * Load list with entries from local completion index.
1152 static void addrcompl_load_local( void ) {
1155 for (count = 0; count < get_completion_count(); count++) {
1158 address = get_complete_address( count );
1159 /* g_print( "\taddress ::%s::\n", address ); */
1161 /* Append contents to end of display queue */
1162 addrcompl_add_queue( address );
1169 static void addrcompl_start_search( void ) {
1170 #ifndef USE_ALT_ADDRBOOK
1173 searchTerm = g_strdup( _compWindow_->searchTerm );
1175 /* Setup the search */
1176 _queryID_ = addrindex_setup_search(
1177 searchTerm, NULL, addrcompl_callback_entry );
1178 g_free( searchTerm );
1180 /* g_print( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1182 /* Load local stuff */
1183 addrcompl_load_local();
1185 /* Sit back and wait until something happens */
1186 _completionIdleID_ =
1187 g_idle_add( (GSourceFunc) addrcompl_idle, NULL );
1188 /* g_print( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1190 #ifndef USE_ALT_ADDRBOOK
1191 addrindex_start_search( _queryID_ );
1198 * Apply the current selection in the list to the entry field. Focus is also
1199 * moved to the next widget so that Tab key works correctly.
1200 * \param list_view List to process.
1201 * \param entry Address entry field.
1202 * \param move_focus Move focus to the next widget ?
1204 static void completion_window_apply_selection(GtkTreeView *list_view,
1206 gboolean move_focus)
1208 gchar *address = NULL, *text = NULL;
1211 GtkTreeSelection *selection;
1212 GtkTreeModel *model;
1214 gboolean is_group = FALSE;
1215 cm_return_if_fail(list_view != NULL);
1216 cm_return_if_fail(entry != NULL);
1217 GList *grp_emails = NULL;
1219 selection = gtk_tree_view_get_selection(list_view);
1220 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
1223 /* First remove the idler */
1224 if( _completionIdleID_ != 0 ) {
1225 g_source_remove( _completionIdleID_ );
1226 _completionIdleID_ = 0;
1229 /* Process selected item */
1230 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text,
1231 ADDR_COMPL_ISGROUP, &is_group,
1232 ADDR_COMPL_GROUPLIST, &grp_emails,
1235 address = get_address_from_edit(entry, &cursor_pos);
1237 replace_address_in_edit(entry, text, cursor_pos, is_group, grp_emails);
1240 /* Move focus to next widget */
1241 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1242 if( parent && move_focus) {
1243 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1248 * Start address completion. Should be called when creating the main window
1249 * containing address completion entries.
1250 * \param mainwindow Main window.
1252 void address_completion_start(GtkWidget *mainwindow)
1254 start_address_completion(NULL);
1255 set_match_any_part(TRUE);
1257 /* register focus change hook */
1258 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
1259 G_CALLBACK(address_completion_mainwindow_set_focus),
1264 * Need unique data to make unregistering signal handler possible for the auto
1267 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1270 * Register specified entry widget for address completion.
1271 * \param entry Address entry field.
1273 void address_completion_register_entry(GtkEntry *entry, gboolean allow_commas)
1275 cm_return_if_fail(entry != NULL);
1276 cm_return_if_fail(GTK_IS_ENTRY(entry));
1278 /* add hooked property */
1279 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1280 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS, GINT_TO_POINTER(allow_commas));
1282 /* add keypress event */
1283 g_signal_connect_closure
1284 (G_OBJECT(entry), "key_press_event",
1285 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1286 COMPLETION_UNIQUE_DATA,
1292 * Unregister specified entry widget from address completion operations.
1293 * \param entry Address entry field.
1295 void address_completion_unregister_entry(GtkEntry *entry)
1299 cm_return_if_fail(entry != NULL);
1300 cm_return_if_fail(GTK_IS_ENTRY(entry));
1302 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1303 cm_return_if_fail(entry_obj);
1304 cm_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1306 /* has the hooked property? */
1307 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1309 /* remove the hook */
1310 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1311 G_CALLBACK(address_completion_entry_key_pressed),
1312 COMPLETION_UNIQUE_DATA);
1316 * End address completion. Should be called when main window with address
1317 * completion entries terminates. NOTE: this function assumes that it is
1318 * called upon destruction of the window.
1319 * \param mainwindow Main window.
1321 void address_completion_end(GtkWidget *mainwindow)
1323 /* if address_completion_end() is really called on closing the window,
1324 * we don't need to unregister the set_focus_cb */
1325 end_address_completion();
1328 /* if focus changes to another entry, then clear completion cache */
1329 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1334 if (widget && GTK_IS_ENTRY(widget) &&
1335 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1336 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), ENTRY_DATA_ALLOW_COMMAS));
1337 clear_completion_cache();
1342 * Listener that watches for tab or other keystroke in address entry field.
1343 * \param entry Address entry field.
1344 * \param ev Event object.
1345 * \param data User data.
1346 * \return <i>TRUE</i>.
1348 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1352 if (ev->keyval == GDK_KEY_Tab) {
1353 addrcompl_clear_queue();
1354 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1355 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1356 /* route a void character to the default handler */
1357 /* this is a dirty hack; we're actually changing a key
1358 * reported by the system. */
1359 ev->keyval = GDK_KEY_AudibleBell_Enable;
1360 ev->state &= ~GDK_SHIFT_MASK;
1363 address_completion_create_completion_window(entry);
1365 /* Start remote queries */
1366 addrcompl_start_search();
1373 } else if (ev->keyval == GDK_KEY_Shift_L
1374 || ev->keyval == GDK_KEY_Shift_R
1375 || ev->keyval == GDK_KEY_Control_L
1376 || ev->keyval == GDK_KEY_Control_R
1377 || ev->keyval == GDK_KEY_Caps_Lock
1378 || ev->keyval == GDK_KEY_Shift_Lock
1379 || ev->keyval == GDK_KEY_Meta_L
1380 || ev->keyval == GDK_KEY_Meta_R
1381 || ev->keyval == GDK_KEY_Alt_L
1382 || ev->keyval == GDK_KEY_Alt_R) {
1383 /* these buttons should not clear the cache... */
1385 clear_completion_cache();
1390 * Initialize search term for address completion.
1391 * \param entry Address entry field.
1393 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1396 gint ncount, cursor_pos;
1397 gchar *searchTerm, *new = NULL;
1399 cm_return_val_if_fail(entry != NULL, FALSE);
1401 if (!gtk_widget_has_focus(GTK_WIDGET(entry))) return FALSE;
1403 /* get an address component from the cursor */
1404 searchTerm = get_address_from_edit( entry, &cursor_pos );
1405 if( ! searchTerm ) return FALSE;
1406 /* g_print( "search for :::%s:::\n", searchTerm ); */
1408 /* Clear any existing search */
1409 g_free( _compWindow_->searchTerm );
1410 _compWindow_->searchTerm = g_strdup( searchTerm );
1412 /* Perform search on local completion index */
1413 ncount = complete_address( searchTerm );
1415 new = get_next_complete_address();
1418 #if (!defined(USE_LDAP) && !defined(GENERIC_UMPC))
1419 /* Select the address if there is only one match */
1421 /* Display selected address in entry field */
1422 gchar *addr = get_complete_address(1);
1423 if (addr && !strstr(addr, " <!--___group___-->")) {
1424 replace_address_in_edit(entry, addr, cursor_pos, FALSE, NULL);
1425 /* Discard the window */
1426 clear_completion_cache();
1430 /* Make sure that drop-down appears uniform! */
1434 addrcompl_add_queue( searchTerm );
1436 g_free( searchTerm );
1443 * Create new address completion window for specified entry.
1444 * \param entry_ Entry widget to associate with window.
1446 static void address_completion_create_completion_window( GtkEntry *entry_ )
1448 gint x, y, height, width, depth;
1449 GtkWidget *scroll, *list_view;
1452 GtkWidget *entry = GTK_WIDGET(entry_);
1455 /* Create new window and list */
1456 window = gtk_window_new(GTK_WINDOW_POPUP);
1457 list_view = addr_compl_list_view_create(_compWindow_);
1459 /* Destroy any existing window */
1460 addrcompl_destroy_window( _compWindow_ );
1462 /* Create new object */
1463 _compWindow_->window = window;
1464 _compWindow_->entry = entry;
1465 _compWindow_->list_view = list_view;
1466 _compWindow_->listCount = 0;
1467 _compWindow_->in_mouse = FALSE;
1469 scroll = gtk_scrolled_window_new(NULL, NULL);
1470 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1471 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1472 gtk_container_add(GTK_CONTAINER(window), scroll);
1473 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1474 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
1476 /* Use entry widget to create initial window */
1477 gdkwin = gtk_widget_get_window(entry),
1478 #if !GTK_CHECK_VERSION(3, 0, 0)
1479 gdk_window_get_geometry(gdkwin, &x, &y, &width, &height, &depth);
1481 gdk_window_get_geometry(gdkwin, &x, &y, &width, &height);
1483 gdk_window_get_origin (gdkwin, &x, &y);
1485 gtk_window_move(GTK_WINDOW(window), x, y);
1487 /* Resize window to fit initial (empty) address list */
1488 gtk_widget_size_request( list_view, &r );
1489 gtk_widget_set_size_request( window, width, r.height );
1490 gtk_widget_show_all( window );
1491 gtk_widget_size_request( list_view, &r );
1493 /* Setup handlers */
1494 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1495 G_CALLBACK(list_view_button_press),
1498 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1499 G_CALLBACK(list_view_button_release),
1502 g_signal_connect(G_OBJECT(window),
1503 "button-press-event",
1504 G_CALLBACK(completion_window_button_press),
1506 g_signal_connect(G_OBJECT(window),
1508 G_CALLBACK(completion_window_key_press),
1510 gdk_pointer_grab(gtk_widget_get_window(window), TRUE,
1511 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1512 GDK_BUTTON_RELEASE_MASK,
1513 NULL, NULL, GDK_CURRENT_TIME);
1514 gdk_keyboard_grab(gtk_widget_get_window(window), FALSE, GDK_CURRENT_TIME);
1515 gtk_grab_add( window );
1519 * Respond to button press in completion window. Check if mouse click is
1520 * anywhere outside the completion window. In that case the completion
1521 * window is destroyed, and the original searchTerm is restored.
1523 * \param widget Window object.
1524 * \param event Event.
1525 * \param compWin Reference to completion window.
1527 static gboolean completion_window_button_press(GtkWidget *widget,
1528 GdkEventButton *event,
1529 CompletionWindow *compWin )
1531 GtkWidget *event_widget, *entry;
1534 gboolean restore = TRUE;
1536 cm_return_val_if_fail(compWin != NULL, FALSE);
1538 entry = compWin->entry;
1539 cm_return_val_if_fail(entry != NULL, FALSE);
1541 /* Test where mouse was clicked */
1542 event_widget = gtk_get_event_widget((GdkEvent *)event);
1543 if (event_widget != widget) {
1544 while (event_widget) {
1545 if (event_widget == widget)
1547 else if (event_widget == entry) {
1551 event_widget = gtk_widget_get_parent(event_widget);
1556 /* Clicked outside of completion window - restore */
1557 searchTerm = _compWindow_->searchTerm;
1558 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1559 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1562 clear_completion_cache();
1563 addrcompl_destroy_window( _compWindow_ );
1569 * Respond to key press in completion window.
1570 * \param widget Window object.
1571 * \param event Event.
1572 * \param compWind Reference to completion window.
1574 static gboolean completion_window_key_press(GtkWidget *widget,
1576 CompletionWindow *compWin )
1578 GdkEventKey tmp_event;
1582 GtkWidget *list_view;
1584 cm_return_val_if_fail(compWin != NULL, FALSE);
1586 entry = compWin->entry;
1587 list_view = compWin->list_view;
1588 cm_return_val_if_fail(entry != NULL, FALSE);
1590 /* allow keyboard navigation in the alternatives tree view */
1591 if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down ||
1592 event->keyval == GDK_KEY_Page_Up || event->keyval == GDK_KEY_Page_Down) {
1593 completion_window_advance_selection
1594 (GTK_TREE_VIEW(list_view),
1595 event->keyval == GDK_KEY_Down ||
1596 event->keyval == GDK_KEY_Page_Down ? TRUE : FALSE);
1600 /* make tab move to next field */
1601 if( event->keyval == GDK_KEY_Tab ) {
1602 /* Reference to parent */
1603 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1605 /* Discard the window */
1606 clear_completion_cache();
1607 addrcompl_destroy_window( _compWindow_ );
1609 /* Move focus to next widget */
1611 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1616 /* make backtab move to previous field */
1617 if( event->keyval == GDK_KEY_ISO_Left_Tab ) {
1618 /* Reference to parent */
1619 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1621 /* Discard the window */
1622 clear_completion_cache();
1623 addrcompl_destroy_window( _compWindow_ );
1625 /* Move focus to previous widget */
1627 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1631 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1633 /* look for presses that accept the selection */
1634 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_space ||
1635 event->keyval == GDK_KEY_KP_Enter ||
1636 (_allowCommas_ && event->keyval == GDK_KEY_comma)) {
1637 /* User selected address with a key press */
1639 /* Display selected address in entry field */
1640 completion_window_apply_selection(
1641 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry),
1642 event->keyval != GDK_KEY_comma);
1644 if (event->keyval == GDK_KEY_comma) {
1645 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
1646 gtk_editable_insert_text(GTK_EDITABLE(entry), ", ", 2, &pos);
1647 gtk_editable_set_position(GTK_EDITABLE(entry), pos + 1);
1650 /* Discard the window */
1651 clear_completion_cache();
1652 addrcompl_destroy_window( _compWindow_ );
1656 /* key state keys should never be handled */
1657 if (event->keyval == GDK_KEY_Shift_L
1658 || event->keyval == GDK_KEY_Shift_R
1659 || event->keyval == GDK_KEY_Control_L
1660 || event->keyval == GDK_KEY_Control_R
1661 || event->keyval == GDK_KEY_Caps_Lock
1662 || event->keyval == GDK_KEY_Shift_Lock
1663 || event->keyval == GDK_KEY_Meta_L
1664 || event->keyval == GDK_KEY_Meta_R
1665 || event->keyval == GDK_KEY_Alt_L
1666 || event->keyval == GDK_KEY_Alt_R) {
1670 /* some other key, let's restore the searchTerm (orignal text) */
1671 searchTerm = _compWindow_->searchTerm;
1672 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1673 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1675 /* make sure anything we typed comes in the edit box */
1676 tmp_event.type = event->type;
1677 tmp_event.window = gtk_widget_get_window(GTK_WIDGET(entry));
1678 tmp_event.send_event = TRUE;
1679 tmp_event.time = event->time;
1680 tmp_event.state = event->state;
1681 tmp_event.keyval = event->keyval;
1682 tmp_event.length = event->length;
1683 tmp_event.string = event->string;
1684 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1686 /* and close the completion window */
1687 clear_completion_cache();
1688 addrcompl_destroy_window( _compWindow_ );
1694 * ============================================================================
1695 * Publically accessible functions.
1696 * ============================================================================
1700 * Setup completion object.
1702 void addrcompl_initialize( void ) {
1703 /* g_print( "addrcompl_initialize...\n" ); */
1704 if( ! _compWindow_ ) {
1705 _compWindow_ = addrcompl_create_window();
1708 _completionIdleID_ = 0;
1709 /* g_print( "addrcompl_initialize...done\n" ); */
1713 * Teardown completion object.
1715 void addrcompl_teardown( void ) {
1716 /* g_print( "addrcompl_teardown...\n" ); */
1717 addrcompl_free_window( _compWindow_ );
1718 _compWindow_ = NULL;
1720 addrcompl_clear_queue();
1722 _completionIdleID_ = 0;
1723 /* g_print( "addrcompl_teardown...done\n" ); */
1727 * tree view functions
1730 static GtkListStore *addr_compl_create_store(void)
1732 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1740 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1742 GtkTreeView *list_view;
1743 GtkTreeSelection *selector;
1744 GtkTreeModel *model;
1746 model = GTK_TREE_MODEL(addr_compl_create_store());
1747 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1748 g_object_unref(model);
1750 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1751 gtk_tree_view_set_headers_visible(list_view, FALSE);
1753 selector = gtk_tree_view_get_selection(list_view);
1754 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1755 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1758 /* create the columns */
1759 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1761 return GTK_WIDGET(list_view);
1764 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1766 GtkTreeViewColumn *column;
1767 GtkCellRenderer *renderer;
1769 renderer = gtk_cell_renderer_pixbuf_new();
1770 column = gtk_tree_view_column_new_with_attributes
1772 "pixbuf", ADDR_COMPL_ICON, NULL);
1773 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1774 renderer = gtk_cell_renderer_text_new();
1775 column = gtk_tree_view_column_new_with_attributes
1776 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1777 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1780 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1781 CompletionWindow *window)
1783 if (window && event && event->type == GDK_BUTTON_PRESS) {
1784 window->in_mouse = TRUE;
1789 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1790 CompletionWindow *window)
1792 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1793 window->in_mouse = FALSE;
1798 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1799 GtkTreeModel *model,
1801 gboolean currently_selected,
1804 CompletionWindow *window = data;
1806 if (currently_selected)
1809 if (!window->in_mouse)
1812 /* XXX: select the entry and kill window later... select is called before
1813 * any other mouse events handlers including the tree view internal one;
1814 * not using a time out would result in a crash. if this doesn't work
1815 * safely, maybe we should set variables when receiving button presses
1816 * in the tree view. */
1817 if (!window->destroying) {
1818 window->destroying = TRUE;
1819 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1825 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1827 GtkEntry *entry = GTK_ENTRY(window->entry);
1829 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1832 clear_completion_cache();
1834 addrcompl_destroy_window(window);
1838 gboolean found_in_addressbook(const gchar *address)
1841 gboolean found = FALSE;
1847 addr = g_strdup(address);
1848 extract_address(addr);
1849 num_addr = complete_address(addr);
1851 /* skip first item (this is the search string itself) */
1853 for (; i < num_addr && !found; i++) {
1854 gchar *caddr = get_complete_address(i);
1855 extract_address(caddr);
1856 if (strcasecmp(caddr, addr) == 0)