terminal-search-provider.c 12.9 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 37 38 39 40 41 42 43 44 45 46
/*
 * Copyright © 2013, 2014 Red Hat, Inc.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include "terminal-app.h"
#include "terminal-debug.h"
#include "terminal-libgsystem.h"
#include "terminal-screen-container.h"
#include "terminal-search-provider.h"
#include "terminal-search-provider-gdbus-generated.h"
#include "terminal-window.h"

struct _TerminalSearchProvider
{
  GObject parent;

  TerminalSearchProvider2 *skeleton;
};

struct _TerminalSearchProviderClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (TerminalSearchProvider, terminal_search_provider, G_TYPE_OBJECT)

static char *
normalize_casefold_and_unaccent (const char *str)
{
47 48
  gs_free char *casefolded = NULL, *normalized = NULL;
  char *retval = NULL;
49 50

  if (str == NULL)
51
    goto out;
52

53 54 55
  normalized = g_utf8_normalize (str, -1, G_NORMALIZE_ALL_COMPOSE);
  casefolded = g_utf8_casefold (normalized, -1);
  retval = g_str_to_ascii (casefolded, NULL);
56

57 58
 out:
  return retval;
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
}

static char **
normalize_casefold_and_unaccent_terms (const char* const *terms)
{
  char **casefolded_terms;
  guint i, n;

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

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

  return casefolded_terms;
}

static gboolean
match_terms (const char        *str,
             const char* const *terms)
{
  gs_free char *casefolded_str = NULL;
  gboolean matches = TRUE;
  guint i;

  if (str == NULL)
    {
      matches = FALSE;
      goto out;
    }

  casefolded_str = normalize_casefold_and_unaccent (str);
  for (i = 0; terms[i] != NULL; i++)
    {
      if (strstr (casefolded_str, terms[i]) == NULL)
        {
          matches = FALSE;
          break;
        }
    }

 out:
  return matches;
}

static gboolean
handle_get_initial_result_set_cb (TerminalSearchProvider2  *skeleton,
                                  GDBusMethodInvocation    *invocation,
                                  const char *const        *terms,
                                  gpointer                  user_data)
{
  GList *l, *screens = NULL, *windows;
  gs_unref_ptrarray GPtrArray *results;
  TerminalApp *app;
  gs_strfreev char **casefolded_terms = NULL;

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetInitialResultSet started\n");

  app = terminal_app_get ();
  windows = gtk_application_get_windows (GTK_APPLICATION (app));
  for (l = windows; l != NULL; l = l->next)
    {
122
      TerminalWindow *window = (TerminalWindow*)(l->data);
123 124
      GList *c, *containers;

125 126 127
      if (!TERMINAL_IS_WINDOW (l->data))
        continue;

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
      containers = terminal_window_list_screen_containers (window);
      for (c = containers; c != NULL; c = c->next)
        {
          TerminalScreenContainer *container = TERMINAL_SCREEN_CONTAINER (c->data);
          TerminalScreen *screen;

          screen = terminal_screen_container_get_screen (container);
          screens = g_list_prepend (screens, screen);
        }
    }

  casefolded_terms = normalize_casefold_and_unaccent_terms (terms);
  results = g_ptr_array_new_with_free_func (g_free);

  for (l = screens; l != NULL; l = l->next)
    {
      TerminalScreen *screen = TERMINAL_SCREEN (l->data);
      gs_free char *cmdline = NULL, *process = NULL;
      const char *cwd, *title;

      cwd = vte_terminal_get_current_directory_uri (VTE_TERMINAL (screen));
      title = terminal_screen_get_title (screen);
      terminal_screen_has_foreground_process (screen, &process, &cmdline);
      if (match_terms (cwd, (const char *const *) casefolded_terms) ||
          match_terms (title, (const char *const *) casefolded_terms) ||
          match_terms (process, (const char *const *) casefolded_terms) ||
          match_terms (cmdline, (const char *const *) casefolded_terms))
        {
          const char *uuid;

          uuid = terminal_screen_get_uuid (screen);
          g_ptr_array_add (results, g_strdup (uuid));

          _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "Search hit: %s\n", uuid);
        }
    }

  g_ptr_array_add (results, NULL);
  terminal_search_provider2_complete_get_initial_result_set (skeleton,
                                                             invocation,
                                                             (const char *const *) results->pdata);

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetInitialResultSet completed\n");
  return TRUE;
}

static gboolean
handle_get_subsearch_result_set_cb (TerminalSearchProvider2  *skeleton,
                                    GDBusMethodInvocation    *invocation,
                                    const char *const        *previous_results,
                                    const char *const        *terms,
                                    gpointer                  user_data)
{
  gs_unref_ptrarray GPtrArray *results;
  TerminalApp *app;
  gs_strfreev char **casefolded_terms = NULL;
  guint i;

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetSubsearchResultSet started\n");

  app = terminal_app_get ();
  casefolded_terms = normalize_casefold_and_unaccent_terms (terms);
  results = g_ptr_array_new_with_free_func (g_free);

  for (i = 0; previous_results[i] != NULL; i++)
    {
      TerminalScreen *screen;
      gs_free char *cmdline = NULL, *process = NULL;
      const char *cwd, *title;

      screen = terminal_app_get_screen_by_uuid (app, previous_results[i]);
      if (screen == NULL)
        {
          _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "Not a screen: %s\n", previous_results[i]);
          continue;
        }

      cwd = vte_terminal_get_current_directory_uri (VTE_TERMINAL (screen));
      title = terminal_screen_get_title (screen);
      terminal_screen_has_foreground_process (screen, &process, &cmdline);
      if (match_terms (cwd, (const char *const *) casefolded_terms) ||
          match_terms (title, (const char *const *) casefolded_terms) ||
          match_terms (process, (const char *const *) casefolded_terms) ||
          match_terms (cmdline, (const char *const *) casefolded_terms))
        {
          g_ptr_array_add (results, g_strdup (previous_results[i]));
          _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "Search hit: %s\n", previous_results[i]);
        }
    }

  g_ptr_array_add (results, NULL);
  terminal_search_provider2_complete_get_subsearch_result_set (skeleton,
                                                               invocation,
                                                               (const char *const *) results->pdata);

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetSubsearchResultSet completed\n");
  return TRUE;
}

static gboolean
handle_get_result_metas_cb (TerminalSearchProvider2  *skeleton,
                            GDBusMethodInvocation    *invocation,
                            const char *const        *results,
                            gpointer                  user_data)
{
  GVariantBuilder builder;
  TerminalApp *app;
  guint i;

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetResultMetas started\n");

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

  for (i = 0; results[i] != NULL; i++)
    {
      TerminalScreen *screen;
      const char *title;
      gs_free char *escaped_text = NULL;
      gs_free char *text = NULL;

      screen = terminal_app_get_screen_by_uuid (app, results[i]);
      if (screen == NULL)
        {
          _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "Not a screen: %s\n", results[i]);
          continue;
        }

      title = terminal_screen_get_title (screen);
257 258 259 260 261 262 263 264 265 266 267 268
      if (terminal_screen_has_foreground_process (screen, NULL, NULL)) {
        VteTerminal *terminal = VTE_TERMINAL (screen);
        long cursor_row;

        vte_terminal_get_cursor_position (terminal, NULL, &cursor_row);
        text = vte_terminal_get_text_range (terminal,
                                            MAX(0, cursor_row - 1),
                                            0,
                                            cursor_row + 1,
                                            vte_terminal_get_column_count (terminal) - 1,
                                            NULL, NULL, NULL);
      }
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

      g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (results[i]));
      g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (title));
      if (text != NULL)
        {
          escaped_text = g_markup_escape_text (text, -1);
          g_variant_builder_add (&builder, "{sv}", "description", g_variant_new_string (escaped_text));
        }
      g_variant_builder_close (&builder);

      _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "Meta for %s: %s\n", results[i], title);
    }

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

  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "GetResultMetas completed\n");

  return TRUE;
}

static gboolean
handle_activate_result_cb (TerminalSearchProvider2  *skeleton,
                           GDBusMethodInvocation    *invocation,
                           const char               *identifier,
                           const char* const        *terms,
                           guint                     timestamp,
                           gpointer                  user_data)
{
  GtkWidget *toplevel;
  TerminalApp *app;
  TerminalScreen *screen;

  app = terminal_app_get ();
  screen = terminal_app_get_screen_by_uuid (app, identifier);
  if (screen == NULL)
    goto out;

  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (screen));
  if (!gtk_widget_is_toplevel (toplevel))
    goto out;

  terminal_window_switch_screen (TERMINAL_WINDOW (toplevel), screen);
  gtk_window_present_with_time (GTK_WINDOW (toplevel), timestamp);
  _terminal_debug_print (TERMINAL_DEBUG_SEARCH, "ActivateResult: %s\n", identifier);

 out:
  terminal_search_provider2_complete_activate_result (skeleton, invocation);
  return TRUE;
}

static void
terminal_search_provider_init (TerminalSearchProvider *provider)
{
  provider->skeleton = terminal_search_provider2_skeleton_new ();

  g_signal_connect (provider->skeleton, "handle-get-initial-result-set",
                    G_CALLBACK (handle_get_initial_result_set_cb), provider);
  g_signal_connect (provider->skeleton, "handle-get-subsearch-result-set",
                    G_CALLBACK (handle_get_subsearch_result_set_cb), provider);
  g_signal_connect (provider->skeleton, "handle-get-result-metas",
                    G_CALLBACK (handle_get_result_metas_cb), provider);
  g_signal_connect (provider->skeleton, "handle-activate-result",
                    G_CALLBACK (handle_activate_result_cb), provider);
}

static void
terminal_search_provider_dispose (GObject *object)
{
  TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (object);

  g_clear_object (&provider->skeleton);

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

static void
terminal_search_provider_class_init (TerminalSearchProviderClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->dispose = terminal_search_provider_dispose;
}

TerminalSearchProvider *
terminal_search_provider_new (void)
{
  return g_object_new (TERMINAL_TYPE_SEARCH_PROVIDER, NULL);
}

gboolean
terminal_search_provider_dbus_register (TerminalSearchProvider  *provider,
                                        GDBusConnection         *connection,
                                        const char              *object_path,
                                        GError                 **error)
{
  return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (provider->skeleton),
                                           connection,
                                           object_path,
                                           error);
}

void
terminal_search_provider_dbus_unregister (TerminalSearchProvider  *provider,
                                          GDBusConnection         *connection,
                                          const char              *object_path)
{
  if (g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (provider->skeleton), connection))
    g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (provider->skeleton),
                                                        connection);
}