terminal-nautilus.c 22 KB
Newer Older
1 2 3
/*
 *  Copyright (C) 2004, 2005 Free Software Foundation, Inc.
 *  Copyright © 2011 Christian Persch
4
 *  Author: Christian Neumair <chris@gnome-de.org>
5
 *
6 7 8 9
 * 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.
10
 *
11 12 13 14
 * 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.
15
 *
16 17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25 26
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <gtk/gtk.h>

27
#include <nautilus-extension.h>
28

29 30
#include <errno.h>
#include <fcntl.h>
31 32 33
#include <stdlib.h>
#include <string.h>

34
#include "terminal-i18n.h"
35 36 37
#include "terminal-client-utils.h"
#include "terminal-defines.h"
#include "terminal-gdbus-generated.h"
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
/* Nautilus extension class */

#define TERMINAL_TYPE_NAUTILUS         (terminal_nautilus_get_type ())
#define TERMINAL_NAUTILUS(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), TERMINAL_TYPE_NAUTILUS, TerminalNautilus))
#define TERMINAL_NAUTILUS_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), TERMINAL_TYPE_NAUTILUS, TerminalNautilusClass))
#define TERMINAL_IS_NAUTILUS(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), TERMINAL_TYPE_NAUTILUS))
#define TERMINAL_IS_NAUTILUS_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), TERMINAL_TYPE_NAUTILUS))
#define TERMINAL_NAUTILUS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TERMINAL_TYPE_NAUTILUS, TerminalNautilusClass))

typedef struct _TerminalNautilus      TerminalNautilus;
typedef struct _TerminalNautilusClass TerminalNautilusClass;

struct _TerminalNautilus {
        GObject parent_instance;

        GSettings *lockdown_prefs;
};

struct _TerminalNautilusClass {
        GObjectClass parent_class;
};

static GType terminal_nautilus_get_type (void);

/* Nautilus menu item class */

#define TERMINAL_TYPE_NAUTILUS_MENU_ITEM        (terminal_nautilus_menu_item_get_type ())
#define TERMINAL_NAUTILUS_MENU_ITEM(o)          (G_TYPE_CHECK_INSTANCE_CAST ((o), TERMINAL_TYPE_NAUTILUS_MENU_ITEM, TerminalNautilusMenuItem))
#define TERMINAL_NAUTILUS_MENU_ITEM_CLASS(k)    (G_TYPE_CHECK_CLASS_CAST((k), TERMINAL_TYPE_NAUTILUS_MENU_ITEM, TerminalNautilusMenuItemClass))
#define TERMINAL_IS_NAUTILUS_MENU_ITEM(o)       (G_TYPE_CHECK_INSTANCE_TYPE ((o), TERMINAL_TYPE_NAUTILUS_MENU_ITEM))
#define TERMINAL_IS_NAUTILUS_MENU_ITEM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), TERMINAL_TYPE_NAUTILUS_MENU_ITEM))
#define TERMINAL_NAUTILUS_MENU_ITEM_GET_CLASS(o)(G_TYPE_INSTANCE_GET_CLASS ((o), TERMINAL_TYPE_NAUTILUS_MENU_ITEM, TerminalNautilusMenuItemClass))

typedef struct _TerminalNautilusMenuItem      TerminalNautilusMenuItem;
typedef struct _TerminalNautilusMenuItemClass TerminalNautilusMenuItemClass;

struct _TerminalNautilusMenuItem {
76
  NautilusMenuItem parent_instance;
77 78 79 80 81 82 83

  TerminalNautilus *nautilus;
  NautilusFileInfo *file_info;
  gboolean remote_terminal;
};

struct _TerminalNautilusMenuItemClass {
84
  NautilusMenuItemClass parent_class;
85 86 87 88 89
};

static GType terminal_nautilus_menu_item_get_type (void);

/* --- */
90

