aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-autocompletion.c
blob: 265590003af52e5338c321c56306371c0177f476 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*
 *  Copyright (C) 2002  Ricardo Fernández Pascual
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <stdlib.h>

#include "ephy-autocompletion.h"
#include "ephy-gobject-misc.h"
#include "ephy-marshal.h"

#define NOT_IMPLEMENTED g_warning ("not implemented: " G_STRLOC);

//#define DEBUG_MSG(x) g_print x
#define DEBUG_MSG(x)

//#define DEBUG_TIME

#ifdef DEBUG_TIME
#include <glib/gtimer.h>
#endif

/**
 * Private data
 */

typedef enum {
    GAS_NEEDS_REFINE,
    GAS_NEEDS_FULL_UPDATE,
    GAS_UPDATED
} EphyAutocompletionStatus;

typedef struct {
    EphyAutocompletionMatch *array;
    guint num_matches;
    guint num_action_matches;
    guint array_size;
} ACMatchArray;

#define ACMA_BASE_SIZE 10240

struct _EphyAutocompletionPrivate {
    GSList *sources;

    guint nkeys;
    gchar **keys;
    guint *key_lengths;
    gchar **prefixes;
    guint *prefix_lengths;

    gchar *common_prefix;
    ACMatchArray matches;
    EphyAutocompletionStatus status;
    gboolean sorted;
    gboolean changed;

    gboolean sort_alpha;
};

/**
 * Private functions, only availble from this file
 */
static void     ephy_autocompletion_class_init          (EphyAutocompletionClass *klass);
static void     ephy_autocompletion_init            (EphyAutocompletion *e);
static void     ephy_autocompletion_finalize_impl       (GObject *o);
static void     ephy_autocompletion_reset           (EphyAutocompletion *ac);
static void     ephy_autocompletion_update_matches      (EphyAutocompletion *ac);
static void     ephy_autocompletion_update_matches_full     (EphyAutocompletion *ac);
static gboolean     ephy_autocompletion_sort_by_score       (EphyAutocompletion *ac);
static void     ephy_autocompletion_data_changed_cb     (EphyAutocompletionSource *s,
                                     EphyAutocompletion *ac);

static void     acma_init                   (ACMatchArray *a);
static void     acma_destroy                    (ACMatchArray *a);
static inline void  acma_append                 (ACMatchArray *a,
                                     EphyAutocompletionMatch *m,
                                     gboolean action);
static void     acma_grow                   (ACMatchArray *a);


static gpointer g_object_class;

/**
 * Signals enums and ids
 */
enum EphyAutocompletionSignalsEnum {
    EPHY_AUTOCOMPLETION_SOURCES_CHANGED,
    EPHY_AUTOCOMPLETION_LAST_SIGNAL
};
static gint EphyAutocompletionSignals[EPHY_AUTOCOMPLETION_LAST_SIGNAL];

/**
 * Autocompletion object
 */

MAKE_GET_TYPE (ephy_autocompletion, "EphyAutocompletion", EphyAutocompletion,
       ephy_autocompletion_class_init, ephy_autocompletion_init, G_TYPE_OBJECT);

static void
ephy_autocompletion_class_init (EphyAutocompletionClass *klass)
{
    G_OBJECT_CLASS (klass)->finalize = ephy_autocompletion_finalize_impl;
    g_object_class = g_type_class_peek_parent (klass);

    EphyAutocompletionSignals[EPHY_AUTOCOMPLETION_SOURCES_CHANGED] = g_signal_new (
        "sources-changed", G_OBJECT_CLASS_TYPE (klass),
        G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
                G_STRUCT_OFFSET (EphyAutocompletionClass, sources_changed),
        NULL, NULL,
        ephy_marshal_VOID__VOID,
        G_TYPE_NONE, 0);
}

static void
ephy_autocompletion_init (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = g_new0 (EphyAutocompletionPrivate, 1);
    ac->priv = p;
    p->sources = NULL;
    p->common_prefix = NULL;
    acma_init (&p->matches);
    p->status = GAS_NEEDS_FULL_UPDATE;

    p->nkeys = 1;

    p->keys = g_new0 (gchar *, 2);
    p->keys[0] = g_strdup ("");
    p->key_lengths = g_new0 (guint, 2);
    p->key_lengths[0] = 0;

    p->prefixes = g_new0 (gchar *, 2);
    p->prefixes[0] = g_strdup ("");
    p->prefix_lengths = g_new0 (guint, 2);
    p->prefix_lengths[0] = 0;

    p->sort_alpha = TRUE;
}

