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