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 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
199 if (_groupAddresses_)
200 g_hash_table_destroy(_groupAddresses_);
201 _groupAddresses_ = NULL;
204 static void clear_completion_cache(void);
205 static void free_completion_list(void)
208 if (!g_completion_list)
211 clear_completion_cache();
213 g_completion_clear_items(g_completion);
215 walk = g_list_first(g_completion_list);
216 for (; walk != NULL; walk = g_list_next(walk)) {
217 completion_entry *ce = (completion_entry *) walk->data;
221 g_list_free(g_completion_list);
222 g_completion_list = NULL;
225 * Free up all completion index data.
227 static void free_all(void)
229 free_completion_list();
230 free_all_addresses();
231 g_completion_free(g_completion);
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?folderpath:"(null)", 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 * complete_matches_found() returns the number of matched addresses according
550 * to the completion mechanism. Unlike complete_address(), the returned value
551 * doesn't count str itself. If there's no match, it returns 0.
552 * To get a list of completion matches, see complete_address() instead.
554 guint complete_matches_found(const gchar *str)
556 GList *result = NULL;
559 g_return_val_if_fail(str != NULL, 0);
561 /* g_completion is case sensitive */
562 d = g_utf8_strdown(str, -1);
564 clear_completion_cache();
565 g_completion_prefix = g_strdup(str);
567 result = g_completion_complete(g_completion, d, NULL);
569 g_free(g_completion_prefix);
572 return g_list_length(result);
576 * Return a complete address from the index.
577 * \param index Index of entry that was found (by the previous call to
578 * <code>complete_address()</code>
579 * \return Completed address string; this should be freed when done.
581 gchar *get_complete_address(gint index)
583 const address_entry *p;
584 gchar *address = NULL;
586 if (index < g_completion_count) {
588 address = g_strdup(g_completion_prefix);
590 /* get something from the unique addresses */
591 p = (address_entry *)g_slist_nth_data
592 (g_completion_addresses, index - 1);
593 if (p != NULL && p->address != NULL) {
594 address = get_complete_address_from_name_email(p->name, p->address);
595 } else if (p != NULL && p->address == NULL && p->name != NULL) {
597 address = g_strdup_printf("%s (%s) <!--___group___-->", p->name, _("Group"));
598 if (!_groupAddresses_) {
599 _groupAddresses_ = g_hash_table_new(NULL, g_direct_equal);
601 if (!g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)))) {
602 g_hash_table_insert(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)), p->grp_emails);
613 * Return the next complete address match from the completion index.
614 * \return Completed address string; this should be freed when done.
616 static gchar *get_next_complete_address(void)
618 if (is_completion_pending()) {
621 res = get_complete_address(g_completion_next);
622 g_completion_next += 1;
623 if (g_completion_next >= g_completion_count)
624 g_completion_next = 0;
632 * Return a count of the completed matches in the completion index.
633 * \return Number of matched entries.
635 static guint get_completion_count(void)
637 if (is_completion_pending())
638 return g_completion_count;
644 * Invalidate address completion index. This function should be called whenever
645 * the address book changes. This forces data to be read into the completion
647 * \return Number of entries in index.
649 gint invalidate_address_completion(void)
652 /* simply the same as start_address_completion() */
653 debug_print("Invalidation request for address completion\n");
654 read_address_book(completion_folder_path);
655 clear_completion_cache();
658 return g_list_length(g_completion_list);
662 * Finished with completion index. This function should be called after
663 * matching addresses.
664 * \return Reference count.
666 gint end_address_completion(void)
668 gboolean different_folder = FALSE;
669 clear_completion_cache();
671 /* reset the folderpath to NULL */
672 if (completion_folder_path) {
673 g_free(completion_folder_path);
674 completion_folder_path = NULL;
675 different_folder = TRUE;
677 if (0 == --g_ref_count)
680 debug_print("end_address_completion ref count %d\n", g_ref_count);
681 if (g_ref_count && different_folder) {
682 debug_print("still ref'd, different folder\n");
683 invalidate_address_completion();
692 static CompletionWindow *_compWindow_ = NULL;
695 * Mutex to protect callback from multiple threads.
697 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
700 * Completion queue list.
702 static GList *_displayQueue_ = NULL;
706 static gint _queryID_ = 0;
709 * Completion idle ID.
711 static guint _completionIdleID_ = 0;
714 * address completion entry ui. the ui (completion list was inspired by galeon's
715 * auto completion list). remaining things powered by claws's completion engine.
718 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
719 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
721 static void address_completion_mainwindow_set_focus (GtkWindow *window,
724 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
727 static gboolean address_completion_complete_address_in_entry
730 static void address_completion_create_completion_window (GtkEntry *entry);
732 static gboolean completion_window_button_press
734 GdkEventButton *event,
735 CompletionWindow *compWin );
737 static gboolean completion_window_key_press
740 CompletionWindow *compWin );
741 static void address_completion_create_completion_window( GtkEntry *entry_ );
744 * Create a completion window object.
745 * \return Initialized completion window.
747 static CompletionWindow *addrcompl_create_window( void ) {
748 CompletionWindow *cw;
750 cw = g_new0( CompletionWindow, 1 );
752 cw->searchTerm = NULL;
755 cw->list_view = NULL;
756 cw->in_mouse = FALSE;
757 cw->destroying = FALSE;
763 * Destroy completion window.
764 * \param cw Window to destroy.
766 static void addrcompl_destroy_window( CompletionWindow *cw ) {
767 /* Stop all searches currently in progress */
768 addrindex_stop_search( _queryID_ );
770 /* Remove idler function... or application may not terminate */
771 if( _completionIdleID_ != 0 ) {
772 g_source_remove( _completionIdleID_ );
773 _completionIdleID_ = 0;
776 /* Now destroy window */
778 /* Clear references to widgets */
780 cw->list_view = NULL;
784 gtk_widget_hide( cw->window );
785 gtk_widget_destroy( cw->window );
788 cw->destroying = FALSE;
789 cw->in_mouse = FALSE;
795 * Free up completion window.
796 * \param cw Window to free.
798 static void addrcompl_free_window( CompletionWindow *cw ) {
800 addrcompl_destroy_window( cw );
802 g_free( cw->searchTerm );
803 cw->searchTerm = NULL;
805 /* Clear references */
814 * Advance selection to previous/next item in list.
815 * \param list_view List to process.
816 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
819 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
821 GtkTreeSelection *selection;
825 g_return_if_fail(list_view != NULL);
827 selection = gtk_tree_view_get_selection(list_view);
828 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
832 forward = gtk_tree_model_iter_next(model, &iter);
834 gtk_tree_selection_select_iter(selection, &iter);
838 prev = gtk_tree_model_get_path(model, &iter);
842 if (gtk_tree_path_prev(prev))
843 gtk_tree_selection_select_path(selection, prev);
845 gtk_tree_path_free(prev);
850 * Resize window to accommodate maximum number of address entries.
851 * \param cw Completion window.
853 static void addrcompl_resize_window( CompletionWindow *cw ) {
855 gint x, y, width, height, depth;
857 /* Get current geometry of window */
858 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
860 gtk_widget_hide_all( cw->window );
861 gtk_widget_show_all( cw->window );
862 gtk_widget_size_request( cw->list_view, &r );
864 /* Adjust window height to available screen space */
865 if( ( y + r.height ) > gdk_screen_height() ) {
866 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
867 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
869 gtk_widget_set_size_request(cw->window, width, r.height);
872 static GdkPixbuf *group_pixbuf = NULL;
873 static GdkPixbuf *email_pixbuf = NULL;
876 * Add an address the completion window address list.
877 * \param cw Completion window.
878 * \param address Address to add.
880 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
883 GtkTreeSelection *selection;
884 gboolean is_group = FALSE;
885 GList *grp_emails = NULL;
886 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
890 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
891 g_object_ref(G_OBJECT(group_pixbuf));
894 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
895 g_object_ref(G_OBJECT(email_pixbuf));
897 /* printf( "\t\tAdding :%s\n", address ); */
898 if (strstr(address, " <!--___group___-->")) {
900 if (_groupAddresses_)
901 grp_emails = g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)));
902 *(strstr(address, " <!--___group___-->")) = '\0';
903 pixbuf = group_pixbuf;
904 } else if (strchr(address, '@') && strchr(address, '<') &&
905 strchr(address, '>')) {
906 pixbuf = email_pixbuf;
910 if (is_group && !_allowCommas_)
912 gtk_list_store_append(store, &iter);
913 gtk_list_store_set(store, &iter,
914 ADDR_COMPL_ICON, pixbuf,
915 ADDR_COMPL_ADDRESS, address,
916 ADDR_COMPL_ISGROUP, is_group,
917 ADDR_COMPL_GROUPLIST, grp_emails,
922 addrcompl_resize_window( cw );
923 gtk_grab_add( cw->window );
925 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
926 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
928 if( cw->listCount == 1 ) {
929 /* Select first row for now */
930 gtk_tree_selection_select_iter(selection, &iter);
933 else if( cw->listCount == 2 ) {
934 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
935 /* Move off first row */
936 gtk_tree_selection_select_iter(selection, &iter);
942 * Completion idle function. This function is called by the main (UI) thread
943 * during UI idle time while an address search is in progress. Items from the
944 * display queue are processed and appended to the address list.
946 * \param data Target completion window to receive email addresses.
947 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
949 static gboolean addrcompl_idle( gpointer data ) {
953 /* Process all entries in display queue */
954 pthread_mutex_lock( & _completionMutex_ );
955 if( _displayQueue_ ) {
956 node = _displayQueue_;
958 address = node->data;
959 /* printf( "address ::: %s :::\n", address ); */
960 addrcompl_add_entry( _compWindow_, address );
962 node = g_list_next( node );
964 g_list_free( _displayQueue_ );
965 _displayQueue_ = NULL;
967 pthread_mutex_unlock( & _completionMutex_ );
974 * Callback entry point. The background thread (if any) appends the address
975 * list to the display queue.
976 * \param sender Sender of query.
977 * \param queryID Query ID of search request.
978 * \param listEMail List of zero of more email objects that met search
980 * \param data Query data.
982 static gint addrcompl_callback_entry(
983 gpointer sender, gint queryID, GList *listEMail, gpointer data )
988 /* printf( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
989 pthread_mutex_lock( & _completionMutex_ );
990 if( queryID == _queryID_ ) {
991 /* Append contents to end of display queue */
994 ItemEMail *email = node->data;
996 address = addritem_format_email( email );
997 /* printf( "\temail/address ::%s::\n", address ); */
998 _displayQueue_ = g_list_append( _displayQueue_, address );
999 node = g_list_next( node );
1002 g_list_free( listEMail );
1003 pthread_mutex_unlock( & _completionMutex_ );
1009 * Clear the display queue.
1011 static void addrcompl_clear_queue( void ) {
1012 /* Clear out display queue */
1013 pthread_mutex_lock( & _completionMutex_ );
1015 g_list_free( _displayQueue_ );
1016 _displayQueue_ = NULL;
1018 pthread_mutex_unlock( & _completionMutex_ );
1022 * Add a single address entry into the display queue.
1023 * \param address Address to append.
1025 static void addrcompl_add_queue( gchar *address ) {
1026 pthread_mutex_lock( & _completionMutex_ );
1027 _displayQueue_ = g_list_append( _displayQueue_, address );
1028 pthread_mutex_unlock( & _completionMutex_ );
1032 * Load list with entries from local completion index.
1034 static void addrcompl_load_local( void ) {
1037 for (count = 0; count < get_completion_count(); count++) {
1040 address = get_complete_address( count );
1041 /* printf( "\taddress ::%s::\n", address ); */
1043 /* Append contents to end of display queue */
1044 addrcompl_add_queue( address );
1051 static void addrcompl_start_search( void ) {
1054 searchTerm = g_strdup( _compWindow_->searchTerm );
1056 /* Setup the search */
1057 _queryID_ = addrindex_setup_search(
1058 searchTerm, NULL, addrcompl_callback_entry );
1059 g_free( searchTerm );
1060 /* printf( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1062 /* Load local stuff */
1063 addrcompl_load_local();
1065 /* Sit back and wait until something happens */
1066 _completionIdleID_ =
1067 g_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
1068 /* printf( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1070 addrindex_start_search( _queryID_ );
1074 * Apply the current selection in the list to the entry field. Focus is also
1075 * moved to the next widget so that Tab key works correctly.
1076 * \param list_view List to process.
1077 * \param entry Address entry field.
1078 * \param move_focus Move focus to the next widget ?
1080 static void completion_window_apply_selection(GtkTreeView *list_view,
1082 gboolean move_focus)
1084 gchar *address = NULL, *text = NULL;
1087 GtkTreeSelection *selection;
1088 GtkTreeModel *model;
1090 gboolean is_group = FALSE;
1091 g_return_if_fail(list_view != NULL);
1092 g_return_if_fail(entry != NULL);
1093 GList *grp_emails = NULL;
1095 selection = gtk_tree_view_get_selection(list_view);
1096 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
1099 /* First remove the idler */
1100 if( _completionIdleID_ != 0 ) {
1101 g_source_remove( _completionIdleID_ );
1102 _completionIdleID_ = 0;
1105 /* Process selected item */
1106 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text,
1107 ADDR_COMPL_ISGROUP, &is_group,
1108 ADDR_COMPL_GROUPLIST, &grp_emails,
1111 address = get_address_from_edit(entry, &cursor_pos);
1113 replace_address_in_edit(entry, text, cursor_pos, is_group, grp_emails);
1116 /* Move focus to next widget */
1117 parent = GTK_WIDGET(entry)->parent;
1118 if( parent && move_focus) {
1119 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1124 * Start address completion. Should be called when creating the main window
1125 * containing address completion entries.
1126 * \param mainwindow Main window.
1128 void address_completion_start(GtkWidget *mainwindow)
1130 start_address_completion(NULL);
1132 /* register focus change hook */
1133 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
1134 G_CALLBACK(address_completion_mainwindow_set_focus),
1139 * Need unique data to make unregistering signal handler possible for the auto
1142 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1145 * Register specified entry widget for address completion.
1146 * \param entry Address entry field.
1148 void address_completion_register_entry(GtkEntry *entry, gboolean allow_commas)
1150 g_return_if_fail(entry != NULL);
1151 g_return_if_fail(GTK_IS_ENTRY(entry));
1153 /* add hooked property */
1154 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1155 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS, GINT_TO_POINTER(allow_commas));
1157 /* add keypress event */
1158 g_signal_connect_closure
1159 (G_OBJECT(entry), "key_press_event",
1160 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1161 COMPLETION_UNIQUE_DATA,
1167 * Unregister specified entry widget from address completion operations.
1168 * \param entry Address entry field.
1170 void address_completion_unregister_entry(GtkEntry *entry)
1172 GtkObject *entry_obj;
1174 g_return_if_fail(entry != NULL);
1175 g_return_if_fail(GTK_IS_ENTRY(entry));
1177 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1178 g_return_if_fail(entry_obj);
1179 g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1181 /* has the hooked property? */
1182 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1184 /* remove the hook */
1185 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1186 G_CALLBACK(address_completion_entry_key_pressed),
1187 COMPLETION_UNIQUE_DATA);
1191 * End address completion. Should be called when main window with address
1192 * completion entries terminates. NOTE: this function assumes that it is
1193 * called upon destruction of the window.
1194 * \param mainwindow Main window.
1196 void address_completion_end(GtkWidget *mainwindow)
1198 /* if address_completion_end() is really called on closing the window,
1199 * we don't need to unregister the set_focus_cb */
1200 end_address_completion();
1203 /* if focus changes to another entry, then clear completion cache */
1204 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1209 if (widget && GTK_IS_ENTRY(widget) &&
1210 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1211 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), ENTRY_DATA_ALLOW_COMMAS));
1212 clear_completion_cache();
1217 * Listener that watches for tab or other keystroke in address entry field.
1218 * \param entry Address entry field.
1219 * \param ev Event object.
1220 * \param data User data.
1221 * \return <i>TRUE</i>.
1223 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1227 if (ev->keyval == GDK_Tab) {
1228 addrcompl_clear_queue();
1229 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1230 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1231 /* route a void character to the default handler */
1232 /* this is a dirty hack; we're actually changing a key
1233 * reported by the system. */
1234 ev->keyval = GDK_AudibleBell_Enable;
1235 ev->state &= ~GDK_SHIFT_MASK;
1238 address_completion_create_completion_window(entry);
1240 /* Start remote queries */
1241 addrcompl_start_search();
1248 } else if (ev->keyval == GDK_Shift_L
1249 || ev->keyval == GDK_Shift_R
1250 || ev->keyval == GDK_Control_L
1251 || ev->keyval == GDK_Control_R
1252 || ev->keyval == GDK_Caps_Lock
1253 || ev->keyval == GDK_Shift_Lock
1254 || ev->keyval == GDK_Meta_L
1255 || ev->keyval == GDK_Meta_R
1256 || ev->keyval == GDK_Alt_L
1257 || ev->keyval == GDK_Alt_R) {
1258 /* these buttons should not clear the cache... */
1260 clear_completion_cache();
1265 * Initialize search term for address completion.
1266 * \param entry Address entry field.
1268 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1271 gint ncount, cursor_pos;
1272 gchar *searchTerm, *new = NULL;
1274 g_return_val_if_fail(entry != NULL, FALSE);
1276 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1278 /* get an address component from the cursor */
1279 searchTerm = get_address_from_edit( entry, &cursor_pos );
1280 if( ! searchTerm ) return FALSE;
1281 /* printf( "search for :::%s:::\n", searchTerm ); */
1283 /* Clear any existing search */
1284 g_free( _compWindow_->searchTerm );
1285 _compWindow_->searchTerm = g_strdup( searchTerm );
1287 /* Perform search on local completion index */
1288 ncount = complete_address( searchTerm );
1290 new = get_next_complete_address();
1293 #if (!defined(USE_LDAP) && !defined(MAEMO))
1294 /* Select the address if there is only one match */
1296 /* Display selected address in entry field */
1297 gchar *addr = get_complete_address(1);
1298 if (addr && !strstr(addr, " <!--___group___-->")) {
1299 replace_address_in_edit(entry, addr, cursor_pos, FALSE, NULL);
1300 /* Discard the window */
1301 clear_completion_cache();
1305 /* Make sure that drop-down appears uniform! */
1309 addrcompl_add_queue( g_strdup( searchTerm ) );
1311 g_free( searchTerm );
1317 * Create new address completion window for specified entry.
1318 * \param entry_ Entry widget to associate with window.
1320 static void address_completion_create_completion_window( GtkEntry *entry_ )
1322 gint x, y, height, width, depth;
1323 GtkWidget *scroll, *list_view;
1326 GtkWidget *entry = GTK_WIDGET(entry_);
1328 /* Create new window and list */
1329 window = gtk_window_new(GTK_WINDOW_POPUP);
1330 list_view = addr_compl_list_view_create(_compWindow_);
1332 /* Destroy any existing window */
1333 addrcompl_destroy_window( _compWindow_ );
1335 /* Create new object */
1336 _compWindow_->window = window;
1337 _compWindow_->entry = entry;
1338 _compWindow_->list_view = list_view;
1339 _compWindow_->listCount = 0;
1340 _compWindow_->in_mouse = FALSE;
1342 scroll = gtk_scrolled_window_new(NULL, NULL);
1343 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1344 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1345 gtk_container_add(GTK_CONTAINER(window), scroll);
1346 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1347 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
1349 /* Use entry widget to create initial window */
1350 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1351 gdk_window_get_deskrelative_origin (entry->window, &x, &y);
1353 gtk_window_move(GTK_WINDOW(window), x, y);
1355 /* Resize window to fit initial (empty) address list */
1356 gtk_widget_size_request( list_view, &r );
1357 gtk_widget_set_size_request( window, width, r.height );
1358 gtk_widget_show_all( window );
1359 gtk_widget_size_request( list_view, &r );
1361 /* Setup handlers */
1362 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1363 G_CALLBACK(list_view_button_press),
1366 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1367 G_CALLBACK(list_view_button_release),
1370 g_signal_connect(G_OBJECT(window),
1371 "button-press-event",
1372 G_CALLBACK(completion_window_button_press),
1374 g_signal_connect(G_OBJECT(window),
1376 G_CALLBACK(completion_window_key_press),
1378 gdk_pointer_grab(window->window, TRUE,
1379 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1380 GDK_BUTTON_RELEASE_MASK,
1381 NULL, NULL, GDK_CURRENT_TIME);
1382 gtk_grab_add( window );
1386 * GTK1: this gets rid of the irritating focus rectangle that doesn't
1387 * follow the selection */
1388 GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
1392 * Respond to button press in completion window. Check if mouse click is
1393 * anywhere outside the completion window. In that case the completion
1394 * window is destroyed, and the original searchTerm is restored.
1396 * \param widget Window object.
1397 * \param event Event.
1398 * \param compWin Reference to completion window.
1400 static gboolean completion_window_button_press(GtkWidget *widget,
1401 GdkEventButton *event,
1402 CompletionWindow *compWin )
1404 GtkWidget *event_widget, *entry;
1407 gboolean restore = TRUE;
1409 g_return_val_if_fail(compWin != NULL, FALSE);
1411 entry = compWin->entry;
1412 g_return_val_if_fail(entry != NULL, FALSE);
1414 /* Test where mouse was clicked */
1415 event_widget = gtk_get_event_widget((GdkEvent *)event);
1416 if (event_widget != widget) {
1417 while (event_widget) {
1418 if (event_widget == widget)
1420 else if (event_widget == entry) {
1424 event_widget = event_widget->parent;
1429 /* Clicked outside of completion window - restore */
1430 searchTerm = _compWindow_->searchTerm;
1431 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1432 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1435 clear_completion_cache();
1436 addrcompl_destroy_window( _compWindow_ );
1442 * Respond to key press in completion window.
1443 * \param widget Window object.
1444 * \param event Event.
1445 * \param compWind Reference to completion window.
1447 static gboolean completion_window_key_press(GtkWidget *widget,
1449 CompletionWindow *compWin )
1451 GdkEventKey tmp_event;
1455 GtkWidget *list_view;
1457 g_return_val_if_fail(compWin != NULL, FALSE);
1459 entry = compWin->entry;
1460 list_view = compWin->list_view;
1461 g_return_val_if_fail(entry != NULL, FALSE);
1463 /* allow keyboard navigation in the alternatives tree view */
1464 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1465 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1466 completion_window_advance_selection
1467 (GTK_TREE_VIEW(list_view),
1468 event->keyval == GDK_Down ||
1469 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1473 /* make tab move to next field */
1474 if( event->keyval == GDK_Tab ) {
1475 /* Reference to parent */
1476 parent = GTK_WIDGET(entry)->parent;
1478 /* Discard the window */
1479 clear_completion_cache();
1480 addrcompl_destroy_window( _compWindow_ );
1482 /* Move focus to next widget */
1484 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1489 /* make backtab move to previous field */
1490 if( event->keyval == GDK_ISO_Left_Tab ) {
1491 /* Reference to parent */
1492 parent = GTK_WIDGET(entry)->parent;
1494 /* Discard the window */
1495 clear_completion_cache();
1496 addrcompl_destroy_window( _compWindow_ );
1498 /* Move focus to previous widget */
1500 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1504 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1506 /* look for presses that accept the selection */
1507 if (event->keyval == GDK_Return || event->keyval == GDK_space ||
1508 event->keyval == GDK_KP_Enter ||
1509 (_allowCommas_ && event->keyval == GDK_comma)) {
1510 /* User selected address with a key press */
1512 /* Display selected address in entry field */
1513 completion_window_apply_selection(
1514 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry),
1515 event->keyval != GDK_comma);
1517 if (event->keyval == GDK_comma) {
1518 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
1519 gtk_editable_insert_text(GTK_EDITABLE(entry), ", ", 2, &pos);
1520 gtk_editable_set_position(GTK_EDITABLE(entry), pos + 1);
1523 /* Discard the window */
1524 clear_completion_cache();
1525 addrcompl_destroy_window( _compWindow_ );
1529 /* key state keys should never be handled */
1530 if (event->keyval == GDK_Shift_L
1531 || event->keyval == GDK_Shift_R
1532 || event->keyval == GDK_Control_L
1533 || event->keyval == GDK_Control_R
1534 || event->keyval == GDK_Caps_Lock
1535 || event->keyval == GDK_Shift_Lock
1536 || event->keyval == GDK_Meta_L
1537 || event->keyval == GDK_Meta_R
1538 || event->keyval == GDK_Alt_L
1539 || event->keyval == GDK_Alt_R) {
1543 /* some other key, let's restore the searchTerm (orignal text) */
1544 searchTerm = _compWindow_->searchTerm;
1545 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1546 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1548 /* make sure anything we typed comes in the edit box */
1549 tmp_event.type = event->type;
1550 tmp_event.window = entry->window;
1551 tmp_event.send_event = TRUE;
1552 tmp_event.time = event->time;
1553 tmp_event.state = event->state;
1554 tmp_event.keyval = event->keyval;
1555 tmp_event.length = event->length;
1556 tmp_event.string = event->string;
1557 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1559 /* and close the completion window */
1560 clear_completion_cache();
1561 addrcompl_destroy_window( _compWindow_ );
1567 * ============================================================================
1568 * Publically accessible functions.
1569 * ============================================================================
1573 * Setup completion object.
1575 void addrcompl_initialize( void ) {
1576 /* printf( "addrcompl_initialize...\n" ); */
1577 if( ! _compWindow_ ) {
1578 _compWindow_ = addrcompl_create_window();
1581 _completionIdleID_ = 0;
1582 /* printf( "addrcompl_initialize...done\n" ); */
1586 * Teardown completion object.
1588 void addrcompl_teardown( void ) {
1589 /* printf( "addrcompl_teardown...\n" ); */
1590 addrcompl_free_window( _compWindow_ );
1591 _compWindow_ = NULL;
1592 if( _displayQueue_ ) {
1593 g_list_free( _displayQueue_ );
1595 _displayQueue_ = NULL;
1596 _completionIdleID_ = 0;
1597 /* printf( "addrcompl_teardown...done\n" ); */
1601 * tree view functions
1604 static GtkListStore *addr_compl_create_store(void)
1606 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1614 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1616 GtkTreeView *list_view;
1617 GtkTreeSelection *selector;
1618 GtkTreeModel *model;
1620 model = GTK_TREE_MODEL(addr_compl_create_store());
1621 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1622 g_object_unref(model);
1624 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1625 gtk_tree_view_set_headers_visible(list_view, FALSE);
1627 selector = gtk_tree_view_get_selection(list_view);
1628 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1629 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1632 /* create the columns */
1633 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1635 return GTK_WIDGET(list_view);
1638 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1640 GtkTreeViewColumn *column;
1641 GtkCellRenderer *renderer;
1643 renderer = gtk_cell_renderer_pixbuf_new();
1644 column = gtk_tree_view_column_new_with_attributes
1646 "pixbuf", ADDR_COMPL_ICON, NULL);
1647 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1648 renderer = gtk_cell_renderer_text_new();
1649 column = gtk_tree_view_column_new_with_attributes
1650 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1651 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1654 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1655 CompletionWindow *window)
1657 if (window && event && event->type == GDK_BUTTON_PRESS) {
1658 window->in_mouse = TRUE;
1663 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1664 CompletionWindow *window)
1666 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1667 window->in_mouse = FALSE;
1672 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1673 GtkTreeModel *model,
1675 gboolean currently_selected,
1678 CompletionWindow *window = data;
1680 if (currently_selected)
1683 if (!window->in_mouse)
1686 /* XXX: select the entry and kill window later... select is called before
1687 * any other mouse events handlers including the tree view internal one;
1688 * not using a time out would result in a crash. if this doesn't work
1689 * safely, maybe we should set variables when receiving button presses
1690 * in the tree view. */
1691 if (!window->destroying) {
1692 window->destroying = TRUE;
1693 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1699 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1701 GtkEntry *entry = GTK_ENTRY(window->entry);
1703 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1706 clear_completion_cache();
1708 addrcompl_destroy_window(window);