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