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