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: Object used for authentication and user interaction
39 * #GMountOperation provides a mechanism for interacting with the user.
40 * It can be used for authenticating mountable operations, such as loop
41 * mounting files, hard drive partitions or server locations. It can
42 * also be used to ask the user questions or show a list of applications
43 * preventing unmount or eject operations from completing.
45 * Note that #GMountOperation is used for more than just #GMount
46 * objects – for example it is also used in g_drive_start() and
49 * Users should instantiate a subclass of this that implements all the
50 * various callbacks to show the required dialogs, such as
51 * #GtkMountOperation. If no user interaction is desired (for example
52 * when automounting filesystems at login time), usually %NULL can be
53 * passed, see each method taking a #GMountOperation for details.
56 G_DEFINE_TYPE (GMountOperation, g_mount_operation, G_TYPE_OBJECT);
67 static guint signals[LAST_SIGNAL] = { 0 };
69 struct _GMountOperationPrivate {
74 GPasswordSave password_save;
89 g_mount_operation_set_property (GObject *object,
94 GMountOperation *operation;
96 operation = G_MOUNT_OPERATION (object);
101 g_mount_operation_set_username (operation,
102 g_value_get_string (value));
106 g_mount_operation_set_password (operation,
107 g_value_get_string (value));
111 g_mount_operation_set_anonymous (operation,
112 g_value_get_boolean (value));
116 g_mount_operation_set_domain (operation,
117 g_value_get_string (value));
120 case PROP_PASSWORD_SAVE:
121 g_mount_operation_set_password_save (operation,
122 g_value_get_enum (value));
126 g_mount_operation_set_choice (operation,
127 g_value_get_int (value));
131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 g_mount_operation_get_property (GObject *object,
143 GMountOperation *operation;
144 GMountOperationPrivate *priv;
146 operation = G_MOUNT_OPERATION (object);
147 priv = operation->priv;
152 g_value_set_string (value, priv->user);
156 g_value_set_string (value, priv->password);
160 g_value_set_boolean (value, priv->anonymous);
164 g_value_set_string (value, priv->domain);
167 case PROP_PASSWORD_SAVE:
168 g_value_set_enum (value, priv->password_save);
172 g_value_set_int (value, priv->choice);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 g_mount_operation_finalize (GObject *object)
185 GMountOperation *operation;
186 GMountOperationPrivate *priv;
188 operation = G_MOUNT_OPERATION (object);
190 priv = operation->priv;
192 g_free (priv->password);
194 g_free (priv->domain);
196 G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize (object);
200 reply_non_handled_in_idle (gpointer data)
202 GMountOperation *op = data;
204 g_mount_operation_reply (op, G_MOUNT_OPERATION_UNHANDLED);
209 ask_password (GMountOperation *op,
211 const char *default_user,
212 const char *default_domain,
213 GAskPasswordFlags flags)
215 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
216 reply_non_handled_in_idle,
222 ask_question (GMountOperation *op,
224 const char *choices[])
226 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
227 reply_non_handled_in_idle,
233 show_processes (GMountOperation *op,
234 const gchar *message,
236 const gchar *choices[])
238 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
239 reply_non_handled_in_idle,
245 g_mount_operation_class_init (GMountOperationClass *klass)
247 GObjectClass *object_class;
249 g_type_class_add_private (klass, sizeof (GMountOperationPrivate));
251 object_class = G_OBJECT_CLASS (klass);
252 object_class->finalize = g_mount_operation_finalize;
253 object_class->get_property = g_mount_operation_get_property;
254 object_class->set_property = g_mount_operation_set_property;
256 klass->ask_password = ask_password;
257 klass->ask_question = ask_question;
258 klass->show_processes = show_processes;
261 * GMountOperation::ask-password:
262 * @op: a #GMountOperation requesting a password.
263 * @message: string containing a message to display to the user.
264 * @default_user: string containing the default user name.
265 * @default_domain: string containing the default domain.
266 * @flags: a set of #GAskPasswordFlags.
268 * Emitted when a mount operation asks the user for a password.
270 * If the message contains a line break, the first line should be
271 * presented as a heading. For example, it may be used as the
272 * primary text in a #GtkMessageDialog.
274 signals[ASK_PASSWORD] =
275 g_signal_new (I_("ask-password"),
276 G_TYPE_FROM_CLASS (object_class),
278 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
280 _gio_marshal_VOID__STRING_STRING_STRING_FLAGS,
282 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ASK_PASSWORD_FLAGS);
285 * GMountOperation::ask-question:
286 * @op: a #GMountOperation asking a question.
287 * @message: string containing a message to display to the user.
288 * @choices: an array of strings for each possible choice.
290 * Emitted when asking the user a question and gives a list of
291 * choices for the user to choose from.
293 * If the message contains a line break, the first line should be
294 * presented as a heading. For example, it may be used as the
295 * primary text in a #GtkMessageDialog.
297 signals[ASK_QUESTION] =
298 g_signal_new (I_("ask-question"),
299 G_TYPE_FROM_CLASS (object_class),
301 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
303 _gio_marshal_VOID__STRING_BOXED,
305 G_TYPE_STRING, G_TYPE_STRV);
308 * GMountOperation::reply:
309 * @op: a #GMountOperation.
310 * @result: a #GMountOperationResult indicating how the request was handled
312 * Emitted when the user has replied to the mount operation.
315 g_signal_new (I_("reply"),
316 G_TYPE_FROM_CLASS (object_class),
318 G_STRUCT_OFFSET (GMountOperationClass, reply),
320 g_cclosure_marshal_VOID__ENUM,
322 G_TYPE_MOUNT_OPERATION_RESULT);
325 * GMountOperation::aborted:
327 * Emitted by the backend when e.g. a device becomes unavailable
328 * while a mount operation is in progress.
330 * Implementations of GMountOperation should handle this signal
331 * by dismissing open password dialogs.
336 g_signal_new (I_("aborted"),
337 G_TYPE_FROM_CLASS (object_class),
339 G_STRUCT_OFFSET (GMountOperationClass, aborted),
341 g_cclosure_marshal_VOID__VOID,
345 * GMountOperation::show-processes:
346 * @op: a #GMountOperation.
347 * @message: string containing a message to display to the user.
348 * @processes: an array of #GPid for processes blocking the operation.
349 * @choices: an array of strings for each possible choice.
351 * Emitted when one or more processes are blocking an operation
352 * e.g. unmounting/ejecting a #GMount or stopping a #GDrive.
354 * Note that this signal may be emitted several times to update the
355 * list of blocking processes as processes close files. The
356 * application should only respond with g_mount_operation_reply() to
357 * the latest signal (setting #GMountOperation:choice to the choice
360 * If the message contains a line break, the first line should be
361 * presented as a heading. For example, it may be used as the
362 * primary text in a #GtkMessageDialog.
366 signals[SHOW_PROCESSES] =
367 g_signal_new (I_("show-processes"),
368 G_TYPE_FROM_CLASS (object_class),
370 G_STRUCT_OFFSET (GMountOperationClass, show_processes),
372 _gio_marshal_VOID__STRING_BOXED_BOXED,
374 G_TYPE_STRING, G_TYPE_ARRAY, G_TYPE_STRV);
377 * GMountOperation:username:
379 * The user name that is used for authentication when carrying out
380 * the mount operation.
382 g_object_class_install_property (object_class,
384 g_param_spec_string ("username",
389 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
392 * GMountOperation:password:
394 * The password that is used for authentication when carrying out
395 * the mount operation.
397 g_object_class_install_property (object_class,
399 g_param_spec_string ("password",
404 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
407 * GMountOperation:anonymous:
409 * Whether to use an anonymous user when authenticating.
411 g_object_class_install_property (object_class,
413 g_param_spec_boolean ("anonymous",
415 P_("Whether to use an anonymous user"),
418 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
421 * GMountOperation:domain:
423 * The domain to use for the mount operation.
425 g_object_class_install_property (object_class,
427 g_param_spec_string ("domain",
429 P_("The domain of the mount operation"),
432 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
435 * GMountOperation:password-save:
437 * Determines if and how the password information should be saved.
439 g_object_class_install_property (object_class,
441 g_param_spec_enum ("password-save",
443 P_("How passwords should be saved"),
444 G_TYPE_PASSWORD_SAVE,
445 G_PASSWORD_SAVE_NEVER,
447 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
450 * GMountOperation:choice:
452 * The index of the user's choice when a question is asked during the
453 * mount operation. See the #GMountOperation::ask-question signal.
455 g_object_class_install_property (object_class,
457 g_param_spec_int ("choice",
459 P_("The users choice"),
462 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
466 g_mount_operation_init (GMountOperation *operation)
468 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
469 G_TYPE_MOUNT_OPERATION,
470 GMountOperationPrivate);
474 * g_mount_operation_new:
476 * Creates a new mount operation.
478 * Returns: a #GMountOperation.
481 g_mount_operation_new (void)
483 return g_object_new (G_TYPE_MOUNT_OPERATION, NULL);
487 * g_mount_operation_get_username
488 * @op: a #GMountOperation.
490 * Get the user name from the mount operation.
492 * Returns: a string containing the user name.
495 g_mount_operation_get_username (GMountOperation *op)
497 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
498 return op->priv->user;
502 * g_mount_operation_set_username:
503 * @op: a #GMountOperation.
504 * @username: input username.
506 * Sets the user name within @op to @username.
509 g_mount_operation_set_username (GMountOperation *op,
510 const char *username)
512 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
513 g_free (op->priv->user);
514 op->priv->user = g_strdup (username);
515 g_object_notify (G_OBJECT (op), "username");
519 * g_mount_operation_get_password:
520 * @op: a #GMountOperation.
522 * Gets a password from the mount operation.
524 * Returns: a string containing the password within @op.
527 g_mount_operation_get_password (GMountOperation *op)
529 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
530 return op->priv->password;
534 * g_mount_operation_set_password:
535 * @op: a #GMountOperation.
536 * @password: password to set.
538 * Sets the mount operation's password to @password.
542 g_mount_operation_set_password (GMountOperation *op,
543 const char *password)
545 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
546 g_free (op->priv->password);
547 op->priv->password = g_strdup (password);
548 g_object_notify (G_OBJECT (op), "password");
552 * g_mount_operation_get_anonymous:
553 * @op: a #GMountOperation.
555 * Check to see whether the mount operation is being used
556 * for an anonymous user.
558 * Returns: %TRUE if mount operation is anonymous.
561 g_mount_operation_get_anonymous (GMountOperation *op)
563 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), FALSE);
564 return op->priv->anonymous;
568 * g_mount_operation_set_anonymous:
569 * @op: a #GMountOperation.
570 * @anonymous: boolean value.
572 * Sets the mount operation to use an anonymous user if @anonymous is %TRUE.
575 g_mount_operation_set_anonymous (GMountOperation *op,
578 GMountOperationPrivate *priv;
579 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
582 if (priv->anonymous != anonymous)
584 priv->anonymous = anonymous;
585 g_object_notify (G_OBJECT (op), "anonymous");
590 * g_mount_operation_get_domain:
591 * @op: a #GMountOperation.
593 * Gets the domain of the mount operation.
595 * Returns: a string set to the domain.
598 g_mount_operation_get_domain (GMountOperation *op)
600 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), NULL);
601 return op->priv->domain;
605 * g_mount_operation_set_domain:
606 * @op: a #GMountOperation.
607 * @domain: the domain to set.
609 * Sets the mount operation's domain.
612 g_mount_operation_set_domain (GMountOperation *op,
615 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
616 g_free (op->priv->domain);
617 op->priv->domain = g_strdup (domain);
618 g_object_notify (G_OBJECT (op), "domain");
622 * g_mount_operation_get_password_save:
623 * @op: a #GMountOperation.
625 * Gets the state of saving passwords for the mount operation.
627 * Returns: a #GPasswordSave flag.
631 g_mount_operation_get_password_save (GMountOperation *op)
633 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), G_PASSWORD_SAVE_NEVER);
634 return op->priv->password_save;
638 * g_mount_operation_set_password_save:
639 * @op: a #GMountOperation.
640 * @save: a set of #GPasswordSave flags.
642 * Sets the state of saving passwords for the mount operation.
646 g_mount_operation_set_password_save (GMountOperation *op,
649 GMountOperationPrivate *priv;
650 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
653 if (priv->password_save != save)
655 priv->password_save = save;
656 g_object_notify (G_OBJECT (op), "password-save");
661 * g_mount_operation_get_choice:
662 * @op: a #GMountOperation.
664 * Gets a choice from the mount operation.
666 * Returns: an integer containing an index of the user's choice from
667 * the choice's list, or %0.
670 g_mount_operation_get_choice (GMountOperation *op)
672 g_return_val_if_fail (G_IS_MOUNT_OPERATION (op), 0);
673 return op->priv->choice;
677 * g_mount_operation_set_choice:
678 * @op: a #GMountOperation.
679 * @choice: an integer.
681 * Sets a default choice for the mount operation.
684 g_mount_operation_set_choice (GMountOperation *op,
687 GMountOperationPrivate *priv;
688 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
690 if (priv->choice != choice)
692 priv->choice = choice;
693 g_object_notify (G_OBJECT (op), "choice");
698 * g_mount_operation_reply:
699 * @op: a #GMountOperation
700 * @result: a #GMountOperationResult
702 * Emits the #GMountOperation::reply signal.
705 g_mount_operation_reply (GMountOperation *op,
706 GMountOperationResult result)
708 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
709 g_signal_emit (op, signals[REPLY], 0, result);
712 #define __G_MOUNT_OPERATION_C__
713 #include "gioaliasdef.c"