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