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
39 * #GMountOperation provides a mechanism for authenticating mountable
40 * operations, such as loop mounting files, hard drive partitions or
43 * Mounting operations are handed a #GMountOperation that then can use
44 * if they require any privileges or authentication for their volumes
45 * to be mounted (e.g. a hard disk partition or an encrypted filesystem),
46 * or if they are implementing a remote server protocol which requires
47 * user credentials such as FTP or WebDAV.
49 * Users should instantiate a subclass of this that implements all
50 * the various callbacks to show the required dialogs, such as
54 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
63 static guint signals[LAST_SIGNAL] = { 0 };
65 struct _GMountOperationPrivate {
70 GPasswordSave password_save;
85 g_mount_operation_set_property (GObject *object,
90 GMountOperation *operation;
92 operation = G_MOUNT_OPERATION (object);
97 g_mount_operation_set_username (operation,
98 g_value_get_string (value));
102 g_mount_operation_set_password (operation,
103 g_value_get_string (value));
107 g_mount_operation_set_anonymous (operation,
108 g_value_get_boolean (value));
112 g_mount_operation_set_domain (operation,
113 g_value_get_string (value));
116 case PROP_PASSWORD_SAVE:
117 g_mount_operation_set_password_save (operation,
118 g_value_get_enum (value));
122 g_mount_operation_set_choice (operation,
123 g_value_get_int (value));
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 g_mount_operation_get_property (GObject *object,
139 GMountOperation *operation;
140 GMountOperationPrivate *priv;
142 operation = G_MOUNT_OPERATION (object);
143 priv = operation->priv;
148 g_value_set_string (value, priv->user);
152 g_value_set_string (value, priv->password);
156 g_value_set_boolean (value, priv->anonymous);
160 g_value_set_string (value, priv->domain);
163 case PROP_PASSWORD_SAVE:
164 g_value_set_enum (value, priv->password_save);
168 g_value_set_int (value, priv->choice);
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 g_mount_operation_finalize (GObject *object)
181 GMountOperation *operation;
182 GMountOperationPrivate *priv;
184 operation = G_MOUNT_OPERATION (object);
186 priv = operation->priv;
188 g_free (priv->password);
190 g_free (priv->domain);
192 if (G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize)
193 (*G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize) (object);
197 reply_non_handled_in_idle (gpointer data)
199 GMountOperation *op = data;
201 g_mount_operation_reply (op, G_MOUNT_OPERATION_UNHANDLED);
206 ask_password (GMountOperation *op,
208 const char *default_user,
209 const char *default_domain,
210 GAskPasswordFlags flags)
212 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
213 reply_non_handled_in_idle,
219 ask_question (GMountOperation *op,
221 const char *choices[])
223 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
224 reply_non_handled_in_idle,
230 g_mount_operation_class_init (GMountOperationClass *klass)
232 GObjectClass *object_class;
234 g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
236 object_class = G_OBJECT_CLASS (klass);
237 object_class->finalize = g_mount_operation_finalize;
238 object_class->get_property = g_mount_operation_get_property;
239 object_class->set_property = g_mount_operation_set_property;
241 klass->ask_password = ask_password;
242 klass->ask_question = ask_question;
245 * GMountOperation::ask-password:
246 * @op: a #GMountOperation requesting a password.
247 * @message: string containing a message to display to the user.
248 * @default_user: string containing the default user name.
249 * @default_domain: string containing the default domain.
250 * @flags: a set of #GAskPasswordFlags.
252 * Emitted when a mount operation asks the user for a password.
254 signals[ASK_PASSWORD] =
255 g_signal_new (I_("ask_password"),
256 G_TYPE_FROM_CLASS (object_class),
258 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
260 _gio_marshal_VOID__STRING_STRING_STRING_FLAGS,
262 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
265 * GMountOperation::ask-question:
266 * @op: a #GMountOperation asking a question.
267 * @message: string containing a message to display to the user.
268 * @choices: an array of strings for each possible choice.
270 * Emitted when asking the user a question and gives a list of
271 * choices for the user to choose from.
273 signals[ASK_QUESTION] =
274 g_signal_new (I_("ask_question"),
275 G_TYPE_FROM_CLASS (object_class),
277 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
279 _gio_marshal_VOID__STRING_BOXED,
281 G_TYPE_STRING, G_TYPE_STRV);
284 * GMountOperation::reply:
285 * @op: a #GMountOperation.
286 * @abort: a boolean indicating %TRUE if the operation was aborted.
288 * Emitted when the user has replied to the mount operation.
291 g_signal_new (I_("reply"),
292 G_TYPE_FROM_CLASS (object_class),
294 G_STRUCT_OFFSET (GMountOperationClass, reply),
296 g_cclosure_marshal_VOID__ENUM,
298 G_TYPE_MOUNT_OPERATION_RESULT);
301 * GMountOperation:username:
303 * The user name that is used for authentication when carrying out
304 * the mount operation.
306 g_object_class_install_property (object_class,
308 g_param_spec_string ("username",
313 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
316 * GMountOperation:password:
318 * The password that is used for authentication when carrying out
319 * the mount operation.
321 g_object_class_install_property (object_class,
323 g_param_spec_string ("password",
328 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
331 * GMountOperation:anonymous:
333 * Whether to use an anonymous user when authenticating.
335 g_object_class_install_property (object_class,
337 g_param_spec_boolean ("anonymous",
339 P_("Whether to use an anonymous user"),
342 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
345 * GMountOperation:domain:
347 * The domain to use for the mount operation.
349 g_object_class_install_property (object_class,
351 g_param_spec_string ("domain",
353 P_("The domain of the mount operation"),
356 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
359 * GMountOperation:password-save:
361 * Determines if and how the password information should be saved.
363 g_object_class_install_property (object_class,
365 g_param_spec_enum ("password-save",
367 P_("How passwords should be saved"),
368 G_TYPE_PASSWORD_SAVE,
369 G_PASSWORD_SAVE_NEVER,
371 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
374 * GMountOperation:choice:
376 * The index of the user's choice when a question is asked during the
377 * mount operation. See the #GMountOperation::ask-question signal.
379 g_object_class_install_property (object_class,
381 g_param_spec_int ("choice",
383 P_("The users choice"),
386 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
390 g_mount_operation_init (GMountOperation *operation)
392 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
393 G_TYPE_MOUNT_OPERATION,
394 GMountOperationPrivate);
398 * g_mount_operation_new:
400 * Creates a new mount operation.
402 * Returns: a #GMountOperation.
405 g_mount_operation_new (void)
407 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
411 * g_mount_operation_get_username
412 * @op: a #GMountOperation.
414 * Get the user name from the mount operation.
416 * Returns: a string containing the user name.
419 g_mount_operation_get_username (GMountOperation *op)
421 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
422 return op->priv->user;
426 * g_mount_operation_set_username:
427 * @op: a #GMountOperation.
428 * @username: input username.
430 * Sets the user name within @op to @username.
434 g_mount_operation_set_username (GMountOperation *op,
435 const char *username)
437 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
438 g_free (op->priv->user);
439 op->priv->user = g_strdup (username);
440 g_object_notify (G_OBJECT (op), "username");
444 * g_mount_operation_get_password:
445 * @op: a #GMountOperation.
447 * Gets a password from the mount operation.
449 * Returns: a string containing the password within @op.
452 g_mount_operation_get_password (GMountOperation *op)
454 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
455 return op->priv->password;
459 * g_mount_operation_set_password:
460 * @op: a #GMountOperation.
461 * @password: password to set.
463 * Sets the mount operation's password to @password.
467 g_mount_operation_set_password (GMountOperation *op,
468 const char *password)
470 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
471 g_free (op->priv->password);
472 op->priv->password = g_strdup (password);
473 g_object_notify (G_OBJECT (op), "password");
477 * g_mount_operation_get_anonymous:
478 * @op: a #GMountOperation.
480 * Check to see whether the mount operation is being used
481 * for an anonymous user.
483 * Returns: %TRUE if mount operation is anonymous.
486 g_mount_operation_get_anonymous (GMountOperation *op)
488 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
489 return op->priv->anonymous;
493 * g_mount_operation_set_anonymous:
494 * @op: a #GMountOperation.
495 * @anonymous: boolean value.
497 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
500 g_mount_operation_set_anonymous (GMountOperation *op,
503 GMountOperationPrivate *priv;
504 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
507 if (priv->anonymous != anonymous)
509 priv->anonymous = anonymous;
510 g_object_notify (G_OBJECT (op), "anonymous");
515 * g_mount_operation_get_domain:
516 * @op: a #GMountOperation.
518 * Gets the domain of the mount operation.
520 * Returns: a string set to the domain.
523 g_mount_operation_get_domain (GMountOperation *op)
525 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
526 return op->priv->domain;
530 * g_mount_operation_set_domain:
531 * @op: a #GMountOperation.
532 * @domain: the domain to set.
534 * Sets the mount operation's domain.
537 g_mount_operation_set_domain (GMountOperation *op,
540 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
541 g_free (op->priv->domain);
542 op->priv->domain = g_strdup (domain);
543 g_object_notify (G_OBJECT (op), "domain");
547 * g_mount_operation_get_password_save:
548 * @op: a #GMountOperation.
550 * Gets the state of saving passwords for the mount operation.
552 * Returns: a #GPasswordSave flag.
556 g_mount_operation_get_password_save (GMountOperation *op)
558 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
559 return op->priv->password_save;
563 * g_mount_operation_set_password_save:
564 * @op: a #GMountOperation.
565 * @save: a set of #GPasswordSave flags.
567 * Sets the state of saving passwords for the mount operation.
571 g_mount_operation_set_password_save (GMountOperation *op,
574 GMountOperationPrivate *priv;
575 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
578 if (priv->password_save != save)
580 priv->password_save = save;
581 g_object_notify (G_OBJECT (op), "password-save");
586 * g_mount_operation_get_choice:
587 * @op: a #GMountOperation.
589 * Gets a choice from the mount operation.
591 * Returns: an integer containing an index of the user's choice from
592 * the choice's list, or %0.
595 g_mount_operation_get_choice (GMountOperation *op)
597 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
598 return op->priv->choice;
602 * g_mount_operation_set_choice:
603 * @op: a #GMountOperation.
604 * @choice: an integer.
606 * Sets a default choice for the mount operation.
609 g_mount_operation_set_choice (GMountOperation *op,
612 GMountOperationPrivate *priv;
613 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
615 if (priv->choice != choice)
617 priv->choice = choice;
618 g_object_notify (G_OBJECT (op), "choice");
623 * g_mount_operation_reply:
624 * @op: a #GMountOperation
625 * @result: a #GMountOperationResult
627 * Emits the #GMountOperation::reply signal.
630 g_mount_operation_reply (GMountOperation *op,
631 GMountOperationResult result)
633 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
634 g_signal_emit (op, signals[REPLY], 0, result);
637 #define __G_MOUNT_OPERATION_C__
638 #include "gioaliasdef.c"