9a618f326675b8df61073e0a95b11bd9460709c2
[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 "gio-marshal.h"
29 #include "glibintl.h"
30
31 #include "gioalias.h"
32
33 /** 
34  * SECTION:gmountoperation
35  * @short_description: Authentication methods for mountable locations
36  *
37  * #GMountOperation provides a mechanism for authenticating mountable 
38  * operations, such as loop mounting files, hard drive partitions or 
39  * server locations. 
40  *
41  * Mountable GIO backends should implement #GMountOperation if they 
42  * require any priviledges or authentication for their volumes to be 
43  * mounted (e.g. a hard disk partition or an encrypted filesystem), or 
44  * if they are implementing a remote server protocol which requires user
45  * credentials such as FTP or WebDAV. 
46  **/
47
48 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
49
50 enum {
51   ASK_PASSWORD,
52   ASK_QUESTION,
53   REPLY,
54   LAST_SIGNAL
55 };
56
57 static guint signals[LAST_SIGNAL] = { 0 };
58
59 struct _GMountOperationPrivate {
60   char *password;
61   char *user;
62   char *domain;
63   gboolean anonymous;
64   GPasswordSave password_save;
65   int choice;
66 };
67
68 static void
69 g_mount_operation_finalize (GObject *object)
70 {
71   GMountOperation *operation;
72   GMountOperationPrivate *priv;
73
74   operation = G_MOUNT_OPERATION (object);
75
76   priv = operation->priv;
77   
78   g_free (priv->password);
79   g_free (priv->user);
80   g_free (priv->domain);
81   
82   if (G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize)
83     (*G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize) (object);
84 }
85
86 static gboolean
87 boolean_handled_accumulator (GSignalInvocationHint *ihint,
88                              GValue                *return_accu,
89                              const GValue          *handler_return,
90                              gpointer               dummy)
91 {
92   gboolean continue_emission;
93   gboolean signal_handled;
94   
95   signal_handled = g_value_get_boolean (handler_return);
96   g_value_set_boolean (return_accu, signal_handled);
97   continue_emission = !signal_handled;
98   
99   return continue_emission;
100 }
101
102 static gboolean
103 ask_password (GMountOperation *op,
104               const char      *message,
105               const char      *default_user,
106               const char      *default_domain,
107               GPasswordFlags   flags)
108 {
109   return FALSE;
110 }
111   
112 static gboolean
113 ask_question (GMountOperation *op,
114               const char      *message,
115               const char      *choices[])
116 {
117   return FALSE;
118 }
119
120 static void
121 g_mount_operation_class_init (GMountOperationClass *klass)
122 {
123   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124   
125   g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
126   
127   gobject_class->finalize = g_mount_operation_finalize;
128   
129   klass->ask_password = ask_password;
130   klass->ask_question = ask_question;
131   
132   /**
133    * GMountOperation::ask-password:
134    * @op: a #GMountOperation requesting a password.
135    * @message: string containing a message to display to the user.
136    * @default_user: string containing the default user name.
137    * @default_domain: string containing the default domain.
138    * @flags: a set of #GPasswordFlags.
139    * 
140    * Emitted when a mount operation asks the user for a password.
141    */
142   signals[ASK_PASSWORD] =
143     g_signal_new (I_("ask_password"),
144                   G_TYPE_FROM_CLASS (gobject_class),
145                   G_SIGNAL_RUN_LAST,
146                   G_STRUCT_OFFSET (GMountOperationClass, ask_password),
147                   boolean_handled_accumulator, NULL,
148                   _gio_marshal_BOOLEAN__STRING_STRING_STRING_INT,
149                   G_TYPE_BOOLEAN, 4,
150                   G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
151                   
152   /**
153    * GMountOperation::ask-question:
154    * @op: a #GMountOperation asking a question.
155    * @message: string containing a message to display to the user.
156    * @choices: an array of strings for each possible choice.
157    * 
158    * Emitted when asking the user a question and gives a list of choices for the 
159    * user to choose from. 
160    */
161   signals[ASK_QUESTION] =
162     g_signal_new (I_("ask_question"),
163                   G_TYPE_FROM_CLASS (gobject_class),
164                   G_SIGNAL_RUN_LAST,
165                   G_STRUCT_OFFSET (GMountOperationClass, ask_question),
166                   boolean_handled_accumulator, NULL,
167                   _gio_marshal_BOOLEAN__STRING_POINTER,
168                   G_TYPE_BOOLEAN, 2,
169                   G_TYPE_STRING, G_TYPE_POINTER);
170                   
171   /**
172    * GMountOperation::reply:
173    * @op: a #GMountOperation.
174    * @abort: a gboolean indicating %TRUE if the operation was aborted.
175    * 
176    * Emitted when the user has replied to the mount operation.
177    */
178   signals[REPLY] =
179     g_signal_new (I_("reply"),
180                   G_TYPE_FROM_CLASS (gobject_class),
181                   G_SIGNAL_RUN_LAST,
182                   G_STRUCT_OFFSET (GMountOperationClass, reply),
183                   NULL, NULL,
184                   g_cclosure_marshal_VOID__BOOLEAN,
185                   G_TYPE_NONE, 1,
186                   G_TYPE_BOOLEAN);
187 }
188
189 static void
190 g_mount_operation_init (GMountOperation *operation)
191 {
192   operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
193                                                  G_TYPE_MOUNT_OPERATION,
194                                                  GMountOperationPrivate);
195 }
196
197 /**
198  * g_mount_operation_new:
199  * 
200  * Creates a new mount operation.
201  * 
202  * Returns: a #GMountOperation.
203  **/
204 GMountOperation *
205 g_mount_operation_new (void)
206 {
207   return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
208 }
209
210 /**
211  * g_mount_operation_get_username
212  * @op: a #GMountOperation.
213  * 
214  * Get the user name from the mount operation.
215  *
216  * Returns: a string containing the user name.
217  **/
218 const char *
219 g_mount_operation_get_username (GMountOperation *op)
220 {
221   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
222   return op->priv->user;
223 }
224
225 /**
226  * g_mount_operation_set_username:
227  * @op: a #GMountOperation.
228  * @username: input username.
229  *
230  * Sets the user name within @op to @username.
231  * 
232  **/
233 void
234 g_mount_operation_set_username (GMountOperation *op,
235                                 const char      *username)
236 {
237   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
238   g_free (op->priv->user);
239   op->priv->user = g_strdup (username);
240 }
241
242 /**
243  * g_mount_operation_get_password:
244  * @op: a #GMountOperation.
245  *
246  * Gets a password from the mount operation. 
247  *
248  * Returns: a string containing the password within @op.
249  **/
250 const char *
251 g_mount_operation_get_password (GMountOperation *op)
252 {
253   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
254   return op->priv->password;
255 }
256
257 /**
258  * g_mount_operation_set_password:
259  * @op: a #GMountOperation.
260  * @password: password to set.
261  * 
262  * Sets the mount operation's password to @password.  
263  *
264  **/
265 void
266 g_mount_operation_set_password (GMountOperation *op,
267                                 const char      *password)
268 {
269   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
270   g_free (op->priv->password);
271   op->priv->password = g_strdup (password);
272 }
273
274 /**
275  * g_mount_operation_get_anonymous:
276  * @op: a #GMountOperation.
277  * 
278  * Check to see whether the mount operation is being used 
279  * for an anonymous user.
280  * 
281  * Returns: %TRUE if mount operation is anonymous. 
282  **/
283 gboolean
284 g_mount_operation_get_anonymous (GMountOperation *op)
285 {
286   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
287   return op->priv->anonymous;
288 }
289
290 /**
291  * g_mount_operation_set_anonymous:
292  * @op: a #GMountOperation.
293  * @anonymous: boolean value.
294  * 
295  * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
296  **/  
297 void
298 g_mount_operation_set_anonymous (GMountOperation *op,
299                                  gboolean         anonymous)
300 {
301   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
302   op->priv->anonymous = anonymous;
303 }
304
305 /**
306  * g_mount_operation_get_domain:
307  * @op: a #GMountOperation.
308  * 
309  * Gets the domain of the mount operation.
310  * 
311  * Returns: a string set to the domain. 
312  **/
313 const char *
314 g_mount_operation_get_domain (GMountOperation *op)
315 {
316   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
317   return op->priv->domain;
318 }
319
320 /**
321  * g_mount_operation_set_domain:
322  * @op: a #GMountOperation.
323  * @domain: the domain to set.
324  * 
325  * Sets the mount operation's domain. 
326  **/  
327 void
328 g_mount_operation_set_domain (GMountOperation *op,
329                               const char      *domain)
330 {
331   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
332   g_free (op->priv->domain);
333   op->priv->domain = g_strdup (domain);
334 }
335
336 /**
337  * g_mount_operation_get_password_save:
338  * @op: a #GMountOperation.
339  * 
340  * Gets the state of saving passwords for the mount operation.
341  *
342  * Returns: a #GPasswordSave flag. 
343  **/  
344
345 GPasswordSave
346 g_mount_operation_get_password_save (GMountOperation *op)
347 {
348   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
349   return op->priv->password_save;
350 }
351
352 /**
353  * g_mount_operation_set_password_save:
354  * @op: a #GMountOperation.
355  * @save: a set of #GPasswordSave flags.
356  * 
357  * Sets the state of saving passwords for the mount operation.
358  * 
359  **/   
360 void
361 g_mount_operation_set_password_save (GMountOperation *op,
362                                      GPasswordSave    save)
363 {
364   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
365   op->priv->password_save = save;
366 }
367
368 /**
369  * g_mount_operation_get_choice:
370  * @op: a #GMountOperation.
371  * 
372  * Gets a choice from the mount operation.
373  *
374  * Returns: an integer containing an index of the user's choice from 
375  * the choice's list, or %0.
376  **/
377 int
378 g_mount_operation_get_choice (GMountOperation *op)
379 {
380   g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
381   return op->priv->choice;
382 }
383
384 /**
385  * g_mount_operation_set_choice:
386  * @op: a #GMountOperation.
387  * @choice: an integer.
388  *
389  * Sets a default choice for the mount operation.
390  **/
391 void
392 g_mount_operation_set_choice (GMountOperation *op,
393                               int choice)
394 {
395   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
396   op->priv->choice = choice;
397 }
398
399 /**
400  * g_mount_operation_reply:
401  * @op: a #GMountOperation.
402  * @abort: boolean.
403  * 
404  * Emits the #GMountOperation::reply signal.
405  **/
406 void
407 g_mount_operation_reply (GMountOperation *op,
408                          gboolean         abort)
409 {
410   g_return_if_fail (G_IS_MOUNT_OPERATION (op));
411   g_signal_emit (op, signals[REPLY], 0, abort);
412 }
413
414 #define __G_MOUNT_OPERATION_C__
415 #include "gioaliasdef.c"