]> Pileus Git - ~andy/gtk/blob - gtk/gtkmountoperation.c
gtk/gtkmountoperation: gtk_misc_set_alignment
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26  */
27
28 #include "config.h"
29
30 #include <string.h>
31
32 #include "gtkmountoperationprivate.h"
33 #include "gtkbox.h"
34 #include "gtkentry.h"
35 #include "gtkhbox.h"
36 #include "gtkintl.h"
37 #include "gtklabel.h"
38 #include "gtkvbox.h"
39 #include "gtkmessagedialog.h"
40 #include "gtkmountoperation.h"
41 #include "gtkprivate.h"
42 #include "gtkradiobutton.h"
43 #include "gtkstock.h"
44 #include "gtktable.h"
45 #include "gtkwindow.h"
46 #include "gtktreeview.h"
47 #include "gtktreeselection.h"
48 #include "gtkcellrenderertext.h"
49 #include "gtkcellrendererpixbuf.h"
50 #include "gtkscrolledwindow.h"
51 #include "gtkicontheme.h"
52 #include "gtkimagemenuitem.h"
53 #include "gtkmain.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   /* for the ask-password dialog */
122   GtkWidget *entry_container;
123   GtkWidget *username_entry;
124   GtkWidget *domain_entry;
125   GtkWidget *password_entry;
126   GtkWidget *anonymous_toggle;
127
128   GAskPasswordFlags ask_flags;
129   GPasswordSave     password_save;
130   gboolean          anonymous;
131
132   /* for the show-processes dialog */
133   GtkWidget *process_tree_view;
134   GtkListStore *process_list_store;
135 };
136
137 static void
138 gtk_mount_operation_class_init (GtkMountOperationClass *klass)
139 {
140   GObjectClass         *object_class = G_OBJECT_CLASS (klass);
141   GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
142
143   g_type_class_add_private (klass, sizeof (GtkMountOperationPrivate));
144
145   object_class->finalize     = gtk_mount_operation_finalize;
146   object_class->get_property = gtk_mount_operation_get_property;
147   object_class->set_property = gtk_mount_operation_set_property;
148
149   mount_op_class->ask_password = gtk_mount_operation_ask_password;
150   mount_op_class->ask_question = gtk_mount_operation_ask_question;
151   mount_op_class->show_processes = gtk_mount_operation_show_processes;
152   mount_op_class->aborted = gtk_mount_operation_aborted;
153
154   g_object_class_install_property (object_class,
155                                    PROP_PARENT,
156                                    g_param_spec_object ("parent",
157                                                         P_("Parent"),
158                                                         P_("The parent window"),
159                                                         GTK_TYPE_WINDOW,
160                                                         GTK_PARAM_READWRITE));
161
162   g_object_class_install_property (object_class,
163                                    PROP_IS_SHOWING,
164                                    g_param_spec_boolean ("is-showing",
165                                                          P_("Is Showing"),
166                                                          P_("Are we showing a dialog"),
167                                                          FALSE,
168                                                          GTK_PARAM_READABLE));
169
170   g_object_class_install_property (object_class,
171                                    PROP_SCREEN,
172                                    g_param_spec_object ("screen",
173                                                         P_("Screen"),
174                                                         P_("The screen where this window will be displayed."),
175                                                         GDK_TYPE_SCREEN,
176                                                         GTK_PARAM_READWRITE));
177 }
178
179 static void
180 gtk_mount_operation_init (GtkMountOperation *operation)
181 {
182   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
183                                                  GTK_TYPE_MOUNT_OPERATION,
184                                                  GtkMountOperationPrivate);
185 }
186
187 static void
188 gtk_mount_operation_finalize (GObject *object)
189 {
190   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
191   GtkMountOperationPrivate *priv = operation->priv;
192
193   if (priv->parent_window)
194     g_object_unref (priv->parent_window);
195
196   if (priv->screen)
197     g_object_unref (priv->screen);
198
199   G_OBJECT_CLASS (gtk_mount_operation_parent_class)->finalize (object);
200 }
201
202 static void
203 gtk_mount_operation_set_property (GObject      *object,
204                                   guint         prop_id,
205                                   const GValue *value,
206                                   GParamSpec   *pspec)
207 {
208   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
209
210   switch (prop_id)
211     {
212     case PROP_PARENT:
213       gtk_mount_operation_set_parent (operation, g_value_get_object (value));
214       break;
215
216     case PROP_SCREEN:
217       gtk_mount_operation_set_screen (operation, g_value_get_object (value));
218       break;
219
220     case PROP_IS_SHOWING:
221     default:
222       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223       break;
224     }
225 }
226
227 static void
228 gtk_mount_operation_get_property (GObject    *object,
229                                   guint       prop_id,
230                                   GValue     *value,
231                                   GParamSpec *pspec)
232 {
233   GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
234   GtkMountOperationPrivate *priv = operation->priv;
235
236   switch (prop_id)
237     {
238     case PROP_PARENT:
239       g_value_set_object (value, priv->parent_window);
240       break;
241
242     case PROP_IS_SHOWING:
243       g_value_set_boolean (value, priv->dialog != NULL);
244       break;
245
246     case PROP_SCREEN:
247       g_value_set_object (value, priv->screen);
248       break;
249
250     default:
251       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252       break;
253     }
254 }
255
256 static void
257 remember_button_toggled (GtkToggleButton   *button,
258                          GtkMountOperation *operation)
259 {
260   GtkMountOperationPrivate *priv = operation->priv;
261
262   if (gtk_toggle_button_get_active (button))
263     {
264       gpointer data;
265
266       data = g_object_get_data (G_OBJECT (button), "password-save");
267       priv->password_save = GPOINTER_TO_INT (data);
268     }
269 }
270
271 static void
272 pw_dialog_got_response (GtkDialog         *dialog,
273                         gint               response_id,
274                         GtkMountOperation *mount_op)
275 {
276   GtkMountOperationPrivate *priv = mount_op->priv;
277   GMountOperation *op = G_MOUNT_OPERATION (mount_op);
278
279   if (response_id == GTK_RESPONSE_OK)
280     {
281       const char *text;
282
283       if (priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED)
284         g_mount_operation_set_anonymous (op, priv->anonymous);
285
286       if (priv->username_entry)
287         {
288           text = gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
289           g_mount_operation_set_username (op, text);
290         }
291
292       if (priv->domain_entry)
293         {
294           text = gtk_entry_get_text (GTK_ENTRY (priv->domain_entry));
295           g_mount_operation_set_domain (op, text);
296         }
297
298       if (priv->password_entry)
299         {
300           text = gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
301           g_mount_operation_set_password (op, text);
302         }
303
304       if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
305         g_mount_operation_set_password_save (op, priv->password_save);
306
307       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
308     }
309   else
310     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
311
312   priv->dialog = NULL;
313   g_object_notify (G_OBJECT (op), "is-showing");
314   gtk_widget_destroy (GTK_WIDGET (dialog));
315   g_object_unref (op);
316 }
317
318 static gboolean
319 entry_has_input (GtkWidget *entry_widget)
320 {
321   const char *text;
322
323   if (entry_widget == NULL)
324     return TRUE;
325
326   text = gtk_entry_get_text (GTK_ENTRY (entry_widget));
327
328   return text != NULL && text[0] != '\0';
329 }
330
331 static gboolean
332 pw_dialog_input_is_valid (GtkMountOperation *operation)
333 {
334   GtkMountOperationPrivate *priv = operation->priv;
335   gboolean is_valid = TRUE;
336
337   /* We don't require password to be non-empty here
338    * since there are situations where it is not needed,
339    * see bug 578365.
340    * We may add a way for the backend to specify that it
341    * definitively needs a password.
342    */
343   is_valid = entry_has_input (priv->username_entry) &&
344              entry_has_input (priv->domain_entry);
345
346   return is_valid;
347 }
348
349 static void
350 pw_dialog_verify_input (GtkEditable       *editable,
351                         GtkMountOperation *operation)
352 {
353   GtkMountOperationPrivate *priv = operation->priv;
354   gboolean is_valid;
355
356   is_valid = pw_dialog_input_is_valid (operation);
357   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
358                                      GTK_RESPONSE_OK,
359                                      is_valid);
360 }
361
362 static void
363 pw_dialog_anonymous_toggled (GtkWidget         *widget,
364                              GtkMountOperation *operation)
365 {
366   GtkMountOperationPrivate *priv = operation->priv;
367   gboolean is_valid;
368
369   priv->anonymous = widget == priv->anonymous_toggle;
370
371   if (priv->anonymous)
372     is_valid = TRUE;
373   else
374     is_valid = pw_dialog_input_is_valid (operation);
375
376   gtk_widget_set_sensitive (priv->entry_container, priv->anonymous == FALSE);
377   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
378                                      GTK_RESPONSE_OK,
379                                      is_valid);
380 }
381
382
383 static void
384 pw_dialog_cycle_focus (GtkWidget         *widget,
385                        GtkMountOperation *operation)
386 {
387   GtkMountOperationPrivate *priv;
388   GtkWidget *next_widget = NULL;
389
390   priv = operation->priv;
391
392   if (widget == priv->username_entry)
393     {
394       if (priv->domain_entry != NULL)
395         next_widget = priv->domain_entry;
396       else if (priv->password_entry != NULL)
397         next_widget = priv->password_entry;
398     }
399   else if (widget == priv->domain_entry && priv->password_entry)
400     next_widget = priv->password_entry;
401
402   if (next_widget)
403     gtk_widget_grab_focus (next_widget);
404   else if (pw_dialog_input_is_valid (operation))
405     gtk_window_activate_default (GTK_WINDOW (priv->dialog));
406 }
407
408 static GtkWidget *
409 table_add_entry (GtkWidget  *table,
410                  int         row,
411                  const char *label_text,
412                  const char *value,
413                  gpointer    user_data)
414 {
415   GtkWidget *entry;
416   GtkWidget *label;
417
418   label = gtk_label_new_with_mnemonic (label_text);
419   gtk_widget_set_halign (label, GTK_ALIGN_START);
420   gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
421
422   entry = gtk_entry_new ();
423
424   if (value)
425     gtk_entry_set_text (GTK_ENTRY (entry), value);
426
427   gtk_table_attach (GTK_TABLE (table), label,
428                     0, 1, row, row + 1,
429                     GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
430   gtk_table_attach_defaults (GTK_TABLE (table), entry,
431                              1, 2, row, row + 1);
432   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
433
434   g_signal_connect (entry, "changed",
435                     G_CALLBACK (pw_dialog_verify_input), user_data);
436
437   g_signal_connect (entry, "activate",
438                     G_CALLBACK (pw_dialog_cycle_focus), user_data);
439
440   return entry;
441 }
442
443 static void
444 gtk_mount_operation_ask_password (GMountOperation   *mount_op,
445                                   const char        *message,
446                                   const char        *default_user,
447                                   const char        *default_domain,
448                                   GAskPasswordFlags  flags)
449 {
450   GtkMountOperation *operation;
451   GtkMountOperationPrivate *priv;
452   GtkWidget *widget;
453   GtkDialog *dialog;
454   GtkWindow *window;
455   GtkWidget *hbox, *main_vbox, *vbox, *icon;
456   GtkWidget *table;
457   GtkWidget *message_label;
458   GtkWidget *content_area, *action_area;
459   gboolean   can_anonymous;
460   guint      rows;
461   const gchar *secondary;
462
463   operation = GTK_MOUNT_OPERATION (mount_op);
464   priv = operation->priv;
465
466   priv->ask_flags = flags;
467
468   widget = gtk_dialog_new ();
469   dialog = GTK_DIALOG (widget);
470   window = GTK_WINDOW (widget);
471
472   priv->dialog = dialog;
473
474   content_area = gtk_dialog_get_content_area (dialog);
475   action_area = gtk_dialog_get_action_area (dialog);
476
477   /* Set the dialog up with HIG properties */
478   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
479   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
480   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
481   gtk_box_set_spacing (GTK_BOX (action_area), 6);
482
483   gtk_window_set_resizable (window, FALSE);
484   gtk_window_set_title (window, "");
485   gtk_window_set_icon_name (window, GTK_STOCK_DIALOG_AUTHENTICATION);
486
487   gtk_dialog_add_buttons (dialog,
488                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
489                           _("Co_nnect"), GTK_RESPONSE_OK,
490                           NULL);
491   gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
492
493   gtk_dialog_set_alternative_button_order (dialog,
494                                            GTK_RESPONSE_OK,
495                                            GTK_RESPONSE_CANCEL,
496                                            -1);
497
498   /* Build contents */
499   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
500   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
501   gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
502
503   icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
504                                    GTK_ICON_SIZE_DIALOG);
505
506   gtk_widget_set_halign (icon, GTK_ALIGN_CENTER);
507   gtk_widget_set_valign (icon, GTK_ALIGN_START);
508   gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
509
510   main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
511   gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
512
513   secondary = strstr (message, "\n");
514   if (secondary != NULL)
515     {
516       gchar *s;
517       gchar *primary;
518
519       primary = g_strndup (message, secondary - message + 1);
520       s = g_strdup_printf ("<big><b>%s</b></big>%s", primary, secondary);
521
522       message_label = gtk_label_new (NULL);
523       gtk_label_set_markup (GTK_LABEL (message_label), s);
524       gtk_widget_set_halign (message_label, GTK_ALIGN_START);
525       gtk_widget_set_valign (message_label, GTK_ALIGN_CENTER);
526       gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
527       gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
528                           FALSE, TRUE, 0);
529
530       g_free (s);
531       g_free (primary);
532     }
533   else
534     {
535       message_label = gtk_label_new (message);
536       gtk_widget_set_halign (message_label, GTK_ALIGN_START);
537       gtk_widget_set_valign (message_label, GTK_ALIGN_CENTER);
538       gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
539       gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
540                           FALSE, FALSE, 0);
541     }
542
543   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
544   gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
545
546   can_anonymous = flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
547
548   priv->anonymous_toggle = NULL;
549   if (can_anonymous)
550     {
551       GtkWidget *anon_box;
552       GtkWidget *choice;
553       GSList    *group;
554
555       anon_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
556       gtk_box_pack_start (GTK_BOX (vbox), anon_box,
557                           FALSE, FALSE, 0);
558
559       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Connect _anonymously"));
560       gtk_box_pack_start (GTK_BOX (anon_box),
561                           choice,
562                           FALSE, FALSE, 0);
563       g_signal_connect (choice, "toggled",
564                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
565       priv->anonymous_toggle = choice;
566
567       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
568       choice = gtk_radio_button_new_with_mnemonic (group, _("Connect as u_ser:"));
569       gtk_box_pack_start (GTK_BOX (anon_box),
570                           choice,
571                           FALSE, FALSE, 0);
572       g_signal_connect (choice, "toggled",
573                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
574     }
575
576   rows = 0;
577
578   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
579     rows++;
580
581   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
582     rows++;
583
584   if (flags &G_ASK_PASSWORD_NEED_DOMAIN)
585     rows++;
586
587   /* The table that holds the entries */
588   table = gtk_table_new (rows, 2, FALSE);
589   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
590   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
591
592   if (can_anonymous)
593     gtk_widget_set_margin_left (table, 12);
594
595   gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
596   priv->entry_container = table;
597
598   rows = 0;
599
600   priv->username_entry = NULL;
601   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
602     priv->username_entry = table_add_entry (table, rows++, _("_Username:"),
603                                             default_user, operation);
604
605   priv->domain_entry = NULL;
606   if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
607     priv->domain_entry = table_add_entry (table, rows++, _("_Domain:"),
608                                           default_domain, operation);
609
610   priv->password_entry = NULL;
611   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
612     {
613       priv->password_entry = table_add_entry (table, rows++, _("_Password:"),
614                                               NULL, operation);
615       gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
616     }
617
618    if (flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
619     {
620       GtkWidget    *choice;
621       GtkWidget    *remember_box;
622       GSList       *group;
623       GPasswordSave password_save;
624
625       remember_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
626       gtk_box_pack_start (GTK_BOX (vbox), remember_box,
627                           FALSE, FALSE, 0);
628
629       password_save = g_mount_operation_get_password_save (mount_op);
630       
631       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
632       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
633                                     password_save == G_PASSWORD_SAVE_NEVER);
634       g_object_set_data (G_OBJECT (choice), "password-save",
635                          GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
636       g_signal_connect (choice, "toggled",
637                         G_CALLBACK (remember_button_toggled), operation);
638       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
639
640       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
641       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
642       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
643                                     password_save == G_PASSWORD_SAVE_FOR_SESSION);
644       g_object_set_data (G_OBJECT (choice), "password-save",
645                          GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
646       g_signal_connect (choice, "toggled",
647                         G_CALLBACK (remember_button_toggled), operation);
648       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
649
650       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
651       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
652       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
653                                     password_save == G_PASSWORD_SAVE_PERMANENTLY);
654       g_object_set_data (G_OBJECT (choice), "password-save",
655                          GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
656       g_signal_connect (choice, "toggled",
657                         G_CALLBACK (remember_button_toggled), operation);
658       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
659     }
660
661   g_signal_connect (G_OBJECT (dialog), "response",
662                     G_CALLBACK (pw_dialog_got_response), operation);
663
664   if (can_anonymous)
665     {
666       /* The anonymous option will be active by default,
667        * ensure the toggled signal is emitted for it.
668        */
669       gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
670     }
671   else if (! pw_dialog_input_is_valid (operation))
672     gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
673
674   g_object_notify (G_OBJECT (operation), "is-showing");
675
676   if (priv->parent_window)
677     {
678       gtk_window_set_transient_for (window, priv->parent_window);
679       gtk_window_set_modal (window, TRUE);
680     }
681   else if (priv->screen)
682     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
683
684   gtk_widget_show_all (GTK_WIDGET (dialog));
685
686   g_object_ref (operation);
687 }
688
689 static void
690 question_dialog_button_clicked (GtkDialog       *dialog,
691                                 gint             button_number,
692                                 GMountOperation *op)
693 {
694   GtkMountOperationPrivate *priv;
695   GtkMountOperation *operation;
696
697   operation = GTK_MOUNT_OPERATION (op);
698   priv = operation->priv;
699
700   if (button_number >= 0)
701     {
702       g_mount_operation_set_choice (op, button_number);
703       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
704     }
705   else
706     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
707
708   priv->dialog = NULL;
709   g_object_notify (G_OBJECT (operation), "is-showing");
710   gtk_widget_destroy (GTK_WIDGET (dialog));
711   g_object_unref (op);
712 }
713
714 static void
715 gtk_mount_operation_ask_question (GMountOperation *op,
716                                   const char      *message,
717                                   const char      *choices[])
718 {
719   GtkMountOperationPrivate *priv;
720   GtkWidget  *dialog;
721   const char *secondary = NULL;
722   char       *primary;
723   int        count, len = 0;
724
725   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
726   g_return_if_fail (message != NULL);
727   g_return_if_fail (choices != NULL);
728
729   priv = GTK_MOUNT_OPERATION (op)->priv;
730
731   primary = strstr (message, "\n");
732   if (primary)
733     {
734       secondary = primary + 1;
735       primary = g_strndup (message, primary - message);
736     }
737
738   dialog = gtk_message_dialog_new (priv->parent_window, 0,
739                                    GTK_MESSAGE_QUESTION,
740                                    GTK_BUTTONS_NONE, "%s",
741                                    primary != NULL ? primary : message);
742   g_free (primary);
743
744   if (secondary)
745     gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
746                                               "%s", secondary);
747
748   /* First count the items in the list then
749    * add the buttons in reverse order */
750
751   while (choices[len] != NULL)
752     len++;
753
754   for (count = len - 1; count >= 0; count--)
755     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
756
757   g_signal_connect (G_OBJECT (dialog), "response",
758                     G_CALLBACK (question_dialog_button_clicked), op);
759
760   priv->dialog = GTK_DIALOG (dialog);
761   g_object_notify (G_OBJECT (op), "is-showing");
762
763   if (priv->parent_window == NULL && priv->screen)
764     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
765
766   gtk_widget_show (dialog);
767   g_object_ref (op);
768 }
769
770 static void
771 show_processes_button_clicked (GtkDialog       *dialog,
772                                gint             button_number,
773                                GMountOperation *op)
774 {
775   GtkMountOperationPrivate *priv;
776   GtkMountOperation *operation;
777
778   operation = GTK_MOUNT_OPERATION (op);
779   priv = operation->priv;
780
781   if (button_number >= 0)
782     {
783       g_mount_operation_set_choice (op, button_number);
784       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
785     }
786   else
787     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
788
789   priv->dialog = NULL;
790   g_object_notify (G_OBJECT (operation), "is-showing");
791   gtk_widget_destroy (GTK_WIDGET (dialog));
792   g_object_unref (op);
793 }
794
795 static gint
796 pid_equal (gconstpointer a,
797            gconstpointer b)
798 {
799   GPid pa, pb;
800
801   pa = *((GPid *) a);
802   pb = *((GPid *) b);
803
804   return GPOINTER_TO_INT(pb) - GPOINTER_TO_INT(pa);
805 }
806
807 static void
808 diff_sorted_arrays (GArray         *array1,
809                     GArray         *array2,
810                     GCompareFunc   compare,
811                     GArray         *added_indices,
812                     GArray         *removed_indices)
813 {
814   gint order;
815   guint n1, n2;
816   guint elem_size;
817
818   n1 = n2 = 0;
819
820   elem_size = g_array_get_element_size (array1);
821   g_assert (elem_size == g_array_get_element_size (array2));
822
823   while (n1 < array1->len && n2 < array2->len)
824     {
825       order = (*compare) (((const char*) array1->data) + n1 * elem_size,
826                           ((const char*) array2->data) + n2 * elem_size);
827       if (order < 0)
828         {
829           g_array_append_val (removed_indices, n1);
830           n1++;
831         }
832       else if (order > 0)
833         {
834           g_array_append_val (added_indices, n2);
835           n2++;
836         }
837       else
838         { /* same item */
839           n1++;
840           n2++;
841         }
842     }
843
844   while (n1 < array1->len)
845     {
846       g_array_append_val (removed_indices, n1);
847       n1++;
848     }
849   while (n2 < array2->len)
850     {
851       g_array_append_val (added_indices, n2);
852       n2++;
853     }
854 }
855
856
857 static void
858 add_pid_to_process_list_store (GtkMountOperation              *mount_operation,
859                                GtkMountOperationLookupContext *lookup_context,
860                                GtkListStore                   *list_store,
861                                GPid                            pid)
862 {
863   gchar *command_line;
864   gchar *name;
865   GdkPixbuf *pixbuf;
866   gchar *markup;
867   GtkTreeIter iter;
868
869   name = NULL;
870   pixbuf = NULL;
871   command_line = NULL;
872   _gtk_mount_operation_lookup_info (lookup_context,
873                                     pid,
874                                     24,
875                                     &name,
876                                     &command_line,
877                                     &pixbuf);
878
879   if (name == NULL)
880     name = g_strdup_printf (_("Unknown Application (PID %d)"), pid);
881
882   if (command_line == NULL)
883     command_line = g_strdup ("");
884
885   if (pixbuf == NULL)
886     {
887       GtkIconTheme *theme;
888       theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (mount_operation->priv->dialog)));
889       pixbuf = gtk_icon_theme_load_icon (theme,
890                                          "application-x-executable",
891                                          24,
892                                          0,
893                                          NULL);
894     }
895
896   markup = g_strdup_printf ("<b>%s</b>\n"
897                             "<small>%s</small>",
898                             name,
899                             command_line);
900
901   gtk_list_store_append (list_store, &iter);
902   gtk_list_store_set (list_store, &iter,
903                       0, pixbuf,
904                       1, markup,
905                       2, pid,
906                       -1);
907
908   if (pixbuf != NULL)
909     g_object_unref (pixbuf);
910   g_free (markup);
911   g_free (name);
912   g_free (command_line);
913 }
914
915 static void
916 remove_pid_from_process_list_store (GtkMountOperation *mount_operation,
917                                     GtkListStore      *list_store,
918                                     GPid               pid)
919 {
920   GtkTreeIter iter;
921   GPid pid_of_item;
922
923   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
924     {
925       do
926         {
927           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
928                               &iter,
929                               2, &pid_of_item,
930                               -1);
931
932           if (pid_of_item == pid)
933             {
934               gtk_list_store_remove (list_store, &iter);
935               break;
936             }
937         }
938       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
939     }
940 }
941
942
943 static void
944 update_process_list_store (GtkMountOperation *mount_operation,
945                            GtkListStore      *list_store,
946                            GArray            *processes)
947 {
948   guint n;
949   GtkMountOperationLookupContext *lookup_context;
950   GArray *current_pids;
951   GArray *pid_indices_to_add;
952   GArray *pid_indices_to_remove;
953   GtkTreeIter iter;
954   GPid pid;
955
956   /* Just removing all items and adding new ones will screw up the
957    * focus handling in the treeview - so compute the delta, and add/remove
958    * items as appropriate
959    */
960   current_pids = g_array_new (FALSE, FALSE, sizeof (GPid));
961   pid_indices_to_add = g_array_new (FALSE, FALSE, sizeof (gint));
962   pid_indices_to_remove = g_array_new (FALSE, FALSE, sizeof (gint));
963
964   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
965     {
966       do
967         {
968           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
969                               &iter,
970                               2, &pid,
971                               -1);
972
973           g_array_append_val (current_pids, pid);
974         }
975       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
976     }
977
978   g_array_sort (current_pids, pid_equal);
979   g_array_sort (processes, pid_equal);
980
981   diff_sorted_arrays (current_pids, processes, pid_equal, pid_indices_to_add, pid_indices_to_remove);
982
983   for (n = 0; n < pid_indices_to_remove->len; n++)
984     {
985       pid = g_array_index (current_pids, GPid, n);
986       remove_pid_from_process_list_store (mount_operation, list_store, pid);
987     }
988
989   if (pid_indices_to_add->len > 0)
990     {
991       lookup_context = _gtk_mount_operation_lookup_context_get (gtk_widget_get_display (mount_operation->priv->process_tree_view));
992       for (n = 0; n < pid_indices_to_add->len; n++)
993         {
994           pid = g_array_index (processes, GPid, n);
995           add_pid_to_process_list_store (mount_operation, lookup_context, list_store, pid);
996         }
997       _gtk_mount_operation_lookup_context_free (lookup_context);
998     }
999
1000   /* select the first item, if we went from a zero to a non-zero amount of processes */
1001   if (current_pids->len == 0 && pid_indices_to_add->len > 0)
1002     {
1003       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1004         {
1005           GtkTreeSelection *tree_selection;
1006           tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mount_operation->priv->process_tree_view));
1007           gtk_tree_selection_select_iter (tree_selection, &iter);
1008         }
1009     }
1010
1011   g_array_unref (current_pids);
1012   g_array_unref (pid_indices_to_add);
1013   g_array_unref (pid_indices_to_remove);
1014 }
1015
1016 static void
1017 on_end_process_activated (GtkMenuItem *item,
1018                           gpointer user_data)
1019 {
1020   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1021   GtkTreeSelection *selection;
1022   GtkTreeIter iter;
1023   GPid pid_to_kill;
1024   GError *error;
1025
1026   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1027
1028   if (!gtk_tree_selection_get_selected (selection,
1029                                         NULL,
1030                                         &iter))
1031     goto out;
1032
1033   gtk_tree_model_get (GTK_TREE_MODEL (op->priv->process_list_store),
1034                       &iter,
1035                       2, &pid_to_kill,
1036                       -1);
1037
1038   /* TODO: We might want to either
1039    *
1040    *       - Be smart about things and send SIGKILL rather than SIGTERM if
1041    *         this is the second time the user requests killing a process
1042    *
1043    *       - Or, easier (but worse user experience), offer both "End Process"
1044    *         and "Terminate Process" options
1045    *
1046    *      But that's not how things work right now....
1047    */
1048   error = NULL;
1049   if (!_gtk_mount_operation_kill_process (pid_to_kill, &error))
1050     {
1051       GtkWidget *dialog;
1052       gint response;
1053
1054       /* Use GTK_DIALOG_DESTROY_WITH_PARENT here since the parent dialog can be
1055        * indeed be destroyed via the GMountOperation::abort signal... for example,
1056        * this is triggered if the user yanks the device while we are showing
1057        * the dialog...
1058        */
1059       dialog = gtk_message_dialog_new (GTK_WINDOW (op->priv->dialog),
1060                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1061                                        GTK_MESSAGE_ERROR,
1062                                        GTK_BUTTONS_CLOSE,
1063                                        _("Unable to end process"));
1064       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1065                                                 "%s",
1066                                                 error->message);
1067
1068       gtk_widget_show_all (dialog);
1069       response = gtk_dialog_run (GTK_DIALOG (dialog));
1070
1071       /* GTK_RESPONSE_NONE means the dialog were programmatically destroy, e.g. that
1072        * GTK_DIALOG_DESTROY_WITH_PARENT kicked in - so it would trigger a warning to
1073        * destroy the dialog in that case
1074        */
1075       if (response != GTK_RESPONSE_NONE)
1076         gtk_widget_destroy (dialog);
1077
1078       g_error_free (error);
1079     }
1080
1081  out:
1082   ;
1083 }
1084
1085 static gboolean
1086 do_popup_menu_for_process_tree_view (GtkWidget         *widget,
1087                                      GdkEventButton    *event,
1088                                      GtkMountOperation *op)
1089 {
1090   GtkWidget *menu;
1091   GtkWidget *item;
1092   gint button;
1093   gint event_time;
1094   gboolean popped_up_menu;
1095
1096   popped_up_menu = FALSE;
1097
1098   menu = gtk_menu_new ();
1099
1100   item = gtk_image_menu_item_new_with_mnemonic (_("_End Process"));
1101   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1102                                  gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
1103   g_signal_connect (item, "activate",
1104                     G_CALLBACK (on_end_process_activated),
1105                     op);
1106   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1107   gtk_widget_show_all (menu);
1108
1109   if (event != NULL)
1110     {
1111       GtkTreePath *path;
1112       GtkTreeSelection *selection;
1113
1114       if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (op->priv->process_tree_view),
1115                                          (gint) event->x,
1116                                          (gint) event->y,
1117                                          &path,
1118                                          NULL,
1119                                          NULL,
1120                                          NULL))
1121         {
1122           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1123           gtk_tree_selection_select_path (selection, path);
1124           gtk_tree_path_free (path);
1125         }
1126       else
1127         {
1128           /* don't popup a menu if the user right-clicked in an area with no rows */
1129           goto out;
1130         }
1131
1132       button = event->button;
1133       event_time = event->time;
1134     }
1135   else
1136     {
1137       button = 0;
1138       event_time = gtk_get_current_event_time ();
1139     }
1140
1141   gtk_menu_popup (GTK_MENU (menu),
1142                   NULL,
1143                   widget,
1144                   NULL,
1145                   NULL,
1146                   button,
1147                   event_time);
1148
1149   popped_up_menu = TRUE;
1150
1151  out:
1152   return popped_up_menu;
1153 }
1154
1155 static gboolean
1156 on_popup_menu_for_process_tree_view (GtkWidget *widget,
1157                                      gpointer   user_data)
1158 {
1159   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1160   return do_popup_menu_for_process_tree_view (widget, NULL, op);
1161 }
1162
1163 static gboolean
1164 on_button_press_event_for_process_tree_view (GtkWidget      *widget,
1165                                              GdkEventButton *event,
1166                                              gpointer        user_data)
1167 {
1168   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1169   gboolean ret;
1170
1171   ret = FALSE;
1172
1173   /* Ignore double-clicks and triple-clicks */
1174   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1175     {
1176       ret = do_popup_menu_for_process_tree_view (widget, event, op);
1177     }
1178
1179   return ret;
1180 }
1181
1182 static GtkWidget *
1183 create_show_processes_dialog (GMountOperation *op,
1184                               const char      *message,
1185                               const char      *choices[])
1186 {
1187   GtkMountOperationPrivate *priv;
1188   GtkWidget  *dialog;
1189   const char *secondary = NULL;
1190   char       *primary;
1191   int        count, len = 0;
1192   GtkWidget *label;
1193   GtkWidget *tree_view;
1194   GtkWidget *scrolled_window;
1195   GtkWidget *vbox;
1196   GtkWidget *content_area;
1197   GtkTreeViewColumn *column;
1198   GtkCellRenderer *renderer;
1199   GtkListStore *list_store;
1200   gchar *s;
1201
1202   priv = GTK_MOUNT_OPERATION (op)->priv;
1203
1204   primary = strstr (message, "\n");
1205   if (primary)
1206     {
1207       secondary = primary + 1;
1208       primary = g_strndup (message, primary - message);
1209     }
1210
1211   dialog = gtk_dialog_new ();
1212
1213   if (priv->parent_window != NULL)
1214     gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->parent_window);
1215   gtk_window_set_title (GTK_WINDOW (dialog), "");
1216
1217   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1218   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
1219   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1220   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
1221
1222   if (secondary != NULL)
1223     {
1224       s = g_strdup_printf ("<big><b>%s</b></big>\n\n%s", primary, secondary);
1225     }
1226   else
1227     {
1228       s = g_strdup_printf ("%s", primary);
1229     }
1230   g_free (primary);
1231   label = gtk_label_new (NULL);
1232   gtk_label_set_markup (GTK_LABEL (label), s);
1233   g_free (s);
1234   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1235
1236   /* First count the items in the list then
1237    * add the buttons in reverse order */
1238
1239   while (choices[len] != NULL)
1240     len++;
1241
1242   for (count = len - 1; count >= 0; count--)
1243     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
1244
1245   g_signal_connect (G_OBJECT (dialog), "response",
1246                     G_CALLBACK (show_processes_button_clicked), op);
1247
1248   priv->dialog = GTK_DIALOG (dialog);
1249   g_object_notify (G_OBJECT (op), "is-showing");
1250
1251   if (priv->parent_window == NULL && priv->screen)
1252     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
1253
1254   tree_view = gtk_tree_view_new ();
1255   /* TODO: should use EM's when gtk+ RI patches land */
1256   gtk_widget_set_size_request (tree_view,
1257                                300,
1258                                120);
1259
1260   column = gtk_tree_view_column_new ();
1261   renderer = gtk_cell_renderer_pixbuf_new ();
1262   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1263   gtk_tree_view_column_set_attributes (column, renderer,
1264                                        "pixbuf", 0,
1265                                        NULL);
1266   renderer = gtk_cell_renderer_text_new ();
1267   g_object_set (renderer,
1268                 "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
1269                 "ellipsize-set", TRUE,
1270                 NULL);
1271   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1272   gtk_tree_view_column_set_attributes (column, renderer,
1273                                        "markup", 1,
1274                                        NULL);
1275   gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
1276   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
1277
1278
1279   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1280   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1281                                   GTK_POLICY_NEVER,
1282                                   GTK_POLICY_AUTOMATIC);
1283   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
1284
1285   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
1286   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
1287
1288   g_signal_connect (tree_view, "popup-menu",
1289                     G_CALLBACK (on_popup_menu_for_process_tree_view),
1290                     op);
1291   g_signal_connect (tree_view, "button-press-event",
1292                     G_CALLBACK (on_button_press_event_for_process_tree_view),
1293                     op);
1294
1295   list_store = gtk_list_store_new (3,
1296                                    GDK_TYPE_PIXBUF,
1297                                    G_TYPE_STRING,
1298                                    G_TYPE_INT);
1299
1300   gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store));
1301
1302   priv->process_list_store = list_store;
1303   priv->process_tree_view = tree_view;
1304   /* set pointers to NULL when dialog goes away */
1305   g_object_add_weak_pointer (G_OBJECT (list_store), (gpointer *) &priv->process_list_store);
1306   g_object_add_weak_pointer (G_OBJECT (tree_view), (gpointer *) &priv->process_tree_view);
1307
1308   g_object_unref (list_store);
1309   g_object_ref (op);
1310
1311   return dialog;
1312 }
1313
1314 static void
1315 gtk_mount_operation_show_processes (GMountOperation *op,
1316                                     const char      *message,
1317                                     GArray          *processes,
1318                                     const char      *choices[])
1319 {
1320   GtkMountOperationPrivate *priv;
1321   GtkWidget *dialog = NULL;
1322
1323   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1324   g_return_if_fail (message != NULL);
1325   g_return_if_fail (processes != NULL);
1326   g_return_if_fail (choices != NULL);
1327
1328   priv = GTK_MOUNT_OPERATION (op)->priv;
1329
1330   if (priv->process_list_store == NULL)
1331     {
1332       /* need to create the dialog */
1333       dialog = create_show_processes_dialog (op, message, choices);
1334     }
1335
1336   /* otherwise, we're showing the dialog, assume messages+choices hasn't changed */
1337
1338   update_process_list_store (GTK_MOUNT_OPERATION (op),
1339                              priv->process_list_store,
1340                              processes);
1341
1342   if (dialog != NULL)
1343     {
1344       gtk_widget_show_all (dialog);
1345     }
1346 }
1347
1348 static void
1349 gtk_mount_operation_aborted (GMountOperation *op)
1350 {
1351   GtkMountOperationPrivate *priv;
1352
1353   priv = GTK_MOUNT_OPERATION (op)->priv;
1354
1355   if (priv->dialog != NULL)
1356     {
1357       gtk_widget_destroy (GTK_WIDGET (priv->dialog));
1358       priv->dialog = NULL;
1359       g_object_notify (G_OBJECT (op), "is-showing");
1360       g_object_unref (op);
1361     }
1362 }
1363
1364 /**
1365  * gtk_mount_operation_new:
1366  * @parent: (allow-none): transient parent of the window, or %NULL
1367  *
1368  * Creates a new #GtkMountOperation
1369  *
1370  * Returns: a new #GtkMountOperation
1371  *
1372  * Since: 2.14
1373  */
1374 GMountOperation *
1375 gtk_mount_operation_new (GtkWindow *parent)
1376 {
1377   GMountOperation *mount_operation;
1378
1379   mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
1380                                   "parent", parent, NULL);
1381
1382   return mount_operation;
1383 }
1384
1385 /**
1386  * gtk_mount_operation_is_showing:
1387  * @op: a #GtkMountOperation
1388  *
1389  * Returns whether the #GtkMountOperation is currently displaying
1390  * a window.
1391  *
1392  * Returns: %TRUE if @op is currently displaying a window
1393  *
1394  * Since: 2.14
1395  */
1396 gboolean
1397 gtk_mount_operation_is_showing (GtkMountOperation *op)
1398 {
1399   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
1400
1401   return op->priv->dialog != NULL;
1402 }
1403
1404 /**
1405  * gtk_mount_operation_set_parent:
1406  * @op: a #GtkMountOperation
1407  * @parent: (allow-none): transient parent of the window, or %NULL
1408  *
1409  * Sets the transient parent for windows shown by the
1410  * #GtkMountOperation.
1411  *
1412  * Since: 2.14
1413  */
1414 void
1415 gtk_mount_operation_set_parent (GtkMountOperation *op,
1416                                 GtkWindow         *parent)
1417 {
1418   GtkMountOperationPrivate *priv;
1419
1420   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1421   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
1422
1423   priv = op->priv;
1424
1425   if (priv->parent_window == parent)
1426     return;
1427
1428   if (priv->parent_window)
1429     {
1430       g_signal_handlers_disconnect_by_func (priv->parent_window,
1431                                             gtk_widget_destroyed,
1432                                             &priv->parent_window);
1433       priv->parent_window = NULL;
1434     }
1435
1436   if (parent)
1437     {
1438       priv->parent_window = g_object_ref (parent);
1439
1440       g_signal_connect (parent, "destroy",
1441                         G_CALLBACK (gtk_widget_destroyed),
1442                         &priv->parent_window);
1443
1444       if (priv->dialog)
1445         gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
1446     }
1447
1448   g_object_notify (G_OBJECT (op), "parent");
1449 }
1450
1451 /**
1452  * gtk_mount_operation_get_parent:
1453  * @op: a #GtkMountOperation
1454  *
1455  * Gets the transient parent used by the #GtkMountOperation
1456  *
1457  * Returns: (transfer none): the transient parent for windows shown by @op
1458  *
1459  * Since: 2.14
1460  */
1461 GtkWindow *
1462 gtk_mount_operation_get_parent (GtkMountOperation *op)
1463 {
1464   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1465
1466   return op->priv->parent_window;
1467 }
1468
1469 /**
1470  * gtk_mount_operation_set_screen:
1471  * @op: a #GtkMountOperation
1472  * @screen: a #GdkScreen
1473  *
1474  * Sets the screen to show windows of the #GtkMountOperation on.
1475  *
1476  * Since: 2.14
1477  */
1478 void
1479 gtk_mount_operation_set_screen (GtkMountOperation *op,
1480                                 GdkScreen         *screen)
1481 {
1482   GtkMountOperationPrivate *priv;
1483
1484   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1485   g_return_if_fail (GDK_IS_SCREEN (screen));
1486
1487   priv = op->priv;
1488
1489   if (priv->screen == screen)
1490     return;
1491
1492   if (priv->screen)
1493     g_object_unref (priv->screen);
1494
1495   priv->screen = g_object_ref (screen);
1496
1497   if (priv->dialog)
1498     gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
1499
1500   g_object_notify (G_OBJECT (op), "screen");
1501 }
1502
1503 /**
1504  * gtk_mount_operation_get_screen:
1505  * @op: a #GtkMountOperation
1506  *
1507  * Gets the screen on which windows of the #GtkMountOperation
1508  * will be shown.
1509  *
1510  * Returns: (transfer none): the screen on which windows of @op are shown
1511  *
1512  * Since: 2.14
1513  */
1514 GdkScreen *
1515 gtk_mount_operation_get_screen (GtkMountOperation *op)
1516 {
1517   GtkMountOperationPrivate *priv;
1518
1519   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1520
1521   priv = op->priv;
1522
1523   if (priv->dialog)
1524     return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
1525   else if (priv->parent_window)
1526     return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
1527   else if (priv->screen)
1528     return priv->screen;
1529   else
1530     return gdk_screen_get_default ();
1531 }