gtkmountoperation.c 58.7 KB
Newer Older
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
Cody Russell's avatar
Cody Russell committed
2
/* GTK - The GIMP Toolkit
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Copyright (C) Christian Kellner <gicmo@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
 */

26
#include "config.h"
27 28 29

#include <string.h>

30
#include "gtkmountoperationprivate.h"
31
#include "gtkbox.h"
32
#include "gtkdbusgenerated.h"
33
#include "gtkentry.h"
Matthias Clasen's avatar
Matthias Clasen committed
34
#include "gtkbox.h"
35 36 37 38 39 40
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkmessagedialog.h"
#include "gtkmountoperation.h"
#include "gtkprivate.h"
#include "gtkradiobutton.h"
41
#include "gtkgrid.h"
42
#include "gtkwindow.h"
43 44 45 46 47 48
#include "gtktreeview.h"
#include "gtktreeselection.h"
#include "gtkcellrenderertext.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkscrolledwindow.h"
#include "gtkicontheme.h"
49
#include "gtkmenuitem.h"
50
#include "gtkmain.h"
51
#include "gtksettings.h"
52

53 54
#include <glib/gprintf.h>

Matthias Clasen's avatar
Matthias Clasen committed
55 56 57
/**
 * SECTION:filesystem
 * @short_description: Functions for working with GIO
58
 * @Title: Filesystem utilities
Matthias Clasen's avatar
Matthias Clasen committed
59 60
 *
 * The functions and objects described here make working with GTK+ and
61
 * GIO more convenient.
Matthias Clasen's avatar
Matthias Clasen committed
62
 *
63 64 65
 * #GtkMountOperation is needed when mounting volumes:
 * It is an implementation of #GMountOperation that can be used with
 * GIO functions for mounting volumes such as
66
 * g_file_mount_enclosing_volume(), g_file_mount_mountable(),
67
 * g_volume_mount(), g_mount_unmount_with_operation() and others.
Matthias Clasen's avatar
Matthias Clasen committed
68
 *
69 70
 * When necessary, #GtkMountOperation shows dialogs to ask for
 * passwords, questions or show processes blocking unmount.
71 72 73 74 75 76
 *
 * gtk_show_uri() is a convenient way to launch applications for URIs.
 *
 * Another object that is worth mentioning in this context is
 * #GdkAppLaunchContext, which provides visual feedback when lauching
 * applications.
Matthias Clasen's avatar
Matthias Clasen committed
77 78
 */

79
static void   gtk_mount_operation_finalize     (GObject          *object);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static void   gtk_mount_operation_set_property (GObject          *object,
                                                guint             prop_id,
                                                const GValue     *value,
                                                GParamSpec       *pspec);
static void   gtk_mount_operation_get_property (GObject          *object,
                                                guint             prop_id,
                                                GValue           *value,
                                                GParamSpec       *pspec);

static void   gtk_mount_operation_ask_password (GMountOperation *op,
                                                const char      *message,
                                                const char      *default_user,
                                                const char      *default_domain,
                                                GAskPasswordFlags flags);

static void   gtk_mount_operation_ask_question (GMountOperation *op,
                                                const char      *message,
                                                const char      *choices[]);

99 100 101 102 103
static void   gtk_mount_operation_show_processes (GMountOperation *op,
                                                  const char      *message,
                                                  GArray          *processes,
                                                  const char      *choices[]);

104 105
static void   gtk_mount_operation_aborted      (GMountOperation *op);

106
struct _GtkMountOperationPrivate {
107 108 109 110
  GtkWindow *parent_window;
  GtkDialog *dialog;
  GdkScreen *screen;

111
  /* bus proxy */
112
  _GtkMountOperationHandler *handler;
113 114 115
  GCancellable *cancellable;
  gboolean handler_showing;

116
  /* for the ask-password dialog */
117
  GtkWidget *grid;
118 119 120 121
  GtkWidget *username_entry;
  GtkWidget *domain_entry;
  GtkWidget *password_entry;
  GtkWidget *anonymous_toggle;
122
  GList *user_widgets;
123 124 125 126

  GAskPasswordFlags ask_flags;
  GPasswordSave     password_save;
  gboolean          anonymous;
127 128 129 130

  /* for the show-processes dialog */
  GtkWidget *process_tree_view;
  GtkListStore *process_list_store;
131 132
};

133 134 135 136 137 138 139 140 141 142
enum {
  PROP_0,
  PROP_PARENT,
  PROP_IS_SHOWING,
  PROP_SCREEN

};

G_DEFINE_TYPE_WITH_PRIVATE (GtkMountOperation, gtk_mount_operation, G_TYPE_MOUNT_OPERATION)

143 144 145 146
static void
gtk_mount_operation_class_init (GtkMountOperationClass *klass)
{
  GObjectClass         *object_class = G_OBJECT_CLASS (klass);
147
  GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
148 149 150 151 152 153 154

  object_class->finalize     = gtk_mount_operation_finalize;
  object_class->get_property = gtk_mount_operation_get_property;
  object_class->set_property = gtk_mount_operation_set_property;

  mount_op_class->ask_password = gtk_mount_operation_ask_password;
  mount_op_class->ask_question = gtk_mount_operation_ask_question;
155
  mount_op_class->show_processes = gtk_mount_operation_show_processes;
156
  mount_op_class->aborted = gtk_mount_operation_aborted;
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

  g_object_class_install_property (object_class,
                                   PROP_PARENT,
                                   g_param_spec_object ("parent",
                                                        P_("Parent"),
                                                        P_("The parent window"),
                                                        GTK_TYPE_WINDOW,
                                                        GTK_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_IS_SHOWING,
                                   g_param_spec_boolean ("is-showing",
                                                         P_("Is Showing"),
                                                         P_("Are we showing a dialog"),
                                                         FALSE,
                                                         GTK_PARAM_READABLE));

  g_object_class_install_property (object_class,
                                   PROP_SCREEN,
                                   g_param_spec_object ("screen",
                                                        P_("Screen"),
                                                        P_("The screen where this window will be displayed."),
179
                                                        GDK_TYPE_SCREEN,
180
                                                        GTK_PARAM_READWRITE));
181
}
182

183 184 185
static void
gtk_mount_operation_init (GtkMountOperation *operation)
{
186 187
  gchar *name_owner;

188
  operation->priv = gtk_mount_operation_get_instance_private (operation);
189 190

  operation->priv->handler =
191 192 193 194 195
    _gtk_mount_operation_handler_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                                                         "org.gtk.MountOperationHandler",
                                                         "/org/gtk/MountOperationHandler",
                                                         NULL, NULL);
196 197 198 199
  name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (operation->priv->handler));
  if (!name_owner)
    g_clear_object (&operation->priv->handler);
  g_free (name_owner);
200

