2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2006 by Alfons Hoogervorst & The Sylpheed Claws 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkmain.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkentry.h>
32 #include <gtk/gtkeditable.h>
33 #include <gtk/gtkscrolledwindow.h>
34 #include <gtk/gtktreeview.h>
35 #include <gtk/gtktreemodel.h>
36 #include <gtk/gtkliststore.h>
40 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
45 #include "addrindex.h"
46 #include "addr_compl.h"
48 #include "prefs_common.h"
53 *\brief For the GtkListStore
63 * The address book is read into memory. We set up an address list
64 * containing all address book entries. Next we make the completion
65 * list, which contains all the completable strings, and store a
66 * reference to the address entry it belongs to.
67 * After calling the g_completion_complete(), we get a reference
68 * to a valid email address.
70 * Completion is very simplified. We never complete on another prefix,
71 * i.e. we neglect the next smallest possible prefix for the current
72 * completion cache. This is simply done so we might break up the
73 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
74 * something like alfons, proteus, demon, nl; and then completing on
75 * any of those words).
79 * address_entry - structure which refers to the original address entry in the
89 * completion_entry - structure used to complete addresses, with a reference
90 * the the real address information.
94 gchar *string; /* string to complete */
95 address_entry *ref; /* address the string belongs to */
98 /*******************************************************************************/
100 static gint g_ref_count; /* list ref count */
101 static GList *g_completion_list; /* list of strings to be checked */
102 static GList *g_address_list; /* address storage */
103 static GCompletion *g_completion; /* completion object */
105 /* To allow for continuing completion we have to keep track of the state
106 * using the following variables. No need to create a context object. */
108 static gint g_completion_count; /* nr of addresses incl. the prefix */
109 static gint g_completion_next; /* next prev address */
110 static GSList *g_completion_addresses; /* unique addresses found in the
112 static gchar *g_completion_prefix; /* last prefix. (this is cached here
113 * because the prefix passed to g_completion
114 * is g_strdown()'ed */
116 static gchar *completion_folder_path = NULL;
118 /*******************************************************************************/
121 * Define the structure of the completion window.
123 typedef struct _CompletionWindow CompletionWindow;
124 struct _CompletionWindow {
129 GtkWidget *list_view;
131 gboolean in_mouse; /*!< mouse press pending... */
132 gboolean destroying; /*!< destruction in progress */
135 static GtkListStore *addr_compl_create_store (void);
137 static GtkWidget *addr_compl_list_view_create (CompletionWindow *window);
139 static void addr_compl_create_list_view_columns (GtkWidget *list_view);
141 static gboolean list_view_button_press (GtkWidget *widget,
142 GdkEventButton *event,
143 CompletionWindow *window);
145 static gboolean list_view_button_release (GtkWidget *widget,
146 GdkEventButton *event,
147 CompletionWindow *window);
149 static gboolean addr_compl_selected (GtkTreeSelection *selector,
152 gboolean currently_selected,
155 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window);
158 * Function used by GTK to find the string data to be used for completion.
159 * \param data Pointer to data being processed.
161 static gchar *completion_func(gpointer data)
163 g_return_val_if_fail(data != NULL, NULL);
165 return ((completion_entry *)data)->string;
169 * Initialize all completion index data.
171 static void init_all(void)
173 g_completion = g_completion_new(completion_func);
174 g_return_if_fail(g_completion != NULL);
178 * Free up all completion index data.
180 static void free_all(void)
184 walk = g_list_first(g_completion_list);
185 for (; walk != NULL; walk = g_list_next(walk)) {
186 completion_entry *ce = (completion_entry *) walk->data;
190 g_list_free(g_completion_list);
191 g_completion_list = NULL;
193 walk = g_address_list;
194 for (; walk != NULL; walk = g_list_next(walk)) {
195 address_entry *ae = (address_entry *) walk->data;
200 g_list_free(g_address_list);
201 g_address_list = NULL;
203 g_completion_free(g_completion);
208 * Append specified address entry to the index.
209 * \param str Index string value.
210 * \param ae Entry containing address data.
212 static void add_address1(const char *str, address_entry *ae)
214 completion_entry *ce1;
215 ce1 = g_new0(completion_entry, 1),
216 /* GCompletion list is case sensitive */
217 ce1->string = g_utf8_strdown(str, -1);
220 g_completion_list = g_list_prepend(g_completion_list, ce1);
224 * Adds address to the completion list. This function looks complicated, but
225 * it's only allocation checks. Each value will be included in the index.
226 * \param name Recipient name.
227 * \param address EMail address.
228 * \param alias Alias to append.
229 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
232 static gint add_address(const gchar *name, const gchar *address,
233 const gchar *nick, const gchar *alias)
237 if (!name || !address) return -1;
239 ae = g_new0(address_entry, 1);
241 g_return_val_if_fail(ae != NULL, -1);
243 ae->name = g_strdup(name);
244 ae->address = g_strdup(address);
246 g_address_list = g_list_prepend(g_address_list, ae);
248 add_address1(name, ae);
249 add_address1(address, ae);
252 add_address1(nick, ae);
254 if ( alias != NULL ) {
255 add_address1(alias, ae);
262 * Read address book, creating all entries in the completion index.
264 static void read_address_book(gchar *folderpath) {
265 addrindex_load_completion( add_address, folderpath );
266 g_address_list = g_list_reverse(g_address_list);
267 g_completion_list = g_list_reverse(g_completion_list);
271 * Test whether there is a completion pending.
272 * \return <code>TRUE</code> if pending.
274 static gboolean is_completion_pending(void)
276 /* check if completion pending, i.e. we might satisfy a request for the next
277 * or previous address */
278 return g_completion_count;
282 * Clear the completion cache.
284 static void clear_completion_cache(void)
286 if (is_completion_pending()) {
287 g_free(g_completion_prefix);
289 if (g_completion_addresses) {
290 g_slist_free(g_completion_addresses);
291 g_completion_addresses = NULL;
294 g_completion_count = g_completion_next = 0;
299 * Prepare completion index. This function should be called prior to attempting
300 * address completion.
301 * \return The number of addresses in the completion list.
303 gint start_address_completion(gchar *folderpath)
305 clear_completion_cache();
307 if ((completion_folder_path == NULL && folderpath != NULL) ||
308 (completion_folder_path != NULL && folderpath == NULL) ||
309 (completion_folder_path != NULL && folderpath != NULL &&
310 strcmp(completion_folder_path, folderpath) != 0)) {
312 debug_print("start_address_completion: resetting\n");
314 /* TODO: wwp: optimize: only reset when the new folderpath is MORE restrictive than the old one
315 (the most easy case is when folderpath is NULL and completion_folder_path is != NULL */
322 g_free(completion_folder_path);
323 if (folderpath != NULL)
324 completion_folder_path = g_strdup(folderpath);
326 completion_folder_path = NULL;
330 /* open the address book */
331 read_address_book(folderpath);
332 /* merge the completion entry list into g_completion */
333 if (g_completion_list)
334 g_completion_add_items(g_completion, g_completion_list);
337 debug_print("start_address_completion(%s) ref count %d\n",
338 folderpath, g_ref_count);
340 return g_list_length(g_completion_list);
344 * Retrieve a possible address (or a part) from an entry box. To make life
345 * easier, we only look at the last valid address component; address
346 * completion only works at the last string component in the entry box.
348 * \param entry Address entry field.
349 * \param start_pos Address of start position of address.
350 * \return Possible address.
352 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
354 const gchar *edit_text, *p;
356 gboolean in_quote = FALSE;
357 gboolean in_bracket = FALSE;
360 edit_text = gtk_entry_get_text(entry);
361 if (edit_text == NULL) return NULL;
363 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
365 /* scan for a separator. doesn't matter if walk points at null byte. */
366 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
368 p = g_utf8_prev_char(p)) {
371 } else if (!in_quote) {
372 if (!in_bracket && *p == ',') {
374 } else if (*p == '<')
381 /* have something valid */
382 if (g_utf8_strlen(p, -1) == 0)
385 #define IS_VALID_CHAR(x) \
386 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
388 /* now scan back until we hit a valid character */
389 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
394 if (g_utf8_strlen(p, -1) == 0)
397 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
405 * Replace an incompleted address with a completed one.
406 * \param entry Address entry field.
407 * \param newtext New text.
408 * \param start_pos Insertion point in entry field.
410 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
413 if (!newtext) return;
414 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
415 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
417 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
421 * Attempt to complete an address, and returns the number of addresses found.
422 * Use <code>get_complete_address()</code> to get an entry from the index.
424 * \param str Search string to find.
425 * \return Zero if no match was found, otherwise the number of addresses; the
426 * original prefix (search string) will appear at index 0.
428 guint complete_address(const gchar *str)
430 GList *result = NULL;
434 completion_entry *ce = NULL;
436 g_return_val_if_fail(str != NULL, 0);
438 /* g_completion is case sensitive */
439 d = g_utf8_strdown(str, -1);
441 clear_completion_cache();
442 g_completion_prefix = g_strdup(str);
444 result = g_completion_complete(g_completion, d, NULL);
446 count = g_list_length(result);
448 /* create list with unique addresses */
449 for (cpl = 0, result = g_list_first(result);
451 result = g_list_next(result)) {
452 ce = (completion_entry *)(result->data);
453 if (NULL == g_slist_find(g_completion_addresses,
456 g_completion_addresses =
457 g_slist_append(g_completion_addresses,
461 count = cpl + 1; /* index 0 is the original prefix */
462 g_completion_next = 1; /* we start at the first completed one */
464 g_free(g_completion_prefix);
465 g_completion_prefix = NULL;
468 g_completion_count = count;
476 * Return a complete address from the index.
477 * \param index Index of entry that was found (by the previous call to
478 * <code>complete_address()</code>
479 * \return Completed address string; this should be freed when done.
481 gchar *get_complete_address(gint index)
483 const address_entry *p;
484 gchar *address = NULL;
486 if (index < g_completion_count) {
488 address = g_strdup(g_completion_prefix);
490 /* get something from the unique addresses */
491 p = (address_entry *)g_slist_nth_data
492 (g_completion_addresses, index - 1);
494 if (!p->name || p->name[0] == '\0')
495 address = g_strdup_printf(p->address);
496 else if (strchr_with_skip_quote(p->name, '"', ','))
497 address = g_strdup_printf
498 ("\"%s\" <%s>", p->name, p->address);
500 address = g_strdup_printf
501 ("%s <%s>", p->name, p->address);
510 * Return the next complete address match from the completion index.
511 * \return Completed address string; this should be freed when done.
513 static gchar *get_next_complete_address(void)
515 if (is_completion_pending()) {
518 res = get_complete_address(g_completion_next);
519 g_completion_next += 1;
520 if (g_completion_next >= g_completion_count)
521 g_completion_next = 0;
529 * Return a count of the completed matches in the completion index.
530 * \return Number of matched entries.
532 static guint get_completion_count(void)
534 if (is_completion_pending())
535 return g_completion_count;
541 * Invalidate address completion index. This function should be called whenever
542 * the address book changes. This forces data to be read into the completion
544 * \return Number of entries in index.
546 gint invalidate_address_completion(void)
549 /* simply the same as start_address_completion() */
550 debug_print("Invalidation request for address completion\n");
553 read_address_book(completion_folder_path);
554 if (g_completion_list)
555 g_completion_add_items(g_completion, g_completion_list);
556 clear_completion_cache();
559 return g_list_length(g_completion_list);
563 * Finished with completion index. This function should be called after
564 * matching addresses.
565 * \return Reference count.
567 gint end_address_completion(void)
569 clear_completion_cache();
571 if (0 == --g_ref_count)
574 debug_print("end_address_completion ref count %d\n", g_ref_count);
582 static CompletionWindow *_compWindow_ = NULL;
585 * Mutex to protect callback from multiple threads.
587 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
590 * Completion queue list.
592 static GList *_displayQueue_ = NULL;
597 static gint _queryID_ = 0;
600 * Completion idle ID.
602 static guint _completionIdleID_ = 0;
605 * address completion entry ui. the ui (completion list was inspired by galeon's
606 * auto completion list). remaining things powered by sylpheed's completion engine.
609 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
611 static void address_completion_mainwindow_set_focus (GtkWindow *window,
614 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
617 static gboolean address_completion_complete_address_in_entry
620 static void address_completion_create_completion_window (GtkEntry *entry);
622 static gboolean completion_window_button_press
624 GdkEventButton *event,
625 CompletionWindow *compWin );
627 static gboolean completion_window_key_press
630 CompletionWindow *compWin );
631 static void address_completion_create_completion_window( GtkEntry *entry_ );
634 * Create a completion window object.
635 * \return Initialized completion window.
637 static CompletionWindow *addrcompl_create_window( void ) {
638 CompletionWindow *cw;
640 cw = g_new0( CompletionWindow, 1 );
642 cw->searchTerm = NULL;
645 cw->list_view = NULL;
646 cw->in_mouse = FALSE;
647 cw->destroying = FALSE;
653 * Destroy completion window.
654 * \param cw Window to destroy.
656 static void addrcompl_destroy_window( CompletionWindow *cw ) {
657 /* Stop all searches currently in progress */
658 addrindex_stop_search( _queryID_ );
660 /* Remove idler function... or application may not terminate */
661 if( _completionIdleID_ != 0 ) {
662 gtk_idle_remove( _completionIdleID_ );
663 _completionIdleID_ = 0;
666 /* Now destroy window */
668 /* Clear references to widgets */
670 cw->list_view = NULL;
674 gtk_widget_hide( cw->window );
675 gtk_widget_destroy( cw->window );
678 cw->destroying = FALSE;
679 cw->in_mouse = FALSE;
685 * Free up completion window.
686 * \param cw Window to free.
688 static void addrcompl_free_window( CompletionWindow *cw ) {
690 addrcompl_destroy_window( cw );
692 g_free( cw->searchTerm );
693 cw->searchTerm = NULL;
695 /* Clear references */
704 * Advance selection to previous/next item in list.
705 * \param list_view List to process.
706 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
709 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
711 GtkTreeSelection *selection;
715 g_return_if_fail(list_view != NULL);
717 selection = gtk_tree_view_get_selection(list_view);
718 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
722 forward = gtk_tree_model_iter_next(model, &iter);
724 gtk_tree_selection_select_iter(selection, &iter);
728 prev = gtk_tree_model_get_path(model, &iter);
732 if (gtk_tree_path_prev(prev))
733 gtk_tree_selection_select_path(selection, prev);
735 gtk_tree_path_free(prev);
740 * Resize window to accommodate maximum number of address entries.
741 * \param cw Completion window.
743 static void addrcompl_resize_window( CompletionWindow *cw ) {
745 gint x, y, width, height, depth;
747 /* Get current geometry of window */
748 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
750 gtk_widget_hide_all( cw->window );
751 gtk_widget_show_all( cw->window );
752 gtk_widget_size_request( cw->list_view, &r );
754 /* Adjust window height to available screen space */
755 if( ( y + r.height ) > gdk_screen_height() ) {
756 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
757 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
759 gtk_widget_set_size_request(cw->window, width, r.height);
763 * Add an address the completion window address list.
764 * \param cw Completion window.
765 * \param address Address to add.
767 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
770 GtkTreeSelection *selection;
772 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
773 gtk_list_store_append(store, &iter);
775 /* printf( "\t\tAdding :%s\n", address ); */
776 gtk_list_store_set(store, &iter, ADDR_COMPL_ADDRESS, address, -1);
780 addrcompl_resize_window( cw );
781 gtk_grab_add( cw->window );
783 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
784 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
786 if( cw->listCount == 1 ) {
787 /* Select first row for now */
788 gtk_tree_selection_select_iter(selection, &iter);
790 else if( cw->listCount == 2 ) {
791 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
792 /* Move off first row */
793 gtk_tree_selection_select_iter(selection, &iter);
798 * Completion idle function. This function is called by the main (UI) thread
799 * during UI idle time while an address search is in progress. Items from the
800 * display queue are processed and appended to the address list.
802 * \param data Target completion window to receive email addresses.
803 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
805 static gboolean addrcompl_idle( gpointer data ) {
809 /* Process all entries in display queue */
810 pthread_mutex_lock( & _completionMutex_ );
811 if( _displayQueue_ ) {
812 node = _displayQueue_;
814 address = node->data;
815 /* printf( "address ::: %s :::\n", address ); */
816 addrcompl_add_entry( _compWindow_, address );
818 node = g_list_next( node );
820 g_list_free( _displayQueue_ );
821 _displayQueue_ = NULL;
823 pthread_mutex_unlock( & _completionMutex_ );
830 * Callback entry point. The background thread (if any) appends the address
831 * list to the display queue.
832 * \param sender Sender of query.
833 * \param queryID Query ID of search request.
834 * \param listEMail List of zero of more email objects that met search
836 * \param data Query data.
838 static gint addrcompl_callback_entry(
839 gpointer sender, gint queryID, GList *listEMail, gpointer data )
844 /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
845 pthread_mutex_lock( & _completionMutex_ );
846 if( queryID == _queryID_ ) {
847 /* Append contents to end of display queue */
850 ItemEMail *email = node->data;
852 address = addritem_format_email( email );
853 /* printf( "\temail/address ::%s::\n", address ); */
854 _displayQueue_ = g_list_append( _displayQueue_, address );
855 node = g_list_next( node );
858 g_list_free( listEMail );
859 pthread_mutex_unlock( & _completionMutex_ );
865 * Clear the display queue.
867 static void addrcompl_clear_queue( void ) {
868 /* Clear out display queue */
869 pthread_mutex_lock( & _completionMutex_ );
871 g_list_free( _displayQueue_ );
872 _displayQueue_ = NULL;
874 pthread_mutex_unlock( & _completionMutex_ );
878 * Add a single address entry into the display queue.
879 * \param address Address to append.
881 static void addrcompl_add_queue( gchar *address ) {
882 pthread_mutex_lock( & _completionMutex_ );
883 _displayQueue_ = g_list_append( _displayQueue_, address );
884 pthread_mutex_unlock( & _completionMutex_ );
888 * Load list with entries from local completion index.
890 static void addrcompl_load_local( void ) {
893 for (count = 0; count < get_completion_count(); count++) {
896 address = get_complete_address( count );
897 /* printf( "\taddress ::%s::\n", address ); */
899 /* Append contents to end of display queue */
900 addrcompl_add_queue( address );
907 static void addrcompl_start_search( void ) {
910 searchTerm = g_strdup( _compWindow_->searchTerm );
912 /* Setup the search */
913 _queryID_ = addrindex_setup_search(
914 searchTerm, NULL, addrcompl_callback_entry );
915 g_free( searchTerm );
916 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
918 /* Load local stuff */
919 addrcompl_load_local();
921 /* Sit back and wait until something happens */
923 gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
924 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
926 addrindex_start_search( _queryID_ );
930 * Apply the current selection in the list to the entry field. Focus is also
931 * moved to the next widget so that Tab key works correctly.
932 * \param list_view List to process.
933 * \param entry Address entry field.
935 static void completion_window_apply_selection(GtkTreeView *list_view, GtkEntry *entry)
937 gchar *address = NULL, *text = NULL;
940 GtkTreeSelection *selection;
944 g_return_if_fail(list_view != NULL);
945 g_return_if_fail(entry != NULL);
947 selection = gtk_tree_view_get_selection(list_view);
948 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
951 /* First remove the idler */
952 if( _completionIdleID_ != 0 ) {
953 gtk_idle_remove( _completionIdleID_ );
954 _completionIdleID_ = 0;
957 /* Process selected item */
958 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text, -1);
960 address = get_address_from_edit(entry, &cursor_pos);
962 replace_address_in_edit(entry, text, cursor_pos);
965 /* Move focus to next widget */
966 parent = GTK_WIDGET(entry)->parent;
968 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
973 * Start address completion. Should be called when creating the main window
974 * containing address completion entries.
975 * \param mainwindow Main window.
977 void address_completion_start(GtkWidget *mainwindow)
979 start_address_completion(NULL);
981 /* register focus change hook */
982 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
983 G_CALLBACK(address_completion_mainwindow_set_focus),
988 * Need unique data to make unregistering signal handler possible for the auto
991 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
994 * Register specified entry widget for address completion.
995 * \param entry Address entry field.
997 void address_completion_register_entry(GtkEntry *entry)
999 g_return_if_fail(entry != NULL);
1000 g_return_if_fail(GTK_IS_ENTRY(entry));
1002 /* add hooked property */
1003 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1005 /* add keypress event */
1006 g_signal_connect_closure
1007 (G_OBJECT(entry), "key_press_event",
1008 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1009 COMPLETION_UNIQUE_DATA,
1015 * Unregister specified entry widget from address completion operations.
1016 * \param entry Address entry field.
1018 void address_completion_unregister_entry(GtkEntry *entry)
1020 GtkObject *entry_obj;
1022 g_return_if_fail(entry != NULL);
1023 g_return_if_fail(GTK_IS_ENTRY(entry));
1025 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1026 g_return_if_fail(entry_obj);
1027 g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1029 /* has the hooked property? */
1030 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1032 /* remove the hook */
1033 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1034 G_CALLBACK(address_completion_entry_key_pressed),
1035 COMPLETION_UNIQUE_DATA);
1039 * End address completion. Should be called when main window with address
1040 * completion entries terminates. NOTE: this function assumes that it is
1041 * called upon destruction of the window.
1042 * \param mainwindow Main window.
1044 void address_completion_end(GtkWidget *mainwindow)
1046 /* if address_completion_end() is really called on closing the window,
1047 * we don't need to unregister the set_focus_cb */
1048 end_address_completion();
1051 /* if focus changes to another entry, then clear completion cache */
1052 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1057 if (widget && GTK_IS_ENTRY(widget) &&
1058 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1059 clear_completion_cache();
1064 * Listener that watches for tab or other keystroke in address entry field.
1065 * \param entry Address entry field.
1066 * \param ev Event object.
1067 * \param data User data.
1068 * \return <i>TRUE</i>.
1070 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1074 if (ev->keyval == GDK_Tab) {
1075 addrcompl_clear_queue();
1077 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1078 /* route a void character to the default handler */
1079 /* this is a dirty hack; we're actually changing a key
1080 * reported by the system. */
1081 ev->keyval = GDK_AudibleBell_Enable;
1082 ev->state &= ~GDK_SHIFT_MASK;
1085 address_completion_create_completion_window(entry);
1087 /* Start remote queries */
1088 addrcompl_start_search();
1095 } else if (ev->keyval == GDK_Shift_L
1096 || ev->keyval == GDK_Shift_R
1097 || ev->keyval == GDK_Control_L
1098 || ev->keyval == GDK_Control_R
1099 || ev->keyval == GDK_Caps_Lock
1100 || ev->keyval == GDK_Shift_Lock
1101 || ev->keyval == GDK_Meta_L
1102 || ev->keyval == GDK_Meta_R
1103 || ev->keyval == GDK_Alt_L
1104 || ev->keyval == GDK_Alt_R) {
1105 /* these buttons should not clear the cache... */
1107 clear_completion_cache();
1112 * Initialize search term for address completion.
1113 * \param entry Address entry field.
1115 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1118 gint ncount, cursor_pos;
1119 gchar *searchTerm, *new = NULL;
1121 g_return_val_if_fail(entry != NULL, FALSE);
1123 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1125 /* get an address component from the cursor */
1126 searchTerm = get_address_from_edit( entry, &cursor_pos );
1127 if( ! searchTerm ) return FALSE;
1128 /* printf( "search for :::%s:::\n", searchTerm ); */
1130 /* Clear any existing search */
1131 g_free( _compWindow_->searchTerm );
1132 _compWindow_->searchTerm = g_strdup( searchTerm );
1134 /* Perform search on local completion index */
1135 ncount = complete_address( searchTerm );
1137 new = get_next_complete_address();
1142 /* Select the address if there is only one match */
1144 /* Display selected address in entry field */
1145 gchar *addr = get_complete_address(1);
1148 replace_address_in_edit(entry, addr, cursor_pos);
1152 /* Discard the window */
1153 clear_completion_cache();
1155 /* Make sure that drop-down appears uniform! */
1159 addrcompl_add_queue( g_strdup( searchTerm ) );
1161 g_free( searchTerm );
1167 * Create new address completion window for specified entry.
1168 * \param entry_ Entry widget to associate with window.
1170 static void address_completion_create_completion_window( GtkEntry *entry_ )
1172 gint x, y, height, width, depth;
1173 GtkWidget *scroll, *list_view;
1176 GtkWidget *entry = GTK_WIDGET(entry_);
1178 /* Create new window and list */
1179 window = gtk_window_new(GTK_WINDOW_POPUP);
1180 list_view = addr_compl_list_view_create(_compWindow_);
1182 /* Destroy any existing window */
1183 addrcompl_destroy_window( _compWindow_ );
1185 /* Create new object */
1186 _compWindow_->window = window;
1187 _compWindow_->entry = entry;
1188 _compWindow_->list_view = list_view;
1189 _compWindow_->listCount = 0;
1190 _compWindow_->in_mouse = FALSE;
1192 scroll = gtk_scrolled_window_new(NULL, NULL);
1193 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1194 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1195 gtk_container_add(GTK_CONTAINER(window), scroll);
1196 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1198 /* Use entry widget to create initial window */
1199 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1200 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1202 gtk_window_move(GTK_WINDOW(window), x, y);
1204 /* Resize window to fit initial (empty) address list */
1205 gtk_widget_size_request( list_view, &r );
1206 gtk_widget_set_size_request( window, width, r.height );
1207 gtk_widget_show_all( window );
1208 gtk_widget_size_request( list_view, &r );
1210 /* Setup handlers */
1211 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1212 G_CALLBACK(list_view_button_press),
1215 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1216 G_CALLBACK(list_view_button_release),
1219 g_signal_connect(G_OBJECT(window),
1220 "button-press-event",
1221 G_CALLBACK(completion_window_button_press),
1223 g_signal_connect(G_OBJECT(window),
1225 G_CALLBACK(completion_window_key_press),
1227 gdk_pointer_grab(window->window, TRUE,
1228 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1229 GDK_BUTTON_RELEASE_MASK,
1230 NULL, NULL, GDK_CURRENT_TIME);
1231 gtk_grab_add( window );
1235 * GTK1: this gets rid of the irritating focus rectangle that doesn't
1236 * follow the selection */
1237 GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
1241 * Respond to button press in completion window. Check if mouse click is
1242 * anywhere outside the completion window. In that case the completion
1243 * window is destroyed, and the original searchTerm is restored.
1245 * \param widget Window object.
1246 * \param event Event.
1247 * \param compWin Reference to completion window.
1249 static gboolean completion_window_button_press(GtkWidget *widget,
1250 GdkEventButton *event,
1251 CompletionWindow *compWin )
1253 GtkWidget *event_widget, *entry;
1256 gboolean restore = TRUE;
1258 g_return_val_if_fail(compWin != NULL, FALSE);
1260 entry = compWin->entry;
1261 g_return_val_if_fail(entry != NULL, FALSE);
1263 /* Test where mouse was clicked */
1264 event_widget = gtk_get_event_widget((GdkEvent *)event);
1265 if (event_widget != widget) {
1266 while (event_widget) {
1267 if (event_widget == widget)
1269 else if (event_widget == entry) {
1273 event_widget = event_widget->parent;
1278 /* Clicked outside of completion window - restore */
1279 searchTerm = _compWindow_->searchTerm;
1280 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1281 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1284 clear_completion_cache();
1285 addrcompl_destroy_window( _compWindow_ );
1291 * Respond to key press in completion window.
1292 * \param widget Window object.
1293 * \param event Event.
1294 * \param compWind Reference to completion window.
1296 static gboolean completion_window_key_press(GtkWidget *widget,
1298 CompletionWindow *compWin )
1300 GdkEventKey tmp_event;
1304 GtkWidget *list_view;
1307 g_return_val_if_fail(compWin != NULL, FALSE);
1309 entry = compWin->entry;
1310 list_view = compWin->list_view;
1311 g_return_val_if_fail(entry != NULL, FALSE);
1313 /* allow keyboard navigation in the alternatives tree view */
1314 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1315 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1316 completion_window_advance_selection
1317 (GTK_TREE_VIEW(list_view),
1318 event->keyval == GDK_Down ||
1319 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1323 /* make tab move to next field */
1324 if( event->keyval == GDK_Tab ) {
1325 /* Reference to parent */
1326 parent = GTK_WIDGET(entry)->parent;
1328 /* Discard the window */
1329 clear_completion_cache();
1330 addrcompl_destroy_window( _compWindow_ );
1332 /* Move focus to next widget */
1334 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1339 /* make backtab move to previous field */
1340 if( event->keyval == GDK_ISO_Left_Tab ) {
1341 /* Reference to parent */
1342 parent = GTK_WIDGET(entry)->parent;
1344 /* Discard the window */
1345 clear_completion_cache();
1346 addrcompl_destroy_window( _compWindow_ );
1348 /* Move focus to previous widget */
1350 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1355 /* look for presses that accept the selection */
1356 if (event->keyval == GDK_Return || event->keyval == GDK_space) {
1357 /* User selected address with a key press */
1359 /* Display selected address in entry field */
1360 completion_window_apply_selection(
1361 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry) );
1363 /* Discard the window */
1364 clear_completion_cache();
1365 addrcompl_destroy_window( _compWindow_ );
1369 /* key state keys should never be handled */
1370 if (event->keyval == GDK_Shift_L
1371 || event->keyval == GDK_Shift_R
1372 || event->keyval == GDK_Control_L
1373 || event->keyval == GDK_Control_R
1374 || event->keyval == GDK_Caps_Lock
1375 || event->keyval == GDK_Shift_Lock
1376 || event->keyval == GDK_Meta_L
1377 || event->keyval == GDK_Meta_R
1378 || event->keyval == GDK_Alt_L
1379 || event->keyval == GDK_Alt_R) {
1383 /* some other key, let's restore the searchTerm (orignal text) */
1384 searchTerm = _compWindow_->searchTerm;
1385 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1386 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1388 /* make sure anything we typed comes in the edit box */
1389 tmp_event.type = event->type;
1390 tmp_event.window = entry->window;
1391 tmp_event.send_event = TRUE;
1392 tmp_event.time = event->time;
1393 tmp_event.state = event->state;
1394 tmp_event.keyval = event->keyval;
1395 tmp_event.length = event->length;
1396 tmp_event.string = event->string;
1397 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1399 /* and close the completion window */
1400 clear_completion_cache();
1401 addrcompl_destroy_window( _compWindow_ );
1407 * ============================================================================
1408 * Publically accessible functions.
1409 * ============================================================================
1413 * Setup completion object.
1415 void addrcompl_initialize( void ) {
1416 /* printf( "addrcompl_initialize...\n" ); */
1417 if( ! _compWindow_ ) {
1418 _compWindow_ = addrcompl_create_window();
1421 _completionIdleID_ = 0;
1422 /* printf( "addrcompl_initialize...done\n" ); */
1426 * Teardown completion object.
1428 void addrcompl_teardown( void ) {
1429 /* printf( "addrcompl_teardown...\n" ); */
1430 addrcompl_free_window( _compWindow_ );
1431 _compWindow_ = NULL;
1432 if( _displayQueue_ ) {
1433 g_list_free( _displayQueue_ );
1435 _displayQueue_ = NULL;
1436 _completionIdleID_ = 0;
1437 /* printf( "addrcompl_teardown...done\n" ); */
1441 * tree view functions
1444 static GtkListStore *addr_compl_create_store(void)
1446 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1451 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1453 GtkTreeView *list_view;
1454 GtkTreeSelection *selector;
1455 GtkTreeModel *model;
1457 model = GTK_TREE_MODEL(addr_compl_create_store());
1458 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1459 g_object_unref(model);
1461 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1462 gtk_tree_view_set_headers_visible(list_view, FALSE);
1464 selector = gtk_tree_view_get_selection(list_view);
1465 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1466 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1469 /* create the columns */
1470 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1472 return GTK_WIDGET(list_view);
1475 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1477 GtkTreeViewColumn *column;
1478 GtkCellRenderer *renderer;
1480 renderer = gtk_cell_renderer_text_new();
1481 column = gtk_tree_view_column_new_with_attributes
1482 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1483 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1486 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1487 CompletionWindow *window)
1489 if (window && event && event->type == GDK_BUTTON_PRESS) {
1490 window->in_mouse = TRUE;
1495 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1496 CompletionWindow *window)
1498 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1499 window->in_mouse = FALSE;
1504 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1505 GtkTreeModel *model,
1507 gboolean currently_selected,
1510 CompletionWindow *window = data;
1512 if (currently_selected)
1515 if (!window->in_mouse)
1518 /* XXX: select the entry and kill window later... select is called before
1519 * any other mouse events handlers including the tree view internal one;
1520 * not using a time out would result in a crash. if this doesn't work
1521 * safely, maybe we should set variables when receiving button presses
1522 * in the tree view. */
1523 if (!window->destroying) {
1524 window->destroying = TRUE;
1525 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1531 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1533 GtkEntry *entry = GTK_ENTRY(window->entry);
1535 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1538 clear_completion_cache();
1540 addrcompl_destroy_window(window);