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