201 202
  if (operation->priv->handler)
    g_dbus_proxy_set_default_timeout (G_DBUS_PROXY (operation->priv->handler), G_MAXINT);
203 204
}

205 206 207 208 209 210
static void
gtk_mount_operation_finalize (GObject *object)
{
  GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
  GtkMountOperationPrivate *priv = operation->priv;

211 212 213
  if (priv->user_widgets)
    g_list_free (priv->user_widgets);

214
  if (priv->parent_window)
215 216 217 218 219 220
    {
      g_signal_handlers_disconnect_by_func (priv->parent_window,
                                            gtk_widget_destroyed,
                                            &priv->parent_window);
      g_object_unref (priv->parent_window);
    }
221 222 223 224

  if (priv->screen)
    g_object_unref (priv->screen);

225 226 227
  if (priv->handler)
    g_object_unref (priv->handler);

228 229
  G_OBJECT_CLASS (gtk_mount_operation_parent_class)->finalize (object);
}
230 231

static void
Matthias Clasen's avatar
Matthias Clasen committed
232 233 234 235
gtk_mount_operation_set_property (GObject      *object,
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
236
{
237
  GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
238 239 240

  switch (prop_id)
    {
241 242 243 244 245 246 247 248 249 250
    case PROP_PARENT:
      gtk_mount_operation_set_parent (operation, g_value_get_object (value));
      break;

    case PROP_SCREEN:
      gtk_mount_operation_set_screen (operation, g_value_get_object (value));
      break;

    case PROP_IS_SHOWING:
    default:
251 252 253 254 255 256
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
257 258 259 260
gtk_mount_operation_get_property (GObject    *object,
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
261
{
262 263
  GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
  GtkMountOperationPrivate *priv = operation->priv;
264 265 266 267 268 269 270 271

  switch (prop_id)
    {
    case PROP_PARENT:
      g_value_set_object (value, priv->parent_window);
      break;

    case PROP_IS_SHOWING:
272
      g_value_set_boolean (value, priv->dialog != NULL || priv->handler_showing);
273 274 275 276 277 278 279 280 281 282 283 284
      break;

    case PROP_SCREEN:
      g_value_set_object (value, priv->screen);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

285 286 287 288
static void
gtk_mount_operation_proxy_finish (GtkMountOperation     *op,
                                  GMountOperationResult  result)
{
289
  _gtk_mount_operation_handler_call_close (op->priv->handler, NULL, NULL, NULL);
290 291 292 293 294 295 296 297 298 299

  op->priv->handler_showing = FALSE;
  g_object_notify (G_OBJECT (op), "is-showing");

  g_mount_operation_reply (G_MOUNT_OPERATION (op), result);

  /* drop the reference acquired when calling the proxy method */
  g_object_unref (op);
}

300
static void
301
remember_button_toggled (GtkToggleButton   *button,
302 303 304 305
                         GtkMountOperation *operation)
{
  GtkMountOperationPrivate *priv = operation->priv;

306 307 308 309 310 311 312
  if (gtk_toggle_button_get_active (button))
    {
      gpointer data;

      data = g_object_get_data (G_OBJECT (button), "password-save");
      priv->password_save = GPOINTER_TO_INT (data);
    }
313 314 315 316 317 318 319
}

static void
pw_dialog_got_response (GtkDialog         *dialog,
                        gint               response_id,
                        GtkMountOperation *mount_op)
{
320 321
  GtkMountOperationPrivate *priv = mount_op->priv;
  GMountOperation *op = G_MOUNT_OPERATION (mount_op);
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 380

  if (response_id == GTK_RESPONSE_OK)
    {
      const char *text;

      if (priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED)
        g_mount_operation_set_anonymous (op, priv->anonymous);

      if (priv->username_entry)
        {
          text = gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
          g_mount_operation_set_username (op, text);
        }

      if (priv->domain_entry)
        {
          text = gtk_entry_get_text (GTK_ENTRY (priv->domain_entry));
          g_mount_operation_set_domain (op, text);
        }

      if (priv->password_entry)
        {
          text = gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
          g_mount_operation_set_password (op, text);
        }

      if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
        g_mount_operation_set_password_save (op, priv->password_save);

      g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
    }
  else
    g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);

  priv->dialog = NULL;
  g_object_notify (G_OBJECT (op), "is-showing");
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (op);
}

static gboolean
entry_has_input (GtkWidget *entry_widget)
{
  const char *text;

  if (entry_widget == NULL)
    return TRUE;

  text = gtk_entry_get_text (GTK_ENTRY (entry_widget));

  return text != NULL && text[0] != '\0';
}

static gboolean
pw_dialog_input_is_valid (GtkMountOperation *operation)
{
  GtkMountOperationPrivate *priv = operation->priv;
  gboolean is_valid = TRUE;

381 382 383 384 385 386
  /* We don't require password to be non-empty here
   * since there are situations where it is not needed,
   * see bug 578365.
   * We may add a way for the backend to specify that it
   * definitively needs a password.
   */
387
  is_valid = entry_has_input (priv->username_entry) &&
388
             entry_has_input (priv->domain_entry);
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

  return is_valid;
}

static void
pw_dialog_verify_input (GtkEditable       *editable,
                        GtkMountOperation *operation)
{
  GtkMountOperationPrivate *priv = operation->priv;
  gboolean is_valid;

  is_valid = pw_dialog_input_is_valid (operation);
  gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
                                     GTK_RESPONSE_OK,
                                     is_valid);
}

static void
pw_dialog_anonymous_toggled (GtkWidget         *widget,
                             GtkMountOperation *operation)
{
  GtkMountOperationPrivate *priv = operation->priv;
  gboolean is_valid;
412
  GList *l;
413 414 415 416 417 418 419 420

  priv->anonymous = widget == priv->anonymous_toggle;

  if (priv->anonymous)
    is_valid = TRUE;
  else
    is_valid = pw_dialog_input_is_valid (operation);

421 422 423 424 425
  for (l = priv->user_widgets; l != NULL; l = l->next)
    {
      gtk_widget_set_sensitive (GTK_WIDGET (l->data), !priv->anonymous);
    }

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
                                     GTK_RESPONSE_OK,
                                     is_valid);
}


static void
pw_dialog_cycle_focus (GtkWidget         *widget,
                       GtkMountOperation *operation)
{
  GtkMountOperationPrivate *priv;
  GtkWidget *next_widget = NULL;

  priv = operation->priv;

  if (widget == priv->username_entry)
    {
      if (priv->domain_entry != NULL)
        next_widget = priv->domain_entry;
      else if (priv->password_entry != NULL)
        next_widget = priv->password_entry;
    }
  else if (widget == priv->domain_entry && priv->password_entry)
    next_widget = priv->password_entry;

  if (next_widget)
    gtk_widget_grab_focus (next_widget);
  else if (pw_dialog_input_is_valid (operation))
    gtk_window_activate_default (GTK_WINDOW (priv->dialog));
}

static GtkWidget *
458 459 460 461 462
table_add_entry (GtkMountOperation *operation,
                 int                row,
                 const char        *label_text,
                 const char        *value,
                 gpointer           user_data)
463 464 465 466 467
{
  GtkWidget *entry;
  GtkWidget *label;

  label = gtk_label_new_with_mnemonic (label_text);
468
  gtk_widget_set_halign (label, GTK_ALIGN_END);
469
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
470 471
  gtk_widget_set_hexpand (label, FALSE);
  operation->priv->user_widgets = g_list_prepend (operation->priv->user_widgets, label);
472 473

  entry = gtk_entry_new ();
474
  gtk_widget_set_hexpand (entry, TRUE);
475 476 477 478

  if (value)
    gtk_entry_set_text (GTK_ENTRY (entry), value);

479 480
  gtk_grid_attach (GTK_GRID (operation->priv->grid), label, 0, row, 1, 1);
  gtk_grid_attach (GTK_GRID (operation->priv->grid), entry, 1, row, 1, 1);
481
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
482
  operation->priv->user_widgets = g_list_prepend (operation->priv->user_widgets, entry);
483 484 485 486 487 488 489 490 491 492 493

  g_signal_connect (entry, "changed",
                    G_CALLBACK (pw_dialog_verify_input), user_data);

  g_signal_connect (entry, "activate",
                    G_CALLBACK (pw_dialog_cycle_focus), user_data);

  return entry;
}

static void
494 495 496 497
gtk_mount_operation_ask_password_do_gtk (GtkMountOperation *operation,
                                         const gchar       *message,
                                         const gchar       *default_user,
                                         const gchar       *default_domain)
498 499 500 501 502
{
  GtkMountOperationPrivate *priv;
  GtkWidget *widget;
  GtkDialog *dialog;
  GtkWindow *window;
503 504 505
  GtkWidget *hbox, *main_vbox, *icon;
  GtkWidget *grid;
  GtkWidget *label;
506
  GtkWidget *content_area, *action_area;
507 508
  gboolean   can_anonymous;
  guint      rows;
509
  gchar *primary;
510
  const gchar *secondary;
511
  PangoAttrList *attrs;
512
  gboolean use_header;
513 514 515

  priv = operation->priv;

516 517 518 519 520 521
  g_object_get (gtk_settings_get_default (),
                "gtk-dialogs-use-header", &use_header,
                NULL);
  widget = g_object_new (GTK_TYPE_DIALOG,
                         "use-header-bar", use_header,
                         NULL);
522 523 524 525 526
  dialog = GTK_DIALOG (widget);
  window = GTK_WINDOW (widget);

  priv->dialog = dialog;

527
  content_area = gtk_dialog_get_content_area (dialog);
528
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
529
  action_area = gtk_dialog_get_action_area (dialog);
530
G_GNUC_END_IGNORE_DEPRECATIONS
531

532 533
  /* Set the dialog up with HIG properties */
  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
534 535 536
  gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
  gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
  gtk_box_set_spacing (GTK_BOX (action_area), 6);
537 538

  gtk_window_set_resizable (window, FALSE);
539
  gtk_window_set_title (window, "");
540
  gtk_window_set_icon_name (window, "dialog-password");
541 542

  gtk_dialog_add_buttons (dialog,
543
                          _("_Cancel"), GTK_RESPONSE_CANCEL,
544 545 546
                          _("Co_nnect"), GTK_RESPONSE_OK,
                          NULL);
  gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
547

548
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
549 550 551 552
  gtk_dialog_set_alternative_button_order (dialog,
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
553
G_GNUC_END_IGNORE_DEPRECATIONS
554

555
  /* Build contents */
556
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
557
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
558
  gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
559

560 561
  icon = gtk_image_new_from_icon_name ("dialog-password",
                                       GTK_ICON_SIZE_DIALOG);
562

563 564
  gtk_widget_set_halign (icon, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (icon, GTK_ALIGN_START);
565 566
  gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);

567
  main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
568 569
  gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);

570 571 572 573 574 575 576
  secondary = strstr (message, "\n");
  if (secondary != NULL)
    {
      primary = g_strndup (message, secondary - message + 1);
    }
  else
    {
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
      primary = g_strdup (message);
    }

  label = gtk_label_new (primary);
  gtk_widget_set_halign (label, GTK_ALIGN_START);
  gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (label),
                      FALSE, TRUE, 0);
  g_free (primary);
  attrs = pango_attr_list_new ();
  pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
  gtk_label_set_attributes (GTK_LABEL (label), attrs);
  pango_attr_list_unref (attrs);

  if (secondary != NULL)
    {
      label = gtk_label_new (secondary);
      gtk_widget_set_halign (label, GTK_ALIGN_START);
      gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
      gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
      gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (label),
599 600
                          FALSE, FALSE, 0);
    }
601

602 603 604 605 606 607
  grid = gtk_grid_new ();
  operation->priv->grid = grid;
  gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
  gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
  gtk_widget_set_margin_bottom (grid, 12);
  gtk_box_pack_start (GTK_BOX (main_vbox), grid, FALSE, FALSE, 0);
608

609
  can_anonymous = priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
610

611 612
  rows = 0;

613
  priv->anonymous_toggle = NULL;
614 615 616 617 618 619
  if (can_anonymous)
    {
      GtkWidget *anon_box;
      GtkWidget *choice;
      GSList    *group;

620 621 622 623 624 625 626 627
      label = gtk_label_new (_("Connect As"));
      gtk_widget_set_halign (label, GTK_ALIGN_END);
      gtk_widget_set_valign (label, GTK_ALIGN_START);
      gtk_widget_set_hexpand (label, FALSE);
      gtk_grid_attach (GTK_GRID (grid), label, 0, rows, 1, 1);

      anon_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_grid_attach (GTK_GRID (grid), anon_box, 1, rows++, 1, 1);
628

629
      choice = gtk_radio_button_new_with_mnemonic (NULL, _("_Anonymous"));
630 631 632 633 634 635 636 637
      gtk_box_pack_start (GTK_BOX (anon_box),
                          choice,
                          FALSE, FALSE, 0);
      g_signal_connect (choice, "toggled",
                        G_CALLBACK (pw_dialog_anonymous_toggled), operation);
      priv->anonymous_toggle = choice;

      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
638
      choice = gtk_radio_button_new_with_mnemonic (group, _("Registered U_ser"));
639 640 641 642 643 644 645
      gtk_box_pack_start (GTK_BOX (anon_box),
                          choice,
                          FALSE, FALSE, 0);
      g_signal_connect (choice, "toggled",
                        G_CALLBACK (pw_dialog_anonymous_toggled), operation);
    }

646
  priv->username_entry = NULL;
647 648

  if (priv->ask_flags & G_ASK_PASSWORD_NEED_USERNAME)
649
    priv->username_entry = table_add_entry (operation, rows++, _("_Username"),
650 651
                                            default_user, operation);

652
  priv->domain_entry = NULL;
653
  if (priv->ask_flags & G_ASK_PASSWORD_NEED_DOMAIN)
654
    priv->domain_entry = table_add_entry (operation, rows++, _("_Domain"),
655 656
                                          default_domain, operation);

657
  priv->password_entry = NULL;
658
  if (priv->ask_flags & G_ASK_PASSWORD_NEED_PASSWORD)
659
    {
660
      priv->password_entry = table_add_entry (operation, rows++, _("_Password"),
661 662 663 664
                                              NULL, operation);
      gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
    }

665
   if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
666
    {
667
      GtkWidget    *remember_box;
668
      GtkWidget    *choice;
669 670
      GSList       *group;
      GPasswordSave password_save;
671

672 673 674 675 676 677
      remember_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
      gtk_grid_attach (GTK_GRID (grid), remember_box, 0, rows++, 2, 1);
      priv->user_widgets = g_list_prepend (priv->user_widgets, remember_box);

      label = gtk_label_new ("");
      gtk_container_add (GTK_CONTAINER (remember_box), label);
678

679
      password_save = g_mount_operation_get_password_save (G_MOUNT_OPERATION (operation));
680
      priv->password_save = password_save;
681

682
      choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
683 684
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
                                    password_save == G_PASSWORD_SAVE_NEVER);
685 686 687 688 689 690 691
      g_object_set_data (G_OBJECT (choice), "password-save",
                         GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
      g_signal_connect (choice, "toggled",
                        G_CALLBACK (remember_button_toggled), operation);
      gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);

      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
692
      choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
693 694
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
                                    password_save == G_PASSWORD_SAVE_FOR_SESSION);
695 696 697 698 699 700 701
      g_object_set_data (G_OBJECT (choice), "password-save",
                         GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
      g_signal_connect (choice, "toggled",
                        G_CALLBACK (remember_button_toggled), operation);
      gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);

      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
702
      choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
703 704
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
                                    password_save == G_PASSWORD_SAVE_PERMANENTLY);
