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