Statistics
| Revision:

root / src / addr_compl.c @ 1

History | View | Annotate | Download (26.9 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 *
4
 * Copyright (C) 2000-2004 by Alfons Hoogervorst & The Sylpheed Claws Team.
5
 *
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.
10
 *
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.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 */
20

    
21
#ifdef HAVE_CONFIG_H
22
#  include "config.h"
23
#endif
24
#include "intl.h"
25
#include "defs.h"
26

    
27
#include <glib.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/gtkclist.h>
34
#include <gtk/gtkscrolledwindow.h>
35

    
36
#include <string.h>
37
#include <ctype.h>
38
#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
39
#  include <wchar.h>
40
#  include <wctype.h>
41
#endif
42

    
43
#include "xml.h"
44
#include "addr_compl.h"
45
#include "utils.h"
46
#include "addressbook.h"
47
#include "main.h"
48

    
49
/* How it works:
50
 *
51
 * The address book is read into memory. We set up an address list
52
 * containing all address book entries. Next we make the completion
53
 * list, which contains all the completable strings, and store a
54
 * reference to the address entry it belongs to.
55
 * After calling the g_completion_complete(), we get a reference
56
 * to a valid email address.  
57
 *
58
 * Completion is very simplified. We never complete on another prefix,
59
 * i.e. we neglect the next smallest possible prefix for the current
60
 * completion cache. This is simply done so we might break up the
61
 * addresses a little more (e.g. break up alfons@proteus.demon.nl into
62
 * something like alfons, proteus, demon, nl; and then completing on
63
 * any of those words).
64
 */ 
65
        
66
/* address_entry - structure which refers to the original address entry in the
67
 * address book 
68
 */
69
typedef struct
70
{
71
        gchar *name;
72
        gchar *address;
73
} address_entry;
74

    
75
/* completion_entry - structure used to complete addresses, with a reference
76
 * the the real address information.
77
 */
78
typedef struct
79
{
80
        gchar                *string; /* string to complete */
81
        address_entry        *ref;         /* address the string belongs to  */
82
} completion_entry;
83

    
84
/*******************************************************************************/
85

    
86
static gint            g_ref_count;        /* list ref count */
87
static GList            *g_completion_list;        /* list of strings to be checked */
88
static GList            *g_address_list;        /* address storage */
89
static GCompletion *g_completion;        /* completion object */
90

    
91
/* To allow for continuing completion we have to keep track of the state
92
 * using the following variables. No need to create a context object. */
93

    
94
static gint            g_completion_count;                /* nr of addresses incl. the prefix */
95
static gint            g_completion_next;                /* next prev address */
96
static GSList           *g_completion_addresses;        /* unique addresses found in the
97
                                                   completion cache. */
98
static gchar           *g_completion_prefix;        /* last prefix. (this is cached here
99
                                                 * because the prefix passed to g_completion
100
                                                 * is g_strdown()'ed */
101

    
102
/*******************************************************************************/
103

    
104
/* completion_func() - used by GTK to find the string data to be used for 
105
 * completion 
106
 */
107
static gchar *completion_func(gpointer data)
108
{
109
        g_return_val_if_fail(data != NULL, NULL);
110

    
111
        return ((completion_entry *)data)->string;
112
} 
113

    
114
static void init_all(void)
115
{
116
        g_completion = g_completion_new(completion_func);
117
        g_return_if_fail(g_completion != NULL);
118
}
119

    
120
static void free_all(void)
121
{
122
        GList *walk;
123
        
124
        walk = g_list_first(g_completion_list);
125
        for (; walk != NULL; walk = g_list_next(walk)) {
126
                completion_entry *ce = (completion_entry *) walk->data;
127
                g_free(ce->string);
128
                g_free(walk->data);
129
        }
130
        g_list_free(g_completion_list);
131
        g_completion_list = NULL;
132
        
133
        walk = g_address_list;
134
        for (; walk != NULL; walk = g_list_next(walk)) {
135
                address_entry *ae = (address_entry *) walk->data;
136
                g_free(ae->name);
137
                g_free(ae->address);
138
                g_free(walk->data);
139
        }
140
        g_list_free(g_address_list);
141
        g_address_list = NULL;
142
        
143
        g_completion_free(g_completion);
144
        g_completion = NULL;
145
}
146

    
147
/* add_address() - adds address to the completion list. this function looks
148
 * complicated, but it's only allocation checks.
149
 */
150
static gint add_address(const gchar *name, const gchar *address)
151
{
152
        address_entry    *ae;
153
        completion_entry *ce1;
154
        completion_entry *ce2;
155

    
156
        if (!name || !address) return -1;
157

    
158
        ae = g_new0(address_entry, 1);
159
        ce1 = g_new0(completion_entry, 1),
160
        ce2 = g_new0(completion_entry, 1);
161

    
162
        g_return_val_if_fail(ae != NULL, -1);
163
        g_return_val_if_fail(ce1 != NULL && ce2 != NULL, -1);        
164

    
165
        ae->name    = g_strdup(name);
166
        ae->address = g_strdup(address);                
167

    
168
        /* GCompletion list is case sensitive */
169
        ce1->string = g_utf8_strdown(name, -1);
170
        ce2->string = g_utf8_strdown(address, -1);
171

    
172
        ce1->ref = ce2->ref = ae;
173

    
174
        g_completion_list = g_list_append(g_completion_list, ce1);
175
        g_completion_list = g_list_append(g_completion_list, ce2);
176
        g_address_list           = g_list_append(g_address_list,    ae);
177

    
178
        return 0;
179
}
180

    
181
/* read_address_book()
182
 */ 
183
static void read_address_book(void) {        
184
        addressbook_load_completion( add_address );
185
}
186

    
187
/* start_address_completion() - returns the number of addresses 
188
 * that should be matched for completion.
189
 */
190
gint start_address_completion(void)
191
{
192
        clear_completion_cache();
193
        if (!g_ref_count) {
194
                init_all();
195
                /* open the address book */
196
                read_address_book();
197
                /* merge the completion entry list into g_completion */
198
                if (g_completion_list)
199
                        g_completion_add_items(g_completion, g_completion_list);
200
        }
201
        g_ref_count++;
202
        debug_print("start_address_completion ref count %d\n", g_ref_count);
203

    
204
        return g_list_length(g_completion_list);
205
}
206

    
207
/* get_address_from_edit() - returns a possible address (or a part)
208
 * from an entry box. To make life easier, we only look at the last valid address 
209
 * component; address completion only works at the last string component in
210
 * the entry box. 
211
 */ 
212
gchar *get_address_from_edit(GtkEntry *entry, gint *start_pos)
213
{
214
        const gchar *edit_text, *p;
215
        gint cur_pos;
216
        gboolean in_quote = FALSE;
217
        gboolean in_bracket = FALSE;
218
        gchar *str;
219

    
220
        edit_text = gtk_entry_get_text(entry);
221
        if (edit_text == NULL) return NULL;
222

    
223
        cur_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
224

    
225
        /* scan for a separator. doesn't matter if walk points at null byte. */
226
        for (p = g_utf8_offset_to_pointer(edit_text, cur_pos);
227
             p > edit_text;
228
             p = g_utf8_prev_char(p)) {
229
                if (*p == '"')
230
                        in_quote ^= TRUE;
231
                else if (!in_quote) {
232
                        if (!in_bracket && *p == ',')
233
                                break;
234
                        else if (*p == '>')
235
                                in_bracket = TRUE;
236
                        else if (*p == '<')
237
                                in_bracket = FALSE;
238
                }
239
        }
240

    
241
        /* have something valid */
242
        if (g_utf8_strlen(p, -1) == 0)
243
                return NULL;
244

    
245
#define IS_VALID_CHAR(x) \
246
        (isalnum(x) || (x) == '"' || (x) == '<' || ((guchar)(x) > 0x7f))
247

    
248
        /* now scan back until we hit a valid character */
249
        for (; *p && !IS_VALID_CHAR(*p); p = g_utf8_next_char(p))
250
                ;
251

    
252
#undef IS_VALID_CHAR
253

    
254
        if (g_utf8_strlen(p, -1) == 0)
255
                return NULL;
256

    
257
        if (start_pos) *start_pos = g_utf8_pointer_to_offset(edit_text, p);
258

    
259
        str = g_strdup(p);
260

    
261
        return str;
262
} 
263

    
264
/* replace_address_in_edit() - replaces an incompleted address with a completed one.
265
 */
266
void replace_address_in_edit(GtkEntry *entry, const gchar *newtext,
267
                             gint start_pos)
268
{
269
        if (!newtext) return;
270

    
271
        gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, -1);
272
        gtk_editable_insert_text(GTK_EDITABLE(entry), newtext, strlen(newtext),
273
                                 &start_pos);
274
        gtk_editable_set_position(GTK_EDITABLE(entry), -1);
275
}
276

    
277
/* complete_address() - tries to complete an addres, and returns the
278
 * number of addresses found. use get_complete_address() to get one.
279
 * returns zero if no match was found, otherwise the number of addresses,
280
 * with the original prefix at index 0. 
281
 */
