1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gmountoperation.h"
28 #include "gioenumtypes.h"
29 #include "gio-marshal.h"
35 * SECTION:gmountoperation
36 * @short_description: Authentication methods for mountable locations
38 * #GMountOperation provides a mechanism for authenticating mountable
39 * operations, such as loop mounting files, hard drive partitions or
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.
49 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
58 static guint signals[LAST_SIGNAL] = { 0 };
60 struct _GMountOperationPrivate {
65 GPasswordSave password_save;
80 g_mount_operation_set_property (GObject *object,
85 GMountOperation *operation;
87 operation = G_MOUNT_OPERATION (object);
92 g_mount_operation_set_username (operation,
93 g_value_get_string (value));
97 g_mount_operation_set_password (operation,
98 g_value_get_string (value));
102 g_mount_operation_set_anonymous (operation,
103 g_value_get_boolean (value));
107 g_mount_operation_set_domain (operation,
108 g_value_get_string (value));
111 case PROP_PASSWORD_SAVE:
112 g_mount_operation_set_password_save (operation,
113 g_value_get_enum (value));
117 g_mount_operation_set_choice (operation,
118 g_value_get_int (value));
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 g_mount_operation_get_property (GObject *object,
134 GMountOperation *operation;
135 GMountOperationPrivate *priv;
137 operation = G_MOUNT_OPERATION (object);
138 priv = operation->priv;
143 g_value_set_string (value, priv->user);
147 g_value_set_string (value, priv->password);
151 g_value_set_boolean (value, priv->anonymous);
155 g_value_set_string (value, priv->domain);
158 case PROP_PASSWORD_SAVE:
159 g_value_set_enum (value, priv->password_save);
163 g_value_set_int (value, priv->choice);
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 g_mount_operation_finalize (GObject *object)
176 GMountOperation *operation;
177 GMountOperationPrivate *priv;
179 operation = G_MOUNT_OPERATION (object);
181 priv = operation->priv;
183 g_free (priv->password);
185 g_free (priv->domain);
187 if (G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize)
188 (*G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize) (object);
192 boolean_handled_accumulator (GSignalInvocationHint *ihint,
194 const GValue *handler_return,
197 gboolean continue_emission;
198 gboolean signal_handled;
200 signal_handled = g_value_get_boolean (handler_return);
201 g_value_set_boolean (return_accu, signal_handled);
202 continue_emission = !signal_handled;
204 return continue_emission;
208 ask_password (GMountOperation *op,
210 const char *default_user,
211 const char *default_domain,
212 GPasswordFlags flags)
218 ask_question (GMountOperation *op,
220 const char *choices[])
226 g_mount_operation_class_init (GMountOperationClass *klass)
228 GObjectClass *object_class;
230 g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
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;
237 klass->ask_password = ask_password;
238 klass->ask_question = ask_question;
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.
248 * Emitted when a mount operation asks the user for a password.
250 signals[ASK_PASSWORD] =
251 g_signal_new (I_("ask_password"),
252 G_TYPE_FROM_CLASS (object_class),
254 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
255 boolean_handled_accumulator, NULL,
256 _gio_marshal_BOOLEAN__STRING_STRING_STRING_INT,
258 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT);
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.
266 * Emitted when asking the user a question and gives a list of
267 * choices for the user to choose from.
269 signals[ASK_QUESTION] =
270 g_signal_new (I_("ask_question"),
271 G_TYPE_FROM_CLASS (object_class),
273 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
274 boolean_handled_accumulator, NULL,
275 _gio_marshal_BOOLEAN__STRING_POINTER,
277 G_TYPE_STRING, G_TYPE_POINTER);
280 * GMountOperation::reply:
281 * @op: a #GMountOperation.
282 * @abort: a boolean indicating %TRUE if the operation was aborted.
284 * Emitted when the user has replied to the mount operation.
287 g_signal_new (I_("reply"),
288 G_TYPE_FROM_CLASS (object_class),
290 G_STRUCT_OFFSET (GMountOperationClass, reply),
292 g_cclosure_marshal_VOID__BOOLEAN,
297 * GMountOperation:username:
299 * The user name that is used for authentication when carrying out
300 * the mount operation.
302 g_object_class_install_property (object_class,
304 g_param_spec_string ("username",
308 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
311 * GMountOperation:password:
313 * The password that is used for authentication when carrying out
314 * the mount operation.
316 g_object_class_install_property (object_class,
318 g_param_spec_string ("password",
322 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
325 * GMountOperation:anonymous:
327 * Whether to use an anonymous user when authenticating.
329 g_object_class_install_property (object_class,
331 g_param_spec_boolean ("anonymous",
333 P_("Whether to use an anonymous user"),
335 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
338 * GMountOperation:domain:
340 * The domain to use for the mount operation.
342 g_object_class_install_property (object_class,
344 g_param_spec_string ("domain",
346 P_("The domain of the mount operation"),
348 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
351 * GMountOperation:password-save:
353 * Determines if and how the password information should be saved.
355 g_object_class_install_property (object_class,
357 g_param_spec_enum ("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));
365 * GMountOperation:choice:
367 * The index of the user's choice when a question is asked during the
368 * mount operation. See the #GMountOperation::ask-question signal.
370 g_object_class_install_property (object_class,
372 g_param_spec_int ("choice",
374 P_("The users choice"),
376 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
380 g_mount_operation_init (GMountOperation *operation)
382 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
383 G_TYPE_MOUNT_OPERATION,
384 GMountOperationPrivate);
388 * g_mount_operation_new:
390 * Creates a new mount operation.
392 * Returns: a #GMountOperation.
395 g_mount_operation_new (void)
397 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
401 * g_mount_operation_get_username
402 * @op: a #GMountOperation.
404 * Get the user name from the mount operation.
406 * Returns: a string containing the user name.
409 g_mount_operation_get_username (GMountOperation *op)
411 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
412 return op->priv->user;
416 * g_mount_operation_set_username:
417 * @op: a #GMountOperation.
418 * @username: input username.
420 * Sets the user name within @op to @username.
424 g_mount_operation_set_username (GMountOperation *op,
425 const char *username)
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");
434 * g_mount_operation_get_password:
435 * @op: a #GMountOperation.
437 * Gets a password from the mount operation.
439 * Returns: a string containing the password within @op.
442 g_mount_operation_get_password (GMountOperation *op)
444 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
445 return op->priv->password;
449 * g_mount_operation_set_password:
450 * @op: a #GMountOperation.
451 * @password: password to set.
453 * Sets the mount operation's password to @password.
457 g_mount_operation_set_password (GMountOperation *op,
458 const char *password)
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");
467 * g_mount_operation_get_anonymous:
468 * @op: a #GMountOperation.
470 * Check to see whether the mount operation is being used
471 * for an anonymous user.
473 * Returns: %TRUE if mount operation is anonymous.
476 g_mount_operation_get_anonymous (GMountOperation *op)
478 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
479 return op->priv->anonymous;
483 * g_mount_operation_set_anonymous:
484 * @op: a #GMountOperation.
485 * @anonymous: boolean value.
487 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
490 g_mount_operation_set_anonymous (GMountOperation *op,
493 GMountOperationPrivate *priv;
494 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
497 if (priv->anonymous != anonymous)
499 priv->anonymous = anonymous;
500 g_object_notify (G_OBJECT (op), "anonymous");
505 * g_mount_operation_get_domain:
506 * @op: a #GMountOperation.
508 * Gets the domain of the mount operation.
510 * Returns: a string set to the domain.
513 g_mount_operation_get_domain (GMountOperation *op)
515 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
516 return op->priv->domain;
520 * g_mount_operation_set_domain:
521 * @op: a #GMountOperation.
522 * @domain: the domain to set.
524 * Sets the mount operation's domain.
527 g_mount_operation_set_domain (GMountOperation *op,
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");
537 * g_mount_operation_get_password_save:
538 * @op: a #GMountOperation.
540 * Gets the state of saving passwords for the mount operation.
542 * Returns: a #GPasswordSave flag.
546 g_mount_operation_get_password_save (GMountOperation *op)
548 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
549 return op->priv->password_save;
553 * g_mount_operation_set_password_save:
554 * @op: a #GMountOperation.
555 * @save: a set of #GPasswordSave flags.
557 * Sets the state of saving passwords for the mount operation.
561 g_mount_operation_set_password_save (GMountOperation *op,
564 GMountOperationPrivate *priv;
565 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
568 if (priv->password_save != save)
570 priv->password_save = save;
571 g_object_notify (G_OBJECT (op), "password-save");
576 * g_mount_operation_get_choice:
577 * @op: a #GMountOperation.
579 * Gets a choice from the mount operation.
581 * Returns: an integer containing an index of the user's choice from
582 * the choice's list, or %0.
585 g_mount_operation_get_choice (GMountOperation *op)
587 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
588 return op->priv->choice;
592 * g_mount_operation_set_choice:
593 * @op: a #GMountOperation.
594 * @choice: an integer.
596 * Sets a default choice for the mount operation.
599 g_mount_operation_set_choice (GMountOperation *op,
602 GMountOperationPrivate *priv;
603 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
605 if (priv->choice != choice)
607 priv->choice = choice;
608 g_object_notify (G_OBJECT (op), "choice");
613 * g_mount_operation_reply:
614 * @op: a #GMountOperation.
617 * Emits the #GMountOperation::reply signal.
620 g_mount_operation_reply (GMountOperation *op,
623 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
624 g_signal_emit (op, signals[REPLY], 0, abort);
627 #define __G_MOUNT_OPERATION_C__
628 #include "gioaliasdef.c"