ephy-search-provider.c 14.4 KB
Newer Older
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
/*
 * Copyright (c) 2013 Giovanni Campagna <scampa.giovanni@gmail.com>
 *
 * 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 the Control Center; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include "ephy-search-provider.h"

#include "ephy-completion-model.h"
#include "ephy-file-helpers.h"
#include "ephy-prefs.h"
#include "ephy-profile-utils.h"

#include <string.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <libsoup/soup.h>

struct _EphySearchProvider
{
37
  GApplication parent;
38
39
40
41
42

  EphyShellSearchProvider2 *skeleton;
  GCancellable             *cancellable;

  GSettings                *settings;
43
44
  EphyHistoryService       *history_service;
  EphyBookmarks            *bookmarks;
45
46
47
48
49
  EphyCompletionModel      *model;
};

struct _EphySearchProviderClass
{
50
  GApplicationClass parent_class;
51
52
};

53
G_DEFINE_TYPE (EphySearchProvider, ephy_search_provider, G_TYPE_APPLICATION)
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

static void
on_model_updated (EphyHistoryService *service,
		  gboolean            success,
		  gpointer            result_data,
		  gpointer            user_data)
{
  GTask *task = user_data;
  EphySearchProvider *self = g_task_get_source_object (task);
  GtkTreeModel *model = GTK_TREE_MODEL (self->model);
  GtkTreeIter iter;
  GPtrArray *results;
  const char *search_string;
  gboolean ok;

  results = g_ptr_array_new ();

  ok = gtk_tree_model_get_iter_first (model, &iter);
  while (ok) {
    char *result;

    result = gtk_tree_model_get_string_from_iter (model, &iter);
    g_ptr_array_add (results, result);

    ok = gtk_tree_model_iter_next (model, &iter);
  }

  search_string = g_task_get_task_data (task);
  g_ptr_array_add (results, g_strdup_printf ("special:search:%s", search_string));
  g_ptr_array_add (results, NULL);

  g_task_return_pointer (task,
                         g_ptr_array_free (results, FALSE),
                         (GDestroyNotify) g_strfreev);
}

static char **
gather_results_finish (EphySearchProvider *self,
                       GAsyncResult       *result,
                       GError            **error)
{
  return g_task_propagate_pointer (G_TASK (result), error);
}

static void
gather_results_async (EphySearchProvider   *self,
                      char                **terms,
                      GCancellable         *cancellable,
                      GAsyncReadyCallback   callback,
                      gpointer              user_data)
{
  GTask *task;
  char *search_string;

  task = g_task_new (self, cancellable, callback, user_data);
  g_task_set_check_cancellable (task, TRUE);

  search_string = g_strjoinv (" ", terms);
  g_task_set_task_data (task, search_string, g_free);

  ephy_completion_model_update_for_string (self->model,
                                           search_string,
                                           on_model_updated,
                                           task);
}

static void
complete_request (GObject      *object,
                  GAsyncResult *result,
                  gpointer      user_data)
{
  EphySearchProvider *self = EPHY_SEARCH_PROVIDER (object);
  char **results;
  GError *error;

  error = NULL;
  results = gather_results_finish (self, result, &error);

  if (results) {
    g_dbus_method_invocation_return_value (user_data,
                                           g_variant_new ("(^as)", results));
  } else {
    g_dbus_method_invocation_take_error (user_data, error);
  }

139
  g_application_release (G_APPLICATION (self));
140
141
142
143
144
145
146
147
}

static gboolean
handle_get_initial_result_set (EphyShellSearchProvider2  *skeleton,
                               GDBusMethodInvocation     *invocation,
                               char                     **terms,
                               EphySearchProvider        *self)
{
148
  g_application_hold (G_APPLICATION (self));
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  g_cancellable_reset (self->cancellable);

  gather_results_async (self, terms, self->cancellable,
                        complete_request, invocation);

  return TRUE;
}

static gboolean
handle_get_subsearch_result_set (EphyShellSearchProvider2  *skeleton,
                                 GDBusMethodInvocation     *invocation,
                                 char                     **previous_results,
                                 char                     **terms,
                                 EphySearchProvider        *self)
{
164
  g_application_hold (G_APPLICATION (self));
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  g_cancellable_reset (self->cancellable);

  gather_results_async (self, terms, self->cancellable,
                        complete_request, invocation);

  return TRUE;
}

static gboolean
handle_get_result_metas (EphyShellSearchProvider2  *skeleton,
                         GDBusMethodInvocation     *invocation,
                         char                     **results,
                         EphySearchProvider        *self)
{
  GtkTreeModel *model = GTK_TREE_MODEL (self->model);
  GtkTreeIter iter;
  int i;
  GVariantBuilder builder;
  GIcon *favicon;
  char *name, *url;
  gboolean is_bookmark;

187
  g_application_hold (G_APPLICATION (self));
188
189
190
191
192
193
  g_cancellable_cancel (self->cancellable);

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));

  for (i = 0; results[i]; i++)
    {
194
      if (g_str_has_prefix (results[i], "special:search:")) {
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
        g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
        g_variant_builder_add (&builder, "{sv}",
                               "id", g_variant_new_string ("special:search"));
        g_variant_builder_add (&builder, "{sv}",
                               "name", g_variant_new_take_string (g_strdup_printf(_("Search the Web for %s"),
										  results[i] + strlen("special:search:"))));
        g_variant_builder_add (&builder, "{sv}",
                               "gicon", g_variant_new_string ("web-browser"));
        g_variant_builder_close (&builder);
        continue;
      }

      if (!gtk_tree_model_get_iter_from_string (model, &iter, results[i]))
        continue;

      gtk_tree_model_get (model, &iter,
                          EPHY_COMPLETION_TEXT_COL, &name,
                          EPHY_COMPLETION_URL_COL, &url,
                          EPHY_COMPLETION_FAVICON_COL, &favicon,
                          EPHY_COMPLETION_EXTRA_COL, &is_bookmark,
                          -1);

      g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&builder, "{sv}",
                             "id", g_variant_new_string (url));
      g_variant_builder_add (&builder, "{sv}",
                             "name", g_variant_new_string (name));

      if (favicon == NULL) {
	char *type;

        type = g_content_type_from_mime_type ("text/html");
        favicon = g_content_type_get_icon (type);

        if (is_bookmark) {
          GEmblem *emblem;
          GIcon *emblem_icon, *emblemed;

          emblem_icon = g_themed_icon_new ("emblem-favorite");
          emblem = g_emblem_new (emblem_icon);

          emblemed = g_emblemed_icon_new (favicon, emblem);

	  g_object_unref (emblem);
	  g_object_unref (emblem_icon);
	  g_object_unref (favicon);
	  favicon = emblemed;
	}
      }

      g_variant_builder_add (&builder, "{sv}",
			     "icon", g_icon_serialize (favicon));
      g_variant_builder_close (&builder);

      g_object_unref (favicon);
      g_free (name);
      g_free (url);
    }

  ephy_shell_search_provider2_complete_get_result_metas (skeleton,
                                                         invocation,
                                                         g_variant_builder_end (&builder));

258
  g_application_release (G_APPLICATION (self));
259
260
261
262
263
264
265
266

  return TRUE;
}

static void
launch_uri (const char  *uri,
            guint        timestamp)
{
267
  char *str;
268

269
270
271
272
  /* TODO: Handle the timestamp */
  str = g_strdup_printf ("epiphany %s", uri);
  g_spawn_command_line_async (str, NULL);
  g_free (str);
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
}

