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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gmountoperation.h"
26 #include "gioenumtypes.h"
31 * SECTION:gmountoperation
32 * @short_description: Object used for authentication and user interaction
35 * #GMountOperation provides a mechanism for interacting with the user.
36 * It can be used for authenticating mountable operations, such as loop
37 * mounting files, hard drive partitions or server locations. It can
38 * also be used to ask the user questions or show a list of applications
39 * preventing unmount or eject operations from completing.
41 * Note that #GMountOperation is used for more than just #GMount
42 * objects – for example it is also used in g_drive_start() and
45 * Users should instantiate a subclass of this that implements all the
46 * various callbacks to show the required dialogs, such as
47 * #GtkMountOperation. If no user interaction is desired (for example
48 * when automounting filesystems at login time), usually %NULL can be
49 * passed, see each method taking a #GMountOperation for details.
58 SHOW_UNMOUNT_PROGRESS
,
62 static guint signals
[LAST_SIGNAL
] = { 0 };
64 struct _GMountOperationPrivate
{
69 GPasswordSave password_save
;
83 G_DEFINE_TYPE_WITH_PRIVATE (GMountOperation
, g_mount_operation
, G_TYPE_OBJECT
)
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
);
202 return G_SOURCE_REMOVE
;
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 show_processes (GMountOperation
*op
,
231 const gchar
*message
,
233 const gchar
*choices
[])
235 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE
,
236 reply_non_handled_in_idle
,
242 show_unmount_progress (GMountOperation
*op
,
243 const gchar
*message
,
251 g_mount_operation_class_init (GMountOperationClass
*klass
)
253 GObjectClass
*object_class
;
255 object_class
= G_OBJECT_CLASS (klass
);
256 object_class
->finalize
= g_mount_operation_finalize
;
257 object_class
->get_property
= g_mount_operation_get_property
;
258 object_class
->set_property
= g_mount_operation_set_property
;
260 klass
->ask_password
= ask_password
;
261 klass
->ask_question
= ask_question
;
262 klass
->show_processes
= show_processes
;
263 klass
->show_unmount_progress
= show_unmount_progress
;
266 * GMountOperation::ask-password:
267 * @op: a #GMountOperation requesting a password.
268 * @message: string containing a message to display to the user.
269 * @default_user: string containing the default user name.
270 * @default_domain: string containing the default domain.
271 * @flags: a set of #GAskPasswordFlags.
273 * Emitted when a mount operation asks the user for a password.
275 * If the message contains a line break, the first line should be
276 * presented as a heading. For example, it may be used as the
277 * primary text in a #GtkMessageDialog.
279 signals
[ASK_PASSWORD
] =
280 g_signal_new (I_("ask-password"),
281 G_TYPE_FROM_CLASS (object_class
),
283 G_STRUCT_OFFSET (GMountOperationClass
, ask_password
),
287 G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_ASK_PASSWORD_FLAGS
);
290 * GMountOperation::ask-question:
291 * @op: a #GMountOperation asking a question.
292 * @message: string containing a message to display to the user.
293 * @choices: an array of strings for each possible choice.
295 * Emitted when asking the user a question and gives a list of
296 * choices for the user to choose from.
298 * If the message contains a line break, the first line should be
299 * presented as a heading. For example, it may be used as the
300 * primary text in a #GtkMessageDialog.
302 signals
[ASK_QUESTION
] =
303 g_signal_new (I_("ask-question"),
304 G_TYPE_FROM_CLASS (object_class
),
306 G_STRUCT_OFFSET (GMountOperationClass
, ask_question
),
310 G_TYPE_STRING
, G_TYPE_STRV
);
313 * GMountOperation::reply:
314 * @op: a #GMountOperation.
315 * @result: a #GMountOperationResult indicating how the request was handled
317 * Emitted when the user has replied to the mount operation.
320 g_signal_new (I_("reply"),
321 G_TYPE_FROM_CLASS (object_class
),
323 G_STRUCT_OFFSET (GMountOperationClass
, reply
),
325 g_cclosure_marshal_VOID__ENUM
,
327 G_TYPE_MOUNT_OPERATION_RESULT
);
330 * GMountOperation::aborted:
332 * Emitted by the backend when e.g. a device becomes unavailable
333 * while a mount operation is in progress.
335 * Implementations of GMountOperation should handle this signal
336 * by dismissing open password dialogs.
341 g_signal_new (I_("aborted"),
342 G_TYPE_FROM_CLASS (object_class
),
344 G_STRUCT_OFFSET (GMountOperationClass
, aborted
),
346 g_cclosure_marshal_VOID__VOID
,
350 * GMountOperation::show-processes:
351 * @op: a #GMountOperation.
352 * @message: string containing a message to display to the user.
353 * @processes: (element-type GPid): an array of #GPid for processes
354 * blocking the operation.
355 * @choices: an array of strings for each possible choice.
357 * Emitted when one or more processes are blocking an operation
358 * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
360 * Note that this signal may be emitted several times to update the
361 * list of blocking processes as processes close files. The
362 * application should only respond with g_mount_operation_reply() to
363 * the latest signal (setting #GMountOperation:choice to the choice
366 * If the message contains a line break, the first line should be
367 * presented as a heading. For example, it may be used as the
368 * primary text in a #GtkMessageDialog.
372 signals
[SHOW_PROCESSES
] =
373 g_signal_new (I_("show-processes"),
374 G_TYPE_FROM_CLASS (object_class
),
376 G_STRUCT_OFFSET (GMountOperationClass
, show_processes
),
380 G_TYPE_STRING
, G_TYPE_ARRAY
, G_TYPE_STRV
);
383 * GMountOperation::show-unmount-progress:
384 * @op: a #GMountOperation:
385 * @message: string containing a mesage to display to the user
386 * @time_left: the estimated time left before the operation completes,
387 * in microseconds, or -1
388 * @bytes_left: the amount of bytes to be written before the operation
389 * completes (or -1 if such amount is not known), or zero if the operation
392 * Emitted when an unmount operation has been busy for more than some time
393 * (typically 1.5 seconds).
395 * When unmounting or ejecting a volume, the kernel might need to flush
396 * pending data in its buffers to the volume stable storage, and this operation
397 * can take a considerable amount of time. This signal may be emitted several
398 * times as long as the unmount operation is outstanding, and then one
399 * last time when the operation is completed, with @bytes_left set to zero.
401 * Implementations of GMountOperation should handle this signal by
402 * showing an UI notification, and then dismiss it, or show another notification
403 * of completion, when @bytes_left reaches zero.
405 * If the message contains a line break, the first line should be
406 * presented as a heading. For example, it may be used as the
407 * primary text in a #GtkMessageDialog.
411 signals
[SHOW_UNMOUNT_PROGRESS
] =
412 g_signal_new (I_("show-unmount-progress"),
413 G_TYPE_FROM_CLASS (object_class
),
415 G_STRUCT_OFFSET (GMountOperationClass
, show_unmount_progress
),
418 G_TYPE_STRING
, G_TYPE_INT64
, G_TYPE_INT64
);
421 * GMountOperation:username:
423 * The user name that is used for authentication when carrying out
424 * the mount operation.
426 g_object_class_install_property (object_class
,
428 g_param_spec_string ("username",
433 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
436 * GMountOperation:password:
438 * The password that is used for authentication when carrying out
439 * the mount operation.
441 g_object_class_install_property (object_class
,
443 g_param_spec_string ("password",
448 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
451 * GMountOperation:anonymous:
453 * Whether to use an anonymous user when authenticating.
455 g_object_class_install_property (object_class
,
457 g_param_spec_boolean ("anonymous",
459 P_("Whether to use an anonymous user"),
462 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
465 * GMountOperation:domain:
467 * The domain to use for the mount operation.
469 g_object_class_install_property (object_class
,
471 g_param_spec_string ("domain",
473 P_("The domain of the mount operation"),
476 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
479 * GMountOperation:password-save:
481 * Determines if and how the password information should be saved.
483 g_object_class_install_property (object_class
,
485 g_param_spec_enum ("password-save",
487 P_("How passwords should be saved"),
488 G_TYPE_PASSWORD_SAVE
,
489 G_PASSWORD_SAVE_NEVER
,
491 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
494 * GMountOperation:choice:
496 * The index of the user's choice when a question is asked during the
497 * mount operation. See the #GMountOperation::ask-question signal.
499 g_object_class_install_property (object_class
,
501 g_param_spec_int ("choice",
503 P_("The users choice"),
506 G_PARAM_STATIC_NAME
|G_PARAM_STATIC_NICK
|G_PARAM_STATIC_BLURB
));
510 g_mount_operation_init (GMountOperation
*operation
)
512 operation
->priv
= g_mount_operation_get_instance_private (operation
);
516 * g_mount_operation_new:
518 * Creates a new mount operation.
520 * Returns: a #GMountOperation.
523 g_mount_operation_new (void)
525 return g_object_new (G_TYPE_MOUNT_OPERATION
, NULL
);
529 * g_mount_operation_get_username:
530 * @op: a #GMountOperation.
532 * Get the user name from the mount operation.
534 * Returns: a string containing the user name.
537 g_mount_operation_get_username (GMountOperation
*op
)
539 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), NULL
);
540 return op
->priv
->user
;
544 * g_mount_operation_set_username:
545 * @op: a #GMountOperation.
546 * @username: input username.
548 * Sets the user name within @op to @username.
551 g_mount_operation_set_username (GMountOperation
*op
,
552 const char *username
)
554 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
555 g_free (op
->priv
->user
);
556 op
->priv
->user
= g_strdup (username
);
557 g_object_notify (G_OBJECT (op
), "username");
561 * g_mount_operation_get_password:
562 * @op: a #GMountOperation.
564 * Gets a password from the mount operation.
566 * Returns: a string containing the password within @op.
569 g_mount_operation_get_password (GMountOperation
*op
)
571 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), NULL
);
572 return op
->priv
->password
;
576 * g_mount_operation_set_password:
577 * @op: a #GMountOperation.
578 * @password: password to set.
580 * Sets the mount operation's password to @password.
584 g_mount_operation_set_password (GMountOperation
*op
,
585 const char *password
)
587 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
588 g_free (op
->priv
->password
);
589 op
->priv
->password
= g_strdup (password
);
590 g_object_notify (G_OBJECT (op
), "password");
594 * g_mount_operation_get_anonymous:
595 * @op: a #GMountOperation.
597 * Check to see whether the mount operation is being used
598 * for an anonymous user.
600 * Returns: %TRUE if mount operation is anonymous.
603 g_mount_operation_get_anonymous (GMountOperation
*op
)
605 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), FALSE
);
606 return op
->priv
->anonymous
;
610 * g_mount_operation_set_anonymous:
611 * @op: a #GMountOperation.
612 * @anonymous: boolean value.
614 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
617 g_mount_operation_set_anonymous (GMountOperation
*op
,
620 GMountOperationPrivate
*priv
;
621 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
624 if (priv
->anonymous
!= anonymous
)
626 priv
->anonymous
= anonymous
;
627 g_object_notify (G_OBJECT (op
), "anonymous");
632 * g_mount_operation_get_domain:
633 * @op: a #GMountOperation.
635 * Gets the domain of the mount operation.
637 * Returns: a string set to the domain.
640 g_mount_operation_get_domain (GMountOperation
*op
)
642 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), NULL
);
643 return op
->priv
->domain
;
647 * g_mount_operation_set_domain:
648 * @op: a #GMountOperation.
649 * @domain: the domain to set.
651 * Sets the mount operation's domain.
654 g_mount_operation_set_domain (GMountOperation
*op
,
657 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
658 g_free (op
->priv
->domain
);
659 op
->priv
->domain
= g_strdup (domain
);
660 g_object_notify (G_OBJECT (op
), "domain");
664 * g_mount_operation_get_password_save:
665 * @op: a #GMountOperation.
667 * Gets the state of saving passwords for the mount operation.
669 * Returns: a #GPasswordSave flag.
673 g_mount_operation_get_password_save (GMountOperation
*op
)
675 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), G_PASSWORD_SAVE_NEVER
);
676 return op
->priv
->password_save
;
680 * g_mount_operation_set_password_save:
681 * @op: a #GMountOperation.
682 * @save: a set of #GPasswordSave flags.
684 * Sets the state of saving passwords for the mount operation.
688 g_mount_operation_set_password_save (GMountOperation
*op
,
691 GMountOperationPrivate
*priv
;
692 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
695 if (priv
->password_save
!= save
)
697 priv
->password_save
= save
;
698 g_object_notify (G_OBJECT (op
), "password-save");
703 * g_mount_operation_get_choice:
704 * @op: a #GMountOperation.
706 * Gets a choice from the mount operation.
708 * Returns: an integer containing an index of the user's choice from
709 * the choice's list, or %0.
712 g_mount_operation_get_choice (GMountOperation
*op
)
714 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op
), 0);
715 return op
->priv
->choice
;
719 * g_mount_operation_set_choice:
720 * @op: a #GMountOperation.
721 * @choice: an integer.
723 * Sets a default choice for the mount operation.
726 g_mount_operation_set_choice (GMountOperation
*op
,
729 GMountOperationPrivate
*priv
;
730 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
732 if (priv
->choice
!= choice
)
734 priv
->choice
= choice
;
735 g_object_notify (G_OBJECT (op
), "choice");
740 * g_mount_operation_reply:
741 * @op: a #GMountOperation
742 * @result: a #GMountOperationResult
744 * Emits the #GMountOperation::reply signal.
747 g_mount_operation_reply (GMountOperation
*op
,
748 GMountOperationResult result
)
750 g_return_if_fail (G_IS_MOUNT_OPERATION (op
));
751 g_signal_emit (op
, signals
[REPLY
], 0, result
);