705 706 707 708 709 710 711 712 713 714 715
      g_object_set_data (G_OBJECT (choice), "password-save",
                         GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
      g_signal_connect (choice, "toggled",
                        G_CALLBACK (remember_button_toggled), operation);
      gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
    }

  g_signal_connect (G_OBJECT (dialog), "response",
                    G_CALLBACK (pw_dialog_got_response), operation);

  if (can_anonymous)
716 717 718 719 720 721
    {
      /* The anonymous option will be active by default,
       * ensure the toggled signal is emitted for it.
       */
      gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
    }
722 723 724 725 726
  else if (! pw_dialog_input_is_valid (operation))
    gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);

  g_object_notify (G_OBJECT (operation), "is-showing");

727 728 729 730 731 732
  if (priv->parent_window)
    {
      gtk_window_set_transient_for (window, priv->parent_window);
      gtk_window_set_modal (window, TRUE);
    }
  else if (priv->screen)
733 734 735 736 737 738 739
    gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);

  gtk_widget_show_all (GTK_WIDGET (dialog));

  g_object_ref (operation);
}

740 741 742 743 744
static void
call_password_proxy_cb (GObject      *source,
                        GAsyncResult *res,
                        gpointer      user_data)
{
745
  _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
746 747 748 749 750 751 752 753
  GMountOperation *op = user_data;
  GMountOperationResult result;
  GVariant *result_details;
  GVariantIter iter;
  const gchar *key;
  GVariant *value;
  GError *error = NULL;

754 755 756 757 758
  if (!_gtk_mount_operation_handler_call_ask_password_finish (proxy,
                                                              &result,
                                                              &result_details,
                                                              res,
                                                              &error))
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    {
      result = G_MOUNT_OPERATION_ABORTED;
      g_warning ("Shell mount operation error: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  g_variant_iter_init (&iter, result_details);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
    {
      if (strcmp (key, "password") == 0)
        g_mount_operation_set_password (op, g_variant_get_string (value, NULL));
      else if (strcmp (key, "password_save") == 0)
        g_mount_operation_set_password_save (op, g_variant_get_uint32 (value));
    }

 out:
  gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
}

static void
gtk_mount_operation_ask_password_do_proxy (GtkMountOperation *operation,
                                           const char        *message,
                                           const char        *default_user,
                                           const char        *default_domain)
{
  gchar id[255];
  g_sprintf(id, "GtkMountOperation%p", operation);

  operation->priv->handler_showing = TRUE;
  g_object_notify (G_OBJECT (operation), "is-showing");

  /* keep a ref to the operation while the handler is showing */
  g_object_ref (operation);

794 795 796 797 798
  _gtk_mount_operation_handler_call_ask_password (operation->priv->handler, id,
                                                  message, "drive-harddisk",
                                                  default_user, default_domain,
                                                  operation->priv->ask_flags, NULL,
                                                  call_password_proxy_cb, operation);
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
}

static void
gtk_mount_operation_ask_password (GMountOperation   *mount_op,
                                  const char        *message,
                                  const char        *default_user,
                                  const char        *default_domain,
                                  GAskPasswordFlags  flags)
{
  GtkMountOperation *operation;
  GtkMountOperationPrivate *priv;
  gboolean use_gtk;

  operation = GTK_MOUNT_OPERATION (mount_op);
  priv = operation->priv;
  priv->ask_flags = flags;

  use_gtk = (operation->priv->handler == NULL) ||
    (priv->ask_flags & G_ASK_PASSWORD_NEED_DOMAIN) ||
    (priv->ask_flags & G_ASK_PASSWORD_NEED_USERNAME);

  if (use_gtk)
    gtk_mount_operation_ask_password_do_gtk (operation, message, default_user, default_domain);
  else
    gtk_mount_operation_ask_password_do_proxy (operation, message, default_user, default_domain);
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
static void
question_dialog_button_clicked (GtkDialog       *dialog,
                                gint             button_number,
                                GMountOperation *op)
{
  GtkMountOperationPrivate *priv;
  GtkMountOperation *operation;

  operation = GTK_MOUNT_OPERATION (op);
  priv = operation->priv;

  if (button_number >= 0)
    {
      g_mount_operation_set_choice (op, button_number);
      g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
    }
  else
    g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
844

845 846 847 848 849 850 851
  priv->dialog = NULL;
  g_object_notify (G_OBJECT (operation), "is-showing");
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (op);
}

static void
852 853 854
gtk_mount_operation_ask_question_do_gtk (GtkMountOperation *op,
                                         const char        *message,
                                         const char        *choices[])
855 856 857 858 859 860 861 862 863 864 865
{
  GtkMountOperationPrivate *priv;
  GtkWidget  *dialog;
  const char *secondary = NULL;
  char       *primary;
  int        count, len = 0;

  g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
  g_return_if_fail (message != NULL);
  g_return_if_fail (choices != NULL);

866
  priv = op->priv;
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

  primary = strstr (message, "\n");
  if (primary)
    {
      secondary = primary + 1;
      primary = g_strndup (message, primary - message);
    }

  dialog = gtk_message_dialog_new (priv->parent_window, 0,
                                   GTK_MESSAGE_QUESTION,
                                   GTK_BUTTONS_NONE, "%s",
                                   primary != NULL ? primary : message);
  g_free (primary);

  if (secondary)
    gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                              "%s", secondary);

  /* First count the items in the list then
   * add the buttons in reverse order */

  while (choices[len] != NULL)
    len++;

  for (count = len - 1; count >= 0; count--)
    gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);

  g_signal_connect (G_OBJECT (dialog), "response",
                    G_CALLBACK (question_dialog_button_clicked), op);

  priv->dialog = GTK_DIALOG (dialog);
  g_object_notify (G_OBJECT (op), "is-showing");

  if (priv->parent_window == NULL && priv->screen)
    gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);

  gtk_widget_show (dialog);
  g_object_ref (op);
}

