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);
62 SHOW_UNMOUNT_PROGRESS,
66 static guint signals[LAST_SIGNAL] = { 0 };
68 struct _GMountOperationPrivate {
73 GPasswordSave password_save;
88 g_mount_operation_set_property (GObject *object,
93 GMountOperation *operation;
95 operation = G_MOUNT_OPERATION (object);
100 g_mount_operation_set_username (operation,
101 g_value_get_string (value));
105 g_mount_operation_set_password (operation,
106 g_value_get_string (value));
110 g_mount_operation_set_anonymous (operation,
111 g_value_get_boolean (value));
115 g_mount_operation_set_domain (operation,
116 g_value_get_string (value));
119 case PROP_PASSWORD_SAVE:
120 g_mount_operation_set_password_save (operation,
121 g_value_get_enum (value));
125 g_mount_operation_set_choice (operation,
126 g_value_get_int (value));
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137 g_mount_operation_get_property (GObject *object,
142 GMountOperation *operation;
143 GMountOperationPrivate *priv;
145 operation = G_MOUNT_OPERATION (object);
146 priv = operation->priv;
151 g_value_set_string (value, priv->user);
155 g_value_set_string (value, priv->password);
159 g_value_set_boolean (value, priv->anonymous);
163 g_value_set_string (value, priv->domain);
166 case PROP_PASSWORD_SAVE:
167 g_value_set_enum (value, priv->password_save);
171 g_value_set_int (value, priv->choice);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 g_mount_operation_finalize (GObject *object)
184 GMountOperation *operation;
185 GMountOperationPrivate *priv;
187 operation = G_MOUNT_OPERATION (object);
189 priv = operation->priv;
191 g_free (priv->password);
193 g_free (priv->domain);
195 G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize (object);
199 reply_non_handled_in_idle (gpointer data)
201 GMountOperation *op = data;
203 g_mount_operation_reply (op, G_MOUNT_OPERATION_UNHANDLED);
204 return G_SOURCE_REMOVE;
208 ask_password (GMountOperation *op,
210 const char *default_user,
211 const char *default_domain,
212 GAskPasswordFlags flags)
214 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
215 reply_non_handled_in_idle,
221 ask_question (GMountOperation *op,
223 const char *choices[])
225 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
226 reply_non_handled_in_idle,
232 show_processes (GMountOperation *op,
233 const gchar *message,
235 const gchar *choices[])
237 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
238 reply_non_handled_in_idle,
244 show_unmount_progress (GMountOperation *op,
245 const gchar *message,
253 g_mount_operation_class_init (GMountOperationClass *klass)
255 GObjectClass *object_class;
257 g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
259 object_class = G_OBJECT_CLASS (klass);
260 object_class->finalize = g_mount_operation_finalize;
261 object_class->get_property = g_mount_operation_get_property;
262 object_class->set_property = g_mount_operation_set_property;
264 klass->ask_password = ask_password;
265 klass->ask_question = ask_question;
266 klass->show_processes = show_processes;
267 klass->show_unmount_progress = show_unmount_progress;
270 * GMountOperation::ask-password:
271 * @op: a #GMountOperation requesting a password.
272 * @message: string containing a message to display to the user.
273 * @default_user: string containing the default user name.
274 * @default_domain: string containing the default domain.
275 * @flags: a set of #GAskPasswordFlags.
277 * Emitted when a mount operation asks the user for a password.
279 * If the message contains a line break, the first line should be
280 * presented as a heading. For example, it may be used as the
281 * primary text in a #GtkMessageDialog.
283 signals[ASK_PASSWORD] =
284 g_signal_new (I_("ask-password"),
285 G_TYPE_FROM_CLASS (object_class),
287 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
291 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
294 * GMountOperation::ask-question:
295 * @op: a #GMountOperation asking a question.
296 * @message: string containing a message to display to the user.
297 * @choices: an array of strings for each possible choice.
299 * Emitted when asking the user a question and gives a list of
300 * choices for the user to choose from.
302 * If the message contains a line break, the first line should be
303 * presented as a heading. For example, it may be used as the
304 * primary text in a #GtkMessageDialog.
306 signals[ASK_QUESTION] =
307 g_signal_new (I_("ask-question"),
308 G_TYPE_FROM_CLASS (object_class),
310 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
314 G_TYPE_STRING, G_TYPE_STRV);
317 * GMountOperation::reply:
318 * @op: a #GMountOperation.
319 * @result: a #GMountOperationResult indicating how the request was handled
321 * Emitted when the user has replied to the mount operation.
324 g_signal_new (I_("reply"),
325 G_TYPE_FROM_CLASS (object_class),
327 G_STRUCT_OFFSET (GMountOperationClass, reply),
329 g_cclosure_marshal_VOID__ENUM,
331 G_TYPE_MOUNT_OPERATION_RESULT);
334 * GMountOperation::aborted:
336 * Emitted by the backend when e.g. a device becomes unavailable
337 * while a mount operation is in progress.
339 * Implementations of GMountOperation should handle this signal
340 * by dismissing open password dialogs.
345 g_signal_new (I_("aborted"),
346 G_TYPE_FROM_CLASS (object_class),
348 G_STRUCT_OFFSET (GMountOperationClass, aborted),
350 g_cclosure_marshal_VOID__VOID,
354 * GMountOperation::show-processes:
355 * @op: a #GMountOperation.
356 * @message: string containing a message to display to the user.
357 * @processes: (element-type GPid): an array of #GPid for processes
358 * blocking the operation.
359 * @choices: an array of strings for each possible choice.
361 * Emitted when one or more processes are blocking an operation
362 * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
364 * Note that this signal may be emitted several times to update the
365 * list of blocking processes as processes close files. The
366 * application should only respond with g_mount_operation_reply() to
367 * the latest signal (setting #GMountOperation:choice to the choice
370 * If the message contains a line break, the first line should be
371 * presented as a heading. For example, it may be used as the
372 * primary text in a #GtkMessageDialog.
376 signals[SHOW_PROCESSES] =
377 g_signal_new (I_("show-processes"),
378 G_TYPE_FROM_CLASS (object_class),
380 G_STRUCT_OFFSET (GMountOperationClass, show_processes),
384 G_TYPE_STRING, G_TYPE_ARRAY, G_TYPE_STRV);
387 * GMountOperation::show-unmount-progress:
388 * @op: a #GMountOperation:
389 * @message: string containing a mesage to display to the user
390 * @time_left: the estimated time left before the operation completes, or -1
391 * @bytes_left: the amount of bytes to be written before the operation
392 * completes (or -1 if such amount is not known), or zero if the operation
395 * Emitted when an unmount operation has been busy for more than some time
396 * (typically 1.5 seconds).
398 * When unmounting or ejecting a volume, the kernel might need to flush
399 * pending data in its buffers to the volume stable storage, and this operation
400 * can take a considerable amount of time. This signal may be emitted several
401 * times as long as the unmount operation is outstanding, and then one
402 * last time when the operation is completed, with @bytes_left set to zero.
404 * Implementations of GMountOperation should handle this signal by
405 * showing an UI notification, and then dismiss it, or show another notification
406 * of completion, when @bytes_left reaches zero.
408 * If the message contains a line break, the first line should be
409 * presented as a heading. For example, it may be used as the
410 * primary text in a #GtkMessageDialog.
414 signals[SHOW_UNMOUNT_PROGRESS] =
415 g_signal_new (I_("show-unmount-progress"),
416 G_TYPE_FROM_CLASS (object_class),
418 G_STRUCT_OFFSET (GMountOperationClass, show_unmount_progress),
421 G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_UINT64);
424 * GMountOperation:username:
426 * The user name that is used for authentication when carrying out
427 * the mount operation.
429 g_object_class_install_property (object_class,
431 g_param_spec_string ("username",
436 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
439 * GMountOperation:password:
441 * The password that is used for authentication when carrying out
442 * the mount operation.
444 g_object_class_install_property (object_class,
446 g_param_spec_string ("password",
451 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
454 * GMountOperation:anonymous:
456 * Whether to use an anonymous user when authenticating.
458 g_object_class_install_property (object_class,
460 g_param_spec_boolean ("anonymous",
462 P_("Whether to use an anonymous user"),
465 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
468 * GMountOperation:domain:
470 * The domain to use for the mount operation.
472 g_object_class_install_property (object_class,
474 g_param_spec_string ("domain",
476 P_("The domain of the mount operation"),
479 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
482 * GMountOperation:password-save:
484 * Determines if and how the password information should be saved.
486 g_object_class_install_property (object_class,
488 g_param_spec_enum ("password-save",
490 P_("How passwords should be saved"),
491 G_TYPE_PASSWORD_SAVE,
492 G_PASSWORD_SAVE_NEVER,
494 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
497 * GMountOperation:choice:
499 * The index of the user's choice when a question is asked during the
500 * mount operation. See the #GMountOperation::ask-question signal.
502 g_object_class_install_property (object_class,
504 g_param_spec_int ("choice",
506 P_("The users choice"),
509 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
513 g_mount_operation_init (GMountOperation *operation)
515 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
516 G_TYPE_MOUNT_OPERATION,
517 GMountOperationPrivate);
521 * g_mount_operation_new:
523 * Creates a new mount operation.
525 * Returns: a #GMountOperation.
528 g_mount_operation_new (void)
530 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
534 * g_mount_operation_get_username:
535 * @op: a #GMountOperation.
537 * Get the user name from the mount operation.
539 * Returns: a string containing the user name.
542 g_mount_operation_get_username (GMountOperation *op)
544 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
545 return op->priv->user;
549 * g_mount_operation_set_username:
550 * @op: a #GMountOperation.
551 * @username: input username.
553 * Sets the user name within @op to @username.
556 g_mount_operation_set_username (GMountOperation *op,
557 const char *username)
559 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
560 g_free (op->priv->user);
561 op->priv->user = g_strdup (username);
562 g_object_notify (G_OBJECT (op), "username");
566 * g_mount_operation_get_password:
567 * @op: a #GMountOperation.
569 * Gets a password from the mount operation.
571 * Returns: a string containing the password within @op.
574 g_mount_operation_get_password (GMountOperation *op)
576 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
577 return op->priv->password;
581 * g_mount_operation_set_password:
582 * @op: a #GMountOperation.
583 * @password: password to set.
585 * Sets the mount operation's password to @password.
589 g_mount_operation_set_password (GMountOperation *op,
590 const char *password)
592 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
593 g_free (op->priv->password);
594 op->priv->password = g_strdup (password);
595 g_object_notify (G_OBJECT (op), "password");
599 * g_mount_operation_get_anonymous:
600 * @op: a #GMountOperation.
602 * Check to see whether the mount operation is being used
603 * for an anonymous user.
605 * Returns: %TRUE if mount operation is anonymous.
608 g_mount_operation_get_anonymous (GMountOperation *op)
610 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
611 return op->priv->anonymous;
615 * g_mount_operation_set_anonymous:
616 * @op: a #GMountOperation.
617 * @anonymous: boolean value.
619 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
622 g_mount_operation_set_anonymous (GMountOperation *op,
625 GMountOperationPrivate *priv;
626 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
629 if (priv->anonymous != anonymous)
631 priv->anonymous = anonymous;
632 g_object_notify (G_OBJECT (op), "anonymous");
637 * g_mount_operation_get_domain:
638 * @op: a #GMountOperation.
640 * Gets the domain of the mount operation.
642 * Returns: a string set to the domain.
645 g_mount_operation_get_domain (GMountOperation *op)
647 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
648 return op->priv->domain;
652 * g_mount_operation_set_domain:
653 * @op: a #GMountOperation.
654 * @domain: the domain to set.
656 * Sets the mount operation's domain.
659 g_mount_operation_set_domain (GMountOperation *op,
662 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
663 g_free (op->priv->domain);
664 op->priv->domain = g_strdup (domain);
665 g_object_notify (G_OBJECT (op), "domain");
669 * g_mount_operation_get_password_save:
670 * @op: a #GMountOperation.
672 * Gets the state of saving passwords for the mount operation.
674 * Returns: a #GPasswordSave flag.
678 g_mount_operation_get_password_save (GMountOperation *op)
680 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
681 return op->priv->password_save;
685 * g_mount_operation_set_password_save:
686 * @op: a #GMountOperation.
687 * @save: a set of #GPasswordSave flags.
689 * Sets the state of saving passwords for the mount operation.
693 g_mount_operation_set_password_save (GMountOperation *op,
696 GMountOperationPrivate *priv;
697 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
700 if (priv->password_save != save)
702 priv->password_save = save;
703 g_object_notify (G_OBJECT (op), "password-save");
708 * g_mount_operation_get_choice:
709 * @op: a #GMountOperation.
711 * Gets a choice from the mount operation.
713 * Returns: an integer containing an index of the user's choice from
714 * the choice's list, or %0.
717 g_mount_operation_get_choice (GMountOperation *op)
719 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
720 return op->priv->choice;
724 * g_mount_operation_set_choice:
725 * @op: a #GMountOperation.
726 * @choice: an integer.
728 * Sets a default choice for the mount operation.
731 g_mount_operation_set_choice (GMountOperation *op,
734 GMountOperationPrivate *priv;
735 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
737 if (priv->choice != choice)
739 priv->choice = choice;
740 g_object_notify (G_OBJECT (op), "choice");
745 * g_mount_operation_reply:
746 * @op: a #GMountOperation
747 * @result: a #GMountOperationResult
749 * Emits the #GMountOperation::reply signal.
752 g_mount_operation_reply (GMountOperation *op,
753 GMountOperationResult result)
755 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
756 g_signal_emit (op, signals[REPLY], 0, result);