Fix up includes in section docs
[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 #include "gioalias.h"
33
34 /** 
35  * SECTION:gmountoperation
36  * @short_description: Authentication methods for mountable locations
37  * @include: gio.h
38  *
39  * #GMountOperation provides a mechanism for authenticating mountable 
40  * operations, such as loop mounting files, hard drive partitions or 
41  * server locations. 
42  *
43  * Mounting operations are handed a #GMountOperation that then can use 
44  * if they require any privileges or authentication for their volumes 
45  * to be mounted (e.g. a hard disk partition or an encrypted filesystem), 
46  * or if they are implementing a remote server protocol which requires 
47  * user credentials such as FTP or WebDAV.
48  *
49  * Users should instantiate a subclass of this that implements all
50  * the various callbacks to show the required dialogs.
51  **/
52
53 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
54
55 enum {
56   ASK_PASSWORD,
57   ASK_QUESTION,
58   REPLY,
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 static void 
84 g_mount_operation_set_property (GObject      *object,
85                                 guint         prop_id,
86                                 const GValue *value,
87                                 GParamSpec   *pspec)
88 {
89   GMountOperation *operation;
90
91   operation = G_MOUNT_OPERATION (object);
92
93   switch (prop_id)
94     {
95     case PROP_USERNAME:
96       g_mount_operation_set_username (operation, 
97                                       g_value_get_string (value));
98       break;
99    
100     case PROP_PASSWORD:
101       g_mount_operation_set_password (operation, 
102                                       g_value_get_string (value));
103       break;
104
105     case PROP_ANONYMOUS:
106       g_mount_operation_set_anonymous (operation, 
107                                        g_value_get_boolean (value));
108       break;
109
110     case PROP_DOMAIN:
111       g_mount_operation_set_domain (operation, 
112                                     g_value_get_string (value));
113       break;
114
115     case PROP_PASSWORD_SAVE:
116       g_mount_operation_set_password_save (operation, 
117                                            g_value_get_enum (value));
118       break;
119
120     case PROP_CHOICE:
121       g_mount_operation_set_choice (operation, 
122                                     g_value_get_int (value));
123       break;
124
125     default:
126       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127       break;
128     }
129 }
130
131
132 static void 
133 g_mount_operation_get_property (GObject    *object,
134                                 guint       prop_id,
135                                 GValue     *value,
136                                 GParamSpec *pspec)
137 {
138   GMountOperation *operation;
139   GMountOperationPrivate *priv;
140
141   operation = G_MOUNT_OPERATION (object);
142   priv = operation->priv;
143   
144   switch (prop_id)
145     {
146     case PROP_USERNAME:
147       g_value_set_string (value, priv->user);
148       break;
149
150     case PROP_PASSWORD:
151       g_value_set_string (value, priv->password);
152       break;
153
154     case PROP_ANONYMOUS:
155       g_value_set_boolean (value, priv->anonymous);
156       break;
157
158     case PROP_DOMAIN:
159       g_value_set_string (value, priv->domain);
160       break;
161
162     case PROP_PASSWORD_SAVE:
163       g_value_set_enum (value, priv->password_save);
164       break;
165
166     case PROP_CHOICE:
167       g_value_set_int (value, priv->choice);
168       break;
169
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173     }
174 }
175
176
177 static void
178 g_mount_operation_finalize (GObject *object)
179 {
180   GMountOperation *operation;
181   GMountOperationPrivate *priv;
182
183   operation = G_MOUNT_OPERATION (object);
184
185   priv = operation->priv;
186   
187   g_free (priv->password);
188   g_free (priv->user);
189   g_free (priv->domain);
190   
191   if (G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize)
192     (*G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize) (object);
193 }
194
195 static gboolean
196 boolean_handled_accumulator (GSignalInvocationHint *ihint,
197                              GValue                *return_accu,
198                              const GValue          *handler_return,
199                              gpointer               dummy)
200 {
201   gboolean continue_emission;
202   gboolean signal_handled;
203   
204   signal_handled = g_value_get_boolean (handler_return);
205   g_value_set_boolean (return_accu, signal_handled);
206   continue_emission = !signal_handled;
207   
208   return continue_emission;
209 }
210
211 static gboolean
212 ask_password (GMountOperation *op,
213               const char      *message,
214               const char      *default_user,
215               const char      *default_domain,
216               GAskPasswordFlags flags)
217 {
218   return FALSE;
219 }
220   
221 static gboolean
222 ask_question (GMountOperation *op,
223               const char      *message,
224               const char      *choices[])
225 {
226   return FALSE;
227 }
228
229 static void
230 g_mount_operation_class_init (GMountOperationClass *klass)
231 {
232   GObjectClass *object_class;
233   
234   g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
235  
236   object_class = G_OBJECT_CLASS (klass);
237   object_class->finalize = g_mount_operation_finalize;
238   object_class->get_property = g_mount_operation_get_property;
239   object_class->set_property = g_mount_operation_set_property;
240   
241   klass->ask_password = ask_password;
242   klass->ask_question = ask_question;
243   
244   /**
245    * GMountOperation::ask-password:
246    * @op: a #GMountOperation requesting a password.
247    * @message: string containing a message to display to the user.
248    * @default_user: string containing the default user name.
249    * @default_domain: string containing the default domain.
250    * @flags: a set of #GAskPasswordFlags.
251    * 
252    * Emitted when a mount operation asks the user for a password.
253    */
254   signals[ASK_PASSWORD] =
255     g_signal_new (I_("ask_password"),
256                   G_TYPE_FROM_CLASS (object_class),
257                   G_SIGNAL_RUN_LAST,
258                   G_STRUCT_OFFSET (GMountOperationClass, ask_password),
259                   boolean_handled_accumulator, NULL,
260                   _gio_marshal_BOOLEAN__STRING_STRING_STRING_INT,
261                   G_TYPE_BOOLEAN, 4,
262                   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
263                   
264   /**
265    * GMountOperation::ask-question:
266    * @op: a #GMountOperation asking a question.
267    * @message: string containing a message to display to the user.
268    * @choices: an array of strings for each possible choice.
269    * 
270    * Emitted when asking the user a question and gives a list of 
271    * choices for the user to choose from. 
272    */
273   signals[ASK_QUESTION] =
274     g_signal_new (I_("ask_question"),
275                   G_TYPE_FROM_CLASS (object_class),
276                   G_SIGNAL_RUN_LAST,
277                   G_STRUCT_OFFSET (GMountOperationClass, ask_question),
278                   boolean_handled_accumulator, NULL,
279                   _gio_marshal_BOOLEAN__STRING_POINTER,
280                   G_TYPE_BOOLEAN, 2,
281                   G_TYPE_STRING, G_TYPE_POINTER);
282                   
283   /**
284    * GMountOperation::reply:
285    * @op: a #GMountOperation.
286    * @abort: a boolean indicating %TRUE if the operation was aborted.
287    * 
288    * Emitted when the user has replied to the mount operation.
289    */
290   signals[REPLY] =
291     g_signal_new (I_("reply"),
292                   G_TYPE_FROM_CLASS (object_class),
293                   G_SIGNAL_RUN_LAST,
294                   G_STRUCT_OFFSET (GMountOperationClass, reply),
295                   NULL, NULL,
296                   g_cclosure_marshal_VOID__BOOLEAN,
297                   G_TYPE_NONE, 1,
298                   G_TYPE_BOOLEAN);
299
300   /**
301    * GMountOperation:username:
302    *
303    * The user name that is used for authentication when carrying out
304    * the mount operation.
305    */ 
306   g_object_class_install_property (object_class,
307                                    PROP_USERNAME,
308                                    g_param_spec_string ("username",
309                                                         P_("Username"),
310                                                         P_("The user name"),
311                                                         NULL,
312                                                         G_PARAM_READWRITE|
313                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
314
315   /**
316    * GMountOperation:password:
317    *
318    * The password that is used for authentication when carrying out
319    * the mount operation.
320    */ 
321   g_object_class_install_property (object_class,
322                                    PROP_PASSWORD,
323                                    g_param_spec_string ("password",
324                                                         P_("Password"),
325                                                         P_("The password"),
326                                                         NULL,
327                                                         G_PARAM_READWRITE|
328                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
329
330   /**
331    * GMountOperation:anonymous:
332    * 
333    * Whether to use an anonymous user when authenticating.
334    */
335   g_object_class_install_property (object_class,
336                                    PROP_ANONYMOUS,
337                                    g_param_spec_boolean ("anonymous",
338                                                          P_("Anonymous"),
339                                                          P_("Whether to use an anonymous user"),
340                                                          FALSE,
341                                                          G_PARAM_READWRITE|
342                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
343
344   /**
345    * GMountOperation:domain:
346    *
347    * The domain to use for the mount operation.
348    */ 
349   g_object_class_install_property (object_class,
350                                    PROP_DOMAIN,
351                                    g_param_spec_string ("domain",
352                                                         P_("Domain"),
353                                                         P_("The domain of the mount operation"),
354                                                         NULL,
355                                                         G_PARAM_READWRITE|
356                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
357
358   /**
359    * GMountOperation:password-save:
360    *
361    * Determines if and how the password information should be saved. 
362    */ 
363   g_object_class_install_property (object_class,
364                                    PROP_PASSWORD_SAVE,
365                                    g_param_spec_enum ("password-save",
366                                                       P_("Password save"),
367                                                       P_("How passwords should be saved"),
368                                                       G_TYPE_PASSWORD_SAVE,
369                                                       G_PASSWORD_SAVE_NEVER,
370                                                       G_PARAM_READWRITE|
371                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
372
373   /**
374    * GMountOperation:choice:
375    *
376    * The index of the user's choice when a question is asked during the 
377    * mount operation. See the #GMountOperation::ask-question signal.
378    */ 
379   g_object_class_install_property (object_class,
380                                    PROP_CHOICE,
381                                    g_param_spec_int ("choice",
382                                                      P_("Choice"),
383                                                      P_("The users choice"),
384                                                      0, G_MAXINT, 0,
385                                                      G_PARAM_READWRITE|
386                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
387 }
388
389 static void
390 g_mount_operation_init (GMountOperation *operation)
391 {
392   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
393                                                  G_TYPE_MOUNT_OPERATION,
394                                                  GMountOperationPrivate);
395 }
396
397 /**
398  * g_mount_operation_new:
399  * 
400  * Creates a new mount operation.
401  * 
402  * Returns: a #GMountOperation.
403  **/
404 GMountOperation *
405 g_mount_operation_new (void)
406 {
407   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
408 }
409
410 /**
411  * g_mount_operation_get_username
412  * @op: a #GMountOperation.
413  * 
414  * Get the user name from the mount operation.
415  *
416  * Returns: a string containing the user name.
417  **/
418 const char *
419 g_mount_operation_get_username (GMountOperation *op)
420 {
421   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
422   return op->priv->user;
423 }
424
425 /**
426  * g_mount_operation_set_username:
427  * @op: a #GMountOperation.
428  * @username: input username.
429  *
430  * Sets the user name within @op to @username.
431  * 
432  **/
433 void
434 g_mount_operation_set_username (GMountOperation *op,
435                                 const char      *username)
436 {
437   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
438   g_free (op->priv->user);
439   op->priv->user = g_strdup (username);
440   g_object_notify (G_OBJECT (op), "username");
441 }
442
443 /**
444  * g_mount_operation_get_password:
445  * @op: a #GMountOperation.
446  *
447  * Gets a password from the mount operation. 
448  *
449  * Returns: a string containing the password within @op.
450  **/
451 const char *
452 g_mount_operation_get_password (GMountOperation *op)
453 {
454   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
455   return op->priv->password;
456 }
457
458 /**
459  * g_mount_operation_set_password:
460  * @op: a #GMountOperation.
461  * @password: password to set.
462  * 
463  * Sets the mount operation's password to @password.  
464  *
465  **/
466 void
467 g_mount_operation_set_password (GMountOperation *op,
468                                 const char      *password)
469 {
470   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
471   g_free (op->priv->password);
472   op->priv->password = g_strdup (password);
473   g_object_notify (G_OBJECT (op), "password");
474 }
475
476 /**
477  * g_mount_operation_get_anonymous:
478  * @op: a #GMountOperation.
479  * 
480  * Check to see whether the mount operation is being used 
481  * for an anonymous user.
482  * 
483  * Returns: %TRUE if mount operation is anonymous. 
484  **/
485 gboolean
486 g_mount_operation_get_anonymous (GMountOperation *op)
487 {
488   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
489   return op->priv->anonymous;
490 }
491
492 /**
493  * g_mount_operation_set_anonymous:
494  * @op: a #GMountOperation.
495  * @anonymous: boolean value.
496  * 
497  * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
498  **/  
499 void
500 g_mount_operation_set_anonymous (GMountOperation *op,
501                                  gboolean         anonymous)
502 {
503   GMountOperationPrivate *priv;
504   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
505   priv = op->priv;
506
507   if (priv->anonymous != anonymous)
508     {
509       priv->anonymous = anonymous;
510       g_object_notify (G_OBJECT (op), "anonymous");
511     }
512 }
513
514 /**
515  * g_mount_operation_get_domain:
516  * @op: a #GMountOperation.
517  * 
518  * Gets the domain of the mount operation.
519  * 
520  * Returns: a string set to the domain. 
521  **/
522 const char *
523 g_mount_operation_get_domain (GMountOperation *op)
524 {
525   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
526   return op->priv->domain;
527 }
528
529 /**
530  * g_mount_operation_set_domain:
531  * @op: a #GMountOperation.
532  * @domain: the domain to set.
533  * 
534  * Sets the mount operation's domain. 
535  **/  
536 void
537 g_mount_operation_set_domain (GMountOperation *op,
538                               const char      *domain)
539 {
540   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
541   g_free (op->priv->domain);
542   op->priv->domain = g_strdup (domain);
543   g_object_notify (G_OBJECT (op), "domain");
544 }
545
546 /**
547  * g_mount_operation_get_password_save:
548  * @op: a #GMountOperation.
549  * 
550  * Gets the state of saving passwords for the mount operation.
551  *
552  * Returns: a #GPasswordSave flag. 
553  **/  
554
555 GPasswordSave
556 g_mount_operation_get_password_save (GMountOperation *op)
557 {
558   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
559   return op->priv->password_save;
560 }
561
562 /**
563  * g_mount_operation_set_password_save:
564  * @op: a #GMountOperation.
565  * @save: a set of #GPasswordSave flags.
566  * 
567  * Sets the state of saving passwords for the mount operation.
568  * 
569  **/   
570 void
571 g_mount_operation_set_password_save (GMountOperation *op,
572                                      GPasswordSave    save)
573 {
574   GMountOperationPrivate *priv;
575   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
576   priv = op->priv;
577  
578   if (priv->password_save != save)
579     {
580       priv->password_save = save;
581       g_object_notify (G_OBJECT (op), "password-save");
582     }
583 }
584
585 /**
586  * g_mount_operation_get_choice:
587  * @op: a #GMountOperation.
588  * 
589  * Gets a choice from the mount operation.
590  *
591  * Returns: an integer containing an index of the user's choice from 
592  * the choice's list, or %0.
593  **/
594 int
595 g_mount_operation_get_choice (GMountOperation *op)
596 {
597   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
598   return op->priv->choice;
599 }
600
601 /**
602  * g_mount_operation_set_choice:
603  * @op: a #GMountOperation.
604  * @choice: an integer.
605  *
606  * Sets a default choice for the mount operation.
607  **/
608 void
609 g_mount_operation_set_choice (GMountOperation *op,
610                               int              choice)
611 {
612   GMountOperationPrivate *priv;
613   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
614   priv = op->priv;
615   if (priv->choice != choice)
616     {
617       priv->choice = choice;
618       g_object_notify (G_OBJECT (op), "choice");
619     }
620 }
621
622 /**
623  * g_mount_operation_reply:
624  * @op: a #GMountOperation.
625  * @abort: boolean.
626  * 
627  * Emits the #GMountOperation::reply signal.
628  **/
629 void
630 g_mount_operation_reply (GMountOperation *op,
631                          gboolean         abort)
632 {
633   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
634   g_signal_emit (op, signals[REPLY], 0, abort);
635 }
636
637 #define __G_MOUNT_OPERATION_C__
638 #include "gioaliasdef.c"