907 908 909 910 911
static void
call_question_proxy_cb (GObject      *source,
                        GAsyncResult *res,
                        gpointer      user_data)
{
912
  _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
913 914 915 916 917 918 919 920
  GMountOperation *op = user_data;
  GMountOperationResult result;
  GVariant *result_details;
  GVariantIter iter;
  const gchar *key;
  GVariant *value;
  GError *error = NULL;

921 922 923 924 925
  if (!_gtk_mount_operation_handler_call_ask_question_finish (proxy,
                                                              &result,
                                                              &result_details,
                                                              res,
                                                              &error))
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
    {
      result = G_MOUNT_OPERATION_ABORTED;
      g_warning ("Shell mount operation error: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  g_variant_iter_init (&iter, result_details);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
    {
      if (strcmp (key, "choice") == 0)
        g_mount_operation_set_choice (op, g_variant_get_int32 (value));
    }
 
 out:
  gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
}

static void
gtk_mount_operation_ask_question_do_proxy (GtkMountOperation *operation,
                                           const char        *message,
                                           const char        *choices[])
{
  gchar id[255];
  g_sprintf(id, "GtkMountOperation%p", operation);

  operation->priv->handler_showing = TRUE;
  g_object_notify (G_OBJECT (operation), "is-showing");

  /* keep a ref to the operation while the handler is showing */
  g_object_ref (operation);

958 959 960 961
  _gtk_mount_operation_handler_call_ask_question (operation->priv->handler, id,
                                                  message, "drive-harddisk",
                                                  choices, NULL,
                                                  call_question_proxy_cb, operation);
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
}

static void
gtk_mount_operation_ask_question (GMountOperation *op,
                                  const char      *message,
                                  const char      *choices[])
{
  GtkMountOperation *operation;
  gboolean use_gtk;

  operation = GTK_MOUNT_OPERATION (op);
  use_gtk = (operation->priv->handler == NULL);

  if (use_gtk)
    gtk_mount_operation_ask_question_do_gtk (operation, message, choices);
  else
    gtk_mount_operation_ask_question_do_proxy (operation, message, choices);
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
static void
show_processes_button_clicked (GtkDialog       *dialog,
                               gint             button_number,
                               GMountOperation *op)
{
  GtkMountOperationPrivate *priv;
  GtkMountOperation *operation;

  operation = GTK_MOUNT_OPERATION (op);
  priv = operation->priv;

  if (button_number >= 0)
    {
      g_mount_operation_set_choice (op, button_number);
      g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
    }
  else
    g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);

  priv->dialog = NULL;
  g_object_notify (G_OBJECT (operation), "is-showing");
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (op);
}

static gint
pid_equal (gconstpointer a,
           gconstpointer b)
{
  GPid pa, pb;

  pa = *((GPid *) a);
  pb = *((GPid *) b);

1015
  return GPOINTER_TO_INT(pb) - GPOINTER_TO_INT(pa);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
}

static void
diff_sorted_arrays (GArray         *array1,
                    GArray         *array2,
                    GCompareFunc   compare,
                    GArray         *added_indices,
                    GArray         *removed_indices)
{
  gint order;
  guint n1, n2;
  guint elem_size;

  n1 = n2 = 0;

  elem_size = g_array_get_element_size (array1);
  g_assert (elem_size == g_array_get_element_size (array2));

  while (n1 < array1->len && n2 < array2->len)
    {
1036 1037
      order = (*compare) (((const char*) array1->data) + n1 * elem_size,
                          ((const char*) array2->data) + n2 * elem_size);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
      if (order < 0)
        {
          g_array_append_val (removed_indices, n1);
          n1++;
        }
      else if (order > 0)
        {
          g_array_append_val (added_indices, n2);
          n2++;
        }
      else
        { /* same item */
          n1++;
          n2++;
        }
    }

  while (n1 < array1->len)
    {
      g_array_append_val (removed_indices, n1);
      n1++;
    }
  while (n2 < array2->len)
    {
      g_array_append_val (added_indices, n2);
      n2++;
    }
}


static void
add_pid_to_process_list_store (GtkMountOperation              *mount_operation,
                               GtkMountOperationLookupContext *lookup_context,
                               GtkListStore                   *list_store,
                               GPid                            pid)
{
  gchar *command_line;
  gchar *name;
  GdkPixbuf *pixbuf;
  gchar *markup;
  GtkTreeIter iter;

  name = NULL;
  pixbuf = NULL;
  command_line = NULL;
  _gtk_mount_operation_lookup_info (lookup_context,
                                    pid,
                                    24,
                                    &name,
                                    &command_line,
                                    &pixbuf);

  if (name == NULL)
LRN's avatar
LRN committed
1091
    name = g_strdup_printf (_("Unknown Application (PID %d)"), (int) pid);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

  if (command_line == NULL)
    command_line = g_strdup ("");

  if (pixbuf == NULL)
    {
      GtkIconTheme *theme;
      theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (mount_operation->priv->dialog)));
      pixbuf = gtk_icon_theme_load_icon (theme,
                                         "application-x-executable",
                                         24,
                                         0,
                                         NULL);
    }

  markup = g_strdup_printf ("<b>%s</b>\n"
                            "<small>%s</small>",
                            name,
                            command_line);

  gtk_list_store_append (list_store, &iter);
  gtk_list_store_set (list_store, &iter,
                      0, pixbuf,
                      1, markup,
                      2, pid,
                      -1);

  if (pixbuf != NULL)
    g_object_unref (pixbuf);
  g_free (markup);
  g_free (name);
  g_free (command_line);
}