static void
launch_search (EphySearchProvider  *self,
               char               **terms,
               guint                timestamp)
{
  char *search_string, *url_search, *query_param, *effective_url;

  url_search = g_settings_get_string (self->settings, EPHY_PREFS_KEYWORD_SEARCH_URL);

  if (url_search == NULL || url_search[0] == '\0') {
    g_free (url_search);

    url_search = g_strdup (_("http://duckduckgo.com/?q=%s&amp;t=epiphany"));
  }

  search_string = g_strjoinv (" ", terms);
  query_param = soup_form_encode ("q", search_string, NULL);
  /* + 2 here is getting rid of 'q=' */
  effective_url = g_strdup_printf (url_search, query_param + 2);

  launch_uri (effective_url, timestamp);

  g_free (query_param);
  g_free (url_search);
  g_free (effective_url);
  g_free (search_string);
}

static gboolean
handle_activate_result (EphyShellSearchProvider2  *skeleton,
                        GDBusMethodInvocation     *invocation,
                        char                      *identifier,
                        char                     **terms,
                        guint                      timestamp,
                        EphySearchProvider        *self)
{
311
  g_application_hold (G_APPLICATION (self));
312
313
314
315
316
317
318
319
  g_cancellable_cancel (self->cancellable);

  if (strcmp (identifier, "special:search") == 0)
    launch_search (self, terms, timestamp);
  else
    launch_uri (identifier, timestamp);

  ephy_shell_search_provider2_complete_activate_result (skeleton, invocation);
320
  g_application_release (G_APPLICATION (self));
321
322
323
324
325
326
327
328
329
330
331

  return TRUE;
}

