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