Add new error codes for when compilation fails and make compilation error
[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|
309                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
310
311   /**
312    * GMountOperation:password:
313    *
314    * The password that is used for authentication when carrying out
315    * the mount operation.
316    */ 
317   g_object_class_install_property (object_class,
318                                    PROP_PASSWORD,
319                                    g_param_spec_string ("password",
320                                                         P_("Password"),
321                                                         P_("The password"),
322                                                         NULL,
323                                                         G_PARAM_READWRITE|
324                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
325
326   /**
327    * GMountOperation:anonymous:
328    * 
329    * Whether to use an anonymous user when authenticating.
330    */
331   g_object_class_install_property (object_class,
332                                    PROP_ANONYMOUS,
333                                    g_param_spec_boolean ("anonymous",
334                                                          P_("Anonymous"),
335                                                          P_("Whether to use an anonymous user"),
336                                                          FALSE,
337                                                          G_PARAM_READWRITE|
338                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
339
340   /**
341    * GMountOperation:domain:
342    *
343    * The domain to use for the mount operation.
344    */ 
345   g_object_class_install_property (object_class,
346                                    PROP_DOMAIN,
347                                    g_param_spec_string ("domain",
348                                                         P_("Domain"),
349                                                         P_("The domain of the mount operation"),
350                                                         NULL,
351                                                         G_PARAM_READWRITE|
352                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
353
354   /**
355    * GMountOperation:password-save:
356    *
357    * Determines if and how the password information should be saved. 
358    */ 
359   g_object_class_install_property (object_class,
360                                    PROP_PASSWORD_SAVE,
361                                    g_param_spec_enum ("password-save",
362                                                       P_("Password save"),
363                                                       P_("How passwords should be saved"),
364                                                       G_TYPE_PASSWORD_SAVE,
365                                                       G_PASSWORD_SAVE_NEVER,
366                                                       G_PARAM_READWRITE|
367                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
368
369   /**
370    * GMountOperation:choice:
371    *
372    * The index of the user's choice when a question is asked during the 
373    * mount operation. See the #GMountOperation::ask-question signal.
374    */ 
375   g_object_class_install_property (object_class,
376                                    PROP_CHOICE,
377                                    g_param_spec_int ("choice",
378                                                      P_("Choice"),
379                                                      P_("The users choice"),
380                                                      0, G_MAXINT, 0,
381                                                      G_PARAM_READWRITE|
382                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
383 }
384
385 static void
386 g_mount_operation_init (GMountOperation *operation)
387 {
388   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
389                                                  G_TYPE_MOUNT_OPERATION,
390                                                  GMountOperationPrivate);
391 }
392
393 /**
394  * g_mount_operation_new:
395  * 
396  * Creates a new mount operation.
397  * 
398  * Returns: a #GMountOperation.
399  **/
400 GMountOperation *
401 g_mount_operation_new (void)
402 {
403   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
404 }
405
406 /**
407  * g_mount_operation_get_username
408  * @op: a #GMountOperation.
409  * 
410  * Get the user name from the mount operation.
411  *
412  * Returns: a string containing the user name.
413  **/
414 const char *
415 g_mount_operation_get_username (GMountOperation *op)
416 {
417   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
418   return op->priv->user;
419 }
420
421 /**
422  * g_mount_operation_set_username:
423  * @op: a #GMountOperation.
424  * @username: input username.
425  *
426  * Sets the user name within @op to @username.
427  * 
428  **/
429 void
430 g_mount_operation_set_username (GMountOperation *op,
431                                 const char      *username)
432 {
433   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
434   g_free (op->priv->user);
435   op->priv->user = g_strdup (username);
436   g_object_notify (G_OBJECT (op), "username");
437 }
438
439 /**
440  * g_mount_operation_get_password:
441  * @op: a #GMountOperation.
442  *
443  * Gets a password from the mount operation. 
444  *
445  * Returns: a string containing the password within @op.
446  **/
447 const char *
448 g_mount_operation_get_password (GMountOperation *op)
449 {
450   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
451   return op->priv->password;
452 }
453
454 /**
455  * g_mount_operation_set_password:
456  * @op: a #GMountOperation.
457  * @password: password to set.
458  * 
459  * Sets the mount operation's password to @password.  
460  *
461  **/
462 void
463 g_mount_operation_set_password (GMountOperation *op,
464                                 const char      *password)
465 {
466   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
467   g_free (op->priv->password);
468   op->priv->password = g_strdup (password);
469   g_object_notify (G_OBJECT (op), "password");
470 }
471
472 /**
473  * g_mount_operation_get_anonymous:
474  * @op: a #GMountOperation.
475  * 
476  * Check to see whether the mount operation is being used 
477  * for an anonymous user.
478  * 
479  * Returns: %TRUE if mount operation is anonymous. 
480  **/
481 gboolean
482 g_mount_operation_get_anonymous (GMountOperation *op)
483 {
484   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
485   return op->priv->anonymous;
486 }
487
488 /**
489  * g_mount_operation_set_anonymous:
490  * @op: a #GMountOperation.
491  * @anonymous: boolean value.
492  * 
493  * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
494  **/  
495 void
496 g_mount_operation_set_anonymous (GMountOperation *op,
497                                  gboolean         anonymous)
498 {
499   GMountOperationPrivate *priv;
500   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
501   priv = op->priv;
502
503   if (priv->anonymous != anonymous)
504     {
505       priv->anonymous = anonymous;
506       g_object_notify (G_OBJECT (op), "anonymous");
507     }
508 }
509
510 /**
511  * g_mount_operation_get_domain:
512  * @op: a #GMountOperation.
513  * 
514  * Gets the domain of the mount operation.
515  * 
516  * Returns: a string set to the domain. 
517  **/
518 const char *
519 g_mount_operation_get_domain (GMountOperation *op)
520 {
521   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
522   return op->priv->domain;
523 }
524
525 /**
526  * g_mount_operation_set_domain:
527  * @op: a #GMountOperation.
528  * @domain: the domain to set.
529  * 
530  * Sets the mount operation's domain. 
531  **/  
532 void
533 g_mount_operation_set_domain (GMountOperation *op,
534                               const char      *domain)
535 {
536   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
537   g_free (op->priv->domain);
538   op->priv->domain = g_strdup (domain);
539   g_object_notify (G_OBJECT (op), "domain");
540 }
541
542 /**
543  * g_mount_operation_get_password_save:
544  * @op: a #GMountOperation.
545  * 
546  * Gets the state of saving passwords for the mount operation.
547  *
548  * Returns: a #GPasswordSave flag. 
549  **/  
550
551 GPasswordSave
552 g_mount_operation_get_password_save (GMountOperation *op)
553 {
554   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
555   return op->priv->password_save;
556 }
557
558 /**
559  * g_mount_operation_set_password_save:
560  * @op: a #GMountOperation.
561  * @save: a set of #GPasswordSave flags.
562  * 
563  * Sets the state of saving passwords for the mount operation.
564  * 
565  **/   
566 void
567 g_mount_operation_set_password_save (GMountOperation *op,
568                                      GPasswordSave    save)
569 {
570   GMountOperationPrivate *priv;
571   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
572   priv = op->priv;
573  
574   if (priv->password_save != save)
575     {
576       priv->password_save = save;
577       g_object_notify (G_OBJECT (op), "password-save");
578     }
579 }
580
581 /**
582  * g_mount_operation_get_choice:
583  * @op: a #GMountOperation.
584  * 
585  * Gets a choice from the mount operation.
586  *
587  * Returns: an integer containing an index of the user's choice from 
588  * the choice's list, or %0.
589  **/
590 int
591 g_mount_operation_get_choice (GMountOperation *op)
592 {
593   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
594   return op->priv->choice;
595 }
596
597 /**
598  * g_mount_operation_set_choice:
599  * @op: a #GMountOperation.
600  * @choice: an integer.
601  *
602  * Sets a default choice for the mount operation.
603  **/
604 void
605 g_mount_operation_set_choice (GMountOperation *op,
606                               int              choice)
607 {
608   GMountOperationPrivate *priv;
609   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
610   priv = op->priv;
611   if (priv->choice != choice)
612     {
613       priv->choice = choice;
614       g_object_notify (G_OBJECT (op), "choice");
615     }
616 }
617
618 /**
619  * g_mount_operation_reply:
620  * @op: a #GMountOperation.
621  * @abort: boolean.
622  * 
623  * Emits the #GMountOperation::reply signal.
624  **/
625 void
626 g_mount_operation_reply (GMountOperation *op,
627                          gboolean         abort)
628 {
629   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
630   g_signal_emit (op, signals[REPLY], 0, abort);
631 }
632
633 #define __G_MOUNT_OPERATION_C__
634 #include "gioaliasdef.c"