]> Pileus Git - ~andy/gtk/blob - gtk/gtkmountoperation.c
Revert name change
[~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     {
619       /* The anonymous option will be active by default,
620        * ensure the toggled signal is emitted for it.
621        */
622       gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
623     }
624   else if (! pw_dialog_input_is_valid (operation))
625     gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
626
627   g_object_notify (G_OBJECT (operation), "is-showing");
628
629   if (priv->parent_window)
630     {
631       gtk_window_set_transient_for (window, priv->parent_window);
632       gtk_window_set_modal (window, TRUE);
633     }
634   else if (priv->screen)
635     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
636
637   gtk_widget_show_all (GTK_WIDGET (dialog));
638
639   g_object_ref (operation);
640 }
641
642 static void
643 question_dialog_button_clicked (GtkDialog       *dialog,
644                                 gint             button_number,
645                                 GMountOperation *op)
646 {
647   GtkMountOperationPrivate *priv;
648   GtkMountOperation *operation;
649
650   operation = GTK_MOUNT_OPERATION (op);
651   priv = operation->priv;
652
653   if (button_number >= 0)
654     {
655       g_mount_operation_set_choice (op, button_number);
656       g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
657     }
658   else
659     g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
660
661   priv->dialog = NULL;
662   g_object_notify (G_OBJECT (operation), "is-showing");
663   gtk_widget_destroy (GTK_WIDGET (dialog));
664   g_object_unref (op);
665 }
666
667 static void
668 gtk_mount_operation_ask_question (GMountOperation *op,
669                                   const char      *message,
670                                   const char      *choices[])
671 {
672   GtkMountOperationPrivate *priv;
673   GtkWidget  *dialog;
674   const char *secondary = NULL;
675   char       *primary;
676   int        count, len = 0;
677
678   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
679   g_return_if_fail (message != NULL);
680   g_return_if_fail (choices != NULL);
681
682   priv = GTK_MOUNT_OPERATION (op)->priv;
683
684   primary = strstr (message, "\n");
685   if (primary)
686     {
687       secondary = primary + 1;
688       primary = g_strndup (message, primary - message);
689     }
690
691   dialog = gtk_message_dialog_new (priv->parent_window, 0,
692                                    GTK_MESSAGE_QUESTION,
693                                    GTK_BUTTONS_NONE, "%s",
694                                    primary != NULL ? primary : message);
695   g_free (primary);
696
697   if (secondary)
698     gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
699                                               "%s", secondary);
700
701   /* First count the items in the list then
702    * add the buttons in reverse order */
703
704   while (choices[len] != NULL)
705     len++;
706
707   for (count = len - 1; count >= 0; count--)
708     gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
709
710   g_signal_connect (G_OBJECT (dialog), "response",
711                     G_CALLBACK (question_dialog_button_clicked), op);
712
713   priv->dialog = GTK_DIALOG (dialog);
714   g_object_notify (G_OBJECT (op), "is-showing");
715
716   if (priv->parent_window == NULL && priv->screen)
717     gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
718
719   gtk_widget_show (dialog);
720   g_object_ref (op);
721 }
722
723 /**
724  * gtk_mount_operation_new:
725  * @parent: transient parent of the window, or %NULL
726  *
727  * Creates a new #GtkMountOperation
728  *
729  * Returns: a new #GtkMountOperation
730  *
731  * Since: 2.14
732  */
733 GMountOperation *
734 gtk_mount_operation_new (GtkWindow *parent)
735 {
736   GMountOperation *mount_operation;
737
738   mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
739                                   "parent", parent, NULL);
740
741   return mount_operation;
742 }
743
744 /**
745  * gtk_mount_operation_is_showing:
746  * @op: a #GtkMountOperation
747  *
748  * Returns whether the #GtkMountOperation is currently displaying
749  * a window.
750  *
751  * Returns: %TRUE if @op is currently displaying a window
752  *
753  * Since: 2.14
754  */
755 gboolean
756 gtk_mount_operation_is_showing (GtkMountOperation *op)
757 {
758   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
759
760   return op->priv->dialog != NULL;
761 }
762
763 /**
764  * gtk_mount_operation_set_parent:
765  * @op: a #GtkMountOperation
766  * @parent: transient parent of the window, or %NULL
767  *
768  * Sets the transient parent for windows shown by the
769  * #GtkMountOperation.
770  */
771 void
772 gtk_mount_operation_set_parent (GtkMountOperation *op,
773                                 GtkWindow         *parent)
774 {
775   GtkMountOperationPrivate *priv;
776
777   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
778   g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
779
780   priv = op->priv;
781
782   if (priv->parent_window == parent)
783     return;
784
785   if (priv->parent_window)
786     {
787       g_signal_handlers_disconnect_by_func (priv->parent_window,
788                                             gtk_widget_destroyed,
789                                             &priv->parent_window);
790       priv->parent_window = NULL;
791     }
792
793   if (parent)
794     {
795       priv->parent_window = g_object_ref (parent);
796
797       g_signal_connect (parent, "destroy",
798                         G_CALLBACK (gtk_widget_destroyed),
799                         &priv->parent_window);
800
801       if (priv->dialog)
802         gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
803     }
804
805   g_object_notify (G_OBJECT (op), "parent");
806 }
807
808 /**
809  * gtk_mount_operation_get_parent:
810  * @op: a #GtkMountOperation
811  *
812  * Gets the transient parent used by the #GtkMountOperation
813  *
814  * Returns: the transient parent for windows shown by @op
815  *
816  * Since: 2.14
817  */
818 GtkWindow *
819 gtk_mount_operation_get_parent (GtkMountOperation *op)
820 {
821   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
822
823   return op->priv->parent_window;
824 }
825
826 /**
827  * gtk_mount_operation_set_screen:
828  * @op: a #GtkMountOperation
829  * @screen: a #GdkScreen
830  *
831  * Sets the screen to show windows of the #GtkMountOperation on.
832  *
833  * Since: 2.14
834  */
835 void
836 gtk_mount_operation_set_screen (GtkMountOperation *op,
837                                 GdkScreen         *screen)
838 {
839   GtkMountOperationPrivate *priv;
840
841   g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
842   g_return_if_fail (GDK_IS_SCREEN (screen));
843
844   priv = op->priv;
845
846   if (priv->screen == screen)
847     return;
848
849   if (priv->screen)
850     g_object_unref (priv->screen);
851
852   priv->screen = g_object_ref (screen);
853
854   if (priv->dialog)
855     gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
856
857   g_object_notify (G_OBJECT (op), "screen");
858 }
859
860 /**
861  * gtk_mount_operation_get_screen:
862  * @op: a #GtkMountOperation
863  *
864  * Gets the screen on which windows of the #GtkMountOperation 
865  * will be shown.
866  *
867  * Returns: the screen on which windows of @op are shown
868  *
869  * Since: 2.14
870  */
871 GdkScreen *
872 gtk_mount_operation_get_screen (GtkMountOperation *op)
873 {
874   GtkMountOperationPrivate *priv;
875
876   g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
877
878   priv = op->priv;
879
880   if (priv->dialog)
881     return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
882   else if (priv->parent_window)
883     return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
884   else if (priv->screen)
885     return priv->screen;
886   else
887     return gdk_screen_get_default ();
888 }
889
890 #define __GTK_MOUNT_OPERATION_C__
891 #include "gtkaliasdef.c"