282
guint complete_address(const gchar *str)
283
{
284
        GList *result;
285
        gchar *d;
286
        guint  count, cpl;
287
        completion_entry *ce;
288

    
289
        g_return_val_if_fail(str != NULL, 0);
290

    
291
        clear_completion_cache();
292
        g_completion_prefix = g_strdup(str);
293

    
294
        /* g_completion is case sensitive */
295
        d = g_utf8_strdown(str, -1);
296
        result = g_completion_complete(g_completion, d, NULL);
297

    
298
        count = g_list_length(result);
299
        if (count) {
300
                /* create list with unique addresses  */
301
                for (cpl = 0, result = g_list_first(result);
302
                     result != NULL;
303
                     result = g_list_next(result)) {
304
                        ce = (completion_entry *)(result->data);
305
                        if (NULL == g_slist_find(g_completion_addresses,
306
                                                 ce->ref)) {
307
                                cpl++;
308
                                g_completion_addresses =
309
                                        g_slist_append(g_completion_addresses,
310
                                                       ce->ref);
311
                        }
312
                }
313
                count = cpl + 1;        /* index 0 is the original prefix */
314
                g_completion_next = 1;        /* we start at the first completed one */
315
        } else {
316
                g_free(g_completion_prefix);
317
                g_completion_prefix = NULL;
318
        }
319

    
320
        g_completion_count = count;
321

    
322
        g_free(d);
323

    
324
        return count;
325
}
326

    
327
/* get_complete_address() - returns a complete address. the returned
328
 * string should be freed 
329
 */
