Merge branch 'issue-699' into 'master'
[glib.git] / gio / gmountoperation.c
blob2a2b4ce5bbb99d78789c3a247e0778445672409f
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
21 #include "config.h"
23 #include <string.h>
25 #include "gmountoperation.h"
26 #include "gioenumtypes.h"
27 #include "glibintl.h"
30 /**
31 * SECTION:gmountoperation
32 * @short_description: Object used for authentication and user interaction
33 * @include: gio/gio.h
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
43 * g_drive_stop().
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.
52 enum {
53 ASK_PASSWORD,
54 ASK_QUESTION,
55 REPLY,
56 ABORTED,
57 SHOW_PROCESSES,
58 SHOW_UNMOUNT_PROGRESS,
59 LAST_SIGNAL
62 static guint signals[LAST_SIGNAL] = { 0 };
64 struct _GMountOperationPrivate {
65 char *password;
66 char *user;
67 char *domain;
68 gboolean anonymous;
69 GPasswordSave password_save;
70 int choice;
73 enum {
74 PROP_0,
75 PROP_USERNAME,
76 PROP_PASSWORD,
77 PROP_ANONYMOUS,
78 PROP_DOMAIN,
79 PROP_PASSWORD_SAVE,
80 PROP_CHOICE
83 G_DEFINE_TYPE_WITH_PRIVATE (GMountOperation, g_mount_operation, G_TYPE_OBJECT)
85 static void
86 g_mount_operation_set_property (GObject *object,
87 guint prop_id,
88 const GValue *value,
89 GParamSpec *pspec)
91 GMountOperation *operation;
93 operation = G_MOUNT_OPERATION (object);
95 switch (prop_id)
97 case PROP_USERNAME:
98 g_mount_operation_set_username (operation,
99 g_value_get_string (value));
100 break;
102 case PROP_PASSWORD:
103 g_mount_operation_set_password (operation,
104 g_value_get_string (value));
105 break;
107 case PROP_ANONYMOUS:
108 g_mount_operation_set_anonymous (operation,
109 g_value_get_boolean (value));
110 break;
112 case PROP_DOMAIN:
113 g_mount_operation_set_domain (operation,
114 g_value_get_string (value));
115 break;
117 case PROP_PASSWORD_SAVE:
118 g_mount_operation_set_password_save (operation,
119 g_value_get_enum (value));
120 break;
122 case PROP_CHOICE:
123 g_mount_operation_set_choice (operation,
124 g_value_get_int (value));
125 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 break;
134 static void
135 g_mount_operation_get_property (GObject *object,
136 guint prop_id,
137 GValue *value,
138 GParamSpec *pspec)
140 GMountOperation *operation;
141 GMountOperationPrivate *priv;
143 operation = G_MOUNT_OPERATION (object);
144 priv = operation->priv;
146 switch (prop_id)
148 case PROP_USERNAME:
149 g_value_set_string (value, priv->user);
150 break;
152 case PROP_PASSWORD:
153 g_value_set_string (value, priv->password);
154 break;
156 case PROP_ANONYMOUS:
157 g_value_set_boolean (value, priv->anonymous);
158 break;
160 case PROP_DOMAIN:
161 g_value_set_string (value, priv->domain);
162 break;
164 case PROP_PASSWORD_SAVE:
165 g_value_set_enum (value, priv->password_save);
166 break;
168 case PROP_CHOICE:
169 g_value_set_int (value, priv->choice);
170 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 break;
179 static void
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);
190 g_free (priv->user);
191 g_free (priv->domain);
193 G_OBJECT_CLASS (g_mount_operation_parent_class)->finalize (object);
196 static gboolean
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;
205 static void
206 ask_password (GMountOperation *op,
207 const char *message,
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,
214 g_object_ref (op),
215 g_object_unref);
218 static void
219 ask_question (GMountOperation *op,
220 const char *message,
221 const char *choices[])
223 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
224 reply_non_handled_in_idle,
225 g_object_ref (op),
226 g_object_unref);
229 static void
230 show_processes (GMountOperation *op,
231 const gchar *message,
232 GArray *processes,
233 const gchar *choices[])
235 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
236 reply_non_handled_in_idle,
237 g_object_ref (op),
238 g_object_unref);
241 static void
242 show_unmount_progress (GMountOperation *op,
243 const gchar *message,
244 gint64 time_left,
245 gint64 bytes_left)
247 /* nothing to do */
250 static void
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),
282 G_SIGNAL_RUN_LAST,
283 G_STRUCT_OFFSET (GMountOperationClass, ask_password),
284 NULL, NULL,
285 NULL,
286 G_TYPE_NONE, 4,
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),
305 G_SIGNAL_RUN_LAST,
306 G_STRUCT_OFFSET (GMountOperationClass, ask_question),
307 NULL, NULL,
308 NULL,
309 G_TYPE_NONE, 2,
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.
319 signals[REPLY] =
320 g_signal_new (I_("reply"),
321 G_TYPE_FROM_CLASS (object_class),
322 G_SIGNAL_RUN_LAST,
323 G_STRUCT_OFFSET (GMountOperationClass, reply),
324 NULL, NULL,
325 g_cclosure_marshal_VOID__ENUM,
326 G_TYPE_NONE, 1,
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.
338 * Since: 2.20
340 signals[ABORTED] =
341 g_signal_new (I_("aborted"),
342 G_TYPE_FROM_CLASS (object_class),
343 G_SIGNAL_RUN_LAST,
344 G_STRUCT_OFFSET (GMountOperationClass, aborted),
345 NULL, NULL,
346 g_cclosure_marshal_VOID__VOID,
347 G_TYPE_NONE, 0);
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
364 * the user made).
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.
370 * Since: 2.22
372 signals[SHOW_PROCESSES] =
373 g_signal_new (I_("show-processes"),
374 G_TYPE_FROM_CLASS (object_class),
375 G_SIGNAL_RUN_LAST,
376 G_STRUCT_OFFSET (GMountOperationClass, show_processes),
377 NULL, NULL,
378 NULL,
379 G_TYPE_NONE, 3,
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
390 * is completed
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.
409 * Since: 2.34
411 signals[SHOW_UNMOUNT_PROGRESS] =
412 g_signal_new (I_("show-unmount-progress"),
413 G_TYPE_FROM_CLASS (object_class),
414 G_SIGNAL_RUN_LAST,
415 G_STRUCT_OFFSET (GMountOperationClass, show_unmount_progress),
416 NULL, NULL, NULL,
417 G_TYPE_NONE, 3,
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,
427 PROP_USERNAME,
428 g_param_spec_string ("username",
429 P_("Username"),
430 P_("The user name"),
431 NULL,
432 G_PARAM_READWRITE|
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,
442 PROP_PASSWORD,
443 g_param_spec_string ("password",
444 P_("Password"),
445 P_("The password"),
446 NULL,
447 G_PARAM_READWRITE|
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,
456 PROP_ANONYMOUS,
457 g_param_spec_boolean ("anonymous",
458 P_("Anonymous"),
459 P_("Whether to use an anonymous user"),
460 FALSE,
461 G_PARAM_READWRITE|
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,
470 PROP_DOMAIN,
471 g_param_spec_string ("domain",
472 P_("Domain"),
473 P_("The domain of the mount operation"),
474 NULL,
475 G_PARAM_READWRITE|
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,
484 PROP_PASSWORD_SAVE,
485 g_param_spec_enum ("password-save",
486 P_("Password save"),
487 P_("How passwords should be saved"),
488 G_TYPE_PASSWORD_SAVE,
489 G_PASSWORD_SAVE_NEVER,
490 G_PARAM_READWRITE|
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,
500 PROP_CHOICE,
501 g_param_spec_int ("choice",
502 P_("Choice"),
503 P_("The users choice"),
504 0, G_MAXINT, 0,
505 G_PARAM_READWRITE|
506 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
509 static void
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.
522 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.
536 const char *
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.
550 void
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.
568 const char *
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.
583 void
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.
602 gboolean
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.
615 **/
616 void
617 g_mount_operation_set_anonymous (GMountOperation *op,
618 gboolean anonymous)
620 GMountOperationPrivate *priv;
621 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
622 priv = op->priv;
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.
639 const char *
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.
652 **/
653 void
654 g_mount_operation_set_domain (GMountOperation *op,
655 const char *domain)
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.
670 **/
672 GPasswordSave
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.
686 **/
687 void
688 g_mount_operation_set_password_save (GMountOperation *op,
689 GPasswordSave save)
691 GMountOperationPrivate *priv;
692 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
693 priv = op->priv;
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.
725 void
726 g_mount_operation_set_choice (GMountOperation *op,
727 int choice)
729 GMountOperationPrivate *priv;
730 g_return_if_fail (G_IS_MOUNT_OPERATION (op));
731 priv = op->priv;
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.
746 void
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);