Rename the generated private data getter function
[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 "glibintl.h"
30
31
32 /**
33  * SECTION:gmountoperation
34  * @short_description: Object used for authentication and user interaction
35  * @include: gio/gio.h
36  *
37  * #GMountOperation provides a mechanism for interacting with the user.
38  * It can be used for authenticating mountable operations, such as loop
39  * mounting files, hard drive partitions or server locations. It can
40  * also be used to ask the user questions or show a list of applications
41  * preventing unmount or eject operations from completing.
42  *
43  * Note that #GMountOperation is used for more than just #GMount
44  * objects – for example it is also used in g_drive_start() and
45  * g_drive_stop().
46  *
47  * Users should instantiate a subclass of this that implements all the
48  * various callbacks to show the required dialogs, such as
49  * #GtkMountOperation. If no user interaction is desired (for example
50  * when automounting filesystems at login time), usually %NULL can be
51  * passed, see each method taking a #GMountOperation for details.
52  */
53
54 enum {
55   ASK_PASSWORD,
56   ASK_QUESTION,
57   REPLY,
58   ABORTED,
59   SHOW_PROCESSES,
60   SHOW_UNMOUNT_PROGRESS,
61   LAST_SIGNAL
62 };
63
64 static guint signals[LAST_SIGNAL] = { 0 };
65
66 struct _GMountOperationPrivate {
67   char *password;
68   char *user;
69   char *domain;
70   gboolean anonymous;
71   GPasswordSave password_save;
72   int choice;
73 };
74
75 enum {
76   PROP_0,
77   PROP_USERNAME,
78   PROP_PASSWORD,
79   PROP_ANONYMOUS,
80   PROP_DOMAIN,
81   PROP_PASSWORD_SAVE,
82   PROP_CHOICE
83 };
84
85 G_DEFINE_TYPE_WITH_PRIVATE (GMountOperation, g_mount_operation, G_TYPE_OBJECT)
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 G_SOURCE_REMOVE;
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 show_unmount_progress (GMountOperation *op,
245                        const gchar     *message,
246                        gint64           time_left,
247                        gint64           bytes_left)
248 {
249   /* nothing to do */
250 }
251
252 static void
253 g_mount_operation_class_init (GMountOperationClass *klass)
254 {
255   GObjectClass *object_class;
256  
257   object_class = G_OBJECT_CLASS (klass);
258   object_class->finalize = g_mount_operation_finalize;
259   object_class->get_property = g_mount_operation_get_property;
260   object_class->set_property = g_mount_operation_set_property;
261   
262   klass->ask_password = ask_password;
263   klass->ask_question = ask_question;
264   klass->show_processes = show_processes;
265   klass->show_unmount_progress = show_unmount_progress;
266   
267   /**
268    * GMountOperation::ask-password:
269    * @op: a #GMountOperation requesting a password.
270    * @message: string containing a message to display to the user.
271    * @default_user: string containing the default user name.
272    * @default_domain: string containing the default domain.
273    * @flags: a set of #GAskPasswordFlags.
274    *
275    * Emitted when a mount operation asks the user for a password.
276    *
277    * If the message contains a line break, the first line should be
278    * presented as a heading. For example, it may be used as the
279    * primary text in a #GtkMessageDialog.
280    */
281   signals[ASK_PASSWORD] =
282     g_signal_new (I_("ask-password"),
283                   G_TYPE_FROM_CLASS (object_class),
284                   G_SIGNAL_RUN_LAST,
285                   G_STRUCT_OFFSET (GMountOperationClass, ask_password),
286                   NULL, NULL,
287                   NULL,
288                   G_TYPE_NONE, 4,
289                   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
290                   
291   /**
292    * GMountOperation::ask-question:
293    * @op: a #GMountOperation asking a question.
294    * @message: string containing a message to display to the user.
295    * @choices: an array of strings for each possible choice.
296    *
297    * Emitted when asking the user a question and gives a list of
298    * choices for the user to choose from.
299    *
300    * If the message contains a line break, the first line should be
301    * presented as a heading. For example, it may be used as the
302    * primary text in a #GtkMessageDialog.
303    */
304   signals[ASK_QUESTION] =
305     g_signal_new (I_("ask-question"),
306                   G_TYPE_FROM_CLASS (object_class),
307                   G_SIGNAL_RUN_LAST,
308                   G_STRUCT_OFFSET (GMountOperationClass, ask_question),
309                   NULL, NULL,
310                   NULL,
311                   G_TYPE_NONE, 2,
312                   G_TYPE_STRING, G_TYPE_STRV);
313                   
314   /**
315    * GMountOperation::reply:
316    * @op: a #GMountOperation.
317    * @result: a #GMountOperationResult indicating how the request was handled
318    *
319    * Emitted when the user has replied to the mount operation.
320    */
321   signals[REPLY] =
322     g_signal_new (I_("reply"),
323                   G_TYPE_FROM_CLASS (object_class),
324                   G_SIGNAL_RUN_LAST,
325                   G_STRUCT_OFFSET (GMountOperationClass, reply),
326                   NULL, NULL,
327                   g_cclosure_marshal_VOID__ENUM,
328                   G_TYPE_NONE, 1,
329                   G_TYPE_MOUNT_OPERATION_RESULT);
330
331   /**
332    * GMountOperation::aborted:
333    *
334    * Emitted by the backend when e.g. a device becomes unavailable
335    * while a mount operation is in progress.
336    *
337    * Implementations of GMountOperation should handle this signal
338    * by dismissing open password dialogs.
339    *
340    * Since: 2.20
341    */
342   signals[ABORTED] =
343     g_signal_new (I_("aborted"),
344                   G_TYPE_FROM_CLASS (object_class),
345                   G_SIGNAL_RUN_LAST,
346                   G_STRUCT_OFFSET (GMountOperationClass, aborted),
347                   NULL, NULL,
348                   g_cclosure_marshal_VOID__VOID,
349                   G_TYPE_NONE, 0);
350
351   /**
352    * GMountOperation::show-processes:
353    * @op: a #GMountOperation.
354    * @message: string containing a message to display to the user.
355    * @processes: (element-type GPid): an array of #GPid for processes
356    *   blocking the operation.
357    * @choices: an array of strings for each possible choice.
358    *
359    * Emitted when one or more processes are blocking an operation
360    * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
361    *
362    * Note that this signal may be emitted several times to update the
363    * list of blocking processes as processes close files. The
364    * application should only respond with g_mount_operation_reply() to
365    * the latest signal (setting #GMountOperation:choice to the choice
366    * the user made).
367    *
368    * If the message contains a line break, the first line should be
369    * presented as a heading. For example, it may be used as the
370    * primary text in a #GtkMessageDialog.
371    *
372    * Since: 2.22
373    */
374   signals[SHOW_PROCESSES] =
375     g_signal_new (I_("show-processes"),
376                   G_TYPE_FROM_CLASS (object_class),
377                   G_SIGNAL_RUN_LAST,
378                   G_STRUCT_OFFSET (GMountOperationClass, show_processes),
379                   NULL, NULL,
380                   NULL,
381                   G_TYPE_NONE, 3,
382                   G_TYPE_STRING, G_TYPE_ARRAY, G_TYPE_STRV);
383
384   /**
385    * GMountOperation::show-unmount-progress:
386    * @op: a #GMountOperation:
387    * @message: string containing a mesage to display to the user
388    * @time_left: the estimated time left before the operation completes,
389    *     in microseconds, or -1
390    * @bytes_left: the amount of bytes to be written before the operation
391    *     completes (or -1 if such amount is not known), or zero if the operation
392    *     is completed
393    *
394    * Emitted when an unmount operation has been busy for more than some time
395    * (typically 1.5 seconds).
396    *
397    * When unmounting or ejecting a volume, the kernel might need to flush
398    * pending data in its buffers to the volume stable storage, and this operation
399    * can take a considerable amount of time. This signal may be emitted several
400    * times as long as the unmount operation is outstanding, and then one
401    * last time when the operation is completed, with @bytes_left set to zero.
402    *
403    * Implementations of GMountOperation should handle this signal by
404    * showing an UI notification, and then dismiss it, or show another notification
405    * of completion, when @bytes_left reaches zero.
406    *
407    * If the message contains a line break, the first line should be
408    * presented as a heading. For example, it may be used as the
409    * primary text in a #GtkMessageDialog.
410    *
411    * Since: 2.34
412    */
413   signals[SHOW_UNMOUNT_PROGRESS] =
414     g_signal_new (I_("show-unmount-progress"),
415                   G_TYPE_FROM_CLASS (object_class),
416                   G_SIGNAL_RUN_LAST,
417                   G_STRUCT_OFFSET (GMountOperationClass, show_unmount_progress),
418                   NULL, NULL, NULL,
419                   G_TYPE_NONE, 3,
420                   G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
421
422   /**
423    * GMountOperation:username:
424    *
425    * The user name that is used for authentication when carrying out
426    * the mount operation.
427    */ 
428   g_object_class_install_property (object_class,
429                                    PROP_USERNAME,
430                                    g_param_spec_string ("username",
431                                                         P_("Username"),
432                                                         P_("The user name"),
433                                                         NULL,
434                                                         G_PARAM_READWRITE|
435                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
436
437   /**
438    * GMountOperation:password:
439    *
440    * The password that is used for authentication when carrying out
441    * the mount operation.
442    */ 
443   g_object_class_install_property (object_class,
444                                    PROP_PASSWORD,
445                                    g_param_spec_string ("password",
446                                                         P_("Password"),
447                                                         P_("The password"),
448                                                         NULL,
449                                                         G_PARAM_READWRITE|
450                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
451
452   /**
453    * GMountOperation:anonymous:
454    * 
455    * Whether to use an anonymous user when authenticating.
456    */
457   g_object_class_install_property (object_class,
458                                    PROP_ANONYMOUS,
459                                    g_param_spec_boolean ("anonymous",
460                                                          P_("Anonymous"),
461                                                          P_("Whether to use an anonymous user"),
462                                                          FALSE,
463                                                          G_PARAM_READWRITE|
464                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
465
466   /**
467    * GMountOperation:domain:
468    *
469    * The domain to use for the mount operation.
470    */ 
471   g_object_class_install_property (object_class,
472                                    PROP_DOMAIN,
473                                    g_param_spec_string ("domain",
474                                                         P_("Domain"),
475                                                         P_("The domain of the mount operation"),
476                                                         NULL,
477                                                         G_PARAM_READWRITE|
478                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
479
480   /**
481    * GMountOperation:password-save:
482    *
483    * Determines if and how the password information should be saved. 
484    */ 
485   g_object_class_install_property (object_class,
486                                    PROP_PASSWORD_SAVE,
487                                    g_param_spec_enum ("password-save",
488                                                       P_("Password save"),
489                                                       P_("How passwords should be saved"),
490                                                       G_TYPE_PASSWORD_SAVE,
491                                                       G_PASSWORD_SAVE_NEVER,
492                                                       G_PARAM_READWRITE|
493                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
494
495   /**
496    * GMountOperation:choice:
497    *
498    * The index of the user's choice when a question is asked during the 
499    * mount operation. See the #GMountOperation::ask-question signal.
500    */ 
501   g_object_class_install_property (object_class,
502                                    PROP_CHOICE,
503                                    g_param_spec_int ("choice",
504                                                      P_("Choice"),
505                                                      P_("The users choice"),
506                                                      0, G_MAXINT, 0,
507                                                      G_PARAM_READWRITE|
508                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
509 }
510
511 static void
512 g_mount_operation_init (GMountOperation *operation)
513 {
514   operation->priv = g_mount_operation_get_instance_private (operation);
515 }
516
517 /**
518  * g_mount_operation_new:
519  * 
520  * Creates a new mount operation.
521  * 
522  * Returns: a #GMountOperation.
523  **/
524 GMountOperation *
525 g_mount_operation_new (void)
526 {
527   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
528 }
529
530 /**
531  * g_mount_operation_get_username:
532  * @op: a #GMountOperation.
533  * 
534  * Get the user name from the mount operation.
535  *
536  * Returns: a string containing the user name.
537  **/
538 const char *
539 g_mount_operation_get_username (GMountOperation *op)
540 {
541   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
542   return op->priv->user;
543 }
544
545 /**
546  * g_mount_operation_set_username:
547  * @op: a #GMountOperation.
548  * @username: input username.
549  *
550  * Sets the user name within @op to @username.
551  **/
552 void
553 g_mount_operation_set_username (GMountOperation *op,
554                                 const char      *username)
555 {
556   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
557   g_free (op->priv->user);
558   op->priv->user = g_strdup (username);
559   g_object_notify (G_OBJECT (op), "username");
560 }
561
562 /**
563  * g_mount_operation_get_password:
564  * @op: a #GMountOperation.
565  *
566  * Gets a password from the mount operation. 
567  *
568  * Returns: a string containing the password within @op.
569  **/
570 const char *
571 g_mount_operation_get_password (GMountOperation *op)
572 {
573   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
574   return op->priv->password;
575 }
576
577 /**
578  * g_mount_operation_set_password:
579  * @op: a #GMountOperation.
580  * @password: password to set.
581  * 
582  * Sets the mount operation's password to @password.  
583  *
584  **/
585 void
586 g_mount_operation_set_password (GMountOperation *op,
587                                 const char      *password)
588 {
589   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
590   g_free (op->priv->password);
591   op->priv->password = g_strdup (password);
592   g_object_notify (G_OBJECT (op), "password");
593 }
594
595 /**
596  * g_mount_operation_get_anonymous:
597  * @op: a #GMountOperation.
598  * 
599  * Check to see whether the mount operation is being used 
600  * for an anonymous user.
601  * 
602  * Returns: %TRUE if mount operation is anonymous. 
603  **/
604 gboolean
605 g_mount_operation_get_anonymous (GMountOperation *op)
606 {
607   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
608   return op->priv->anonymous;
609 }
610
611 /**
612  * g_mount_operation_set_anonymous:
613  * @op: a #GMountOperation.
614  * @anonymous: boolean value.
615  * 
616  * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
617  **/  
618 void
619 g_mount_operation_set_anonymous (GMountOperation *op,
620                                  gboolean         anonymous)
621 {
622   GMountOperationPrivate *priv;
623   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
624   priv = op->priv;
625
626   if (priv->anonymous != anonymous)
627     {
628       priv->anonymous = anonymous;
629       g_object_notify (G_OBJECT (op), "anonymous");
630     }
631 }
632
633 /**
634  * g_mount_operation_get_domain:
635  * @op: a #GMountOperation.
636  * 
637  * Gets the domain of the mount operation.
638  * 
639  * Returns: a string set to the domain. 
640  **/
641 const char *
642 g_mount_operation_get_domain (GMountOperation *op)
643 {
644   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
645   return op->priv->domain;
646 }
647
648 /**
649  * g_mount_operation_set_domain:
650  * @op: a #GMountOperation.
651  * @domain: the domain to set.
652  * 
653  * Sets the mount operation's domain. 
654  **/  
655 void
656 g_mount_operation_set_domain (GMountOperation *op,
657                               const char      *domain)
658 {
659   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
660   g_free (op->priv->domain);
661   op->priv->domain = g_strdup (domain);
662   g_object_notify (G_OBJECT (op), "domain");
663 }
664
665 /**
666  * g_mount_operation_get_password_save:
667  * @op: a #GMountOperation.
668  * 
669  * Gets the state of saving passwords for the mount operation.
670  *
671  * Returns: a #GPasswordSave flag. 
672  **/  
673
674 GPasswordSave
675 g_mount_operation_get_password_save (GMountOperation *op)
676 {
677   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
678   return op->priv->password_save;
679 }
680
681 /**
682  * g_mount_operation_set_password_save:
683  * @op: a #GMountOperation.
684  * @save: a set of #GPasswordSave flags.
685  * 
686  * Sets the state of saving passwords for the mount operation.
687  * 
688  **/   
689 void
690 g_mount_operation_set_password_save (GMountOperation *op,
691                                      GPasswordSave    save)
692 {
693   GMountOperationPrivate *priv;
694   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
695   priv = op->priv;
696  
697   if (priv->password_save != save)
698     {
699       priv->password_save = save;
700       g_object_notify (G_OBJECT (op), "password-save");
701     }
702 }
703
704 /**
705  * g_mount_operation_get_choice:
706  * @op: a #GMountOperation.
707  * 
708  * Gets a choice from the mount operation.
709  *
710  * Returns: an integer containing an index of the user's choice from 
711  * the choice's list, or %0.
712  **/
713 int
714 g_mount_operation_get_choice (GMountOperation *op)
715 {
716   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
717   return op->priv->choice;
718 }
719
720 /**
721  * g_mount_operation_set_choice:
722  * @op: a #GMountOperation.
723  * @choice: an integer.
724  *
725  * Sets a default choice for the mount operation.
726  **/
727 void
728 g_mount_operation_set_choice (GMountOperation *op,
729                               int              choice)
730 {
731   GMountOperationPrivate *priv;
732   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
733   priv = op->priv;
734   if (priv->choice != choice)
735     {
736       priv->choice = choice;
737       g_object_notify (G_OBJECT (op), "choice");
738     }
739 }
740
741 /**
742  * g_mount_operation_reply:
743  * @op: a #GMountOperation
744  * @result: a #GMountOperationResult
745  * 
746  * Emits the #GMountOperation::reply signal.
747  **/
748 void
749 g_mount_operation_reply (GMountOperation *op,
750                          GMountOperationResult result)
751 {
752   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
753   g_signal_emit (op, signals[REPLY], 0, result);
754 }