2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
4 * Copyright (C) 2000-2007 by Alfons Hoogervorst & The Claws Mail Team.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 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
66 * The address book is read into memory. We set up an address list
67 * containing all address book entries. Next we make the completion
68 * list, which contains all the completable strings, and store a
69 * reference to the address entry it belongs to.
70 * After calling the g_completion_complete(), we get a reference
71 * to a valid email address.
73 * Completion is very simplified. We never complete on another prefix,
74 * i.e. we neglect the next smallest possible prefix for the current
75 * completion cache. This is simply done so we might break up the
76 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
77 * something like alfons, proteus, demon, nl; and then completing on
78 * any of those words).
82 * address_entry - structure which refers to the original address entry in the
93 * completion_entry - structure used to complete addresses, with a reference
94 * the the real address information.
98 gchar *string; /* string to complete */
99 address_entry *ref; /* address the string belongs to */
102 /*******************************************************************************/
104 static gint g_ref_count; /* list ref count */
105 static GList *g_completion_list = NULL; /* list of strings to be checked */
106 static GList *g_address_list = NULL; /* address storage */
107 static GCompletion *g_completion; /* completion object */
109 static GHashTable *_groupAddresses_ = NULL;
110 static gboolean _allowCommas_ = TRUE;
112 /* To allow for continuing completion we have to keep track of the state
113 * using the following variables. No need to create a context object. */
115 static gint g_completion_count; /* nr of addresses incl. the prefix */
116 static gint g_completion_next; /* next prev address */
117 static GSList *g_completion_addresses; /* unique addresses found in the
119 static gchar *g_completion_prefix; /* last prefix. (this is cached here
120 * because the prefix passed to g_completion
121 * is g_strdown()'ed */
123 static gchar *completion_folder_path = NULL;
125 /*******************************************************************************/
128 * Define the structure of the completion window.
130 typedef struct _CompletionWindow CompletionWindow;
131 struct _CompletionWindow {
136 GtkWidget *list_view;
138 gboolean in_mouse; /*!< mouse press pending... */
139 gboolean destroying; /*!< destruction in progress */
142 static GtkListStore *addr_compl_create_store (void);
144 static GtkWidget *addr_compl_list_view_create (CompletionWindow *window);
146 static void addr_compl_create_list_view_columns (GtkWidget *list_view);
148 static gboolean list_view_button_press (GtkWidget *widget,
149 GdkEventButton *event,
150 CompletionWindow *window);
152 static gboolean list_view_button_release (GtkWidget *widget,
153 GdkEventButton *event,
154 CompletionWindow *window);
156 static gboolean addr_compl_selected (GtkTreeSelection *selector,
159 gboolean currently_selected,
162 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window);
165 * Function used by GTK to find the string data to be used for completion.
166 * \param data Pointer to data being processed.
168 static gchar *completion_func(gpointer data)
170 g_return_val_if_fail(data != NULL, NULL);
172 return ((completion_entry *)data)->string;
176 * Initialize all completion index data.
178 static void init_all(void)
180 g_completion = g_completion_new(completion_func);
181 g_return_if_fail(g_completion != NULL);
184 static void free_all_addresses(void)
189 walk = g_address_list;
190 for (; walk != NULL; walk = g_list_next(walk)) {
191 address_entry *ae = (address_entry *) walk->data;
194 g_list_free(ae->grp_emails);
197 g_list_free(g_address_list);
198 g_address_list = NULL;
201 static void clear_completion_cache(void);
202 static void free_completion_list(void)
205 if (!g_completion_list)
208 clear_completion_cache();
210 g_completion_clear_items(g_completion);
212 walk = g_list_first(g_completion_list);
213 for (; walk != NULL; walk = g_list_next(walk)) {
214 completion_entry *ce = (completion_entry *) walk->data;
218 g_list_free(g_completion_list);
219 g_completion_list = NULL;
222 * Free up all completion index data.
224 static void free_all(void)
226 free_completion_list();
227 free_all_addresses();
228 g_completion_free(g_completion);
230 if (_groupAddresses_)
231 g_hash_table_destroy(_groupAddresses_);
232 _groupAddresses_ = NULL;
236 * Append specified address entry to the index.
237 * \param str Index string value.
238 * \param ae Entry containing address data.
240 static void add_address1(const char *str, address_entry *ae)
242 completion_entry *ce1;
243 ce1 = g_new0(completion_entry, 1),
244 /* GCompletion list is case sensitive */
245 ce1->string = g_utf8_strdown(str, -1);
248 g_completion_list = g_list_prepend(g_completion_list, ce1);
252 * Adds address to the completion list. This function looks complicated, but
253 * it's only allocation checks. Each value will be included in the index.
254 * \param name Recipient name.
255 * \param address EMail address.
256 * \param alias Alias to append.
257 * \param grp_emails the emails in case of a group. List should be freed later,
258 * but not its strings
259 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
262 static gint add_address(const gchar *name, const gchar *address,
263 const gchar *nick, const gchar *alias, GList *grp_emails)
266 gboolean is_group = FALSE;
268 if (!name || !address) {
269 if (!address && !nick && !alias && grp_emails) {
275 ae = g_new0(address_entry, 1);
277 g_return_val_if_fail(ae != NULL, -1);
279 ae->name = g_strdup(name);
280 ae->address = g_strdup(address);
281 ae->grp_emails = grp_emails;
282 g_address_list = g_list_prepend(g_address_list, ae);
284 add_address1(name, ae);
285 if (address != NULL && *address != '\0')
286 add_address1(address, ae);
288 if (nick != NULL && *nick != '\0')
289 add_address1(nick, ae);
291 if ( alias != NULL && *alias != '\0') {
292 add_address1(alias, ae);
299 * Read address book, creating all entries in the completion index.
301 static void read_address_book(gchar *folderpath) {
302 free_all_addresses();
303 free_completion_list();
305 addrindex_load_completion( add_address, folderpath );
306 g_address_list = g_list_reverse(g_address_list);
307 g_completion_list = g_list_reverse(g_completion_list);
308 /* merge the completion entry list into g_completion */
309 if (g_completion_list) {
310 g_completion_add_items(g_completion, g_completion_list);
311 if (debug_get_mode())
312 debug_print("read %d items in %s\n",
313 g_list_length(g_completion_list),
314 folderpath?folderpath:"(null)");
319 * Test whether there is a completion pending.
320 * \return <code>TRUE</code> if pending.
322 static gboolean is_completion_pending(void)
324 /* check if completion pending, i.e. we might satisfy a request for the next
325 * or previous address */
326 return g_completion_count;
330 * Clear the completion cache.
332 static void clear_completion_cache(void)
334 if (is_completion_pending()) {
335 g_free(g_completion_prefix);
337 if (g_completion_addresses) {
338 g_slist_free(g_completion_addresses);
339 g_completion_addresses = NULL;
342 g_completion_count = g_completion_next = 0;
347 * Prepare completion index. This function should be called prior to attempting
348 * address completion.
349 * \return The number of addresses in the completion list.
351 gint start_address_completion(gchar *folderpath)
353 gboolean different_book = FALSE;
354 clear_completion_cache();
356 if (strcmp2(completion_folder_path,folderpath))
357 different_book = TRUE;
359 g_free(completion_folder_path);
360 if (folderpath != NULL)
361 completion_folder_path = g_strdup(folderpath);
363 completion_folder_path = NULL;
367 /* open the address book */
368 read_address_book(folderpath);
369 } else if (different_book)
370 read_address_book(folderpath);
373 debug_print("start_address_completion(%s) ref count %d\n",
374 folderpath, g_ref_count);
376 return g_list_length(g_completion_list);
380 * Retrieve a possible address (or a part) from an entry box. To make life
381 * easier, we only look at the last valid address component; address
382 * completion only works at the last string component in the entry box.
384 * \param entry Address entry field.
385 * \param start_pos Address of start position of address.
386 * \return Possible address.
388 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
390 const gchar *edit_text, *p;
392 gboolean in_quote = FALSE;
393 gboolean in_bracket = FALSE;
396 edit_text = gtk_entry_get_text(entry);
397 if (edit_text == NULL) return NULL;
399 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
401 /* scan for a separator. doesn't matter if walk points at null byte. */
402 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
404 p = g_utf8_prev_char(p)) {
407 } else if (!in_quote) {
408 if (!in_bracket && *p == ',') {
410 } else if (*p == '<')
417 /* have something valid */
418 if (g_utf8_strlen(p, -1) == 0)
421 #define IS_VALID_CHAR(x) \
422 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
424 /* now scan back until we hit a valid character */
425 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
430 if (g_utf8_strlen(p, -1) == 0)
433 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
440 static gchar *get_complete_address_from_name_email(const gchar *name, const gchar *email)
442 gchar *address = NULL;
443 if (!name || name[0] == '\0')
444 address = g_strdup_printf("<%s>", email);
445 else if (strchr_with_skip_quote(name, '"', ','))
446 address = g_strdup_printf
447 ("\"%s\" <%s>", name, email);
449 address = g_strdup_printf
450 ("%s <%s>", name, email);
455 * Replace an incompleted address with a completed one.
456 * \param entry Address entry field.
457 * \param newtext New text.
458 * \param start_pos Insertion point in entry field.
460 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
461 gint start_pos, gboolean is_group, GList *grp_emails)
463 if (!newtext) return;
464 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
466 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
469 gchar *addresses = NULL;
470 GList *cur = grp_emails;
471 for (; cur; cur = cur->next) {
473 ItemEMail *email = (ItemEMail *)cur->data;
474 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
476 gchar *addr = get_complete_address_from_name_email(
477 ADDRITEM_NAME(person), email->address);
479 tmp = g_strdup_printf("%s, %s", addresses, addr);
481 tmp = g_strdup_printf("%s", addr);
486 gtk_editable_insert_text(GTK_EDITABLE(entry), addresses, strlen(addresses),
490 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
494 * Attempt to complete an address, and returns the number of addresses found.
495 * Use <code>get_complete_address()</code> to get an entry from the index.
497 * \param str Search string to find.
498 * \return Zero if no match was found, otherwise the number of addresses; the
499 * original prefix (search string) will appear at index 0.
501 guint complete_address(const gchar *str)
503 GList *result = NULL;
507 completion_entry *ce = NULL;
509 g_return_val_if_fail(str != NULL, 0);
511 /* g_completion is case sensitive */
512 d = g_utf8_strdown(str, -1);
514 clear_completion_cache();
515 g_completion_prefix = g_strdup(str);
517 result = g_completion_complete(g_completion, d, NULL);
519 count = g_list_length(result);
521 /* create list with unique addresses */
522 for (cpl = 0, result = g_list_first(result);
524 result = g_list_next(result)) {
525 ce = (completion_entry *)(result->data);
526 if (NULL == g_slist_find(g_completion_addresses,
529 g_completion_addresses =
530 g_slist_append(g_completion_addresses,
534 count = cpl + 1; /* index 0 is the original prefix */
535 g_completion_next = 1; /* we start at the first completed one */
537 g_free(g_completion_prefix);
538 g_completion_prefix = NULL;
541 g_completion_count = count;
549 * Return a complete address from the index.
550 * \param index Index of entry that was found (by the previous call to
551 * <code>complete_address()</code>
552 * \return Completed address string; this should be freed when done.
554 gchar *get_complete_address(gint index)
556 const address_entry *p;
557 gchar *address = NULL;
559 if (index < g_completion_count) {
561 address = g_strdup(g_completion_prefix);
563 /* get something from the unique addresses */
564 p = (address_entry *)g_slist_nth_data
565 (g_completion_addresses, index - 1);
566 if (p != NULL && p->address != NULL) {
567 address = get_complete_address_from_name_email(p->name, p->address);
568 } else if (p != NULL && p->address == NULL && p->name != NULL) {
570 address = g_strdup_printf("%s (%s) <!--___group___-->", p->name, _("Group"));
571 if (!_groupAddresses_) {
572 _groupAddresses_ = g_hash_table_new(NULL, g_direct_equal);
574 if (!g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)))) {
575 g_hash_table_insert(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)), p->grp_emails);
585 * Return the next complete address match from the completion index.
586 * \return Completed address string; this should be freed when done.
588 static gchar *get_next_complete_address(void)
590 if (is_completion_pending()) {
593 res = get_complete_address(g_completion_next);
594 g_completion_next += 1;
595 if (g_completion_next >= g_completion_count)
596 g_completion_next = 0;
604 * Return a count of the completed matches in the completion index.
605 * \return Number of matched entries.
607 static guint get_completion_count(void)
609 if (is_completion_pending())
610 return g_completion_count;
616 * Invalidate address completion index. This function should be called whenever
617 * the address book changes. This forces data to be read into the completion
619 * \return Number of entries in index.
621 gint invalidate_address_completion(void)
624 /* simply the same as start_address_completion() */
625 debug_print("Invalidation request for address completion\n");
626 read_address_book(completion_folder_path);
627 clear_completion_cache();
630 return g_list_length(g_completion_list);
634 * Finished with completion index. This function should be called after
635 * matching addresses.
636 * \return Reference count.
638 gint end_address_completion(void)
640 gboolean different_folder = FALSE;
641 clear_completion_cache();
643 /* reset the folderpath to NULL */
644 if (completion_folder_path) {
645 g_free(completion_folder_path);
646 completion_folder_path = NULL;
647 different_folder = TRUE;
649 if (0 == --g_ref_count)
652 debug_print("end_address_completion ref count %d\n", g_ref_count);
653 if (g_ref_count && different_folder) {
654 debug_print("still ref'd, different folder\n");
655 invalidate_address_completion();
664 static CompletionWindow *_compWindow_ = NULL;
667 * Mutex to protect callback from multiple threads.
669 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
672 * Completion queue list.
674 static GList *_displayQueue_ = NULL;
678 static gint _queryID_ = 0;
681 * Completion idle ID.
683 static guint _completionIdleID_ = 0;
686 * address completion entry ui. the ui (completion list was inspired by galeon's
687 * auto completion list). remaining things powered by claws's completion engine.
690 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
691 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
693 static void address_completion_mainwindow_set_focus (GtkWindow *window,
696 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
699 static gboolean address_completion_complete_address_in_entry
702 static void address_completion_create_completion_window (GtkEntry *entry);
704 static gboolean completion_window_button_press
706 GdkEventButton *event,
707 CompletionWindow *compWin );
709 static gboolean completion_window_key_press
712 CompletionWindow *compWin );
713 static void address_completion_create_completion_window( GtkEntry *entry_ );
716 * Create a completion window object.
717 * \return Initialized completion window.
719 static CompletionWindow *addrcompl_create_window( void ) {
720 CompletionWindow *cw;
722 cw = g_new0( CompletionWindow, 1 );
724 cw->searchTerm = NULL;
727 cw->list_view = NULL;
728 cw->in_mouse = FALSE;
729 cw->destroying = FALSE;
735 * Destroy completion window.
736 * \param cw Window to destroy.
738 static void addrcompl_destroy_window( CompletionWindow *cw ) {
739 /* Stop all searches currently in progress */
740 addrindex_stop_search( _queryID_ );
742 /* Remove idler function... or application may not terminate */
743 if( _completionIdleID_ != 0 ) {
744 g_source_remove( _completionIdleID_ );
745 _completionIdleID_ = 0;
748 /* Now destroy window */
750 /* Clear references to widgets */
752 cw->list_view = NULL;
756 gtk_widget_hide( cw->window );
757 gtk_widget_destroy( cw->window );
760 cw->destroying = FALSE;
761 cw->in_mouse = FALSE;
767 * Free up completion window.
768 * \param cw Window to free.
770 static void addrcompl_free_window( CompletionWindow *cw ) {
772 addrcompl_destroy_window( cw );
774 g_free( cw->searchTerm );
775 cw->searchTerm = NULL;
777 /* Clear references */
786 * Advance selection to previous/next item in list.
787 * \param list_view List to process.
788 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
791 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
793 GtkTreeSelection *selection;
797 g_return_if_fail(list_view != NULL);
799 selection = gtk_tree_view_get_selection(list_view);
800 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
804 forward = gtk_tree_model_iter_next(model, &iter);
806 gtk_tree_selection_select_iter(selection, &iter);
810 prev = gtk_tree_model_get_path(model, &iter);
814 if (gtk_tree_path_prev(prev))
815 gtk_tree_selection_select_path(selection, prev);
817 gtk_tree_path_free(prev);
822 * Resize window to accommodate maximum number of address entries.
823 * \param cw Completion window.
825 static void addrcompl_resize_window( CompletionWindow *cw ) {
827 gint x, y, width, height, depth;
829 /* Get current geometry of window */
830 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
832 gtk_widget_hide_all( cw->window );
833 gtk_widget_show_all( cw->window );
834 gtk_widget_size_request( cw->list_view, &r );
836 /* Adjust window height to available screen space */
837 if( ( y + r.height ) > gdk_screen_height() ) {
838 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
839 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
841 gtk_widget_set_size_request(cw->window, width, r.height);
844 static GdkPixbuf *group_pixbuf = NULL;
845 static GdkPixbuf *email_pixbuf = NULL;
848 * Add an address the completion window address list.
849 * \param cw Completion window.
850 * \param address Address to add.
852 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
855 GtkTreeSelection *selection;
856 gboolean is_group = FALSE;
857 GList *grp_emails = NULL;
858 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
862 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
864 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
866 /* printf( "\t\tAdding :%s\n", address ); */
867 if (strstr(address, " <!--___group___-->")) {
869 if (_groupAddresses_)
870 grp_emails = g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)));
871 *(strstr(address, " <!--___group___-->")) = '\0';
872 pixbuf = group_pixbuf;
873 } else if (strchr(address, '@') && strchr(address, '<') &&
874 strchr(address, '>')) {
875 pixbuf = email_pixbuf;
879 if (is_group && !_allowCommas_)
881 gtk_list_store_append(store, &iter);
882 gtk_list_store_set(store, &iter,
883 ADDR_COMPL_ICON, pixbuf,
884 ADDR_COMPL_ADDRESS, address,
885 ADDR_COMPL_ISGROUP, is_group,
886 ADDR_COMPL_GROUPLIST, grp_emails,
891 addrcompl_resize_window( cw );
892 gtk_grab_add( cw->window );
894 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
895 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
897 if( cw->listCount == 1 ) {
898 /* Select first row for now */
899 gtk_tree_selection_select_iter(selection, &iter);
901 else if( cw->listCount == 2 ) {
902 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
903 /* Move off first row */
904 gtk_tree_selection_select_iter(selection, &iter);
909 * Completion idle function. This function is called by the main (UI) thread
910 * during UI idle time while an address search is in progress. Items from the
911 * display queue are processed and appended to the address list.
913 * \param data Target completion window to receive email addresses.
914 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
916 static gboolean addrcompl_idle( gpointer data ) {
920 /* Process all entries in display queue */
921 pthread_mutex_lock( & _completionMutex_ );
922 if( _displayQueue_ ) {
923 node = _displayQueue_;
925 address = node->data;
926 /* printf( "address ::: %s :::\n", address ); */
927 addrcompl_add_entry( _compWindow_, address );
929 node = g_list_next( node );
931 g_list_free( _displayQueue_ );
932 _displayQueue_ = NULL;
934 pthread_mutex_unlock( & _completionMutex_ );
941 * Callback entry point. The background thread (if any) appends the address
942 * list to the display queue.
943 * \param sender Sender of query.
944 * \param queryID Query ID of search request.
945 * \param listEMail List of zero of more email objects that met search
947 * \param data Query data.
949 static gint addrcompl_callback_entry(
950 gpointer sender, gint queryID, GList *listEMail, gpointer data )
955 /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
956 pthread_mutex_lock( & _completionMutex_ );
957 if( queryID == _queryID_ ) {
958 /* Append contents to end of display queue */
961 ItemEMail *email = node->data;
963 address = addritem_format_email( email );
964 /* printf( "\temail/address ::%s::\n", address ); */
965 _displayQueue_ = g_list_append( _displayQueue_, address );
966 node = g_list_next( node );
969 g_list_free( listEMail );
970 pthread_mutex_unlock( & _completionMutex_ );
976 * Clear the display queue.
978 static void addrcompl_clear_queue( void ) {
979 /* Clear out display queue */
980 pthread_mutex_lock( & _completionMutex_ );
982 g_list_free( _displayQueue_ );
983 _displayQueue_ = NULL;
985 pthread_mutex_unlock( & _completionMutex_ );
989 * Add a single address entry into the display queue.
990 * \param address Address to append.
992 static void addrcompl_add_queue( gchar *address ) {
993 pthread_mutex_lock( & _completionMutex_ );
994 _displayQueue_ = g_list_append( _displayQueue_, address );
995 pthread_mutex_unlock( & _completionMutex_ );
999 * Load list with entries from local completion index.
1001 static void addrcompl_load_local( void ) {
1004 for (count = 0; count < get_completion_count(); count++) {
1007 address = get_complete_address( count );
1008 /* printf( "\taddress ::%s::\n", address ); */
1010 /* Append contents to end of display queue */
1011 addrcompl_add_queue( address );
1018 static void addrcompl_start_search( void ) {
1021 searchTerm = g_strdup( _compWindow_->searchTerm );
1023 /* Setup the search */
1024 _queryID_ = addrindex_setup_search(
1025 searchTerm, NULL, addrcompl_callback_entry );
1026 g_free( searchTerm );
1027 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1029 /* Load local stuff */
1030 addrcompl_load_local();
1032 /* Sit back and wait until something happens */
1033 _completionIdleID_ =
1034 g_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
1035 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1037 addrindex_start_search( _queryID_ );
1041 * Apply the current selection in the list to the entry field. Focus is also
1042 * moved to the next widget so that Tab key works correctly.
1043 * \param list_view List to process.
1044 * \param entry Address entry field.
1045 * \param move_focus Move focus to the next widget ?
1047 static void completion_window_apply_selection(GtkTreeView *list_view,
1049 gboolean move_focus)
1051 gchar *address = NULL, *text = NULL;
1054 GtkTreeSelection *selection;
1055 GtkTreeModel *model;
1057 gboolean is_group = FALSE;
1058 g_return_if_fail(list_view != NULL);
1059 g_return_if_fail(entry != NULL);
1060 GList *grp_emails = NULL;
1062 selection = gtk_tree_view_get_selection(list_view);
1063 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
1066 /* First remove the idler */
1067 if( _completionIdleID_ != 0 ) {
1068 g_source_remove( _completionIdleID_ );
1069 _completionIdleID_ = 0;
1072 /* Process selected item */
1073 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text,
1074 ADDR_COMPL_ISGROUP, &is_group,
1075 ADDR_COMPL_GROUPLIST, &grp_emails,
1078 address = get_address_from_edit(entry, &cursor_pos);
1080 replace_address_in_edit(entry, text, cursor_pos, is_group, grp_emails);
1083 /* Move focus to next widget */
1084 parent = GTK_WIDGET(entry)->parent;
1085 if( parent && move_focus) {
1086 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1091 * Start address completion. Should be called when creating the main window
1092 * containing address completion entries.
1093 * \param mainwindow Main window.
1095 void address_completion_start(GtkWidget *mainwindow)
1097 start_address_completion(NULL);
1099 /* register focus change hook */
1100 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
1101 G_CALLBACK(address_completion_mainwindow_set_focus),
1106 * Need unique data to make unregistering signal handler possible for the auto
1109 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1112 * Register specified entry widget for address completion.
1113 * \param entry Address entry field.
1115 void address_completion_register_entry(GtkEntry *entry, gboolean allow_commas)
1117 g_return_if_fail(entry != NULL);
1118 g_return_if_fail(GTK_IS_ENTRY(entry));
1120 /* add hooked property */
1121 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1122 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS, GINT_TO_POINTER(allow_commas));
1124 /* add keypress event */
1125 g_signal_connect_closure
1126 (G_OBJECT(entry), "key_press_event",
1127 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1128 COMPLETION_UNIQUE_DATA,
1134 * Unregister specified entry widget from address completion operations.
1135 * \param entry Address entry field.
1137 void address_completion_unregister_entry(GtkEntry *entry)
1139 GtkObject *entry_obj;
1141 g_return_if_fail(entry != NULL);
1142 g_return_if_fail(GTK_IS_ENTRY(entry));
1144 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1145 g_return_if_fail(entry_obj);
1146 g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1148 /* has the hooked property? */
1149 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1151 /* remove the hook */
1152 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1153 G_CALLBACK(address_completion_entry_key_pressed),
1154 COMPLETION_UNIQUE_DATA);
1158 * End address completion. Should be called when main window with address
1159 * completion entries terminates. NOTE: this function assumes that it is
1160 * called upon destruction of the window.
1161 * \param mainwindow Main window.
1163 void address_completion_end(GtkWidget *mainwindow)
1165 /* if address_completion_end() is really called on closing the window,
1166 * we don't need to unregister the set_focus_cb */
1167 end_address_completion();
1170 /* if focus changes to another entry, then clear completion cache */
1171 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1176 if (widget && GTK_IS_ENTRY(widget) &&
1177 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1178 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), ENTRY_DATA_ALLOW_COMMAS));
1179 clear_completion_cache();
1184 * Listener that watches for tab or other keystroke in address entry field.
1185 * \param entry Address entry field.
1186 * \param ev Event object.
1187 * \param data User data.
1188 * \return <i>TRUE</i>.
1190 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1194 if (ev->keyval == GDK_Tab) {
1195 addrcompl_clear_queue();
1196 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1197 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1198 /* route a void character to the default handler */
1199 /* this is a dirty hack; we're actually changing a key
1200 * reported by the system. */
1201 ev->keyval = GDK_AudibleBell_Enable;
1202 ev->state &= ~GDK_SHIFT_MASK;
1205 address_completion_create_completion_window(entry);
1207 /* Start remote queries */
1208 addrcompl_start_search();
1215 } else if (ev->keyval == GDK_Shift_L
1216 || ev->keyval == GDK_Shift_R
1217 || ev->keyval == GDK_Control_L
1218 || ev->keyval == GDK_Control_R
1219 || ev->keyval == GDK_Caps_Lock
1220 || ev->keyval == GDK_Shift_Lock
1221 || ev->keyval == GDK_Meta_L
1222 || ev->keyval == GDK_Meta_R
1223 || ev->keyval == GDK_Alt_L
1224 || ev->keyval == GDK_Alt_R) {
1225 /* these buttons should not clear the cache... */
1227 clear_completion_cache();
1232 * Initialize search term for address completion.
1233 * \param entry Address entry field.
1235 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1238 gint ncount, cursor_pos;
1239 gchar *searchTerm, *new = NULL;
1241 g_return_val_if_fail(entry != NULL, FALSE);
1243 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1245 /* get an address component from the cursor */
1246 searchTerm = get_address_from_edit( entry, &cursor_pos );
1247 if( ! searchTerm ) return FALSE;
1248 /* printf( "search for :::%s:::\n", searchTerm ); */
1250 /* Clear any existing search */
1251 g_free( _compWindow_->searchTerm );
1252 _compWindow_->searchTerm = g_strdup( searchTerm );
1254 /* Perform search on local completion index */
1255 ncount = complete_address( searchTerm );
1257 new = get_next_complete_address();
1261 /* Select the address if there is only one match */
1263 /* Display selected address in entry field */
1264 gchar *addr = get_complete_address(1);
1265 if (addr && !strstr(addr, " <!--___group___-->")) {
1266 replace_address_in_edit(entry, addr, cursor_pos, FALSE, NULL);
1267 /* Discard the window */
1268 clear_completion_cache();
1272 /* Make sure that drop-down appears uniform! */
1276 addrcompl_add_queue( g_strdup( searchTerm ) );
1278 g_free( searchTerm );
1284 * Create new address completion window for specified entry.
1285 * \param entry_ Entry widget to associate with window.
1287 static void address_completion_create_completion_window( GtkEntry *entry_ )
1289 gint x, y, height, width, depth;
1290 GtkWidget *scroll, *list_view;
1293 GtkWidget *entry = GTK_WIDGET(entry_);
1295 /* Create new window and list */
1296 window = gtk_window_new(GTK_WINDOW_POPUP);
1297 list_view = addr_compl_list_view_create(_compWindow_);
1299 /* Destroy any existing window */
1300 addrcompl_destroy_window( _compWindow_ );
1302 /* Create new object */
1303 _compWindow_->window = window;
1304 _compWindow_->entry = entry;
1305 _compWindow_->list_view = list_view;
1306 _compWindow_->listCount = 0;
1307 _compWindow_->in_mouse = FALSE;
1309 scroll = gtk_scrolled_window_new(NULL, NULL);
1310 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1311 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1312 gtk_container_add(GTK_CONTAINER(window), scroll);
1313 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1314 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
1316 /* Use entry widget to create initial window */
1317 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1318 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1320 gtk_window_move(GTK_WINDOW(window), x, y);
1322 /* Resize window to fit initial (empty) address list */
1323 gtk_widget_size_request( list_view, &r );
1324 gtk_widget_set_size_request( window, width, r.height );
1325 gtk_widget_show_all( window );
1326 gtk_widget_size_request( list_view, &r );
1328 /* Setup handlers */
1329 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1330 G_CALLBACK(list_view_button_press),
1333 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1334 G_CALLBACK(list_view_button_release),
1337 g_signal_connect(G_OBJECT(window),
1338 "button-press-event",
1339 G_CALLBACK(completion_window_button_press),
1341 g_signal_connect(G_OBJECT(window),
1343 G_CALLBACK(completion_window_key_press),
1345 gdk_pointer_grab(window->window, TRUE,
1346 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1347 GDK_BUTTON_RELEASE_MASK,
1348 NULL, NULL, GDK_CURRENT_TIME);
1349 gtk_grab_add( window );
1353 * GTK1: this gets rid of the irritating focus rectangle that doesn't
1354 * follow the selection */
1355 GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
1359 * Respond to button press in completion window. Check if mouse click is
1360 * anywhere outside the completion window. In that case the completion
1361 * window is destroyed, and the original searchTerm is restored.
1363 * \param widget Window object.
1364 * \param event Event.
1365 * \param compWin Reference to completion window.
1367 static gboolean completion_window_button_press(GtkWidget *widget,
1368 GdkEventButton *event,
1369 CompletionWindow *compWin )
1371 GtkWidget *event_widget, *entry;
1374 gboolean restore = TRUE;
1376 g_return_val_if_fail(compWin != NULL, FALSE);
1378 entry = compWin->entry;
1379 g_return_val_if_fail(entry != NULL, FALSE);
1381 /* Test where mouse was clicked */
1382 event_widget = gtk_get_event_widget((GdkEvent *)event);
1383 if (event_widget != widget) {
1384 while (event_widget) {
1385 if (event_widget == widget)
1387 else if (event_widget == entry) {
1391 event_widget = event_widget->parent;
1396 /* Clicked outside of completion window - restore */
1397 searchTerm = _compWindow_->searchTerm;
1398 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1399 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1402 clear_completion_cache();
1403 addrcompl_destroy_window( _compWindow_ );
1409 * Respond to key press in completion window.
1410 * \param widget Window object.
1411 * \param event Event.
1412 * \param compWind Reference to completion window.
1414 static gboolean completion_window_key_press(GtkWidget *widget,
1416 CompletionWindow *compWin )
1418 GdkEventKey tmp_event;
1422 GtkWidget *list_view;
1424 g_return_val_if_fail(compWin != NULL, FALSE);
1426 entry = compWin->entry;
1427 list_view = compWin->list_view;
1428 g_return_val_if_fail(entry != NULL, FALSE);
1430 /* allow keyboard navigation in the alternatives tree view */
1431 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1432 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1433 completion_window_advance_selection
1434 (GTK_TREE_VIEW(list_view),
1435 event->keyval == GDK_Down ||
1436 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1440 /* make tab move to next field */
1441 if( event->keyval == GDK_Tab ) {
1442 /* Reference to parent */
1443 parent = GTK_WIDGET(entry)->parent;
1445 /* Discard the window */
1446 clear_completion_cache();
1447 addrcompl_destroy_window( _compWindow_ );
1449 /* Move focus to next widget */
1451 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1456 /* make backtab move to previous field */
1457 if( event->keyval == GDK_ISO_Left_Tab ) {
1458 /* Reference to parent */
1459 parent = GTK_WIDGET(entry)->parent;
1461 /* Discard the window */
1462 clear_completion_cache();
1463 addrcompl_destroy_window( _compWindow_ );
1465 /* Move focus to previous widget */
1467 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1471 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1473 /* look for presses that accept the selection */
1474 if (event->keyval == GDK_Return || event->keyval == GDK_space
1475 || (_allowCommas_ && event->keyval == GDK_comma)) {
1476 /* User selected address with a key press */
1478 /* Display selected address in entry field */
1479 completion_window_apply_selection(
1480 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry),
1481 event->keyval != GDK_comma);
1483 if (event->keyval == GDK_comma) {
1484 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
1485 gtk_editable_insert_text(GTK_EDITABLE(entry), ", ", 2, &pos);
1486 gtk_editable_set_position(GTK_EDITABLE(entry), pos + 1);
1489 /* Discard the window */
1490 clear_completion_cache();
1491 addrcompl_destroy_window( _compWindow_ );
1495 /* key state keys should never be handled */
1496 if (event->keyval == GDK_Shift_L
1497 || event->keyval == GDK_Shift_R
1498 || event->keyval == GDK_Control_L
1499 || event->keyval == GDK_Control_R
1500 || event->keyval == GDK_Caps_Lock
1501 || event->keyval == GDK_Shift_Lock
1502 || event->keyval == GDK_Meta_L
1503 || event->keyval == GDK_Meta_R
1504 || event->keyval == GDK_Alt_L
1505 || event->keyval == GDK_Alt_R) {
1509 /* some other key, let's restore the searchTerm (orignal text) */
1510 searchTerm = _compWindow_->searchTerm;
1511 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1512 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1514 /* make sure anything we typed comes in the edit box */
1515 tmp_event.type = event->type;
1516 tmp_event.window = entry->window;
1517 tmp_event.send_event = TRUE;
1518 tmp_event.time = event->time;
1519 tmp_event.state = event->state;
1520 tmp_event.keyval = event->keyval;
1521 tmp_event.length = event->length;
1522 tmp_event.string = event->string;
1523 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1525 /* and close the completion window */
1526 clear_completion_cache();
1527 addrcompl_destroy_window( _compWindow_ );
1533 * ============================================================================
1534 * Publically accessible functions.
1535 * ============================================================================
1539 * Setup completion object.
1541 void addrcompl_initialize( void ) {
1542 /* printf( "addrcompl_initialize...\n" ); */
1543 if( ! _compWindow_ ) {
1544 _compWindow_ = addrcompl_create_window();
1547 _completionIdleID_ = 0;
1548 /* printf( "addrcompl_initialize...done\n" ); */
1552 * Teardown completion object.
1554 void addrcompl_teardown( void ) {
1555 /* printf( "addrcompl_teardown...\n" ); */
1556 addrcompl_free_window( _compWindow_ );
1557 _compWindow_ = NULL;
1558 if( _displayQueue_ ) {
1559 g_list_free( _displayQueue_ );
1561 _displayQueue_ = NULL;
1562 _completionIdleID_ = 0;
1563 /* printf( "addrcompl_teardown...done\n" ); */
1567 * tree view functions
1570 static GtkListStore *addr_compl_create_store(void)
1572 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1580 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1582 GtkTreeView *list_view;
1583 GtkTreeSelection *selector;
1584 GtkTreeModel *model;
1586 model = GTK_TREE_MODEL(addr_compl_create_store());
1587 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1588 g_object_unref(model);
1590 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1591 gtk_tree_view_set_headers_visible(list_view, FALSE);
1593 selector = gtk_tree_view_get_selection(list_view);
1594 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1595 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1598 /* create the columns */
1599 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1601 return GTK_WIDGET(list_view);
1604 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1606 GtkTreeViewColumn *column;
1607 GtkCellRenderer *renderer;
1609 renderer = gtk_cell_renderer_pixbuf_new();
1610 column = gtk_tree_view_column_new_with_attributes
1612 "pixbuf", ADDR_COMPL_ICON, NULL);
1613 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1614 renderer = gtk_cell_renderer_text_new();
1615 column = gtk_tree_view_column_new_with_attributes
1616 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1617 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1620 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1621 CompletionWindow *window)
1623 if (window && event && event->type == GDK_BUTTON_PRESS) {
1624 window->in_mouse = TRUE;
1629 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1630 CompletionWindow *window)
1632 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1633 window->in_mouse = FALSE;
1638 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1639 GtkTreeModel *model,
1641 gboolean currently_selected,
1644 CompletionWindow *window = data;
1646 if (currently_selected)
1649 if (!window->in_mouse)
1652 /* XXX: select the entry and kill window later... select is called before
1653 * any other mouse events handlers including the tree view internal one;
1654 * not using a time out would result in a crash. if this doesn't work
1655 * safely, maybe we should set variables when receiving button presses
1656 * in the tree view. */
1657 if (!window->destroying) {
1658 window->destroying = TRUE;
1659 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1665 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1667 GtkEntry *entry = GTK_ENTRY(window->entry);
1669 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1672 clear_completion_cache();
1674 addrcompl_destroy_window(window);