Jakub Steiner's avatar
Jakub Steiner committed
91
#define TERMINAL_ICON_NAME "org.gnome.Terminal"
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
typedef enum {
  /* local files. Always open "conventionally", i.e. cd and spawn. */
  FILE_INFO_LOCAL,
  FILE_INFO_DESKTOP,
  /* SFTP: Shell terminals are opened "remote" (i.e. with ssh client),
   * commands are executed like OTHER.
   */
  FILE_INFO_SFTP,
  /* OTHER: Terminals and commands are opened by mapping the URI back
   * to ~/.gvfs, i.e. to the GVFS FUSE bridge.
   */
  FILE_INFO_OTHER
} TerminalFileInfo;

static TerminalFileInfo
get_terminal_file_info_from_uri (const char *uri)
{
  TerminalFileInfo ret;
  char *uri_scheme;

  uri_scheme = g_uri_parse_scheme (uri);

  if (uri_scheme == NULL) {
    ret = FILE_INFO_OTHER;
  } else if (strcmp (uri_scheme, "file") == 0) {
    ret = FILE_INFO_LOCAL;
  } else if (strcmp (uri_scheme, "x-nautilus-desktop") == 0) {
    ret = FILE_INFO_DESKTOP;
  } else if (strcmp (uri_scheme, "sftp") == 0 ||
             strcmp (uri_scheme, "ssh") == 0) {
    ret = FILE_INFO_SFTP;
  } else {
    ret = FILE_INFO_OTHER;
  }

  g_free (uri_scheme);

  return ret;
}

133 134 135 136
/* Helpers */

#define NAUTILUS_SETTINGS_SCHEMA                "org.gnome.Nautilus"
#define GNOME_DESKTOP_LOCKDOWN_SETTINGS_SCHEMA  "org.gnome.desktop.lockdown"
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

/* a very simple URI parsing routine from Launchpad #333462, until GLib supports URI parsing (GNOME #489862) */
#define SFTP_PREFIX "sftp://"
static void
parse_sftp_uri (GFile *file,
                char **user,
                char **host,
                unsigned int *port,
                char **path)
{
  char *tmp, *save;
  char *uri;

  uri = g_file_get_uri (file);
  g_assert (uri != NULL);
  save = uri;

  *path = NULL;
  *user = NULL;
  *host = NULL;
  *port = 0;

  /* skip intial 'sftp:// prefix */
  g_assert (!strncmp (uri, SFTP_PREFIX, strlen (SFTP_PREFIX)));
  uri += strlen (SFTP_PREFIX);

  /* cut out the path */
  tmp = strchr (uri, '/');
  if (tmp != NULL) {
    *path = g_uri_unescape_string (tmp, "/");
    *tmp = '\0';
  }

  /* read the username - it ends with @ */
  tmp = strchr (uri, '@');
  if (tmp != NULL) {
    *tmp++ = '\0';

    *user = strdup (uri);
    if (strchr (*user, ':') != NULL) {
      /* chop the password */
      *(strchr (*user, ':')) = '\0'; 
    }

    uri = tmp;
  }

  /* now read the port, starts with : */
  tmp = strchr (uri, ':');
  if (tmp != NULL) {
    *tmp++ = '\0';
    *port = atoi (tmp);  /*FIXME: getservbyname*/
  }

  /* what is left is the host */
  *host = strdup (uri);
  g_free (save);
}

196 197 198
static char **
ssh_argv (const char *uri,
          int *argcp)