330
gchar *get_complete_address(gint index)
331
{
332
        const address_entry *p;
333
        gchar *address = NULL;
334

    
335
        if (index < g_completion_count) {
336
                if (index == 0)
337
                        address = g_strdup(g_completion_prefix);
338
                else {
339
                        /* get something from the unique addresses */
340
                        p = (address_entry *)g_slist_nth_data
341
                                (g_completion_addresses, index - 1);
342
                        if (p != NULL) {
343
                                if (!p->name || p->name[0] == '\0')
344
                                        address = g_strdup_printf(p->address);
345
                                else if (p->name[0] != '"' &&
346
                                         strpbrk(p->name, ",.[]<>") != NULL)
347
                                        address = g_strdup_printf
348
                                                ("\"%s\" <%s>", p->name, p->address);
349
                                else
350
                                        address = g_strdup_printf
351
                                                ("%s <%s>", p->name, p->address);
352
                        }
353
                }
354
        }
355

    
356
        return address;
357
}
358

    
359
gchar *get_next_complete_address(void)
360
{
361
        if (is_completion_pending()) {
362
                gchar *res;
363

    
364
                res = get_complete_address(g_completion_next);
365
                g_completion_next += 1;
366
                if (g_completion_next >= g_completion_count)
367
                        g_completion_next = 0;
368

    
369
                return res;
370
        } else
371
                return NULL;
372
}
373

    
374
gchar *get_prev_complete_address(void)
375
{
376
        if (is_completion_pending()) {
377
                int n = g_completion_next - 2;
378

    
379
                /* real previous */
380
                n = (n + (g_completion_count * 5)) % g_completion_count;
381

    
382
                /* real next */
383
                g_completion_next = n + 1;
384
                if (g_completion_next >=  g_completion_count)
385
                        g_completion_next = 0;
386
                return get_complete_address(n);
387
        } else
388
                return NULL;
389
}
390

    
391
guint get_completion_count(void)
392
{
393
        if (is_completion_pending())
394
                return g_completion_count;
395
        else
396
                return 0;
397
}
398

    
399
/* should clear up anything after complete_address() */
400
void clear_completion_cache(void)
401
{
402
        if (is_completion_pending()) {
403
                if (g_completion_prefix)
404
                        g_free(g_completion_prefix);
405

    
406
                if (g_completion_addresses) {
407
                        g_slist_free(g_completion_addresses);
408
                        g_completion_addresses = NULL;
409
                }
410

    
411
                g_completion_count = g_completion_next = 0;
412
        }
413
}
414

    
415
gboolean is_completion_pending(void)
416
{
417
        /* check if completion pending, i.e. we might satisfy a request for the next
418
         * or previous address */
419
         return g_completion_count;
420
}
421

    
422
/* invalidate_address_completion() - should be called if address book
423
 * changed; 
424
 */