static gboolean
handle_launch_search (EphyShellSearchProvider2  *skeleton,
                      GDBusMethodInvocation     *invocation,
                      char                     **terms,
                      guint                      timestamp,
                      EphySearchProvider        *self)
{
332
  g_application_hold (G_APPLICATION (self));
333
334
335
336
337
  g_cancellable_cancel (self->cancellable);

  launch_search (self, terms, timestamp);

  ephy_shell_search_provider2_complete_launch_search (skeleton, invocation);
338
  g_application_release (G_APPLICATION (self));
339
340
341
342
343
344
345

  return TRUE;
}

static void
ephy_search_provider_init (EphySearchProvider *self)
{
346
347
348
349
  char *filename;

  g_application_set_flags (G_APPLICATION (self), G_APPLICATION_IS_SERVICE);

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  self->skeleton = ephy_shell_search_provider2_skeleton_new ();

  g_signal_connect (self->skeleton, "handle-get-initial-result-set",
                    G_CALLBACK (handle_get_initial_result_set), self);
  g_signal_connect (self->skeleton, "handle-get-subsearch-result-set",
                    G_CALLBACK (handle_get_subsearch_result_set), self);
  g_signal_connect (self->skeleton, "handle-get-result-metas",
                    G_CALLBACK (handle_get_result_metas), self);
  g_signal_connect (self->skeleton, "handle-activate-result",
                    G_CALLBACK (handle_activate_result), self);
  g_signal_connect (self->skeleton, "handle-launch-search",
                    G_CALLBACK (handle_launch_search), self);

  self->settings = g_settings_new (EPHY_PREFS_SCHEMA);

365
366
367
368
369
370
  filename = g_build_filename (ephy_dot_dir (), EPHY_HISTORY_FILE, NULL);
  self->history_service = ephy_history_service_new (filename, TRUE);
  self->bookmarks = ephy_bookmarks_new ();
  self->model = ephy_completion_model_new (self->history_service, self->bookmarks);
  g_free (filename);

371
372
373
  self->cancellable = g_cancellable_new ();
}

374
375
376
377
378
static gboolean
ephy_search_provider_dbus_register (GApplication    *application,
                                    GDBusConnection *connection,
                                    const gchar     *object_path,
                                    GError         **error)
379
{
380
  EphySearchProvider *self;
381
382
  GDBusInterfaceSkeleton *skeleton;

383
384
385
386
387
388
389
  if (!G_APPLICATION_CLASS (ephy_search_provider_parent_class)->dbus_register (application,
                                                                               connection,
                                                                               object_path,
                                                                               error))
    return FALSE;

  self = EPHY_SEARCH_PROVIDER (application);
390
391
392
393
394
  skeleton = G_DBUS_INTERFACE_SKELETON (self->skeleton);

  return g_dbus_interface_skeleton_export (skeleton, connection, object_path, error);
}

395
396
397
398
static void
ephy_search_provider_dbus_unregister (GApplication    *application,
                                      GDBusConnection *connection,
                                      const gchar     *object_path)
399
{
400
  EphySearchProvider *self;
401
402
  GDBusInterfaceSkeleton *skeleton;

403
  self = EPHY_SEARCH_PROVIDER (application);
404
405
  skeleton = G_DBUS_INTERFACE_SKELETON (self->skeleton);
  if (g_dbus_interface_skeleton_has_connection (skeleton, connection))
406
407
408
409
410
411
412
    g_dbus_interface_skeleton_unexport_from_connection (skeleton, connection);

  g_clear_object (&self->skeleton);

  G_APPLICATION_CLASS (ephy_search_provider_parent_class)->dbus_unregister (application,
                                                                            connection,
                                                                            object_path);
413
414
415
416
417
418
419
420
421
422
423
424
}

static void
ephy_search_provider_dispose (GObject *object)
{
  EphySearchProvider *self;

  self = EPHY_SEARCH_PROVIDER (object);

  g_clear_object (&self->settings);
  g_clear_object (&self->cancellable);
  g_clear_object (&self->model);
425
426
  g_clear_object (&self->history_service);
  g_clear_object (&self->bookmarks);
427
428
429
430
431
432
433
434

  G_OBJECT_CLASS (ephy_search_provider_parent_class)->dispose (object);
}

static void
ephy_search_provider_class_init (EphySearchProviderClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
435
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
436
437

  object_class->dispose = ephy_search_provider_dispose;
438
439
440

  application_class->dbus_register = ephy_search_provider_dbus_register;
  application_class->dbus_unregister = ephy_search_provider_dbus_unregister;
441
442
443
444
445
}

EphySearchProvider *
ephy_search_provider_new (void)
{
446
447
448
  return g_object_new (EPHY_TYPE_SEARCH_PROVIDER,
                       "application-id", "org.gnome.EpiphanySearchProvider",
                       NULL);
449
450
}