static void
ephy_autocompletion_finalize_impl (GObject *o)
{
    EphyAutocompletion *ac = EPHY_AUTOCOMPLETION (o);
    EphyAutocompletionPrivate *p = ac->priv;
    GSList *li;

    ephy_autocompletion_reset (ac);

    for (li = p->sources; li; li = li->next)
    {
        g_signal_handlers_disconnect_by_func (li->data,
                              ephy_autocompletion_data_changed_cb, ac);
        g_object_unref (li->data);
    }

    g_slist_free (p->sources);

    g_strfreev (p->keys);
    g_free (p->key_lengths);
    g_strfreev (p->prefixes);
    g_free (p->prefix_lengths);

    G_OBJECT_CLASS (g_object_class)->finalize (o);

}

static void
ephy_autocompletion_reset (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
#ifdef DEBUG_TIME
    GTimer *timer = g_timer_new ();
    g_timer_start (timer);
#endif
    acma_destroy (&p->matches);
    g_free (p->common_prefix);
    p->common_prefix = NULL;
    p->status = GAS_NEEDS_FULL_UPDATE;
#ifdef DEBUG_TIME
    DEBUG_MSG (("AC: %f elapsed resetting\n", g_timer_elapsed (timer, NULL)));
    g_timer_destroy (timer);
#endif
}

EphyAutocompletion *
ephy_autocompletion_new (void)
{
    EphyAutocompletion *ret = g_object_new (EPHY_TYPE_AUTOCOMPLETION, NULL);
    return ret;
}
void
ephy_autocompletion_add_source (EphyAutocompletion *ac,
                EphyAutocompletionSource *s)
{
    EphyAutocompletionPrivate *p = ac->priv;
    g_object_ref (G_OBJECT (s));
    p->sources = g_slist_prepend (p->sources, s);
    ephy_autocompletion_reset (ac);
    g_signal_connect (s, "data-changed", G_CALLBACK (ephy_autocompletion_data_changed_cb), ac);

    g_signal_emit (ac, EphyAutocompletionSignals[EPHY_AUTOCOMPLETION_SOURCES_CHANGED], 0);
}

void
ephy_autocompletion_set_key (EphyAutocompletion *ac,
                 const gchar *key)
{
    EphyAutocompletionPrivate *p = ac->priv;
    guint i;
    guint keylen = strlen (key);

    if (strcmp (key, p->keys[0]))
    {
        GSList *li;
        for (li = p->sources; li; li = li->next)
        {
            ephy_autocompletion_source_set_basic_key
                (EPHY_AUTOCOMPLETION_SOURCE (li->data), key);
        }
    }

    if (keylen >= p->key_lengths[0]
        && !strncmp (p->keys[0], key, p->key_lengths[0]))
    {
        if (!strcmp (key, p->keys[0]))
        {
            return;
        }
        if (p->status != GAS_NEEDS_FULL_UPDATE)
        {
            p->status = GAS_NEEDS_REFINE;
        }
        if (p->common_prefix)
        {
            if (strncmp (p->common_prefix, key, keylen))
            {
                g_free (p->common_prefix);
                p->common_prefix = NULL;
            }
        }
    }
    else
    {
        p->status = GAS_NEEDS_FULL_UPDATE;
        g_free (p->common_prefix);
        p->common_prefix = NULL;
    }

    for (i = 0; p->prefixes[i]; ++i)
    {
        g_free (p->keys[i]);
        p->keys[i] = g_strconcat (p->prefixes[i], key, NULL);
        p->key_lengths[i] = keylen + p->prefix_lengths[i];
    }

}

gchar *
ephy_autocompletion_get_common_prefix (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
    ephy_autocompletion_update_matches (ac);
    if (!p->common_prefix)
    {
        guint common_length = 0;
        guint i;
#ifdef DEBUG_TIME
        GTimer *timer = g_timer_new ();
        g_timer_start (timer);
#endif
        for (i = 0; i < p->matches.num_matches; i++)
        {
            EphyAutocompletionMatch *mi = &p->matches.array[i];
            const gchar *realmatch = mi->title + mi->offset;
            if (!p->common_prefix)
            {
                p->common_prefix = g_strdup (realmatch);
                common_length = strlen (p->common_prefix);
                continue;
            }
            else if (!strncmp (realmatch, p->common_prefix, common_length))
            {
                continue;
            }
            else
            {
                common_length = 0;
                while (realmatch[common_length]
                       && realmatch[common_length] == p->common_prefix[common_length])
                {
                    ++common_length;
                }
                g_free (p->common_prefix);
                p->common_prefix = g_strndup (realmatch, common_length);
            }
        }
#ifdef DEBUG_TIME
        DEBUG_MSG (("AC: %f elapsed calculating common prefix\n", g_timer_elapsed (timer, NULL)));
        g_timer_destroy (timer);
#endif
    }
    return g_strdup (p->common_prefix);
}

const EphyAutocompletionMatch *
ephy_autocompletion_get_matches (EphyAutocompletion *ac)
{
    ephy_autocompletion_update_matches (ac);
    return ac->priv->matches.array;
}

