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_NEW_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;
190 match = strcasestr(addr->name, g_completion_prefix);
192 if (match == addr->name)
194 else if (match > addr->name && *(match - 1) == ' ')
197 n_weight = match - addr->name;
201 match = strcasestr(addr->address, g_completion_prefix);
203 if (match == addr->address)
206 a_weight = match - addr->address;
208 if (strlen(match) > strlen(g_completion_prefix)
209 && *(match + strlen(g_completion_prefix)) == '@')
214 if (n_weight == -4 && a_weight < 0)
217 return MIN(a_weight, n_weight);
220 static gint addr_comparison_func(gconstpointer a, gconstpointer b)
222 const address_entry* a_ref = (const address_entry*)a;
223 const address_entry* b_ref = (const address_entry*)b;
224 gint a_weight = weight_addr_match(a_ref);
225 gint b_weight = weight_addr_match(b_ref);
228 if (a_weight < b_weight)
230 else if (a_weight > b_weight)
233 cmp = strcmp(a_ref->name, b_ref->name);
234 return cmp ? cmp : strcmp(a_ref->address, b_ref->address);
239 * Initialize all completion index data.
241 static void init_all(void)
243 g_completion = g_completion_new(completion_func);
244 cm_return_if_fail(g_completion != NULL);
248 * set the compare function (default is strncmp)
250 static void set_match_any_part(const gboolean any_part)
252 if (any_part && prefs_common.address_search_wildcard)
253 g_completion_set_compare(g_completion, addr_completion_func);
255 g_completion_set_compare(g_completion, strncmp);
258 static void free_all_addresses(void)
263 walk = g_address_list;
264 for (; walk != NULL; walk = g_list_next(walk)) {
265 address_entry *ae = (address_entry *) walk->data;
268 g_list_free(ae->grp_emails);
271 g_list_free(g_address_list);
272 g_address_list = NULL;
273 if (_groupAddresses_)
274 g_hash_table_destroy(_groupAddresses_);
275 _groupAddresses_ = NULL;
278 static void clear_completion_cache(void);
279 static void free_completion_list(void)
282 if (!g_completion_list)
285 clear_completion_cache();
287 g_completion_clear_items(g_completion);
289 walk = g_list_first(g_completion_list);
290 for (; walk != NULL; walk = g_list_next(walk)) {
291 completion_entry *ce = (completion_entry *) walk->data;
295 g_list_free(g_completion_list);
296 g_completion_list = NULL;
299 * Free up all completion index data.
301 static void free_all(void)
303 free_completion_list();
304 free_all_addresses();
305 g_completion_free(g_completion);
310 * Append specified address entry to the index.
311 * \param str Index string value.
312 * \param ae Entry containing address data.
314 void addr_compl_add_address1(const char *str, address_entry *ae)
316 completion_entry *ce1;
317 ce1 = g_new0(completion_entry, 1),
318 /* GCompletion list is case sensitive */
319 ce1->string = g_utf8_strdown(str, -1);
322 g_completion_list = g_list_prepend(g_completion_list, ce1);
326 * Adds address to the completion list. This function looks complicated, but
327 * it's only allocation checks. Each value will be included in the index.
328 * \param name Recipient name.
329 * \param address EMail address.
330 * \param alias Alias to append.
331 * \param grp_emails the emails in case of a group. List should be freed later,
332 * but not its strings
333 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
336 static gint add_address(const gchar *name, const gchar *address,
337 const gchar *nick, const gchar *alias, GList *grp_emails)
341 if (!address && !grp_emails)
347 ae = g_new0(address_entry, 1);
348 cm_return_val_if_fail(ae != NULL, -1);
350 ae->name = g_strdup(name);
351 ae->address = g_strdup(address);
352 ae->grp_emails = grp_emails;
353 g_address_list = g_list_prepend(g_address_list, ae);
355 addr_compl_add_address1(name, ae);
357 if (address != NULL && *address != '\0')
358 addr_compl_add_address1(address, ae);
360 if (nick != NULL && *nick != '\0')
361 addr_compl_add_address1(nick, ae);
363 if (alias != NULL && *alias != '\0')
364 addr_compl_add_address1(alias, ae);
370 * Read address book, creating all entries in the completion index.
372 static void read_address_book(gchar *folderpath) {
373 free_all_addresses();
374 free_completion_list();
376 #ifndef USE_NEW_ADDRBOOK
377 addrindex_load_completion( add_address, folderpath );
379 GError* error = NULL;
381 addrcompl_initialize();
382 if (! addrindex_dbus_load_completion(add_address, &error)) {
383 g_warning("Failed to populate address completion list");
388 /* plugins may hook in here to modify/extend the completion list */
389 hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, &g_address_list);
391 g_address_list = g_list_reverse(g_address_list);
392 g_completion_list = g_list_reverse(g_completion_list);
393 /* merge the completion entry list into g_completion */
394 if (g_completion_list) {
395 g_completion_add_items(g_completion, g_completion_list);
396 if (debug_get_mode())
397 debug_print("read %d items in %s\n",
398 g_list_length(g_completion_list),
399 folderpath?folderpath:"(null)");
404 * Test whether there is a completion pending.
405 * \return <code>TRUE</code> if pending.
407 static gboolean is_completion_pending(void)
409 /* check if completion pending, i.e. we might satisfy a request for the next
410 * or previous address */
411 return g_completion_count;
415 * Clear the completion cache.
417 static void clear_completion_cache(void)
419 if (is_completion_pending()) {
420 g_free(g_completion_prefix);
422 if (g_completion_addresses) {
423 g_slist_free(g_completion_addresses);
424 g_completion_addresses = NULL;
427 g_completion_count = g_completion_next = 0;
432 * Prepare completion index. This function should be called prior to attempting
433 * address completion.
434 * \return The number of addresses in the completion list.
436 gint start_address_completion(gchar *folderpath)
438 gboolean different_book = FALSE;
439 clear_completion_cache();
441 if (strcmp2(completion_folder_path,folderpath))
442 different_book = TRUE;
444 g_free(completion_folder_path);
445 if (folderpath != NULL)
446 completion_folder_path = g_strdup(folderpath);
448 completion_folder_path = NULL;
452 /* open the address book */
453 read_address_book(folderpath);
454 } else if (different_book)
455 read_address_book(folderpath);
458 debug_print("start_address_completion(%s) ref count %d\n",
459 folderpath?folderpath:"(null)", g_ref_count);
461 return g_list_length(g_completion_list);
465 * Retrieve a possible address (or a part) from an entry box. To make life
466 * easier, we only look at the last valid address component; address
467 * completion only works at the last string component in the entry box.
469 * \param entry Address entry field.
470 * \param start_pos Address of start position of address.
471 * \return Possible address.
473 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
475 const gchar *edit_text, *p;
477 gboolean in_quote = FALSE;
478 gboolean in_bracket = FALSE;
481 edit_text = gtk_entry_get_text(entry);
482 if (edit_text == NULL) return NULL;
484 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
486 /* scan for a separator. doesn't matter if walk points at null byte. */
487 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
489 p = g_utf8_prev_char(p)) {
492 } else if (!in_quote) {
493 if (!in_bracket && *p == ',') {
495 } else if (*p == '<')
502 /* have something valid */
503 if (g_utf8_strlen(p, -1) == 0)
506 #define IS_VALID_CHAR(x) \
507 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
509 /* now scan back until we hit a valid character */
510 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
515 if (g_utf8_strlen(p, -1) == 0)
518 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
525 static gchar *get_complete_address_from_name_email(const gchar *name, const gchar *email)
527 gchar *address = NULL;
528 if (!name || name[0] == '\0')
529 address = g_strdup_printf("<%s>", email);
530 else if (strchr_with_skip_quote(name, '"', ','))
531 address = g_strdup_printf
532 ("\"%s\" <%s>", name, email);
534 address = g_strdup_printf
535 ("%s <%s>", name, email);
540 * Replace an incompleted address with a completed one.
541 * \param entry Address entry field.
542 * \param newtext New text.
543 * \param start_pos Insertion point in entry field.
545 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
546 gint start_pos, gboolean is_group, GList *grp_emails)
548 if (!newtext) return;
549 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
551 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
554 gchar *addresses = NULL;
555 GList *cur = grp_emails;
556 for (; cur; cur = cur->next) {
558 ItemEMail *email = (ItemEMail *)cur->data;
559 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
561 gchar *addr = get_complete_address_from_name_email(
562 ADDRITEM_NAME(person), email->address);
564 tmp = g_strdup_printf("%s, %s", addresses, addr);
566 tmp = g_strdup_printf("%s", addr);
571 gtk_editable_insert_text(GTK_EDITABLE(entry), addresses, strlen(addresses),
575 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
579 * Attempt to complete an address, and returns the number of addresses found.
580 * Use <code>get_complete_address()</code> to get an entry from the index.
582 * \param str Search string to find.
583 * \return Zero if no match was found, otherwise the number of addresses; the
584 * original prefix (search string) will appear at index 0.
586 guint complete_address(const gchar *str)
588 GList *result = NULL;
592 completion_entry *ce = NULL;
594 cm_return_val_if_fail(str != NULL, 0);
596 /* g_completion is case sensitive */
597 d = g_utf8_strdown(str, -1);
599 clear_completion_cache();
600 g_completion_prefix = g_strdup(str);
602 result = g_completion_complete(g_completion, d, NULL);
604 count = g_list_length(result);
606 /* create list with unique addresses */
607 for (cpl = 0, result = g_list_first(result);
609 result = g_list_next(result)) {
610 ce = (completion_entry *)(result->data);
611 if (NULL == g_slist_find(g_completion_addresses,
614 g_completion_addresses =
615 g_slist_append(g_completion_addresses,
619 count = cpl + 1; /* index 0 is the original prefix */
620 g_completion_next = 1; /* we start at the first completed one */
621 if (prefs_common.address_search_wildcard)
622 g_completion_addresses = g_slist_sort(g_completion_addresses,
623 addr_comparison_func);
625 g_free(g_completion_prefix);
626 g_completion_prefix = NULL;
629 g_completion_count = count;
637 * complete_matches_found() returns the number of matched addresses according
638 * to the completion mechanism. Unlike complete_address(), the returned value
639 * doesn't count str itself. If there's no match, it returns 0.
640 * To get a list of completion matches, see complete_address() instead.
642 guint complete_matches_found(const gchar *str)
644 GList *result = NULL;
647 cm_return_val_if_fail(str != NULL, 0);
649 /* g_completion is case sensitive */
650 d = g_utf8_strdown(str, -1);
652 clear_completion_cache();
653 g_completion_prefix = g_strdup(str);
655 result = g_completion_complete(g_completion, d, NULL);
657 g_free(g_completion_prefix);
660 return g_list_length(result);
664 * Return a complete address from the index.
665 * \param index Index of entry that was found (by the previous call to
666 * <code>complete_address()</code>
667 * \return Completed address string; this should be freed when done.
669 gchar *get_complete_address(gint index)
671 const address_entry *p;
672 gchar *address = NULL;
674 if (index < g_completion_count) {
676 address = g_strdup(g_completion_prefix);
678 /* get something from the unique addresses */
679 p = (address_entry *)g_slist_nth_data
680 (g_completion_addresses, index - 1);
681 if (p != NULL && p->address != NULL) {
682 address = get_complete_address_from_name_email(p->name, p->address);
683 } else if (p != NULL && p->address == NULL && p->name != NULL) {
685 address = g_strdup_printf("%s (%s) <!--___group___-->", p->name, _("Group"));
686 if (!_groupAddresses_) {
687 _groupAddresses_ = g_hash_table_new(NULL, g_direct_equal);
689 if (!g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)))) {
690 g_hash_table_insert(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)), p->grp_emails);
701 * Return the next complete address match from the completion index.
702 * \return Completed address string; this should be freed when done.
704 static gchar *get_next_complete_address(void)
706 if (is_completion_pending()) {
709 res = get_complete_address(g_completion_next);
710 g_completion_next += 1;
711 if (g_completion_next >= g_completion_count)
712 g_completion_next = 0;
720 * Return a count of the completed matches in the completion index.
721 * \return Number of matched entries.
723 static guint get_completion_count(void)
725 if (is_completion_pending())
726 return g_completion_count;
732 * Invalidate address completion index. This function should be called whenever
733 * the address book changes. This forces data to be read into the completion
735 * \return Number of entries in index.
737 gint invalidate_address_completion(void)
740 /* simply the same as start_address_completion() */
741 debug_print("Invalidation request for address completion\n");
742 read_address_book(completion_folder_path);
743 clear_completion_cache();
746 return g_list_length(g_completion_list);
750 * Finished with completion index. This function should be called after
751 * matching addresses.
752 * \return Reference count.
754 gint end_address_completion(void)
756 gboolean different_folder = FALSE;
757 clear_completion_cache();
759 /* reset the folderpath to NULL */
760 if (completion_folder_path) {
761 g_free(completion_folder_path);
762 completion_folder_path = NULL;
763 different_folder = TRUE;
765 if (0 == --g_ref_count)
768 debug_print("end_address_completion ref count %d\n", g_ref_count);
769 if (g_ref_count && different_folder) {
770 debug_print("still ref'd, different folder\n");
771 invalidate_address_completion();
780 static CompletionWindow *_compWindow_ = NULL;
783 * Mutex to protect callback from multiple threads.
785 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
788 * Completion queue list.
790 static GList *_displayQueue_ = NULL;
794 static gint _queryID_ = 0;
797 * Completion idle ID.
799 static guint _completionIdleID_ = 0;
802 * address completion entry ui. the ui (completion list was inspired by galeon's
803 * auto completion list). remaining things powered by claws's completion engine.
806 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
807 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
809 static void address_completion_mainwindow_set_focus (GtkWindow *window,
812 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
815 static gboolean address_completion_complete_address_in_entry
818 static void address_completion_create_completion_window (GtkEntry *entry);
820 static gboolean completion_window_button_press
822 GdkEventButton *event,
823 CompletionWindow *compWin );
825 static gboolean completion_window_key_press
828 CompletionWindow *compWin );
829 static void address_completion_create_completion_window( GtkEntry *entry_ );
832 * Create a completion window object.
833 * \return Initialized completion window.
835 static CompletionWindow *addrcompl_create_window( void ) {
836 CompletionWindow *cw;
838 cw = g_new0( CompletionWindow, 1 );
840 cw->searchTerm = NULL;
843 cw->list_view = NULL;
844 cw->in_mouse = FALSE;
845 cw->destroying = FALSE;
851 * Destroy completion window.
852 * \param cw Window to destroy.
854 static void addrcompl_destroy_window( CompletionWindow *cw ) {
855 /* Stop all searches currently in progress */
856 #ifndef USE_NEW_ADDRBOOK
857 addrindex_stop_search( _queryID_ );
859 /* Remove idler function... or application may not terminate */
860 if( _completionIdleID_ != 0 ) {
861 g_source_remove( _completionIdleID_ );
862 _completionIdleID_ = 0;
865 /* Now destroy window */
867 /* Clear references to widgets */
869 cw->list_view = NULL;
873 gtk_widget_hide( cw->window );
874 gtk_widget_destroy( cw->window );
877 cw->destroying = FALSE;
878 cw->in_mouse = FALSE;
884 * Free up completion window.
885 * \param cw Window to free.
887 static void addrcompl_free_window( CompletionWindow *cw ) {
889 addrcompl_destroy_window( cw );
891 g_free( cw->searchTerm );
892 cw->searchTerm = NULL;
894 /* Clear references */
903 * Advance selection to previous/next item in list.
904 * \param list_view List to process.
905 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
908 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
910 GtkTreeSelection *selection;
914 cm_return_if_fail(list_view != NULL);
916 selection = gtk_tree_view_get_selection(list_view);
917 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
921 forward = gtk_tree_model_iter_next(model, &iter);
923 gtk_tree_selection_select_iter(selection, &iter);
927 prev = gtk_tree_model_get_path(model, &iter);
931 if (gtk_tree_path_prev(prev))
932 gtk_tree_selection_select_path(selection, prev);
934 gtk_tree_path_free(prev);
939 * Resize window to accommodate maximum number of address entries.
940 * \param cw Completion window.
942 static void addrcompl_resize_window( CompletionWindow *cw ) {
944 gint x, y, width, height, depth;
946 /* Get current geometry of window */
947 #if !GTK_CHECK_VERSION(3, 0, 0)
948 gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height, &depth );
950 gdk_window_get_geometry( gtk_widget_get_window( cw->window ), &x, &y, &width, &height );
953 gtk_widget_queue_resize_no_redraw(cw->list_view);
954 gtk_widget_size_request( cw->list_view, &r );
956 /* Adjust window height to available screen space */
957 if( y + r.height > gdk_screen_height())
958 r.height = gdk_screen_height() - y;
960 gtk_widget_set_size_request(cw->window, width, r.height);
962 gdk_pointer_grab(gtk_widget_get_window(cw->window), TRUE,
963 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
964 GDK_BUTTON_RELEASE_MASK,
965 NULL, NULL, GDK_CURRENT_TIME);
966 gdk_keyboard_grab(gtk_widget_get_window(cw->window), FALSE, GDK_CURRENT_TIME);
967 gtk_grab_add(cw->window);
971 static GdkPixbuf *group_pixbuf = NULL;
972 static GdkPixbuf *email_pixbuf = NULL;
975 * Add an address the completion window address list.
976 * \param cw Completion window.
977 * \param address Address to add.
979 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
982 GtkTreeSelection *selection;
983 gboolean is_group = FALSE;
984 GList *grp_emails = NULL;
985 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
989 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
990 g_object_ref(G_OBJECT(group_pixbuf));
993 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
994 g_object_ref(G_OBJECT(email_pixbuf));
996 /* g_print( "\t\tAdding :%s\n", address ); */
997 if (strstr(address, " <!--___group___-->")) {
999 if (_groupAddresses_)
1000 grp_emails = g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)));
1001 *(strstr(address, " <!--___group___-->")) = '\0';
1002 pixbuf = group_pixbuf;
1003 } else if (strchr(address, '@') && strchr(address, '<') &&
1004 strchr(address, '>')) {
1005 pixbuf = email_pixbuf;
1009 if (is_group && !_allowCommas_)
1011 gtk_list_store_append(store, &iter);
1012 gtk_list_store_set(store, &iter,
1013 ADDR_COMPL_ICON, pixbuf,
1014 ADDR_COMPL_ADDRESS, address,
1015 ADDR_COMPL_ISGROUP, is_group,
1016 ADDR_COMPL_GROUPLIST, grp_emails,
1021 addrcompl_resize_window( cw );
1022 gtk_grab_add( cw->window );
1024 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
1025 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
1027 if( cw->listCount == 1 ) {
1028 /* Select first row for now */
1029 gtk_tree_selection_select_iter(selection, &iter);
1031 #ifndef GENERIC_UMPC
1032 else if( cw->listCount == 2 ) {
1033 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter)) {
1034 /* Move off first row */
1035 gtk_tree_selection_select_iter(selection, &iter);
1041 void addrcompl_reflect_prefs_pixmap_theme(void) {
1043 g_object_unref(G_OBJECT(group_pixbuf));
1044 group_pixbuf = NULL;
1047 g_object_unref(G_OBJECT(email_pixbuf));
1048 email_pixbuf = NULL;
1053 * Completion idle function. This function is called by the main (UI) thread
1054 * during UI idle time while an address search is in progress. Items from the
1055 * display queue are processed and appended to the address list.
1057 * \param data Target completion window to receive email addresses.
1058 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
1060 static gboolean addrcompl_idle( gpointer data ) {
1064 /* Process all entries in display queue */
1065 pthread_mutex_lock( & _completionMutex_ );
1066 if( _displayQueue_ ) {
1067 node = _displayQueue_;
1069 address = node->data;
1070 /* g_print( "address ::: %s :::\n", address ); */
1071 addrcompl_add_entry( _compWindow_, address );
1073 node = g_list_next( node );
1075 g_list_free( _displayQueue_ );
1076 _displayQueue_ = NULL;
1078 pthread_mutex_unlock( & _completionMutex_ );
1085 * Callback entry point. The background thread (if any) appends the address
1086 * list to the display queue.
1087 * \param sender Sender of query.
1088 * \param queryID Query ID of search request.
1089 * \param listEMail List of zero of more email objects that met search
1091 * \param data Query data.
1093 #ifndef USE_NEW_ADDRBOOK
1094 static gint addrcompl_callback_entry(
1095 gpointer sender, gint queryID, GList *listEMail, gpointer data )
1100 /* g_print( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
1101 pthread_mutex_lock( & _completionMutex_ );
1102 if( queryID == _queryID_ ) {
1103 /* Append contents to end of display queue */
1106 ItemEMail *email = node->data;
1108 address = addritem_format_email( email );
1109 /* g_print( "\temail/address ::%s::\n", address ); */
1110 _displayQueue_ = g_list_append( _displayQueue_, address );
1111 node = g_list_next( node );
1114 g_list_free( listEMail );
1115 pthread_mutex_unlock( & _completionMutex_ );
1122 * Clear the display queue.
1124 static void addrcompl_clear_queue( void ) {
1125 /* Clear out display queue */
1126 pthread_mutex_lock( & _completionMutex_ );
1128 g_list_free( _displayQueue_ );
1129 _displayQueue_ = NULL;
1131 pthread_mutex_unlock( & _completionMutex_ );
1135 * Add a single address entry into the display queue.
1136 * \param address Address to append.
1138 static void addrcompl_add_queue( gchar *address ) {
1139 pthread_mutex_lock( & _completionMutex_ );
1140 _displayQueue_ = g_list_append( _displayQueue_, address );
1141 pthread_mutex_unlock( & _completionMutex_ );
1145 * Load list with entries from local completion index.
1147 static void addrcompl_load_local( void ) {
1150 for (count = 0; count < get_completion_count(); count++) {
1153 address = get_complete_address( count );
1154 /* g_print( "\taddress ::%s::\n", address ); */
1156 /* Append contents to end of display queue */
1157 addrcompl_add_queue( address );
1164 static void addrcompl_start_search( void ) {
1165 #ifndef USE_NEW_ADDRBOOK
1168 searchTerm = g_strdup( _compWindow_->searchTerm );
1170 /* Setup the search */
1171 _queryID_ = addrindex_setup_search(
1172 searchTerm, NULL, addrcompl_callback_entry );
1173 g_free( searchTerm );
1175 /* g_print( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1177 /* Load local stuff */
1178 addrcompl_load_local();
1180 /* Sit back and wait until something happens */
1181 _completionIdleID_ =
1182 g_idle_add( (GSourceFunc) addrcompl_idle, NULL );
1183 /* g_print( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1185 #ifndef USE_NEW_ADDRBOOK
1186 addrindex_start_search( _queryID_ );
1193 * Apply the current selection in the list to the entry field. Focus is also
1194 * moved to the next widget so that Tab key works correctly.
1195 * \param list_view List to process.
1196 * \param entry Address entry field.
1197 * \param move_focus Move focus to the next widget ?
1199 static void completion_window_apply_selection(GtkTreeView *list_view,
1201 gboolean move_focus)
1203 gchar *address = NULL, *text = NULL;
1206 GtkTreeSelection *selection;
1207 GtkTreeModel *model;
1209 gboolean is_group = FALSE;
1210 cm_return_if_fail(list_view != NULL);
1211 cm_return_if_fail(entry != NULL);
1212 GList *grp_emails = NULL;
1214 selection = gtk_tree_view_get_selection(list_view);
1215 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
1218 /* First remove the idler */
1219 if( _completionIdleID_ != 0 ) {
1220 g_source_remove( _completionIdleID_ );
1221 _completionIdleID_ = 0;
1224 /* Process selected item */
1225 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text,
1226 ADDR_COMPL_ISGROUP, &is_group,
1227 ADDR_COMPL_GROUPLIST, &grp_emails,
1230 address = get_address_from_edit(entry, &cursor_pos);
1232 replace_address_in_edit(entry, text, cursor_pos, is_group, grp_emails);
1235 /* Move focus to next widget */
1236 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1237 if( parent && move_focus) {
1238 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1243 * Start address completion. Should be called when creating the main window
1244 * containing address completion entries.
1245 * \param mainwindow Main window.
1247 void address_completion_start(GtkWidget *mainwindow)
1249 start_address_completion(NULL);
1250 set_match_any_part(TRUE);
1252 /* register focus change hook */
1253 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
1254 G_CALLBACK(address_completion_mainwindow_set_focus),
1259 * Need unique data to make unregistering signal handler possible for the auto
1262 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1265 * Register specified entry widget for address completion.
1266 * \param entry Address entry field.
1268 void address_completion_register_entry(GtkEntry *entry, gboolean allow_commas)
1270 cm_return_if_fail(entry != NULL);
1271 cm_return_if_fail(GTK_IS_ENTRY(entry));
1273 /* add hooked property */
1274 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1275 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS, GINT_TO_POINTER(allow_commas));
1277 /* add keypress event */
1278 g_signal_connect_closure
1279 (G_OBJECT(entry), "key_press_event",
1280 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1281 COMPLETION_UNIQUE_DATA,
1287 * Unregister specified entry widget from address completion operations.
1288 * \param entry Address entry field.
1290 void address_completion_unregister_entry(GtkEntry *entry)
1294 cm_return_if_fail(entry != NULL);
1295 cm_return_if_fail(GTK_IS_ENTRY(entry));
1297 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1298 cm_return_if_fail(entry_obj);
1299 cm_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1301 /* has the hooked property? */
1302 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1304 /* remove the hook */
1305 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1306 G_CALLBACK(address_completion_entry_key_pressed),
1307 COMPLETION_UNIQUE_DATA);
1311 * End address completion. Should be called when main window with address
1312 * completion entries terminates. NOTE: this function assumes that it is
1313 * called upon destruction of the window.
1314 * \param mainwindow Main window.
1316 void address_completion_end(GtkWidget *mainwindow)
1318 /* if address_completion_end() is really called on closing the window,
1319 * we don't need to unregister the set_focus_cb */
1320 end_address_completion();
1323 /* if focus changes to another entry, then clear completion cache */
1324 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1329 if (widget && GTK_IS_ENTRY(widget) &&
1330 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1331 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), ENTRY_DATA_ALLOW_COMMAS));
1332 clear_completion_cache();
1337 * Listener that watches for tab or other keystroke in address entry field.
1338 * \param entry Address entry field.
1339 * \param ev Event object.
1340 * \param data User data.
1341 * \return <i>TRUE</i>.
1343 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1347 if (ev->keyval == GDK_KEY_Tab) {
1348 addrcompl_clear_queue();
1349 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1350 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1351 /* route a void character to the default handler */
1352 /* this is a dirty hack; we're actually changing a key
1353 * reported by the system. */
1354 ev->keyval = GDK_KEY_AudibleBell_Enable;
1355 ev->state &= ~GDK_SHIFT_MASK;
1358 address_completion_create_completion_window(entry);
1360 /* Start remote queries */
1361 addrcompl_start_search();
1368 } else if (ev->keyval == GDK_KEY_Shift_L
1369 || ev->keyval == GDK_KEY_Shift_R
1370 || ev->keyval == GDK_KEY_Control_L
1371 || ev->keyval == GDK_KEY_Control_R
1372 || ev->keyval == GDK_KEY_Caps_Lock
1373 || ev->keyval == GDK_KEY_Shift_Lock
1374 || ev->keyval == GDK_KEY_Meta_L
1375 || ev->keyval == GDK_KEY_Meta_R
1376 || ev->keyval == GDK_KEY_Alt_L
1377 || ev->keyval == GDK_KEY_Alt_R) {
1378 /* these buttons should not clear the cache... */
1380 clear_completion_cache();
1385 * Initialize search term for address completion.
1386 * \param entry Address entry field.
1388 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1391 gint ncount, cursor_pos;
1392 gchar *searchTerm, *new = NULL;
1394 cm_return_val_if_fail(entry != NULL, FALSE);
1396 if (!gtk_widget_has_focus(GTK_WIDGET(entry))) return FALSE;
1398 /* get an address component from the cursor */
1399 searchTerm = get_address_from_edit( entry, &cursor_pos );
1400 if( ! searchTerm ) return FALSE;
1401 /* g_print( "search for :::%s:::\n", searchTerm ); */
1403 /* Clear any existing search */
1404 g_free( _compWindow_->searchTerm );
1405 _compWindow_->searchTerm = g_strdup( searchTerm );
1407 /* Perform search on local completion index */
1408 ncount = complete_address( searchTerm );
1410 new = get_next_complete_address();
1413 #if (!defined(USE_LDAP) && !defined(GENERIC_UMPC))
1414 /* Select the address if there is only one match */
1416 /* Display selected address in entry field */
1417 gchar *addr = get_complete_address(1);
1418 if (addr && !strstr(addr, " <!--___group___-->")) {
1419 replace_address_in_edit(entry, addr, cursor_pos, FALSE, NULL);
1420 /* Discard the window */
1421 clear_completion_cache();
1425 /* Make sure that drop-down appears uniform! */
1429 addrcompl_add_queue( g_strdup( searchTerm ) );
1431 g_free( searchTerm );
1437 * Create new address completion window for specified entry.
1438 * \param entry_ Entry widget to associate with window.
1440 static void address_completion_create_completion_window( GtkEntry *entry_ )
1442 gint x, y, height, width, depth;
1443 GtkWidget *scroll, *list_view;
1446 GtkWidget *entry = GTK_WIDGET(entry_);
1449 /* Create new window and list */
1450 window = gtk_window_new(GTK_WINDOW_POPUP);
1451 list_view = addr_compl_list_view_create(_compWindow_);
1453 /* Destroy any existing window */
1454 addrcompl_destroy_window( _compWindow_ );
1456 /* Create new object */
1457 _compWindow_->window = window;
1458 _compWindow_->entry = entry;
1459 _compWindow_->list_view = list_view;
1460 _compWindow_->listCount = 0;
1461 _compWindow_->in_mouse = FALSE;
1463 scroll = gtk_scrolled_window_new(NULL, NULL);
1464 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1465 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1466 gtk_container_add(GTK_CONTAINER(window), scroll);
1467 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1468 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
1470 /* Use entry widget to create initial window */
1471 gdkwin = gtk_widget_get_window(entry),
1472 #if !GTK_CHECK_VERSION(3, 0, 0)
1473 gdk_window_get_geometry(gdkwin, &x, &y, &width, &height, &depth);
1475 gdk_window_get_geometry(gdkwin, &x, &y, &width, &height);
1477 gdk_window_get_origin (gdkwin, &x, &y);
1479 gtk_window_move(GTK_WINDOW(window), x, y);
1481 /* Resize window to fit initial (empty) address list */
1482 gtk_widget_size_request( list_view, &r );
1483 gtk_widget_set_size_request( window, width, r.height );
1484 gtk_widget_show_all( window );
1485 gtk_widget_size_request( list_view, &r );
1487 /* Setup handlers */
1488 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1489 G_CALLBACK(list_view_button_press),
1492 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1493 G_CALLBACK(list_view_button_release),
1496 g_signal_connect(G_OBJECT(window),
1497 "button-press-event",
1498 G_CALLBACK(completion_window_button_press),
1500 g_signal_connect(G_OBJECT(window),
1502 G_CALLBACK(completion_window_key_press),
1504 gdk_pointer_grab(gtk_widget_get_window(window), TRUE,
1505 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1506 GDK_BUTTON_RELEASE_MASK,
1507 NULL, NULL, GDK_CURRENT_TIME);
1508 gdk_keyboard_grab(gtk_widget_get_window(window), FALSE, GDK_CURRENT_TIME);
1509 gtk_grab_add( window );
1513 * Respond to button press in completion window. Check if mouse click is
1514 * anywhere outside the completion window. In that case the completion
1515 * window is destroyed, and the original searchTerm is restored.
1517 * \param widget Window object.
1518 * \param event Event.
1519 * \param compWin Reference to completion window.
1521 static gboolean completion_window_button_press(GtkWidget *widget,
1522 GdkEventButton *event,
1523 CompletionWindow *compWin )
1525 GtkWidget *event_widget, *entry;
1528 gboolean restore = TRUE;
1530 cm_return_val_if_fail(compWin != NULL, FALSE);
1532 entry = compWin->entry;
1533 cm_return_val_if_fail(entry != NULL, FALSE);
1535 /* Test where mouse was clicked */
1536 event_widget = gtk_get_event_widget((GdkEvent *)event);
1537 if (event_widget != widget) {
1538 while (event_widget) {
1539 if (event_widget == widget)
1541 else if (event_widget == entry) {
1545 event_widget = gtk_widget_get_parent(event_widget);
1550 /* Clicked outside of completion window - restore */
1551 searchTerm = _compWindow_->searchTerm;
1552 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1553 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1556 clear_completion_cache();
1557 addrcompl_destroy_window( _compWindow_ );
1563 * Respond to key press in completion window.
1564 * \param widget Window object.
1565 * \param event Event.
1566 * \param compWind Reference to completion window.
1568 static gboolean completion_window_key_press(GtkWidget *widget,
1570 CompletionWindow *compWin )
1572 GdkEventKey tmp_event;
1576 GtkWidget *list_view;
1578 cm_return_val_if_fail(compWin != NULL, FALSE);
1580 entry = compWin->entry;
1581 list_view = compWin->list_view;
1582 cm_return_val_if_fail(entry != NULL, FALSE);
1584 /* allow keyboard navigation in the alternatives tree view */
1585 if (event->keyval == GDK_KEY_Up || event->keyval == GDK_KEY_Down ||
1586 event->keyval == GDK_KEY_Page_Up || event->keyval == GDK_KEY_Page_Down) {
1587 completion_window_advance_selection
1588 (GTK_TREE_VIEW(list_view),
1589 event->keyval == GDK_KEY_Down ||
1590 event->keyval == GDK_KEY_Page_Down ? TRUE : FALSE);
1594 /* make tab move to next field */
1595 if( event->keyval == GDK_KEY_Tab ) {
1596 /* Reference to parent */
1597 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1599 /* Discard the window */
1600 clear_completion_cache();
1601 addrcompl_destroy_window( _compWindow_ );
1603 /* Move focus to next widget */
1605 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1610 /* make backtab move to previous field */
1611 if( event->keyval == GDK_KEY_ISO_Left_Tab ) {
1612 /* Reference to parent */
1613 parent = gtk_widget_get_parent(GTK_WIDGET(entry));
1615 /* Discard the window */
1616 clear_completion_cache();
1617 addrcompl_destroy_window( _compWindow_ );
1619 /* Move focus to previous widget */
1621 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1625 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1627 /* look for presses that accept the selection */
1628 if (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_space ||
1629 event->keyval == GDK_KEY_KP_Enter ||
1630 (_allowCommas_ && event->keyval == GDK_KEY_comma)) {
1631 /* User selected address with a key press */
1633 /* Display selected address in entry field */
1634 completion_window_apply_selection(
1635 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry),
1636 event->keyval != GDK_KEY_comma);
1638 if (event->keyval == GDK_KEY_comma) {
1639 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
1640 gtk_editable_insert_text(GTK_EDITABLE(entry), ", ", 2, &pos);
1641 gtk_editable_set_position(GTK_EDITABLE(entry), pos + 1);
1644 /* Discard the window */
1645 clear_completion_cache();
1646 addrcompl_destroy_window( _compWindow_ );
1650 /* key state keys should never be handled */
1651 if (event->keyval == GDK_KEY_Shift_L
1652 || event->keyval == GDK_KEY_Shift_R
1653 || event->keyval == GDK_KEY_Control_L
1654 || event->keyval == GDK_KEY_Control_R
1655 || event->keyval == GDK_KEY_Caps_Lock
1656 || event->keyval == GDK_KEY_Shift_Lock
1657 || event->keyval == GDK_KEY_Meta_L
1658 || event->keyval == GDK_KEY_Meta_R
1659 || event->keyval == GDK_KEY_Alt_L
1660 || event->keyval == GDK_KEY_Alt_R) {
1664 /* some other key, let's restore the searchTerm (orignal text) */
1665 searchTerm = _compWindow_->searchTerm;
1666 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1667 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1669 /* make sure anything we typed comes in the edit box */
1670 tmp_event.type = event->type;
1671 tmp_event.window = gtk_widget_get_window(GTK_WIDGET(entry));
1672 tmp_event.send_event = TRUE;
1673 tmp_event.time = event->time;
1674 tmp_event.state = event->state;
1675 tmp_event.keyval = event->keyval;
1676 tmp_event.length = event->length;
1677 tmp_event.string = event->string;
1678 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1680 /* and close the completion window */
1681 clear_completion_cache();
1682 addrcompl_destroy_window( _compWindow_ );
1688 * ============================================================================
1689 * Publically accessible functions.
1690 * ============================================================================
1694 * Setup completion object.
1696 void addrcompl_initialize( void ) {
1697 /* g_print( "addrcompl_initialize...\n" ); */
1698 if( ! _compWindow_ ) {
1699 _compWindow_ = addrcompl_create_window();
1702 _completionIdleID_ = 0;
1703 /* g_print( "addrcompl_initialize...done\n" ); */
1707 * Teardown completion object.
1709 void addrcompl_teardown( void ) {
1710 /* g_print( "addrcompl_teardown...\n" ); */
1711 addrcompl_free_window( _compWindow_ );
1712 _compWindow_ = NULL;
1714 addrcompl_clear_queue();
1716 _completionIdleID_ = 0;
1717 /* g_print( "addrcompl_teardown...done\n" ); */
1721 * tree view functions
1724 static GtkListStore *addr_compl_create_store(void)
1726 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1734 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1736 GtkTreeView *list_view;
1737 GtkTreeSelection *selector;
1738 GtkTreeModel *model;
1740 model = GTK_TREE_MODEL(addr_compl_create_store());
1741 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1742 g_object_unref(model);
1744 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1745 gtk_tree_view_set_headers_visible(list_view, FALSE);
1747 selector = gtk_tree_view_get_selection(list_view);
1748 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1749 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1752 /* create the columns */
1753 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1755 return GTK_WIDGET(list_view);
1758 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1760 GtkTreeViewColumn *column;
1761 GtkCellRenderer *renderer;
1763 renderer = gtk_cell_renderer_pixbuf_new();
1764 column = gtk_tree_view_column_new_with_attributes
1766 "pixbuf", ADDR_COMPL_ICON, NULL);
1767 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1768 renderer = gtk_cell_renderer_text_new();
1769 column = gtk_tree_view_column_new_with_attributes
1770 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1771 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1774 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1775 CompletionWindow *window)
1777 if (window && event && event->type == GDK_BUTTON_PRESS) {
1778 window->in_mouse = TRUE;
1783 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1784 CompletionWindow *window)
1786 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1787 window->in_mouse = FALSE;
1792 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1793 GtkTreeModel *model,
1795 gboolean currently_selected,
1798 CompletionWindow *window = data;
1800 if (currently_selected)
1803 if (!window->in_mouse)
1806 /* XXX: select the entry and kill window later... select is called before
1807 * any other mouse events handlers including the tree view internal one;
1808 * not using a time out would result in a crash. if this doesn't work
1809 * safely, maybe we should set variables when receiving button presses
1810 * in the tree view. */
1811 if (!window->destroying) {
1812 window->destroying = TRUE;
1813 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1819 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1821 GtkEntry *entry = GTK_ENTRY(window->entry);
1823 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1826 clear_completion_cache();
1828 addrcompl_destroy_window(window);