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);
64 static guint signals[LAST_SIGNAL] = { 0 };
66 struct _GMountOperationPrivate {
71 GPasswordSave password_save;
86 g_mount_operation_set_property (GObject *object,
91 GMountOperation *operation;
93 operation = G_MOUNT_OPERATION (object);
98 g_mount_operation_set_username (operation,
99 g_value_get_string (value));
103 g_mount_operation_set_password (operation,
104 g_value_get_string (value));
108 g_mount_operation_set_anonymous (operation,
109 g_value_get_boolean (value));
113 g_mount_operation_set_domain (operation,
114 g_value_get_string (value));
117 case PROP_PASSWORD_SAVE:
118 g_mount_operation_set_password_save (operation,
119 g_value_get_enum (value));
123 g_mount_operation_set_choice (operation,
124 g_value_get_int (value));
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 g_mount_operation_get_property (GObject *object,
140 GMountOperation *operation;
141 GMountOperationPrivate *priv;
143 operation = G_MOUNT_OPERATION (object);
144 priv = operation->priv;
149 g_value_set_string (value, priv->user);
153 g_value_set_string (value, priv->password);
157 g_value_set_boolean (value, priv->anonymous);
161 g_value_set_string (value, priv->domain);
164 case PROP_PASSWORD_SAVE:
165 g_value_set_enum (value, priv->password_save);
169 g_value_set_int (value, priv->choice);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 g_mount_operation_finalize (GObject *object)
182 GMountOperation *operation;
183 GMountOperationPrivate *priv;
185 operation = G_MOUNT_OPERATION (object);
187 priv = operation->priv;
189 g_free (priv->password);
191 g_free (priv->domain);
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 * If the message contains a line break, the first line should be
255 * presented as a heading. For example, it may be used as the
256 * primary text in a #GtkMessageDialog.
258 signals[ASK_PASSWORD] =
259 g_signal_new (I_("ask-password"),
260 G_TYPE_FROM_CLASS (object_class),
262 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
264 _gio_marshal_VOID__STRING_STRING_STRING_FLAGS,
266 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
269 * GMountOperation::ask-question:
270 * @op: a #GMountOperation asking a question.
271 * @message: string containing a message to display to the user.
272 * @choices: an array of strings for each possible choice.
274 * Emitted when asking the user a question and gives a list of
275 * choices for the user to choose from.
277 * If the message contains a line break, the first line should be
278 * presented as a heading. For example, it may be used as the
279 * primary text in a #GtkMessageDialog.
281 signals[ASK_QUESTION] =
282 g_signal_new (I_("ask-question"),
283 G_TYPE_FROM_CLASS (object_class),
285 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
287 _gio_marshal_VOID__STRING_BOXED,
289 G_TYPE_STRING, G_TYPE_STRV);
292 * GMountOperation::reply:
293 * @op: a #GMountOperation.
294 * @result: a #GMountOperationResult indicating how the request was handled
296 * Emitted when the user has replied to the mount operation.
299 g_signal_new (I_("reply"),
300 G_TYPE_FROM_CLASS (object_class),
302 G_STRUCT_OFFSET (GMountOperationClass, reply),
304 g_cclosure_marshal_VOID__ENUM,
306 G_TYPE_MOUNT_OPERATION_RESULT);
309 * GMountOperation::aborted:
311 * Emitted by the backend when e.g. a device becomes unavailable
312 * while a mount operation is in progress.
314 * Implementations of GMountOperation should handle this signal
315 * by dismissing open password dialogs.
320 g_signal_new (I_("aborted"),
321 G_TYPE_FROM_CLASS (object_class),
323 G_STRUCT_OFFSET (GMountOperationClass, aborted),
325 g_cclosure_marshal_VOID__VOID,
329 * GMountOperation:username:
331 * The user name that is used for authentication when carrying out
332 * the mount operation.
334 g_object_class_install_property (object_class,
336 g_param_spec_string ("username",
341 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
344 * GMountOperation:password:
346 * The password that is used for authentication when carrying out
347 * the mount operation.
349 g_object_class_install_property (object_class,
351 g_param_spec_string ("password",
356 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
359 * GMountOperation:anonymous:
361 * Whether to use an anonymous user when authenticating.
363 g_object_class_install_property (object_class,
365 g_param_spec_boolean ("anonymous",
367 P_("Whether to use an anonymous user"),
370 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
373 * GMountOperation:domain:
375 * The domain to use for the mount operation.
377 g_object_class_install_property (object_class,
379 g_param_spec_string ("domain",
381 P_("The domain of the mount operation"),
384 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
387 * GMountOperation:password-save:
389 * Determines if and how the password information should be saved.
391 g_object_class_install_property (object_class,
393 g_param_spec_enum ("password-save",
395 P_("How passwords should be saved"),
396 G_TYPE_PASSWORD_SAVE,
397 G_PASSWORD_SAVE_NEVER,
399 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
402 * GMountOperation:choice:
404 * The index of the user's choice when a question is asked during the
405 * mount operation. See the #GMountOperation::ask-question signal.
407 g_object_class_install_property (object_class,
409 g_param_spec_int ("choice",
411 P_("The users choice"),
414 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
418 g_mount_operation_init (GMountOperation *operation)
420 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
421 G_TYPE_MOUNT_OPERATION,
422 GMountOperationPrivate);
426 * g_mount_operation_new:
428 * Creates a new mount operation.
430 * Returns: a #GMountOperation.
433 g_mount_operation_new (void)
435 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
439 * g_mount_operation_get_username
440 * @op: a #GMountOperation.
442 * Get the user name from the mount operation.
444 * Returns: a string containing the user name.
447 g_mount_operation_get_username (GMountOperation *op)
449 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
450 return op->priv->user;
454 * g_mount_operation_set_username:
455 * @op: a #GMountOperation.
456 * @username: input username.
458 * Sets the user name within @op to @username.
461 g_mount_operation_set_username (GMountOperation *op,
462 const char *username)
464 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
465 g_free (op->priv->user);
466 op->priv->user = g_strdup (username);
467 g_object_notify (G_OBJECT (op), "username");
471 * g_mount_operation_get_password:
472 * @op: a #GMountOperation.
474 * Gets a password from the mount operation.
476 * Returns: a string containing the password within @op.
479 g_mount_operation_get_password (GMountOperation *op)
481 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
482 return op->priv->password;
486 * g_mount_operation_set_password:
487 * @op: a #GMountOperation.
488 * @password: password to set.
490 * Sets the mount operation's password to @password.
494 g_mount_operation_set_password (GMountOperation *op,
495 const char *password)
497 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
498 g_free (op->priv->password);
499 op->priv->password = g_strdup (password);
500 g_object_notify (G_OBJECT (op), "password");
504 * g_mount_operation_get_anonymous:
505 * @op: a #GMountOperation.
507 * Check to see whether the mount operation is being used
508 * for an anonymous user.
510 * Returns: %TRUE if mount operation is anonymous.
513 g_mount_operation_get_anonymous (GMountOperation *op)
515 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
516 return op->priv->anonymous;
520 * g_mount_operation_set_anonymous:
521 * @op: a #GMountOperation.
522 * @anonymous: boolean value.
524 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
527 g_mount_operation_set_anonymous (GMountOperation *op,
530 GMountOperationPrivate *priv;
531 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
534 if (priv->anonymous != anonymous)
536 priv->anonymous = anonymous;
537 g_object_notify (G_OBJECT (op), "anonymous");
542 * g_mount_operation_get_domain:
543 * @op: a #GMountOperation.
545 * Gets the domain of the mount operation.
547 * Returns: a string set to the domain.
550 g_mount_operation_get_domain (GMountOperation *op)
552 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
553 return op->priv->domain;
557 * g_mount_operation_set_domain:
558 * @op: a #GMountOperation.
559 * @domain: the domain to set.
561 * Sets the mount operation's domain.
564 g_mount_operation_set_domain (GMountOperation *op,
567 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
568 g_free (op->priv->domain);
569 op->priv->domain = g_strdup (domain);
570 g_object_notify (G_OBJECT (op), "domain");
574 * g_mount_operation_get_password_save:
575 * @op: a #GMountOperation.
577 * Gets the state of saving passwords for the mount operation.
579 * Returns: a #GPasswordSave flag.
583 g_mount_operation_get_password_save (GMountOperation *op)
585 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
586 return op->priv->password_save;
590 * g_mount_operation_set_password_save:
591 * @op: a #GMountOperation.
592 * @save: a set of #GPasswordSave flags.
594 * Sets the state of saving passwords for the mount operation.
598 g_mount_operation_set_password_save (GMountOperation *op,
601 GMountOperationPrivate *priv;
602 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
605 if (priv->password_save != save)
607 priv->password_save = save;
608 g_object_notify (G_OBJECT (op), "password-save");
613 * g_mount_operation_get_choice:
614 * @op: a #GMountOperation.
616 * Gets a choice from the mount operation.
618 * Returns: an integer containing an index of the user's choice from
619 * the choice's list, or %0.
622 g_mount_operation_get_choice (GMountOperation *op)
624 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
625 return op->priv->choice;
629 * g_mount_operation_set_choice:
630 * @op: a #GMountOperation.
631 * @choice: an integer.
633 * Sets a default choice for the mount operation.
636 g_mount_operation_set_choice (GMountOperation *op,
639 GMountOperationPrivate *priv;
640 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
642 if (priv->choice != choice)
644 priv->choice = choice;
645 g_object_notify (G_OBJECT (op), "choice");
650 * g_mount_operation_reply:
651 * @op: a #GMountOperation
652 * @result: a #GMountOperationResult
654 * Emits the #GMountOperation::reply signal.
657 g_mount_operation_reply (GMountOperation *op,
658 GMountOperationResult result)
660 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
661 g_signal_emit (op, signals[REPLY], 0, result);
664 #define __G_MOUNT_OPERATION_C__
665 #include "gioaliasdef.c"