const EphyAutocompletionMatch *
ephy_autocompletion_get_matches_sorted_by_score (EphyAutocompletion *ac,
                         gboolean *changed)
{
    *changed = ephy_autocompletion_sort_by_score (ac);
    return ac->priv->matches.array;
}

guint
ephy_autocompletion_get_num_matches (EphyAutocompletion *ac)
{
    ephy_autocompletion_update_matches (ac);

    return ac->priv->matches.num_matches;
}

guint
ephy_autocompletion_get_num_action_matches (EphyAutocompletion *ac)
{
    return ac->priv->matches.num_matches -
        ac->priv->matches.num_action_matches;
}

static void
ephy_autocompletion_refine_matches (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
    ACMatchArray oldmatches = p->matches;
    ACMatchArray newmatches;
    guint i;
    gchar *key0 = p->keys[0];
    guint key0l = p->key_lengths[0];
#ifdef DEBUG_TIME
    GTimer *timer = g_timer_new ();
#endif
    DEBUG_MSG (("AC: refining\n"));

#ifdef DEBUG_TIME
    g_timer_start (timer);
#endif
    acma_init (&newmatches);

    p->changed = FALSE;

    for (i = 0; i < oldmatches.num_matches; i++)
    {
        EphyAutocompletionMatch *mi = &oldmatches.array[i];

        if (mi->is_action ||
            (mi->substring && g_strrstr (mi->match, p->keys[0])) ||
            (mi->substring && g_strrstr (mi->title, p->keys[0])) ||
            !strncmp (key0, mi->title + mi->offset, key0l))
        {
            acma_append (&newmatches, mi, mi->is_action);
        }
        else
        {
            p->changed = TRUE;
        }
    }

    acma_destroy (&oldmatches);
    p->matches = newmatches;

#ifdef DEBUG_TIME
    DEBUG_MSG (("AC: %f elapsed refining\n", g_timer_elapsed (timer, NULL)));
    g_timer_destroy (timer);
#endif
    DEBUG_MSG (("AC: %d matches\n", p->matches.num_matches));
}

static void
ephy_autocompletion_update_matches (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
    if (p->status == GAS_UPDATED)
    {
        return;
    }
    if (p->status == GAS_NEEDS_FULL_UPDATE)
    {
        ephy_autocompletion_update_matches_full (ac);
    }
    if (p->status == GAS_NEEDS_REFINE)
    {
        /* FIXME we do full update for now */
        ephy_autocompletion_refine_matches (ac);
    }

    g_free (p->common_prefix);
    p->common_prefix = NULL;
    p->status = GAS_UPDATED;
}

static void
ephy_autocompletion_update_matches_full_item (EphyAutocompletionSource *source,
                          const char *item,
                          const char *title,
                          const char *target,
                          gboolean is_action,
                          gboolean substring,
                          guint32 score,
                          EphyAutocompletionPrivate *p)
{
    if (is_action ||
        (substring && g_strrstr (item, p->keys[0])) ||
        (substring && g_strrstr (title, p->keys[0])))
    {
        EphyAutocompletionMatch m;
        m.match = item;
        m.title = title;
        m.target = target;
        m.is_action = is_action;
        m.substring = substring;
        m.offset = 0;
        m.score = score;
        acma_append (&p->matches, &m, is_action);
    }
    else
    {
        guint i;
        for (i = 0; p->keys[i]; ++i)
        {
            if (!strncmp (item, p->keys[i], p->key_lengths[i]))
            {
                EphyAutocompletionMatch m;
                m.match = item;
                m.title = title;
                m.target = target;
                m.is_action = is_action;
                m.substring = substring;
                m.offset = p->prefix_lengths[i];
                m.score = score;
                acma_append (&p->matches, &m, is_action);
            }
        }
    }
}

static void
ephy_autocompletion_update_matches_full (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
    GSList *li;
#ifdef DEBUG_TIME
    GTimer *timer = g_timer_new ();
#endif

    DEBUG_MSG (("AC: fully updating\n"));
    ephy_autocompletion_reset (ac);

#ifdef DEBUG_TIME
    g_timer_start (timer);
#endif
    for (li = p->sources; li; li = li->next)
    {
        EphyAutocompletionSource *s = EPHY_AUTOCOMPLETION_SOURCE (li->data);
        g_assert (s);
        ephy_autocompletion_source_foreach (s, p->keys[0],
                            (EphyAutocompletionSourceForeachFunc)
                            ephy_autocompletion_update_matches_full_item,
                            p);
    }
#ifdef DEBUG_TIME
    DEBUG_MSG (("AC: %f elapsed fully updating\n", g_timer_elapsed (timer, NULL)));
    g_timer_destroy (timer);
#endif

    p->sorted = FALSE;
    p->changed = TRUE;

    DEBUG_MSG (("AC: %d matches, fully updated\n", p->matches.num_matches));
}

