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