diff options
| author | Tomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com> | 2009-02-03 17:43:50 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com> | 2009-02-03 17:43:50 +0100 |
| commit | b7a646451fe20eabbdfe076d13117703d7d8b9c3 (patch) | |
| tree | 9608903b83a56f73c7e7856228c3a283a670d789 /src | |
| parent | 068e3eb375dca26cc12adc90200105c347b4a30e (diff) | |
| download | nautilus-gdu-b7a646451fe20eabbdfe076d13117703d7d8b9c3.tar.gz nautilus-gdu-b7a646451fe20eabbdfe076d13117703d7d8b9c3.tar.xz nautilus-gdu-b7a646451fe20eabbdfe076d13117703d7d8b9c3.zip | |
PolicyKit integration
Diffstat (limited to 'src')
| -rw-r--r-- | src/nautilus-gdu-window.c | 224 |
1 files changed, 187 insertions, 37 deletions
diff --git a/src/nautilus-gdu-window.c b/src/nautilus-gdu-window.c index a65ca4f..0e193de 100644 --- a/src/nautilus-gdu-window.c +++ b/src/nautilus-gdu-window.c @@ -38,6 +38,8 @@ #include <gdu/gdu.h> #include <gdu-gtk/gdu-gtk.h> +#include <polkit-gnome/polkit-gnome.h> + @@ -65,6 +67,13 @@ typedef struct { GduPool *pool; gboolean job_running; + + PolKitAction *pk_unmount_action; + PolKitGnomeAction *unmount_action; + PolKitAction *pk_format_action; + PolKitGnomeAction *format_action; + PolKitAction *pk_part_modify_action; + PolKitGnomeAction *part_modify_action; } FormatDialogPrivate; typedef struct { @@ -77,9 +86,16 @@ typedef struct { GduDevice *device; gboolean take_ownership; char *secure_erase; + const char *recommended_part_type; - gulong job_handler_id; guint job_progress_pulse_timer_id; + gulong job_handler_id; + gulong pk_unmount_action_handler_id; + gulong pk_unmount_auth_end_handler_id; + gulong pk_format_action_handler_id; + gulong pk_format_auth_end_handler_id; + gulong pk_part_modify_action_handler_id; + gulong pk_part_modify_auth_end_handler_id; } FormatProcessData; @@ -253,6 +269,13 @@ presentable_job_changed (GduPresentable *presentable, gpointer user_data) } } +static void unmount_auth_end_callback (PolKitGnomeAction *action, gboolean gained_privilege, gpointer user_data); +static void unmount_action_callback (GtkAction *action, gpointer user_data); +static void format_auth_end_callback (PolKitGnomeAction *action, gboolean gained_privilege, gpointer user_data); +static void format_action_callback (GtkAction *action, gpointer user_data); +static void part_modify_auth_end_callback (PolKitGnomeAction *action, gboolean gained_privilege, gpointer user_data); +static void part_modify_action_callback (GtkAction *action, gpointer user_data); + static void update_ui_progress (FormatDialogPrivate *priv, FormatProcessData *data, @@ -270,6 +293,12 @@ update_ui_progress (FormatDialogPrivate *priv, gtk_widget_show_all (priv->progress_bar_box); if (data) { data->job_handler_id = g_signal_connect (priv->presentable, "job-changed", G_CALLBACK (presentable_job_changed), data); + data->pk_unmount_auth_end_handler_id = g_signal_connect (priv->unmount_action, "auth-end", G_CALLBACK (unmount_auth_end_callback), data); + data->pk_unmount_action_handler_id = g_signal_connect (priv->unmount_action, "activate", G_CALLBACK (unmount_action_callback), data); + data->pk_format_auth_end_handler_id = g_signal_connect (priv->format_action, "auth-end", G_CALLBACK (format_auth_end_callback), data); + data->pk_format_action_handler_id = g_signal_connect (priv->format_action, "activate", G_CALLBACK (format_action_callback), data); + data->pk_part_modify_auth_end_handler_id = g_signal_connect (priv->part_modify_action, "auth-end", G_CALLBACK (part_modify_auth_end_callback), data); + data->pk_part_modify_action_handler_id = g_signal_connect (priv->part_modify_action, "activate", G_CALLBACK (part_modify_action_callback), data); } } else @@ -277,6 +306,18 @@ update_ui_progress (FormatDialogPrivate *priv, if (data) { if (data->job_handler_id) g_signal_handler_disconnect (priv->presentable, data->job_handler_id); + if (data->pk_unmount_auth_end_handler_id) + g_signal_handler_disconnect (priv->unmount_action, data->pk_unmount_auth_end_handler_id); + if (data->pk_unmount_action_handler_id) + g_signal_handler_disconnect (priv->unmount_action, data->pk_unmount_action_handler_id); + if (data->pk_format_auth_end_handler_id) + g_signal_handler_disconnect (priv->format_action, data->pk_format_auth_end_handler_id); + if (data->pk_format_action_handler_id) + g_signal_handler_disconnect (priv->format_action, data->pk_format_action_handler_id); + if (data->pk_part_modify_auth_end_handler_id) + g_signal_handler_disconnect (priv->part_modify_action, data->pk_part_modify_auth_end_handler_id); + if (data->pk_part_modify_action_handler_id) + g_signal_handler_disconnect (priv->part_modify_action, data->pk_part_modify_action_handler_id); if (data->job_progress_pulse_timer_id > 0) { g_source_remove (data->job_progress_pulse_timer_id); data->job_progress_pulse_timer_id = 0; @@ -684,6 +725,8 @@ free_format_action_data (FormatProcessData *data) } } +/* ---------------------------------------------------------------------------------------------------- */ + static void modify_partition_completed (GduDevice *device, GError *error, @@ -710,13 +753,45 @@ modify_partition_completed (GduDevice *device, } static void +part_modify_action_callback (GtkAction *action, gpointer user_data) +{ + FormatProcessData *data = user_data; + + g_return_if_fail (data != NULL); + g_print ("part_modify_action_callback\n"); + + /* DK is buggy, passing a label string causes the operation to fail */ + gdu_device_op_partition_modify (data->device, data->recommended_part_type, NULL /* data->fslabel */, NULL, modify_partition_completed, data); +} + +static void +part_modify_auth_end_callback (PolKitGnomeAction *action, + gboolean gained_privilege, + gpointer user_data) +{ + FormatProcessData *data = user_data; + + g_return_if_fail (data != NULL); + g_print ("part_modify_auth_end_callback\n"); + + if (! gained_privilege) { + /* cancel the whole operation */ + free_format_action_data (data); + } + else { + /* positive reply should be handled by part_modify_action_callback */ + } +} + +/* ---------------------------------------------------------------------------------------------------- */ + +static void format_action_completed (GduDevice *device, GError *error, gpointer user_data) { FormatProcessData *data = user_data; const char *part_type; - const char *recommended_part_type; const char *scheme; g_print ("format_action_completed\n"); @@ -747,15 +822,14 @@ format_action_completed (GduDevice *device, /* TODO: extend to other partition types once this feature is working in DK */ if (strcmp (scheme, "mbr") == 0) { part_type = gdu_device_partition_get_type (device); - recommended_part_type = get_part_type_for_fs (data->fstype); - g_print ("format_action_completed: part_type = %s, recommended_part_type = %s\n", part_type, recommended_part_type); + data->recommended_part_type = get_part_type_for_fs (data->fstype); + g_print ("format_action_completed: part_type = %s, recommended_part_type = %s\n", part_type, data->recommended_part_type); /* Change partition type if necessary */ - if (strcasecmp (part_type, recommended_part_type) != 0 && - ! (mbr_part_is_linux_type (part_type) && mbr_part_is_linux_type (recommended_part_type))) + if (strcasecmp (part_type, data->recommended_part_type) != 0 && + ! (mbr_part_is_linux_type (part_type) && mbr_part_is_linux_type (data->recommended_part_type))) { - g_print ("changing part type to %s, device = %s\n", recommended_part_type, gdu_device_get_device_file (device)); - /* DK is buggy, passing a label string causes the operation to fail */ - gdu_device_op_partition_modify (device, recommended_part_type, NULL /* data->fslabel */, NULL, modify_partition_completed, data); + g_print ("changing part type to %s, device = %s\n", data->recommended_part_type, gdu_device_get_device_file (device)); + gtk_action_activate (GTK_ACTION (data->priv->part_modify_action)); return; /* don't change the UI yet */ } } @@ -766,7 +840,62 @@ format_action_completed (GduDevice *device, free_format_action_data (data); } -static gboolean unmount_show_busy (gpointer user_data); +static void +format_action_callback (GtkAction *action, gpointer user_data) +{ + FormatProcessData *data = user_data; + + g_return_if_fail (data != NULL); + g_print ("format_action_callback\n"); + + gdu_device_op_filesystem_create (data->device, + data->fstype, + data->fslabel, + data->secure_erase, + data->encrypt_passphrase, + data->take_ownership, + format_action_completed, + data); +} + +static void +format_auth_end_callback (PolKitGnomeAction *action, + gboolean gained_privilege, + gpointer user_data) +{ + FormatProcessData *data = user_data; + + g_return_if_fail (data != NULL); + g_print ("format_auth_end_callback\n"); + + if (! gained_privilege) { + /* cancel the whole operation */ + free_format_action_data (data); + } + else { + /* positive reply should be handled by format_action_callback */ + } +} + + +/* ---------------------------------------------------------------------------------------------------- */ + +static gboolean +unmount_show_busy (gpointer user_data) +{ + FormatProcessData *data = user_data; + + if (gdu_util_dialog_show_filesystem_busy (GTK_WIDGET (data->priv->dialog), data->priv->presentable)) { + /* user managed to kill all applications; try again */ + gtk_action_activate (GTK_ACTION (data->priv->unmount_action)); + } + else { + /* cancel the whole operation */ + free_format_action_data (data); + } + + return FALSE; +} static void unmount_action_completed (GduDevice *device, @@ -787,11 +916,11 @@ unmount_action_completed (GduDevice *device, } else { - free_format_action_data (data); nautilus_gdu_show_error (GTK_WIDGET (data->priv->dialog), data->priv->presentable, error, _("Error unmounting device")); + free_format_action_data (data); } g_error_free (error); } @@ -799,34 +928,41 @@ unmount_action_completed (GduDevice *device, { /* TODO: maybe perform a refresh? */ g_print (" formatting...\n"); - gdu_device_op_filesystem_create (data->device, - data->fstype, - data->fslabel, - data->secure_erase, - data->encrypt_passphrase, - data->take_ownership, - format_action_completed, - data); + gtk_action_activate (GTK_ACTION (data->priv->format_action)); } } -static gboolean -unmount_show_busy (gpointer user_data) +static void +unmount_action_callback (GtkAction *action, gpointer user_data) { FormatProcessData *data = user_data; - if (gdu_util_dialog_show_filesystem_busy (GTK_WIDGET (data->priv->dialog), data->priv->presentable)) { - /* user managed to kill all applications; try again */ - gdu_device_op_filesystem_unmount (data->device, unmount_action_completed, data); - } - else { + g_return_if_fail (data != NULL); + g_print ("unmount_action_callback\n"); + + gdu_device_op_filesystem_unmount (data->device, unmount_action_completed, data); +} + +static void +unmount_auth_end_callback (PolKitGnomeAction *action, + gboolean gained_privilege, + gpointer user_data) +{ + FormatProcessData *data = user_data; + + g_return_if_fail (data != NULL); + g_print ("unmount_auth_end_callback\n"); + + if (! gained_privilege) { /* cancel the whole operation */ free_format_action_data (data); } - - return FALSE; + else { + /* positive reply should be handled by unmount_action_callback */ + } } +/* ---------------------------------------------------------------------------------------------------- */ /* taken from palimpsest/gdu-section-unrecognized.c */ static void @@ -941,16 +1077,9 @@ do_format (FormatDialogPrivate *priv) /* unmount the device before formatting */ if (gdu_device_is_mounted (data->device)) { - gdu_device_op_filesystem_unmount (data->device, unmount_action_completed, data); + gtk_action_activate (GTK_ACTION (priv->unmount_action)); } else { - gdu_device_op_filesystem_create (data->device, - data->fstype, - data->fslabel, - data->secure_erase, - data->encrypt_passphrase, - data->take_ownership, - format_action_completed, - data); + gtk_action_activate (GTK_ACTION (priv->format_action)); } out: @@ -973,6 +1102,15 @@ nautilus_gdu_destroy (FormatDialogPrivate *priv) /* disconnect our handlers, since the presentable refence counter doesn't really need to be zero */ g_signal_handlers_disconnect_matched (priv->presentable, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, priv); + /* destroy PolicyKit actions */ + polkit_action_unref (priv->pk_unmount_action); + g_object_unref (priv->unmount_action); + polkit_action_unref (priv->pk_format_action); + g_object_unref (priv->format_action); + polkit_action_unref (priv->pk_part_modify_action); + g_object_unref (priv->part_modify_action); + + /* destroy the dialog and internal struct */ gtk_widget_destroy (GTK_WIDGET (priv->dialog)); g_object_unref (priv->pool); g_print ("before unreffing presentable, count = %d [%p]\n", ((GObject*)priv->presentable)->ref_count, priv->presentable); @@ -1247,6 +1385,18 @@ nautilus_gdu_spawn_dialog (GduPresentable *presentable) gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0); + /* set up PolicyKit actions */ + priv->pk_unmount_action = polkit_action_new (); + polkit_action_set_action_id (priv->pk_unmount_action, "org.freedesktop.devicekit.disks.filesystem-unmount-others"); + priv->unmount_action = polkit_gnome_action_new_default ("unmount", priv->pk_unmount_action, NULL, NULL); + priv->pk_format_action = polkit_action_new (); + polkit_action_set_action_id (priv->pk_format_action, "org.freedesktop.devicekit.disks.change"); + priv->format_action = polkit_gnome_action_new_default ("format", priv->pk_format_action, NULL, NULL); + priv->pk_part_modify_action = polkit_action_new (); + /* action_id is the same as for format, but sometimes authentication is one shot */ + polkit_action_set_action_id (priv->pk_part_modify_action, "org.freedesktop.devicekit.disks.change"); + priv->part_modify_action = polkit_gnome_action_new_default ("part_modify", priv->pk_part_modify_action, NULL, NULL); + // gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE); |
