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"
33 * SECTION:gmountoperation
34 * @short_description: Object used for authentication and user interaction
37 * #GMountOperation provides a mechanism for interacting with the user.
38 * It can be used for authenticating mountable operations, such as loop
39 * mounting files, hard drive partitions or server locations. It can
40 * also be used to ask the user questions or show a list of applications
41 * preventing unmount or eject operations from completing.
43 * Note that #GMountOperation is used for more than just #GMount
44 * objects – for example it is also used in g_drive_start() and
47 * Users should instantiate a subclass of this that implements all the
48 * various callbacks to show the required dialogs, such as
49 * #GtkMountOperation. If no user interaction is desired (for example
50 * when automounting filesystems at login time), usually %NULL can be
51 * passed, see each method taking a #GMountOperation for details.
54 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
65 static guint signals[LAST_SIGNAL] = { 0 };
67 struct _GMountOperationPrivate {
72 GPasswordSave password_save;
87 g_mount_operation_set_property (GObject *object,
92 GMountOperation *operation;
94 operation = G_MOUNT_OPERATION (object);
99 g_mount_operation_set_username (operation,
100 g_value_get_string (value));
104 g_mount_operation_set_password (operation,
105 g_value_get_string (value));
109 g_mount_operation_set_anonymous (operation,
110 g_value_get_boolean (value));
114 g_mount_operation_set_domain (operation,
115 g_value_get_string (value));
118 case PROP_PASSWORD_SAVE:
119 g_mount_operation_set_password_save (operation,
120 g_value_get_enum (value));
124 g_mount_operation_set_choice (operation,
125 g_value_get_int (value));
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 g_mount_operation_get_property (GObject *object,
141 GMountOperation *operation;
142 GMountOperationPrivate *priv;
144 operation = G_MOUNT_OPERATION (object);
145 priv = operation->priv;
150 g_value_set_string (value, priv->user);
154 g_value_set_string (value, priv->password);
158 g_value_set_boolean (value, priv->anonymous);
162 g_value_set_string (value, priv->domain);
165 case PROP_PASSWORD_SAVE:
166 g_value_set_enum (value, priv->password_save);
170 g_value_set_int (value, priv->choice);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 g_mount_operation_finalize (GObject *object)
183 GMountOperation *operation;
184 GMountOperationPrivate *priv;
186 operation = G_MOUNT_OPERATION (object);
188 priv = operation->priv;
190 g_free (priv->password);
192 g_free (priv->domain);
194 G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize (object);
198 reply_non_handled_in_idle (gpointer data)
200 GMountOperation *op = data;
202 g_mount_operation_reply (op, G_MOUNT_OPERATION_UNHANDLED);
203 return G_SOURCE_REMOVE;
207 ask_password (GMountOperation *op,
209 const char *default_user,
210 const char *default_domain,
211 GAskPasswordFlags flags)
213 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
214 reply_non_handled_in_idle,
220 ask_question (GMountOperation *op,
222 const char *choices[])
224 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
225 reply_non_handled_in_idle,
231 show_processes (GMountOperation *op,
232 const gchar *message,
234 const gchar *choices[])
236 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
237 reply_non_handled_in_idle,
243 g_mount_operation_class_init (GMountOperationClass *klass)
245 GObjectClass *object_class;
247 g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
249 object_class = G_OBJECT_CLASS (klass);
250 object_class->finalize = g_mount_operation_finalize;
251 object_class->get_property = g_mount_operation_get_property;
252 object_class->set_property = g_mount_operation_set_property;
254 klass->ask_password = ask_password;
255 klass->ask_question = ask_question;
256 klass->show_processes = show_processes;
259 * GMountOperation::ask-password:
260 * @op: a #GMountOperation requesting a password.
261 * @message: string containing a message to display to the user.
262 * @default_user: string containing the default user name.
263 * @default_domain: string containing the default domain.
264 * @flags: a set of #GAskPasswordFlags.
266 * Emitted when a mount operation asks the user for a password.
268 * If the message contains a line break, the first line should be
269 * presented as a heading. For example, it may be used as the
270 * primary text in a #GtkMessageDialog.
272 signals[ASK_PASSWORD] =
273 g_signal_new (I_("ask-password"),
274 G_TYPE_FROM_CLASS (object_class),
276 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
280 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
283 * GMountOperation::ask-question:
284 * @op: a #GMountOperation asking a question.
285 * @message: string containing a message to display to the user.
286 * @choices: an array of strings for each possible choice.
288 * Emitted when asking the user a question and gives a list of
289 * choices for the user to choose from.
291 * If the message contains a line break, the first line should be
292 * presented as a heading. For example, it may be used as the
293 * primary text in a #GtkMessageDialog.
295 signals[ASK_QUESTION] =
296 g_signal_new (I_("ask-question"),
297 G_TYPE_FROM_CLASS (object_class),
299 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
303 G_TYPE_STRING, G_TYPE_STRV);
306 * GMountOperation::reply:
307 * @op: a #GMountOperation.
308 * @result: a #GMountOperationResult indicating how the request was handled
310 * Emitted when the user has replied to the mount operation.
313 g_signal_new (I_("reply"),
314 G_TYPE_FROM_CLASS (object_class),
316 G_STRUCT_OFFSET (GMountOperationClass, reply),
318 g_cclosure_marshal_VOID__ENUM,
320 G_TYPE_MOUNT_OPERATION_RESULT);
323 * GMountOperation::aborted:
325 * Emitted by the backend when e.g. a device becomes unavailable
326 * while a mount operation is in progress.
328 * Implementations of GMountOperation should handle this signal
329 * by dismissing open password dialogs.
334 g_signal_new (I_("aborted"),
335 G_TYPE_FROM_CLASS (object_class),
337 G_STRUCT_OFFSET (GMountOperationClass, aborted),
339 g_cclosure_marshal_VOID__VOID,
343 * GMountOperation::show-processes:
344 * @op: a #GMountOperation.
345 * @message: string containing a message to display to the user.
346 * @processes: (element-type GPid): an array of #GPid for processes
347 * blocking the operation.
348 * @choices: an array of strings for each possible choice.
350 * Emitted when one or more processes are blocking an operation
351 * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
353 * Note that this signal may be emitted several times to update the
354 * list of blocking processes as processes close files. The
355 * application should only respond with g_mount_operation_reply() to
356 * the latest signal (setting #GMountOperation:choice to the choice
359 * If the message contains a line break, the first line should be
360 * presented as a heading. For example, it may be used as the
361 * primary text in a #GtkMessageDialog.
365 signals[SHOW_PROCESSES] =
366 g_signal_new (I_("show-processes"),
367 G_TYPE_FROM_CLASS (object_class),
369 G_STRUCT_OFFSET (GMountOperationClass, show_processes),
373 G_TYPE_STRING, G_TYPE_ARRAY, G_TYPE_STRV);
376 * GMountOperation:username:
378 * The user name that is used for authentication when carrying out
379 * the mount operation.
381 g_object_class_install_property (object_class,
383 g_param_spec_string ("username",
388 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
391 * GMountOperation:password:
393 * The password that is used for authentication when carrying out
394 * the mount operation.
396 g_object_class_install_property (object_class,
398 g_param_spec_string ("password",
403 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
406 * GMountOperation:anonymous:
408 * Whether to use an anonymous user when authenticating.
410 g_object_class_install_property (object_class,
412 g_param_spec_boolean ("anonymous",
414 P_("Whether to use an anonymous user"),
417 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
420 * GMountOperation:domain:
422 * The domain to use for the mount operation.
424 g_object_class_install_property (object_class,
426 g_param_spec_string ("domain",
428 P_("The domain of the mount operation"),
431 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
434 * GMountOperation:password-save:
436 * Determines if and how the password information should be saved.
438 g_object_class_install_property (object_class,
440 g_param_spec_enum ("password-save",
442 P_("How passwords should be saved"),
443 G_TYPE_PASSWORD_SAVE,
444 G_PASSWORD_SAVE_NEVER,
446 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
449 * GMountOperation:choice:
451 * The index of the user's choice when a question is asked during the
452 * mount operation. See the #GMountOperation::ask-question signal.
454 g_object_class_install_property (object_class,
456 g_param_spec_int ("choice",
458 P_("The users choice"),
461 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
465 g_mount_operation_init (GMountOperation *operation)
467 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
468 G_TYPE_MOUNT_OPERATION,
469 GMountOperationPrivate);
473 * g_mount_operation_new:
475 * Creates a new mount operation.
477 * Returns: a #GMountOperation.
480 g_mount_operation_new (void)
482 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
486 * g_mount_operation_get_username:
487 * @op: a #GMountOperation.
489 * Get the user name from the mount operation.
491 * Returns: a string containing the user name.
494 g_mount_operation_get_username (GMountOperation *op)
496 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
497 return op->priv->user;
501 * g_mount_operation_set_username:
502 * @op: a #GMountOperation.
503 * @username: input username.
505 * Sets the user name within @op to @username.
508 g_mount_operation_set_username (GMountOperation *op,
509 const char *username)
511 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
512 g_free (op->priv->user);
513 op->priv->user = g_strdup (username);
514 g_object_notify (G_OBJECT (op), "username");
518 * g_mount_operation_get_password:
519 * @op: a #GMountOperation.
521 * Gets a password from the mount operation.
523 * Returns: a string containing the password within @op.
526 g_mount_operation_get_password (GMountOperation *op)
528 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
529 return op->priv->password;
533 * g_mount_operation_set_password:
534 * @op: a #GMountOperation.
535 * @password: password to set.
537 * Sets the mount operation's password to @password.
541 g_mount_operation_set_password (GMountOperation *op,
542 const char *password)
544 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
545 g_free (op->priv->password);
546 op->priv->password = g_strdup (password);
547 g_object_notify (G_OBJECT (op), "password");
551 * g_mount_operation_get_anonymous:
552 * @op: a #GMountOperation.
554 * Check to see whether the mount operation is being used
555 * for an anonymous user.
557 * Returns: %TRUE if mount operation is anonymous.
560 g_mount_operation_get_anonymous (GMountOperation *op)
562 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
563 return op->priv->anonymous;
567 * g_mount_operation_set_anonymous:
568 * @op: a #GMountOperation.
569 * @anonymous: boolean value.
571 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
574 g_mount_operation_set_anonymous (GMountOperation *op,
577 GMountOperationPrivate *priv;
578 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
581 if (priv->anonymous != anonymous)
583 priv->anonymous = anonymous;
584 g_object_notify (G_OBJECT (op), "anonymous");
589 * g_mount_operation_get_domain:
590 * @op: a #GMountOperation.
592 * Gets the domain of the mount operation.
594 * Returns: a string set to the domain.
597 g_mount_operation_get_domain (GMountOperation *op)
599 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
600 return op->priv->domain;
604 * g_mount_operation_set_domain:
605 * @op: a #GMountOperation.
606 * @domain: the domain to set.
608 * Sets the mount operation's domain.
611 g_mount_operation_set_domain (GMountOperation *op,
614 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
615 g_free (op->priv->domain);
616 op->priv->domain = g_strdup (domain);
617 g_object_notify (G_OBJECT (op), "domain");
621 * g_mount_operation_get_password_save:
622 * @op: a #GMountOperation.
624 * Gets the state of saving passwords for the mount operation.
626 * Returns: a #GPasswordSave flag.
630 g_mount_operation_get_password_save (GMountOperation *op)
632 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
633 return op->priv->password_save;
637 * g_mount_operation_set_password_save:
638 * @op: a #GMountOperation.
639 * @save: a set of #GPasswordSave flags.
641 * Sets the state of saving passwords for the mount operation.
645 g_mount_operation_set_password_save (GMountOperation *op,
648 GMountOperationPrivate *priv;
649 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
652 if (priv->password_save != save)
654 priv->password_save = save;
655 g_object_notify (G_OBJECT (op), "password-save");
660 * g_mount_operation_get_choice:
661 * @op: a #GMountOperation.
663 * Gets a choice from the mount operation.
665 * Returns: an integer containing an index of the user's choice from
666 * the choice's list, or %0.
669 g_mount_operation_get_choice (GMountOperation *op)
671 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
672 return op->priv->choice;
676 * g_mount_operation_set_choice:
677 * @op: a #GMountOperation.
678 * @choice: an integer.
680 * Sets a default choice for the mount operation.
683 g_mount_operation_set_choice (GMountOperation *op,
686 GMountOperationPrivate *priv;
687 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
689 if (priv->choice != choice)
691 priv->choice = choice;
692 g_object_notify (G_OBJECT (op), "choice");
697 * g_mount_operation_reply:
698 * @op: a #GMountOperation
699 * @result: a #GMountOperationResult
701 * Emits the #GMountOperation::reply signal.
704 g_mount_operation_reply (GMountOperation *op,
705 GMountOperationResult result)
707 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
708 g_signal_emit (op, signals[REPLY], 0, result);