static void
remove_pid_from_process_list_store (GtkMountOperation *mount_operation,
                                    GtkListStore      *list_store,
                                    GPid               pid)
{
  GtkTreeIter iter;
  GPid pid_of_item;

  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
    {
      do
        {
          gtk_tree_model_get (GTK_TREE_MODEL (list_store),
                              &iter,
                              2, &pid_of_item,
                              -1);

          if (pid_of_item == pid)
            {
              gtk_list_store_remove (list_store, &iter);
              break;
            }
        }
      while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
    }
}


static void
update_process_list_store (GtkMountOperation *mount_operation,
                           GtkListStore      *list_store,
                           GArray            *processes)
{
  guint n;
  GtkMountOperationLookupContext *lookup_context;
  GArray *current_pids;
  GArray *pid_indices_to_add;
  GArray *pid_indices_to_remove;
  GtkTreeIter iter;
  GPid pid;

  /* Just removing all items and adding new ones will screw up the
   * focus handling in the treeview - so compute the delta, and add/remove
   * items as appropriate
   */
  current_pids = g_array_new (FALSE, FALSE, sizeof (GPid));
  pid_indices_to_add = g_array_new (FALSE, FALSE, sizeof (gint));
  pid_indices_to_remove = g_array_new (FALSE, FALSE, sizeof (gint));

  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
    {
      do
        {
          gtk_tree_model_get (GTK_TREE_MODEL (list_store),
                              &iter,
                              2, &pid,
                              -1);

          g_array_append_val (current_pids, pid);
        }
      while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
    }

  g_array_sort (current_pids, pid_equal);
  g_array_sort (processes, pid_equal);

  diff_sorted_arrays (current_pids, processes, pid_equal, pid_indices_to_add, pid_indices_to_remove);

  for (n = 0; n < pid_indices_to_remove->len; n++)
    {
      pid = g_array_index (current_pids, GPid, n);
      remove_pid_from_process_list_store (mount_operation, list_store, pid);
    }
1199

1200
  if (pid_indices_to_add->len > 0)
1201 1202 1203 1204 1205 1206 1207 1208 1209
    {
      lookup_context = _gtk_mount_operation_lookup_context_get (gtk_widget_get_display (mount_operation->priv->process_tree_view));
      for (n = 0; n < pid_indices_to_add->len; n++)
        {
          pid = g_array_index (processes, GPid, n);
          add_pid_to_process_list_store (mount_operation, lookup_context, list_store, pid);
        }
      _gtk_mount_operation_lookup_context_free (lookup_context);
    }
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

  /* select the first item, if we went from a zero to a non-zero amount of processes */
  if (current_pids->len == 0 && pid_indices_to_add->len > 0)
    {
      if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
        {
          GtkTreeSelection *tree_selection;
          tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mount_operation->priv->process_tree_view));
          gtk_tree_selection_select_iter (tree_selection, &iter);
        }
    }

  g_array_unref (current_pids);
  g_array_unref (pid_indices_to_add);
  g_array_unref (pid_indices_to_remove);
}