425
gint invalidate_address_completion(void)
426
{
427
        if (g_ref_count) {
428
                /* simply the same as start_address_completion() */
429
                debug_print("Invalidation request for address completion\n");
430
                free_all();
431
                init_all();
432
                read_address_book();
433
                if (g_completion_list)
434
                        g_completion_add_items(g_completion, g_completion_list);
435
                clear_completion_cache();
436
        }
437

    
438
        return g_list_length(g_completion_list);
439
}
440

    
441
gint end_address_completion(void)
442
{
443
        clear_completion_cache();
444

    
445
        if (0 == --g_ref_count)
446
                free_all();
447

    
448
        debug_print("end_address_completion ref count %d\n", g_ref_count);
449

    
450
        return g_ref_count; 
451
}
452

    
453

    
454
/* address completion entry ui. the ui (completion list was inspired by galeon's
455
 * auto completion list). remaining things powered by sylpheed's completion engine.
456
 */
457

    
458
#define ENTRY_DATA_TAB_HOOK        "tab_hook"                        /* used to lookup entry */
459
#define WINDOW_DATA_COMPL_ENTRY        "compl_entry"        /* used to store entry for compl. window */
460
#define WINDOW_DATA_COMPL_CLIST "compl_clist"        /* used to store clist for compl. window */
461

    
462
static void address_completion_mainwindow_set_focus        (GtkWindow   *window,
463
                                                         GtkWidget   *widget,
464
                                                         gpointer     data);
465
static gboolean address_completion_entry_key_pressed        (GtkEntry    *entry,
466
                                                         GdkEventKey *ev,
467
                                                         gpointer     data);
468
static gboolean address_completion_complete_address_in_entry
469
                                                        (GtkEntry    *entry,
470
                                                         gboolean     next);
471
static void address_completion_create_completion_window        (GtkEntry    *entry);
472

    
473
static void completion_window_select_row(GtkCList         *clist,
474
                                         gint                  row,
475
                                         gint                  col,
476
                                         GdkEvent         *event,
477
                                         GtkWidget        **completion_window);
478
static gboolean completion_window_button_press
479
                                        (GtkWidget         *widget,
480
                                         GdkEventButton  *event,
481
                                         GtkWidget        **completion_window);
482
static gboolean completion_window_key_press
483
                                        (GtkWidget         *widget,
484
                                         GdkEventKey         *event,
485
                                         GtkWidget        **completion_window);
486

    
487

    
488
static void completion_window_advance_to_row(GtkCList *clist, gint row)
489
{
490
        g_return_if_fail(row < g_completion_count);
491
        gtk_clist_select_row(clist, row, 0);
492
}
493

    
494
static void completion_window_advance_selection(GtkCList *clist, gboolean forward)
495
{
496
        int row;
497

    
498
        g_return_if_fail(clist != NULL);
499
        g_return_if_fail(clist->selection != NULL);
500

    
501
        row = GPOINTER_TO_INT(clist->selection->data);
502

    
503
        row = forward ? (row + 1) % g_completion_count :
504
                        (row - 1) < 0 ? g_completion_count - 1 : row - 1;
505

    
506
        gtk_clist_freeze(clist);
507
        completion_window_advance_to_row(clist, row);                                        
508
        gtk_clist_thaw(clist);
509
}
510

    
511
#if 0
512
/* completion_window_accept_selection() - accepts the current selection in the
513
 * clist, and destroys the window */
514
static void completion_window_accept_selection(GtkWidget **window,
515
                                               GtkCList *clist,
516
                                               GtkEntry *entry)