199 200
{
  GFile *file;
201 202
  char **argv;
  int argc;
203
  char *host_name, *path, *user_name, *quoted_path;
204 205 206 207
  guint host_port;

  g_assert (uri != NULL);

208
  argv = g_new0 (char *, 9);
209
  argc = 0;
210
  argv[argc++] = g_strdup ("ssh");
211
  argv[argc++] = g_strdup ("-t");
212

213 214 215 216
  file = g_file_new_for_uri (uri);
  parse_sftp_uri (file, &user_name, &host_name, &host_port, &path);
  g_object_unref (file);

217 218 219 220
  if (user_name != NULL) {
    argv[argc++ ]= g_strdup_printf ("%s@%s", user_name, host_name);
    g_free (host_name);
    g_free (user_name);
221
  } else {
222
    argv[argc++] = host_name;
223 224
  }

225 226
  if (host_port != 0) {
    argv[argc++] = g_strdup ("-p");
227
    argv[argc++] = g_strdup_printf ("%u", host_port);
228 229
  }

230
  /* FIXME to we have to consider the remote file encoding? */
231
  quoted_path = g_shell_quote (path);
232

233
  /* login shell */
234
  argv[argc++] = g_strdup_printf ("cd %s && exec $SHELL -l", quoted_path);
235 236 237 238

  g_free (path);
  g_free (quoted_path);

239
  *argcp = argc;
240 241 242
  return argv;
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
static gboolean
terminal_locked_down (TerminalNautilus *nautilus)
{
  return g_settings_get_boolean (nautilus->lockdown_prefs,
                                 "disable-command-line");
}

/* used to determine for remote URIs whether GVFS is capable of mapping them to ~/.gvfs */
static gboolean
uri_has_local_path (const char *uri)
{
  GFile *file;
  char *path;
  gboolean ret;

  file = g_file_new_for_uri (uri);
  path = g_file_get_path (file);

  ret = (path != NULL);

  g_free (path);
  g_object_unref (file);

  return ret;
267 268
}

269
/* Nautilus menu item class */
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
typedef struct {
  TerminalNautilus *nautilus;
  guint32 timestamp;
  char *path;
  char *uri;
  TerminalFileInfo info;
  gboolean remote;
} ExecData;

static void
exec_data_free (ExecData *data)
{
  g_object_unref (data->nautilus);
  g_free (data->path);
  g_free (data->uri);

  g_free (data);
}

/* FIXME: make this async */
static gboolean
create_terminal (ExecData *data /* transfer full */)
{
  TerminalFactory *factory;
  TerminalReceiver *receiver;
  GError *error = NULL;
  GVariantBuilder builder;
  char *object_path;
  char startup_id[32];
  char **argv;
  int argc;

  factory = terminal_factory_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                     G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                                     G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                                     TERMINAL_APPLICATION_ID,
                                                     TERMINAL_FACTORY_OBJECT_PATH,
                                                     NULL /* cancellable */,
                                                     &error);
  if (factory == NULL) {
    g_dbus_error_strip_remote_error (error);
    g_printerr ("Error constructing proxy for %s:%s: %s\n",
                TERMINAL_APPLICATION_ID, TERMINAL_FACTORY_OBJECT_PATH,
                error->message);
    g_error_free (error);
    exec_data_free (data);
    return FALSE;
  }

  g_snprintf (startup_id, sizeof (startup_id), "_TIME%u", data->timestamp);

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

  terminal_client_append_create_instance_options (&builder,
325
                                                  gdk_display_get_name (gdk_display_get_default ()),
326 327 328 329
                                                  startup_id,
                                                  NULL /* geometry */,
                                                  NULL /* role */,
                                                  NULL /* use default profile */,
330
                                                  NULL /* use profile encoding */,
331
                                                  NULL /* title */,
332
                                                  TRUE, /* active */
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
                                                  FALSE /* maximised */,
                                                  FALSE /* fullscreen */);

  if (!terminal_factory_call_create_instance_sync
         (factory,
          g_variant_builder_end (&builder),
          &object_path,
          NULL /* cancellable */,
          &error)) {
    g_dbus_error_strip_remote_error (error);
    g_printerr ("Error creating terminal: %s\n", error->message);
    g_error_free (error);
    g_object_unref (factory);
    exec_data_free (data);
    return FALSE;
  }

  g_object_unref (factory);

  receiver = terminal_receiver_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                       G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
                                                       G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
                                                       TERMINAL_APPLICATION_ID,
                                                       object_path,
                                                       NULL /* cancellable */,
                                                       &error);
  if (receiver == NULL) {
    g_dbus_error_strip_remote_error (error);
    g_printerr ("Failed to create proxy for terminal: %s\n", error->message);
    g_error_free (error);
    g_free (object_path);
    exec_data_free (data);
    return FALSE;
  }

  g_free (object_path);

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

  terminal_client_append_exec_options (&builder,
                                       data->path,
374
                                       NULL, 0, /* FD array */
375 376 377 378
                                       TRUE /* shell */);

  if (data->info == FILE_INFO_SFTP &&
      data->remote) {
379
    argv = ssh_argv (data->uri, &argc);
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
  } else {
    argv = NULL; argc = 0;
  }

  if (!terminal_receiver_call_exec_sync (receiver,
                                         g_variant_builder_end (&builder),
                                         g_variant_new_bytestring_array ((const char * const *) argv, argc),
                                         NULL /* in FD list */,
                                         NULL /* out FD list */,
                                         NULL /* cancellable */,
                                         &error)) {
    g_dbus_error_strip_remote_error (error);
    g_printerr ("Error: %s\n", error->message);
    g_error_free (error);
    g_strfreev (argv);
    g_object_unref (receiver);
    exec_data_free (data);
    return FALSE;
  }

  g_strfreev (argv);

  exec_data_free (data);

  g_object_unref (receiver);

  return TRUE;
}

