summaryrefslogtreecommitdiffstats
path: root/search-provider/cc-search-provider.c
blob: 8a8981a568b60dc59e82bcca50afdc6c621bbf8e (plain)
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
/*
 * Copyright (c) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
 *
 * The Control Center 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.
 *
 * The Control Center 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 <glib/gi18n.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include <gtk/gtk.h>
#include <string.h>

#include <shell/cc-panel.h>
#include <shell/cc-shell-model.h>
#include <shell/cc-panel-loader.h>

#include "cc-util.h"

#include "control-center-search-provider.h"
#include "cc-search-provider.h"

struct _CcSearchProvider
{
  GObject parent;

  CcShellSearchProvider2 *skeleton;

  GHashTable *iter_table; /* COL_ID -> GtkTreeIter */
};

typedef enum {
  MATCH_NONE,
  MATCH_PREFIX,
  MATCH_SUBSTRING
} PanelSearchMatch;

G_DEFINE_TYPE (CcSearchProvider, cc_search_provider, G_TYPE_OBJECT)

static char **
get_casefolded_terms (char **terms)
{
  char **casefolded_terms;
  int i, n;

  n = g_strv_length ((char**) terms);
  casefolded_terms = g_new (char*, n + 1);

  for (i = 0; i < n; i++)
    casefolded_terms[i] = cc_util_normalize_casefold_and_unaccent (terms[i]);
  casefolded_terms[n] = NULL;

  return casefolded_terms;
}

static gboolean
matches_all_terms (GtkTreeModel  *model,
                   GtkTreeIter   *iter,
                   char         **terms)
{
  int i;

  for (i = 0; terms[i]; i++)
    {
      if (!cc_shell_model_iter_matches_search (CC_SHELL_MODEL (model),
                                               iter,
                                               terms[i]))
        return FALSE;
    }

  return TRUE;
}

static GtkTreeModel *
get_model (void)
{
  CcSearchProviderApp *app;

  app = cc_search_provider_app_get ();
  return GTK_TREE_MODEL (cc_search_provider_app_get_model (app));
}

static gchar **
get_results (gchar **terms)
{
  g_auto(GStrv) casefolded_terms = NULL;
  GtkTreeModel *model = get_model ();
  GtkTreeIter iter;
  GPtrArray *results;
  gboolean ok;

  casefolded_terms = get_casefolded_terms (terms);
  results = g_ptr_array_new ();

  cc_shell_model_set_sort_terms (CC_SHELL_MODEL (model), casefolded_terms);

  ok = gtk_tree_model_get_iter_first (model, &iter);
  while (ok)
    {
      if (matches_all_terms (model, &iter, casefolded_terms))
        {
          gchar *id;
          gtk_tree_model_get (model, &iter, COL_ID, &id, -1);
          g_ptr_array_add (results, id);
        }

      ok = gtk_tree_model_iter_next (model, &iter);
    }

  g_ptr_array_add (results, NULL);

  return (char**) g_ptr_array_free (results, FALSE);
}

static gboolean
handle_get_initial_result_set (CcShellSearchProvider2  *skeleton,
                               GDBusMethodInvocation   *invocation,
                               char                   **terms,
                               CcSearchProvider        *self)
{
  g_auto(GStrv) results = get_results (terms);
  cc_shell_search_provider2_complete_get_initial_result_set (skeleton,
                                                             invocation,
                                                             (const char* const*) results);
  return TRUE;
}

static gboolean
handle_get_subsearch_result_set (CcShellSearchProvider2  *skeleton,
                                 GDBusMethodInvocation   *invocation,
                                 char                   **previous_results,
                                 char                   **terms,
                                 CcSearchProvider        *self)
{
  /* We ignore the previous results here since the model re-sorts for
   * the new terms. This means that we're not really doing a subsearch
   * but, on the other hand, the results are consistent with the
   * control center's own search. In any case, the number of elements
   * in the model is always small enough that we don't need to worry
   * about this taking too long.
   */
  g_auto(GStrv) results = get_results (terms);
  cc_shell_search_provider2_complete_get_subsearch_result_set (skeleton,
                                                               invocation,
                                                               (const char* const*) results);
  return TRUE;
}

static GtkTreeIter *
get_iter_for_result (CcSearchProvider *self,
                     const gchar      *result)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  gboolean ok;
  gchar *id;

  /* Caching GtkTreeIters in this way is only OK because the model is
   * a GtkListStore which guarantees that while a row exists, the iter
   * is persistent.
   */
  if (self->iter_table)
    goto lookup;

  self->iter_table = g_hash_table_new_full (g_str_hash, g_str_equal,
                                            g_free, (GDestroyNotify) gtk_tree_iter_free);

  model = get_model ();
  ok = gtk_tree_model_get_iter_first (model, &iter);
  while (ok)
    {
      gtk_tree_model_get (model, &iter, COL_ID, &id, -1);

      g_hash_table_replace (self->iter_table, id, gtk_tree_iter_copy (&iter));

      ok = gtk_tree_model_iter_next (model, &iter);
    }

 lookup:
  return g_hash_table_lookup (self->iter_table, result);
}