517
{
518
        gchar *address = NULL, *text = NULL;
519
        gint   cursor_pos, row;
520

521
        g_return_if_fail(window != NULL);
522
        g_return_if_fail(*window != NULL);
523
        g_return_if_fail(clist != NULL);
524
        g_return_if_fail(entry != NULL);
525
        g_return_if_fail(clist->selection != NULL);
526

527
        /* FIXME: I believe it's acceptable to access the selection member directly  */
528
        row = GPOINTER_TO_INT(clist->selection->data);
529

530
        /* we just need the cursor position */
531
        address = get_address_from_edit(entry, &cursor_pos);
532
        g_free(address);
533
        gtk_clist_get_text(clist, row, 0, &text);
534
        replace_address_in_edit(entry, text, cursor_pos);
535

536
        clear_completion_cache();
537
        gtk_widget_destroy(*window);
538
        *window = NULL;
539
}
540
#endif
541

    
542
/* completion_window_apply_selection() - apply the current selection in the
543
 * clist */
544
static void completion_window_apply_selection(GtkCList *clist, GtkEntry *entry)
545
{
546
        gchar *address = NULL, *text = NULL;
547
        gint   cursor_pos, row;
548

    
549
        g_return_if_fail(clist != NULL);
550
        g_return_if_fail(entry != NULL);
551
        g_return_if_fail(clist->selection != NULL);
552

    
553
        row = GPOINTER_TO_INT(clist->selection->data);
554

    
555
        address = get_address_from_edit(entry, &cursor_pos);
556
        g_free(address);
557
        gtk_clist_get_text(clist, row, 0, &text);
558
        replace_address_in_edit(entry, text, cursor_pos);
559
}
560

    
561
/* should be called when creating the main window containing address
562
 * completion entries */
563
void address_completion_start(GtkWidget *mainwindow)
564
{
565
        start_address_completion();
566

    
567
        /* register focus change hook */
568
        g_signal_connect(G_OBJECT(mainwindow), "set_focus",
569
                         G_CALLBACK(address_completion_mainwindow_set_focus),
570
                         mainwindow);
571
}
572

    
573
/* Need unique data to make unregistering signal handler possible for the auto
574
 * completed entry */
575
#define COMPLETION_UNIQUE_DATA (GINT_TO_POINTER(0xfeefaa))
576

    
577
void address_completion_register_entry(GtkEntry *entry)
578
{
579
        g_return_if_fail(entry != NULL);
580
        g_return_if_fail(GTK_IS_ENTRY(entry));
581

    
582
        /* add hooked property */
583
        g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, entry);
584

    
585
        /* add keypress event */
586
        g_signal_connect_closure
587
                (G_OBJECT(entry), "key_press_event",
588
                 g_cclosure_new
589
                        (G_CALLBACK(address_completion_entry_key_pressed),
590
                         COMPLETION_UNIQUE_DATA, NULL),
591
                 FALSE);
592
}
593

    
594
void address_completion_unregister_entry(GtkEntry *entry)
595
{
596
        GObject *entry_obj;
597

    
598
        g_return_if_fail(entry != NULL);
599
        g_return_if_fail(GTK_IS_ENTRY(entry));
600

    
601
        entry_obj = g_object_get_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK);
602
        g_return_if_fail(entry_obj);
603
        g_return_if_fail(entry_obj == G_OBJECT(entry));
604

    
605
        /* has the hooked property? */
606
        g_object_set_data(G_OBJECT(entry), ENTRY_DATA_TAB_HOOK, NULL);
607

    
608
        /* remove the hook */
609
        g_signal_handlers_disconnect_by_func
610
                (G_OBJECT(entry), 
611
                 G_CALLBACK(address_completion_entry_key_pressed),
612
                 COMPLETION_UNIQUE_DATA);
613
}
614

    
615
/* should be called when main window with address completion entries
616
 * terminates.
617
 * NOTE: this function assumes that it is called upon destruction of
618
 * the window */
619
void address_completion_end(GtkWidget *mainwindow)
620
{
621
        /* if address_completion_end() is really called on closing the window,
622
         * we don't need to unregister the set_focus_cb */
623
        end_address_completion();
624
}
625

    
626
/* if focus changes to another entry, then clear completion cache */
627
static void address_completion_mainwindow_set_focus(GtkWindow *window,
628
                                                    GtkWidget *widget,
629
                                                    gpointer   data)
630
{
631
        if (widget && GTK_IS_ENTRY(widget) &&
632
            g_object_get_data(G_OBJECT(widget), ENTRY_DATA_TAB_HOOK)) 
633
                clear_completion_cache();
634
}
635

    
636
/* watch for tabs in one of the address entries. if no tab then clear the
637
 * completion cache */