static void
on_end_process_activated (GtkMenuItem *item,
                          gpointer user_data)
{
  GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
  GtkTreeSelection *selection;
  GtkTreeIter iter;
  GPid pid_to_kill;
  GError *error;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));

  if (!gtk_tree_selection_get_selected (selection,
                                        NULL,
                                        &iter))
    goto out;

  gtk_tree_model_get (GTK_TREE_MODEL (op->priv->process_list_store),
                      &iter,
                      2, &pid_to_kill,
                      -1);

  /* TODO: We might want to either
   *
   *       - Be smart about things and send SIGKILL rather than SIGTERM if
   *         this is the second time the user requests killing a process
   *
   *       - Or, easier (but worse user experience), offer both "End Process"
   *         and "Terminate Process" options
   *
   *      But that's not how things work right now....
   */
  error = NULL;
  if (!_gtk_mount_operation_kill_process (pid_to_kill, &error))
    {
      GtkWidget *dialog;
      gint response;

      /* Use GTK_DIALOG_DESTROY_WITH_PARENT here since the parent dialog can be
       * indeed be destroyed via the GMountOperation::abort signal... for example,
       * this is triggered if the user yanks the device while we are showing
       * the dialog...
       */
      dialog = gtk_message_dialog_new (GTK_WINDOW (op->priv->dialog),
                                       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                       GTK_MESSAGE_ERROR,
                                       GTK_BUTTONS_CLOSE,
                                       _("Unable to end process"));
      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                "%s",
                                                error->message);

      gtk_widget_show_all (dialog);
      response = gtk_dialog_run (GTK_DIALOG (dialog));

      /* GTK_RESPONSE_NONE means the dialog were programmatically destroy, e.g. that
       * GTK_DIALOG_DESTROY_WITH_PARENT kicked in - so it would trigger a warning to
       * destroy the dialog in that case
       */
      if (response != GTK_RESPONSE_NONE)
        gtk_widget_destroy (dialog);

      g_error_free (error);
    }

 out:
  ;
}

static gboolean
do_popup_menu_for_process_tree_view (GtkWidget         *widget,
                                     GdkEventButton    *event,
                                     GtkMountOperation *op)
{
  GtkWidget *menu;
  GtkWidget *item;
  gint button;
  gint event_time;
  gboolean popped_up_menu;

  popped_up_menu = FALSE;

  menu = gtk_menu_new ();
1310 1311
  gtk_style_context_add_class (gtk_widget_get_style_context (menu),
                               GTK_STYLE_CLASS_CONTEXT_MENU);
1312

1313
  item = gtk_menu_item_new_with_mnemonic (_("_End Process"));
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
  g_signal_connect (item, "activate",
                    G_CALLBACK (on_end_process_activated),
                    op);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
  gtk_widget_show_all (menu);

  if (event != NULL)
    {
      GtkTreePath *path;
      GtkTreeSelection *selection;

      if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (op->priv->process_tree_view),
                                         (gint) event->x,
                                         (gint) event->y,
                                         &path,
                                         NULL,
                                         NULL,
                                         NULL))
        {
          selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
          gtk_tree_selection_select_path (selection, path);
          gtk_tree_path_free (path);
        }
      else
        {
          /* don't popup a menu if the user right-clicked in an area with no rows */
          goto out;
        }

      button = event->button;
      event_time = event->time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_popup (GTK_MENU (menu),
                  NULL,
                  widget,
                  NULL,
                  NULL,
                  button,
                  event_time);

  popped_up_menu = TRUE;

 out:
  return popped_up_menu;
}