static gint
ephy_autocompletion_compare_scores (EphyAutocompletionMatch *a, EphyAutocompletionMatch *b)
{
    /* higher scores first */
    return b->score - a->score;
}

static gint
ephy_autocompletion_compare_scores_and_alpha (EphyAutocompletionMatch *a, EphyAutocompletionMatch *b)
{
    if (a->score == b->score)
    {
        return strcmp (a->title, b->title);
    }
    else
    {
        /* higher scores first */
        return b->score - a->score;
    }
}

static gboolean
ephy_autocompletion_sort_by_score (EphyAutocompletion *ac)
{
    EphyAutocompletionPrivate *p = ac->priv;
#ifdef DEBUG_TIME
    GTimer *timer;
#endif
    gint (*comparer) (EphyAutocompletionMatch *m1, EphyAutocompletionMatch *m2);

    if (p->sort_alpha)
    {
        comparer = ephy_autocompletion_compare_scores_and_alpha;
    }
    else
    {
        comparer = ephy_autocompletion_compare_scores;
    }

    ephy_autocompletion_update_matches (ac);
    if (p->changed == FALSE) return FALSE;

    DEBUG_MSG (("AC: sorting\n"));
#ifdef DEBUG_TIME
    timer = g_timer_new ();
    g_timer_start (timer);
#endif
    if (p->matches.num_matches > 0)
    {
        qsort (p->matches.array, p->matches.num_matches,
               sizeof (EphyAutocompletionMatch),
               (void *) comparer);
    }

    p->sorted = TRUE;

#ifdef DEBUG_TIME
    DEBUG_MSG (("AC: %f elapsed sorting by score\n", g_timer_elapsed (timer, NULL)));
    g_timer_destroy (timer);
#endif
    return TRUE;
}

static void
ephy_autocompletion_data_changed_cb (EphyAutocompletionSource *s,
                     EphyAutocompletion *ac)
{
    DEBUG_MSG (("AC: data changed, reseting\n"));
    ephy_autocompletion_reset (ac);

    g_signal_emit (ac, EphyAutocompletionSignals[EPHY_AUTOCOMPLETION_SOURCES_CHANGED], 0);
}

void
ephy_autocompletion_set_prefixes (EphyAutocompletion *ac,
                  const gchar **prefixes)
{
    EphyAutocompletionPrivate *p = ac->priv;
    guint nprefixes = 0;
    gchar *oldkey = g_strdup (p->keys[0]);
    guint i;

    /* count prefixes */
    while (prefixes[nprefixes])
    {
        ++nprefixes;
    }

    nprefixes--;

    g_strfreev (p->keys);
    g_free (p->key_lengths);
    g_strfreev (p->prefixes);
    g_free (p->prefix_lengths);

    p->prefixes = g_new0 (gchar *, nprefixes + 2);
    p->prefix_lengths = g_new0 (guint, nprefixes + 2);
    p->keys = g_new0 (gchar *, nprefixes + 2);
    p->key_lengths = g_new0 (guint, nprefixes + 2);

    p->prefixes[0] = g_strdup ("");
    p->prefix_lengths[0] = 0;
    p->keys[0] = oldkey;
    p->key_lengths[0] = strlen (p->keys[0]);

    for (i = 0; i < nprefixes; ++i)
    {
        p->prefixes[i + 1] = g_strdup (prefixes[i]);
        p->prefix_lengths[i + 1] = strlen (prefixes[i]);
        p->keys[i + 1] = g_strconcat (p->prefixes[i + 1], p->keys[0], NULL);
        p->key_lengths[i + 1] = p->prefix_lengths[i + 1] + p->key_lengths[0];
    }

    p->nkeys = nprefixes;
}


/* ACMatchArray */

static void
acma_init (ACMatchArray *a)
{
    a->array = NULL;
    a->array_size = 0;
    a->num_matches = 0;
}

/**
 * Does not free the struct itself, only its contents
 */
static void
acma_destroy (ACMatchArray *a)
{
    g_free (a->array);
    a->array = NULL;
    a->array_size = 0;
    a->num_matches = 0;
    a->num_action_matches = 0;
}

static inline void
acma_append (ACMatchArray *a,
         EphyAutocompletionMatch *m,
         gboolean action)
{
    if (a->array_size == a->num_matches)
    {
        acma_grow (a);
    }

    a->array[a->num_matches] = *m;
    a->num_matches++;
    if (action) a->num_action_matches++;
}

static void
acma_grow (ACMatchArray *a)
{
    gint new_size;
    EphyAutocompletionMatch *new_array;

    new_size = a->array_size + ACMA_BASE_SIZE;
    new_array = g_renew (EphyAutocompletionMatch, a->array, new_size);

    a->array_size = new_size;
    a->array = new_array;
}