638
static gboolean address_completion_entry_key_pressed(GtkEntry    *entry,
639
                                                     GdkEventKey *ev,
640
                                                     gpointer     data)
641
{
642
        if (ev->keyval == GDK_Tab) {
643
                if (address_completion_complete_address_in_entry(entry, TRUE)) {
644
                        address_completion_create_completion_window(entry);
645
                        /* route a void character to the default handler */
646
                        /* this is a dirty hack; we're actually changing a key
647
                         * reported by the system. */
648
                        ev->keyval = GDK_AudibleBell_Enable;
649
                        ev->state &= ~GDK_SHIFT_MASK;
650
                        //gtk_signal_emit_stop_by_name(G_OBJECT(entry),
651
                        //                             "key_press_event");
652
                        return TRUE; //
653
                } else {
654
                        /* old behaviour */
655
                }
656
        } else if (ev->keyval == GDK_Shift_L
657
                || ev->keyval == GDK_Shift_R
658
                || ev->keyval == GDK_Control_L
659
                || ev->keyval == GDK_Control_R
660
                || ev->keyval == GDK_Caps_Lock
661
                || ev->keyval == GDK_Shift_Lock
662
                || ev->keyval == GDK_Meta_L
663
                || ev->keyval == GDK_Meta_R
664
                || ev->keyval == GDK_Alt_L
665
                || ev->keyval == GDK_Alt_R) {
666
                /* these buttons should not clear the cache... */
667
        } else
668
                clear_completion_cache();
669

    
670
        return FALSE;
671
}
672

    
673
/* initialize the completion cache and put first completed string
674
 * in entry. this function used to do back cycling but this is not
675
 * currently used. since the address completion behaviour has been
676
 * changed regularly, we keep the feature in case someone changes
677
 * his / her mind again. :) */
678
static gboolean address_completion_complete_address_in_entry(GtkEntry *entry,
679
                                                             gboolean  next)
680
{
681
        gint ncount, cursor_pos;
682
        gchar *address, *new = NULL;
683
        gboolean completed = FALSE;
684

    
685
        g_return_val_if_fail(entry != NULL, FALSE);
686

    
687
        if (!GTK_WIDGET_HAS_FOCUS(entry)) return FALSE;
688

    
689
        /* get an address component from the cursor */
690
        address = get_address_from_edit(entry, &cursor_pos);
691
        if (!address) return FALSE;
692

    
693
        /* still something in the cache */
694
        if (is_completion_pending()) {
695
                new = next ? get_next_complete_address() :
696
                        get_prev_complete_address();
697
        } else {
698
                if (0 < (ncount = complete_address(address)))
699
                        new = get_next_complete_address();
700
        }
701

    
702
        if (new) {
703
                /* prevent "change" signal */
704
                /* replace_address_in_edit(entry, new, cursor_pos); */
705
                g_free(new);
706
                completed = TRUE;
707
        }
708

    
709
        g_free(address);
710

    
711
        return completed;
712
}
713

    
714
static void address_completion_create_completion_window(GtkEntry *entry_)
715
{
716
        static GtkWidget *completion_window;
717
        gint x, y, height, width, depth;
718
        GtkWidget *scroll, *clist;
719
        GtkRequisition r;
720
        guint count = 0;
721
        GtkWidget *entry = GTK_WIDGET(entry_);
722

    
723
        if (completion_window) {
724
                gtk_widget_destroy(completion_window);
725
                completion_window = NULL;
726
        }
727

    
728
        scroll = gtk_scrolled_window_new(NULL, NULL);
729
        clist  = gtk_clist_new(1);
730
        gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_SINGLE);
731
        
732
        completion_window = gtk_window_new(GTK_WINDOW_POPUP);
733

    
734
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
735
                                       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
736
        gtk_container_add(GTK_CONTAINER(completion_window), scroll);
737
        gtk_container_add(GTK_CONTAINER(scroll), clist);
738

    
739
        /* set the unique data so we can always get back the entry and
740
         * clist window to which this completion window has been attached */
741
        g_object_set_data(G_OBJECT(completion_window),
742
                          WINDOW_DATA_COMPL_ENTRY, entry_);
743
        g_object_set_data(G_OBJECT(completion_window),
744
                          WINDOW_DATA_COMPL_CLIST, clist);
