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>
33 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
38 #include "addrindex.h"
39 #include "addr_compl.h"
41 #include "prefs_common.h"
46 *\brief For the GtkListStore
59 * The address book is read into memory. We set up an address list
60 * containing all address book entries. Next we make the completion
61 * list, which contains all the completable strings, and store a
62 * reference to the address entry it belongs to.
63 * After calling the g_completion_complete(), we get a reference
64 * to a valid email address.
66 * Completion is very simplified. We never complete on another prefix,
67 * i.e. we neglect the next smallest possible prefix for the current
68 * completion cache. This is simply done so we might break up the
69 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
70 * something like alfons, proteus, demon, nl; and then completing on
71 * any of those words).
75 * address_entry - structure which refers to the original address entry in the
86 * completion_entry - structure used to complete addresses, with a reference
87 * the the real address information.
91 gchar *string; /* string to complete */
92 address_entry *ref; /* address the string belongs to */
95 /*******************************************************************************/
97 static gint g_ref_count; /* list ref count */
98 static GList *g_completion_list = NULL; /* list of strings to be checked */
99 static GList *g_address_list = NULL; /* address storage */
100 static GCompletion *g_completion; /* completion object */
102 static GHashTable *_groupAddresses_ = NULL;
103 static gboolean _allowCommas_ = TRUE;
105 /* To allow for continuing completion we have to keep track of the state
106 * using the following variables. No need to create a context object. */
108 static gint g_completion_count; /* nr of addresses incl. the prefix */
109 static gint g_completion_next; /* next prev address */
110 static GSList *g_completion_addresses; /* unique addresses found in the
112 static gchar *g_completion_prefix; /* last prefix. (this is cached here
113 * because the prefix passed to g_completion
114 * is g_utf8_strdown()'ed */
116 static gchar *completion_folder_path = NULL;
118 /*******************************************************************************/
121 * Define the structure of the completion window.
123 typedef struct _CompletionWindow CompletionWindow;
124 struct _CompletionWindow {
129 GtkWidget *list_view;
131 gboolean in_mouse; /*!< mouse press pending... */
132 gboolean destroying; /*!< destruction in progress */
135 static GtkListStore *addr_compl_create_store (void);
137 static GtkWidget *addr_compl_list_view_create (CompletionWindow *window);
139 static void addr_compl_create_list_view_columns (GtkWidget *list_view);
141 static gboolean list_view_button_press (GtkWidget *widget,
142 GdkEventButton *event,
143 CompletionWindow *window);
145 static gboolean list_view_button_release (GtkWidget *widget,
146 GdkEventButton *event,
147 CompletionWindow *window);
149 static gboolean addr_compl_selected (GtkTreeSelection *selector,
152 gboolean currently_selected,
155 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window);
158 * Function used by GTK to find the string data to be used for completion.
159 * \param data Pointer to data being processed.
161 static gchar *completion_func(gpointer data)
163 g_return_val_if_fail(data != NULL, NULL);
165 return ((completion_entry *)data)->string;
169 * Initialize all completion index data.
171 static void init_all(void)
173 g_completion = g_completion_new(completion_func);
174 g_return_if_fail(g_completion != NULL);
177 static void free_all_addresses(void)
182 walk = g_address_list;
183 for (; walk != NULL; walk = g_list_next(walk)) {
184 address_entry *ae = (address_entry *) walk->data;
187 g_list_free(ae->grp_emails);
190 g_list_free(g_address_list);
191 g_address_list = NULL;
192 if (_groupAddresses_)
193 g_hash_table_destroy(_groupAddresses_);
194 _groupAddresses_ = NULL;
197 static void clear_completion_cache(void);
198 static void free_completion_list(void)
201 if (!g_completion_list)
204 clear_completion_cache();
206 g_completion_clear_items(g_completion);
208 walk = g_list_first(g_completion_list);
209 for (; walk != NULL; walk = g_list_next(walk)) {
210 completion_entry *ce = (completion_entry *) walk->data;
214 g_list_free(g_completion_list);
215 g_completion_list = NULL;
218 * Free up all completion index data.
220 static void free_all(void)
222 free_completion_list();
223 free_all_addresses();
224 g_completion_free(g_completion);
229 * Append specified address entry to the index.
230 * \param str Index string value.
231 * \param ae Entry containing address data.
233 static void add_address1(const char *str, address_entry *ae)
235 completion_entry *ce1;
236 ce1 = g_new0(completion_entry, 1),
237 /* GCompletion list is case sensitive */
238 ce1->string = g_utf8_strdown(str, -1);
241 g_completion_list = g_list_prepend(g_completion_list, ce1);
245 * Adds address to the completion list. This function looks complicated, but
246 * it's only allocation checks. Each value will be included in the index.
247 * \param name Recipient name.
248 * \param address EMail address.
249 * \param alias Alias to append.
250 * \param grp_emails the emails in case of a group. List should be freed later,
251 * but not its strings
252 * \return <code>0</code> if entry appended successfully, or <code>-1</code>
255 static gint add_address(const gchar *name, const gchar *address,
256 const gchar *nick, const gchar *alias, GList *grp_emails)
259 gboolean is_group = FALSE;
261 if (!name || !address) {
262 if (!address && !nick && !alias && grp_emails) {
268 ae = g_new0(address_entry, 1);
270 g_return_val_if_fail(ae != NULL, -1);
272 ae->name = g_strdup(name);
273 ae->address = g_strdup(address);
274 ae->grp_emails = grp_emails;
275 g_address_list = g_list_prepend(g_address_list, ae);
277 add_address1(name, ae);
278 if (address != NULL && *address != '\0')
279 add_address1(address, ae);
281 if (nick != NULL && *nick != '\0')
282 add_address1(nick, ae);
284 if ( alias != NULL && *alias != '\0') {
285 add_address1(alias, ae);
292 * Read address book, creating all entries in the completion index.
294 static void read_address_book(gchar *folderpath) {
295 free_all_addresses();
296 free_completion_list();
298 addrindex_load_completion( add_address, folderpath );
299 g_address_list = g_list_reverse(g_address_list);
300 g_completion_list = g_list_reverse(g_completion_list);
301 /* merge the completion entry list into g_completion */
302 if (g_completion_list) {
303 g_completion_add_items(g_completion, g_completion_list);
304 if (debug_get_mode())
305 debug_print("read %d items in %s\n",
306 g_list_length(g_completion_list),
307 folderpath?folderpath:"(null)");
312 * Test whether there is a completion pending.
313 * \return <code>TRUE</code> if pending.
315 static gboolean is_completion_pending(void)
317 /* check if completion pending, i.e. we might satisfy a request for the next
318 * or previous address */
319 return g_completion_count;
323 * Clear the completion cache.
325 static void clear_completion_cache(void)
327 if (is_completion_pending()) {
328 g_free(g_completion_prefix);
330 if (g_completion_addresses) {
331 g_slist_free(g_completion_addresses);
332 g_completion_addresses = NULL;
335 g_completion_count = g_completion_next = 0;
340 * Prepare completion index. This function should be called prior to attempting
341 * address completion.
342 * \return The number of addresses in the completion list.
344 gint start_address_completion(gchar *folderpath)
346 gboolean different_book = FALSE;
347 clear_completion_cache();
349 if (strcmp2(completion_folder_path,folderpath))
350 different_book = TRUE;
352 g_free(completion_folder_path);
353 if (folderpath != NULL)
354 completion_folder_path = g_strdup(folderpath);
356 completion_folder_path = NULL;
360 /* open the address book */
361 read_address_book(folderpath);
362 } else if (different_book)
363 read_address_book(folderpath);
366 debug_print("start_address_completion(%s) ref count %d\n",
367 folderpath?folderpath:"(null)", g_ref_count);
369 return g_list_length(g_completion_list);
373 * Retrieve a possible address (or a part) from an entry box. To make life
374 * easier, we only look at the last valid address component; address
375 * completion only works at the last string component in the entry box.
377 * \param entry Address entry field.
378 * \param start_pos Address of start position of address.
379 * \return Possible address.
381 static gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
383 const gchar *edit_text, *p;
385 gboolean in_quote = FALSE;
386 gboolean in_bracket = FALSE;
389 edit_text = gtk_entry_get_text(entry);
390 if (edit_text == NULL) return NULL;
392 cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
394 /* scan for a separator. doesn't matter if walk points at null byte. */
395 for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
397 p = g_utf8_prev_char(p)) {
400 } else if (!in_quote) {
401 if (!in_bracket && *p == ',') {
403 } else if (*p == '<')
410 /* have something valid */
411 if (g_utf8_strlen(p, -1) == 0)
414 #define IS_VALID_CHAR(x) \
415 (g_ascii_isalnum(x) || (x) == '"' || (x) == '<' || (((unsigned char)(x)) > 0x7f))
417 /* now scan back until we hit a valid character */
418 for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
423 if (g_utf8_strlen(p, -1) == 0)
426 if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
433 static gchar *get_complete_address_from_name_email(const gchar *name, const gchar *email)
435 gchar *address = NULL;
436 if (!name || name[0] == '\0')
437 address = g_strdup_printf("<%s>", email);
438 else if (strchr_with_skip_quote(name, '"', ','))
439 address = g_strdup_printf
440 ("\"%s\" <%s>", name, email);
442 address = g_strdup_printf
443 ("%s <%s>", name, email);
448 * Replace an incompleted address with a completed one.
449 * \param entry Address entry field.
450 * \param newtext New text.
451 * \param start_pos Insertion point in entry field.
453 static void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
454 gint start_pos, gboolean is_group, GList *grp_emails)
456 if (!newtext) return;
457 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
459 gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
462 gchar *addresses = NULL;
463 GList *cur = grp_emails;
464 for (; cur; cur = cur->next) {
466 ItemEMail *email = (ItemEMail *)cur->data;
467 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(email);
469 gchar *addr = get_complete_address_from_name_email(
470 ADDRITEM_NAME(person), email->address);
472 tmp = g_strdup_printf("%s, %s", addresses, addr);
474 tmp = g_strdup_printf("%s", addr);
479 gtk_editable_insert_text(GTK_EDITABLE(entry), addresses, strlen(addresses),
483 gtk_editable_set_position(GTK_EDITABLE(entry), -1);
487 * Attempt to complete an address, and returns the number of addresses found.
488 * Use <code>get_complete_address()</code> to get an entry from the index.
490 * \param str Search string to find.
491 * \return Zero if no match was found, otherwise the number of addresses; the
492 * original prefix (search string) will appear at index 0.
494 guint complete_address(const gchar *str)
496 GList *result = NULL;
500 completion_entry *ce = NULL;
502 g_return_val_if_fail(str != NULL, 0);
504 /* g_completion is case sensitive */
505 d = g_utf8_strdown(str, -1);
507 clear_completion_cache();
508 g_completion_prefix = g_strdup(str);
510 result = g_completion_complete(g_completion, d, NULL);
512 count = g_list_length(result);
514 /* create list with unique addresses */
515 for (cpl = 0, result = g_list_first(result);
517 result = g_list_next(result)) {
518 ce = (completion_entry *)(result->data);
519 if (NULL == g_slist_find(g_completion_addresses,
522 g_completion_addresses =
523 g_slist_append(g_completion_addresses,
527 count = cpl + 1; /* index 0 is the original prefix */
528 g_completion_next = 1; /* we start at the first completed one */
530 g_free(g_completion_prefix);
531 g_completion_prefix = NULL;
534 g_completion_count = count;
542 * complete_matches_found() returns the number of matched addresses according
543 * to the completion mechanism. Unlike complete_address(), the returned value
544 * doesn't count str itself. If there's no match, it returns 0.
545 * To get a list of completion matches, see complete_address() instead.
547 guint complete_matches_found(const gchar *str)
549 GList *result = NULL;
552 g_return_val_if_fail(str != NULL, 0);
554 /* g_completion is case sensitive */
555 d = g_utf8_strdown(str, -1);
557 clear_completion_cache();
558 g_completion_prefix = g_strdup(str);
560 result = g_completion_complete(g_completion, d, NULL);
562 g_free(g_completion_prefix);
565 return g_list_length(result);
569 * Return a complete address from the index.
570 * \param index Index of entry that was found (by the previous call to
571 * <code>complete_address()</code>
572 * \return Completed address string; this should be freed when done.
574 gchar *get_complete_address(gint index)
576 const address_entry *p;
577 gchar *address = NULL;
579 if (index < g_completion_count) {
581 address = g_strdup(g_completion_prefix);
583 /* get something from the unique addresses */
584 p = (address_entry *)g_slist_nth_data
585 (g_completion_addresses, index - 1);
586 if (p != NULL && p->address != NULL) {
587 address = get_complete_address_from_name_email(p->name, p->address);
588 } else if (p != NULL && p->address == NULL && p->name != NULL) {
590 address = g_strdup_printf("%s (%s) <!--___group___-->", p->name, _("Group"));
591 if (!_groupAddresses_) {
592 _groupAddresses_ = g_hash_table_new(NULL, g_direct_equal);
594 if (!g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)))) {
595 g_hash_table_insert(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)), p->grp_emails);
606 * Return the next complete address match from the completion index.
607 * \return Completed address string; this should be freed when done.
609 static gchar *get_next_complete_address(void)
611 if (is_completion_pending()) {
614 res = get_complete_address(g_completion_next);
615 g_completion_next += 1;
616 if (g_completion_next >= g_completion_count)
617 g_completion_next = 0;
625 * Return a count of the completed matches in the completion index.
626 * \return Number of matched entries.
628 static guint get_completion_count(void)
630 if (is_completion_pending())
631 return g_completion_count;
637 * Invalidate address completion index. This function should be called whenever
638 * the address book changes. This forces data to be read into the completion
640 * \return Number of entries in index.
642 gint invalidate_address_completion(void)
645 /* simply the same as start_address_completion() */
646 debug_print("Invalidation request for address completion\n");
647 read_address_book(completion_folder_path);
648 clear_completion_cache();
651 return g_list_length(g_completion_list);
655 * Finished with completion index. This function should be called after
656 * matching addresses.
657 * \return Reference count.
659 gint end_address_completion(void)
661 gboolean different_folder = FALSE;
662 clear_completion_cache();
664 /* reset the folderpath to NULL */
665 if (completion_folder_path) {
666 g_free(completion_folder_path);
667 completion_folder_path = NULL;
668 different_folder = TRUE;
670 if (0 == --g_ref_count)
673 debug_print("end_address_completion ref count %d\n", g_ref_count);
674 if (g_ref_count && different_folder) {
675 debug_print("still ref'd, different folder\n");
676 invalidate_address_completion();
685 static CompletionWindow *_compWindow_ = NULL;
688 * Mutex to protect callback from multiple threads.
690 static pthread_mutex_t _completionMutex_ = PTHREAD_MUTEX_INITIALIZER;
693 * Completion queue list.
695 static GList *_displayQueue_ = NULL;
699 static gint _queryID_ = 0;
702 * Completion idle ID.
704 static guint _completionIdleID_ = 0;
707 * address completion entry ui. the ui (completion list was inspired by galeon's
708 * auto completion list). remaining things powered by claws's completion engine.
711 #define ENTRY_DATA_TAB_HOOK "tab_hook" /* used to lookup entry */
712 #define ENTRY_DATA_ALLOW_COMMAS "allowcommas" /* used to know whether to present groups */
714 static void address_completion_mainwindow_set_focus (GtkWindow *window,
717 static gboolean address_completion_entry_key_pressed (GtkEntry *entry,
720 static gboolean address_completion_complete_address_in_entry
723 static void address_completion_create_completion_window (GtkEntry *entry);
725 static gboolean completion_window_button_press
727 GdkEventButton *event,
728 CompletionWindow *compWin );
730 static gboolean completion_window_key_press
733 CompletionWindow *compWin );
734 static void address_completion_create_completion_window( GtkEntry *entry_ );
737 * Create a completion window object.
738 * \return Initialized completion window.
740 static CompletionWindow *addrcompl_create_window( void ) {
741 CompletionWindow *cw;
743 cw = g_new0( CompletionWindow, 1 );
745 cw->searchTerm = NULL;
748 cw->list_view = NULL;
749 cw->in_mouse = FALSE;
750 cw->destroying = FALSE;
756 * Destroy completion window.
757 * \param cw Window to destroy.
759 static void addrcompl_destroy_window( CompletionWindow *cw ) {
760 /* Stop all searches currently in progress */
761 addrindex_stop_search( _queryID_ );
763 /* Remove idler function... or application may not terminate */
764 if( _completionIdleID_ != 0 ) {
765 g_source_remove( _completionIdleID_ );
766 _completionIdleID_ = 0;
769 /* Now destroy window */
771 /* Clear references to widgets */
773 cw->list_view = NULL;
777 gtk_widget_hide( cw->window );
778 gtk_widget_destroy( cw->window );
781 cw->destroying = FALSE;
782 cw->in_mouse = FALSE;
788 * Free up completion window.
789 * \param cw Window to free.
791 static void addrcompl_free_window( CompletionWindow *cw ) {
793 addrcompl_destroy_window( cw );
795 g_free( cw->searchTerm );
796 cw->searchTerm = NULL;
798 /* Clear references */
807 * Advance selection to previous/next item in list.
808 * \param list_view List to process.
809 * \param forward Set to <i>TRUE</i> to select next or <i>FALSE</i> for
812 static void completion_window_advance_selection(GtkTreeView *list_view, gboolean forward)
814 GtkTreeSelection *selection;
818 g_return_if_fail(list_view != NULL);
820 selection = gtk_tree_view_get_selection(list_view);
821 if (!gtk_tree_selection_get_selected(selection, &model, &iter))
825 forward = gtk_tree_model_iter_next(model, &iter);
827 gtk_tree_selection_select_iter(selection, &iter);
831 prev = gtk_tree_model_get_path(model, &iter);
835 if (gtk_tree_path_prev(prev))
836 gtk_tree_selection_select_path(selection, prev);
838 gtk_tree_path_free(prev);
843 * Resize window to accommodate maximum number of address entries.
844 * \param cw Completion window.
846 static void addrcompl_resize_window( CompletionWindow *cw ) {
848 gint x, y, width, height, depth;
850 /* Get current geometry of window */
851 gdk_window_get_geometry( cw->window->window, &x, &y, &width, &height, &depth );
853 gtk_widget_hide_all( cw->window );
854 gtk_widget_show_all( cw->window );
855 gtk_widget_size_request( cw->list_view, &r );
857 /* Adjust window height to available screen space */
858 if( ( y + r.height ) > gdk_screen_height() ) {
859 gtk_window_set_resizable(GTK_WINDOW(cw->window), FALSE);
860 gtk_widget_set_size_request( cw->window, width, gdk_screen_height() - y );
862 gtk_widget_set_size_request(cw->window, width, r.height);
865 static GdkPixbuf *group_pixbuf = NULL;
866 static GdkPixbuf *email_pixbuf = NULL;
869 * Add an address the completion window address list.
870 * \param cw Completion window.
871 * \param address Address to add.
873 static void addrcompl_add_entry( CompletionWindow *cw, gchar *address ) {
876 GtkTreeSelection *selection;
877 gboolean is_group = FALSE;
878 GList *grp_emails = NULL;
879 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(cw->list_view)));
883 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_TWO, &group_pixbuf);
884 g_object_ref(G_OBJECT(group_pixbuf));
887 stock_pixbuf_gdk(cw->list_view, STOCK_PIXMAP_ADDR_ONE, &email_pixbuf);
888 g_object_ref(G_OBJECT(email_pixbuf));
890 /* g_print( "\t\tAdding :%s\n", address ); */
891 if (strstr(address, " <!--___group___-->")) {
893 if (_groupAddresses_)
894 grp_emails = g_hash_table_lookup(_groupAddresses_, GINT_TO_POINTER(g_str_hash(address)));
895 *(strstr(address, " <!--___group___-->")) = '\0';
896 pixbuf = group_pixbuf;
897 } else if (strchr(address, '@') && strchr(address, '<') &&
898 strchr(address, '>')) {
899 pixbuf = email_pixbuf;
903 if (is_group && !_allowCommas_)
905 gtk_list_store_append(store, &iter);
906 gtk_list_store_set(store, &iter,
907 ADDR_COMPL_ICON, pixbuf,
908 ADDR_COMPL_ADDRESS, address,
909 ADDR_COMPL_ISGROUP, is_group,
910 ADDR_COMPL_GROUPLIST, grp_emails,
915 addrcompl_resize_window( cw );
916 gtk_grab_add( cw->window );
918 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cw->list_view));
919 gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
921 if( cw->listCount == 1 ) {
922 /* Select first row for now */
923 gtk_tree_selection_select_iter(selection, &iter);
926 else if( cw->listCount == 2 ) {
927 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
928 /* Move off first row */
929 gtk_tree_selection_select_iter(selection, &iter);
935 * Completion idle function. This function is called by the main (UI) thread
936 * during UI idle time while an address search is in progress. Items from the
937 * display queue are processed and appended to the address list.
939 * \param data Target completion window to receive email addresses.
940 * \return <i>TRUE</i> to ensure that idle event do not get ignored.
942 static gboolean addrcompl_idle( gpointer data ) {
946 /* Process all entries in display queue */
947 pthread_mutex_lock( & _completionMutex_ );
948 if( _displayQueue_ ) {
949 node = _displayQueue_;
951 address = node->data;
952 /* g_print( "address ::: %s :::\n", address ); */
953 addrcompl_add_entry( _compWindow_, address );
955 node = g_list_next( node );
957 g_list_free( _displayQueue_ );
958 _displayQueue_ = NULL;
960 pthread_mutex_unlock( & _completionMutex_ );
967 * Callback entry point. The background thread (if any) appends the address
968 * list to the display queue.
969 * \param sender Sender of query.
970 * \param queryID Query ID of search request.
971 * \param listEMail List of zero of more email objects that met search
973 * \param data Query data.
975 static gint addrcompl_callback_entry(
976 gpointer sender, gint queryID, GList *listEMail, gpointer data )
981 /* g_print( "addrcompl_callback_entry::queryID=%d\n", queryID ); */
982 pthread_mutex_lock( & _completionMutex_ );
983 if( queryID == _queryID_ ) {
984 /* Append contents to end of display queue */
987 ItemEMail *email = node->data;
989 address = addritem_format_email( email );
990 /* g_print( "\temail/address ::%s::\n", address ); */
991 _displayQueue_ = g_list_append( _displayQueue_, address );
992 node = g_list_next( node );
995 g_list_free( listEMail );
996 pthread_mutex_unlock( & _completionMutex_ );
1002 * Clear the display queue.
1004 static void addrcompl_clear_queue( void ) {
1005 /* Clear out display queue */
1006 pthread_mutex_lock( & _completionMutex_ );
1008 g_list_free( _displayQueue_ );
1009 _displayQueue_ = NULL;
1011 pthread_mutex_unlock( & _completionMutex_ );
1015 * Add a single address entry into the display queue.
1016 * \param address Address to append.
1018 static void addrcompl_add_queue( gchar *address ) {
1019 pthread_mutex_lock( & _completionMutex_ );
1020 _displayQueue_ = g_list_append( _displayQueue_, address );
1021 pthread_mutex_unlock( & _completionMutex_ );
1025 * Load list with entries from local completion index.
1027 static void addrcompl_load_local( void ) {
1030 for (count = 0; count < get_completion_count(); count++) {
1033 address = get_complete_address( count );
1034 /* g_print( "\taddress ::%s::\n", address ); */
1036 /* Append contents to end of display queue */
1037 addrcompl_add_queue( address );
1044 static void addrcompl_start_search( void ) {
1047 searchTerm = g_strdup( _compWindow_->searchTerm );
1049 /* Setup the search */
1050 _queryID_ = addrindex_setup_search(
1051 searchTerm, NULL, addrcompl_callback_entry );
1052 g_free( searchTerm );
1053 /* g_print( "addrcompl_start_search::queryID=%d\n", _queryID_ ); */
1055 /* Load local stuff */
1056 addrcompl_load_local();
1058 /* Sit back and wait until something happens */
1059 _completionIdleID_ =
1060 g_idle_add( ( GtkFunction ) addrcompl_idle, NULL );
1061 /* g_print( "addrindex_start_search::queryID=%d\n", _queryID_ ); */
1063 addrindex_start_search( _queryID_ );
1067 * Apply the current selection in the list to the entry field. Focus is also
1068 * moved to the next widget so that Tab key works correctly.
1069 * \param list_view List to process.
1070 * \param entry Address entry field.
1071 * \param move_focus Move focus to the next widget ?
1073 static void completion_window_apply_selection(GtkTreeView *list_view,
1075 gboolean move_focus)
1077 gchar *address = NULL, *text = NULL;
1080 GtkTreeSelection *selection;
1081 GtkTreeModel *model;
1083 gboolean is_group = FALSE;
1084 g_return_if_fail(list_view != NULL);
1085 g_return_if_fail(entry != NULL);
1086 GList *grp_emails = NULL;
1088 selection = gtk_tree_view_get_selection(list_view);
1089 if (! gtk_tree_selection_get_selected(selection, &model, &iter))
1092 /* First remove the idler */
1093 if( _completionIdleID_ != 0 ) {
1094 g_source_remove( _completionIdleID_ );
1095 _completionIdleID_ = 0;
1098 /* Process selected item */
1099 gtk_tree_model_get(model, &iter, ADDR_COMPL_ADDRESS, &text,
1100 ADDR_COMPL_ISGROUP, &is_group,
1101 ADDR_COMPL_GROUPLIST, &grp_emails,
1104 address = get_address_from_edit(entry, &cursor_pos);
1106 replace_address_in_edit(entry, text, cursor_pos, is_group, grp_emails);
1109 /* Move focus to next widget */
1110 parent = GTK_WIDGET(entry)->parent;
1111 if( parent && move_focus) {
1112 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1117 * Start address completion. Should be called when creating the main window
1118 * containing address completion entries.
1119 * \param mainwindow Main window.
1121 void address_completion_start(GtkWidget *mainwindow)
1123 start_address_completion(NULL);
1125 /* register focus change hook */
1126 g_signal_connect(G_OBJECT(mainwindow), "set_focus",
1127 G_CALLBACK(address_completion_mainwindow_set_focus),
1132 * Need unique data to make unregistering signal handler possible for the auto
1135 #define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
1138 * Register specified entry widget for address completion.
1139 * \param entry Address entry field.
1141 void address_completion_register_entry(GtkEntry *entry, gboolean allow_commas)
1143 g_return_if_fail(entry != NULL);
1144 g_return_if_fail(GTK_IS_ENTRY(entry));
1146 /* add hooked property */
1147 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
1148 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS, GINT_TO_POINTER(allow_commas));
1150 /* add keypress event */
1151 g_signal_connect_closure
1152 (G_OBJECT(entry), "key_press_event",
1153 g_cclosure_new(G_CALLBACK(address_completion_entry_key_pressed),
1154 COMPLETION_UNIQUE_DATA,
1160 * Unregister specified entry widget from address completion operations.
1161 * \param entry Address entry field.
1163 void address_completion_unregister_entry(GtkEntry *entry)
1165 GtkObject *entry_obj;
1167 g_return_if_fail(entry != NULL);
1168 g_return_if_fail(GTK_IS_ENTRY(entry));
1170 entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
1171 g_return_if_fail(entry_obj);
1172 g_return_if_fail(G_OBJECT(entry_obj) == G_OBJECT(entry));
1174 /* has the hooked property? */
1175 g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
1177 /* remove the hook */
1178 g_signal_handlers_disconnect_by_func(G_OBJECT(entry),
1179 G_CALLBACK(address_completion_entry_key_pressed),
1180 COMPLETION_UNIQUE_DATA);
1184 * End address completion. Should be called when main window with address
1185 * completion entries terminates. NOTE: this function assumes that it is
1186 * called upon destruction of the window.
1187 * \param mainwindow Main window.
1189 void address_completion_end(GtkWidget *mainwindow)
1191 /* if address_completion_end() is really called on closing the window,
1192 * we don't need to unregister the set_focus_cb */
1193 end_address_completion();
1196 /* if focus changes to another entry, then clear completion cache */
1197 static void address_completion_mainwindow_set_focus(GtkWindow *window,
1202 if (widget && GTK_IS_ENTRY(widget) &&
1203 g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) {
1204 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), ENTRY_DATA_ALLOW_COMMAS));
1205 clear_completion_cache();
1210 * Listener that watches for tab or other keystroke in address entry field.
1211 * \param entry Address entry field.
1212 * \param ev Event object.
1213 * \param data User data.
1214 * \return <i>TRUE</i>.
1216 static gboolean address_completion_entry_key_pressed(GtkEntry *entry,
1220 if (ev->keyval == GDK_Tab) {
1221 addrcompl_clear_queue();
1222 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1223 if( address_completion_complete_address_in_entry( entry, TRUE ) ) {
1224 /* route a void character to the default handler */
1225 /* this is a dirty hack; we're actually changing a key
1226 * reported by the system. */
1227 ev->keyval = GDK_AudibleBell_Enable;
1228 ev->state &= ~GDK_SHIFT_MASK;
1231 address_completion_create_completion_window(entry);
1233 /* Start remote queries */
1234 addrcompl_start_search();
1241 } else if (ev->keyval == GDK_Shift_L
1242 || ev->keyval == GDK_Shift_R
1243 || ev->keyval == GDK_Control_L
1244 || ev->keyval == GDK_Control_R
1245 || ev->keyval == GDK_Caps_Lock
1246 || ev->keyval == GDK_Shift_Lock
1247 || ev->keyval == GDK_Meta_L
1248 || ev->keyval == GDK_Meta_R
1249 || ev->keyval == GDK_Alt_L
1250 || ev->keyval == GDK_Alt_R) {
1251 /* these buttons should not clear the cache... */
1253 clear_completion_cache();
1258 * Initialize search term for address completion.
1259 * \param entry Address entry field.
1261 static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
1264 gint ncount, cursor_pos;
1265 gchar *searchTerm, *new = NULL;
1267 g_return_val_if_fail(entry != NULL, FALSE);
1269 if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
1271 /* get an address component from the cursor */
1272 searchTerm = get_address_from_edit( entry, &cursor_pos );
1273 if( ! searchTerm ) return FALSE;
1274 /* g_print( "search for :::%s:::\n", searchTerm ); */
1276 /* Clear any existing search */
1277 g_free( _compWindow_->searchTerm );
1278 _compWindow_->searchTerm = g_strdup( searchTerm );
1280 /* Perform search on local completion index */
1281 ncount = complete_address( searchTerm );
1283 new = get_next_complete_address();
1286 #if (!defined(USE_LDAP) && !defined(GENERIC_UMPC))
1287 /* Select the address if there is only one match */
1289 /* Display selected address in entry field */
1290 gchar *addr = get_complete_address(1);
1291 if (addr && !strstr(addr, " <!--___group___-->")) {
1292 replace_address_in_edit(entry, addr, cursor_pos, FALSE, NULL);
1293 /* Discard the window */
1294 clear_completion_cache();
1298 /* Make sure that drop-down appears uniform! */
1302 addrcompl_add_queue( g_strdup( searchTerm ) );
1304 g_free( searchTerm );
1310 * Create new address completion window for specified entry.
1311 * \param entry_ Entry widget to associate with window.
1313 static void address_completion_create_completion_window( GtkEntry *entry_ )
1315 gint x, y, height, width, depth;
1316 GtkWidget *scroll, *list_view;
1319 GtkWidget *entry = GTK_WIDGET(entry_);
1321 /* Create new window and list */
1322 window = gtk_window_new(GTK_WINDOW_POPUP);
1323 list_view = addr_compl_list_view_create(_compWindow_);
1325 /* Destroy any existing window */
1326 addrcompl_destroy_window( _compWindow_ );
1328 /* Create new object */
1329 _compWindow_->window = window;
1330 _compWindow_->entry = entry;
1331 _compWindow_->list_view = list_view;
1332 _compWindow_->listCount = 0;
1333 _compWindow_->in_mouse = FALSE;
1335 scroll = gtk_scrolled_window_new(NULL, NULL);
1336 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
1337 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1338 gtk_container_add(GTK_CONTAINER(window), scroll);
1339 gtk_container_add(GTK_CONTAINER(scroll), list_view);
1340 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
1342 /* Use entry widget to create initial window */
1343 gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
1344 gdk_window_get_origin (entry->window, &x, &y);
1346 gtk_window_move(GTK_WINDOW(window), x, y);
1348 /* Resize window to fit initial (empty) address list */
1349 gtk_widget_size_request( list_view, &r );
1350 gtk_widget_set_size_request( window, width, r.height );
1351 gtk_widget_show_all( window );
1352 gtk_widget_size_request( list_view, &r );
1354 /* Setup handlers */
1355 g_signal_connect(G_OBJECT(list_view), "button_press_event",
1356 G_CALLBACK(list_view_button_press),
1359 g_signal_connect(G_OBJECT(list_view), "button_release_event",
1360 G_CALLBACK(list_view_button_release),
1363 g_signal_connect(G_OBJECT(window),
1364 "button-press-event",
1365 G_CALLBACK(completion_window_button_press),
1367 g_signal_connect(G_OBJECT(window),
1369 G_CALLBACK(completion_window_key_press),
1371 gdk_pointer_grab(window->window, TRUE,
1372 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
1373 GDK_BUTTON_RELEASE_MASK,
1374 NULL, NULL, GDK_CURRENT_TIME);
1375 gtk_grab_add( window );
1379 * GTK1: this gets rid of the irritating focus rectangle that doesn't
1380 * follow the selection */
1381 GTK_WIDGET_UNSET_FLAGS(list_view, GTK_CAN_FOCUS);
1385 * Respond to button press in completion window. Check if mouse click is
1386 * anywhere outside the completion window. In that case the completion
1387 * window is destroyed, and the original searchTerm is restored.
1389 * \param widget Window object.
1390 * \param event Event.
1391 * \param compWin Reference to completion window.
1393 static gboolean completion_window_button_press(GtkWidget *widget,
1394 GdkEventButton *event,
1395 CompletionWindow *compWin )
1397 GtkWidget *event_widget, *entry;
1400 gboolean restore = TRUE;
1402 g_return_val_if_fail(compWin != NULL, FALSE);
1404 entry = compWin->entry;
1405 g_return_val_if_fail(entry != NULL, FALSE);
1407 /* Test where mouse was clicked */
1408 event_widget = gtk_get_event_widget((GdkEvent *)event);
1409 if (event_widget != widget) {
1410 while (event_widget) {
1411 if (event_widget == widget)
1413 else if (event_widget == entry) {
1417 event_widget = event_widget->parent;
1422 /* Clicked outside of completion window - restore */
1423 searchTerm = _compWindow_->searchTerm;
1424 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1425 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1428 clear_completion_cache();
1429 addrcompl_destroy_window( _compWindow_ );
1435 * Respond to key press in completion window.
1436 * \param widget Window object.
1437 * \param event Event.
1438 * \param compWind Reference to completion window.
1440 static gboolean completion_window_key_press(GtkWidget *widget,
1442 CompletionWindow *compWin )
1444 GdkEventKey tmp_event;
1448 GtkWidget *list_view;
1450 g_return_val_if_fail(compWin != NULL, FALSE);
1452 entry = compWin->entry;
1453 list_view = compWin->list_view;
1454 g_return_val_if_fail(entry != NULL, FALSE);
1456 /* allow keyboard navigation in the alternatives tree view */
1457 if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
1458 event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
1459 completion_window_advance_selection
1460 (GTK_TREE_VIEW(list_view),
1461 event->keyval == GDK_Down ||
1462 event->keyval == GDK_Page_Down ? TRUE : FALSE);
1466 /* make tab move to next field */
1467 if( event->keyval == GDK_Tab ) {
1468 /* Reference to parent */
1469 parent = GTK_WIDGET(entry)->parent;
1471 /* Discard the window */
1472 clear_completion_cache();
1473 addrcompl_destroy_window( _compWindow_ );
1475 /* Move focus to next widget */
1477 gtk_widget_child_focus( parent, GTK_DIR_TAB_FORWARD );
1482 /* make backtab move to previous field */
1483 if( event->keyval == GDK_ISO_Left_Tab ) {
1484 /* Reference to parent */
1485 parent = GTK_WIDGET(entry)->parent;
1487 /* Discard the window */
1488 clear_completion_cache();
1489 addrcompl_destroy_window( _compWindow_ );
1491 /* Move focus to previous widget */
1493 gtk_widget_child_focus( parent, GTK_DIR_TAB_BACKWARD );
1497 _allowCommas_ = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(entry), ENTRY_DATA_ALLOW_COMMAS));
1499 /* look for presses that accept the selection */
1500 if (event->keyval == GDK_Return || event->keyval == GDK_space ||
1501 event->keyval == GDK_KP_Enter ||
1502 (_allowCommas_ && event->keyval == GDK_comma)) {
1503 /* User selected address with a key press */
1505 /* Display selected address in entry field */
1506 completion_window_apply_selection(
1507 GTK_TREE_VIEW(list_view), GTK_ENTRY(entry),
1508 event->keyval != GDK_comma);
1510 if (event->keyval == GDK_comma) {
1511 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
1512 gtk_editable_insert_text(GTK_EDITABLE(entry), ", ", 2, &pos);
1513 gtk_editable_set_position(GTK_EDITABLE(entry), pos + 1);
1516 /* Discard the window */
1517 clear_completion_cache();
1518 addrcompl_destroy_window( _compWindow_ );
1522 /* key state keys should never be handled */
1523 if (event->keyval == GDK_Shift_L
1524 || event->keyval == GDK_Shift_R
1525 || event->keyval == GDK_Control_L
1526 || event->keyval == GDK_Control_R
1527 || event->keyval == GDK_Caps_Lock
1528 || event->keyval == GDK_Shift_Lock
1529 || event->keyval == GDK_Meta_L
1530 || event->keyval == GDK_Meta_R
1531 || event->keyval == GDK_Alt_L
1532 || event->keyval == GDK_Alt_R) {
1536 /* some other key, let's restore the searchTerm (orignal text) */
1537 searchTerm = _compWindow_->searchTerm;
1538 g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
1539 replace_address_in_edit(GTK_ENTRY(entry), searchTerm, cursor_pos, FALSE, NULL);
1541 /* make sure anything we typed comes in the edit box */
1542 tmp_event.type = event->type;
1543 tmp_event.window = entry->window;
1544 tmp_event.send_event = TRUE;
1545 tmp_event.time = event->time;
1546 tmp_event.state = event->state;
1547 tmp_event.keyval = event->keyval;
1548 tmp_event.length = event->length;
1549 tmp_event.string = event->string;
1550 gtk_widget_event(entry, (GdkEvent *)&tmp_event);
1552 /* and close the completion window */
1553 clear_completion_cache();
1554 addrcompl_destroy_window( _compWindow_ );
1560 * ============================================================================
1561 * Publically accessible functions.
1562 * ============================================================================
1566 * Setup completion object.
1568 void addrcompl_initialize( void ) {
1569 /* g_print( "addrcompl_initialize...\n" ); */
1570 if( ! _compWindow_ ) {
1571 _compWindow_ = addrcompl_create_window();
1574 _completionIdleID_ = 0;
1575 /* g_print( "addrcompl_initialize...done\n" ); */
1579 * Teardown completion object.
1581 void addrcompl_teardown( void ) {
1582 /* g_print( "addrcompl_teardown...\n" ); */
1583 addrcompl_free_window( _compWindow_ );
1584 _compWindow_ = NULL;
1585 if( _displayQueue_ ) {
1586 g_list_free( _displayQueue_ );
1588 _displayQueue_ = NULL;
1589 _completionIdleID_ = 0;
1590 /* g_print( "addrcompl_teardown...done\n" ); */
1594 * tree view functions
1597 static GtkListStore *addr_compl_create_store(void)
1599 return gtk_list_store_new(N_ADDR_COMPL_COLUMNS,
1607 static GtkWidget *addr_compl_list_view_create(CompletionWindow *window)
1609 GtkTreeView *list_view;
1610 GtkTreeSelection *selector;
1611 GtkTreeModel *model;
1613 model = GTK_TREE_MODEL(addr_compl_create_store());
1614 list_view = GTK_TREE_VIEW(gtk_tree_view_new_with_model(model));
1615 g_object_unref(model);
1617 gtk_tree_view_set_rules_hint(list_view, prefs_common.use_stripes_everywhere);
1618 gtk_tree_view_set_headers_visible(list_view, FALSE);
1620 selector = gtk_tree_view_get_selection(list_view);
1621 gtk_tree_selection_set_mode(selector, GTK_SELECTION_BROWSE);
1622 gtk_tree_selection_set_select_function(selector, addr_compl_selected,
1625 /* create the columns */
1626 addr_compl_create_list_view_columns(GTK_WIDGET(list_view));
1628 return GTK_WIDGET(list_view);
1631 static void addr_compl_create_list_view_columns(GtkWidget *list_view)
1633 GtkTreeViewColumn *column;
1634 GtkCellRenderer *renderer;
1636 renderer = gtk_cell_renderer_pixbuf_new();
1637 column = gtk_tree_view_column_new_with_attributes
1639 "pixbuf", ADDR_COMPL_ICON, NULL);
1640 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1641 renderer = gtk_cell_renderer_text_new();
1642 column = gtk_tree_view_column_new_with_attributes
1643 ("", renderer, "text", ADDR_COMPL_ADDRESS, NULL);
1644 gtk_tree_view_append_column(GTK_TREE_VIEW(list_view), column);
1647 static gboolean list_view_button_press(GtkWidget *widget, GdkEventButton *event,
1648 CompletionWindow *window)
1650 if (window && event && event->type == GDK_BUTTON_PRESS) {
1651 window->in_mouse = TRUE;
1656 static gboolean list_view_button_release(GtkWidget *widget, GdkEventButton *event,
1657 CompletionWindow *window)
1659 if (window && event && event->type == GDK_BUTTON_RELEASE) {
1660 window->in_mouse = FALSE;
1665 static gboolean addr_compl_selected(GtkTreeSelection *selector,
1666 GtkTreeModel *model,
1668 gboolean currently_selected,
1671 CompletionWindow *window = data;
1673 if (currently_selected)
1676 if (!window->in_mouse)
1679 /* XXX: select the entry and kill window later... select is called before
1680 * any other mouse events handlers including the tree view internal one;
1681 * not using a time out would result in a crash. if this doesn't work
1682 * safely, maybe we should set variables when receiving button presses
1683 * in the tree view. */
1684 if (!window->destroying) {
1685 window->destroying = TRUE;
1686 g_idle_add((GSourceFunc) addr_compl_defer_select_destruct, data);
1692 static gboolean addr_compl_defer_select_destruct(CompletionWindow *window)
1694 GtkEntry *entry = GTK_ENTRY(window->entry);
1696 completion_window_apply_selection(GTK_TREE_VIEW(window->list_view),
1699 clear_completion_cache();
1701 addrcompl_destroy_window(window);