]> Pileus Git - ~andy/gtk/blob - gtk/gtkmountoperation.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkmountoperation.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) Christian Kellner <gicmo@gnome.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21  * file for a list of people on the GTK+ Team.  See the ChangeLog
22  * files for a list of changes.  These files are distributed with
23  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "gtkmountoperationprivate.h"
31 #include "gtkbox.h"
32 #include "gtkdbusgenerated.h"
33 #include "gtkentry.h"
34 #include "gtkbox.h"
35 #include "gtkintl.h"
36 #include "gtklabel.h"
37 #include "gtkmessagedialog.h"
38 #include "gtkmountoperation.h"
39 #include "gtkprivate.h"
40 #include "gtkradiobutton.h"
41 #include "gtkstock.h"
42 #include "gtkgrid.h"
43 #include "gtkwindow.h"
44 #include "gtktreeview.h"
45 #include "gtktreeselection.h"
46 #include "gtkcellrenderertext.h"
47 #include "gtkcellrendererpixbuf.h"
48 #include "gtkscrolledwindow.h"
49 #include "gtkicontheme.h"
50 #include "gtkimagemenuitem.h"
51 #include "gtkmain.h"
52
53 #include <glib/gprintf.h>
54
55 /**
56  * SECTION:filesystem
57  * @short_description: Functions for working with GIO
58  * @Title: Filesystem utilities
59  *
60  * The functions and objects described here make working with GTK+ and
61  * GIO more convenient.
62  *
63  * #GtkMountOperation is needed when mounting volumes:
64  * It is an implementation of #GMountOperation that can be used with
65  * 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.
68  *
69  * When necessary, #GtkMountOperation shows dialogs to ask for
70  * passwords, questions or show processes blocking unmount.
71  *
72  * gtk_show_uri() is a convenient way to launch applications for URIs.
73  *
74  * Another object that is worth mentioning in this context is
75  * #GdkAppLaunchContext, which provides visual feedback when lauching
76  * applications.
77  */
78
79 static void   gtk_mount_operation_finalize     (GObject          *object);
80 static void   gtk_mount_operation_set_property (GObject          *object,
81                                                 guint             prop_id,
82                                                 const GValue     *value,
83                                                 GParamSpec       *pspec);
84 static void   gtk_mount_operation_get_property (GObject          *object,
85                                                 guint             prop_id,
86                                                 GValue           *value,
87                                                 GParamSpec       *pspec);
88
89 static void   gtk_mount_operation_ask_password (GMountOperation *op,
90                                                 const char      *message,
91                                                 const char      *default_user,
92                                                 const char      *default_domain,
93                                                 GAskPasswordFlags flags);
94
95 static void   gtk_mount_operation_ask_question (GMountOperation *op,
96                                                 const char      *message,
97                                                 const char      *choices[]);
98
99 static void   gtk_mount_operation_show_processes (GMountOperation *op,
100                                                   const char      *message,
101                                                   GArray          *processes,
102                                                   const char      *choices[]);
103
104 static void   gtk_mount_operation_aborted      (GMountOperation *op);
105
106 G_DEFINE_TYPE (GtkMountOperation, gtk_mount_operation, G_TYPE_MOUNT_OPERATION);
107
108 enum {
109   PROP_0,
110   PROP_PARENT,
111   PROP_IS_SHOWING,
112   PROP_SCREEN
113
114 };
115
116 struct _GtkMountOperationPrivate {
117   GtkWindow *parent_window;
118   GtkDialog *dialog;
119   GdkScreen *screen;
120
121   /* bus proxy */
122   _GtkMountOperationHandler *handler;
123   GCancellable *cancellable;
124   gboolean handler_showing;
125
126   /* for the ask-password dialog */
127   GtkWidget *grid;
128   GtkWidget *username_entry;
129   GtkWidget *domain_entry;
130   GtkWidget *password_entry;
131   GtkWidget *anonymous_toggle;
132   GList *user_widgets;
133
134   GAskPasswordFlags ask_flags;
135   GPasswordSave     password_save;
136   gboolean          anonymous;
137
138   /* for the show-processes dialog */
139   GtkWidget *process_tree_view;
140   GtkListStore *process_list_store;
141 };
142
143 static void
144 gtk_mount_operation_class_init (GtkMountOperationClass *klass)
145 {
146   GObjectClass         *object_class = G_OBJECT_CLASS (klass);
147   GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
148
149   g_type_class_add_private (klass, sizeof (GtkMountOperationPrivate));
150
151   object_class->finalize     = gtk_mount_operation_finalize;
152   object_class->get_property = gtk_mount_operation_get_property;
153   object_class->set_property = gtk_mount_operation_set_property;
154
155   mount_op_class->ask_password = gtk_mount_operation_ask_password;
156   mount_op_class->ask_question = gtk_mount_operation_ask_question;
157   mount_op_class->show_processes = gtk_mount_operation_show_processes;
158   mount_op_class->aborted = gtk_mount_operation_aborted;
159
160   g_object_class_install_property (object_class,
161                                    PROP_PARENT,
162                                    g_param_spec_object ("parent",
163                                                         P_("Parent"),
164                                                         P_("The parent window"),
165                                                         GTK_TYPE_WINDOW,
166                                                         GTK_PARAM_READWRITE));
167
168   g_object_class_install_property (object_class,
169                                    PROP_IS_SHOWING,
170                                    g_param_spec_boolean ("is-showing",
171                                                          P_("Is Showing"),
172                                                          P_("Are we showing a dialog"),
173                                                          FALSE,
174                                                          GTK_PARAM_READABLE));
175
176   g_object_class_install_property (object_class,
177                                    PROP_SCREEN,
178                                    g_param_spec_object ("screen",
179                                                         P_("Screen"),
180                                                         P_("The screen where this window will be displayed."),
181                                                         GDK_TYPE_SCREEN,
182                                                         GTK_PARAM_READWRITE));
183 }
184
185 static void
186 gtk_mount_operation_init (GtkMountOperation *operation)
187 {
188   gchar *name_owner;
189
190   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
191                                                  GTK_TYPE_MOUNT_OPERATION,
192                                                  GtkMountOperationPrivate);
193
194   operation->priv->handler =
195     _gtk_mount_operation_handler_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
196                                                          G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
197                                                          "org.gtk.MountOperationHandler",
198                                                          "/org/gtk/MountOperationHandler",
199                                                          NULL, NULL);
200   name_owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (operation->priv->handler));
201   if (!name_owner)
202     g_clear_object (&operation->priv->handler);
203   g_free (name_owner);
204
205   if (operation->priv->handler)
206     g_dbus_proxy_set_default_timeout (G_DBUS_PROXY (operation->priv->handler), G_MAXINT);
207 }
208
209 static void
210 gtk_mount_operation_finalize (GObject *object)
211 {
212   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
213   GtkMountOperationPrivate *priv = operation->priv;
214
215   if (priv->user_widgets)
216     g_list_free (priv->user_widgets);
217
218   if (priv->parent_window)
219     g_object_unref (priv->parent_window);
220
221   if (priv->screen)
222     g_object_unref (priv->screen);
223
224   if (priv->handler)
225     g_object_unref (priv->handler);
226
227   G_OBJECT_CLASS (gtk_mount_operation_parent_class)->finalize (object);
228 }
229
230 static void
231 gtk_mount_operation_set_property (GObject      *object,
232                                   guint         prop_id,
233                                   const GValue *value,
234                                   GParamSpec   *pspec)
235 {
236   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
237
238   switch (prop_id)
239     {
240     case PROP_PARENT:
241       gtk_mount_operation_set_parent (operation, g_value_get_object (value));
242       break;
243
244     case PROP_SCREEN:
245       gtk_mount_operation_set_screen (operation, g_value_get_object (value));
246       break;
247
248     case PROP_IS_SHOWING:
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252     }
253 }
254
255 static void
256 gtk_mount_operation_get_property (GObject    *object,
257                                   guint       prop_id,
258                                   GValue     *value,
259                                   GParamSpec *pspec)
260 {
261   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
262   GtkMountOperationPrivate *priv = operation->priv;
263
264   switch (prop_id)
265     {
266     case PROP_PARENT:
267       g_value_set_object (value, priv->parent_window);
268       break;
269
270     case PROP_IS_SHOWING:
271       g_value_set_boolean (value, priv->dialog != NULL || priv->handler_showing);
272       break;
273
274     case PROP_SCREEN:
275       g_value_set_object (value, priv->screen);
276       break;
277
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281     }
282 }
283
284 static void
285 gtk_mount_operation_proxy_finish (GtkMountOperation     *op,
286                                   GMountOperationResult  result)
287 {
288   _gtk_mount_operation_handler_call_close (op->priv->handler, NULL, NULL, NULL);
289
290   op->priv->handler_showing = FALSE;
291   g_object_notify (G_OBJECT (op), "is-showing");
292
293   g_mount_operation_reply (G_MOUNT_OPERATION (op), result);
294
295   /* drop the reference acquired when calling the proxy method */
296   g_object_unref (op);
297 }
298
299 static void
300 remember_button_toggled (GtkToggleButton   *button,
301                          GtkMountOperation *operation)
302 {
303   GtkMountOperationPrivate *priv = operation->priv;
304
305   if (gtk_toggle_button_get_active (button))
306     {
307       gpointer data;
308
309       data = g_object_get_data (G_OBJECT (button), "password-save");
310       priv->password_save = GPOINTER_TO_INT (data);
311     }
312 }
313
314 static void
315 pw_dialog_got_response (GtkDialog         *dialog,
316                         gint               response_id,
317                         GtkMountOperation *mount_op)
318 {
319   GtkMountOperationPrivate *priv = mount_op->priv;
320   GMountOperation *op = G_MOUNT_OPERATION (mount_op);
321
322   if (response_id == GTK_RESPONSE_OK)
323     {
324       const char *text;
325
326       if (priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED)
327         g_mount_operation_set_anonymous (op, priv->anonymous);
328
329       if (priv->username_entry)
330         {
331           text = gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
332           g_mount_operation_set_username (op, text);
333         }
334
335       if (priv->domain_entry)
336         {
337           text = gtk_entry_get_text (GTK_ENTRY (priv->domain_entry));
338           g_mount_operation_set_domain (op, text);
339         }
340
341       if (priv->password_entry)
342         {
343           text = gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
344           g_mount_operation_set_password (op, text);
345         }
346
347       if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
348         g_mount_operation_set_password_save (op, priv->password_save);
349
350       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
351     }
352   else
353     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
354
355   priv->dialog = NULL;
356   g_object_notify (G_OBJECT (op), "is-showing");
357   gtk_widget_destroy (GTK_WIDGET (dialog));
358   g_object_unref (op);
359 }
360
361 static gboolean
362 entry_has_input (GtkWidget *entry_widget)
363 {
364   const char *text;
365
366   if (entry_widget == NULL)
367     return TRUE;
368
369   text = gtk_entry_get_text (GTK_ENTRY (entry_widget));
370
371   return text != NULL && text[0] != '\0';
372 }
373
374 static gboolean
375 pw_dialog_input_is_valid (GtkMountOperation *operation)
376 {
377   GtkMountOperationPrivate *priv = operation->priv;
378   gboolean is_valid = TRUE;
379
380   /* We don't require password to be non-empty here
381    * since there are situations where it is not needed,
382    * see bug 578365.
383    * We may add a way for the backend to specify that it
384    * definitively needs a password.
385    */
386   is_valid = entry_has_input (priv->username_entry) &&
387              entry_has_input (priv->domain_entry);
388
389   return is_valid;
390 }
391
392 static void
393 pw_dialog_verify_input (GtkEditable       *editable,
394                         GtkMountOperation *operation)
395 {
396   GtkMountOperationPrivate *priv = operation->priv;
397   gboolean is_valid;
398
399   is_valid = pw_dialog_input_is_valid (operation);
400   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
401                                      GTK_RESPONSE_OK,
402                                      is_valid);
403 }
404
405 static void
406 pw_dialog_anonymous_toggled (GtkWidget         *widget,
407                              GtkMountOperation *operation)
408 {
409   GtkMountOperationPrivate *priv = operation->priv;
410   gboolean is_valid;
411   GList *l;
412
413   priv->anonymous = widget == priv->anonymous_toggle;
414
415   if (priv->anonymous)
416     is_valid = TRUE;
417   else
418     is_valid = pw_dialog_input_is_valid (operation);
419
420   for (l = priv->user_widgets; l != NULL; l = l->next)
421     {
422       gtk_widget_set_sensitive (GTK_WIDGET (l->data), !priv->anonymous);
423     }
424
425   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
426                                      GTK_RESPONSE_OK,
427                                      is_valid);
428 }
429
430
431 static void
432 pw_dialog_cycle_focus (GtkWidget         *widget,
433                        GtkMountOperation *operation)
434 {
435   GtkMountOperationPrivate *priv;
436   GtkWidget *next_widget = NULL;
437
438   priv = operation->priv;
439
440   if (widget == priv->username_entry)
441     {
442       if (priv->domain_entry != NULL)
443         next_widget = priv->domain_entry;
444       else if (priv->password_entry != NULL)
445         next_widget = priv->password_entry;
446     }
447   else if (widget == priv->domain_entry && priv->password_entry)
448     next_widget = priv->password_entry;
449
450   if (next_widget)
451     gtk_widget_grab_focus (next_widget);
452   else if (pw_dialog_input_is_valid (operation))
453     gtk_window_activate_default (GTK_WINDOW (priv->dialog));
454 }
455
456 static GtkWidget *
457 table_add_entry (GtkMountOperation *operation,
458                  int                row,
459                  const char        *label_text,
460                  const char        *value,
461                  gpointer           user_data)
462 {
463   GtkWidget *entry;
464   GtkWidget *label;
465
466   label = gtk_label_new_with_mnemonic (label_text);
467   gtk_widget_set_halign (label, GTK_ALIGN_END);
468   gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
469   gtk_widget_set_hexpand (label, FALSE);
470   operation->priv->user_widgets = g_list_prepend (operation->priv->user_widgets, label);
471
472   entry = gtk_entry_new ();
473   gtk_widget_set_hexpand (entry, TRUE);
474
475   if (value)
476     gtk_entry_set_text (GTK_ENTRY (entry), value);
477
478   gtk_grid_attach (GTK_GRID (operation->priv->grid), label, 0, row, 1, 1);
479   gtk_grid_attach (GTK_GRID (operation->priv->grid), entry, 1, row, 1, 1);
480   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
481   operation->priv->user_widgets = g_list_prepend (operation->priv->user_widgets, entry);
482
483   g_signal_connect (entry, "changed",
484                     G_CALLBACK (pw_dialog_verify_input), user_data);
485
486   g_signal_connect (entry, "activate",
487                     G_CALLBACK (pw_dialog_cycle_focus), user_data);
488
489   return entry;
490 }
491
492 static void
493 gtk_mount_operation_ask_password_do_gtk (GtkMountOperation *operation,
494                                          const gchar       *message,
495                                          const gchar       *default_user,
496                                          const gchar       *default_domain)
497 {
498   GtkMountOperationPrivate *priv;
499   GtkWidget *widget;
500   GtkDialog *dialog;
501   GtkWindow *window;
502   GtkWidget *hbox, *main_vbox, *icon;
503   GtkWidget *grid;
504   GtkWidget *label;
505   GtkWidget *content_area, *action_area;
506   gboolean   can_anonymous;
507   guint      rows;
508   gchar *primary;
509   const gchar *secondary;
510   PangoAttrList *attrs;
511
512   priv = operation->priv;
513
514   widget = gtk_dialog_new ();
515   dialog = GTK_DIALOG (widget);
516   window = GTK_WINDOW (widget);
517
518   priv->dialog = dialog;
519
520   content_area = gtk_dialog_get_content_area (dialog);
521   action_area = gtk_dialog_get_action_area (dialog);
522
523   /* Set the dialog up with HIG properties */
524   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
525   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
526   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
527   gtk_box_set_spacing (GTK_BOX (action_area), 6);
528
529   gtk_window_set_resizable (window, FALSE);
530   gtk_window_set_title (window, "");
531   gtk_window_set_icon_name (window, GTK_STOCK_DIALOG_AUTHENTICATION);
532
533   gtk_dialog_add_buttons (dialog,
534                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
535                           _("Co_nnect"), GTK_RESPONSE_OK,
536                           NULL);
537   gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
538
539   gtk_dialog_set_alternative_button_order (dialog,
540                                            GTK_RESPONSE_OK,
541                                            GTK_RESPONSE_CANCEL,
542                                            -1);
543
544   /* Build contents */
545   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
546   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
547   gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
548
549   icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
550                                    GTK_ICON_SIZE_DIALOG);
551
552   gtk_widget_set_halign (icon, GTK_ALIGN_CENTER);
553   gtk_widget_set_valign (icon, GTK_ALIGN_START);
554   gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
555
556   main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
557   gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
558
559   secondary = strstr (message, "\n");
560   if (secondary != NULL)
561     {
562       primary = g_strndup (message, secondary - message + 1);
563     }
564   else
565     {
566       primary = g_strdup (message);
567     }
568
569   label = gtk_label_new (primary);
570   gtk_widget_set_halign (label, GTK_ALIGN_START);
571   gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
572   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
573   gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (label),
574                       FALSE, TRUE, 0);
575   g_free (primary);
576   attrs = pango_attr_list_new ();
577   pango_attr_list_insert (attrs, pango_attr_weight_new (PANGO_WEIGHT_BOLD));
578   gtk_label_set_attributes (GTK_LABEL (label), attrs);
579   pango_attr_list_unref (attrs);
580
581   if (secondary != NULL)
582     {
583       label = gtk_label_new (secondary);
584       gtk_widget_set_halign (label, GTK_ALIGN_START);
585       gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
586       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
587       gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (label),
588                           FALSE, FALSE, 0);
589     }
590
591   grid = gtk_grid_new ();
592   operation->priv->grid = grid;
593   gtk_grid_set_row_spacing (GTK_GRID (grid), 12);
594   gtk_grid_set_column_spacing (GTK_GRID (grid), 12);
595   gtk_widget_set_margin_bottom (grid, 12);
596   gtk_box_pack_start (GTK_BOX (main_vbox), grid, FALSE, FALSE, 0);
597
598   can_anonymous = priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
599
600   rows = 0;
601
602   priv->anonymous_toggle = NULL;
603   if (can_anonymous)
604     {
605       GtkWidget *anon_box;
606       GtkWidget *choice;
607       GSList    *group;
608
609       label = gtk_label_new (_("Connect As"));
610       gtk_widget_set_halign (label, GTK_ALIGN_END);
611       gtk_widget_set_valign (label, GTK_ALIGN_START);
612       gtk_widget_set_hexpand (label, FALSE);
613       gtk_grid_attach (GTK_GRID (grid), label, 0, rows, 1, 1);
614
615       anon_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
616       gtk_grid_attach (GTK_GRID (grid), anon_box, 1, rows++, 1, 1);
617
618       choice = gtk_radio_button_new_with_mnemonic (NULL, _("_Anonymous"));
619       gtk_box_pack_start (GTK_BOX (anon_box),
620                           choice,
621                           FALSE, FALSE, 0);
622       g_signal_connect (choice, "toggled",
623                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
624       priv->anonymous_toggle = choice;
625
626       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
627       choice = gtk_radio_button_new_with_mnemonic (group, _("Registered U_ser"));
628       gtk_box_pack_start (GTK_BOX (anon_box),
629                           choice,
630                           FALSE, FALSE, 0);
631       g_signal_connect (choice, "toggled",
632                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
633     }
634
635   priv->username_entry = NULL;
636
637   if (priv->ask_flags & G_ASK_PASSWORD_NEED_USERNAME)
638     priv->username_entry = table_add_entry (operation, rows++, _("_Username"),
639                                             default_user, operation);
640
641   priv->domain_entry = NULL;
642   if (priv->ask_flags & G_ASK_PASSWORD_NEED_DOMAIN)
643     priv->domain_entry = table_add_entry (operation, rows++, _("_Domain"),
644                                           default_domain, operation);
645
646   priv->password_entry = NULL;
647   if (priv->ask_flags & G_ASK_PASSWORD_NEED_PASSWORD)
648     {
649       priv->password_entry = table_add_entry (operation, rows++, _("_Password"),
650                                               NULL, operation);
651       gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
652     }
653
654    if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
655     {
656       GtkWidget    *remember_box;
657       GtkWidget    *choice;
658       GSList       *group;
659       GPasswordSave password_save;
660
661       remember_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
662       gtk_grid_attach (GTK_GRID (grid), remember_box, 0, rows++, 2, 1);
663       priv->user_widgets = g_list_prepend (priv->user_widgets, remember_box);
664
665       label = gtk_label_new ("");
666       gtk_container_add (GTK_CONTAINER (remember_box), label);
667
668       password_save = g_mount_operation_get_password_save (G_MOUNT_OPERATION (operation));
669       priv->password_save = password_save;
670
671       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
672       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
673                                     password_save == G_PASSWORD_SAVE_NEVER);
674       g_object_set_data (G_OBJECT (choice), "password-save",
675                          GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
676       g_signal_connect (choice, "toggled",
677                         G_CALLBACK (remember_button_toggled), operation);
678       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
679
680       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
681       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
682       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
683                                     password_save == G_PASSWORD_SAVE_FOR_SESSION);
684       g_object_set_data (G_OBJECT (choice), "password-save",
685                          GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
686       g_signal_connect (choice, "toggled",
687                         G_CALLBACK (remember_button_toggled), operation);
688       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
689
690       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
691       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
692       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
693                                     password_save == G_PASSWORD_SAVE_PERMANENTLY);
694       g_object_set_data (G_OBJECT (choice), "password-save",
695                          GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
696       g_signal_connect (choice, "toggled",
697                         G_CALLBACK (remember_button_toggled), operation);
698       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
699     }
700
701   g_signal_connect (G_OBJECT (dialog), "response",
702                     G_CALLBACK (pw_dialog_got_response), operation);
703
704   if (can_anonymous)
705     {
706       /* The anonymous option will be active by default,
707        * ensure the toggled signal is emitted for it.
708        */
709       gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
710     }
711   else if (! pw_dialog_input_is_valid (operation))
712     gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
713
714   g_object_notify (G_OBJECT (operation), "is-showing");
715
716   if (priv->parent_window)
717     {
718       gtk_window_set_transient_for (window, priv->parent_window);
719       gtk_window_set_modal (window, TRUE);
720     }
721   else if (priv->screen)
722     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
723
724   gtk_widget_show_all (GTK_WIDGET (dialog));
725
726   g_object_ref (operation);
727 }
728
729 static void
730 call_password_proxy_cb (GObject      *source,
731                         GAsyncResult *res,
732                         gpointer      user_data)
733 {
734   _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
735   GMountOperation *op = user_data;
736   GMountOperationResult result;
737   GVariant *result_details;
738   GVariantIter iter;
739   const gchar *key;
740   GVariant *value;
741   GError *error = NULL;
742
743   if (!_gtk_mount_operation_handler_call_ask_password_finish (proxy,
744                                                               &result,
745                                                               &result_details,
746                                                               res,
747                                                               &error))
748     {
749       result = G_MOUNT_OPERATION_ABORTED;
750       g_warning ("Shell mount operation error: %s\n", error->message);
751       g_error_free (error);
752       goto out;
753     }
754
755   g_variant_iter_init (&iter, result_details);
756   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
757     {
758       if (strcmp (key, "password") == 0)
759         g_mount_operation_set_password (op, g_variant_get_string (value, NULL));
760       else if (strcmp (key, "password_save") == 0)
761         g_mount_operation_set_password_save (op, g_variant_get_uint32 (value));
762     }
763
764  out:
765   gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
766 }
767
768 static void
769 gtk_mount_operation_ask_password_do_proxy (GtkMountOperation *operation,
770                                            const char        *message,
771                                            const char        *default_user,
772                                            const char        *default_domain)
773 {
774   gchar id[255];
775   g_sprintf(id, "GtkMountOperation%p", operation);
776
777   operation->priv->handler_showing = TRUE;
778   g_object_notify (G_OBJECT (operation), "is-showing");
779
780   /* keep a ref to the operation while the handler is showing */
781   g_object_ref (operation);
782
783   _gtk_mount_operation_handler_call_ask_password (operation->priv->handler, id,
784                                                   message, "drive-harddisk",
785                                                   default_user, default_domain,
786                                                   operation->priv->ask_flags, NULL,
787                                                   call_password_proxy_cb, operation);
788 }
789
790 static void
791 gtk_mount_operation_ask_password (GMountOperation   *mount_op,
792                                   const char        *message,
793                                   const char        *default_user,
794                                   const char        *default_domain,
795                                   GAskPasswordFlags  flags)
796 {
797   GtkMountOperation *operation;
798   GtkMountOperationPrivate *priv;
799   gboolean use_gtk;
800
801   operation = GTK_MOUNT_OPERATION (mount_op);
802   priv = operation->priv;
803   priv->ask_flags = flags;
804
805   use_gtk = (operation->priv->handler == NULL) ||
806     (priv->ask_flags & G_ASK_PASSWORD_NEED_DOMAIN) ||
807     (priv->ask_flags & G_ASK_PASSWORD_NEED_USERNAME);
808
809   if (use_gtk)
810     gtk_mount_operation_ask_password_do_gtk (operation, message, default_user, default_domain);
811   else
812     gtk_mount_operation_ask_password_do_proxy (operation, message, default_user, default_domain);
813 }
814
815 static void
816 question_dialog_button_clicked (GtkDialog       *dialog,
817                                 gint             button_number,
818                                 GMountOperation *op)
819 {
820   GtkMountOperationPrivate *priv;
821   GtkMountOperation *operation;
822
823   operation = GTK_MOUNT_OPERATION (op);
824   priv = operation->priv;
825
826   if (button_number >= 0)
827     {
828       g_mount_operation_set_choice (op, button_number);
829       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
830     }
831   else
832     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
833
834   priv->dialog = NULL;
835   g_object_notify (G_OBJECT (operation), "is-showing");
836   gtk_widget_destroy (GTK_WIDGET (dialog));
837   g_object_unref (op);
838 }
839
840 static void
841 gtk_mount_operation_ask_question_do_gtk (GtkMountOperation *op,
842                                          const char        *message,
843                                          const char        *choices[])
844 {
845   GtkMountOperationPrivate *priv;
846   GtkWidget  *dialog;
847   const char *secondary = NULL;
848   char       *primary;
849   int        count, len = 0;
850
851   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
852   g_return_if_fail (message != NULL);
853   g_return_if_fail (choices != NULL);
854
855   priv = op->priv;
856
857   primary = strstr (message, "\n");
858   if (primary)
859     {
860       secondary = primary + 1;
861       primary = g_strndup (message, primary - message);
862     }
863
864   dialog = gtk_message_dialog_new (priv->parent_window, 0,
865                                    GTK_MESSAGE_QUESTION,
866                                    GTK_BUTTONS_NONE, "%s",
867                                    primary != NULL ? primary : message);
868   g_free (primary);
869
870   if (secondary)
871     gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
872                                               "%s", secondary);
873
874   /* First count the items in the list then
875    * add the buttons in reverse order */
876
877   while (choices[len] != NULL)
878     len++;
879
880   for (count = len - 1; count >= 0; count--)
881     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
882
883   g_signal_connect (G_OBJECT (dialog), "response",
884                     G_CALLBACK (question_dialog_button_clicked), op);
885
886   priv->dialog = GTK_DIALOG (dialog);
887   g_object_notify (G_OBJECT (op), "is-showing");
888
889   if (priv->parent_window == NULL && priv->screen)
890     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
891
892   gtk_widget_show (dialog);
893   g_object_ref (op);
894 }
895
896 static void
897 call_question_proxy_cb (GObject      *source,
898                         GAsyncResult *res,
899                         gpointer      user_data)
900 {
901   _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
902   GMountOperation *op = user_data;
903   GMountOperationResult result;
904   GVariant *result_details;
905   GVariantIter iter;
906   const gchar *key;
907   GVariant *value;
908   GError *error = NULL;
909
910   if (!_gtk_mount_operation_handler_call_ask_question_finish (proxy,
911                                                               &result,
912                                                               &result_details,
913                                                               res,
914                                                               &error))
915     {
916       result = G_MOUNT_OPERATION_ABORTED;
917       g_warning ("Shell mount operation error: %s\n", error->message);
918       g_error_free (error);
919       goto out;
920     }
921
922   g_variant_iter_init (&iter, result_details);
923   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
924     {
925       if (strcmp (key, "choice") == 0)
926         g_mount_operation_set_choice (op, g_variant_get_int32 (value));
927     }
928  
929  out:
930   gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
931 }
932
933 static void
934 gtk_mount_operation_ask_question_do_proxy (GtkMountOperation *operation,
935                                            const char        *message,
936                                            const char        *choices[])
937 {
938   gchar id[255];
939   g_sprintf(id, "GtkMountOperation%p", operation);
940
941   operation->priv->handler_showing = TRUE;
942   g_object_notify (G_OBJECT (operation), "is-showing");
943
944   /* keep a ref to the operation while the handler is showing */
945   g_object_ref (operation);
946
947   _gtk_mount_operation_handler_call_ask_question (operation->priv->handler, id,
948                                                   message, "drive-harddisk",
949                                                   choices, NULL,
950                                                   call_question_proxy_cb, operation);
951 }
952
953 static void
954 gtk_mount_operation_ask_question (GMountOperation *op,
955                                   const char      *message,
956                                   const char      *choices[])
957 {
958   GtkMountOperation *operation;
959   gboolean use_gtk;
960
961   operation = GTK_MOUNT_OPERATION (op);
962   use_gtk = (operation->priv->handler == NULL);
963
964   if (use_gtk)
965     gtk_mount_operation_ask_question_do_gtk (operation, message, choices);
966   else
967     gtk_mount_operation_ask_question_do_proxy (operation, message, choices);
968 }
969
970 static void
971 show_processes_button_clicked (GtkDialog       *dialog,
972                                gint             button_number,
973                                GMountOperation *op)
974 {
975   GtkMountOperationPrivate *priv;
976   GtkMountOperation *operation;
977
978   operation = GTK_MOUNT_OPERATION (op);
979   priv = operation->priv;
980
981   if (button_number >= 0)
982     {
983       g_mount_operation_set_choice (op, button_number);
984       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
985     }
986   else
987     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
988
989   priv->dialog = NULL;
990   g_object_notify (G_OBJECT (operation), "is-showing");
991   gtk_widget_destroy (GTK_WIDGET (dialog));
992   g_object_unref (op);
993 }
994
995 static gint
996 pid_equal (gconstpointer a,
997            gconstpointer b)
998 {
999   GPid pa, pb;
1000
1001   pa = *((GPid *) a);
1002   pb = *((GPid *) b);
1003
1004   return GPOINTER_TO_INT(pb) - GPOINTER_TO_INT(pa);
1005 }
1006
1007 static void
1008 diff_sorted_arrays (GArray         *array1,
1009                     GArray         *array2,
1010                     GCompareFunc   compare,
1011                     GArray         *added_indices,
1012                     GArray         *removed_indices)
1013 {
1014   gint order;
1015   guint n1, n2;
1016   guint elem_size;
1017
1018   n1 = n2 = 0;
1019
1020   elem_size = g_array_get_element_size (array1);
1021   g_assert (elem_size == g_array_get_element_size (array2));
1022
1023   while (n1 < array1->len && n2 < array2->len)
1024     {
1025       order = (*compare) (((const char*) array1->data) + n1 * elem_size,
1026                           ((const char*) array2->data) + n2 * elem_size);
1027       if (order < 0)
1028         {
1029           g_array_append_val (removed_indices, n1);
1030           n1++;
1031         }
1032       else if (order > 0)
1033         {
1034           g_array_append_val (added_indices, n2);
1035           n2++;
1036         }
1037       else
1038         { /* same item */
1039           n1++;
1040           n2++;
1041         }
1042     }
1043
1044   while (n1 < array1->len)
1045     {
1046       g_array_append_val (removed_indices, n1);
1047       n1++;
1048     }
1049   while (n2 < array2->len)
1050     {
1051       g_array_append_val (added_indices, n2);
1052       n2++;
1053     }
1054 }
1055
1056
1057 static void
1058 add_pid_to_process_list_store (GtkMountOperation              *mount_operation,
1059                                GtkMountOperationLookupContext *lookup_context,
1060                                GtkListStore                   *list_store,
1061                                GPid                            pid)
1062 {
1063   gchar *command_line;
1064   gchar *name;
1065   GdkPixbuf *pixbuf;
1066   gchar *markup;
1067   GtkTreeIter iter;
1068
1069   name = NULL;
1070   pixbuf = NULL;
1071   command_line = NULL;
1072   _gtk_mount_operation_lookup_info (lookup_context,
1073                                     pid,
1074                                     24,
1075                                     &name,
1076                                     &command_line,
1077                                     &pixbuf);
1078
1079   if (name == NULL)
1080     name = g_strdup_printf (_("Unknown Application (PID %d)"), pid);
1081
1082   if (command_line == NULL)
1083     command_line = g_strdup ("");
1084
1085   if (pixbuf == NULL)
1086     {
1087       GtkIconTheme *theme;
1088       theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (mount_operation->priv->dialog)));
1089       pixbuf = gtk_icon_theme_load_icon (theme,
1090                                          "application-x-executable",
1091                                          24,
1092                                          0,
1093                                          NULL);
1094     }
1095
1096   markup = g_strdup_printf ("<b>%s</b>\n"
1097                             "<small>%s</small>",
1098                             name,
1099                             command_line);
1100
1101   gtk_list_store_append (list_store, &iter);
1102   gtk_list_store_set (list_store, &iter,
1103                       0, pixbuf,
1104                       1, markup,
1105                       2, pid,
1106                       -1);
1107
1108   if (pixbuf != NULL)
1109     g_object_unref (pixbuf);
1110   g_free (markup);
1111   g_free (name);
1112   g_free (command_line);
1113 }
1114
1115 static void
1116 remove_pid_from_process_list_store (GtkMountOperation *mount_operation,
1117                                     GtkListStore      *list_store,
1118                                     GPid               pid)
1119 {
1120   GtkTreeIter iter;
1121   GPid pid_of_item;
1122
1123   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1124     {
1125       do
1126         {
1127           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
1128                               &iter,
1129                               2, &pid_of_item,
1130                               -1);
1131
1132           if (pid_of_item == pid)
1133             {
1134               gtk_list_store_remove (list_store, &iter);
1135               break;
1136             }
1137         }
1138       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
1139     }
1140 }
1141
1142
1143 static void
1144 update_process_list_store (GtkMountOperation *mount_operation,
1145                            GtkListStore      *list_store,
1146                            GArray            *processes)
1147 {
1148   guint n;
1149   GtkMountOperationLookupContext *lookup_context;
1150   GArray *current_pids;
1151   GArray *pid_indices_to_add;
1152   GArray *pid_indices_to_remove;
1153   GtkTreeIter iter;
1154   GPid pid;
1155
1156   /* Just removing all items and adding new ones will screw up the
1157    * focus handling in the treeview - so compute the delta, and add/remove
1158    * items as appropriate
1159    */
1160   current_pids = g_array_new (FALSE, FALSE, sizeof (GPid));
1161   pid_indices_to_add = g_array_new (FALSE, FALSE, sizeof (gint));
1162   pid_indices_to_remove = g_array_new (FALSE, FALSE, sizeof (gint));
1163
1164   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1165     {
1166       do
1167         {
1168           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
1169                               &iter,
1170                               2, &pid,
1171                               -1);
1172
1173           g_array_append_val (current_pids, pid);
1174         }
1175       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
1176     }
1177
1178   g_array_sort (current_pids, pid_equal);
1179   g_array_sort (processes, pid_equal);
1180
1181   diff_sorted_arrays (current_pids, processes, pid_equal, pid_indices_to_add, pid_indices_to_remove);
1182
1183   for (n = 0; n < pid_indices_to_remove->len; n++)
1184     {
1185       pid = g_array_index (current_pids, GPid, n);
1186       remove_pid_from_process_list_store (mount_operation, list_store, pid);
1187     }
1188
1189   if (pid_indices_to_add->len > 0)
1190     {
1191       lookup_context = _gtk_mount_operation_lookup_context_get (gtk_widget_get_display (mount_operation->priv->process_tree_view));
1192       for (n = 0; n < pid_indices_to_add->len; n++)
1193         {
1194           pid = g_array_index (processes, GPid, n);
1195           add_pid_to_process_list_store (mount_operation, lookup_context, list_store, pid);
1196         }
1197       _gtk_mount_operation_lookup_context_free (lookup_context);
1198     }
1199
1200   /* select the first item, if we went from a zero to a non-zero amount of processes */
1201   if (current_pids->len == 0 && pid_indices_to_add->len > 0)
1202     {
1203       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1204         {
1205           GtkTreeSelection *tree_selection;
1206           tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mount_operation->priv->process_tree_view));
1207           gtk_tree_selection_select_iter (tree_selection, &iter);
1208         }
1209     }
1210
1211   g_array_unref (current_pids);
1212   g_array_unref (pid_indices_to_add);
1213   g_array_unref (pid_indices_to_remove);
1214 }
1215
1216 static void
1217 on_end_process_activated (GtkMenuItem *item,
1218                           gpointer user_data)
1219 {
1220   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1221   GtkTreeSelection *selection;
1222   GtkTreeIter iter;
1223   GPid pid_to_kill;
1224   GError *error;
1225
1226   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1227
1228   if (!gtk_tree_selection_get_selected (selection,
1229                                         NULL,
1230                                         &iter))
1231     goto out;
1232
1233   gtk_tree_model_get (GTK_TREE_MODEL (op->priv->process_list_store),
1234                       &iter,
1235                       2, &pid_to_kill,
1236                       -1);
1237
1238   /* TODO: We might want to either
1239    *
1240    *       - Be smart about things and send SIGKILL rather than SIGTERM if
1241    *         this is the second time the user requests killing a process
1242    *
1243    *       - Or, easier (but worse user experience), offer both "End Process"
1244    *         and "Terminate Process" options
1245    *
1246    *      But that's not how things work right now....
1247    */
1248   error = NULL;
1249   if (!_gtk_mount_operation_kill_process (pid_to_kill, &error))
1250     {
1251       GtkWidget *dialog;
1252       gint response;
1253
1254       /* Use GTK_DIALOG_DESTROY_WITH_PARENT here since the parent dialog can be
1255        * indeed be destroyed via the GMountOperation::abort signal... for example,
1256        * this is triggered if the user yanks the device while we are showing
1257        * the dialog...
1258        */
1259       dialog = gtk_message_dialog_new (GTK_WINDOW (op->priv->dialog),
1260                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1261                                        GTK_MESSAGE_ERROR,
1262                                        GTK_BUTTONS_CLOSE,
1263                                        _("Unable to end process"));
1264       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1265                                                 "%s",
1266                                                 error->message);
1267
1268       gtk_widget_show_all (dialog);
1269       response = gtk_dialog_run (GTK_DIALOG (dialog));
1270
1271       /* GTK_RESPONSE_NONE means the dialog were programmatically destroy, e.g. that
1272        * GTK_DIALOG_DESTROY_WITH_PARENT kicked in - so it would trigger a warning to
1273        * destroy the dialog in that case
1274        */
1275       if (response != GTK_RESPONSE_NONE)
1276         gtk_widget_destroy (dialog);
1277
1278       g_error_free (error);
1279     }
1280
1281  out:
1282   ;
1283 }
1284
1285 static gboolean
1286 do_popup_menu_for_process_tree_view (GtkWidget         *widget,
1287                                      GdkEventButton    *event,
1288                                      GtkMountOperation *op)
1289 {
1290   GtkWidget *menu;
1291   GtkWidget *item;
1292   gint button;
1293   gint event_time;
1294   gboolean popped_up_menu;
1295
1296   popped_up_menu = FALSE;
1297
1298   menu = gtk_menu_new ();
1299
1300   item = gtk_image_menu_item_new_with_mnemonic (_("_End Process"));
1301   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1302                                  gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
1303   g_signal_connect (item, "activate",
1304                     G_CALLBACK (on_end_process_activated),
1305                     op);
1306   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1307   gtk_widget_show_all (menu);
1308
1309   if (event != NULL)
1310     {
1311       GtkTreePath *path;
1312       GtkTreeSelection *selection;
1313
1314       if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (op->priv->process_tree_view),
1315                                          (gint) event->x,
1316                                          (gint) event->y,
1317                                          &path,
1318                                          NULL,
1319                                          NULL,
1320                                          NULL))
1321         {
1322           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1323           gtk_tree_selection_select_path (selection, path);
1324           gtk_tree_path_free (path);
1325         }
1326       else
1327         {
1328           /* don't popup a menu if the user right-clicked in an area with no rows */
1329           goto out;
1330         }
1331
1332       button = event->button;
1333       event_time = event->time;
1334     }
1335   else
1336     {
1337       button = 0;
1338       event_time = gtk_get_current_event_time ();
1339     }
1340
1341   gtk_menu_popup (GTK_MENU (menu),
1342                   NULL,
1343                   widget,
1344                   NULL,
1345                   NULL,
1346                   button,
1347                   event_time);
1348
1349   popped_up_menu = TRUE;
1350
1351  out:
1352   return popped_up_menu;
1353 }
1354
1355 static gboolean
1356 on_popup_menu_for_process_tree_view (GtkWidget *widget,
1357                                      gpointer   user_data)
1358 {
1359   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1360   return do_popup_menu_for_process_tree_view (widget, NULL, op);
1361 }
1362
1363 static gboolean
1364 on_button_press_event_for_process_tree_view (GtkWidget      *widget,
1365                                              GdkEventButton *event,
1366                                              gpointer        user_data)
1367 {
1368   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1369   gboolean ret;
1370
1371   ret = FALSE;
1372
1373   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
1374     {
1375       ret = do_popup_menu_for_process_tree_view (widget, event, op);
1376     }
1377
1378   return ret;
1379 }
1380
1381 static GtkWidget *
1382 create_show_processes_dialog (GtkMountOperation *op,
1383                               const char      *message,
1384                               const char      *choices[])
1385 {
1386   GtkMountOperationPrivate *priv;
1387   GtkWidget  *dialog;
1388   const char *secondary = NULL;
1389   char       *primary;
1390   int        count, len = 0;
1391   GtkWidget *label;
1392   GtkWidget *tree_view;
1393   GtkWidget *scrolled_window;
1394   GtkWidget *vbox;
1395   GtkWidget *content_area;
1396   GtkTreeViewColumn *column;
1397   GtkCellRenderer *renderer;
1398   GtkListStore *list_store;
1399   gchar *s;
1400
1401   priv = op->priv;
1402
1403   primary = strstr (message, "\n");
1404   if (primary)
1405     {
1406       secondary = primary + 1;
1407       primary = g_strndup (message, primary - message);
1408     }
1409
1410   dialog = gtk_dialog_new ();
1411
1412   if (priv->parent_window != NULL)
1413     gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->parent_window);
1414   gtk_window_set_title (GTK_WINDOW (dialog), "");
1415
1416   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1417   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1418   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1419   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
1420
1421   if (secondary != NULL)
1422     {
1423       s = g_strdup_printf ("<big><b>%s</b></big>\n\n%s", primary, secondary);
1424     }
1425   else
1426     {
1427       s = g_strdup_printf ("%s", primary);
1428     }
1429   g_free (primary);
1430   label = gtk_label_new (NULL);
1431   gtk_label_set_markup (GTK_LABEL (label), s);
1432   g_free (s);
1433   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1434
1435   /* First count the items in the list then
1436    * add the buttons in reverse order */
1437
1438   while (choices[len] != NULL)
1439     len++;
1440
1441   for (count = len - 1; count >= 0; count--)
1442     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
1443
1444   g_signal_connect (G_OBJECT (dialog), "response",
1445                     G_CALLBACK (show_processes_button_clicked), op);
1446
1447   priv->dialog = GTK_DIALOG (dialog);
1448   g_object_notify (G_OBJECT (op), "is-showing");
1449
1450   if (priv->parent_window == NULL && priv->screen)
1451     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
1452
1453   tree_view = gtk_tree_view_new ();
1454   /* TODO: should use EM's when gtk+ RI patches land */
1455   gtk_widget_set_size_request (tree_view,
1456                                300,
1457                                120);
1458
1459   column = gtk_tree_view_column_new ();
1460   renderer = gtk_cell_renderer_pixbuf_new ();
1461   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1462   gtk_tree_view_column_set_attributes (column, renderer,
1463                                        "pixbuf", 0,
1464                                        NULL);
1465   renderer = gtk_cell_renderer_text_new ();
1466   g_object_set (renderer,
1467                 "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
1468                 "ellipsize-set", TRUE,
1469                 NULL);
1470   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1471   gtk_tree_view_column_set_attributes (column, renderer,
1472                                        "markup", 1,
1473                                        NULL);
1474   gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
1475   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
1476
1477
1478   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1479   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1480                                   GTK_POLICY_NEVER,
1481                                   GTK_POLICY_AUTOMATIC);
1482   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
1483
1484   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
1485   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
1486
1487   g_signal_connect (tree_view, "popup-menu",
1488                     G_CALLBACK (on_popup_menu_for_process_tree_view),
1489                     op);
1490   g_signal_connect (tree_view, "button-press-event",
1491                     G_CALLBACK (on_button_press_event_for_process_tree_view),
1492                     op);
1493
1494   list_store = gtk_list_store_new (3,
1495                                    GDK_TYPE_PIXBUF,
1496                                    G_TYPE_STRING,
1497                                    G_TYPE_INT);
1498
1499   gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store));
1500
1501   priv->process_list_store = list_store;
1502   priv->process_tree_view = tree_view;
1503   /* set pointers to NULL when dialog goes away */
1504   g_object_add_weak_pointer (G_OBJECT (list_store), (gpointer *) &priv->process_list_store);
1505   g_object_add_weak_pointer (G_OBJECT (tree_view), (gpointer *) &priv->process_tree_view);
1506
1507   g_object_unref (list_store);
1508   g_object_ref (op);
1509
1510   return dialog;
1511 }
1512
1513 static void
1514 call_processes_proxy_cb (GObject     *source,
1515                         GAsyncResult *res,
1516                         gpointer      user_data)
1517 {
1518   _GtkMountOperationHandler *proxy = _GTK_MOUNT_OPERATION_HANDLER (source);
1519   GMountOperation *op = user_data;
1520   GMountOperationResult result;
1521   GVariant *result_details;
1522   GVariantIter iter;
1523   const gchar *key;
1524   GVariant *value;
1525   GError *error = NULL;
1526
1527   if (!_gtk_mount_operation_handler_call_show_processes_finish (proxy,
1528                                                                 &result,
1529                                                                 &result_details,
1530                                                                 res,
1531                                                                 &error))
1532     {
1533       result = G_MOUNT_OPERATION_ABORTED;
1534       g_warning ("Shell mount operation error: %s\n", error->message);
1535       g_error_free (error);
1536       goto out;
1537     }
1538
1539   /* If the request was unhandled it means we called the method again;
1540    * in this case, just return and wait for the next response.
1541    */
1542   if (result == G_MOUNT_OPERATION_UNHANDLED)
1543     return;
1544
1545   g_variant_iter_init (&iter, result_details);
1546   while (g_variant_iter_loop (&iter, "{&sv}", &key, &value))
1547     {
1548       if (strcmp (key, "choice") == 0)
1549         g_mount_operation_set_choice (op, g_variant_get_int32 (value));
1550     }
1551
1552  out:
1553   gtk_mount_operation_proxy_finish (GTK_MOUNT_OPERATION (op), result);
1554 }
1555
1556 static void
1557 gtk_mount_operation_show_processes_do_proxy (GtkMountOperation *operation,
1558                                              const char        *message,
1559                                              GArray            *processes,
1560                                              const char        *choices[])
1561 {
1562   gchar id[255];
1563   g_sprintf(id, "GtkMountOperation%p", operation);
1564
1565   operation->priv->handler_showing = TRUE;
1566   g_object_notify (G_OBJECT (operation), "is-showing");
1567
1568   /* keep a ref to the operation while the handler is showing */
1569   g_object_ref (operation);
1570
1571   _gtk_mount_operation_handler_call_show_processes (operation->priv->handler, id,
1572                                                     message, "drive-harddisk",
1573                                                     g_variant_new_fixed_array (G_VARIANT_TYPE_INT32,
1574                                                                                processes->data, processes->len,
1575                                                                                sizeof (GPid)),
1576                                                     choices, NULL,
1577                                                     call_processes_proxy_cb, operation);
1578 }
1579
1580 static void
1581 gtk_mount_operation_show_processes_do_gtk (GtkMountOperation *op,
1582                                            const char        *message,
1583                                            GArray            *processes,
1584                                            const char        *choices[])
1585 {
1586   GtkMountOperationPrivate *priv;
1587   GtkWidget *dialog = NULL;
1588
1589   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1590   g_return_if_fail (message != NULL);
1591   g_return_if_fail (processes != NULL);
1592   g_return_if_fail (choices != NULL);
1593
1594   priv = op->priv;
1595
1596   if (priv->process_list_store == NULL)
1597     {
1598       /* need to create the dialog */
1599       dialog = create_show_processes_dialog (op, message, choices);
1600     }
1601
1602   /* otherwise, we're showing the dialog, assume messages+choices hasn't changed */
1603
1604   update_process_list_store (op,
1605                              priv->process_list_store,
1606                              processes);
1607
1608   if (dialog != NULL)
1609     {
1610       gtk_widget_show_all (dialog);
1611     }
1612 }
1613
1614
1615 static void
1616 gtk_mount_operation_show_processes (GMountOperation *op,
1617                                     const char      *message,
1618                                     GArray          *processes,
1619                                     const char      *choices[])
1620 {
1621
1622   GtkMountOperation *operation;
1623   gboolean use_gtk;
1624
1625   operation = GTK_MOUNT_OPERATION (op);
1626   use_gtk = (operation->priv->handler == NULL);
1627
1628   if (use_gtk)
1629     gtk_mount_operation_show_processes_do_gtk (operation, message, processes, choices);
1630   else
1631     gtk_mount_operation_show_processes_do_proxy (operation, message, processes, choices);
1632 }
1633
1634 static void
1635 gtk_mount_operation_aborted (GMountOperation *op)
1636 {
1637   GtkMountOperationPrivate *priv;
1638
1639   priv = GTK_MOUNT_OPERATION (op)->priv;
1640
1641   if (priv->dialog != NULL)
1642     {
1643       gtk_widget_destroy (GTK_WIDGET (priv->dialog));
1644       priv->dialog = NULL;
1645       g_object_notify (G_OBJECT (op), "is-showing");
1646       g_object_unref (op);
1647     }
1648
1649   if (priv->handler != NULL)
1650     {
1651       _gtk_mount_operation_handler_call_close (priv->handler, NULL, NULL, NULL);
1652
1653       priv->handler_showing = FALSE;
1654       g_object_notify (G_OBJECT (op), "is-showing");
1655     }
1656 }
1657
1658 /**
1659  * gtk_mount_operation_new:
1660  * @parent: (allow-none): transient parent of the window, or %NULL
1661  *
1662  * Creates a new #GtkMountOperation
1663  *
1664  * Returns: a new #GtkMountOperation
1665  *
1666  * Since: 2.14
1667  */
1668 GMountOperation *
1669 gtk_mount_operation_new (GtkWindow *parent)
1670 {
1671   GMountOperation *mount_operation;
1672
1673   mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
1674                                   "parent", parent, NULL);
1675
1676   return mount_operation;
1677 }
1678
1679 /**
1680  * gtk_mount_operation_is_showing:
1681  * @op: a #GtkMountOperation
1682  *
1683  * Returns whether the #GtkMountOperation is currently displaying
1684  * a window.
1685  *
1686  * Returns: %TRUE if @op is currently displaying a window
1687  *
1688  * Since: 2.14
1689  */
1690 gboolean
1691 gtk_mount_operation_is_showing (GtkMountOperation *op)
1692 {
1693   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
1694
1695   return op->priv->dialog != NULL;
1696 }
1697
1698 /**
1699  * gtk_mount_operation_set_parent:
1700  * @op: a #GtkMountOperation
1701  * @parent: (allow-none): transient parent of the window, or %NULL
1702  *
1703  * Sets the transient parent for windows shown by the
1704  * #GtkMountOperation.
1705  *
1706  * Since: 2.14
1707  */
1708 void
1709 gtk_mount_operation_set_parent (GtkMountOperation *op,
1710                                 GtkWindow         *parent)
1711 {
1712   GtkMountOperationPrivate *priv;
1713
1714   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1715   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
1716
1717   priv = op->priv;
1718
1719   if (priv->parent_window == parent)
1720     return;
1721
1722   if (priv->parent_window)
1723     {
1724       g_signal_handlers_disconnect_by_func (priv->parent_window,
1725                                             gtk_widget_destroyed,
1726                                             &priv->parent_window);
1727       priv->parent_window = NULL;
1728     }
1729
1730   if (parent)
1731     {
1732       priv->parent_window = g_object_ref (parent);
1733
1734       g_signal_connect (parent, "destroy",
1735                         G_CALLBACK (gtk_widget_destroyed),
1736                         &priv->parent_window);
1737
1738       if (priv->dialog)
1739         gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
1740     }
1741
1742   g_object_notify (G_OBJECT (op), "parent");
1743 }
1744
1745 /**
1746  * gtk_mount_operation_get_parent:
1747  * @op: a #GtkMountOperation
1748  *
1749  * Gets the transient parent used by the #GtkMountOperation
1750  *
1751  * Returns: (transfer none): the transient parent for windows shown by @op
1752  *
1753  * Since: 2.14
1754  */
1755 GtkWindow *
1756 gtk_mount_operation_get_parent (GtkMountOperation *op)
1757 {
1758   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1759
1760   return op->priv->parent_window;
1761 }
1762
1763 /**
1764  * gtk_mount_operation_set_screen:
1765  * @op: a #GtkMountOperation
1766  * @screen: a #GdkScreen
1767  *
1768  * Sets the screen to show windows of the #GtkMountOperation on.
1769  *
1770  * Since: 2.14
1771  */
1772 void
1773 gtk_mount_operation_set_screen (GtkMountOperation *op,
1774                                 GdkScreen         *screen)
1775 {
1776   GtkMountOperationPrivate *priv;
1777
1778   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1779   g_return_if_fail (GDK_IS_SCREEN (screen));
1780
1781   priv = op->priv;
1782
1783   if (priv->screen == screen)
1784     return;
1785
1786   if (priv->screen)
1787     g_object_unref (priv->screen);
1788
1789   priv->screen = g_object_ref (screen);
1790
1791   if (priv->dialog)
1792     gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
1793
1794   g_object_notify (G_OBJECT (op), "screen");
1795 }
1796
1797 /**
1798  * gtk_mount_operation_get_screen:
1799  * @op: a #GtkMountOperation
1800  *
1801  * Gets the screen on which windows of the #GtkMountOperation
1802  * will be shown.
1803  *
1804  * Returns: (transfer none): the screen on which windows of @op are shown
1805  *
1806  * Since: 2.14
1807  */
1808 GdkScreen *
1809 gtk_mount_operation_get_screen (GtkMountOperation *op)
1810 {
1811   GtkMountOperationPrivate *priv;
1812
1813   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1814
1815   priv = op->priv;
1816
1817   if (priv->dialog)
1818     return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
1819   else if (priv->parent_window)
1820     return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
1821   else if (priv->screen)
1822     return priv->screen;
1823   else
1824     return gdk_screen_get_default ();
1825 }