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