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",
309 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
312 * GMountOperation:password:
314 * The password that is used for authentication when carrying out
315 * the mount operation.
317 g_object_class_install_property (object_class,
319 g_param_spec_string ("password",
324 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
327 * GMountOperation:anonymous:
329 * Whether to use an anonymous user when authenticating.
331 g_object_class_install_property (object_class,
333 g_param_spec_boolean ("anonymous",
335 P_("Whether to use an anonymous user"),
338 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
341 * GMountOperation:domain:
343 * The domain to use for the mount operation.
345 g_object_class_install_property (object_class,
347 g_param_spec_string ("domain",
349 P_("The domain of the mount operation"),
352 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
355 * GMountOperation:password-save:
357 * Determines if and how the password information should be saved.
359 g_object_class_install_property (object_class,
361 g_param_spec_enum ("password-save",
363 P_("How passwords should be saved"),
364 G_TYPE_PASSWORD_SAVE,
365 G_PASSWORD_SAVE_NEVER,
367 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
370 * GMountOperation:choice:
372 * The index of the user's choice when a question is asked during the
373 * mount operation. See the #GMountOperation::ask-question signal.
375 g_object_class_install_property (object_class,
377 g_param_spec_int ("choice",
379 P_("The users choice"),
382 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
386 g_mount_operation_init (GMountOperation *operation)
388 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
389 G_TYPE_MOUNT_OPERATION,
390 GMountOperationPrivate);
394 * g_mount_operation_new:
396 * Creates a new mount operation.
398 * Returns: a #GMountOperation.
401 g_mount_operation_new (void)
403 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
407 * g_mount_operation_get_username
408 * @op: a #GMountOperation.
410 * Get the user name from the mount operation.
412 * Returns: a string containing the user name.
415 g_mount_operation_get_username (GMountOperation *op)
417 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
418 return op->priv->user;
422 * g_mount_operation_set_username:
423 * @op: a #GMountOperation.
424 * @username: input username.
426 * Sets the user name within @op to @username.
430 g_mount_operation_set_username (GMountOperation *op,
431 const char *username)
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");
440 * g_mount_operation_get_password:
441 * @op: a #GMountOperation.
443 * Gets a password from the mount operation.
445 * Returns: a string containing the password within @op.
448 g_mount_operation_get_password (GMountOperation *op)
450 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
451 return op->priv->password;
455 * g_mount_operation_set_password:
456 * @op: a #GMountOperation.
457 * @password: password to set.
459 * Sets the mount operation's password to @password.
463 g_mount_operation_set_password (GMountOperation *op,
464 const char *password)
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");
473 * g_mount_operation_get_anonymous:
474 * @op: a #GMountOperation.
476 * Check to see whether the mount operation is being used
477 * for an anonymous user.
479 * Returns: %TRUE if mount operation is anonymous.
482 g_mount_operation_get_anonymous (GMountOperation *op)
484 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
485 return op->priv->anonymous;
489 * g_mount_operation_set_anonymous:
490 * @op: a #GMountOperation.
491 * @anonymous: boolean value.
493 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
496 g_mount_operation_set_anonymous (GMountOperation *op,
499 GMountOperationPrivate *priv;
500 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
503 if (priv->anonymous != anonymous)
505 priv->anonymous = anonymous;
506 g_object_notify (G_OBJECT (op), "anonymous");
511 * g_mount_operation_get_domain:
512 * @op: a #GMountOperation.
514 * Gets the domain of the mount operation.
516 * Returns: a string set to the domain.
519 g_mount_operation_get_domain (GMountOperation *op)
521 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
522 return op->priv->domain;
526 * g_mount_operation_set_domain:
527 * @op: a #GMountOperation.
528 * @domain: the domain to set.
530 * Sets the mount operation's domain.
533 g_mount_operation_set_domain (GMountOperation *op,
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");
543 * g_mount_operation_get_password_save:
544 * @op: a #GMountOperation.
546 * Gets the state of saving passwords for the mount operation.
548 * Returns: a #GPasswordSave flag.
552 g_mount_operation_get_password_save (GMountOperation *op)
554 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
555 return op->priv->password_save;
559 * g_mount_operation_set_password_save:
560 * @op: a #GMountOperation.
561 * @save: a set of #GPasswordSave flags.
563 * Sets the state of saving passwords for the mount operation.
567 g_mount_operation_set_password_save (GMountOperation *op,
570 GMountOperationPrivate *priv;
571 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
574 if (priv->password_save != save)
576 priv->password_save = save;
577 g_object_notify (G_OBJECT (op), "password-save");
582 * g_mount_operation_get_choice:
583 * @op: a #GMountOperation.
585 * Gets a choice from the mount operation.
587 * Returns: an integer containing an index of the user's choice from
588 * the choice's list, or %0.
591 g_mount_operation_get_choice (GMountOperation *op)
593 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
594 return op->priv->choice;
598 * g_mount_operation_set_choice:
599 * @op: a #GMountOperation.
600 * @choice: an integer.
602 * Sets a default choice for the mount operation.
605 g_mount_operation_set_choice (GMountOperation *op,
608 GMountOperationPrivate *priv;
609 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
611 if (priv->choice != choice)
613 priv->choice = choice;
614 g_object_notify (G_OBJECT (op), "choice");
619 * g_mount_operation_reply:
620 * @op: a #GMountOperation.
623 * Emits the #GMountOperation::reply signal.
626 g_mount_operation_reply (GMountOperation *op,
629 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
630 g_signal_emit (op, signals[REPLY], 0, abort);
633 #define __G_MOUNT_OPERATION_C__
634 #include "gioaliasdef.c"