409 410
static void
terminal_nautilus_menu_item_activate (NautilusMenuItem *item)
411
{
412 413
  TerminalNautilusMenuItem *menu_item = TERMINAL_NAUTILUS_MENU_ITEM (item);
  TerminalNautilus *nautilus = menu_item->nautilus;
414 415
  char *uri, *path;
  TerminalFileInfo info;
416
  ExecData *data;
417

418 419 420
  uri = nautilus_file_info_get_activation_uri (menu_item->file_info);
  if (uri == NULL)
    return;
421 422

  path = NULL;
423
  info = get_terminal_file_info_from_uri (uri);
424

425
  switch (info) {
426
    case FILE_INFO_LOCAL:
427
      path = g_filename_from_uri (uri, NULL, NULL);
428 429 430
      break;

    case FILE_INFO_DESKTOP:
431
      path = g_strdup (g_get_home_dir ());
432 433 434
      break;

    case FILE_INFO_SFTP:
435
      if (menu_item->remote_terminal)
436
        break;
437

438 439
      /* fall through */

440 441 442 443 444 445 446
    case FILE_INFO_OTHER: {
      GFile *file;

      /* map back remote URI to local path */
      file = g_file_new_for_uri (uri);
      path = g_file_get_path (file);
      g_object_unref (file);
447
      break;
448
    }
449 450 451 452 453

    default:
      g_assert_not_reached ();
  }

454 455 456 457 458
  if (path == NULL && (info != FILE_INFO_SFTP || !menu_item->remote_terminal)) {
    g_free (uri);
    return;
  }

459 460 461 462 463 464 465 466 467
  data = g_new (ExecData, 1);
  data->nautilus = g_object_ref (nautilus);
  data->timestamp = gtk_get_current_event_time ();
  data->path = path;
  data->uri = uri;
  data->info = info;
  data->remote = menu_item->remote_terminal;

  create_terminal (data);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
}

G_DEFINE_DYNAMIC_TYPE (TerminalNautilusMenuItem, terminal_nautilus_menu_item, NAUTILUS_TYPE_MENU_ITEM)

static void 
terminal_nautilus_menu_item_init (TerminalNautilusMenuItem *nautilus_menu_item)
{
}