static gboolean
handle_get_result_metas (CcShellSearchProvider2  *skeleton,
                         GDBusMethodInvocation   *invocation,
                         char                   **results,
                         CcSearchProvider        *self)
{
  GtkTreeModel *model = get_model ();
  GtkTreeIter *iter;
  int i;
  GVariantBuilder builder;
  const char *id;

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

  for (i = 0; results[i]; i++)
    {
      g_autofree gchar *description = NULL;
      g_autofree gchar *name = NULL;
      g_autoptr(GAppInfo) app = NULL;
      g_autoptr(GIcon) icon = NULL;

      iter = get_iter_for_result (self, results[i]);
      if (!iter)
        continue;

      gtk_tree_model_get (model, iter,
                          COL_APP, &app,
                          COL_NAME, &name,
                          COL_GICON, &icon,
                          COL_DESCRIPTION, &description,
                          -1);
      id = g_app_info_get_id (app);

      g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&builder, "{sv}",
                             "id", g_variant_new_string (id));
      g_variant_builder_add (&builder, "{sv}",
                             "name", g_variant_new_string (name));
      g_variant_builder_add (&builder, "{sv}",
                             "icon", g_icon_serialize (icon));
      g_variant_builder_add (&builder, "{sv}",
                             "description", g_variant_new_string (description));
      g_variant_builder_close (&builder);
    }

  cc_shell_search_provider2_complete_get_result_metas (skeleton,
                                                       invocation,
                                                       g_variant_builder_end (&builder));
  return TRUE;
}

static gboolean
handle_activate_result (CcShellSearchProvider2  *skeleton,
                        GDBusMethodInvocation   *invocation,
                        char                    *identifier,
                        char                   **results,
                        guint                    timestamp,
                        CcSearchProvider        *self)
{
  GdkAppLaunchContext *launch_context;
  g_autoptr(GError) error = NULL;
  GAppInfo *app;

  launch_context = gdk_display_get_app_launch_context (gdk_display_get_default ());
  gdk_app_launch_context_set_timestamp (launch_context, timestamp);

  app = G_APP_INFO (g_desktop_app_info_new (identifier));

  if (!g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (launch_context), &error))
    g_dbus_method_invocation_return_gerror (invocation, error);
  else
    cc_shell_search_provider2_complete_activate_result (skeleton, invocation);

  return TRUE;
}

static gboolean
handle_launch_search (CcShellSearchProvider2  *skeleton,
                      GDBusMethodInvocation   *invocation,
                      char                   **terms,
                      guint                    timestamp,
                      CcSearchProvider        *self)
{
  GdkAppLaunchContext *launch_context;
  g_autoptr(GError) error = NULL;
  char *joined_terms, *command_line;
  GAppInfo *app;

  launch_context = gdk_display_get_app_launch_context (gdk_display_get_default ());
  gdk_app_launch_context_set_timestamp (launch_context, timestamp);

  joined_terms = g_strjoinv (" ", terms);
  command_line = g_strdup_printf ("gnome-control-center -s '%s'", joined_terms);
  app = g_app_info_create_from_commandline (command_line,
                                            "gnome-control-center.desktop",
                                            G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION,
                                            &error);
  if (!app)
    {
      g_dbus_method_invocation_return_gerror (invocation, error);
      return TRUE;
    }

  if (!g_app_info_launch (app, NULL, G_APP_LAUNCH_CONTEXT (launch_context), &error))
    g_dbus_method_invocation_return_gerror (invocation, error);
  else
    cc_shell_search_provider2_complete_launch_search (skeleton, invocation);

  return TRUE;
}

static void
cc_search_provider_init (CcSearchProvider *self)
{
  self->skeleton = cc_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);
}

gboolean
cc_search_provider_dbus_register (CcSearchProvider  *self,
                                  GDBusConnection   *connection,
                                  const gchar       *object_path,
                                  GError           **error)
{
  GDBusInterfaceSkeleton *skeleton;

  skeleton = G_DBUS_INTERFACE_SKELETON (self->skeleton);

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

void
cc_search_provider_dbus_unregister (CcSearchProvider *self,
                                    GDBusConnection  *connection,
                                    const gchar      *object_path)
{
  GDBusInterfaceSkeleton *skeleton;

  skeleton = G_DBUS_INTERFACE_SKELETON (self->skeleton);

  if (g_dbus_interface_skeleton_has_connection (skeleton, connection))
      g_dbus_interface_skeleton_unexport_from_connection (skeleton, connection);
}

static void
cc_search_provider_dispose (GObject *object)
{
  CcSearchProvider *self;

  self = CC_SEARCH_PROVIDER (object);

  g_clear_object (&self->skeleton);
  g_clear_pointer (&self->iter_table, g_hash_table_destroy);

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

static void
cc_search_provider_class_init (CcSearchProviderClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = cc_search_provider_dispose;
}

CcSearchProvider *
cc_search_provider_new (void)
{
  return g_object_new (CC_TYPE_SEARCH_PROVIDER, NULL);
}