static gboolean
on_popup_menu_for_process_tree_view (GtkWidget *widget,
                                     gpointer   user_data)
{
  GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
  return do_popup_menu_for_process_tree_view (widget, NULL, op);
}

static gboolean
on_button_press_event_for_process_tree_view (GtkWidget      *widget,
                                             GdkEventButton *event,
                                             gpointer        user_data)
{
  GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
  gboolean ret;

  ret = FALSE;

1384
  if (gdk_event_triggers_context_menu ((GdkEvent *) event))
1385 1386 1387 1388 1389 1390 1391
    {
      ret = do_popup_menu_for_process_tree_view (widget, event, op);
    }

  return ret;
}

1392
static GtkWidget *
1393
create_show_processes_dialog (GtkMountOperation *op,
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
                              const char      *message,
                              const char      *choices[])
{
  GtkMountOperationPrivate *priv;
  GtkWidget  *dialog;
  const char *secondary = NULL;
  char       *primary;
  int        count, len = 0;
  GtkWidget *label;
  GtkWidget *tree_view;
  GtkWidget *scrolled_window;
  GtkWidget *vbox;
  GtkWidget *content_area;
  GtkTreeViewColumn *column;
  GtkCellRenderer *renderer;
  GtkListStore *list_store;
  gchar *s;
1411
  gboolean use_header;
1412

1413
  priv = op->priv;
1414 1415 1416 1417 1418 1419 1420 1421

  primary = strstr (message, "\n");
  if (primary)
    {
      secondary = primary + 1;
      primary = g_strndup (message, primary - message);
    }

1422 1423 1424 1425 1426 1427
  g_object_get (gtk_settings_get_default (),
                "gtk-dialogs-use-header", &use_header,
                NULL);
  dialog = g_object_new (GTK_TYPE_DIALOG,
                         "use-header-bar", use_header,
                         NULL);
1428 1429 1430 1431 1432 1433

  if (priv->parent_window != NULL)
    gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->parent_window);
  gtk_window_set_title (GTK_WINDOW (dialog), "");

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1434
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
  gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);

  if (secondary != NULL)
    {
      s = g_strdup_printf ("<big><b>%s</b></big>\n\n%s", primary, secondary);
    }
  else
    {
      s = g_strdup_printf ("%s", primary);
    }
  g_free (primary);
  label = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (label), s);
  g_free (s);
  gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);

  /* First count the items in the list then
   * add the buttons in reverse order */

  while (choices[len] != NULL)
    len++;

  for (count = len - 1; count >= 0; count--)
    gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);

  g_signal_connect (G_OBJECT (dialog), "response",
                    G_CALLBACK (show_processes_button_clicked), op);

  priv->dialog = GTK_DIALOG (dialog);
  g_object_notify (G_OBJECT (op), "is-showing");

  if (priv->parent_window == NULL && priv->screen)
    gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);

  tree_view = gtk_tree_view_new ();
  /* TODO: should use EM's when gtk+ RI patches land */
  gtk_widget_set_size_request (tree_view,
                               300,
                               120);

  column = gtk_tree_view_column_new ();
  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "pixbuf", 0,
                                       NULL);
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (renderer,
                "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
                "ellipsize-set", TRUE,
                NULL);
  gtk_tree_view_column_pack_start (column, renderer, TRUE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "markup", 1,
                                       NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);


  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                  GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);

  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);

  g_signal_connect (tree_view, "popup-menu",
                    G_CALLBACK (on_popup_menu_for_process_tree_view),
                    op);
  g_signal_connect (tree_view, "button-press-event",
                    G_CALLBACK (on_button_press_event_for_process_tree_view),
                    op);

  list_store = gtk_list_store_new (3,
                                   GDK_TYPE_PIXBUF,
                                   G_TYPE_STRING,
                                   G_TYPE_INT);

  gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store));

  priv->process_list_store = list_store;
  priv->process_tree_view = tree_view;
  /* set pointers to NULL when dialog goes away */
  g_object_add_weak_pointer (G_OBJECT (list_store), (gpointer *) &priv->process_list_store);
  g_object_add_weak_pointer (G_OBJECT (tree_view), (gpointer *) &priv->process_tree_view);

  g_object_unref (list_store);
  g_object_ref (op);
1526 1527

  return dialog;
1528 1529 1530
}

static void
1531 1532 1533 1534
call_processes_proxy_cb (GObject     *source,
                        GAsyncResult *res,
                        gpointer      user_data)
{
1535
  _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
1536 1537 1538 1539 1540 1541 1542 1543
  GMountOperation *op = user_data;
  GMountOperationResult result;
  GVariant *result_details;
  GVariantIter iter;
  const gchar *key;
  GVariant *value;
  GError *error = NULL;

1544 1545 1546 1547 1548
  if (!_gtk_mount_operation_handler_call_show_processes_finish (proxy,
                                                                &result,
                                                                &result_details,
                                                                res,
                                                                &error))
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    {
      result = G_MOUNT_OPERATION_ABORTED;
      g_warning ("Shell mount operation error: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  /* If the request was unhandled it means we called the method again;
   * in this case, just return and wait for the next response.
   */
  if (result == G_MOUNT_OPERATION_UNHANDLED)
    return;

  g_variant_iter_init (&iter, result_details);
  while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
    {
      if (strcmp (key, "choice") == 0)
        g_mount_operation_set_choice (op, g_variant_get_int32 (value));
    }

 out:
  gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
}

static void
gtk_mount_operation_show_processes_do_proxy (GtkMountOperation *operation,
                                             const char        *message,
                                             GArray            *processes,
                                             const char        *choices[])
{
  gchar id[255];
  g_sprintf(id, "GtkMountOperation%p", operation);

  operation->priv->handler_showing = TRUE;
  g_object_notify (G_OBJECT (operation), "is-showing");

  /* keep a ref to the operation while the handler is showing */
  g_object_ref (operation);

1588 1589 1590 1591 1592 1593 1594
  _gtk_mount_operation_handler_call_show_processes (operation->priv->handler, id,
                                                    message, "drive-harddisk",
                                                    g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
                                                                               processes->data, processes->len,
                                                                               sizeof (GPid)),
                                                    choices, NULL,
                                                    call_processes_proxy_cb, operation);
1595 1596 1597 1598 1599 1600 1601
}

static void
gtk_mount_operation_show_processes_do_gtk (GtkMountOperation *op,
                                           const char        *message,
                                           GArray            *processes,
                                           const char        *choices[])
1602 1603
{
  GtkMountOperationPrivate *priv;
1604
  GtkWidget *dialog = NULL;
1605 1606 1607 1608 1609 1610

  g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
  g_return_if_fail (message != NULL);
  g_return_if_fail (processes != NULL);
  g_return_if_fail (choices != NULL);

1611
  priv = op->priv;
1612 1613 1614 1615

  if (priv->process_list_store == NULL)
    {
      /* need to create the dialog */
1616
      dialog = create_show_processes_dialog (op, message, choices);
1617 1618 1619 1620
    }

  /* otherwise, we're showing the dialog, assume messages+choices hasn't changed */

1621
  update_process_list_store (op,
1622 1623
                             priv->process_list_store,
                             processes);
1624 1625 1626 1627 1628

  if (dialog != NULL)
    {
      gtk_widget_show_all (dialog);
    }
1629 1630
}

1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650

static void
gtk_mount_operation_show_processes (GMountOperation *op,
                                    const char      *message,
                                    GArray          *processes,
                                    const char      *choices[])
{

  GtkMountOperation *operation;
  gboolean use_gtk;

  operation = GTK_MOUNT_OPERATION (op);
  use_gtk = (operation->priv->handler == NULL);

  if (use_gtk)
    gtk_mount_operation_show_processes_do_gtk (operation, message, processes, choices);
  else
    gtk_mount_operation_show_processes_do_proxy (operation, message, processes, choices);
}

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
static void
gtk_mount_operation_aborted (GMountOperation *op)
{
  GtkMountOperationPrivate *priv;

  priv = GTK_MOUNT_OPERATION (op)->priv;

  if (priv->dialog != NULL)
    {
      gtk_widget_destroy (GTK_WIDGET (priv->dialog));
      priv->dialog = NULL;
      g_object_notify (G_OBJECT (op), "is-showing");
      g_object_unref (op);
    }
1665 1666 1667

  if (priv->handler != NULL)
    {
1668
      _gtk_mount_operation_handler_call_close (priv->handler, NULL, NULL, NULL);
1669 1670 1671 1672

      priv->handler_showing = FALSE;
      g_object_notify (G_OBJECT (op), "is-showing");
    }
1673 1674
}

Matthias Clasen's avatar
Matthias Clasen committed
1675 1676
/**
 * gtk_mount_operation_new:
1677
 * @parent: (allow-none): transient parent of the window, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
1678 1679 1680 1681 1682 1683 1684
 *
 * Creates a new #GtkMountOperation
 *
 * Returns: a new #GtkMountOperation
 *
 * Since: 2.14
 */
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
GMountOperation *
gtk_mount_operation_new (GtkWindow *parent)
{
  GMountOperation *mount_operation;

  mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
                                  "parent", parent, NULL);

  return mount_operation;
}

