Trivial: fix a typo
[platform/upstream/glib.git] / gio / gmountoperation.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
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
16  * Public 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  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "gmountoperation.h"
28 #include "gioenumtypes.h"
29 #include "gio-marshal.h"
30 #include "glibintl.h"
31
32
33 /**
34  * SECTION:gmountoperation
35  * @short_description: Object used for authentication and user interaction
36  * @include: gio/gio.h
37  *
38  * #GMountOperation provides a mechanism for interacting with the user.
39  * It can be used for authenticating mountable operations, such as loop
40  * mounting files, hard drive partitions or server locations. It can
41  * also be used to ask the user questions or show a list of applications
42  * preventing unmount or eject operations from completing.
43  *
44  * Note that #GMountOperation is used for more than just #GMount
45  * objects – for example it is also used in g_drive_start() and
46  * g_drive_stop().
47  *
48  * Users should instantiate a subclass of this that implements all the
49  * various callbacks to show the required dialogs, such as
50  * #GtkMountOperation. If no user interaction is desired (for example
51  * when automounting filesystems at login time), usually %NULL can be
52  * passed, see each method taking a #GMountOperation for details.
53  */
54
55 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
56
57 enum {
58   ASK_PASSWORD,
59   ASK_QUESTION,
60   REPLY,
61   ABORTED,
62   SHOW_PROCESSES,
63   LAST_SIGNAL
64 };
65
66 static guint signals[LAST_SIGNAL] = { 0 };
67
68 struct _GMountOperationPrivate {
69   char *password;
70   char *user;
71   char *domain;
72   gboolean anonymous;
73   GPasswordSave password_save;
74   int choice;
75 };
76
77 enum {
78   PROP_0,
79   PROP_USERNAME,
80   PROP_PASSWORD,
81   PROP_ANONYMOUS,
82   PROP_DOMAIN,
83   PROP_PASSWORD_SAVE,
84   PROP_CHOICE
85 };
86
87 static void 
88 g_mount_operation_set_property (GObject      *object,
89                                 guint         prop_id,
90                                 const GValue *value,
91                                 GParamSpec   *pspec)
92 {
93   GMountOperation *operation;
94
95   operation = G_MOUNT_OPERATION (object);
96
97   switch (prop_id)
98     {
99     case PROP_USERNAME:
100       g_mount_operation_set_username (operation, 
101                                       g_value_get_string (value));
102       break;
103    
104     case PROP_PASSWORD:
105       g_mount_operation_set_password (operation, 
106                                       g_value_get_string (value));
107       break;
108
109     case PROP_ANONYMOUS:
110       g_mount_operation_set_anonymous (operation, 
111                                        g_value_get_boolean (value));
112       break;
113
114     case PROP_DOMAIN:
115       g_mount_operation_set_domain (operation, 
116                                     g_value_get_string (value));
117       break;
118
119     case PROP_PASSWORD_SAVE:
120       g_mount_operation_set_password_save (operation, 
121                                            g_value_get_enum (value));
122       break;
123
124     case PROP_CHOICE:
125       g_mount_operation_set_choice (operation, 
126                                     g_value_get_int (value));
127       break;
128
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132     }
133 }
134
135
136 static void 
137 g_mount_operation_get_property (GObject    *object,
138                                 guint       prop_id,
139                                 GValue     *value,
140                                 GParamSpec *pspec)
141 {
142   GMountOperation *operation;
143   GMountOperationPrivate *priv;
144
145   operation = G_MOUNT_OPERATION (object);
146   priv = operation->priv;
147   
148   switch (prop_id)
149     {
150     case PROP_USERNAME:
151       g_value_set_string (value, priv->user);
152       break;
153
154     case PROP_PASSWORD:
155       g_value_set_string (value, priv->password);
156       break;
157
158     case PROP_ANONYMOUS:
159       g_value_set_boolean (value, priv->anonymous);
160       break;
161
162     case PROP_DOMAIN:
163       g_value_set_string (value, priv->domain);
164       break;
165
166     case PROP_PASSWORD_SAVE:
167       g_value_set_enum (value, priv->password_save);
168       break;
169
170     case PROP_CHOICE:
171       g_value_set_int (value, priv->choice);
172       break;
173
174     default:
175       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176       break;
177     }
178 }
179
180
181 static void
182 g_mount_operation_finalize (GObject *object)
183 {
184   GMountOperation *operation;
185   GMountOperationPrivate *priv;
186
187   operation = G_MOUNT_OPERATION (object);
188
189   priv = operation->priv;
190   
191   g_free (priv->password);
192   g_free (priv->user);
193   g_free (priv->domain);
194
195   G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize (object);
196 }
197
198 static gboolean
199 reply_non_handled_in_idle (gpointer data)
200 {
201   GMountOperation *op = data;
202
203   g_mount_operation_reply (op, G_MOUNT_OPERATION_UNHANDLED);
204   return FALSE;
205 }
206
207 static void
208 ask_password (GMountOperation *op,
209               const char      *message,
210               const char      *default_user,
211               const char      *default_domain,
212               GAskPasswordFlags flags)
213 {
214   g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
215                    reply_non_handled_in_idle,
216                    g_object_ref (op),
217                    g_object_unref);
218 }
219   
220 static void
221 ask_question (GMountOperation *op,
222               const char      *message,
223               const char      *choices[])
224 {
225   g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
226                    reply_non_handled_in_idle,
227                    g_object_ref (op),
228                    g_object_unref);
229 }
230
231 static void
232 show_processes (GMountOperation      *op,
233                 const gchar          *message,
234                 GArray               *processes,
235                 const gchar          *choices[])
236 {
237   g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
238                    reply_non_handled_in_idle,
239                    g_object_ref (op),
240                    g_object_unref);
241 }
242
243 static void
244 g_mount_operation_class_init (GMountOperationClass *klass)
245 {
246   GObjectClass *object_class;
247   
248   g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
249  
250   object_class = G_OBJECT_CLASS (klass);
251   object_class->finalize = g_mount_operation_finalize;
252   object_class->get_property = g_mount_operation_get_property;
253   object_class->set_property = g_mount_operation_set_property;
254   
255   klass->ask_password = ask_password;
256   klass->ask_question = ask_question;
257   klass->show_processes = show_processes;
258   
259   /**
260    * GMountOperation::ask-password:
261    * @op: a #GMountOperation requesting a password.
262    * @message: string containing a message to display to the user.
263    * @default_user: string containing the default user name.
264    * @default_domain: string containing the default domain.
265    * @flags: a set of #GAskPasswordFlags.
266    *
267    * Emitted when a mount operation asks the user for a password.
268    *
269    * If the message contains a line break, the first line should be
270    * presented as a heading. For example, it may be used as the
271    * primary text in a #GtkMessageDialog.
272    */
273   signals[ASK_PASSWORD] =
274     g_signal_new (I_("ask-password"),
275                   G_TYPE_FROM_CLASS (object_class),
276                   G_SIGNAL_RUN_LAST,
277                   G_STRUCT_OFFSET (GMountOperationClass, ask_password),
278                   NULL, NULL,
279                   _gio_marshal_VOID__STRING_STRING_STRING_FLAGS,
280                   G_TYPE_NONE, 4,
281                   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
282                   
283   /**
284    * GMountOperation::ask-question:
285    * @op: a #GMountOperation asking a question.
286    * @message: string containing a message to display to the user.
287    * @choices: an array of strings for each possible choice.
288    *
289    * Emitted when asking the user a question and gives a list of
290    * choices for the user to choose from.
291    *
292    * If the message contains a line break, the first line should be
293    * presented as a heading. For example, it may be used as the
294    * primary text in a #GtkMessageDialog.
295    */
296   signals[ASK_QUESTION] =
297     g_signal_new (I_("ask-question"),
298                   G_TYPE_FROM_CLASS (object_class),
299                   G_SIGNAL_RUN_LAST,
300                   G_STRUCT_OFFSET (GMountOperationClass, ask_question),
301                   NULL, NULL,
302                   _gio_marshal_VOID__STRING_BOXED,
303                   G_TYPE_NONE, 2,
304                   G_TYPE_STRING, G_TYPE_STRV);
305                   
306   /**
307    * GMountOperation::reply:
308    * @op: a #GMountOperation.
309    * @result: a #GMountOperationResult indicating how the request was handled
310    *
311    * Emitted when the user has replied to the mount operation.
312    */
313   signals[REPLY] =
314     g_signal_new (I_("reply"),
315                   G_TYPE_FROM_CLASS (object_class),
316                   G_SIGNAL_RUN_LAST,
317                   G_STRUCT_OFFSET (GMountOperationClass, reply),
318                   NULL, NULL,
319                   g_cclosure_marshal_VOID__ENUM,
320                   G_TYPE_NONE, 1,
321                   G_TYPE_MOUNT_OPERATION_RESULT);
322
323   /**
324    * GMountOperation::aborted:
325    *
326    * Emitted by the backend when e.g. a device becomes unavailable
327    * while a mount operation is in progress.
328    *
329    * Implementations of GMountOperation should handle this signal
330    * by dismissing open password dialogs.
331    *
332    * Since: 2.20
333    */
334   signals[ABORTED] =
335     g_signal_new (I_("aborted"),
336                   G_TYPE_FROM_CLASS (object_class),
337                   G_SIGNAL_RUN_LAST,
338                   G_STRUCT_OFFSET (GMountOperationClass, aborted),
339                   NULL, NULL,
340                   g_cclosure_marshal_VOID__VOID,
341                   G_TYPE_NONE, 0);
342
343   /**
344    * GMountOperation::show-processes:
345    * @op: a #GMountOperation.
346    * @message: string containing a message to display to the user.
347    * @processes: an array of #GPid for processes blocking the operation.
348    * @choices: an array of strings for each possible choice.
349    *
350    * Emitted when one or more processes are blocking an operation
351    * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
352    *
353    * Note that this signal may be emitted several times to update the
354    * list of blocking processes as processes close files. The
355    * application should only respond with g_mount_operation_reply() to
356    * the latest signal (setting #GMountOperation:choice to the choice
357    * the user made).
358    *
359    * If the message contains a line break, the first line should be
360    * presented as a heading. For example, it may be used as the
361    * primary text in a #GtkMessageDialog.
362    *
363    * Since: 2.22
364    */
365   signals[SHOW_PROCESSES] =
366     g_signal_new (I_("show-processes"),
367                   G_TYPE_FROM_CLASS (object_class),
368                   G_SIGNAL_RUN_LAST,
369                   G_STRUCT_OFFSET (GMountOperationClass, show_processes),
370                   NULL, NULL,
371                   _gio_marshal_VOID__STRING_BOXED_BOXED,
372                   G_TYPE_NONE, 3,
373                   G_TYPE_STRING, G_TYPE_ARRAY, G_TYPE_STRV);
374
375   /**
376    * GMountOperation:username:
377    *
378    * The user name that is used for authentication when carrying out
379    * the mount operation.
380    */ 
381   g_object_class_install_property (object_class,
382                                    PROP_USERNAME,
383                                    g_param_spec_string ("username",
384                                                         P_("Username"),
385                                                         P_("The user name"),
386                                                         NULL,
387                                                         G_PARAM_READWRITE|
388                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
389
390   /**
391    * GMountOperation:password:
392    *
393    * The password that is used for authentication when carrying out
394    * the mount operation.
395    */ 
396   g_object_class_install_property (object_class,
397                                    PROP_PASSWORD,
398                                    g_param_spec_string ("password",
399                                                         P_("Password"),
400                                                         P_("The password"),
401                                                         NULL,
402                                                         G_PARAM_READWRITE|
403                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
404
405   /**
406    * GMountOperation:anonymous:
407    * 
408    * Whether to use an anonymous user when authenticating.
409    */
410   g_object_class_install_property (object_class,
411                                    PROP_ANONYMOUS,
412                                    g_param_spec_boolean ("anonymous",
413                                                          P_("Anonymous"),
414                                                          P_("Whether to use an anonymous user"),
415                                                          FALSE,
416                                                          G_PARAM_READWRITE|
417                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
418
419   /**
420    * GMountOperation:domain:
421    *
422    * The domain to use for the mount operation.
423    */ 
424   g_object_class_install_property (object_class,
425                                    PROP_DOMAIN,
426                                    g_param_spec_string ("domain",
427                                                         P_("Domain"),
428                                                         P_("The domain of the mount operation"),
429                                                         NULL,
430                                                         G_PARAM_READWRITE|
431                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
432
433   /**
434    * GMountOperation:password-save:
435    *
436    * Determines if and how the password information should be saved. 
437    */ 
438   g_object_class_install_property (object_class,
439                                    PROP_PASSWORD_SAVE,
440                                    g_param_spec_enum ("password-save",
441                                                       P_("Password save"),
442                                                       P_("How passwords should be saved"),
443                                                       G_TYPE_PASSWORD_SAVE,
444                                                       G_PASSWORD_SAVE_NEVER,
445                                                       G_PARAM_READWRITE|
446                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
447
448   /**
449    * GMountOperation:choice:
450    *
451    * The index of the user's choice when a question is asked during the 
452    * mount operation. See the #GMountOperation::ask-question signal.
453    */ 
454   g_object_class_install_property (object_class,
455                                    PROP_CHOICE,
456                                    g_param_spec_int ("choice",
457                                                      P_("Choice"),
458                                                      P_("The users choice"),
459                                                      0, G_MAXINT, 0,
460                                                      G_PARAM_READWRITE|
461                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
462 }
463
464 static void
465 g_mount_operation_init (GMountOperation *operation)
466 {
467   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
468                                                  G_TYPE_MOUNT_OPERATION,
469                                                  GMountOperationPrivate);
470 }
471
472 /**
473  * g_mount_operation_new:
474  * 
475  * Creates a new mount operation.
476  * 
477  * Returns: a #GMountOperation.
478  **/
479 GMountOperation *
480 g_mount_operation_new (void)
481 {
482   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
483 }
484
485 /**
486  * g_mount_operation_get_username
487  * @op: a #GMountOperation.
488  * 
489  * Get the user name from the mount operation.
490  *
491  * Returns: a string containing the user name.
492  **/
493 const char *
494 g_mount_operation_get_username (GMountOperation *op)
495 {
496   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
497   return op->priv->user;
498 }
499
500 /**
501  * g_mount_operation_set_username:
502  * @op: a #GMountOperation.
503  * @username: input username.
504  *
505  * Sets the user name within @op to @username.
506  **/
507 void
508 g_mount_operation_set_username (GMountOperation *op,
509                                 const char      *username)
510 {
511   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
512   g_free (op->priv->user);
513   op->priv->user = g_strdup (username);
514   g_object_notify (G_OBJECT (op), "username");
515 }
516
517 /**
518  * g_mount_operation_get_password:
519  * @op: a #GMountOperation.
520  *
521  * Gets a password from the mount operation. 
522  *
523  * Returns: a string containing the password within @op.
524  **/
525 const char *
526 g_mount_operation_get_password (GMountOperation *op)
527 {
528   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
529   return op->priv->password;
530 }
531
532 /**
533  * g_mount_operation_set_password:
534  * @op: a #GMountOperation.
535  * @password: password to set.
536  * 
537  * Sets the mount operation's password to @password.  
538  *
539  **/
540 void
541 g_mount_operation_set_password (GMountOperation *op,
542                                 const char      *password)
543 {
544   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
545   g_free (op->priv->password);
546   op->priv->password = g_strdup (password);
547   g_object_notify (G_OBJECT (op), "password");
548 }
549
550 /**
551  * g_mount_operation_get_anonymous:
552  * @op: a #GMountOperation.
553  * 
554  * Check to see whether the mount operation is being used 
555  * for an anonymous user.
556  * 
557  * Returns: %TRUE if mount operation is anonymous. 
558  **/
559 gboolean
560 g_mount_operation_get_anonymous (GMountOperation *op)
561 {
562   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
563   return op->priv->anonymous;
564 }
565
566 /**
567  * g_mount_operation_set_anonymous:
568  * @op: a #GMountOperation.
569  * @anonymous: boolean value.
570  * 
571  * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
572  **/  
573 void
574 g_mount_operation_set_anonymous (GMountOperation *op,
575                                  gboolean         anonymous)
576 {
577   GMountOperationPrivate *priv;
578   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
579   priv = op->priv;
580
581   if (priv->anonymous != anonymous)
582     {
583       priv->anonymous = anonymous;
584       g_object_notify (G_OBJECT (op), "anonymous");
585     }
586 }
587
588 /**
589  * g_mount_operation_get_domain:
590  * @op: a #GMountOperation.
591  * 
592  * Gets the domain of the mount operation.
593  * 
594  * Returns: a string set to the domain. 
595  **/
596 const char *
597 g_mount_operation_get_domain (GMountOperation *op)
598 {
599   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
600   return op->priv->domain;
601 }
602
603 /**
604  * g_mount_operation_set_domain:
605  * @op: a #GMountOperation.
606  * @domain: the domain to set.
607  * 
608  * Sets the mount operation's domain. 
609  **/  
610 void
611 g_mount_operation_set_domain (GMountOperation *op,
612                               const char      *domain)
613 {
614   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
615   g_free (op->priv->domain);
616   op->priv->domain = g_strdup (domain);
617   g_object_notify (G_OBJECT (op), "domain");
618 }
619
620 /**
621  * g_mount_operation_get_password_save:
622  * @op: a #GMountOperation.
623  * 
624  * Gets the state of saving passwords for the mount operation.
625  *
626  * Returns: a #GPasswordSave flag. 
627  **/  
628
629 GPasswordSave
630 g_mount_operation_get_password_save (GMountOperation *op)
631 {
632   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
633   return op->priv->password_save;
634 }
635
636 /**
637  * g_mount_operation_set_password_save:
638  * @op: a #GMountOperation.
639  * @save: a set of #GPasswordSave flags.
640  * 
641  * Sets the state of saving passwords for the mount operation.
642  * 
643  **/   
644 void
645 g_mount_operation_set_password_save (GMountOperation *op,
646                                      GPasswordSave    save)
647 {
648   GMountOperationPrivate *priv;
649   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
650   priv = op->priv;
651  
652   if (priv->password_save != save)
653     {
654       priv->password_save = save;
655       g_object_notify (G_OBJECT (op), "password-save");
656     }
657 }
658
659 /**
660  * g_mount_operation_get_choice:
661  * @op: a #GMountOperation.
662  * 
663  * Gets a choice from the mount operation.
664  *
665  * Returns: an integer containing an index of the user's choice from 
666  * the choice's list, or %0.
667  **/
668 int
669 g_mount_operation_get_choice (GMountOperation *op)
670 {
671   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
672   return op->priv->choice;
673 }
674
675 /**
676  * g_mount_operation_set_choice:
677  * @op: a #GMountOperation.
678  * @choice: an integer.
679  *
680  * Sets a default choice for the mount operation.
681  **/
682 void
683 g_mount_operation_set_choice (GMountOperation *op,
684                               int              choice)
685 {
686   GMountOperationPrivate *priv;
687   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
688   priv = op->priv;
689   if (priv->choice != choice)
690     {
691       priv->choice = choice;
692       g_object_notify (G_OBJECT (op), "choice");
693     }
694 }
695
696 /**
697  * g_mount_operation_reply:
698  * @op: a #GMountOperation
699  * @result: a #GMountOperationResult
700  * 
701  * Emits the #GMountOperation::reply signal.
702  **/
703 void
704 g_mount_operation_reply (GMountOperation *op,
705                          GMountOperationResult result)
706 {
707   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
708   g_signal_emit (op, signals[REPLY], 0, result);
709 }