static void
terminal_nautilus_menu_item_dispose (GObject *object)
{
  TerminalNautilusMenuItem *menu_item = TERMINAL_NAUTILUS_MENU_ITEM (object);

  if (menu_item->file_info != NULL) {
    g_object_unref (menu_item->file_info);
    menu_item->file_info = NULL;
  }
486 487 488 489
  if (menu_item->nautilus != NULL) {
    g_object_unref (menu_item->nautilus);
    menu_item->nautilus = NULL;
  }
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

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

static void
terminal_nautilus_menu_item_class_init (TerminalNautilusMenuItemClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  NautilusMenuItemClass *menu_item_class = NAUTILUS_MENU_ITEM_CLASS (klass);

  gobject_class->dispose = terminal_nautilus_menu_item_dispose;

  menu_item_class->activate = terminal_nautilus_menu_item_activate;
}

static void
terminal_nautilus_menu_item_class_finalize (TerminalNautilusMenuItemClass *class)
{
}

static NautilusMenuItem *
terminal_nautilus_menu_item_new (TerminalNautilus *nautilus,
                                 NautilusFileInfo *file_info,
513 514 515
                                 TerminalFileInfo  terminal_file_info,
                                 gboolean          remote_terminal,
                                 gboolean          is_file_item)
516 517
{
  TerminalNautilusMenuItem *item;
518 519 520 521
  const char *action_name;
  const char *name;
  const char *tooltip;

522 523 524 525 526 527 528
  if (is_file_item) {
    action_name = remote_terminal ? "TerminalNautilus:OpenRemote"
                                  : "TerminalNautilus:OpenLocal";
  } else {
    action_name = remote_terminal ? "TerminalNautilus:OpenFolderRemote"
                                  : "TerminalNautilus:OpenFolderLocal";
  }
529

530 531 532 533 534 535 536
  switch (terminal_file_info) {
    case FILE_INFO_SFTP:
      if (remote_terminal) {
        name = _("Open in _Remote Terminal");
      } else {
        name = _("Open in _Local Terminal");
      }
537

538 539 540 541 542 543
      if (is_file_item) {
        tooltip = _("Open the currently selected folder in a terminal");
      } else {
        tooltip = _("Open the currently open folder in a terminal");
      }
      break;
544

545 546 547
    case FILE_INFO_LOCAL:
    case FILE_INFO_OTHER:
      name = _("Open in T_erminal");
548

549 550 551 552 553 554
      if (is_file_item) {
        tooltip = _("Open the currently selected folder in a terminal");
      } else {
        tooltip = _("Open the currently open folder in a terminal");
      }
      break;
555

556 557 558 559
    case FILE_INFO_DESKTOP:
      name = _("Open T_erminal");
      tooltip = _("Open a terminal");
      break;
560

561 562
    default:
      g_assert_not_reached ();
563
  }
564 565

  item = g_object_new (TERMINAL_TYPE_NAUTILUS_MENU_ITEM,
566 567 568 569
                       "name", action_name,
                       "label", name,
                       "tip", tooltip,
                       "icon", TERMINAL_ICON_NAME,
570 571 572 573 574
                       NULL);

  item->nautilus = g_object_ref (nautilus);
  item->file_info = g_object_ref (file_info);
  item->remote_terminal = remote_terminal;
575

576 577 578 579 580
  return (NautilusMenuItem *) item;
}

/* Nautilus extension class implementation */

581 582
static GList *
terminal_nautilus_get_background_items (NautilusMenuProvider *provider,
583 584
                                        GtkWidget            *window,
                                        NautilusFileInfo     *file_info)
585
{
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  TerminalNautilus *nautilus = TERMINAL_NAUTILUS (provider);
  gchar *uri;
  GList *items;
  NautilusMenuItem *item;
  TerminalFileInfo terminal_file_info;

  if (terminal_locked_down (nautilus))
    return NULL;

  uri = nautilus_file_info_get_activation_uri (file_info);
  if (uri == NULL)
    return NULL;

  items = NULL;

  terminal_file_info = get_terminal_file_info_from_uri (uri);

603 604 605

  if (terminal_file_info == FILE_INFO_SFTP) {
    /* remote SSH location */
606 607
    item = terminal_nautilus_menu_item_new (nautilus,
                                            file_info, 
608
                                            terminal_file_info,
609
                                            TRUE,
610 611 612 613
                                            FALSE);
    items = g_list_append (items, item);
  }

614
  if (terminal_file_info == FILE_INFO_DESKTOP ||
615
      uri_has_local_path (uri)) {
616
    /* local locations and remote locations that offer local back-mapping */
617 618
    item = terminal_nautilus_menu_item_new (nautilus,
                                            file_info, 
619
                                            terminal_file_info,
620 621 622 623 624 625
                                            FALSE, 
                                            FALSE);
    items = g_list_append (items, item);
  }

  g_free (uri);
626

627
  return items;
628 629 630 631
}

static GList *
terminal_nautilus_get_file_items (NautilusMenuProvider *provider,
632 633
                                  GtkWidget            *window,
                                  GList                *files)
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 668 669 670 671 672
  TerminalNautilus *nautilus = TERMINAL_NAUTILUS (provider);
  gchar *uri;
  GList *items;
  NautilusMenuItem *item;
  NautilusFileInfo *file_info;
  GFileType file_type;
  TerminalFileInfo terminal_file_info;

  if (terminal_locked_down (nautilus))
    return NULL;

  /* Only add items when passed exactly one file */
  if (files == NULL || files->next != NULL)
    return NULL;

  file_info = (NautilusFileInfo *) files->data;
  file_type = nautilus_file_info_get_file_type (file_info);
  if (!nautilus_file_info_is_directory (file_info) &&
      file_type != G_FILE_TYPE_SHORTCUT &&
      file_type != G_FILE_TYPE_MOUNTABLE)
    return NULL;

  uri = nautilus_file_info_get_activation_uri (file_info);
  if (uri == NULL)
    return NULL;

  items = NULL;

  terminal_file_info = get_terminal_file_info_from_uri (uri);

  switch (terminal_file_info) {
    case FILE_INFO_LOCAL:
    case FILE_INFO_SFTP:
    case FILE_INFO_OTHER:
      if (terminal_file_info == FILE_INFO_SFTP || 
          uri_has_local_path (uri)) {
        item = terminal_nautilus_menu_item_new (nautilus,
                                                file_info,
673
                                                terminal_file_info,
674 675 676 677 678 679 680 681 682
                                                terminal_file_info == FILE_INFO_SFTP, 
                                                TRUE);
        items = g_list_append (items, item);
      }

      if (terminal_file_info == FILE_INFO_SFTP &&
          uri_has_local_path (uri)) {
        item = terminal_nautilus_menu_item_new (nautilus,
                                                file_info, 
683
                                                terminal_file_info,
684 685 686 687 688 689 690
                                                FALSE, 
                                                TRUE);
        items = g_list_append (items, item);
      }

    case FILE_INFO_DESKTOP:
      break;
691

692 693 694 695 696 697 698
    default:
      g_assert_not_reached ();
  }

  g_free (uri);

  return items;
699 700 701 702 703
}