Matthias Clasen's avatar
Matthias Clasen committed
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
/**
 * gtk_mount_operation_is_showing:
 * @op: a #GtkMountOperation
 *
 * Returns whether the #GtkMountOperation is currently displaying
 * a window.
 *
 * Returns: %TRUE if @op is currently displaying a window
 *
 * Since: 2.14
 */
1707 1708 1709 1710 1711 1712 1713 1714
gboolean
gtk_mount_operation_is_showing (GtkMountOperation *op)
{
  g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);

  return op->priv->dialog != NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
1715 1716 1717
/**
 * gtk_mount_operation_set_parent:
 * @op: a #GtkMountOperation
1718
 * @parent: (allow-none): transient parent of the window, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
1719 1720 1721
 *
 * Sets the transient parent for windows shown by the
 * #GtkMountOperation.
1722 1723
 *
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
1724
 */
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
void
gtk_mount_operation_set_parent (GtkMountOperation *op,
                                GtkWindow         *parent)
{
  GtkMountOperationPrivate *priv;

  g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
  g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));

  priv = op->priv;

  if (priv->parent_window == parent)
    return;

  if (priv->parent_window)
    {
      g_signal_handlers_disconnect_by_func (priv->parent_window,
                                            gtk_widget_destroyed,
                                            &priv->parent_window);
1744
      g_object_unref (priv->parent_window);
1745
    }
1746 1747
  priv->parent_window = parent;
  if (priv->parent_window)
1748
    {
1749 1750
      g_object_ref (priv->parent_window);
      g_signal_connect (priv->parent_window, "destroy",
1751 1752 1753 1754
                        G_CALLBACK (gtk_widget_destroyed),
                        &priv->parent_window);
    }

1755 1756 1757
  if (priv->dialog)
    gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), priv->parent_window);

1758 1759 1760
  g_object_notify (G_OBJECT (op), "parent");
}

Matthias Clasen's avatar
Matthias Clasen committed
1761 1762 1763 1764 1765 1766
/**
 * gtk_mount_operation_get_parent:
 * @op: a #GtkMountOperation
 *
 * Gets the transient parent used by the #GtkMountOperation
 *
1767
 * Returns: (transfer none): the transient parent for windows shown by @op
Matthias Clasen's avatar
Matthias Clasen committed
1768 1769 1770
 *
 * Since: 2.14
 */
1771 1772 1773 1774 1775 1776 1777 1778
GtkWindow *
gtk_mount_operation_get_parent (GtkMountOperation *op)
{
  g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);

  return op->priv->parent_window;
}

Matthias Clasen's avatar
Matthias Clasen committed
1779 1780 1781 1782 1783 1784 1785 1786 1787
/**
 * gtk_mount_operation_set_screen:
 * @op: a #GtkMountOperation
 * @screen: a #GdkScreen
 *
 * Sets the screen to show windows of the #GtkMountOperation on.
 *
 * Since: 2.14
 */
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
void
gtk_mount_operation_set_screen (GtkMountOperation *op,
                                GdkScreen         *screen)
{
  GtkMountOperationPrivate *priv;

  g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
  g_return_if_fail (GDK_IS_SCREEN (screen));

  priv = op->priv;

  if (priv->screen == screen)
    return;

  if (priv->screen)
    g_object_unref (priv->screen);

  priv->screen = g_object_ref (screen);

  if (priv->dialog)
    gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);

  g_object_notify (G_OBJECT (op), "screen");
}

Matthias Clasen's avatar
Matthias Clasen committed
1813 1814 1815 1816
/**
 * gtk_mount_operation_get_screen:
 * @op: a #GtkMountOperation
 *
1817
 * Gets the screen on which windows of the #GtkMountOperation
Matthias Clasen's avatar
Matthias Clasen committed
1818 1819
 * will be shown.
 *
1820
 * Returns: (transfer none): the screen on which windows of @op are shown
Matthias Clasen's avatar
Matthias Clasen committed
1821 1822 1823
 *
 * Since: 2.14
 */
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
GdkScreen *
gtk_mount_operation_get_screen (GtkMountOperation *op)
{
  GtkMountOperationPrivate *priv;

  g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);

  priv = op->priv;

  if (priv->dialog)
    return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
  else if (priv->parent_window)
    return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
  else if (priv->screen)
    return priv->screen;
  else
    return gdk_screen_get_default ();
}