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