]> Pileus Git - ~andy/gtk/blob - gtk/gtkmountoperation.c
Require GLib 2.19.7 for GMountOperation::aborted.
[~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 "gtkalignment.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 "gtkalias.h"
48
49 /**
50  * SECTION:filesystem
51  * @short_description: Functions for working with GIO
52  *
53  * The functions and objects described here make working with GTK+ and
54  * GIO more convenient. #GtkMountOperation is needed when mounting volumes
55  * and gtk_show_uri() is a convenient way to launch applications for URIs.
56  * Another object that is worth mentioning in this context is 
57  * #GdkAppLaunchContext, which provides visual feedback when lauching
58  * applications.
59  */
60
61 /** 
62  * GtkMountOperation:
63  *
64  * #GtkMountOperation is an implementation of #GMountOperation that 
65  * can be used with GIO functions for mounting volumes such as
66  * g_file_mount_enclosing_volume() or g_file_mount_mountable().
67  *
68  * When necessary, #GtkMountOperation shows dialogs to ask for passwords.
69  */
70
71 /* GObject, GtkObject methods
72  */
73 static void   gtk_mount_operation_set_property (GObject          *object,
74                                                 guint             prop_id,
75                                                 const GValue     *value,
76                                                 GParamSpec       *pspec);
77 static void   gtk_mount_operation_get_property (GObject          *object,
78                                                 guint             prop_id,
79                                                 GValue           *value,
80                                                 GParamSpec       *pspec);
81 static void   gtk_mount_operation_finalize     (GObject          *object);
82
83 /* GMountOperation methods
84  */
85 static void   gtk_mount_operation_ask_password (GMountOperation *op,
86                                                 const char      *message,
87                                                 const char      *default_user,
88                                                 const char      *default_domain,
89                                                 GAskPasswordFlags flags);
90
91 static void   gtk_mount_operation_ask_question (GMountOperation *op,
92                                                 const char      *message,
93                                                 const char      *choices[]);
94
95 static void   gtk_mount_operation_aborted      (GMountOperation *op);
96
97 G_DEFINE_TYPE (GtkMountOperation, gtk_mount_operation, G_TYPE_MOUNT_OPERATION);
98
99 enum {
100   PROP_0,
101   PROP_PARENT,
102   PROP_IS_SHOWING,
103   PROP_SCREEN
104
105 };
106
107 struct _GtkMountOperationPrivate {
108   GtkWindow *parent_window;
109   GtkDialog *dialog;
110   GdkScreen *screen;
111
112   /* for the ask-password dialog */
113   GtkWidget *entry_container;
114   GtkWidget *username_entry;
115   GtkWidget *domain_entry;
116   GtkWidget *password_entry;
117   GtkWidget *anonymous_toggle;
118
119   GAskPasswordFlags ask_flags;
120   GPasswordSave     password_save;
121   gboolean          anonymous;
122 };
123
124 static void
125 gtk_mount_operation_finalize (GObject *object)
126 {
127   GtkMountOperation *operation;
128   GtkMountOperationPrivate *priv;
129
130   operation = GTK_MOUNT_OPERATION (object);
131
132   priv = operation->priv;
133
134   if (priv->parent_window)
135     g_object_unref (priv->parent_window);
136
137   if (priv->screen)
138     g_object_unref (priv->screen);
139
140   G_OBJECT_CLASS (gtk_mount_operation_parent_class)->finalize (object);
141 }
142
143 static void
144 gtk_mount_operation_class_init (GtkMountOperationClass *klass)
145 {
146   GObjectClass         *object_class = G_OBJECT_CLASS (klass);
147   GMountOperationClass *mount_op_class;
148
149   g_type_class_add_private (klass, sizeof (GtkMountOperationPrivate));
150
151   object_class->finalize     = gtk_mount_operation_finalize;
152   object_class->get_property = gtk_mount_operation_get_property;
153   object_class->set_property = gtk_mount_operation_set_property;
154
155   mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
156   mount_op_class->ask_password = gtk_mount_operation_ask_password;
157   mount_op_class->ask_question = gtk_mount_operation_ask_question;
158   mount_op_class->aborted = gtk_mount_operation_aborted;
159
160   g_object_class_install_property (object_class,
161                                    PROP_PARENT,
162                                    g_param_spec_object ("parent",
163                                                         P_("Parent"),
164                                                         P_("The parent window"),
165                                                         GTK_TYPE_WINDOW,
166                                                         GTK_PARAM_READWRITE));
167
168   g_object_class_install_property (object_class,
169                                    PROP_IS_SHOWING,
170                                    g_param_spec_boolean ("is-showing",
171                                                          P_("Is Showing"),
172                                                          P_("Are we showing a dialog"),
173                                                          FALSE,
174                                                          GTK_PARAM_READABLE));
175
176   g_object_class_install_property (object_class,
177                                    PROP_SCREEN,
178                                    g_param_spec_object ("screen",
179                                                         P_("Screen"),
180                                                         P_("The screen where this window will be displayed."),
181                                                         GDK_TYPE_SCREEN,
182                                                         GTK_PARAM_READWRITE));
183
184 }
185
186
187 static void
188 gtk_mount_operation_set_property (GObject      *object,
189                                   guint         prop_id,
190                                   const GValue *value,
191                                   GParamSpec   *pspec)
192 {
193   GtkMountOperation *operation;
194   gpointer tmp;
195
196   operation = GTK_MOUNT_OPERATION (object);
197
198   switch (prop_id)
199     {
200    case PROP_PARENT:
201      tmp = g_value_get_object (value);
202      gtk_mount_operation_set_parent (operation, tmp);
203      break;
204
205    case PROP_SCREEN:
206       tmp = g_value_get_object (value);
207       gtk_mount_operation_set_screen (operation, tmp);
208      break;
209
210    case PROP_IS_SHOWING:
211    default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214     }
215 }
216
217 static void
218 gtk_mount_operation_get_property (GObject    *object,
219                                   guint       prop_id,
220                                   GValue     *value,
221                                   GParamSpec *pspec)
222 {
223   GtkMountOperationPrivate *priv;
224   GtkMountOperation *operation;
225
226   operation = GTK_MOUNT_OPERATION (object);
227   priv = operation->priv;
228
229   switch (prop_id)
230     {
231     case PROP_PARENT:
232       g_value_set_object (value, priv->parent_window);
233       break;
234
235     case PROP_IS_SHOWING:
236       g_value_set_boolean (value, priv->dialog != NULL);
237       break;
238
239     case PROP_SCREEN:
240       g_value_set_object (value, priv->screen);
241       break;
242
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246     }
247 }
248
249 static void
250 gtk_mount_operation_init (GtkMountOperation *operation)
251 {
252   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
253                                                  GTK_TYPE_MOUNT_OPERATION,
254                                                  GtkMountOperationPrivate);
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;
278   GMountOperation *op;
279
280   priv = mount_op->priv;
281   op = G_MOUNT_OPERATION (mount_op);
282
283   if (response_id == GTK_RESPONSE_OK)
284     {
285       const char *text;
286
287       if (priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED)
288         g_mount_operation_set_anonymous (op, priv->anonymous);
289
290       if (priv->username_entry)
291         {
292           text = gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
293           g_mount_operation_set_username (op, text);
294         }
295
296       if (priv->domain_entry)
297         {
298           text = gtk_entry_get_text (GTK_ENTRY (priv->domain_entry));
299           g_mount_operation_set_domain (op, text);
300         }
301
302       if (priv->password_entry)
303         {
304           text = gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
305           g_mount_operation_set_password (op, text);
306         }
307
308       if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
309         g_mount_operation_set_password_save (op, priv->password_save);
310
311       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
312     }
313   else
314     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
315
316   priv->dialog = NULL;
317   g_object_notify (G_OBJECT (op), "is-showing");
318   gtk_widget_destroy (GTK_WIDGET (dialog));
319   g_object_unref (op);
320 }
321
322 static gboolean
323 entry_has_input (GtkWidget *entry_widget)
324 {
325   const char *text;
326
327   if (entry_widget == NULL)
328     return TRUE;
329
330   text = gtk_entry_get_text (GTK_ENTRY (entry_widget));
331
332   return text != NULL && text[0] != '\0';
333 }
334
335 static gboolean
336 pw_dialog_input_is_valid (GtkMountOperation *operation)
337 {
338   GtkMountOperationPrivate *priv = operation->priv;
339   gboolean is_valid = TRUE;
340
341   is_valid = entry_has_input (priv->username_entry) &&
342              entry_has_input (priv->domain_entry) &&
343              entry_has_input (priv->password_entry);
344
345   return is_valid;
346 }
347
348 static void
349 pw_dialog_verify_input (GtkEditable       *editable,
350                         GtkMountOperation *operation)
351 {
352   GtkMountOperationPrivate *priv = operation->priv;
353   gboolean is_valid;
354
355   is_valid = pw_dialog_input_is_valid (operation);
356   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
357                                      GTK_RESPONSE_OK,
358                                      is_valid);
359 }
360
361 static void
362 pw_dialog_anonymous_toggled (GtkWidget         *widget,
363                              GtkMountOperation *operation)
364 {
365   GtkMountOperationPrivate *priv = operation->priv;
366   gboolean is_valid;
367
368   priv->anonymous = widget == priv->anonymous_toggle;
369
370   if (priv->anonymous)
371     is_valid = TRUE;
372   else
373     is_valid = pw_dialog_input_is_valid (operation);
374
375   gtk_widget_set_sensitive (priv->entry_container, priv->anonymous == FALSE);
376   gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
377                                      GTK_RESPONSE_OK,
378                                      is_valid);
379 }
380
381
382 static void
383 pw_dialog_cycle_focus (GtkWidget         *widget,
384                        GtkMountOperation *operation)
385 {
386   GtkMountOperationPrivate *priv;
387   GtkWidget *next_widget = NULL;
388
389   priv = operation->priv;
390
391   if (widget == priv->username_entry)
392     {
393       if (priv->domain_entry != NULL)
394         next_widget = priv->domain_entry;
395       else if (priv->password_entry != NULL)
396         next_widget = priv->password_entry;
397     }
398   else if (widget == priv->domain_entry && priv->password_entry)
399     next_widget = priv->password_entry;
400
401   if (next_widget)
402     gtk_widget_grab_focus (next_widget);
403   else if (pw_dialog_input_is_valid (operation))
404     gtk_window_activate_default (GTK_WINDOW (priv->dialog));
405 }
406
407 static GtkWidget *
408 table_add_entry (GtkWidget  *table,
409                  int         row,
410                  const char *label_text,
411                  const char *value,
412                  gpointer    user_data)
413 {
414   GtkWidget *entry;
415   GtkWidget *label;
416
417   label = gtk_label_new_with_mnemonic (label_text);
418   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
419
420   entry = gtk_entry_new ();
421
422   if (value)
423     gtk_entry_set_text (GTK_ENTRY (entry), value);
424
425   gtk_table_attach (GTK_TABLE (table), label,
426                     0, 1, row, row + 1,
427                     GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
428   gtk_table_attach_defaults (GTK_TABLE (table), entry,
429                              1, 2, row, row + 1);
430   gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
431
432   g_signal_connect (entry, "changed",
433                     G_CALLBACK (pw_dialog_verify_input), user_data);
434
435   g_signal_connect (entry, "activate",
436                     G_CALLBACK (pw_dialog_cycle_focus), user_data);
437
438   return entry;
439 }
440
441 static void
442 gtk_mount_operation_ask_password (GMountOperation   *mount_op,
443                                   const char        *message,
444                                   const char        *default_user,
445                                   const char        *default_domain,
446                                   GAskPasswordFlags  flags)
447 {
448   GtkMountOperation *operation;
449   GtkMountOperationPrivate *priv;
450   GtkWidget *widget;
451   GtkDialog *dialog;
452   GtkWindow *window;
453   GtkWidget *entry_container;
454   GtkWidget *hbox, *main_vbox, *vbox, *icon;
455   GtkWidget *table;
456   GtkWidget *message_label;
457   gboolean   can_anonymous;
458   guint      rows;
459
460   operation = GTK_MOUNT_OPERATION (mount_op);
461   priv = operation->priv;
462
463   priv->ask_flags = flags;
464
465   widget = gtk_dialog_new ();
466   dialog = GTK_DIALOG (widget);
467   window = GTK_WINDOW (widget);
468
469   priv->dialog = dialog;
470
471   /* Set the dialog up with HIG properties */
472   gtk_dialog_set_has_separator (dialog, FALSE);
473   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
474   gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
475   gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
476   gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
477
478   gtk_window_set_resizable (window, FALSE);
479   gtk_window_set_title (window, "");
480   gtk_window_set_icon_name (window, GTK_STOCK_DIALOG_AUTHENTICATION);
481
482   gtk_dialog_add_buttons (dialog,
483                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
484                           _("Co_nnect"), GTK_RESPONSE_OK,
485                           NULL);
486   gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
487
488   gtk_dialog_set_alternative_button_order (dialog,
489                                            GTK_RESPONSE_OK,
490                                            GTK_RESPONSE_CANCEL,
491                                            -1);
492
493   /* Build contents */
494   hbox = gtk_hbox_new (FALSE, 12);
495   gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
496   gtk_box_pack_start (GTK_BOX (dialog->vbox), hbox, TRUE, TRUE, 0);
497
498   icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
499                                    GTK_ICON_SIZE_DIALOG);
500
501   gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
502   gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
503
504   main_vbox = gtk_vbox_new (FALSE, 18);
505   gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
506
507   message_label = gtk_label_new (message);
508   gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5);
509   gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
510   gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
511                       FALSE, FALSE, 0);
512
513   vbox = gtk_vbox_new (FALSE, 6);
514   gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
515
516   can_anonymous = flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
517
518   if (can_anonymous)
519     {
520       GtkWidget *anon_box;
521       GtkWidget *choice;
522       GSList    *group;
523
524       anon_box = gtk_vbox_new (FALSE, 6);
525       gtk_box_pack_start (GTK_BOX (vbox), anon_box,
526                           FALSE, FALSE, 0);
527
528       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Connect _anonymously"));
529       gtk_box_pack_start (GTK_BOX (anon_box),
530                           choice,
531                           FALSE, FALSE, 0);
532       g_signal_connect (choice, "toggled",
533                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
534       priv->anonymous_toggle = choice;
535
536       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
537       choice = gtk_radio_button_new_with_mnemonic (group, _("Connect as u_ser:"));
538       gtk_box_pack_start (GTK_BOX (anon_box),
539                           choice,
540                           FALSE, FALSE, 0);
541       g_signal_connect (choice, "toggled",
542                         G_CALLBACK (pw_dialog_anonymous_toggled), operation);
543     }
544
545   rows = 0;
546
547   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
548     rows++;
549
550   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
551     rows++;
552
553   if (flags &G_ASK_PASSWORD_NEED_DOMAIN)
554     rows++;
555
556   /* The table that holds the entries */
557   entry_container = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
558
559   gtk_alignment_set_padding (GTK_ALIGNMENT (entry_container),
560                              0, 0, can_anonymous ? 12 : 0, 0);
561
562   gtk_box_pack_start (GTK_BOX (vbox), entry_container,
563                       FALSE, FALSE, 0);
564   priv->entry_container = entry_container;
565
566   table = gtk_table_new (rows, 2, FALSE);
567   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
568   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
569   gtk_container_add (GTK_CONTAINER (entry_container), table);
570
571   rows = 0;
572
573   if (flags & G_ASK_PASSWORD_NEED_USERNAME)
574     priv->username_entry = table_add_entry (table, rows++, _("_Username:"),
575                                             default_user, operation);
576
577   if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
578     priv->domain_entry = table_add_entry (table, rows++, _("_Domain:"),
579                                           default_domain, operation);
580
581   if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
582     {
583       priv->password_entry = table_add_entry (table, rows++, _("_Password:"),
584                                               NULL, operation);
585       gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
586     }
587
588    if (flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
589     {
590       GtkWidget    *choice;
591       GtkWidget    *remember_box;
592       GSList       *group;
593       GPasswordSave password_save;
594
595       remember_box = gtk_vbox_new (FALSE, 6);
596       gtk_box_pack_start (GTK_BOX (vbox), remember_box,
597                           FALSE, FALSE, 0);
598
599       password_save = g_mount_operation_get_password_save (mount_op);
600       
601       choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
602       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
603                                     password_save == G_PASSWORD_SAVE_NEVER);
604       g_object_set_data (G_OBJECT (choice), "password-save",
605                          GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
606       g_signal_connect (choice, "toggled",
607                         G_CALLBACK (remember_button_toggled), operation);
608       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
609
610       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
611       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
612       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
613                                     password_save == G_PASSWORD_SAVE_FOR_SESSION);
614       g_object_set_data (G_OBJECT (choice), "password-save",
615                          GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
616       g_signal_connect (choice, "toggled",
617                         G_CALLBACK (remember_button_toggled), operation);
618       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
619
620       group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
621       choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
622       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
623                                     password_save == G_PASSWORD_SAVE_PERMANENTLY);
624       g_object_set_data (G_OBJECT (choice), "password-save",
625                          GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
626       g_signal_connect (choice, "toggled",
627                         G_CALLBACK (remember_button_toggled), operation);
628       gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
629     }
630
631   g_signal_connect (G_OBJECT (dialog), "response",
632                     G_CALLBACK (pw_dialog_got_response), operation);
633
634   if (can_anonymous)
635     {
636       /* The anonymous option will be active by default,
637        * ensure the toggled signal is emitted for it.
638        */
639       gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
640     }
641   else if (! pw_dialog_input_is_valid (operation))
642     gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
643
644   g_object_notify (G_OBJECT (operation), "is-showing");
645
646   if (priv->parent_window)
647     {
648       gtk_window_set_transient_for (window, priv->parent_window);
649       gtk_window_set_modal (window, TRUE);
650     }
651   else if (priv->screen)
652     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
653
654   gtk_widget_show_all (GTK_WIDGET (dialog));
655
656   g_object_ref (operation);
657 }
658
659 static void
660 question_dialog_button_clicked (GtkDialog       *dialog,
661                                 gint             button_number,
662                                 GMountOperation *op)
663 {
664   GtkMountOperationPrivate *priv;
665   GtkMountOperation *operation;
666
667   operation = GTK_MOUNT_OPERATION (op);
668   priv = operation->priv;
669
670   if (button_number >= 0)
671     {
672       g_mount_operation_set_choice (op, button_number);
673       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
674     }
675   else
676     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
677
678   priv->dialog = NULL;
679   g_object_notify (G_OBJECT (operation), "is-showing");
680   gtk_widget_destroy (GTK_WIDGET (dialog));
681   g_object_unref (op);
682 }
683
684 static void
685 gtk_mount_operation_ask_question (GMountOperation *op,
686                                   const char      *message,
687                                   const char      *choices[])
688 {
689   GtkMountOperationPrivate *priv;
690   GtkWidget  *dialog;
691   const char *secondary = NULL;
692   char       *primary;
693   int        count, len = 0;
694
695   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
696   g_return_if_fail (message != NULL);
697   g_return_if_fail (choices != NULL);
698
699   priv = GTK_MOUNT_OPERATION (op)->priv;
700
701   primary = strstr (message, "\n");
702   if (primary)
703     {
704       secondary = primary + 1;
705       primary = g_strndup (message, primary - message);
706     }
707
708   dialog = gtk_message_dialog_new (priv->parent_window, 0,
709                                    GTK_MESSAGE_QUESTION,
710                                    GTK_BUTTONS_NONE, "%s",
711                                    primary != NULL ? primary : message);
712   g_free (primary);
713
714   if (secondary)
715     gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
716                                               "%s", secondary);
717
718   /* First count the items in the list then
719    * add the buttons in reverse order */
720
721   while (choices[len] != NULL)
722     len++;
723
724   for (count = len - 1; count >= 0; count--)
725     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
726
727   g_signal_connect (G_OBJECT (dialog), "response",
728                     G_CALLBACK (question_dialog_button_clicked), op);
729
730   priv->dialog = GTK_DIALOG (dialog);
731   g_object_notify (G_OBJECT (op), "is-showing");
732
733   if (priv->parent_window == NULL && priv->screen)
734     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
735
736   gtk_widget_show (dialog);
737   g_object_ref (op);
738 }
739
740 static void
741 gtk_mount_operation_aborted (GMountOperation *op)
742 {
743   GtkMountOperationPrivate *priv;
744
745   priv = GTK_MOUNT_OPERATION (op)->priv;
746
747   if (priv->dialog != NULL)
748     {
749       gtk_widget_destroy (GTK_WIDGET (priv->dialog));
750       priv->dialog = NULL;
751       g_object_notify (G_OBJECT (op), "is-showing");
752       g_object_unref (op);
753     }
754 }
755
756 /**
757  * gtk_mount_operation_new:
758  * @parent: transient parent of the window, or %NULL
759  *
760  * Creates a new #GtkMountOperation
761  *
762  * Returns: a new #GtkMountOperation
763  *
764  * Since: 2.14
765  */
766 GMountOperation *
767 gtk_mount_operation_new (GtkWindow *parent)
768 {
769   GMountOperation *mount_operation;
770
771   mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
772                                   "parent", parent, NULL);
773
774   return mount_operation;
775 }
776
777 /**
778  * gtk_mount_operation_is_showing:
779  * @op: a #GtkMountOperation
780  *
781  * Returns whether the #GtkMountOperation is currently displaying
782  * a window.
783  *
784  * Returns: %TRUE if @op is currently displaying a window
785  *
786  * Since: 2.14
787  */
788 gboolean
789 gtk_mount_operation_is_showing (GtkMountOperation *op)
790 {
791   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
792
793   return op->priv->dialog != NULL;
794 }
795
796 /**
797  * gtk_mount_operation_set_parent:
798  * @op: a #GtkMountOperation
799  * @parent: transient parent of the window, or %NULL
800  *
801  * Sets the transient parent for windows shown by the
802  * #GtkMountOperation.
803  *
804  * Since: 2.14
805  */
806 void
807 gtk_mount_operation_set_parent (GtkMountOperation *op,
808                                 GtkWindow         *parent)
809 {
810   GtkMountOperationPrivate *priv;
811
812   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
813   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
814
815   priv = op->priv;
816
817   if (priv->parent_window == parent)
818     return;
819
820   if (priv->parent_window)
821     {
822       g_signal_handlers_disconnect_by_func (priv->parent_window,
823                                             gtk_widget_destroyed,
824                                             &priv->parent_window);
825       priv->parent_window = NULL;
826     }
827
828   if (parent)
829     {
830       priv->parent_window = g_object_ref (parent);
831
832       g_signal_connect (parent, "destroy",
833                         G_CALLBACK (gtk_widget_destroyed),
834                         &priv->parent_window);
835
836       if (priv->dialog)
837         gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
838     }
839
840   g_object_notify (G_OBJECT (op), "parent");
841 }
842
843 /**
844  * gtk_mount_operation_get_parent:
845  * @op: a #GtkMountOperation
846  *
847  * Gets the transient parent used by the #GtkMountOperation
848  *
849  * Returns: the transient parent for windows shown by @op
850  *
851  * Since: 2.14
852  */
853 GtkWindow *
854 gtk_mount_operation_get_parent (GtkMountOperation *op)
855 {
856   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
857
858   return op->priv->parent_window;
859 }
860
861 /**
862  * gtk_mount_operation_set_screen:
863  * @op: a #GtkMountOperation
864  * @screen: a #GdkScreen
865  *
866  * Sets the screen to show windows of the #GtkMountOperation on.
867  *
868  * Since: 2.14
869  */
870 void
871 gtk_mount_operation_set_screen (GtkMountOperation *op,
872                                 GdkScreen         *screen)
873 {
874   GtkMountOperationPrivate *priv;
875
876   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
877   g_return_if_fail (GDK_IS_SCREEN (screen));
878
879   priv = op->priv;
880
881   if (priv->screen == screen)
882     return;
883
884   if (priv->screen)
885     g_object_unref (priv->screen);
886
887   priv->screen = g_object_ref (screen);
888
889   if (priv->dialog)
890     gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
891
892   g_object_notify (G_OBJECT (op), "screen");
893 }
894
895 /**
896  * gtk_mount_operation_get_screen:
897  * @op: a #GtkMountOperation
898  *
899  * Gets the screen on which windows of the #GtkMountOperation 
900  * will be shown.
901  *
902  * Returns: the screen on which windows of @op are shown
903  *
904  * Since: 2.14
905  */
906 GdkScreen *
907 gtk_mount_operation_get_screen (GtkMountOperation *op)
908 {
909   GtkMountOperationPrivate *priv;
910
911   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
912
913   priv = op->priv;
914
915   if (priv->dialog)
916     return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
917   else if (priv->parent_window)
918     return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
919   else if (priv->screen)
920     return priv->screen;
921   else
922     return gdk_screen_get_default ();
923 }
924
925 #define __GTK_MOUNT_OPERATION_C__
926 #include "gtkaliasdef.c"