2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2005 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
51 *\brief For the GtkListStore
61 * The address book is read into memory. We set up an address list
62 * containing all address book entries. Next we make the completion
63 * list, which contains all the completable strings, and store a
64 * reference to the address entry it belongs to.
65 * After calling the g_completion_complete(), we get a reference
66 * to a valid email address.
68 * Completion is very simplified. We never complete on another prefix,
69 * i.e. we neglect the next smallest possible prefix for the current
70 * completion cache. This is simply done so we might break up the
71 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
72 * something like alfons, proteus, demon, nl; and then completing on
73 * any of those words).
77 * address_entry - structure which refers to the original address entry in the
87 * completion_entry - structure used to complete addresses, with a reference
88 * the the real address information.
92 gchar *string; /* string to complete */
93 address_entry *ref; /* address the string belongs to */
96 /*******************************************************************************/
98 static gint g_ref_count; /* list ref count */
99 static GList *g_completion_list; /* list of strings to be checked */
100 static GList *g_address_list; /* address storage */
101 static GCompletion *g_completion; /* completion object */
103 /* To allow for continuing completion we have to keep track of the state
104 * using the following variables. No need to create a context object. */
106 static gint g_completion_count; /* nr of addresses incl. the prefix */
107 static gint g_completion_next; /* next prev address */
108 static GSList *g_completion_addresses; /* unique addresses found in the
110 static gchar *g_completion_prefix; /* last prefix. (this is cached here
111 * because the prefix passed to g_completion
112 * is g_strdown()'ed */
114 /*******************************************************************************/
117 * Define the structure of the completion window.
119 typedef struct _CompletionWindow CompletionWindow;
120 struct _CompletionWindow {
125 GtkWidget *list_view;
127 gboolean in_mouse; /*!< mouse press pending... */
128 gboolean destroying; /*!< destruction in progress */
131 static GtkListStore *addr_compl_create_store (void);
133 static void addr_compl_list_view_add_address (GtkWidget *list_view,
134 const gchar *address);
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 g_return_val_if_fail(data != NULL, NULL);
164 return ((completion_entry *)data)->string;
168 * Initialize all completion index data.
170 static void init_all(void)
172 g_completion = g_completion_new(completion_func);
173 g_return_if_fail(g_completion != NULL);
177 * Free up all completion index data.
179 static void free_all(void)
183 walk = g_list_first(g_completion_list);
184 for (; walk != NULL; walk = g_list_next(walk)) {
185 completion_entry *ce = (completion_entry *) walk->data;
189 g_list_free(g_completion_list);
190 g_completion_list = NULL;
192 walk = g_address_list;
193 for (; walk != NULL; walk = g_list_next(walk)) {
194 address_entry *ae = (address_entry *) walk->data;
199 g_list_free(g_address_list);
200 g_address_list = NULL;
202 g_completion_free(g_completion);
207 * Append specified address entry to the index.
208 * \param str Index string value.
209 * \param ae Entry containing address data.
211 static void add_address1(const char *str, address_entry *ae)
213 completion_entry *ce1;
214 ce1 = g_new0(completion_entry, 1),
215 ce1->string = g_utf8_strdown(str, -1);
216 /* GCompletion list is case sensitive */
217 g_strdown(ce1->string);
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(void) {
265 addrindex_load_completion( add_address );
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 if (g_completion_prefix)
288 g_free(g_completion_prefix);
290 if (g_completion_addresses) {
291 g_slist_free(g_completion_addresses);
292 g_completion_addresses = NULL;
295 g_completion_count = g_completion_next = 0;
300 * Prepare completion index. This function should be called prior to attempting
301 * address completion.
302 * \return The number of addresses in the completion list.
304 gint start_address_completion(void)
306 clear_completion_cache();
309 /* open the address book */
311 /* merge the completion entry list into g_completion */
312 if (g_completion_list)
313 g_completion_add_items(g_completion, g_completion_list);
316 debug_print("start_address_completion ref count %d\n", g_ref_count);
318 return g_list_length(g_completion_list);
322 * Retrieve a possible address (or a part) from an entry box. To make life
323 * easier, we only look at the last valid address component; address
324 * completion only works at the last string component in the entry box.
326 * \param entry Address entry field.
327 * \param start_pos Address of start position of address.
328 * \return Possible address.
330 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
332 const gchar *edit_text, *p;
334 gboolean in_quote = FALSE;
335 gboolean in_bracket = FALSE;
338 edit_text = gtk_entry_get_text(entry);
339 if (edit_text == NULL) return NULL;
341 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
343 /* scan for a separator. doesn't matter if walk points at null byte. */
344 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
346 p = g_utf8_prev_char(p)) {
349 } else if (!in_quote) {
350 if (!in_bracket && *p == ',') {
352 } else if (*p == '<')
359 /* have something valid */
360 if (g_utf8_strlen(p, -1) == 0)
363 #define IS_VALID_CHAR(x) \
364 (isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
366 /* now scan back until we hit a valid character */
367 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
372 if (g_utf8_strlen(p, -1) == 0)
375 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
383 * Replace an incompleted address with a completed one.
384 * \param entry Address entry field.
385 * \param newtext New text.
386 * \param start_pos Insertion point in entry field.
388 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
391 if (!newtext) return;
392 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
393 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
395 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
399 * Attempt to complete an address, and returns the number of addresses found.
400 * Use <code>get_complete_address()</code> to get an entry from the index.
402 * \param str Search string to find.
403 * \return Zero if no match was found, otherwise the number of addresses; the
404 * original prefix (search string) will appear at index 0.
406 guint complete_address(const gchar *str)
411 completion_entry *ce;
413 g_return_val_if_fail(str != NULL, 0);
415 /* g_completion is case sensitive */
416 d = g_utf8_strdown(str, -1);
418 clear_completion_cache();
419 g_completion_prefix = g_strdup(str);
421 result = g_completion_complete(g_completion, d, NULL);
423 count = g_list_length(result);
425 /* create list with unique addresses */
426 for (cpl = 0, result = g_list_first(result);
428 result = g_list_next(result)) {
429 ce = (completion_entry *)(result->data);
430 if (NULL == g_slist_find(g_completion_addresses,
433 g_completion_addresses =
434 g_slist_append(g_completion_addresses,
438 count = cpl + 1; /* index 0 is the original prefix */
439 g_completion_next = 1; /* we start at the first completed one */
441 g_free(g_completion_prefix);
442 g_completion_prefix = NULL;
445 g_completion_count = count;
453 * Return a complete address from the index.
454 * \param index Index of entry that was found (by the previous call to
455 * <code>complete_address()</code>
456 * \return Completed address string; this should be freed when done.
458 gchar *get_complete_address(gint index)
460 const address_entry *p;
461 gchar *address = NULL;
463 if (index < g_completion_count) {
465 address = g_strdup(g_completion_prefix);
467 /* get something from the unique addresses */
468 p = (address_entry *)g_slist_nth_data
469 (g_completion_addresses, index - 1);
471 if (!p->name || p->name[0] == '\0')
472 address = g_strdup_printf(p->address);
473 else if (strchr_with_skip_quote(p->name, '"', ','))
474 address = g_strdup_printf
475 ("\"%s\" <%s>", p->name, p->address);
477 address = g_strdup_printf
478 ("%s <%s>", p->name, p->address);
487 * Return the next complete address match from the completion index.
488 * \return Completed address string; this should be freed when done.
490 static gchar *get_next_complete_address(void)
492 if (is_completion_pending()) {
495 res = get_complete_address(g_completion_next);
496 g_completion_next += 1;
497 if (g_completion_next >= g_completion_count)
498 g_completion_next = 0;
506 * Return a count of the completed matches in the completion index.
507 * \return Number of matched entries.
509 static guint get_completion_count(void)
511 if (is_completion_pending())
512 return g_completion_count;
518 * Invalidate address completion index. This function should be called whenever
519 * the address book changes. This forces data to be read into the completion
521 * \return Number of entries in index.
523 gint invalidate_address_completion(void)
526 /* simply the same as start_address_completion() */
527 debug_print("Invalidation request for address completion\n");
531 g_completion_add_items(g_completion, g_completion_list);
532 clear_completion_cache();
535 return g_list_length(g_completion_list);
539 * Finished with completion index. This function should be called after
540 * matching addresses.
541 * \return Reference count.
543 gint end_address_completion(void)
545 clear_completion_cache();
547 if (0 == --g_ref_count)
550 debug_print("end_address_completion ref count %d\n", g_ref_count);
558 static CompletionWindow *_compWindow_ = NULL;
561 * Mutex to protect callback from multiple threads.
563 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
566 * Completion queue list.
568 static GList *_displayQueue_ = NULL;
573 static gint _queryID_ = 0;
576 * Completion idle ID.
578 static guint _completionIdleID_ = 0;
581 * address completion entry ui. the ui (completion list was inspired by galeon's
582 * auto completion list). remaining things powered by sylpheed's completion engine.
585 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
587 static void address_completion_mainwindow_set_focus (GtkWindow *window,
590 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
593 static gboolean address_completion_complete_address_in_entry
596 static void address_completion_create_completion_window (GtkEntry *entry);
598 static gboolean completion_window_button_press
600 GdkEventButton *event,
601 CompletionWindow *compWin );
603 static gboolean completion_window_key_press
606 CompletionWindow *compWin );
607 static void address_completion_create_completion_window( GtkEntry *entry_ );
610 * Create a completion window object.
611 * \return Initialized completion window.
613 static CompletionWindow *addrcompl_create_window( void ) {
614 CompletionWindow *cw;
616 cw = g_new0( CompletionWindow, 1 );
618 cw->searchTerm = NULL;
621 cw->list_view = NULL;
622 cw->in_mouse = FALSE;
623 cw->destroying = FALSE;
629 * Destroy completion window.
630 * \param cw Window to destroy.
632 static void addrcompl_destroy_window( CompletionWindow *cw ) {
633 /* Stop all searches currently in progress */
634 addrindex_stop_search( _queryID_ );
636 /* Remove idler function... or application may not terminate */
637 if( _completionIdleID_ != 0 ) {
638 gtk_idle_remove( _completionIdleID_ );
639 _completionIdleID_ = 0;
642 /* Now destroy window */
644 /* Clear references to widgets */
646 cw->list_view = NULL;
650 gtk_widget_hide( cw->window );
651 gtk_widget_destroy( cw->window );
654 cw->destroying = FALSE;
655 cw->in_mouse = FALSE;
661 * Free up completion window.
662 * \param cw Window to free.
664 static void addrcompl_free_window( CompletionWindow *cw ) {
666 addrcompl_destroy_window( cw );
668 g_free( cw->searchTerm );
669 cw->searchTerm = NULL;
671 /* Clear references */
680 * Advance selection to previous/next item in list.
681 * \param list_view List to process.
682 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
685 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
687 GtkTreeSelection *selection;
691 g_return_if_fail(list_view != NULL);
693 selection = gtk_tree_view_get_selection(list_view);
694 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
698 forward = gtk_tree_model_iter_next(model, &iter);
700 gtk_tree_selection_select_iter(selection, &iter);
704 prev = gtk_tree_model_get_path(model, &iter);
708 if (gtk_tree_path_prev(prev))
709 gtk_tree_selection_select_path(selection, prev);
711 gtk_tree_path_free(prev);
716 /* completion_window_accept_selection() - accepts the current selection in the
717 * clist, and destroys the window */
718 static void completion_window_accept_selection(GtkWidget **window,
722 gchar *address = NULL, *text = NULL;
723 gint cursor_pos, row;
725 g_return_if_fail(window != NULL);
726 g_return_if_fail(*window != NULL);
727 g_return_if_fail(clist != NULL);
728 g_return_if_fail(entry != NULL);
729 g_return_if_fail(clist->selection != NULL);
731 /* FIXME: I believe it's acceptable to access the selection member directly */
732 row = GPOINTER_TO_INT(clist->selection->data);
734 /* we just need the cursor position */
735 address = get_address_from_edit(entry, &cursor_pos);
737 gtk_clist_get_text(clist, row, 0, &text);
738 replace_address_in_edit(entry, text, cursor_pos);
740 clear_completion_cache();
741 gtk_widget_destroy(*window);
747 * Resize window to accommodate maximum number of address entries.
748 * \param cw Completion window.
750 static void addrcompl_resize_window( CompletionWindow *cw ) {
752 gint x, y, width, height, depth;
754 /* Get current geometry of window */
755 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
757 gtk_widget_hide_all( cw->window );
758 gtk_widget_show_all( cw->window );
759 gtk_widget_size_request( cw->list_view, &r );
761 /* Adjust window height to available screen space */
762 if( ( y + r.height ) > gdk_screen_height() ) {
763 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
764 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
766 gtk_widget_set_size_request(cw->window, width, r.height);
770 * Add an address the completion window address list.
771 * \param cw Completion window.
772 * \param address Address to add.
774 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
777 GtkTreeSelection *selection;
779 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
780 gtk_list_store_append(store, &iter);
782 /* printf( "\t\tAdding :%s\n", address ); */
783 gtk_list_store_set(store, &iter, ADDR_COMPL_ADDRESS, address, -1);
787 addrcompl_resize_window( cw );
788 gtk_grab_add( cw->window );
790 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
791 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
793 if( cw->listCount == 1 ) {
794 /* Select first row for now */
795 gtk_tree_selection_select_iter(selection, &iter);
797 else if( cw->listCount == 2 ) {
798 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
799 /* Move off first row */
800 gtk_tree_selection_select_iter(selection, &iter);
805 * Completion idle function. This function is called by the main (UI) thread
806 * during UI idle time while an address search is in progress. Items from the
807 * display queue are processed and appended to the address list.
809 * \param data Target completion window to receive email addresses.
810 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
812 static gboolean addrcompl_idle( gpointer data ) {
816 /* Process all entries in display queue */
817 pthread_mutex_lock( & _completionMutex_ );
818 if( _displayQueue_ ) {
819 node = _displayQueue_;
821 address = node->data;
822 /* printf( "address ::: %s :::\n", address ); */
823 addrcompl_add_entry( _compWindow_, address );
825 node = g_list_next( node );
827 g_list_free( _displayQueue_ );
828 _displayQueue_ = NULL;
830 pthread_mutex_unlock( & _completionMutex_ );
836 * Callback entry point. The background thread (if any) appends the address
837 * list to the display queue.
838 * \param sender Sender of query.
839 * \param queryID Query ID of search request.
840 * \param listEMail List of zero of more email objects that met search
842 * \param data Query data.
844 static gint addrcompl_callback_entry(
845 gpointer sender, gint queryID, GList *listEMail, gpointer data )
850 /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
851 pthread_mutex_lock( & _completionMutex_ );
852 if( queryID == _queryID_ ) {
853 /* Append contents to end of display queue */
856 ItemEMail *email = node->data;
858 address = addritem_format_email( email );
859 /* printf( "\temail/address ::%s::\n", address ); */
860 _displayQueue_ = g_list_append( _displayQueue_, address );
861 node = g_list_next( node );
864 g_list_free( listEMail );
865 pthread_mutex_unlock( & _completionMutex_ );
871 * Clear the display queue.
873 static void addrcompl_clear_queue( void ) {
874 /* Clear out display queue */
875 pthread_mutex_lock( & _completionMutex_ );
877 g_list_free( _displayQueue_ );
878 _displayQueue_ = NULL;
880 pthread_mutex_unlock( & _completionMutex_ );
884 * Add a single address entry into the display queue.
885 * \param address Address to append.
887 static void addrcompl_add_queue( gchar *address ) {
888 pthread_mutex_lock( & _completionMutex_ );
889 _displayQueue_ = g_list_append( _displayQueue_, address );
890 pthread_mutex_unlock( & _completionMutex_ );
894 * Load list with entries from local completion index.
896 static void addrcompl_load_local( void ) {
899 for (count = 0; count < get_completion_count(); count++) {
902 address = get_complete_address( count );
903 /* printf( "\taddress ::%s::\n", address ); */
905 /* Append contents to end of display queue */
906 addrcompl_add_queue( address );
913 static void addrcompl_start_search( void ) {
916 searchTerm = g_strdup( _compWindow_->searchTerm );
918 /* Setup the search */
919 _queryID_ = addrindex_setup_search(
920 searchTerm, NULL, addrcompl_callback_entry );
921 g_free( searchTerm );
922 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
924 /* Load local stuff */
925 addrcompl_load_local();
927 /* Sit back and wait until something happens */
929 gtk_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
930 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
932 addrindex_start_search( _queryID_ );
936 * Apply the current selection in the list to the entry field. Focus is also
937 * moved to the next widget so that Tab key works correctly.
938 * \param list_view List to process.
939 * \param entry Address entry field.
941 static void completion_window_apply_selection(GtkTreeView *list_view, GtkEntry *entry)
943 gchar *address = NULL, *text = NULL;
946 GtkTreeSelection *selection;
950 g_return_if_fail(list_view != NULL);
951 g_return_if_fail(entry != NULL);
953 selection = gtk_tree_view_get_selection(list_view);
954 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
957 /* First remove the idler */
958 if( _completionIdleID_ != 0 ) {
959 gtk_idle_remove( _completionIdleID_ );
960 _completionIdleID_ = 0;
963 /* Process selected item */
964 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text, -1);
966 address = get_address_from_edit(entry, &cursor_pos);
968 replace_address_in_edit(entry, text, cursor_pos);
971 /* Move focus to next widget */
972 parent = GTK_WIDGET(entry)->parent;
974 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
979 * Start address completion. Should be called when creating the main window
980 * containing address completion entries.
981 * \param mainwindow Main window.
983 void address_completion_start(GtkWidget *mainwindow)
985 start_address_completion();
987 /* register focus change hook */
988 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
989 G_CALLBACK(address_completion_mainwindow_set_focus),
994 * Need unique data to make unregistering signal handler possible for the auto
997 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1000 * Register specified entry widget for address completion.
1001 * \param entry Address entry field.
1003 void address_completion_register_entry(GtkEntry *entry)
1005 g_return_if_fail(entry != NULL);
1006 g_return_if_fail(GTK_IS_ENTRY(entry));
1008 /* add hooked property */
1009 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1011 /* add keypress event */
1012 g_signal_connect_closure
1013 (G_OBJECT(entry), "key_press_event",
1014 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1015 COMPLETION_UNIQUE_DATA,
1021 * Unregister specified entry widget from address completion operations.
1022 * \param entry Address entry field.
1024 void address_completion_unregister_entry(GtkEntry *entry)
1026 GtkObject *entry_obj;
1028 g_return_if_fail(entry != NULL);
1029 g_return_if_fail(GTK_IS_ENTRY(entry));
1031 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1032 g_return_if_fail(entry_obj);
1033 g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1035 /* has the hooked property? */
1036 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1038 /* remove the hook */
1039 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1040 G_CALLBACK(address_completion_entry_key_pressed),
1041 COMPLETION_UNIQUE_DATA);
1045 * End address completion. Should be called when main window with address
1046 * completion entries terminates. NOTE: this function assumes that it is
1047 * called upon destruction of the window.
1048 * \param mainwindow Main window.
1050 void address_completion_end(GtkWidget *mainwindow)
1052 /* if address_completion_end() is really called on closing the window,
1053 * we don't need to unregister the set_focus_cb */
1054 end_address_completion();
1057 /* if focus changes to another entry, then clear completion cache */
1058 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1063 if (widget && GTK_IS_ENTRY(widget) &&
1064 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1065 clear_completion_cache();
1070 * Listener that watches for tab or other keystroke in address entry field.
1071 * \param entry Address entry field.
1072 * \param ev Event object.
1073 * \param data User data.
1074 * \return <i>TRUE</i>.
1076 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1080 if (ev->keyval == GDK_Tab) {
1081 addrcompl_clear_queue();
1083 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1084 /* route a void character to the default handler */
1085 /* this is a dirty hack; we're actually changing a key
1086 * reported by the system. */
1087 ev->keyval = GDK_AudibleBell_Enable;
1088 ev->state &= ~GDK_SHIFT_MASK;
1091 address_completion_create_completion_window(entry);
1093 /* Start remote queries */
1094 addrcompl_start_search();
1101 } else if (ev->keyval == GDK_Shift_L
1102 || ev->keyval == GDK_Shift_R
1103 || ev->keyval == GDK_Control_L
1104 || ev->keyval == GDK_Control_R
1105 || ev->keyval == GDK_Caps_Lock
1106 || ev->keyval == GDK_Shift_Lock
1107 || ev->keyval == GDK_Meta_L
1108 || ev->keyval == GDK_Meta_R
1109 || ev->keyval == GDK_Alt_L
1110 || ev->keyval == GDK_Alt_R) {
1111 /* these buttons should not clear the cache... */
1113 clear_completion_cache();
1118 * Initialize search term for address completion.
1119 * \param entry Address entry field.
1121 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1124 gint ncount, cursor_pos;
1125 gchar *searchTerm, *new = NULL;
1127 g_return_val_if_fail(entry != NULL, FALSE);
1129 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1131 /* get an address component from the cursor */
1132 searchTerm = get_address_from_edit( entry, &cursor_pos );
1133 if( ! searchTerm ) return FALSE;
1134 /* printf( "search for :::%s:::\n", searchTerm ); */
1136 /* Clear any existing search */
1137 if( _compWindow_->searchTerm ) {
1138 g_free( _compWindow_->searchTerm );
1140 _compWindow_->searchTerm = g_strdup( searchTerm );
1142 /* Perform search on local completion index */
1143 ncount = complete_address( searchTerm );
1145 new = get_next_complete_address();
1149 /* Select the address if there is only one match */
1151 /* Display selected address in entry field */
1152 gchar *addr = get_complete_address(1);
1155 replace_address_in_edit(entry, addr, cursor_pos);
1159 /* Discard the window */
1160 clear_completion_cache();
1162 /* Make sure that drop-down appears uniform! */
1163 else if( ncount == 0 ) {
1164 addrcompl_add_queue( g_strdup( searchTerm ) );
1166 g_free( searchTerm );
1172 * Create new address completion window for specified entry.
1173 * \param entry_ Entry widget to associate with window.
1175 static void address_completion_create_completion_window( GtkEntry *entry_ )
1177 gint x, y, height, width, depth;
1178 GtkWidget *scroll, *list_view;
1181 GtkWidget *entry = GTK_WIDGET(entry_);
1183 /* Create new window and list */
1184 window = gtk_window_new(GTK_WINDOW_POPUP);
1185 list_view = addr_compl_list_view_create(_compWindow_);
1187 /* Destroy any existing window */
1188 addrcompl_destroy_window( _compWindow_ );
1190 /* Create new object */
1191 _compWindow_->window = window;
1192 _compWindow_->entry = entry;
1193 _compWindow_->list_view = list_view;
1194 _compWindow_->listCount = 0;
1195 _compWindow_->in_mouse = FALSE;
1197 scroll = gtk_scrolled_window_new(NULL, NULL);
1198 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1199 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1200 gtk_container_add(GTK_CONTAINER(window), scroll);
1201 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1203 /* Use entry widget to create initial window */
1204 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1205 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1207 gtk_window_move(GTK_WINDOW(window), x, y);
1209 /* Resize window to fit initial (empty) address list */
1210 gtk_widget_size_request( list_view, &r );
1211 gtk_widget_set_size_request( window, width, r.height );
1212 gtk_widget_show_all( window );
1213 gtk_widget_size_request( list_view, &r );
1215 /* Setup handlers */
1216 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1217 G_CALLBACK(list_view_button_press),
1220 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1221 G_CALLBACK(list_view_button_release),
1224 g_signal_connect(G_OBJECT(window),
1225 "button-press-event",
1226 G_CALLBACK(completion_window_button_press),
1228 g_signal_connect(G_OBJECT(window),
1230 G_CALLBACK(completion_window_key_press),
1232 gdk_pointer_grab(window->window, TRUE,
1233 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1234 GDK_BUTTON_RELEASE_MASK,
1235 NULL, NULL, GDK_CURRENT_TIME);
1236 gtk_grab_add( window );
1240 * GTK1: this gets rid of the irritating focus rectangle that doesn't
1241 * follow the selection */
1242 GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
1246 * Respond to button press in completion window. Check if mouse click is
1247 * anywhere outside the completion window. In that case the completion
1248 * window is destroyed, and the original searchTerm is restored.
1250 * \param widget Window object.
1251 * \param event Event.
1252 * \param compWin Reference to completion window.
1254 static gboolean completion_window_button_press(GtkWidget *widget,
1255 GdkEventButton *event,
1256 CompletionWindow *compWin )
1258 GtkWidget *event_widget, *entry;
1261 gboolean restore = TRUE;
1263 g_return_val_if_fail(compWin != NULL, FALSE);
1265 entry = compWin->entry;
1266 g_return_val_if_fail(entry != NULL, FALSE);
1268 /* Test where mouse was clicked */
1269 event_widget = gtk_get_event_widget((GdkEvent *)event);
1270 if (event_widget != widget) {
1271 while (event_widget) {
1272 if (event_widget == widget)
1274 else if (event_widget == entry) {
1278 event_widget = event_widget->parent;
1283 /* Clicked outside of completion window - restore */
1284 searchTerm = _compWindow_->searchTerm;
1285 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1286 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1289 clear_completion_cache();
1290 addrcompl_destroy_window( _compWindow_ );
1296 * Respond to key press in completion window.
1297 * \param widget Window object.
1298 * \param event Event.
1299 * \param compWind Reference to completion window.
1301 static gboolean completion_window_key_press(GtkWidget *widget,
1303 CompletionWindow *compWin )
1305 GdkEventKey tmp_event;
1309 GtkWidget *list_view;
1312 g_return_val_if_fail(compWin != NULL, FALSE);
1314 entry = compWin->entry;
1315 list_view = compWin->list_view;
1316 g_return_val_if_fail(entry != NULL, FALSE);
1318 /* allow keyboard navigation in the alternatives tree view */
1319 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1320 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1321 completion_window_advance_selection
1322 (GTK_TREE_VIEW(list_view),
1323 event->keyval == GDK_Down ||
1324 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1329 /* also make tab / shift tab go to next previous completion entry. we're
1330 * changing the key value */
1331 if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
1332 event->keyval = (event->state & GDK_SHIFT_MASK)
1333 ? GDK_Up : GDK_Down;
1334 /* need to reset shift state if going up */
1335 if (event->state & GDK_SHIFT_MASK)
1336 event->state &= ~GDK_SHIFT_MASK;
1337 completion_window_advance_selection(GTK_CLIST(clist),
1338 event->keyval == GDK_Down ? TRUE : FALSE);
1343 /* make tab move to next field */
1344 if( event->keyval == GDK_Tab ) {
1345 /* Reference to parent */
1346 parent = GTK_WIDGET(entry)->parent;
1348 /* Discard the window */
1349 clear_completion_cache();
1350 addrcompl_destroy_window( _compWindow_ );
1352 /* Move focus to next widget */
1354 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1359 /* make backtab move to previous field */
1360 if( event->keyval == GDK_ISO_Left_Tab ) {
1361 /* Reference to parent */
1362 parent = GTK_WIDGET(entry)->parent;
1364 /* Discard the window */
1365 clear_completion_cache();
1366 addrcompl_destroy_window( _compWindow_ );
1368 /* Move focus to previous widget */
1370 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1375 /* look for presses that accept the selection */
1376 if (event->keyval == GDK_Return || event->keyval == GDK_space) {
1377 /* User selected address with a key press */
1379 /* Display selected address in entry field */
1380 completion_window_apply_selection(
1381 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry) );
1383 /* Discard the window */
1384 clear_completion_cache();
1385 addrcompl_destroy_window( _compWindow_ );
1389 /* key state keys should never be handled */
1390 if (event->keyval == GDK_Shift_L
1391 || event->keyval == GDK_Shift_R
1392 || event->keyval == GDK_Control_L
1393 || event->keyval == GDK_Control_R
1394 || event->keyval == GDK_Caps_Lock
1395 || event->keyval == GDK_Shift_Lock
1396 || event->keyval == GDK_Meta_L
1397 || event->keyval == GDK_Meta_R
1398 || event->keyval == GDK_Alt_L
1399 || event->keyval == GDK_Alt_R) {
1403 /* some other key, let's restore the searchTerm (orignal text) */
1404 searchTerm = _compWindow_->searchTerm;
1405 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1406 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos);
1408 /* make sure anything we typed comes in the edit box */
1409 tmp_event.type = event->type;
1410 tmp_event.window = entry->window;
1411 tmp_event.send_event = TRUE;
1412 tmp_event.time = event->time;
1413 tmp_event.state = event->state;
1414 tmp_event.keyval = event->keyval;
1415 tmp_event.length = event->length;
1416 tmp_event.string = event->string;
1417 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1419 /* and close the completion window */
1420 clear_completion_cache();
1421 addrcompl_destroy_window( _compWindow_ );
1427 * ============================================================================
1428 * Publically accessible functions.
1429 * ============================================================================
1433 * Setup completion object.
1435 void addrcompl_initialize( void ) {
1436 /* printf( "addrcompl_initialize...\n" ); */
1437 if( ! _compWindow_ ) {
1438 _compWindow_ = addrcompl_create_window();
1441 _completionIdleID_ = 0;
1442 /* printf( "addrcompl_initialize...done\n" ); */
1446 * Teardown completion object.
1448 void addrcompl_teardown( void ) {
1449 /* printf( "addrcompl_teardown...\n" ); */
1450 addrcompl_free_window( _compWindow_ );
1451 _compWindow_ = NULL;
1452 if( _displayQueue_ ) {
1453 g_list_free( _displayQueue_ );
1455 _displayQueue_ = NULL;
1456 _completionIdleID_ = 0;
1457 /* printf( "addrcompl_teardown...done\n" ); */
1461 * tree view functions
1464 static GtkListStore *addr_compl_create_store(void)
1466 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1471 static void addr_compl_list_view_add_address(GtkWidget *list_view,
1472 const gchar *address)
1475 GtkListStore *store = GTK_LIST_STORE(gtk_tree_view_get_model
1476 (GTK_TREE_VIEW(list_view)));
1478 gtk_list_store_append(store, &iter);
1479 gtk_list_store_set(store, &iter,
1480 ADDR_COMPL_ADDRESS, address,
1484 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1486 GtkTreeView *list_view;
1487 GtkTreeSelection *selector;
1488 GtkTreeModel *model;
1490 model = GTK_TREE_MODEL(addr_compl_create_store());
1491 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1492 g_object_unref(model);
1494 gtk_tree_view_set_rules_hint(list_view, TRUE);
1495 gtk_tree_view_set_headers_visible(list_view, FALSE);
1497 selector = gtk_tree_view_get_selection(list_view);
1498 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1499 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1502 /* create the columns */
1503 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1505 return GTK_WIDGET(list_view);
1508 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1510 GtkTreeViewColumn *column;
1511 GtkCellRenderer *renderer;
1513 renderer = gtk_cell_renderer_text_new();
1514 column = gtk_tree_view_column_new_with_attributes
1515 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1516 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1519 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1520 CompletionWindow *window)
1522 if (window && event && event->type == GDK_BUTTON_PRESS) {
1523 window->in_mouse = TRUE;
1528 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1529 CompletionWindow *window)
1531 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1532 window->in_mouse = FALSE;
1537 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1538 GtkTreeModel *model,
1540 gboolean currently_selected,
1543 CompletionWindow *window = data;
1545 if (currently_selected)
1548 if (!window->in_mouse)
1551 /* XXX: select the entry and kill window later... select is called before
1552 * any other mouse events handlers including the tree view internal one;
1553 * not using a time out would result in a crash. if this doesn't work
1554 * safely, maybe we should set variables when receiving button presses
1555 * in the tree view. */
1556 if (!window->destroying) {
1557 window->destroying = TRUE;
1558 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1564 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1566 GtkEntry *entry = GTK_ENTRY(window->entry);
1568 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1571 clear_completion_cache();
1573 addrcompl_destroy_window(window);