2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2021 the Claws Mail team and Alfons Hoogervorst
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 if (!a_ref
->name
|| !b_ref
->name
)
236 cmp
= !!a_ref
->name
- !!b_ref
->name
;
238 cmp
= strcmp(a_ref
->name
, b_ref
->name
);
241 if (!a_ref
->address
|| !b_ref
->address
)
242 cmp
= !!a_ref
->address
- !!b_ref
->address
;
244 cmp
= g_strcmp0(a_ref
->address
, b_ref
->address
);
251 * Initialize all completion index data.
253 static void init_all(void)
255 g_completion
= g_completion_new(completion_func
);
256 cm_return_if_fail(g_completion
!= NULL
);
260 * set the compare function (default is strncmp)
262 static void set_match_any_part(const gboolean any_part
)
264 if (any_part
&& prefs_common
.address_search_wildcard
)
265 g_completion_set_compare(g_completion
, addr_completion_func
);
267 g_completion_set_compare(g_completion
, strncmp
);
270 static void free_all_addresses(void)
275 walk
= g_address_list
;
276 for (; walk
!= NULL
; walk
= g_list_next(walk
)) {
277 address_entry
*ae
= (address_entry
*) walk
->data
;
280 g_list_free(ae
->grp_emails
);
283 g_list_free(g_address_list
);
284 g_address_list
= NULL
;
285 if (_groupAddresses_
)
286 g_hash_table_destroy(_groupAddresses_
);
287 _groupAddresses_
= NULL
;
290 static void clear_completion_cache(void);
291 static void free_completion_list(void)
294 if (!g_completion_list
)
297 clear_completion_cache();
299 g_completion_clear_items(g_completion
);
301 walk
= g_list_first(g_completion_list
);
302 for (; walk
!= NULL
; walk
= g_list_next(walk
)) {
303 completion_entry
*ce
= (completion_entry
*) walk
->data
;
307 g_list_free(g_completion_list
);
308 g_completion_list
= NULL
;
311 * Free up all completion index data.
313 static void free_all(void)
315 free_completion_list();
316 free_all_addresses();
317 g_completion_free(g_completion
);
322 * Append specified address entry to the index.
323 * \param str Index string value.
324 * \param ae Entry containing address data.
326 void addr_compl_add_address1(const char *str
, address_entry
*ae
)
328 completion_entry
*ce1
;
329 ce1
= g_new0(completion_entry
, 1),
330 /* GCompletion list is case sensitive */
331 ce1
->string
= g_utf8_strdown(str
, -1);
334 g_completion_list
= g_list_prepend(g_completion_list
, ce1
);
338 * Adds address to the completion list. This function looks complicated, but
339 * it's only allocation checks. Each value will be included in the index.
340 * \param name Recipient name.
341 * \param address EMail address.
342 * \param alias Alias to append.
343 * \param grp_emails the emails in case of a group. List should be freed later,
344 * but not its strings
345 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
348 static gint
add_address(const gchar
*name
, const gchar
*address
,
349 const gchar
*nick
, const gchar
*alias
, GList
*grp_emails
)
353 if (!address
&& !grp_emails
)
359 ae
= g_new0(address_entry
, 1);
360 cm_return_val_if_fail(ae
!= NULL
, -1);
362 ae
->name
= g_strdup(name
);
363 ae
->address
= g_strdup(address
);
364 ae
->grp_emails
= grp_emails
;
365 g_address_list
= g_list_prepend(g_address_list
, ae
);
367 addr_compl_add_address1(name
, ae
);
369 if (address
!= NULL
&& *address
!= '\0')
370 addr_compl_add_address1(address
, ae
);
372 if (nick
!= NULL
&& *nick
!= '\0')
373 addr_compl_add_address1(nick
, ae
);
375 if (alias
!= NULL
&& *alias
!= '\0')
376 addr_compl_add_address1(alias
, ae
);
382 * Read address book, creating all entries in the completion index.
384 static void read_address_book(gchar
*folderpath
) {
385 free_all_addresses();
386 free_completion_list();
388 #ifndef USE_ALT_ADDRBOOK
389 addrindex_load_completion( add_address
, folderpath
);
391 GError
* error
= NULL
;
393 addrcompl_initialize();
394 if (! addrindex_dbus_load_completion(add_address
, &error
)) {
395 g_warning("Failed to populate address completion list");
400 /* plugins may hook in here to modify/extend the completion list */
402 hooks_invoke(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST
, &g_address_list
);
405 g_address_list
= g_list_reverse(g_address_list
);
406 g_completion_list
= g_list_reverse(g_completion_list
);
407 /* merge the completion entry list into g_completion */
408 if (g_completion_list
) {
409 g_completion_add_items(g_completion
, g_completion_list
);
410 if (debug_get_mode())
411 debug_print("read %d items in %s\n",
412 g_list_length(g_completion_list
),
413 folderpath
?folderpath
:"(null)");
418 * Test whether there is a completion pending.
419 * \return <code>TRUE</code> if pending.
421 static gboolean
is_completion_pending(void)
423 /* check if completion pending, i.e. we might satisfy a request for the next
424 * or previous address */
425 return g_completion_count
;
429 * Clear the completion cache.
431 static void clear_completion_cache(void)
433 if (is_completion_pending()) {
434 g_free(g_completion_prefix
);
436 if (g_completion_addresses
) {
437 g_slist_free(g_completion_addresses
);
438 g_completion_addresses
= NULL
;
441 g_completion_count
= g_completion_next
= 0;
446 * Prepare completion index. This function should be called prior to attempting
447 * address completion.
448 * \return The number of addresses in the completion list.
450 guint
start_address_completion(gchar
*folderpath
)
452 gboolean different_book
= FALSE
;
453 clear_completion_cache();
455 if (g_strcmp0(completion_folder_path
,folderpath
))
456 different_book
= TRUE
;
458 g_free(completion_folder_path
);
459 if (folderpath
!= NULL
)
460 completion_folder_path
= g_strdup(folderpath
);
462 completion_folder_path
= NULL
;
466 /* open the address book */
467 read_address_book(folderpath
);
468 } else if (different_book
)
469 read_address_book(folderpath
);
472 debug_print("start_address_completion(%s) ref count %d\n",
473 folderpath
?folderpath
:"(null)", g_ref_count
);
475 return g_list_length(g_completion_list
);
479 * Retrieve a possible address (or a part) from an entry box. To make life
480 * easier, we only look at the last valid address component; address
481 * completion only works at the last string component in the entry box.
483 * \param entry Address entry field.
484 * \param start_pos Address of start position of address.
485 * \return Possible address.
487 static gchar
*get_address_from_edit(GtkEntry
*entry
, gint
*start_pos
)
489 const gchar
*edit_text
, *p
;
491 gboolean in_quote
= FALSE
;
492 gboolean in_bracket
= FALSE
;
495 edit_text
= gtk_entry_get_text(entry
);
496 if (edit_text
== NULL
) return NULL
;
498 cur_pos
= gtk_editable_get_position(GTK_EDITABLE(entry
));
500 /* scan for a separator. doesn't matter if walk points at null byte. */
501 for (p
= g_utf8_offset_to_pointer(edit_text
, cur_pos
);
503 p
= g_utf8_prev_char(p
)) {
506 } else if (!in_quote
) {
507 if (!in_bracket
&& *p
== ',') {
509 } else if (*p
== '<')
516 /* have something valid */
517 if (g_utf8_strlen(p
, -1) == 0)
520 #define IS_VALID_CHAR(x) \
521 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
523 /* now scan back until we hit a valid character */
524 for (; *p
&& !IS_VALID_CHAR(*p
); p
= g_utf8_next_char(p
))
529 if (g_utf8_strlen(p
, -1) == 0)
532 if (start_pos
) *start_pos
= g_utf8_pointer_to_offset(edit_text
, p
);
539 static gchar
*get_complete_address_from_name_email(const gchar
*name
, const gchar
*email
)
541 gchar
*address
= NULL
;
542 if (!name
|| name
[0] == '\0')
543 address
= g_strdup_printf("<%s>", email
);
544 else if (strchr_with_skip_quote(name
, '"', ','))
545 address
= g_strdup_printf
546 ("\"%s\" <%s>", name
, email
);
548 address
= g_strdup_printf
549 ("%s <%s>", name
, email
);
554 * Replace an incompleted address with a completed one.
555 * \param entry Address entry field.
556 * \param newtext New text.
557 * \param start_pos Insertion point in entry field.
559 static void replace_address_in_edit(GtkEntry
*entry
, const gchar
*newtext
,
560 gint start_pos
, gboolean is_group
, GList
*grp_emails
)
562 if (!newtext
) return;
563 gtk_editable_delete_text(GTK_EDITABLE(entry
), start_pos
, -1);
565 gtk_editable_insert_text(GTK_EDITABLE(entry
), newtext
, strlen(newtext
),
568 gchar
*addresses
= NULL
;
569 GList
*cur
= grp_emails
;
570 for (; cur
; cur
= cur
->next
) {
572 ItemEMail
*email
= (ItemEMail
*)cur
->data
;
573 ItemPerson
*person
= ( ItemPerson
* ) ADDRITEM_PARENT(email
);
575 gchar
*addr
= get_complete_address_from_name_email(
576 ADDRITEM_NAME(person
), email
->address
);
578 tmp
= g_strdup_printf("%s, %s", addresses
, addr
);
580 tmp
= g_strdup_printf("%s", addr
);
585 gtk_editable_insert_text(GTK_EDITABLE(entry
), addresses
, strlen(addresses
),
589 gtk_editable_set_position(GTK_EDITABLE(entry
), -1);
593 * Attempt to complete an address, and returns the number of addresses found.
594 * Use <code>get_complete_address()</code> to get an entry from the index.
596 * \param str Search string to find.
597 * \return Zero if no match was found, otherwise the number of addresses; the
598 * original prefix (search string) will appear at index 0.
600 guint
complete_address(const gchar
*str
)
602 GList
*result
= NULL
;
606 completion_entry
*ce
= NULL
;
608 cm_return_val_if_fail(str
!= NULL
, 0);
610 /* g_completion is case sensitive */
611 d
= g_utf8_strdown(str
, -1);
613 clear_completion_cache();
614 g_completion_prefix
= g_strdup(str
);
616 result
= g_completion_complete(g_completion
, d
, NULL
);
618 count
= g_list_length(result
);
620 /* create list with unique addresses */
621 for (cpl
= 0, result
= g_list_first(result
);
623 result
= g_list_next(result
)) {
624 ce
= (completion_entry
*)(result
->data
);
625 if (NULL
== g_slist_find(g_completion_addresses
,
628 g_completion_addresses
=
629 g_slist_append(g_completion_addresses
,
633 count
= cpl
+ 1; /* index 0 is the original prefix */
634 g_completion_next
= 1; /* we start at the first completed one */
635 if (prefs_common
.address_search_wildcard
)
636 g_completion_addresses
= g_slist_sort(g_completion_addresses
,
637 addr_comparison_func
);
639 g_free(g_completion_prefix
);
640 g_completion_prefix
= NULL
;
643 g_completion_count
= count
;
651 * complete_matches_found() returns the number of matched addresses according
652 * to the completion mechanism. Unlike complete_address(), the returned value
653 * doesn't count str itself. If there's no match, it returns 0.
654 * To get a list of completion matches, see complete_address() instead.
656 guint
complete_matches_found(const gchar
*str
)
658 GList
*result
= NULL
;
661 cm_return_val_if_fail(str
!= NULL
, 0);
663 /* g_completion is case sensitive */
664 d
= g_utf8_strdown(str
, -1);
666 clear_completion_cache();
667 g_completion_prefix
= g_strdup(str
);
669 result
= g_completion_complete(g_completion
, d
, NULL
);
671 g_free(g_completion_prefix
);
674 return g_list_length(result
);
678 * Return a complete address from the index.
679 * \param index Index of entry that was found (by the previous call to
680 * <code>complete_address()</code>
681 * \return Completed address string; this should be freed when done.
683 gchar
*get_complete_address(gint index
)
685 const address_entry
*p
;
686 gchar
*address
= NULL
;
688 if (index
< g_completion_count
) {
690 address
= g_strdup(g_completion_prefix
);
692 /* get something from the unique addresses */
693 p
= (address_entry
*)g_slist_nth_data
694 (g_completion_addresses
, index
- 1);
695 if (p
!= NULL
&& p
->address
!= NULL
) {
696 address
= get_complete_address_from_name_email(p
->name
, p
->address
);
697 } else if (p
!= NULL
&& p
->address
== NULL
&& p
->name
!= NULL
) {
699 address
= g_strdup_printf("%s (%s) <!--___group___-->", p
->name
, _("Group"));
700 if (!_groupAddresses_
) {
701 _groupAddresses_
= g_hash_table_new(NULL
, g_direct_equal
);
703 if (!g_hash_table_lookup(_groupAddresses_
, GINT_TO_POINTER(g_str_hash(address
)))) {
704 g_hash_table_insert(_groupAddresses_
, GINT_TO_POINTER(g_str_hash(address
)), p
->grp_emails
);
715 * Return the next complete address match from the completion index.
716 * \return Completed address string; this should be freed when done.
718 static gchar
*get_next_complete_address(void)
720 if (is_completion_pending()) {
723 res
= get_complete_address(g_completion_next
);
724 g_completion_next
+= 1;
725 if (g_completion_next
>= g_completion_count
)
726 g_completion_next
= 0;
734 * Return a count of the completed matches in the completion index.
735 * \return Number of matched entries.
737 static guint
get_completion_count(void)
739 if (is_completion_pending())
740 return g_completion_count
;
746 * Invalidate address completion index. This function should be called whenever
747 * the address book changes. This forces data to be read into the completion
749 * \return Number of entries in index.
751 gint
invalidate_address_completion(void)
754 /* simply the same as start_address_completion() */
755 debug_print("Invalidation request for address completion\n");
756 read_address_book(completion_folder_path
);
757 clear_completion_cache();
760 return g_list_length(g_completion_list
);
764 * Finished with completion index. This function should be called after
765 * matching addresses.
766 * \return Reference count.
768 gint
end_address_completion(void)
770 gboolean different_folder
= FALSE
;
771 clear_completion_cache();
773 /* reset the folderpath to NULL */
774 if (completion_folder_path
) {
775 g_free(completion_folder_path
);
776 completion_folder_path
= NULL
;
777 different_folder
= TRUE
;
779 if (0 == --g_ref_count
)
782 debug_print("end_address_completion ref count %d\n", g_ref_count
);
783 if (g_ref_count
&& different_folder
) {
784 debug_print("still ref'd, different folder\n");
785 invalidate_address_completion();
794 static CompletionWindow
*_compWindow_
= NULL
;
797 * Mutex to protect callback from multiple threads.
799 static pthread_mutex_t _completionMutex_
= PTHREAD_MUTEX_INITIALIZER
;
802 * Completion queue list.
804 static GList
*_displayQueue_
= NULL
;
808 static gint _queryID_
= 0;
811 * Completion idle ID.
813 static guint _completionIdleID_
= 0;
816 * address completion entry ui. the ui (completion list was inspired by galeon's
817 * auto completion list). remaining things powered by claws's completion engine.
820 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
821 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
823 static void address_completion_mainwindow_set_focus (GtkWindow
*window
,
826 static gboolean
address_completion_entry_key_pressed (GtkEntry
*entry
,
829 static gboolean address_completion_complete_address_in_entry
832 static void address_completion_create_completion_window (GtkEntry
*entry
);
834 static gboolean completion_window_button_press
836 GdkEventButton
*event
,
837 CompletionWindow
*compWin
);
839 static gboolean completion_window_key_press
842 CompletionWindow
*compWin
);
843 static void address_completion_create_completion_window( GtkEntry
*entry_
);
846 * Create a completion window object.
847 * \return Initialized completion window.
849 static CompletionWindow
*addrcompl_create_window( void ) {
850 CompletionWindow
*cw
;
852 cw
= g_new0( CompletionWindow
, 1 );
854 cw
->searchTerm
= NULL
;
857 cw
->list_view
= NULL
;
858 cw
->in_mouse
= FALSE
;
859 cw
->destroying
= FALSE
;
865 * Destroy completion window.
866 * \param cw Window to destroy.
868 static void addrcompl_destroy_window( CompletionWindow
*cw
) {
872 display
= gdk_display_get_default();
873 seat
= gdk_display_get_default_seat(display
);
874 /* Stop all searches currently in progress */
875 #ifndef USE_ALT_ADDRBOOK
876 addrindex_stop_search( _queryID_
);
878 /* Remove idler function... or application may not terminate */
879 if( _completionIdleID_
!= 0 ) {
880 g_source_remove( _completionIdleID_
);
881 _completionIdleID_
= 0;
884 /* Now destroy window */
886 /* Clear references to widgets */
888 cw
->list_view
= NULL
;
892 gtk_widget_hide( cw
->window
);
893 gtk_widget_destroy( cw
->window
);
896 cw
->destroying
= FALSE
;
897 cw
->in_mouse
= FALSE
;
900 /* Re-enable keyboard, required at least for Gtk3/Win32 */
901 gdk_seat_ungrab(seat
);
905 * Free up completion window.
906 * \param cw Window to free.
908 static void addrcompl_free_window( CompletionWindow
*cw
) {
910 addrcompl_destroy_window( cw
);
912 g_free( cw
->searchTerm
);
913 cw
->searchTerm
= NULL
;
915 /* Clear references */
924 * Advance selection to previous/next item in list.
925 * \param list_view List to process.
926 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
929 static void completion_window_advance_selection(GtkTreeView
*list_view
, gboolean forward
)
931 GtkTreeSelection
*selection
;
935 cm_return_if_fail(list_view
!= NULL
);
937 selection
= gtk_tree_view_get_selection(list_view
);
938 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
942 forward
= gtk_tree_model_iter_next(model
, &iter
);
944 gtk_tree_selection_select_iter(selection
, &iter
);
948 prev
= gtk_tree_model_get_path(model
, &iter
);
952 if (gtk_tree_path_prev(prev
))
953 gtk_tree_selection_select_path(selection
, prev
);
955 gtk_tree_path_free(prev
);
960 * Resize window to accommodate maximum number of address entries.
961 * \param cw Completion window.
963 static void addrcompl_resize_window( CompletionWindow
*cw
) {
966 GdkGrabStatus status
;
969 gdk_window_get_position(gtk_widget_get_window(cw
->window
), &x
, &y
);
970 width
= gdk_window_get_width(gtk_widget_get_window(cw
->window
));
972 gtk_widget_queue_resize_no_redraw(cw
->list_view
);
973 gtk_widget_get_preferred_size(cw
->list_view
, &r
, NULL
);
975 gtk_widget_set_size_request(cw
->window
, width
, r
.height
);
977 display
= gdk_display_get_default();
978 status
= gdk_seat_grab(gdk_display_get_default_seat(display
),
979 gtk_widget_get_window(cw
->window
),
980 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK
|
981 GDK_BUTTON_RELEASE_MASK
,
982 TRUE
, NULL
, NULL
, NULL
, NULL
);
983 if (status
!= GDK_GRAB_SUCCESS
)
984 g_warning("gdk_seat_grab failed with status %d", status
);
985 gtk_grab_add(cw
->window
);
989 static GdkPixbuf
*group_pixbuf
= NULL
;
990 static GdkPixbuf
*email_pixbuf
= NULL
;
993 * Add an address the completion window address list.
994 * \param cw Completion window.
995 * \param address Address to add.
997 static void addrcompl_add_entry( CompletionWindow
*cw
, gchar
*address
) {
1000 GtkTreeSelection
*selection
;
1001 gboolean is_group
= FALSE
;
1002 GList
*grp_emails
= NULL
;
1003 store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw
->list_view
)));
1006 if (!group_pixbuf
) {
1007 stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_TWO
, &group_pixbuf
);
1008 g_object_ref(G_OBJECT(group_pixbuf
));
1010 if (!email_pixbuf
) {
1011 stock_pixbuf_gdk(STOCK_PIXMAP_ADDR_ONE
, &email_pixbuf
);
1012 g_object_ref(G_OBJECT(email_pixbuf
));
1014 /* g_print( "\t\tAdding :%s\n", address ); */
1015 if (strstr(address
, " <!--___group___-->")) {
1017 if (_groupAddresses_
)
1018 grp_emails
= g_hash_table_lookup(_groupAddresses_
, GINT_TO_POINTER(g_str_hash(address
)));
1019 *(strstr(address
, " <!--___group___-->")) = '\0';
1020 pixbuf
= group_pixbuf
;
1021 } else if (strchr(address
, '@') && strchr(address
, '<') &&
1022 strchr(address
, '>')) {
1023 pixbuf
= email_pixbuf
;
1027 if (is_group
&& !_allowCommas_
)
1029 gtk_list_store_append(store
, &iter
);
1030 gtk_list_store_set(store
, &iter
,
1031 ADDR_COMPL_ICON
, pixbuf
,
1032 ADDR_COMPL_ADDRESS
, address
,
1033 ADDR_COMPL_ISGROUP
, is_group
,
1034 ADDR_COMPL_GROUPLIST
, grp_emails
,
1039 addrcompl_resize_window( cw
);
1040 gtk_grab_add( cw
->window
);
1042 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(cw
->list_view
));
1043 if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
))
1046 if (cw
->listCount
== 1) {
1047 /* Select first row for now */
1048 gtk_tree_selection_select_iter(selection
, &iter
);
1050 #ifndef GENERIC_UMPC
1051 else if (cw
->listCount
== 2) {
1052 if (gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
)) {
1053 /* Move off first row */
1054 gtk_tree_selection_select_iter(selection
, &iter
);
1060 void addrcompl_reflect_prefs_pixmap_theme(void) {
1062 g_object_unref(G_OBJECT(group_pixbuf
));
1063 group_pixbuf
= NULL
;
1066 g_object_unref(G_OBJECT(email_pixbuf
));
1067 email_pixbuf
= NULL
;
1072 * Completion idle function. This function is called by the main (UI) thread
1073 * during UI idle time while an address search is in progress. Items from the
1074 * display queue are processed and appended to the address list.
1076 * \param data Target completion window to receive email addresses.
1077 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
1079 static gboolean
addrcompl_idle( gpointer data
) {
1083 /* Process all entries in display queue */
1084 pthread_mutex_lock( & _completionMutex_
);
1085 if( _displayQueue_
) {
1086 node
= _displayQueue_
;
1087 node
= g_list_next(node
); /* skip search term */
1089 address
= node
->data
;
1090 /* g_print( "address ::: %s :::\n", address ); */
1091 addrcompl_add_entry( _compWindow_
, address
);
1093 node
= g_list_next( node
);
1095 g_list_free( _displayQueue_
);
1096 _displayQueue_
= NULL
;
1098 pthread_mutex_unlock( & _completionMutex_
);
1105 * Callback entry point. The background thread (if any) appends the address
1106 * list to the display queue.
1107 * \param sender Sender of query.
1108 * \param queryID Query ID of search request.
1109 * \param listEMail List of zero of more email objects that met search
1111 * \param data Query data.
1113 #ifndef USE_ALT_ADDRBOOK
1114 static gint
addrcompl_callback_entry(
1115 gpointer sender
, gint queryID
, GList
*listEMail
, gpointer data
)
1120 /* g_print( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
1121 pthread_mutex_lock( & _completionMutex_
);
1122 if( queryID
== _queryID_
) {
1123 /* Append contents to end of display queue */
1126 ItemEMail
*email
= node
->data
;
1128 address
= addritem_format_email( email
);
1129 /* g_print( "\temail/address ::%s::\n", address ); */
1130 _displayQueue_
= g_list_append( _displayQueue_
, address
);
1131 node
= g_list_next( node
);
1134 g_list_free( listEMail
);
1135 pthread_mutex_unlock( & _completionMutex_
);
1142 * Clear the display queue.
1144 static void addrcompl_clear_queue( void ) {
1145 /* Clear out display queue */
1146 pthread_mutex_lock( & _completionMutex_
);
1148 g_list_free_full( _displayQueue_
, g_free
);
1149 _displayQueue_
= NULL
;
1151 pthread_mutex_unlock( & _completionMutex_
);
1155 * Add a single address entry into the display queue.
1156 * \param address Address to append.
1158 static void addrcompl_add_queue( gchar
*address
) {
1159 pthread_mutex_lock( & _completionMutex_
);
1160 _displayQueue_
= g_list_append( _displayQueue_
, address
);
1161 pthread_mutex_unlock( & _completionMutex_
);
1165 * Load list with entries from local completion index.
1167 static void addrcompl_load_local( void ) {
1170 for (count
= 0; count
< get_completion_count(); count
++) {
1173 address
= get_complete_address( count
);
1174 /* g_print( "\taddress ::%s::\n", address ); */
1176 /* Append contents to end of display queue */
1177 addrcompl_add_queue( address
);
1184 static void addrcompl_start_search( void ) {
1185 #ifndef USE_ALT_ADDRBOOK
1188 searchTerm
= g_strdup( _compWindow_
->searchTerm
);
1190 /* Setup the search */
1191 _queryID_
= addrindex_setup_search(
1192 searchTerm
, NULL
, addrcompl_callback_entry
);
1193 g_free( searchTerm
);
1195 /* g_print( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1197 /* Load local stuff */
1198 addrcompl_load_local();
1200 /* Sit back and wait until something happens */
1201 _completionIdleID_
=
1202 g_idle_add( (GSourceFunc
) addrcompl_idle
, NULL
);
1203 /* g_print( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1205 #ifndef USE_ALT_ADDRBOOK
1206 addrindex_start_search( _queryID_
);
1213 * Apply the current selection in the list to the entry field. Focus is also
1214 * moved to the next widget so that Tab key works correctly.
1215 * \param list_view List to process.
1216 * \param entry Address entry field.
1217 * \param move_focus Move focus to the next widget ?
1219 static void completion_window_apply_selection(GtkTreeView
*list_view
,
1221 gboolean move_focus
)
1223 gchar
*address
= NULL
, *text
= NULL
;
1226 GtkTreeSelection
*selection
;
1227 GtkTreeModel
*model
;
1229 gboolean is_group
= FALSE
;
1230 cm_return_if_fail(list_view
!= NULL
);
1231 cm_return_if_fail(entry
!= NULL
);
1232 GList
*grp_emails
= NULL
;
1234 selection
= gtk_tree_view_get_selection(list_view
);
1235 if (!gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1238 /* First remove the idler */
1239 if( _completionIdleID_
!= 0 ) {
1240 g_source_remove( _completionIdleID_
);
1241 _completionIdleID_
= 0;
1244 /* Process selected item */
1245 gtk_tree_model_get(model
, &iter
, ADDR_COMPL_ADDRESS
, &text
,
1246 ADDR_COMPL_ISGROUP
, &is_group
,
1247 ADDR_COMPL_GROUPLIST
, &grp_emails
,
1250 address
= get_address_from_edit(entry
, &cursor_pos
);
1252 replace_address_in_edit(entry
, text
, cursor_pos
, is_group
, grp_emails
);
1255 /* Move focus to next widget */
1256 parent
= gtk_widget_get_parent(GTK_WIDGET(entry
));
1257 if( parent
&& move_focus
) {
1258 gtk_widget_child_focus( parent
, GTK_DIR_TAB_FORWARD
);
1263 * Start address completion. Should be called when creating the main window
1264 * containing address completion entries.
1265 * \param mainwindow Main window.
1267 void address_completion_start(GtkWidget
*mainwindow
)
1269 start_address_completion(NULL
);
1270 set_match_any_part(TRUE
);
1272 /* register focus change hook */
1273 g_signal_connect(G_OBJECT(mainwindow
), "set_focus",
1274 G_CALLBACK(address_completion_mainwindow_set_focus
),
1279 * Need unique data to make unregistering signal handler possible for the auto
1282 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1285 * Register specified entry widget for address completion.
1286 * \param entry Address entry field.
1288 void address_completion_register_entry(GtkEntry
*entry
, gboolean allow_commas
)
1290 cm_return_if_fail(entry
!= NULL
);
1291 cm_return_if_fail(GTK_IS_ENTRY(entry
));
1293 /* add hooked property */
1294 g_object_set_data(G_OBJECT(entry
), ENTRY_DATA_TAB_HOOK
, entry
);
1295 g_object_set_data(G_OBJECT(entry
), ENTRY_DATA_ALLOW_COMMAS
, GINT_TO_POINTER(allow_commas
));
1297 /* add keypress event */
1298 g_signal_connect_closure
1299 (G_OBJECT(entry
), "key_press_event",
1300 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed
),
1301 COMPLETION_UNIQUE_DATA
,
1307 * Unregister specified entry widget from address completion operations.
1308 * \param entry Address entry field.
1310 void address_completion_unregister_entry(GtkEntry
*entry
)
1314 cm_return_if_fail(entry
!= NULL
);
1315 cm_return_if_fail(GTK_IS_ENTRY(entry
));
1317 entry_obj
= g_object_get_data(G_OBJECT(entry
), ENTRY_DATA_TAB_HOOK
);
1318 cm_return_if_fail(entry_obj
);
1319 cm_return_if_fail(G_OBJECT(entry_obj
) == G_OBJECT(entry
));
1321 /* has the hooked property? */
1322 g_object_set_data(G_OBJECT(entry
), ENTRY_DATA_TAB_HOOK
, NULL
);
1324 /* remove the hook */
1325 g_signal_handlers_disconnect_by_func(G_OBJECT(entry
),
1326 G_CALLBACK(address_completion_entry_key_pressed
),
1327 COMPLETION_UNIQUE_DATA
);
1331 * End address completion. Should be called when main window with address
1332 * completion entries terminates. NOTE: this function assumes that it is
1333 * called upon destruction of the window.
1334 * \param mainwindow Main window.
1336 void address_completion_end(GtkWidget
*mainwindow
)
1338 /* if address_completion_end() is really called on closing the window,
1339 * we don't need to unregister the set_focus_cb */
1340 end_address_completion();
1343 /* if focus changes to another entry, then clear completion cache */
1344 static void address_completion_mainwindow_set_focus(GtkWindow
*window
,
1349 if (widget
&& GTK_IS_ENTRY(widget
) &&
1350 g_object_get_data(G_OBJECT(widget
), ENTRY_DATA_TAB_HOOK
)) {
1351 _allowCommas_
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget
), ENTRY_DATA_ALLOW_COMMAS
));
1352 clear_completion_cache();
1357 * Listener that watches for tab or other keystroke in address entry field.
1358 * \param entry Address entry field.
1359 * \param ev Event object.
1360 * \param data User data.
1361 * \return <i>TRUE</i>.
1363 static gboolean
address_completion_entry_key_pressed(GtkEntry
*entry
,
1367 if (ev
->keyval
== GDK_KEY_Tab
) {
1368 addrcompl_clear_queue();
1369 _allowCommas_
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry
), ENTRY_DATA_ALLOW_COMMAS
));
1370 if( address_completion_complete_address_in_entry( entry
, TRUE
) ) {
1371 /* route a void character to the default handler */
1372 /* this is a dirty hack; we're actually changing a key
1373 * reported by the system. */
1374 ev
->keyval
= GDK_KEY_AudibleBell_Enable
;
1375 ev
->state
&= ~GDK_SHIFT_MASK
;
1378 address_completion_create_completion_window(entry
);
1380 /* Start remote queries */
1381 addrcompl_start_search();
1388 } else if (ev
->keyval
== GDK_KEY_Shift_L
1389 || ev
->keyval
== GDK_KEY_Shift_R
1390 || ev
->keyval
== GDK_KEY_Control_L
1391 || ev
->keyval
== GDK_KEY_Control_R
1392 || ev
->keyval
== GDK_KEY_Caps_Lock
1393 || ev
->keyval
== GDK_KEY_Shift_Lock
1394 || ev
->keyval
== GDK_KEY_Meta_L
1395 || ev
->keyval
== GDK_KEY_Meta_R
1396 || ev
->keyval
== GDK_KEY_Alt_L
1397 || ev
->keyval
== GDK_KEY_Alt_R
) {
1398 /* these buttons should not clear the cache... */
1400 clear_completion_cache();
1405 * Initialize search term for address completion.
1406 * \param entry Address entry field.
1408 static gboolean
address_completion_complete_address_in_entry(GtkEntry
*entry
,
1411 gint ncount
, cursor_pos
;
1412 gchar
*searchTerm
, *new = NULL
;
1414 cm_return_val_if_fail(entry
!= NULL
, FALSE
);
1416 if (!gtk_widget_has_focus(GTK_WIDGET(entry
))) return FALSE
;
1418 /* get an address component from the cursor */
1419 searchTerm
= get_address_from_edit( entry
, &cursor_pos
);
1420 if( ! searchTerm
) return FALSE
;
1421 /* g_print( "search for :::%s:::\n", searchTerm ); */
1423 /* Clear any existing search */
1424 g_free( _compWindow_
->searchTerm
);
1425 _compWindow_
->searchTerm
= g_strdup( searchTerm
);
1427 /* Perform search on local completion index */
1428 ncount
= complete_address( searchTerm
);
1430 new = get_next_complete_address();
1433 #if (!defined(USE_LDAP) && !defined(GENERIC_UMPC))
1434 /* Select the address if there is only one match */
1436 /* Display selected address in entry field */
1437 gchar
*addr
= get_complete_address(1);
1438 if (addr
&& !strstr(addr
, " <!--___group___-->")) {
1439 replace_address_in_edit(entry
, addr
, cursor_pos
, FALSE
, NULL
);
1440 /* Discard the window */
1441 clear_completion_cache();
1445 /* Make sure that drop-down appears uniform! */
1449 addrcompl_add_queue( searchTerm
);
1451 g_free( searchTerm
);
1458 * Create new address completion window for specified entry.
1459 * \param entry_ Entry widget to associate with window.
1461 static void address_completion_create_completion_window( GtkEntry
*entry_
)
1465 GtkWidget
*scroll
, *list_view
;
1466 GdkGrabStatus status
;
1467 GdkDisplay
*display
;
1470 GtkWidget
*entry
= GTK_WIDGET(entry_
);
1472 /* Create new window and list */
1473 window
= gtk_window_new(GTK_WINDOW_POPUP
);
1474 list_view
= addr_compl_list_view_create(_compWindow_
);
1476 /* Destroy any existing window */
1477 addrcompl_destroy_window( _compWindow_
);
1479 /* Create new object */
1480 _compWindow_
->window
= window
;
1481 _compWindow_
->entry
= entry
;
1482 _compWindow_
->list_view
= list_view
;
1483 _compWindow_
->listCount
= 0;
1484 _compWindow_
->in_mouse
= FALSE
;
1486 scroll
= gtk_scrolled_window_new(NULL
, NULL
);
1487 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll
),
1488 GTK_POLICY_NEVER
, GTK_POLICY_AUTOMATIC
);
1489 gtk_container_add(GTK_CONTAINER(window
), scroll
);
1490 gtk_container_add(GTK_CONTAINER(scroll
), list_view
);
1491 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll
),
1494 /* Use entry widget to position initial window */
1495 gtk_widget_get_allocation(entry
, &rect
);
1497 /* rect.x and rect.y are relative to parent of our GtkEntry,
1498 * we need to convert them to absolute coordinates */
1499 gdk_window_get_root_coords(
1500 gtk_widget_get_window(gtk_widget_get_parent(entry
)),
1501 rect
.x
, rect
.y
, &x
, &y
);
1503 /* Move the window to just below the GtkEntry */
1504 gtk_window_move(GTK_WINDOW(window
), x
, y
+ rect
.height
);
1506 /* Resize window to fit initial (empty) address list */
1507 gtk_widget_get_preferred_size(list_view
, &r
, NULL
);
1508 gtk_widget_set_size_request(window
, rect
.width
, r
.height
);
1509 gtk_widget_show_all(window
);
1511 /* Setup handlers */
1512 g_signal_connect(G_OBJECT(list_view
), "button_press_event",
1513 G_CALLBACK(list_view_button_press
),
1516 g_signal_connect(G_OBJECT(list_view
), "button_release_event",
1517 G_CALLBACK(list_view_button_release
),
1520 g_signal_connect(G_OBJECT(window
),
1521 "button-press-event",
1522 G_CALLBACK(completion_window_button_press
),
1524 g_signal_connect(G_OBJECT(window
),
1526 G_CALLBACK(completion_window_key_press
),
1528 display
= gdk_display_get_default();
1529 status
= gdk_seat_grab(gdk_display_get_default_seat(display
),
1530 gtk_widget_get_window(window
),
1531 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK
|
1532 GDK_BUTTON_RELEASE_MASK
,
1533 TRUE
, NULL
, NULL
, NULL
, NULL
);
1534 if (status
!= GDK_GRAB_SUCCESS
)
1535 g_warning("gdk_seat_grab failed with status %d", status
);
1536 gtk_grab_add( window
);
1540 * Respond to button press in completion window. Check if mouse click is
1541 * anywhere outside the completion window. In that case the completion
1542 * window is destroyed, and the original searchTerm is restored.
1544 * \param widget Window object.
1545 * \param event Event.
1546 * \param compWin Reference to completion window.
1548 static gboolean
completion_window_button_press(GtkWidget
*widget
,
1549 GdkEventButton
*event
,
1550 CompletionWindow
*compWin
)
1552 GtkWidget
*event_widget
, *entry
;
1555 gboolean restore
= TRUE
;
1557 cm_return_val_if_fail(compWin
!= NULL
, FALSE
);
1559 entry
= compWin
->entry
;
1560 cm_return_val_if_fail(entry
!= NULL
, FALSE
);
1562 /* Test where mouse was clicked */
1563 event_widget
= gtk_get_event_widget((GdkEvent
*)event
);
1564 if (event_widget
!= widget
) {
1565 while (event_widget
) {
1566 if (event_widget
== widget
)
1568 else if (event_widget
== entry
) {
1572 event_widget
= gtk_widget_get_parent(event_widget
);
1577 /* Clicked outside of completion window - restore */
1578 searchTerm
= _compWindow_
->searchTerm
;
1579 g_free(get_address_from_edit(GTK_ENTRY(entry
), &cursor_pos
));
1580 replace_address_in_edit(GTK_ENTRY(entry
), searchTerm
, cursor_pos
, FALSE
, NULL
);
1583 clear_completion_cache();
1584 addrcompl_destroy_window( _compWindow_
);
1590 * Respond to key press in completion window.
1591 * \param widget Window object.
1592 * \param event Event.
1593 * \param compWind Reference to completion window.
1595 static gboolean
completion_window_key_press(GtkWidget
*widget
,
1597 CompletionWindow
*compWin
)
1599 GdkEventKey tmp_event
;
1603 GtkWidget
*list_view
;
1605 cm_return_val_if_fail(compWin
!= NULL
, FALSE
);
1607 entry
= compWin
->entry
;
1608 list_view
= compWin
->list_view
;
1609 cm_return_val_if_fail(entry
!= NULL
, FALSE
);
1611 /* allow keyboard navigation in the alternatives tree view */
1612 if (event
->keyval
== GDK_KEY_Up
|| event
->keyval
== GDK_KEY_Down
||
1613 event
->keyval
== GDK_KEY_Page_Up
|| event
->keyval
== GDK_KEY_Page_Down
) {
1614 completion_window_advance_selection
1615 (GTK_TREE_VIEW(list_view
),
1616 event
->keyval
== GDK_KEY_Down
||
1617 event
->keyval
== GDK_KEY_Page_Down
? TRUE
: FALSE
);
1621 /* make tab move to next field */
1622 if( event
->keyval
== GDK_KEY_Tab
) {
1623 /* Reference to parent */
1624 parent
= gtk_widget_get_parent(GTK_WIDGET(entry
));
1626 /* Discard the window */
1627 clear_completion_cache();
1628 addrcompl_destroy_window( _compWindow_
);
1630 /* Move focus to next widget */
1632 gtk_widget_child_focus( parent
, GTK_DIR_TAB_FORWARD
);
1637 /* make backtab move to previous field */
1638 if( event
->keyval
== GDK_KEY_ISO_Left_Tab
) {
1639 /* Reference to parent */
1640 parent
= gtk_widget_get_parent(GTK_WIDGET(entry
));
1642 /* Discard the window */
1643 clear_completion_cache();
1644 addrcompl_destroy_window( _compWindow_
);
1646 /* Move focus to previous widget */
1648 gtk_widget_child_focus( parent
, GTK_DIR_TAB_BACKWARD
);
1652 _allowCommas_
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry
), ENTRY_DATA_ALLOW_COMMAS
));
1654 /* look for presses that accept the selection */
1655 if (event
->keyval
== GDK_KEY_Return
|| event
->keyval
== GDK_KEY_space
||
1656 event
->keyval
== GDK_KEY_KP_Enter
||
1657 (_allowCommas_
&& event
->keyval
== GDK_KEY_comma
)) {
1658 /* User selected address with a key press */
1660 /* Display selected address in entry field */
1661 completion_window_apply_selection(
1662 GTK_TREE_VIEW(list_view
), GTK_ENTRY(entry
),
1663 event
->keyval
!= GDK_KEY_comma
);
1665 if (event
->keyval
== GDK_KEY_comma
) {
1666 gint pos
= gtk_editable_get_position(GTK_EDITABLE(entry
));
1667 gtk_editable_insert_text(GTK_EDITABLE(entry
), ", ", 2, &pos
);
1668 gtk_editable_set_position(GTK_EDITABLE(entry
), pos
+ 1);
1671 /* Discard the window */
1672 clear_completion_cache();
1673 addrcompl_destroy_window( _compWindow_
);
1677 /* key state keys should never be handled */
1678 if (event
->keyval
== GDK_KEY_Shift_L
1679 || event
->keyval
== GDK_KEY_Shift_R
1680 || event
->keyval
== GDK_KEY_Control_L
1681 || event
->keyval
== GDK_KEY_Control_R
1682 || event
->keyval
== GDK_KEY_Caps_Lock
1683 || event
->keyval
== GDK_KEY_Shift_Lock
1684 || event
->keyval
== GDK_KEY_Meta_L
1685 || event
->keyval
== GDK_KEY_Meta_R
1686 || event
->keyval
== GDK_KEY_Alt_L
1687 || event
->keyval
== GDK_KEY_Alt_R
) {
1691 /* some other key, let's restore the searchTerm (orignal text) */
1692 searchTerm
= _compWindow_
->searchTerm
;
1693 g_free(get_address_from_edit(GTK_ENTRY(entry
), &cursor_pos
));
1694 replace_address_in_edit(GTK_ENTRY(entry
), searchTerm
, cursor_pos
, FALSE
, NULL
);
1696 /* make sure anything we typed comes in the edit box */
1697 tmp_event
.type
= event
->type
;
1698 tmp_event
.window
= gtk_widget_get_window(GTK_WIDGET(entry
));
1699 tmp_event
.send_event
= TRUE
;
1700 tmp_event
.time
= event
->time
;
1701 tmp_event
.state
= event
->state
;
1702 tmp_event
.keyval
= event
->keyval
;
1703 tmp_event
.length
= event
->length
;
1704 tmp_event
.string
= event
->string
;
1705 gtk_widget_event(entry
, (GdkEvent
*)&tmp_event
);
1707 /* and close the completion window */
1708 clear_completion_cache();
1709 addrcompl_destroy_window( _compWindow_
);
1715 * ============================================================================
1716 * Publically accessible functions.
1717 * ============================================================================
1721 * Setup completion object.
1723 void addrcompl_initialize( void ) {
1724 /* g_print( "addrcompl_initialize...\n" ); */
1725 if( ! _compWindow_
) {
1726 _compWindow_
= addrcompl_create_window();
1729 _completionIdleID_
= 0;
1730 /* g_print( "addrcompl_initialize...done\n" ); */
1734 * Teardown completion object.
1736 void addrcompl_teardown( void ) {
1737 /* g_print( "addrcompl_teardown...\n" ); */
1738 addrcompl_free_window( _compWindow_
);
1739 _compWindow_
= NULL
;
1741 addrcompl_clear_queue();
1743 _completionIdleID_
= 0;
1744 /* g_print( "addrcompl_teardown...done\n" ); */
1748 * tree view functions
1751 static GtkListStore
*addr_compl_create_store(void)
1753 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS
,
1761 static GtkWidget
*addr_compl_list_view_create(CompletionWindow
*window
)
1763 GtkTreeView
*list_view
;
1764 GtkTreeSelection
*selector
;
1765 GtkTreeModel
*model
;
1767 model
= GTK_TREE_MODEL(addr_compl_create_store());
1768 list_view
= GTK_TREE_VIEW(gtk_tree_view_new_with_model(model
));
1769 g_object_unref(model
);
1771 gtk_tree_view_set_rules_hint(list_view
, prefs_common
.use_stripes_everywhere
);
1772 gtk_tree_view_set_headers_visible(list_view
, FALSE
);
1774 selector
= gtk_tree_view_get_selection(list_view
);
1775 gtk_tree_selection_set_mode(selector
, GTK_SELECTION_BROWSE
);
1776 gtk_tree_selection_set_select_function(selector
, addr_compl_selected
,
1779 /* create the columns */
1780 addr_compl_create_list_view_columns(GTK_WIDGET(list_view
));
1782 return GTK_WIDGET(list_view
);
1785 static void addr_compl_create_list_view_columns(GtkWidget
*list_view
)
1787 GtkTreeViewColumn
*column
;
1788 GtkCellRenderer
*renderer
;
1790 renderer
= gtk_cell_renderer_pixbuf_new();
1791 column
= gtk_tree_view_column_new_with_attributes
1793 "pixbuf", ADDR_COMPL_ICON
, NULL
);
1794 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1795 renderer
= gtk_cell_renderer_text_new();
1796 column
= gtk_tree_view_column_new_with_attributes
1797 ("", renderer
, "text", ADDR_COMPL_ADDRESS
, NULL
);
1798 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view
), column
);
1801 static gboolean
list_view_button_press(GtkWidget
*widget
, GdkEventButton
*event
,
1802 CompletionWindow
*window
)
1804 if (window
&& event
&& event
->type
== GDK_BUTTON_PRESS
) {
1805 window
->in_mouse
= TRUE
;
1810 static gboolean
list_view_button_release(GtkWidget
*widget
, GdkEventButton
*event
,
1811 CompletionWindow
*window
)
1813 if (window
&& event
&& event
->type
== GDK_BUTTON_RELEASE
) {
1814 window
->in_mouse
= FALSE
;
1819 static gboolean
addr_compl_selected(GtkTreeSelection
*selector
,
1820 GtkTreeModel
*model
,
1822 gboolean currently_selected
,
1825 CompletionWindow
*window
= data
;
1827 if (currently_selected
)
1830 if (!window
->in_mouse
)
1833 /* XXX: select the entry and kill window later... select is called before
1834 * any other mouse events handlers including the tree view internal one;
1835 * not using a time out would result in a crash. if this doesn't work
1836 * safely, maybe we should set variables when receiving button presses
1837 * in the tree view. */
1838 if (!window
->destroying
) {
1839 window
->destroying
= TRUE
;
1840 g_idle_add((GSourceFunc
) addr_compl_defer_select_destruct
, data
);
1846 static gboolean
addr_compl_defer_select_destruct(CompletionWindow
*window
)
1848 GtkEntry
*entry
= GTK_ENTRY(window
->entry
);
1850 completion_window_apply_selection(GTK_TREE_VIEW(window
->list_view
),
1853 clear_completion_cache();
1855 addrcompl_destroy_window(window
);
1859 gboolean
found_in_addressbook(const gchar
*address
)
1862 gboolean found
= FALSE
;
1868 addr
= g_strdup(address
);
1869 extract_address(addr
);
1870 num_addr
= complete_address(addr
);
1872 /* skip first item (this is the search string itself) */
1874 for (; i
< num_addr
&& !found
; i
++) {
1875 gchar
*caddr
= get_complete_address(i
);
1876 extract_address(caddr
);
1877 if (strcasecmp(caddr
, addr
) == 0)