745

    
746
        g_signal_connect(G_OBJECT(clist), "select_row",
747
                         G_CALLBACK(completion_window_select_row),
748
                         &completion_window);
749

    
750
        for (count = 0; count < get_completion_count(); count++) {
751
                gchar *text[] = {NULL, NULL};
752

    
753
                text[0] = get_complete_address(count);
754
                gtk_clist_append(GTK_CLIST(clist), text);
755
                g_free(text[0]);
756
        }
757

    
758
        gdk_window_get_geometry(entry->window, &x, &y, &width, &height, &depth);
759
        gdk_window_get_deskrelative_origin (entry->window, &x, &y);
760
        y += height;
761
        gtk_window_move(GTK_WINDOW(completion_window), x, y);
762

    
763
        gtk_widget_size_request(clist, &r);
764
        gtk_widget_set_size_request(completion_window, width, r.height);
765
        gtk_widget_show_all(completion_window);
766
        gtk_widget_size_request(clist, &r);
767

    
768
        if ((y + r.height) > gdk_screen_height()) {
769
                gtk_window_set_policy(GTK_WINDOW(completion_window),
770
                                      TRUE, FALSE, FALSE);
771
                gtk_widget_set_size_request(completion_window, width,
772
                                            gdk_screen_height () - y);
773
        }
774

    
775
        g_signal_connect(G_OBJECT(completion_window),
776
                         "button-press-event",
777
                         G_CALLBACK(completion_window_button_press),
778
                         &completion_window);
779
        g_signal_connect(G_OBJECT(completion_window),
780
                         "key-press-event",
781
                         G_CALLBACK(completion_window_key_press),
782
                         &completion_window);
783
        gdk_pointer_grab(completion_window->window, TRUE,
784
                         GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK |
785
                         GDK_BUTTON_RELEASE_MASK,
786
                         NULL, NULL, GDK_CURRENT_TIME);
787
        gtk_grab_add(completion_window);
788

    
789
        /* this gets rid of the irritating focus rectangle that doesn't
790
         * follow the selection */
791
        GTK_WIDGET_UNSET_FLAGS(clist, GTK_CAN_FOCUS);
792
        gtk_clist_select_row(GTK_CLIST(clist), 1, 0);
793
}
794

    
795

    
796
/* row selection sends completed address to entry.
797
 * note: event is NULL if selected by anything else than a mouse button. */
798
static void completion_window_select_row(GtkCList *clist, gint row, gint col,
799
                                         GdkEvent *event,
800
                                         GtkWidget **completion_window)
801
{
802
        GtkEntry *entry;
803

    
804
        g_return_if_fail(completion_window != NULL);
805
        g_return_if_fail(*completion_window != NULL);
806

    
807
        entry = GTK_ENTRY(g_object_get_data(G_OBJECT(*completion_window),
808
                                            WINDOW_DATA_COMPL_ENTRY));
809
        g_return_if_fail(entry != NULL);
810

    
811
        completion_window_apply_selection(clist, entry);
812

    
813
        if (!event || event->type != GDK_BUTTON_RELEASE)
814
                return;
815

    
816
        clear_completion_cache();
817
        gtk_widget_destroy(*completion_window);
818
        *completion_window = NULL;
819
}
820

    
821
/* completion_window_button_press() - check is mouse click is anywhere
822
 * else (not in the completion window). in that case the completion
823
 * window is destroyed, and the original prefix is restored */
824
static gboolean completion_window_button_press(GtkWidget *widget,
825
                                               GdkEventButton *event,
826
                                               GtkWidget **completion_window)
827
{
828
        GtkWidget *event_widget, *entry;
829
        gchar *prefix;
830
        gint cursor_pos;
831
        gboolean restore = TRUE;
832

    
833
        g_return_val_if_fail(completion_window != NULL, FALSE);
834
        g_return_val_if_fail(*completion_window != NULL, FALSE);
835

    
836
        entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
837
                                             WINDOW_DATA_COMPL_ENTRY));
838
        g_return_val_if_fail(entry != NULL, FALSE);
839

    
840
        event_widget = gtk_get_event_widget((GdkEvent *)event);
841
        if (event_widget != widget) {
842
                while (event_widget) {
843
                        if (event_widget == widget)
844
                                return FALSE;
845
                        else if (event_widget == entry) {
846
                                restore = FALSE;
847
                                break;
848
                        }
849
                    event_widget = event_widget->parent;
850
                }
851
        }
