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
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* Copyright © 2012 Igalia S.L.
*
* 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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "ephy-completion-model.h"
#include "ephy-embed-prefs.h"
#include "ephy-embed-shell.h"
#include "ephy-history-service.h"
#include "ephy-shell.h"
#include <string.h>
G_DEFINE_TYPE (EphyCompletionModel, ephy_completion_model, GTK_TYPE_LIST_STORE)
#define EPHY_COMPLETION_MODEL_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_COMPLETION_MODEL, EphyCompletionModelPrivate))
struct _EphyCompletionModelPrivate {
EphyHistoryService *history_service;
GCancellable *cancellable;
EphyNode *bookmarks;
GSList *search_terms;
};
static void
ephy_completion_model_constructed (GObject *object)
{
GType types[N_COL] = { G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN,
GDK_TYPE_PIXBUF };
gtk_list_store_set_column_types (GTK_LIST_STORE (object),
N_COL,
types);
}
static void
free_search_terms (GSList *search_terms)
{
GSList *iter;
for (iter = search_terms; iter != NULL; iter = iter->next)
g_regex_unref ((GRegex*)iter->data);
g_slist_free (search_terms);
}
static void
ephy_completion_model_finalize (GObject *object)
{
EphyCompletionModelPrivate *priv = EPHY_COMPLETION_MODEL (object)->priv;
if (priv->search_terms) {
free_search_terms (priv->search_terms);
priv->search_terms = NULL;
}
if (priv->cancellable) {
g_cancellable_cancel (priv->cancellable);
g_clear_object (&priv->cancellable);
}
G_OBJECT_CLASS (ephy_completion_model_parent_class)->finalize (object);
}
static void
ephy_completion_model_class_init (EphyCompletionModelClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->constructed = ephy_completion_model_constructed;
object_class->finalize = ephy_completion_model_finalize;
g_type_class_add_private (object_class, sizeof (EphyCompletionModelPrivate));
}
static void
ephy_completion_model_init (EphyCompletionModel *model)
{
EphyCompletionModelPrivate *priv;
EphyBookmarks *bookmarks_service;
model->priv = priv = EPHY_COMPLETION_MODEL_GET_PRIVATE (model);
priv->history_service = EPHY_HISTORY_SERVICE (ephy_embed_shell_get_global_history_service (embed_shell));
bookmarks_service = ephy_shell_get_bookmarks (ephy_shell);
priv->bookmarks = ephy_bookmarks_get_bookmarks (bookmarks_service);
}
static gboolean
is_base_address (const char *address)
{
if (address == NULL)
return FALSE;
/* A base address is <scheme>://<host>/
* Neither scheme nor host contain a slash, so we can use slashes
* figure out if it's a base address.
*
* Note: previous code was using a GRegExp to do the same thing.
* While regexps are much nicer to read, they're also a lot
* slower.
*/
address = strchr (address, '/');
if (address == NULL ||
address[1] != '/')
return FALSE;
address += 2;
address = strchr (address, '/');
if (address == NULL ||
address[1] != 0)
return FALSE;
return TRUE;
}
static int
get_relevance (const char *location,
int visit_count,
gboolean is_bookmark)
{
/* FIXME: use frecency. */
int relevance = 0;
/* We have three ordered groups: history's base addresses,
bookmarks, deep history addresses. */
if (is_bookmark)
relevance = 1 << 5;
else {
visit_count = MIN (visit_count, (1 << 5) - 1);
if (is_base_address (location))
relevance = visit_count << 10;
else
relevance = visit_count;
}
return relevance;
}
typedef struct {
char *title;
char *location;
char *keywords;
int relevance;
gboolean is_bookmark;
} PotentialRow;
typedef struct {
GtkListStore *model;
GtkTreeRowReference *row_reference;
} IconLoadData;
static void
icon_loaded_cb (GObject *source, GAsyncResult *result, gpointer user_data)
{
GtkTreeIter iter;
GtkTreePath *path;
IconLoadData *data = (IconLoadData *) user_data;
GdkPixbuf *favicon = webkit_favicon_database_get_favicon_pixbuf_finish (webkit_get_favicon_database (), result, NULL);
if (favicon) {
/* The completion model might have changed its contents */
if (gtk_tree_row_reference_valid (data->row_reference)) {
path = gtk_tree_row_reference_get_path (data->row_reference);
gtk_tree_model_get_iter (GTK_TREE_MODEL (data->model), &iter, path);
gtk_list_store_set (data->model, &iter, EPHY_COMPLETION_FAVICON_COL, favicon, -1);
g_object_unref (favicon);
gtk_tree_path_free (path);
}
}
g_object_unref (data->model);
gtk_tree_row_reference_free (data->row_reference);
g_slice_free (IconLoadData, data);
}
static void
set_row_in_model (EphyCompletionModel *model, int position, PotentialRow *row)
{
GtkTreeIter iter;
GdkPixbuf *favicon;
GtkTreePath *path;
IconLoadData *data;
WebKitFaviconDatabase* database = webkit_get_favicon_database ();
gtk_list_store_insert_with_values (GTK_LIST_STORE (model), &iter, position,
EPHY_COMPLETION_TEXT_COL, row->title ? row->title : "",
EPHY_COMPLETION_URL_COL, row->location,
EPHY_COMPLETION_ACTION_COL, row->location,
EPHY_COMPLETION_KEYWORDS_COL, row->keywords ? row->keywords : "",
EPHY_COMPLETION_EXTRA_COL, row->is_bookmark,
EPHY_COMPLETION_RELEVANCE_COL, row->relevance,
-1);
/* We try first with the try_get_favicon_pixbuf() because if the icon
is in the DB it's faster than the async version. */
favicon = webkit_favicon_database_try_get_favicon_pixbuf (database, row->location,
FAVICON_SIZE, FAVICON_SIZE);
if (favicon) {
gtk_list_store_set (GTK_LIST_STORE (model), &iter, EPHY_COMPLETION_FAVICON_COL, favicon, -1);
g_object_unref (favicon);
return;
}
data = g_slice_new (IconLoadData);
data->model = GTK_LIST_STORE (g_object_ref(model));
path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
data->row_reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (model), path);
gtk_tree_path_free (path);
webkit_favicon_database_get_favicon_pixbuf (database, row->location,
FAVICON_SIZE, FAVICON_SIZE, NULL,
icon_loaded_cb, data);
}
static void
replace_rows_in_model (EphyCompletionModel *model, GSList *new_rows)
{
/* This is by far the simplest way of doing, and yet it gives
* basically the same result than the other methods... */
int i;
gtk_list_store_clear (GTK_LIST_STORE (model));
for (i = 0; new_rows != NULL; i++) {
PotentialRow *row = (PotentialRow*)new_rows->data;
set_row_in_model (model, i, row);
new_rows = new_rows->next;
}
}
static gboolean
should_add_bookmark_to_model (EphyCompletionModel *model,
const char *search_string,
const char *title,
const char *location,
const char *keywords)
{
gboolean ret = TRUE;
EphyCompletionModelPrivate *priv = model->priv;
if (priv->search_terms) {
GSList *iter;
GRegex *current = NULL;
for (iter = priv->search_terms; iter != NULL; iter = iter->next) {
current = (GRegex*)iter->data;
if ((!g_regex_match (current, title ? title : "", G_REGEX_MATCH_NOTEMPTY, NULL)) &&
(!g_regex_match (current, location ? location : "", G_REGEX_MATCH_NOTEMPTY, NULL)) &&
(!g_regex_match (current, keywords ? keywords : "", G_REGEX_MATCH_NOTEMPTY, NULL))) {
ret = FALSE;
break;
}
}
}
return ret;
}
typedef struct {
EphyCompletionModel *model;
char *search_string;
EphyHistoryJobCallback callback;
gpointer user_data;
} FindURLsData;
static int
find_url (gconstpointer a,
gconstpointer b)
{
return g_strcmp0 (((PotentialRow*)a)->location,
((char *)b));
}
static PotentialRow *
potential_row_new (const char *title, const char *location,
const char *keywords, int visit_count,
gboolean is_bookmark)
{
PotentialRow *row = g_slice_new0 (PotentialRow);
row->title = g_strdup (title);
row->location = g_strdup (location);
row->keywords = g_strdup (keywords);
row->relevance = get_relevance (location, visit_count, is_bookmark);
row->is_bookmark = is_bookmark;
return row;
}
static void
free_potential_row (PotentialRow *row)
{
g_free (row->title);
g_free (row->location);
g_free (row->keywords);
g_slice_free (PotentialRow, row);
}
static GSList *
add_to_potential_rows (GSList *rows,
const char *title,
const char *location,
const char *keywords,
int visit_count,
gboolean is_bookmark,
gboolean search_for_duplicates)
{
gboolean found = FALSE;
PotentialRow *row = potential_row_new (title, location, keywords, visit_count, is_bookmark);
if (search_for_duplicates) {
GSList *p;
p = g_slist_find_custom (rows, location, find_url);
if (p) {
PotentialRow *match = (PotentialRow*)p->data;
if (row->relevance > match->relevance)
match->relevance = row->relevance;
found = TRUE;
free_potential_row (row);
}
}
if (!found)
rows = g_slist_prepend (rows, row);
return rows;
}
static int
sort_by_relevance (gconstpointer a, gconstpointer b)
{
PotentialRow *r1 = (PotentialRow*)a;
PotentialRow *r2 = (PotentialRow*)b;
if (r1->relevance < r2->relevance)
return 1;
else if (r1->relevance > r2->relevance)
return -1;
else
return 0;
}
static void
query_completed_cb (EphyHistoryService *service,
gboolean success,
gpointer result_data,
FindURLsData *user_data)
{
EphyCompletionModel *model = user_data->model;
EphyCompletionModelPrivate *priv = model->priv;
GList *p, *urls;
GPtrArray *children;
GSList *list = NULL;
int i;
/* Bookmarks */
children = ephy_node_get_children (priv->bookmarks);
/* FIXME: perhaps this could be done in a service thread? There
* should never be a ton of bookmarks, but seems a bit cleaner and
* consistent with what we do for the history. */
for (i = 0; i < children->len; i++) {
EphyNode *kid;
const char *keywords, *location, *title;
kid = g_ptr_array_index (children, i);
location = ephy_node_get_property_string (kid, EPHY_NODE_BMK_PROP_LOCATION);
title = ephy_node_get_property_string (kid, EPHY_NODE_BMK_PROP_TITLE);
keywords = ephy_node_get_property_string (kid, EPHY_NODE_BMK_PROP_KEYWORDS);
if (should_add_bookmark_to_model (model, user_data->search_string,
title, location, keywords))
list = add_to_potential_rows (list, title, location, keywords, 0, TRUE, FALSE);
}
/* History */
urls = (GList*)result_data;
for (p = urls; p != NULL; p = p->next) {
EphyHistoryURL *url = (EphyHistoryURL*)p->data;
list = add_to_potential_rows (list, url->title, url->url, NULL, url->visit_count, FALSE, TRUE);
}
/* Sort the rows by relevance. */
list = g_slist_sort (list, sort_by_relevance);
/* Now that we have all the rows we want to insert, replace the rows
* in the current model one by one, sorted by relevance. */
replace_rows_in_model (model, list);
/* Notify */
if (user_data->callback)
user_data->callback (service, success, result_data, user_data->user_data);
g_free (user_data->search_string);
g_slice_free (FindURLsData, user_data);
g_list_free_full (urls, (GDestroyNotify)ephy_history_url_free);
g_slist_free_full (list, (GDestroyNotify)free_potential_row);
g_clear_object (&priv->cancellable);
}
static void
update_search_terms (EphyCompletionModel *model,
const char *text)
{
const char *current;
const char *ptr;
char *tmp;
char *term;
GRegex *term_regex;
GRegex *quote_regex;
gint count;
gboolean inside_quotes = FALSE;
EphyCompletionModelPrivate *priv = model->priv;
if (priv->search_terms) {
free_search_terms (priv->search_terms);
priv->search_terms = NULL;
}
quote_regex = g_regex_new ("\"", G_REGEX_OPTIMIZE,
G_REGEX_MATCH_NOTEMPTY, NULL);
/*
* This code loops through the string using pointer arythmetics.
* Although the string we are handling may contain UTF-8 chars
* this works because only ASCII chars affect what is actually
* copied from the string as a search term.
*/
for (count = 0, current = ptr = text; ptr[0] != '\0'; ptr++, count++) {
/*
* If we found a double quote character; we will
* consume bytes up until the next quote, or
* end of line;
*/
if (ptr[0] == '"')
inside_quotes = !inside_quotes;
/*
* If we found a space, and we are not looking for a
* closing double quote, or if the next char is the
* end of the string, append what we have already as
* a search term.
*/
if (((ptr[0] == ' ') && (!inside_quotes)) || ptr[1] == '\0') {
/*
* We special-case the end of the line because
* we would otherwise not copy the last character
* of the search string, since the for loop will
* stop before that.
*/
if (ptr[1] == '\0')
count++;
/*
* remove quotes, and quote any regex-sensitive
* characters
*/
tmp = g_regex_escape_string (current, count);
term = g_regex_replace (quote_regex, tmp, -1, 0,
"", G_REGEX_MATCH_NOTEMPTY, NULL);
g_strstrip (term);
g_free (tmp);
/* we don't want empty search terms */
if (term[0] != '\0') {
term_regex = g_regex_new (term,
G_REGEX_CASELESS | G_REGEX_OPTIMIZE,
G_REGEX_MATCH_NOTEMPTY, NULL);
priv->search_terms = g_slist_append (priv->search_terms, term_regex);
}
g_free (term);
/* count will be incremented by the for loop */
count = -1;
current = ptr + 1;
}
}
g_regex_unref (quote_regex);
}
#define MAX_COMPLETION_HISTORY_URLS 8
void
ephy_completion_model_update_for_string (EphyCompletionModel *model,
const char *search_string,
EphyHistoryJobCallback callback,
gpointer data)
{
EphyCompletionModelPrivate *priv;
char **strings;
int i;
GList *query = NULL;
FindURLsData *user_data;
g_return_if_fail (EPHY_IS_COMPLETION_MODEL (model));
g_return_if_fail (search_string != NULL);
priv = model->priv;
/* Split the search string. */
strings = g_strsplit (search_string, " ", -1);
for (i = 0; strings[i]; i++)
query = g_list_append (query, g_strdup (strings[i]));
g_strfreev (strings);
update_search_terms (model, search_string);
user_data = g_slice_new (FindURLsData);
user_data->model = model;
user_data->search_string = g_strdup (search_string);
user_data->callback = callback;
user_data->user_data = data;
if (priv->cancellable) {
g_cancellable_cancel (priv->cancellable);
g_object_unref (priv->cancellable);
}
priv->cancellable = g_cancellable_new ();
ephy_history_service_find_urls (priv->history_service,
0, 0,
MAX_COMPLETION_HISTORY_URLS, 0,
query, priv->cancellable,
(EphyHistoryJobCallback)query_completed_cb,
user_data);
}
EphyCompletionModel *
ephy_completion_model_new (void)
{
return g_object_new (EPHY_TYPE_COMPLETION_MODEL, NULL);
}
|