]> Pileus Git - ~andy/gtk/blob - gtk/gtkmountoperation.c
Merge branch 'windows_list'
[~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_dialog_set_has_separator (dialog, FALSE);
481   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
482   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
483   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
484   gtk_box_set_spacing (GTK_BOX (action_area), 6);
485
486   gtk_window_set_resizable (window, FALSE);
487   gtk_window_set_title (window, "");
488   gtk_window_set_icon_name (window, GTK_STOCK_DIALOG_AUTHENTICATION);
489
490   gtk_dialog_add_buttons (dialog,
491                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
492                           _("Co_nnect"), GTK_RESPONSE_OK,
493                           NULL);
494   gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
495
496   gtk_dialog_set_alternative_button_order (dialog,
497                                            GTK_RESPONSE_OK,
498                                            GTK_RESPONSE_CANCEL,
499                                            -1);
500
501   /* Build contents */
502   hbox = gtk_hbox_new (FALSE, 12);
503   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
504   gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
505
506   icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
507                                    GTK_ICON_SIZE_DIALOG);
508
509   gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
510   gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
511
512   main_vbox = gtk_vbox_new (FALSE, 18);
513   gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
514
515   secondary = strstr (message, "\n");
516   if (secondary != NULL)
517     {
518       gchar *s;
519       gchar *primary;
520
521       primary = g_strndup (message, secondary - message + 1);
522       s = g_strdup_printf ("<big><b>%s</b></big>%s", primary, secondary);
523
524       message_label = gtk_label_new (NULL);
525       gtk_label_set_markup (GTK_LABEL (message_label), s);
526       gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5);
527       gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
528       gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
529                           FALSE, TRUE, 0);
530
531       g_free (s);
532       g_free (primary);
533     }
534   else
535     {
536       message_label = gtk_label_new (message);
537       gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5);
538       gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
539       gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
540                           FALSE, FALSE, 0);
541     }
542
543   vbox = gtk_vbox_new (FALSE, 6);
544   gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
545
546   can_anonymous = flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
547
548   priv->anonymous_toggle = NULL;
549   if (can_anonymous)
550     {
551       GtkWidget *anon_box;
552       GtkWidget *choice;
553       GSList    *group;
554
555       anon_box = gtk_vbox_new (FALSE, 6);
556       gtk_box_pack_start (GTK_BOX (vbox), anon_box,
557                           FALSE, FALSE, 0);
558
559       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Connect _anonymously"));
560       gtk_box_pack_start (GTK_BOX (anon_box),
561                           choice,
562                           FALSE, FALSE, 0);
563       g_signal_connect (choice, "toggled",
564                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
565       priv->anonymous_toggle = choice;
566
567       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
568       choice = gtk_radio_button_new_with_mnemonic (group, _("Connect as u_ser:"));
569       gtk_box_pack_start (GTK_BOX (anon_box),
570                           choice,
571                           FALSE, FALSE, 0);
572       g_signal_connect (choice, "toggled",
573                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
574     }
575
576   rows = 0;
577
578   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
579     rows++;
580
581   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
582     rows++;
583
584   if (flags &G_ASK_PASSWORD_NEED_DOMAIN)
585     rows++;
586
587   /* The table that holds the entries */
588   entry_container = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
589
590   gtk_alignment_set_padding (GTK_ALIGNMENT (entry_container),
591                              0, 0, can_anonymous ? 12 : 0, 0);
592
593   gtk_box_pack_start (GTK_BOX (vbox), entry_container,
594                       FALSE, FALSE, 0);
595   priv->entry_container = entry_container;
596
597   table = gtk_table_new (rows, 2, FALSE);
598   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
599   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
600   gtk_container_add (GTK_CONTAINER (entry_container), table);
601
602   rows = 0;
603
604   priv->username_entry = NULL;
605   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
606     priv->username_entry = table_add_entry (table, rows++, _("_Username:"),
607                                             default_user, operation);
608
609   priv->domain_entry = NULL;
610   if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
611     priv->domain_entry = table_add_entry (table, rows++, _("_Domain:"),
612                                           default_domain, operation);
613
614   priv->password_entry = NULL;
615   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
616     {
617       priv->password_entry = table_add_entry (table, rows++, _("_Password:"),
618                                               NULL, operation);
619       gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
620     }
621
622    if (flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
623     {
624       GtkWidget    *choice;
625       GtkWidget    *remember_box;
626       GSList       *group;
627       GPasswordSave password_save;
628
629       remember_box = gtk_vbox_new (FALSE, 6);
630       gtk_box_pack_start (GTK_BOX (vbox), remember_box,
631                           FALSE, FALSE, 0);
632
633       password_save = g_mount_operation_get_password_save (mount_op);
634       
635       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
636       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
637                                     password_save == G_PASSWORD_SAVE_NEVER);
638       g_object_set_data (G_OBJECT (choice), "password-save",
639                          GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
640       g_signal_connect (choice, "toggled",
641                         G_CALLBACK (remember_button_toggled), operation);
642       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
643
644       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
645       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
646       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
647                                     password_save == G_PASSWORD_SAVE_FOR_SESSION);
648       g_object_set_data (G_OBJECT (choice), "password-save",
649                          GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
650       g_signal_connect (choice, "toggled",
651                         G_CALLBACK (remember_button_toggled), operation);
652       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
653
654       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
655       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
656       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
657                                     password_save == G_PASSWORD_SAVE_PERMANENTLY);
658       g_object_set_data (G_OBJECT (choice), "password-save",
659                          GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
660       g_signal_connect (choice, "toggled",
661                         G_CALLBACK (remember_button_toggled), operation);
662       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
663     }
664
665   g_signal_connect (G_OBJECT (dialog), "response",
666                     G_CALLBACK (pw_dialog_got_response), operation);
667
668   if (can_anonymous)
669     {
670       /* The anonymous option will be active by default,
671        * ensure the toggled signal is emitted for it.
672        */
673       gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
674     }
675   else if (! pw_dialog_input_is_valid (operation))
676     gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
677
678   g_object_notify (G_OBJECT (operation), "is-showing");
679
680   if (priv->parent_window)
681     {
682       gtk_window_set_transient_for (window, priv->parent_window);
683       gtk_window_set_modal (window, TRUE);
684     }
685   else if (priv->screen)
686     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
687
688   gtk_widget_show_all (GTK_WIDGET (dialog));
689
690   g_object_ref (operation);
691 }
692
693 static void
694 question_dialog_button_clicked (GtkDialog       *dialog,
695                                 gint             button_number,
696                                 GMountOperation *op)
697 {
698   GtkMountOperationPrivate *priv;
699   GtkMountOperation *operation;
700
701   operation = GTK_MOUNT_OPERATION (op);
702   priv = operation->priv;
703
704   if (button_number >= 0)
705     {
706       g_mount_operation_set_choice (op, button_number);
707       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
708     }
709   else
710     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
711
712   priv->dialog = NULL;
713   g_object_notify (G_OBJECT (operation), "is-showing");
714   gtk_widget_destroy (GTK_WIDGET (dialog));
715   g_object_unref (op);
716 }
717
718 static void
719 gtk_mount_operation_ask_question (GMountOperation *op,
720                                   const char      *message,
721                                   const char      *choices[])
722 {
723   GtkMountOperationPrivate *priv;
724   GtkWidget  *dialog;
725   const char *secondary = NULL;
726   char       *primary;
727   int        count, len = 0;
728
729   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
730   g_return_if_fail (message != NULL);
731   g_return_if_fail (choices != NULL);
732
733   priv = GTK_MOUNT_OPERATION (op)->priv;
734
735   primary = strstr (message, "\n");
736   if (primary)
737     {
738       secondary = primary + 1;
739       primary = g_strndup (message, primary - message);
740     }
741
742   dialog = gtk_message_dialog_new (priv->parent_window, 0,
743                                    GTK_MESSAGE_QUESTION,
744                                    GTK_BUTTONS_NONE, "%s",
745                                    primary != NULL ? primary : message);
746   g_free (primary);
747
748   if (secondary)
749     gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
750                                               "%s", secondary);
751
752   /* First count the items in the list then
753    * add the buttons in reverse order */
754
755   while (choices[len] != NULL)
756     len++;
757
758   for (count = len - 1; count >= 0; count--)
759     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
760
761   g_signal_connect (G_OBJECT (dialog), "response",
762                     G_CALLBACK (question_dialog_button_clicked), op);
763
764   priv->dialog = GTK_DIALOG (dialog);
765   g_object_notify (G_OBJECT (op), "is-showing");
766
767   if (priv->parent_window == NULL && priv->screen)
768     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
769
770   gtk_widget_show (dialog);
771   g_object_ref (op);
772 }
773
774 static void
775 show_processes_button_clicked (GtkDialog       *dialog,
776                                gint             button_number,
777                                GMountOperation *op)
778 {
779   GtkMountOperationPrivate *priv;
780   GtkMountOperation *operation;
781
782   operation = GTK_MOUNT_OPERATION (op);
783   priv = operation->priv;
784
785   if (button_number >= 0)
786     {
787       g_mount_operation_set_choice (op, button_number);
788       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
789     }
790   else
791     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
792
793   priv->dialog = NULL;
794   g_object_notify (G_OBJECT (operation), "is-showing");
795   gtk_widget_destroy (GTK_WIDGET (dialog));
796   g_object_unref (op);
797 }
798
799 static gint
800 pid_equal (gconstpointer a,
801            gconstpointer b)
802 {
803   GPid pa, pb;
804
805   pa = *((GPid *) a);
806   pb = *((GPid *) b);
807
808   return GPOINTER_TO_INT(pb) - GPOINTER_TO_INT(pa);
809 }
810
811 static void
812 diff_sorted_arrays (GArray         *array1,
813                     GArray         *array2,
814                     GCompareFunc   compare,
815                     GArray         *added_indices,
816                     GArray         *removed_indices)
817 {
818   gint order;
819   guint n1, n2;
820   guint elem_size;
821
822   n1 = n2 = 0;
823
824   elem_size = g_array_get_element_size (array1);
825   g_assert (elem_size == g_array_get_element_size (array2));
826
827   while (n1 < array1->len && n2 < array2->len)
828     {
829       order = (*compare) (((const char*) array1->data) + n1 * elem_size,
830                           ((const char*) array2->data) + n2 * elem_size);
831       if (order < 0)
832         {
833           g_array_append_val (removed_indices, n1);
834           n1++;
835         }
836       else if (order > 0)
837         {
838           g_array_append_val (added_indices, n2);
839           n2++;
840         }
841       else
842         { /* same item */
843           n1++;
844           n2++;
845         }
846     }
847
848   while (n1 < array1->len)
849     {
850       g_array_append_val (removed_indices, n1);
851       n1++;
852     }
853   while (n2 < array2->len)
854     {
855       g_array_append_val (added_indices, n2);
856       n2++;
857     }
858 }
859
860
861 static void
862 add_pid_to_process_list_store (GtkMountOperation              *mount_operation,
863                                GtkMountOperationLookupContext *lookup_context,
864                                GtkListStore                   *list_store,
865                                GPid                            pid)
866 {
867   gchar *command_line;
868   gchar *name;
869   GdkPixbuf *pixbuf;
870   gchar *markup;
871   GtkTreeIter iter;
872
873   name = NULL;
874   pixbuf = NULL;
875   command_line = NULL;
876   _gtk_mount_operation_lookup_info (lookup_context,
877                                     pid,
878                                     24,
879                                     &name,
880                                     &command_line,
881                                     &pixbuf);
882
883   if (name == NULL)
884     name = g_strdup_printf (_("Unknown Application (pid %d)"), pid);
885
886   if (command_line == NULL)
887     command_line = g_strdup ("");
888
889   if (pixbuf == NULL)
890     {
891       GtkIconTheme *theme;
892       theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (mount_operation->priv->dialog)));
893       pixbuf = gtk_icon_theme_load_icon (theme,
894                                          "application-x-executable",
895                                          24,
896                                          0,
897                                          NULL);
898     }
899
900   markup = g_strdup_printf ("<b>%s</b>\n"
901                             "<small>%s</small>",
902                             name,
903                             command_line);
904
905   gtk_list_store_append (list_store, &iter);
906   gtk_list_store_set (list_store, &iter,
907                       0, pixbuf,
908                       1, markup,
909                       2, pid,
910                       -1);
911
912   if (pixbuf != NULL)
913     g_object_unref (pixbuf);
914   g_free (markup);
915   g_free (name);
916   g_free (command_line);
917 }
918
919 static void
920 remove_pid_from_process_list_store (GtkMountOperation *mount_operation,
921                                     GtkListStore      *list_store,
922                                     GPid               pid)
923 {
924   GtkTreeIter iter;
925   GPid pid_of_item;
926
927   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
928     {
929       do
930         {
931           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
932                               &iter,
933                               2, &pid_of_item,
934                               -1);
935
936           if (pid_of_item == pid)
937             {
938               gtk_list_store_remove (list_store, &iter);
939               break;
940             }
941         }
942       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
943     }
944 }
945
946
947 static void
948 update_process_list_store (GtkMountOperation *mount_operation,
949                            GtkListStore      *list_store,
950                            GArray            *processes)
951 {
952   guint n;
953   GtkMountOperationLookupContext *lookup_context;
954   GArray *current_pids;
955   GArray *pid_indices_to_add;
956   GArray *pid_indices_to_remove;
957   GtkTreeIter iter;
958   GPid pid;
959
960   /* Just removing all items and adding new ones will screw up the
961    * focus handling in the treeview - so compute the delta, and add/remove
962    * items as appropriate
963    */
964   current_pids = g_array_new (FALSE, FALSE, sizeof (GPid));
965   pid_indices_to_add = g_array_new (FALSE, FALSE, sizeof (gint));
966   pid_indices_to_remove = g_array_new (FALSE, FALSE, sizeof (gint));
967
968   if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
969     {
970       do
971         {
972           gtk_tree_model_get (GTK_TREE_MODEL (list_store),
973                               &iter,
974                               2, &pid,
975                               -1);
976
977           g_array_append_val (current_pids, pid);
978         }
979       while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
980     }
981
982   g_array_sort (current_pids, pid_equal);
983   g_array_sort (processes, pid_equal);
984
985   diff_sorted_arrays (current_pids, processes, pid_equal, pid_indices_to_add, pid_indices_to_remove);
986
987   for (n = 0; n < pid_indices_to_remove->len; n++)
988     {
989       pid = g_array_index (current_pids, GPid, n);
990       remove_pid_from_process_list_store (mount_operation, list_store, pid);
991     }
992
993   if (pid_indices_to_add->len > 0)
994     {
995       lookup_context = _gtk_mount_operation_lookup_context_get (gtk_widget_get_display (mount_operation->priv->process_tree_view));
996       for (n = 0; n < pid_indices_to_add->len; n++)
997         {
998           pid = g_array_index (processes, GPid, n);
999           add_pid_to_process_list_store (mount_operation, lookup_context, list_store, pid);
1000         }
1001       _gtk_mount_operation_lookup_context_free (lookup_context);
1002     }
1003
1004   /* select the first item, if we went from a zero to a non-zero amount of processes */
1005   if (current_pids->len == 0 && pid_indices_to_add->len > 0)
1006     {
1007       if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1008         {
1009           GtkTreeSelection *tree_selection;
1010           tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mount_operation->priv->process_tree_view));
1011           gtk_tree_selection_select_iter (tree_selection, &iter);
1012         }
1013     }
1014
1015   g_array_unref (current_pids);
1016   g_array_unref (pid_indices_to_add);
1017   g_array_unref (pid_indices_to_remove);
1018 }
1019
1020 static void
1021 on_end_process_activated (GtkMenuItem *item,
1022                           gpointer user_data)
1023 {
1024   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1025   GtkTreeSelection *selection;
1026   GtkTreeIter iter;
1027   GPid pid_to_kill;
1028   GError *error;
1029
1030   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1031
1032   if (!gtk_tree_selection_get_selected (selection,
1033                                         NULL,
1034                                         &iter))
1035     goto out;
1036
1037   gtk_tree_model_get (GTK_TREE_MODEL (op->priv->process_list_store),
1038                       &iter,
1039                       2, &pid_to_kill,
1040                       -1);
1041
1042   /* TODO: We might want to either
1043    *
1044    *       - Be smart about things and send SIGKILL rather than SIGTERM if
1045    *         this is the second time the user requests killing a process
1046    *
1047    *       - Or, easier (but worse user experience), offer both "End Process"
1048    *         and "Terminate Process" options
1049    *
1050    *      But that's not how things work right now....
1051    */
1052   error = NULL;
1053   if (!_gtk_mount_operation_kill_process (pid_to_kill, &error))
1054     {
1055       GtkWidget *dialog;
1056       gint response;
1057
1058       /* Use GTK_DIALOG_DESTROY_WITH_PARENT here since the parent dialog can be
1059        * indeed be destroyed via the GMountOperation::abort signal... for example,
1060        * this is triggered if the user yanks the device while we are showing
1061        * the dialog...
1062        */
1063       dialog = gtk_message_dialog_new (GTK_WINDOW (op->priv->dialog),
1064                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1065                                        GTK_MESSAGE_ERROR,
1066                                        GTK_BUTTONS_CLOSE,
1067                                        _("Unable to end process"));
1068       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1069                                                 "%s",
1070                                                 error->message);
1071
1072       gtk_widget_show_all (dialog);
1073       response = gtk_dialog_run (GTK_DIALOG (dialog));
1074
1075       /* GTK_RESPONSE_NONE means the dialog were programmatically destroy, e.g. that
1076        * GTK_DIALOG_DESTROY_WITH_PARENT kicked in - so it would trigger a warning to
1077        * destroy the dialog in that case
1078        */
1079       if (response != GTK_RESPONSE_NONE)
1080         gtk_widget_destroy (dialog);
1081
1082       g_error_free (error);
1083     }
1084
1085  out:
1086   ;
1087 }
1088
1089 static gboolean
1090 do_popup_menu_for_process_tree_view (GtkWidget         *widget,
1091                                      GdkEventButton    *event,
1092                                      GtkMountOperation *op)
1093 {
1094   GtkWidget *menu;
1095   GtkWidget *item;
1096   gint button;
1097   gint event_time;
1098   gboolean popped_up_menu;
1099
1100   popped_up_menu = FALSE;
1101
1102   menu = gtk_menu_new ();
1103
1104   item = gtk_image_menu_item_new_with_mnemonic (_("_End Process"));
1105   gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1106                                  gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
1107   g_signal_connect (item, "activate",
1108                     G_CALLBACK (on_end_process_activated),
1109                     op);
1110   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1111   gtk_widget_show_all (menu);
1112
1113   if (event != NULL)
1114     {
1115       GtkTreePath *path;
1116       GtkTreeSelection *selection;
1117
1118       if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (op->priv->process_tree_view),
1119                                          (gint) event->x,
1120                                          (gint) event->y,
1121                                          &path,
1122                                          NULL,
1123                                          NULL,
1124                                          NULL))
1125         {
1126           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1127           gtk_tree_selection_select_path (selection, path);
1128           gtk_tree_path_free (path);
1129         }
1130       else
1131         {
1132           /* don't popup a menu if the user right-clicked in an area with no rows */
1133           goto out;
1134         }
1135
1136       button = event->button;
1137       event_time = event->time;
1138     }
1139   else
1140     {
1141       button = 0;
1142       event_time = gtk_get_current_event_time ();
1143     }
1144
1145   gtk_menu_popup (GTK_MENU (menu),
1146                   NULL,
1147                   widget,
1148                   NULL,
1149                   NULL,
1150                   button,
1151                   event_time);
1152
1153   popped_up_menu = TRUE;
1154
1155  out:
1156   return popped_up_menu;
1157 }
1158
1159 static gboolean
1160 on_popup_menu_for_process_tree_view (GtkWidget *widget,
1161                                      gpointer   user_data)
1162 {
1163   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1164   return do_popup_menu_for_process_tree_view (widget, NULL, op);
1165 }
1166
1167 static gboolean
1168 on_button_press_event_for_process_tree_view (GtkWidget      *widget,
1169                                              GdkEventButton *event,
1170                                              gpointer        user_data)
1171 {
1172   GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1173   gboolean ret;
1174
1175   ret = FALSE;
1176
1177   /* Ignore double-clicks and triple-clicks */
1178   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1179     {
1180       ret = do_popup_menu_for_process_tree_view (widget, event, op);
1181     }
1182
1183   return ret;
1184 }
1185
1186 static void
1187 create_show_processes_dialog (GMountOperation *op,
1188                               const char      *message,
1189                               const char      *choices[])
1190 {
1191   GtkMountOperationPrivate *priv;
1192   GtkWidget  *dialog;
1193   const char *secondary = NULL;
1194   char       *primary;
1195   int        count, len = 0;
1196   GtkWidget *label;
1197   GtkWidget *tree_view;
1198   GtkWidget *scrolled_window;
1199   GtkWidget *vbox;
1200   GtkWidget *content_area;
1201   GtkTreeViewColumn *column;
1202   GtkCellRenderer *renderer;
1203   GtkListStore *list_store;
1204   gchar *s;
1205
1206   priv = GTK_MOUNT_OPERATION (op)->priv;
1207
1208   primary = strstr (message, "\n");
1209   if (primary)
1210     {
1211       secondary = primary + 1;
1212       primary = g_strndup (message, primary - message);
1213     }
1214
1215   dialog = gtk_dialog_new ();
1216
1217   if (priv->parent_window != NULL)
1218     gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->parent_window);
1219   gtk_window_set_title (GTK_WINDOW (dialog), "");
1220   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1221
1222   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1223   vbox = gtk_vbox_new (FALSE, 12);
1224   gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1225   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
1226
1227   if (secondary != NULL)
1228     {
1229       s = g_strdup_printf ("<big><b>%s</b></big>\n\n%s", primary, secondary);
1230     }
1231   else
1232     {
1233       s = g_strdup_printf ("%s", primary);
1234     }
1235   g_free (primary);
1236   label = gtk_label_new (NULL);
1237   gtk_label_set_markup (GTK_LABEL (label), s);
1238   g_free (s);
1239   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1240
1241   /* First count the items in the list then
1242    * add the buttons in reverse order */
1243
1244   while (choices[len] != NULL)
1245     len++;
1246
1247   for (count = len - 1; count >= 0; count--)
1248     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
1249
1250   g_signal_connect (G_OBJECT (dialog), "response",
1251                     G_CALLBACK (show_processes_button_clicked), op);
1252
1253   priv->dialog = GTK_DIALOG (dialog);
1254   g_object_notify (G_OBJECT (op), "is-showing");
1255
1256   if (priv->parent_window == NULL && priv->screen)
1257     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
1258
1259   tree_view = gtk_tree_view_new ();
1260   /* TODO: should use EM's when gtk+ RI patches land */
1261   gtk_widget_set_size_request (tree_view,
1262                                300,
1263                                120);
1264
1265   column = gtk_tree_view_column_new ();
1266   renderer = gtk_cell_renderer_pixbuf_new ();
1267   gtk_tree_view_column_pack_start (column, renderer, FALSE);
1268   gtk_tree_view_column_set_attributes (column, renderer,
1269                                        "pixbuf", 0,
1270                                        NULL);
1271   renderer = gtk_cell_renderer_text_new ();
1272   g_object_set (renderer,
1273                 "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
1274                 "ellipsize-set", TRUE,
1275                 NULL);
1276   gtk_tree_view_column_pack_start (column, renderer, TRUE);
1277   gtk_tree_view_column_set_attributes (column, renderer,
1278                                        "markup", 1,
1279                                        NULL);
1280   gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
1281   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
1282
1283
1284   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1285   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1286                                   GTK_POLICY_NEVER,
1287                                   GTK_POLICY_AUTOMATIC);
1288   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
1289
1290   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
1291   gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
1292
1293   g_signal_connect (tree_view, "popup-menu",
1294                     G_CALLBACK (on_popup_menu_for_process_tree_view),
1295                     op);
1296   g_signal_connect (tree_view, "button-press-event",
1297                     G_CALLBACK (on_button_press_event_for_process_tree_view),
1298                     op);
1299
1300   list_store = gtk_list_store_new (3,
1301                                    GDK_TYPE_PIXBUF,
1302                                    G_TYPE_STRING,
1303                                    G_TYPE_INT);
1304
1305   gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store));
1306
1307   priv->process_list_store = list_store;
1308   priv->process_tree_view = tree_view;
1309   /* set pointers to NULL when dialog goes away */
1310   g_object_add_weak_pointer (G_OBJECT (list_store), (gpointer *) &priv->process_list_store);
1311   g_object_add_weak_pointer (G_OBJECT (tree_view), (gpointer *) &priv->process_tree_view);
1312
1313   g_object_unref (list_store);
1314
1315   gtk_widget_show_all (dialog);
1316   g_object_ref (op);
1317 }
1318
1319 static void
1320 gtk_mount_operation_show_processes (GMountOperation *op,
1321                                     const char      *message,
1322                                     GArray          *processes,
1323                                     const char      *choices[])
1324 {
1325   GtkMountOperationPrivate *priv;
1326
1327   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1328   g_return_if_fail (message != NULL);
1329   g_return_if_fail (processes != NULL);
1330   g_return_if_fail (choices != NULL);
1331
1332   priv = GTK_MOUNT_OPERATION (op)->priv;
1333
1334   if (priv->process_list_store == NULL)
1335     {
1336       /* need to create the dialog */
1337       create_show_processes_dialog (op, message, choices);
1338     }
1339
1340   /* otherwise, we're showing the dialog, assume messages+choices hasn't changed */
1341
1342   update_process_list_store (GTK_MOUNT_OPERATION (op),
1343                              priv->process_list_store,
1344                              processes);
1345 }
1346
1347 static void
1348 gtk_mount_operation_aborted (GMountOperation *op)
1349 {
1350   GtkMountOperationPrivate *priv;
1351
1352   priv = GTK_MOUNT_OPERATION (op)->priv;
1353
1354   if (priv->dialog != NULL)
1355     {
1356       gtk_widget_destroy (GTK_WIDGET (priv->dialog));
1357       priv->dialog = NULL;
1358       g_object_notify (G_OBJECT (op), "is-showing");
1359       g_object_unref (op);
1360     }
1361 }
1362
1363 /**
1364  * gtk_mount_operation_new:
1365  * @parent: (allow-none): transient parent of the window, or %NULL
1366  *
1367  * Creates a new #GtkMountOperation
1368  *
1369  * Returns: a new #GtkMountOperation
1370  *
1371  * Since: 2.14
1372  */
1373 GMountOperation *
1374 gtk_mount_operation_new (GtkWindow *parent)
1375 {
1376   GMountOperation *mount_operation;
1377
1378   mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
1379                                   "parent", parent, NULL);
1380
1381   return mount_operation;
1382 }
1383
1384 /**
1385  * gtk_mount_operation_is_showing:
1386  * @op: a #GtkMountOperation
1387  *
1388  * Returns whether the #GtkMountOperation is currently displaying
1389  * a window.
1390  *
1391  * Returns: %TRUE if @op is currently displaying a window
1392  *
1393  * Since: 2.14
1394  */
1395 gboolean
1396 gtk_mount_operation_is_showing (GtkMountOperation *op)
1397 {
1398   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
1399
1400   return op->priv->dialog != NULL;
1401 }
1402
1403 /**
1404  * gtk_mount_operation_set_parent:
1405  * @op: a #GtkMountOperation
1406  * @parent: (allow-none): transient parent of the window, or %NULL
1407  *
1408  * Sets the transient parent for windows shown by the
1409  * #GtkMountOperation.
1410  *
1411  * Since: 2.14
1412  */
1413 void
1414 gtk_mount_operation_set_parent (GtkMountOperation *op,
1415                                 GtkWindow         *parent)
1416 {
1417   GtkMountOperationPrivate *priv;
1418
1419   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1420   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
1421
1422   priv = op->priv;
1423
1424   if (priv->parent_window == parent)
1425     return;
1426
1427   if (priv->parent_window)
1428     {
1429       g_signal_handlers_disconnect_by_func (priv->parent_window,
1430                                             gtk_widget_destroyed,
1431                                             &priv->parent_window);
1432       priv->parent_window = NULL;
1433     }
1434
1435   if (parent)
1436     {
1437       priv->parent_window = g_object_ref (parent);
1438
1439       g_signal_connect (parent, "destroy",
1440                         G_CALLBACK (gtk_widget_destroyed),
1441                         &priv->parent_window);
1442
1443       if (priv->dialog)
1444         gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
1445     }
1446
1447   g_object_notify (G_OBJECT (op), "parent");
1448 }
1449
1450 /**
1451  * gtk_mount_operation_get_parent:
1452  * @op: a #GtkMountOperation
1453  *
1454  * Gets the transient parent used by the #GtkMountOperation
1455  *
1456  * Returns: the transient parent for windows shown by @op
1457  *
1458  * Since: 2.14
1459  */
1460 GtkWindow *
1461 gtk_mount_operation_get_parent (GtkMountOperation *op)
1462 {
1463   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1464
1465   return op->priv->parent_window;
1466 }
1467
1468 /**
1469  * gtk_mount_operation_set_screen:
1470  * @op: a #GtkMountOperation
1471  * @screen: a #GdkScreen
1472  *
1473  * Sets the screen to show windows of the #GtkMountOperation on.
1474  *
1475  * Since: 2.14
1476  */
1477 void
1478 gtk_mount_operation_set_screen (GtkMountOperation *op,
1479                                 GdkScreen         *screen)
1480 {
1481   GtkMountOperationPrivate *priv;
1482
1483   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1484   g_return_if_fail (GDK_IS_SCREEN (screen));
1485
1486   priv = op->priv;
1487
1488   if (priv->screen == screen)
1489     return;
1490
1491   if (priv->screen)
1492     g_object_unref (priv->screen);
1493
1494   priv->screen = g_object_ref (screen);
1495
1496   if (priv->dialog)
1497     gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
1498
1499   g_object_notify (G_OBJECT (op), "screen");
1500 }
1501
1502 /**
1503  * gtk_mount_operation_get_screen:
1504  * @op: a #GtkMountOperation
1505  *
1506  * Gets the screen on which windows of the #GtkMountOperation 
1507  * will be shown.
1508  *
1509  * Returns: the screen on which windows of @op are shown
1510  *
1511  * Since: 2.14
1512  */
1513 GdkScreen *
1514 gtk_mount_operation_get_screen (GtkMountOperation *op)
1515 {
1516   GtkMountOperationPrivate *priv;
1517
1518   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1519
1520   priv = op->priv;
1521
1522   if (priv->dialog)
1523     return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
1524   else if (priv->parent_window)
1525     return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
1526   else if (priv->screen)
1527     return priv->screen;
1528   else
1529     return gdk_screen_get_default ();
1530 }