852

    
853
        if (restore) {
854
                prefix = get_complete_address(0);
855
                g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
856
                replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
857
                g_free(prefix);
858
        }
859

    
860
        clear_completion_cache();
861
        gtk_widget_destroy(*completion_window);
862
        *completion_window = NULL;
863

    
864
        return TRUE;
865
}
866

    
867
static gboolean completion_window_key_press(GtkWidget *widget,
868
                                            GdkEventKey *event,
869
                                            GtkWidget **completion_window)
870
{
871
        GdkEventKey tmp_event;
872
        GtkWidget *entry;
873
        gchar *prefix;
874
        gint cursor_pos;
875
        GtkWidget *clist;
876

    
877
        g_return_val_if_fail(completion_window != NULL, FALSE);
878
        g_return_val_if_fail(*completion_window != NULL, FALSE);
879

    
880
        entry = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
881
                                             WINDOW_DATA_COMPL_ENTRY));
882
        clist = GTK_WIDGET(g_object_get_data(G_OBJECT(*completion_window),
883
                                             WINDOW_DATA_COMPL_CLIST));
884
        g_return_val_if_fail(entry != NULL, FALSE);
885

    
886
        /* allow keyboard navigation in the alternatives clist */
887
        if (event->keyval == GDK_Up || event->keyval == GDK_Down ||
888
            event->keyval == GDK_Page_Up || event->keyval == GDK_Page_Down) {
889
                completion_window_advance_selection
890
                        (GTK_CLIST(clist),
891
                         event->keyval == GDK_Down ||
892
                         event->keyval == GDK_Page_Down ? TRUE : FALSE);
893
                return FALSE;
894
        }                
895

    
896
        /* also make tab / shift tab go to next previous completion entry. we're
897
         * changing the key value */
898
        if (event->keyval == GDK_Tab || event->keyval == GDK_ISO_Left_Tab) {
899
                event->keyval = (event->state & GDK_SHIFT_MASK)
900
                        ? GDK_Up : GDK_Down;
901
                /* need to reset shift state if going up */
902
                if (event->state & GDK_SHIFT_MASK)
903
                        event->state &= ~GDK_SHIFT_MASK;
904
                completion_window_advance_selection(GTK_CLIST(clist), 
905
                        event->keyval == GDK_Down ? TRUE : FALSE);
906
                return FALSE;
907
        }
908

    
909
        /* look for presses that accept the selection */
910
        if (event->keyval == GDK_Return || event->keyval == GDK_space) {
911
                clear_completion_cache();
912
                gtk_widget_destroy(*completion_window);
913
                *completion_window = NULL;
914
                return FALSE;
915
        }
916

    
917
        /* key state keys should never be handled */
918
        if (event->keyval == GDK_Shift_L
919
                 || event->keyval == GDK_Shift_R
920
                 || event->keyval == GDK_Control_L
921
                 || event->keyval == GDK_Control_R
922
                 || event->keyval == GDK_Caps_Lock
923
                 || event->keyval == GDK_Shift_Lock
924
                 || event->keyval == GDK_Meta_L
925
                 || event->keyval == GDK_Meta_R
926
                 || event->keyval == GDK_Alt_L
927
                 || event->keyval == GDK_Alt_R) {
928
                return FALSE;
929
        }
930

    
931
        /* other key, let's restore the prefix (orignal text) */
932
        prefix = get_complete_address(0);
933
        g_free(get_address_from_edit(GTK_ENTRY(entry), &cursor_pos));
934
        replace_address_in_edit(GTK_ENTRY(entry), prefix, cursor_pos);
935
        g_free(prefix);
936
        clear_completion_cache();
937

    
938
        /* make sure anything we typed comes in the edit box */
939
        tmp_event.type       = event->type;
940
        tmp_event.window     = entry->window;
941
        tmp_event.send_event = TRUE;
942
        tmp_event.time       = event->time;
943
        tmp_event.state      = event->state;
944
        tmp_event.keyval     = event->keyval;
945
        tmp_event.length     = event->length;
946
        tmp_event.string     = event->string;
947
        gtk_widget_event(entry, (GdkEvent *)&tmp_event);
948

    
949
        /* and close the completion window */
950
        gtk_widget_destroy(*completion_window);
951
        *completion_window = NULL;
952

    
953
        return TRUE;
954
}