static void
terminal_nautilus_menu_provider_iface_init (NautilusMenuProviderIface *iface)
{
704 705
  iface->get_background_items = terminal_nautilus_get_background_items;
  iface->get_file_items = terminal_nautilus_get_file_items;
706 707
}

708 709 710 711
G_DEFINE_DYNAMIC_TYPE_EXTENDED (TerminalNautilus, terminal_nautilus, G_TYPE_OBJECT, 0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (NAUTILUS_TYPE_MENU_PROVIDER,
                                                               terminal_nautilus_menu_provider_iface_init))

712
static void 
713 714 715 716 717 718 719
terminal_nautilus_init (TerminalNautilus *nautilus)
{
  nautilus->lockdown_prefs = g_settings_new (GNOME_DESKTOP_LOCKDOWN_SETTINGS_SCHEMA);
}

static void
terminal_nautilus_dispose (GObject *object)
720
{
721 722
  TerminalNautilus *nautilus = TERMINAL_NAUTILUS (object);

723
  g_clear_object (&nautilus->lockdown_prefs);
724 725

  G_OBJECT_CLASS (terminal_nautilus_parent_class)->dispose (object);
726 727 728
}

static void
729
terminal_nautilus_class_init (TerminalNautilusClass *klass)
730
{
731
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
732

733
  gobject_class->dispose = terminal_nautilus_dispose;
734

735
  terminal_i18n_init (FALSE);
736 737 738 739 740 741 742
}

static void
terminal_nautilus_class_finalize (TerminalNautilusClass *class)
{
}

743 744 745 746
/* Nautilus extension */

static GType type_list[1];

747 748 749
#define EXPORT __attribute__((__visibility__("default"))) extern

EXPORT void
750
nautilus_module_initialize (GTypeModule *module)
751
{
752 753 754 755
  terminal_nautilus_register_type (module);
  terminal_nautilus_menu_item_register_type (module);

  type_list[0] = TERMINAL_TYPE_NAUTILUS;
756 757
}

758
EXPORT void
759 760 761 762
nautilus_module_shutdown (void)
{
}

763
EXPORT void
764 765
nautilus_module_list_types (const GType **types,
                            int          *num_types)
766
{
767 768
  *types = type_list;
  *num_types = G_N_ELEMENTS (type_list);
769
}