Blame gio/gio-tool-mount.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#ifdef HAVE_TERMIOS_H
Packit ae235b
#include <termios.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
#define STDIN_FILENO 0
Packit ae235b
Packit ae235b
typedef enum {
Packit ae235b
  MOUNT_OP_NONE,
Packit ae235b
  MOUNT_OP_ASKED,
Packit ae235b
  MOUNT_OP_ABORTED
Packit ae235b
} MountOpState;
Packit ae235b
Packit ae235b
static int outstanding_mounts = 0;
Packit ae235b
static GMainLoop *main_loop;
Packit ae235b
Packit ae235b
static gboolean mount_mountable = FALSE;
Packit ae235b
static gboolean mount_unmount = FALSE;
Packit ae235b
static gboolean mount_eject = FALSE;
Packit ae235b
static gboolean force = FALSE;
Packit ae235b
static gboolean anonymous = FALSE;
Packit ae235b
static gboolean mount_list = FALSE;
Packit ae235b
static gboolean extra_detail = FALSE;
Packit ae235b
static gboolean mount_monitor = FALSE;
Packit ae235b
static const char *unmount_scheme = NULL;
Packit ae235b
static const char *mount_device_file = NULL;
Packit ae235b
static gboolean success = TRUE;
Packit ae235b
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] =
Packit ae235b
{
Packit ae235b
  { "mountable", 'm', 0, G_OPTION_ARG_NONE, &mount_mountable, N_("Mount as mountable"), NULL },
Packit ae235b
  { "device", 'd', 0, G_OPTION_ARG_STRING, &mount_device_file, N_("Mount volume with device file"), N_("DEVICE") },
Packit ae235b
  { "unmount", 'u', 0, G_OPTION_ARG_NONE, &mount_unmount, N_("Unmount"), NULL},
Packit ae235b
  { "eject", 'e', 0, G_OPTION_ARG_NONE, &mount_eject, N_("Eject"), NULL},
Packit ae235b
  { "unmount-scheme", 's', 0, G_OPTION_ARG_STRING, &unmount_scheme, N_("Unmount all mounts with the given scheme"), N_("SCHEME") },
Packit ae235b
  { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore outstanding file operations when unmounting or ejecting"), NULL },
Packit ae235b
  { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, N_("Use an anonymous user when authenticating"), NULL },
Packit ae235b
  /* Translator: List here is a verb as in 'List all mounts' */
Packit ae235b
  { "list", 'l', 0, G_OPTION_ARG_NONE, &mount_list, N_("List"), NULL},
Packit ae235b
  { "monitor", 'o', 0, G_OPTION_ARG_NONE, &mount_monitor, N_("Monitor events"), NULL},
Packit ae235b
  { "detail", 'i', 0, G_OPTION_ARG_NONE, &extra_detail, N_("Show extra information"), NULL},
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static char *
Packit ae235b
prompt_for (const char *prompt, const char *default_value, gboolean echo)
Packit ae235b
{
Packit ae235b
#ifdef HAVE_TERMIOS_H
Packit ae235b
  struct termios term_attr;
Packit ae235b
  int old_flags;
Packit ae235b
  gboolean restore_flags;
Packit ae235b
#endif
Packit ae235b
  char data[256];
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  if (default_value && *default_value != 0)
Packit ae235b
    g_print ("%s [%s]: ", prompt, default_value);
Packit ae235b
  else
Packit ae235b
    g_print ("%s: ", prompt);
Packit ae235b
Packit ae235b
  data[0] = 0;
Packit ae235b
Packit ae235b
#ifdef HAVE_TERMIOS_H
Packit ae235b
  restore_flags = FALSE;
Packit ae235b
  if (!echo && tcgetattr (STDIN_FILENO, &term_attr) == 0)
Packit ae235b
    {
Packit ae235b
      old_flags = term_attr.c_lflag;
Packit ae235b
      term_attr.c_lflag &= ~ECHO;
Packit ae235b
      restore_flags = TRUE;
Packit ae235b
Packit ae235b
      if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
Packit ae235b
        g_print ("Warning! Password will be echoed");
Packit ae235b
    }
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  fgets(data, sizeof (data), stdin);
Packit ae235b
Packit ae235b
#ifdef HAVE_TERMIOS_H
Packit ae235b
  if (restore_flags)
Packit ae235b
    {
Packit ae235b
      term_attr.c_lflag = old_flags;
Packit ae235b
      tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  len = strlen (data);
Packit ae235b
  if (len == 0)
Packit ae235b
    {
Packit ae235b
      g_print ("\n");
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
  if (data[len-1] == '\n')
Packit ae235b
    data[len-1] = 0;
Packit ae235b
Packit ae235b
  if (!echo)
Packit ae235b
    g_print ("\n");
Packit ae235b
Packit ae235b
  if (*data == 0 && default_value)
Packit ae235b
    return g_strdup (default_value);
Packit ae235b
  return g_strdup (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
ask_password_cb (GMountOperation *op,
Packit ae235b
                 const char      *message,
Packit ae235b
                 const char      *default_user,
Packit ae235b
                 const char      *default_domain,
Packit ae235b
                 GAskPasswordFlags flags)
Packit ae235b
{
Packit ae235b
  if ((flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) && anonymous)
Packit ae235b
    {
Packit ae235b
      g_mount_operation_set_anonymous (op, TRUE);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      char *s;
Packit ae235b
      g_print ("%s\n", message);
Packit ae235b
Packit ae235b
      if (flags & G_ASK_PASSWORD_NEED_USERNAME)
Packit ae235b
        {
Packit ae235b
          s = prompt_for ("User", default_user, TRUE);
Packit ae235b
          if (!s)
Packit ae235b
            goto error;
Packit ae235b
          g_mount_operation_set_username (op, s);
Packit ae235b
          g_free (s);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
Packit ae235b
        {
Packit ae235b
          s = prompt_for ("Domain", default_domain, TRUE);
Packit ae235b
          if (!s)
Packit ae235b
            goto error;
Packit ae235b
          g_mount_operation_set_domain (op, s);
Packit ae235b
          g_free (s);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
Packit ae235b
        {
Packit ae235b
          s = prompt_for ("Password", NULL, FALSE);
Packit ae235b
          if (!s)
Packit ae235b
            goto error;
Packit ae235b
          g_mount_operation_set_password (op, s);
Packit ae235b
          g_free (s);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Only try anonymous access once. */
Packit ae235b
  if (anonymous &&
Packit ae235b
      GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ASKED)
Packit ae235b
    {
Packit ae235b
      g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ABORTED));
Packit ae235b
      g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ASKED));
Packit ae235b
      g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return;
Packit ae235b
Packit ae235b
error:
Packit ae235b
  g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
ask_question_cb (GMountOperation *op,
Packit ae235b
                 char *message,
Packit ae235b
                 char **choices,
Packit ae235b
                 gpointer user_data)
Packit ae235b
{
Packit ae235b
  char **ptr = choices;
Packit ae235b
  char *s;
Packit ae235b
  int i, choice;
Packit ae235b
Packit ae235b
  g_print ("%s\n", message);
Packit ae235b
Packit ae235b
  i = 1;
Packit ae235b
  while (*ptr)
Packit ae235b
    {
Packit ae235b
      g_print ("[%d] %s\n", i, *ptr++);
Packit ae235b
      i++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  s = prompt_for ("Choice", NULL, TRUE);
Packit ae235b
  if (!s)
Packit ae235b
    goto error;
Packit ae235b
Packit ae235b
  choice = atoi (s);
Packit ae235b
  if (choice > 0 && choice < i)
Packit ae235b
    {
Packit ae235b
      g_mount_operation_set_choice (op, choice - 1);
Packit ae235b
      g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
Packit ae235b
    }
Packit ae235b
  g_free (s);
Packit ae235b
Packit ae235b
  return;
Packit ae235b
Packit ae235b
error:
Packit ae235b
  g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mount_mountable_done_cb (GObject *object,
Packit ae235b
                         GAsyncResult *res,
Packit ae235b
                         gpointer user_data)
Packit ae235b
{
Packit ae235b
  GFile *target;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMountOperation *op = user_data;
Packit ae235b
Packit ae235b
  target = g_file_mount_mountable_finish (G_FILE (object), res, &error);
Packit ae235b
Packit ae235b
  if (target == NULL)
Packit ae235b
    {
Packit ae235b
      success = FALSE;
Packit ae235b
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
Packit ae235b
        print_file_error (G_FILE (object), _("Anonymous access denied"));
Packit ae235b
      else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
Packit ae235b
        print_file_error (G_FILE (object), error->message);
Packit ae235b
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_object_unref (target);
Packit ae235b
Packit ae235b
  outstanding_mounts--;
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mount_done_cb (GObject *object,
Packit ae235b
               GAsyncResult *res,
Packit ae235b
               gpointer user_data)
Packit ae235b
{
Packit ae235b
  gboolean succeeded;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMountOperation *op = user_data;
Packit ae235b
Packit ae235b
  succeeded = g_file_mount_enclosing_volume_finish (G_FILE (object), res, &error);
Packit ae235b
Packit ae235b
  if (!succeeded)
Packit ae235b
    {
Packit ae235b
      success = FALSE;
Packit ae235b
      if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
Packit ae235b
        print_file_error (G_FILE (object), _("Anonymous access denied"));
Packit ae235b
      else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
Packit ae235b
        print_file_error (G_FILE (object), error->message);
Packit ae235b
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  outstanding_mounts--;
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMountOperation *
Packit ae235b
new_mount_op (void)
Packit ae235b
{
Packit ae235b
  GMountOperation *op;
Packit ae235b
Packit ae235b
  op = g_mount_operation_new ();
Packit ae235b
Packit ae235b
  g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_NONE));
Packit ae235b
Packit ae235b
  g_signal_connect (op, "ask_password", G_CALLBACK (ask_password_cb), NULL);
Packit ae235b
  g_signal_connect (op, "ask_question", G_CALLBACK (ask_question_cb), NULL);
Packit ae235b
Packit ae235b
  /* TODO: we *should* also connect to the "aborted" signal but since the
Packit ae235b
   * main thread is blocked handling input we won't get that signal anyway...
Packit ae235b
   */
Packit ae235b
Packit ae235b
  return op;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
mount (GFile *file)
Packit ae235b
{
Packit ae235b
  GMountOperation *op;
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  op = new_mount_op ();
Packit ae235b
Packit ae235b
  if (mount_mountable)
Packit ae235b
    g_file_mount_mountable (file, 0, op, NULL, mount_mountable_done_cb, op);
Packit ae235b
  else
Packit ae235b
    g_file_mount_enclosing_volume (file, 0, op, NULL, mount_done_cb, op);
Packit ae235b
Packit ae235b
  outstanding_mounts++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
unmount_done_cb (GObject *object,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer user_data)
Packit ae235b
{
Packit ae235b
  gboolean succeeded;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file = G_FILE (user_data);
Packit ae235b
Packit ae235b
  succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
Packit ae235b
Packit ae235b
  g_object_unref (G_MOUNT (object));
Packit ae235b
Packit ae235b
  if (!succeeded)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      success = FALSE;
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  outstanding_mounts--;
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
unmount (GFile *file)
Packit ae235b
{
Packit ae235b
  GMount *mount;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMountOperation *mount_op;
Packit ae235b
  GMountUnmountFlags flags;
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  mount = g_file_find_enclosing_mount (file, NULL, &error);
Packit ae235b
  if (mount == NULL)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      success = FALSE;
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  mount_op = new_mount_op ();
Packit ae235b
  flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
Packit ae235b
  g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, g_object_ref (file));
Packit ae235b
  g_object_unref (mount_op);
Packit ae235b
Packit ae235b
  outstanding_mounts++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject_done_cb (GObject *object,
Packit ae235b
               GAsyncResult *res,
Packit ae235b
               gpointer user_data)
Packit ae235b
{
Packit ae235b
  gboolean succeeded;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file = G_FILE (user_data);
Packit ae235b
Packit ae235b
  succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
Packit ae235b
Packit ae235b
  g_object_unref (G_MOUNT (object));
Packit ae235b
Packit ae235b
  if (!succeeded)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      success = FALSE;
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  outstanding_mounts--;
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject (GFile *file)
Packit ae235b
{
Packit ae235b
  GMount *mount;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GMountOperation *mount_op;
Packit ae235b
  GMountUnmountFlags flags;
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  mount = g_file_find_enclosing_mount (file, NULL, &error);
Packit ae235b
  if (mount == NULL)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      success = FALSE;
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  mount_op = new_mount_op ();
Packit ae235b
  flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
Packit ae235b
  g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
Packit ae235b
  g_object_unref (mount_op);
Packit ae235b
Packit ae235b
  outstanding_mounts++;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
iterate_gmain_timeout_function (gpointer data)
Packit ae235b
{
Packit ae235b
  g_main_loop_quit (main_loop);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
iterate_gmain(void)
Packit ae235b
{
Packit ae235b
  g_timeout_add (500, iterate_gmain_timeout_function, NULL);
Packit ae235b
  g_main_loop_run (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
show_themed_icon_names (GThemedIcon *icon, gboolean symbolic, int indent)
Packit ae235b
{
Packit ae235b
  char **names;
Packit ae235b
  char **iter;
Packit ae235b
Packit ae235b
  g_print ("%*s%sthemed icons:", indent, " ", symbolic ? "symbolic " : "");
Packit ae235b
Packit ae235b
  names = NULL;
Packit ae235b
Packit ae235b
  g_object_get (icon, "names", &names, NULL);
Packit ae235b
Packit ae235b
  for (iter = names; *iter; iter++)
Packit ae235b
    g_print ("  [%s]", *iter);
Packit ae235b
Packit ae235b
  g_print ("\n");
Packit ae235b
  g_strfreev (names);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* don't copy-paste this code */
Packit ae235b
static char *
Packit ae235b
get_type_name (gpointer object)
Packit ae235b
{
Packit ae235b
  const char *type_name;
Packit ae235b
  char *ret;
Packit ae235b
Packit ae235b
  type_name = g_type_name (G_TYPE_FROM_INSTANCE (object));
Packit ae235b
  if (strcmp ("GProxyDrive", type_name) == 0)
Packit ae235b
    {
Packit ae235b
      ret = g_strdup_printf ("%s (%s)",
Packit ae235b
                             type_name,
Packit ae235b
                             (const char *) g_object_get_data (G_OBJECT (object),
Packit ae235b
                                                               "g-proxy-drive-volume-monitor-name"));
Packit ae235b
    }
Packit ae235b
  else if (strcmp ("GProxyVolume", type_name) == 0)
Packit ae235b
    {
Packit ae235b
      ret = g_strdup_printf ("%s (%s)",
Packit ae235b
                             type_name,
Packit ae235b
                             (const char *) g_object_get_data (G_OBJECT (object),
Packit ae235b
                                                               "g-proxy-volume-volume-monitor-name"));
Packit ae235b
    }
Packit ae235b
  else if (strcmp ("GProxyMount", type_name) == 0)
Packit ae235b
    {
Packit ae235b
      ret = g_strdup_printf ("%s (%s)",
Packit ae235b
                             type_name,
Packit ae235b
                             (const char *) g_object_get_data (G_OBJECT (object),
Packit ae235b
                                                               "g-proxy-mount-volume-monitor-name"));
Packit ae235b
    }
Packit ae235b
  else if (strcmp ("GProxyShadowMount", type_name) == 0)
Packit ae235b
    {
Packit ae235b
      ret = g_strdup_printf ("%s (%s)",
Packit ae235b
                             type_name,
Packit ae235b
                             (const char *) g_object_get_data (G_OBJECT (object),
Packit ae235b
                                                               "g-proxy-shadow-mount-volume-monitor-name"));
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      ret = g_strdup (type_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
list_mounts (GList *mounts,
Packit ae235b
             int indent,
Packit ae235b
             gboolean only_with_no_volume)
Packit ae235b
{
Packit ae235b
  GList *l;
Packit ae235b
  int c;
Packit ae235b
  GMount *mount;
Packit ae235b
  GVolume *volume;
Packit ae235b
  char *name, *uuid, *uri;
Packit ae235b
  GFile *root, *default_location;
Packit ae235b
  GIcon *icon;
Packit ae235b
  char **x_content_types;
Packit ae235b
  char *type_name;
Packit ae235b
  const gchar *sort_key;
Packit ae235b
Packit ae235b
  for (c = 0, l = mounts; l != NULL; l = l->next, c++)
Packit ae235b
    {
Packit ae235b
      mount = (GMount *) l->data;
Packit ae235b
Packit ae235b
      if (only_with_no_volume)
Packit ae235b
        {
Packit ae235b
          volume = g_mount_get_volume (mount);
Packit ae235b
          if (volume != NULL)
Packit ae235b
            {
Packit ae235b
              g_object_unref (volume);
Packit ae235b
              continue;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      name = g_mount_get_name (mount);
Packit ae235b
      root = g_mount_get_root (mount);
Packit ae235b
      uri = g_file_get_uri (root);
Packit ae235b
Packit ae235b
      g_print ("%*sMount(%d): %s -> %s\n", indent, "", c, name, uri);
Packit ae235b
Packit ae235b
      type_name = get_type_name (mount);
Packit ae235b
      g_print ("%*sType: %s\n", indent+2, "", type_name);
Packit ae235b
      g_free (type_name);
Packit ae235b
Packit ae235b
      if (extra_detail)
Packit ae235b
        {
Packit ae235b
          uuid = g_mount_get_uuid (mount);
Packit ae235b
          if (uuid)
Packit ae235b
            g_print ("%*suuid=%s\n", indent + 2, "", uuid);
Packit ae235b
Packit ae235b
          default_location = g_mount_get_default_location (mount);
Packit ae235b
          if (default_location)
Packit ae235b
            {
Packit ae235b
              char *loc_uri = g_file_get_uri (default_location);
Packit ae235b
              g_print ("%*sdefault_location=%s\n", indent + 2, "", loc_uri);
Packit ae235b
              g_free (loc_uri);
Packit ae235b
              g_object_unref (default_location);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          icon = g_mount_get_icon (mount);
Packit ae235b
          if (icon)
Packit ae235b
            {
Packit ae235b
              if (G_IS_THEMED_ICON (icon))
Packit ae235b
                show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
Packit ae235b
Packit ae235b
              g_object_unref (icon);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          icon = g_mount_get_symbolic_icon (mount);
Packit ae235b
          if (icon)
Packit ae235b
            {
Packit ae235b
              if (G_IS_THEMED_ICON (icon))
Packit ae235b
                show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
Packit ae235b
Packit ae235b
              g_object_unref (icon);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          x_content_types = g_mount_guess_content_type_sync (mount, FALSE, NULL, NULL);
Packit ae235b
          if (x_content_types != NULL && g_strv_length (x_content_types) > 0)
Packit ae235b
            {
Packit ae235b
              int n;
Packit ae235b
              g_print ("%*sx_content_types:", indent + 2, "");
Packit ae235b
              for (n = 0; x_content_types[n] != NULL; n++)
Packit ae235b
                  g_print (" %s", x_content_types[n]);
Packit ae235b
              g_print ("\n");
Packit ae235b
            }
Packit ae235b
          g_strfreev (x_content_types);
Packit ae235b
Packit ae235b
          g_print ("%*scan_unmount=%d\n", indent + 2, "", g_mount_can_unmount (mount));
Packit ae235b
          g_print ("%*scan_eject=%d\n", indent + 2, "", g_mount_can_eject (mount));
Packit ae235b
          g_print ("%*sis_shadowed=%d\n", indent + 2, "", g_mount_is_shadowed (mount));
Packit ae235b
          sort_key = g_mount_get_sort_key (mount);
Packit ae235b
          if (sort_key != NULL)
Packit ae235b
            g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
Packit ae235b
          g_free (uuid);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_object_unref (root);
Packit ae235b
      g_free (name);
Packit ae235b
      g_free (uri);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
list_volumes (GList *volumes,
Packit ae235b
              int indent,
Packit ae235b
              gboolean only_with_no_drive)
Packit ae235b
{
Packit ae235b
  GList *l, *mounts;
Packit ae235b
  int c, i;
Packit ae235b
  GMount *mount;
Packit ae235b
  GVolume *volume;
Packit ae235b
  GDrive *drive;
Packit ae235b
  char *name;
Packit ae235b
  char *uuid;
Packit ae235b
  GFile *activation_root;
Packit ae235b
  char **ids;
Packit ae235b
  GIcon *icon;
Packit ae235b
  char *type_name;
Packit ae235b
  const gchar *sort_key;
Packit ae235b
Packit ae235b
  for (c = 0, l = volumes; l != NULL; l = l->next, c++)
Packit ae235b
    {
Packit ae235b
      volume = (GVolume *) l->data;
Packit ae235b
Packit ae235b
      if (only_with_no_drive)
Packit ae235b
        {
Packit ae235b
          drive = g_volume_get_drive (volume);
Packit ae235b
          if (drive != NULL)
Packit ae235b
            {
Packit ae235b
              g_object_unref (drive);
Packit ae235b
              continue;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      name = g_volume_get_name (volume);
Packit ae235b
Packit ae235b
      g_print ("%*sVolume(%d): %s\n", indent, "", c, name);
Packit ae235b
      g_free (name);
Packit ae235b
Packit ae235b
      type_name = get_type_name (volume);
Packit ae235b
      g_print ("%*sType: %s\n", indent+2, "", type_name);
Packit ae235b
      g_free (type_name);
Packit ae235b
Packit ae235b
      if (extra_detail)
Packit ae235b
        {
Packit ae235b
          ids = g_volume_enumerate_identifiers (volume);
Packit ae235b
          if (ids && ids[0] != NULL)
Packit ae235b
            {
Packit ae235b
              g_print ("%*sids:\n", indent+2, "");
Packit ae235b
              for (i = 0; ids[i] != NULL; i++)
Packit ae235b
                {
Packit ae235b
                  char *id = g_volume_get_identifier (volume,
Packit ae235b
                                                      ids[i]);
Packit ae235b
                  g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
Packit ae235b
                  g_free (id);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          g_strfreev (ids);
Packit ae235b
Packit ae235b
          uuid = g_volume_get_uuid (volume);
Packit ae235b
          if (uuid)
Packit ae235b
            g_print ("%*suuid=%s\n", indent + 2, "", uuid);
Packit ae235b
          activation_root = g_volume_get_activation_root (volume);
Packit ae235b
          if (activation_root)
Packit ae235b
            {
Packit ae235b
              char *uri;
Packit ae235b
              uri = g_file_get_uri (activation_root);
Packit ae235b
              g_print ("%*sactivation_root=%s\n", indent + 2, "", uri);
Packit ae235b
              g_free (uri);
Packit ae235b
              g_object_unref (activation_root);
Packit ae235b
            }
Packit ae235b
          icon = g_volume_get_icon (volume);
Packit ae235b
          if (icon)
Packit ae235b
            {
Packit ae235b
              if (G_IS_THEMED_ICON (icon))
Packit ae235b
                show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
Packit ae235b
Packit ae235b
              g_object_unref (icon);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          icon = g_volume_get_symbolic_icon (volume);
Packit ae235b
          if (icon)
Packit ae235b
            {
Packit ae235b
              if (G_IS_THEMED_ICON (icon))
Packit ae235b
                show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
Packit ae235b
Packit ae235b
              g_object_unref (icon);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_print ("%*scan_mount=%d\n", indent + 2, "", g_volume_can_mount (volume));
Packit ae235b
          g_print ("%*scan_eject=%d\n", indent + 2, "", g_volume_can_eject (volume));
Packit ae235b
          g_print ("%*sshould_automount=%d\n", indent + 2, "", g_volume_should_automount (volume));
Packit ae235b
          sort_key = g_volume_get_sort_key (volume);
Packit ae235b
          if (sort_key != NULL)
Packit ae235b
            g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
Packit ae235b
          g_free (uuid);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      mount = g_volume_get_mount (volume);
Packit ae235b
      if (mount)
Packit ae235b
        {
Packit ae235b
          mounts = g_list_prepend (NULL, mount);
Packit ae235b
          list_mounts (mounts, indent + 2, FALSE);
Packit ae235b
          g_list_free (mounts);
Packit ae235b
          g_object_unref (mount);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
list_drives (GList *drives,
Packit ae235b
             int indent)
Packit ae235b
{
Packit ae235b
  GList *volumes, *l;
Packit ae235b
  int c, i;
Packit ae235b
  GDrive *drive;
Packit ae235b
  char *name;
Packit ae235b
  char **ids;
Packit ae235b
  GIcon *icon;
Packit ae235b
  char *type_name;
Packit ae235b
  const gchar *sort_key;
Packit ae235b
Packit ae235b
  for (c = 0, l = drives; l != NULL; l = l->next, c++)
Packit ae235b
    {
Packit ae235b
      drive = (GDrive *) l->data;
Packit ae235b
      name = g_drive_get_name (drive);
Packit ae235b
Packit ae235b
      g_print ("%*sDrive(%d): %s\n", indent, "", c, name);
Packit ae235b
      g_free (name);
Packit ae235b
Packit ae235b
      type_name = get_type_name (drive);
Packit ae235b
      g_print ("%*sType: %s\n", indent+2, "", type_name);
Packit ae235b
      g_free (type_name);
Packit ae235b
Packit ae235b
      if (extra_detail)
Packit ae235b
        {
Packit ae235b
          GEnumValue *enum_value;
Packit ae235b
          gpointer klass;
Packit ae235b
Packit ae235b
          ids = g_drive_enumerate_identifiers (drive);
Packit ae235b
          if (ids && ids[0] != NULL)
Packit ae235b
            {
Packit ae235b
              g_print ("%*sids:\n", indent+2, "");
Packit ae235b
              for (i = 0; ids[i] != NULL; i++)
Packit ae235b
                {
Packit ae235b
                  char *id = g_drive_get_identifier (drive,
Packit ae235b
                                                     ids[i]);
Packit ae235b
                  g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
Packit ae235b
                  g_free (id);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
          g_strfreev (ids);
Packit ae235b
Packit ae235b
          icon = g_drive_get_icon (drive);
Packit ae235b
          if (icon)
Packit ae235b
          {
Packit ae235b
                  if (G_IS_THEMED_ICON (icon))
Packit ae235b
                          show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
Packit ae235b
                  g_object_unref (icon);
Packit ae235b
          }
Packit ae235b
Packit ae235b
          icon = g_drive_get_symbolic_icon (drive);
Packit ae235b
          if (icon)
Packit ae235b
            {
Packit ae235b
              if (G_IS_THEMED_ICON (icon))
Packit ae235b
                show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
Packit ae235b
Packit ae235b
              g_object_unref (icon);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_print ("%*sis_removable=%d\n", indent + 2, "", g_drive_is_removable (drive));
Packit ae235b
          g_print ("%*sis_media_removable=%d\n", indent + 2, "", g_drive_is_media_removable (drive));
Packit ae235b
          g_print ("%*shas_media=%d\n", indent + 2, "", g_drive_has_media (drive));
Packit ae235b
          g_print ("%*sis_media_check_automatic=%d\n", indent + 2, "", g_drive_is_media_check_automatic (drive));
Packit ae235b
          g_print ("%*scan_poll_for_media=%d\n", indent + 2, "", g_drive_can_poll_for_media (drive));
Packit ae235b
          g_print ("%*scan_eject=%d\n", indent + 2, "", g_drive_can_eject (drive));
Packit ae235b
          g_print ("%*scan_start=%d\n", indent + 2, "", g_drive_can_start (drive));
Packit ae235b
          g_print ("%*scan_stop=%d\n", indent + 2, "", g_drive_can_stop (drive));
Packit ae235b
Packit ae235b
          enum_value = NULL;
Packit ae235b
          klass = g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE);
Packit ae235b
          if (klass != NULL)
Packit ae235b
            {
Packit ae235b
              enum_value = g_enum_get_value (klass, g_drive_get_start_stop_type (drive));
Packit ae235b
              g_print ("%*sstart_stop_type=%s\n", indent + 2, "",
Packit ae235b
                       enum_value != NULL ? enum_value->value_nick : "UNKNOWN");
Packit ae235b
              g_type_class_unref (klass);
Packit ae235b
            }
Packit ae235b
Packit ae235b
          sort_key = g_drive_get_sort_key (drive);
Packit ae235b
          if (sort_key != NULL)
Packit ae235b
            g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
Packit ae235b
        }
Packit ae235b
      volumes = g_drive_get_volumes (drive);
Packit ae235b
      list_volumes (volumes, indent + 2, FALSE);
Packit ae235b
      g_list_free_full (volumes, g_object_unref);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
list_monitor_items (void)
Packit ae235b
{
Packit ae235b
  GVolumeMonitor *volume_monitor;
Packit ae235b
  GList *drives, *volumes, *mounts;
Packit ae235b
Packit ae235b
  volume_monitor = g_volume_monitor_get();
Packit ae235b
Packit ae235b
  /* populate gvfs network mounts */
Packit ae235b
  iterate_gmain();
Packit ae235b
Packit ae235b
  drives = g_volume_monitor_get_connected_drives (volume_monitor);
Packit ae235b
  list_drives (drives, 0);
Packit ae235b
  g_list_free_full (drives, g_object_unref);
Packit ae235b
Packit ae235b
  volumes = g_volume_monitor_get_volumes (volume_monitor);
Packit ae235b
  list_volumes (volumes, 0, TRUE);
Packit ae235b
  g_list_free_full (volumes, g_object_unref);
Packit ae235b
Packit ae235b
  mounts = g_volume_monitor_get_mounts (volume_monitor);
Packit ae235b
  list_mounts (mounts, 0, TRUE);
Packit ae235b
  g_list_free_full (mounts, g_object_unref);
Packit ae235b
Packit ae235b
  g_object_unref (volume_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
unmount_all_with_scheme (const char *scheme)
Packit ae235b
{
Packit ae235b
  GVolumeMonitor *volume_monitor;
Packit ae235b
  GList *mounts;
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  volume_monitor = g_volume_monitor_get();
Packit ae235b
Packit ae235b
  /* populate gvfs network mounts */
Packit ae235b
  iterate_gmain();
Packit ae235b
Packit ae235b
  mounts = g_volume_monitor_get_mounts (volume_monitor);
Packit ae235b
  for (l = mounts; l != NULL; l = l->next) {
Packit ae235b
    GMount *mount = G_MOUNT (l->data);
Packit ae235b
    GFile *root;
Packit ae235b
Packit ae235b
    root = g_mount_get_root (mount);
Packit ae235b
    if (g_file_has_uri_scheme (root, scheme)) {
Packit ae235b
            unmount (root);
Packit ae235b
    }
Packit ae235b
    g_object_unref (root);
Packit ae235b
  }
Packit ae235b
  g_list_free_full (mounts, g_object_unref);
Packit ae235b
Packit ae235b
  g_object_unref (volume_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mount_with_device_file_cb (GObject *object,
Packit ae235b
                           GAsyncResult *res,
Packit ae235b
                           gpointer user_data)
Packit ae235b
{
Packit ae235b
  GVolume *volume;
Packit ae235b
  gboolean succeeded;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gchar *device_path = (gchar *)user_data;
Packit ae235b
Packit ae235b
  volume = G_VOLUME (object);
Packit ae235b
Packit ae235b
  succeeded = g_volume_mount_finish (volume, res, &error);
Packit ae235b
Packit ae235b
  if (!succeeded)
Packit ae235b
    {
Packit ae235b
      print_error ("%s: %s", device_path, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      success = FALSE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GMount *mount;
Packit ae235b
      GFile *root;
Packit ae235b
      char *mount_path;
Packit ae235b
Packit ae235b
      mount = g_volume_get_mount (volume);
Packit ae235b
      root = g_mount_get_root (mount);
Packit ae235b
      mount_path = g_file_get_path (root);
Packit ae235b
Packit ae235b
      g_print (_("Mounted %s at %s\n"), device_path, mount_path);
Packit ae235b
Packit ae235b
      g_object_unref (mount);
Packit ae235b
      g_object_unref (root);
Packit ae235b
      g_free (mount_path);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (device_path);
Packit ae235b
Packit ae235b
  outstanding_mounts--;
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    g_main_loop_quit (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
mount_with_device_file (const char *device_file)
Packit ae235b
{
Packit ae235b
  GVolumeMonitor *volume_monitor;
Packit ae235b
  GList *volumes;
Packit ae235b
  GList *l;
Packit ae235b
Packit ae235b
  volume_monitor = g_volume_monitor_get();
Packit ae235b
Packit ae235b
  volumes = g_volume_monitor_get_volumes (volume_monitor);
Packit ae235b
  for (l = volumes; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      GVolume *volume = G_VOLUME (l->data);
Packit ae235b
      gchar *id;
Packit ae235b
Packit ae235b
      id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
Packit ae235b
      if (g_strcmp0 (id, device_file) == 0)
Packit ae235b
        {
Packit ae235b
          GMountOperation *op;
Packit ae235b
Packit ae235b
          op = new_mount_op ();
Packit ae235b
Packit ae235b
          g_volume_mount (volume,
Packit ae235b
                          G_MOUNT_MOUNT_NONE,
Packit ae235b
                          op,
Packit ae235b
                          NULL,
Packit ae235b
                          mount_with_device_file_cb,
Packit ae235b
                          id);
Packit ae235b
Packit ae235b
          outstanding_mounts++;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_free (id);
Packit ae235b
    }
Packit ae235b
  g_list_free_full (volumes, g_object_unref);
Packit ae235b
Packit ae235b
  if (outstanding_mounts == 0)
Packit ae235b
    {
Packit ae235b
      print_error ("%s: %s", device_file, _("No volume for device file"));
Packit ae235b
      success = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (volume_monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_print_mount (GMount *mount)
Packit ae235b
{
Packit ae235b
  if (extra_detail)
Packit ae235b
    {
Packit ae235b
      GList *l;
Packit ae235b
      l = g_list_prepend (NULL, mount);
Packit ae235b
      list_mounts (l, 2, FALSE);
Packit ae235b
      g_list_free (l);
Packit ae235b
      g_print ("\n");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_print_volume (GVolume *volume)
Packit ae235b
{
Packit ae235b
  if (extra_detail)
Packit ae235b
    {
Packit ae235b
      GList *l;
Packit ae235b
      l = g_list_prepend (NULL, volume);
Packit ae235b
      list_volumes (l, 2, FALSE);
Packit ae235b
      g_list_free (l);
Packit ae235b
      g_print ("\n");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_print_drive (GDrive *drive)
Packit ae235b
{
Packit ae235b
  if (extra_detail)
Packit ae235b
    {
Packit ae235b
      GList *l;
Packit ae235b
      l = g_list_prepend (NULL, drive);
Packit ae235b
      list_drives (l, 2);
Packit ae235b
      g_list_free (l);
Packit ae235b
      g_print ("\n");
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_mount_added (GVolumeMonitor *volume_monitor, GMount *mount)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_mount_get_name (mount);
Packit ae235b
  g_print ("Mount added:        '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_mount (mount);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_mount_removed (GVolumeMonitor *volume_monitor, GMount *mount)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_mount_get_name (mount);
Packit ae235b
  g_print ("Mount removed:      '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_mount (mount);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_mount_changed (GVolumeMonitor *volume_monitor, GMount *mount)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_mount_get_name (mount);
Packit ae235b
  g_print ("Mount changed:      '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_mount (mount);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_mount_pre_unmount (GVolumeMonitor *volume_monitor, GMount *mount)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_mount_get_name (mount);
Packit ae235b
  g_print ("Mount pre-unmount:  '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_mount (mount);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_volume_added (GVolumeMonitor *volume_monitor, GVolume *volume)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_volume_get_name (volume);
Packit ae235b
  g_print ("Volume added:       '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_volume (volume);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_volume_removed (GVolumeMonitor *volume_monitor, GVolume *volume)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_volume_get_name (volume);
Packit ae235b
  g_print ("Volume removed:     '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_volume (volume);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_volume_changed (GVolumeMonitor *volume_monitor, GVolume *volume)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_volume_get_name (volume);
Packit ae235b
  g_print ("Volume changed:     '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_volume (volume);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_drive_connected (GVolumeMonitor *volume_monitor, GDrive *drive)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_drive_get_name (drive);
Packit ae235b
  g_print ("Drive connected:    '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_drive (drive);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_drive_disconnected (GVolumeMonitor *volume_monitor, GDrive *drive)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_drive_get_name (drive);
Packit ae235b
  g_print ("Drive disconnected: '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_drive (drive);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_drive_changed (GVolumeMonitor *volume_monitor, GDrive *drive)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_drive_get_name (drive);
Packit ae235b
  g_print ("Drive changed:      '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
  monitor_print_drive (drive);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor_drive_eject_button (GVolumeMonitor *volume_monitor, GDrive *drive)
Packit ae235b
{
Packit ae235b
  char *name;
Packit ae235b
  name = g_drive_get_name (drive);
Packit ae235b
  g_print ("Drive eject button: '%s'\n", name);
Packit ae235b
  g_free (name);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
monitor (void)
Packit ae235b
{
Packit ae235b
  GVolumeMonitor *volume_monitor;
Packit ae235b
Packit ae235b
  volume_monitor = g_volume_monitor_get ();
Packit ae235b
Packit ae235b
  g_signal_connect (volume_monitor, "mount-added", (GCallback) monitor_mount_added, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "mount-removed", (GCallback) monitor_mount_removed, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "mount-changed", (GCallback) monitor_mount_changed, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback) monitor_mount_pre_unmount, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "volume-added", (GCallback) monitor_volume_added, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "volume-removed", (GCallback) monitor_volume_removed, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "volume-changed", (GCallback) monitor_volume_changed, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "drive-connected", (GCallback) monitor_drive_connected, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "drive-disconnected", (GCallback) monitor_drive_disconnected, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "drive-changed", (GCallback) monitor_drive_changed, NULL);
Packit ae235b
  g_signal_connect (volume_monitor, "drive-eject-button", (GCallback) monitor_drive_eject_button, NULL);
Packit ae235b
Packit ae235b
  g_print ("Monitoring events. Press Ctrl+C to quit.\n");
Packit ae235b
Packit ae235b
  g_main_loop_run (main_loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_mount (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio mount");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("[%s...]", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context, _("Mount or unmount the locations."));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  main_loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  if (mount_list)
Packit ae235b
    list_monitor_items ();
Packit ae235b
  else if (mount_device_file != NULL)
Packit ae235b
    mount_with_device_file (mount_device_file);
Packit ae235b
  else if (unmount_scheme != NULL)
Packit ae235b
    unmount_all_with_scheme (unmount_scheme);
Packit ae235b
  else if (mount_monitor)
Packit ae235b
    monitor ();
Packit ae235b
  else if (argc > 1)
Packit ae235b
    {
Packit ae235b
      for (i = 1; i < argc; i++)
Packit ae235b
        {
Packit ae235b
          file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
          if (mount_unmount)
Packit ae235b
            unmount (file);
Packit ae235b
          else if (mount_eject)
Packit ae235b
            eject (file);
Packit ae235b
          else
Packit ae235b
            mount (file);
Packit ae235b
          g_object_unref (file);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (outstanding_mounts > 0)
Packit ae235b
    g_main_loop_run (main_loop);
Packit ae235b
Packit ae235b
  return success ? 0 : 2;
Packit ae235b
}