[Xfce4-commits] <xfce4-mount-plugin:master> Add a boolean/checkbox to use sudo(8) to run the mount/umount commands [wip]

Landry Breuil noreply at xfce.org
Wed Apr 11 14:30:02 CEST 2012


Updating branch refs/heads/master
         to c1ab23fe307ab5b6413a8dc776c768e6846a24b6 (commit)
       from a8bb3443c0838c0c69b376febde8c872d1c761a4 (commit)

commit c1ab23fe307ab5b6413a8dc776c768e6846a24b6
Author: Landry Breuil <landry at xfce.org>
Date:   Tue Apr 10 23:17:40 2012 +0200

    Add a boolean/checkbox to use sudo(8) to run the mount/umount commands [wip]

 panel-plugin/devices.c      |   25 ++++++++++++++++++-------
 panel-plugin/devices.h      |   10 ++++++----
 panel-plugin/mount-plugin.c |   36 +++++++++++++++++++++++++++++++++---
 panel-plugin/mount-plugin.h |    2 ++
 4 files changed, 59 insertions(+), 14 deletions(-)

diff --git a/panel-plugin/devices.c b/panel-plugin/devices.c
index 43f2de2..986a979 100644
--- a/panel-plugin/devices.c
+++ b/panel-plugin/devices.c
@@ -233,22 +233,30 @@ disk_free(t_disk **pdisk)
  * Return exit status of the mount command
  */
 void
-disk_mount (t_disk *pdisk, char *on_mount_cmd, char* mount_command, gboolean eject)
+disk_mount (t_disk *pdisk, char *on_mount_cmd, char* mount_command, gboolean eject, gboolean use_sudo)
 {
     gchar *tmp=NULL, *cmd, *tmp2 = NULL;
     GError *error = NULL;
     gboolean val;
 
-    DBG("disk_mount: eject=%d\n", eject);
+    DBG("disk_mount: eject=%d, use_sudo=%d\n", eject, use_sudo);
 
     if (pdisk != NULL)
     {
         deviceprintf (&tmp, mount_command, pdisk->device);
         mountpointprintf (&tmp2, tmp, pdisk->mount_point);
+        /* re-use tmp */
+        g_free(tmp);
+        tmp = NULL;
+        if (use_sudo)
+            tmp = g_strdup ("sudo sh -c '");
+        else
+            tmp = g_strdup ("sh -c '");
+
         if (eject)
-            cmd = g_strconcat ("sh -c ' eject -t ", pdisk->device, " && ", tmp2, NULL);
+            cmd = g_strconcat (tmp, " eject -t ", pdisk->device, " && ", tmp2, NULL);
         else
-            cmd = g_strconcat ("sh -c ' ", tmp2, NULL);
+            cmd = g_strconcat (tmp, tmp2, NULL);
 
         g_free(tmp);
         g_free(tmp2);
@@ -279,7 +287,7 @@ disk_mount (t_disk *pdisk, char *on_mount_cmd, char* mount_command, gboolean eje
  * Return exit status of the umount command.
  */
 int
-disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean eject)
+disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean eject, gboolean use_sudo)
 {
     int retval = NONE;
     gchar *tmp = NULL, *tmp2 = NULL;
@@ -287,7 +295,7 @@ disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean
     char *cmd;
     GError *error = NULL;
 
-    DBG("disk_umount: eject=%d\n", eject);
+    DBG("disk_umount: eject=%d, use_sudo=%d\n", eject, use_sudo);
 
 
     if (pdisk != NULL)
@@ -295,7 +303,10 @@ disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean
 
         deviceprintf(&tmp, umount_command, pdisk->device);
         mountpointprintf(&tmp2, tmp, pdisk->mount_point);
-        cmd = g_strconcat ("sh -c '", tmp2, NULL);
+        if (use_sudo)
+            cmd = g_strconcat ("sudo sh -c '", tmp2, NULL);
+        else
+            cmd = g_strconcat ("sh -c '", tmp2, NULL);
 
         /* re-use tmp */
         g_free(tmp);
diff --git a/panel-plugin/devices.h b/panel-plugin/devices.h
index 0d97aa9..6bf95ec 100644
--- a/panel-plugin/devices.h
+++ b/panel-plugin/devices.h
@@ -93,20 +93,22 @@ char * get_size_human_readable(float size);
  * @param on_mount_cmd    Command to execute after successfully mounting the device
  * @param mount_command    Command to use for mounting the device, still containing placeholders like \%d and \%m.
  * @param eject            Whether to inject the device before mounting.
+ * @param use_sudo         Whether to use sudo to run the mount command.
  * @return                Exit status of the mount command
  */
-void disk_mount (t_disk *pdisk, char *on_mount_cmd, char* mount_command, gboolean eject);
+void disk_mount (t_disk *pdisk, char *on_mount_cmd, char* mount_command, gboolean eject, gboolean use_sudo);
 
 
 /**
  * Unmount a t_disk.
- * @param pdisk            Disk to mount
+ * @param pdisk            Disk to umount
  * @param umount_command    Command to use for unmounting the device, still containing placeholders like \%d and \%m.
  * @param synchronous    Whether to execute the command synchronously to the program itself thus waiting for the output
  * @param eject            Whether to eject the device after unmounting.
- * @return                 Exit status of the mount command
+ * @param use_sudo         Whether to use sudo to run the umount command.
+ * @return                 Exit status of the umount command
  */
-int disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean eject);
+int disk_umount (t_disk *pdisk, char* umount_command, gboolean synchronous, gboolean eject, gboolean use_sudo);
 
 
 /**
diff --git a/panel-plugin/mount-plugin.c b/panel-plugin/mount-plugin.c
index ab5ca5f..05c9ae0 100644
--- a/panel-plugin/mount-plugin.c
+++ b/panel-plugin/mount-plugin.c
@@ -58,7 +58,7 @@ on_activate_disk_display (GtkWidget *widget, t_disk * disk)
     if (disk != NULL) {
         if (disk->mount_info != NULL) { /* disk is mounted */
             int result = disk_umount (disk, mt->umount_command,
-                                      mt->message_dialog, eject);
+                                      mt->message_dialog, eject, mt->use_sudo);
 
             if (mt->message_dialog && (!eject || result!=NONE) ) { /* popup dialog */
 
@@ -84,7 +84,7 @@ on_activate_disk_display (GtkWidget *widget, t_disk * disk)
             }
         }
         else { /* disk is not mounted */
-            disk_mount (disk, mt->on_mount_cmd, mt->mount_command, eject);
+            disk_mount (disk, mt->on_mount_cmd, mt->mount_command, eject, mt->use_sudo);
         }
     }
 
@@ -364,6 +364,8 @@ mounter_data_new (t_mounter *mt)
 
     mt->exclude_devicenames = FALSE;
 
+    mt->use_sudo = FALSE;
+
     TRACE ("leaves mounter_data_new");
 
     return ;
@@ -374,7 +376,7 @@ static void
 mounter_refresh (t_mounter * mt)
 {
     gchar *mount, *umount, *icon, *excl_filesystems;
-    gboolean msg_dlg, incl_NFSs, excl_FSs, excl_DNs, eject;
+    gboolean msg_dlg, incl_NFSs, excl_FSs, excl_DNs, use_sudo, eject;
     TRACE ("enters mounter_refresh");
 
     mounter_data_free (mt);
@@ -387,6 +389,7 @@ mounter_refresh (t_mounter * mt)
     incl_NFSs = mt->include_NFSs;
     excl_FSs = mt->exclude_FSs;
     excl_DNs = mt->exclude_devicenames;
+    use_sudo = mt->use_sudo;
     eject = mt->eject_drives;
 
     mounter_data_new (mt);
@@ -399,6 +402,7 @@ mounter_refresh (t_mounter * mt)
     mt->include_NFSs = incl_NFSs;
     mt->exclude_FSs = excl_FSs;
     mt->exclude_devicenames = excl_DNs;
+    mt->use_sudo = use_sudo;
     mt->eject_drives = eject;
 
     TRACE ("leaves mounter_refresh");
@@ -477,6 +481,9 @@ mounter_read_config (XfcePanelPlugin *plugin, t_mounter *mt)
     if ( (value = xfce_rc_read_entry(rc, "eject_drives", NULL)) )
         mt->eject_drives= atoi (value);
 
+    if ( (value = xfce_rc_read_entry(rc, "use_sudo", NULL)) )
+        mt->use_sudo= atoi (value);
+
     xfce_rc_close (rc);
 
     TRACE ("leaves read_config");
@@ -528,6 +535,9 @@ mounter_write_config (XfcePanelPlugin *plugin, t_mounter *mt)
     if ( mt->eject_drives==1 )
         xfce_rc_write_entry (rc, "eject_drives", "1");
 
+    if ( mt->use_sudo==1 )
+        xfce_rc_write_entry (rc, "use_sudo", "1");
+
     xfce_rc_write_entry (rc, "icon", mt->icon);
 
     xfce_rc_close (rc);
@@ -632,6 +642,9 @@ mounter_apply_options (t_mounter_dialog *md)
     mt->eject_drives = gtk_toggle_button_get_active
             (GTK_TOGGLE_BUTTON(md->show_eject_drives));
 
+    mt->use_sudo = gtk_toggle_button_get_active
+            (GTK_TOGGLE_BUTTON(md->show_use_sudo));
+
     mt->exclude_FSs = gtk_toggle_button_get_active
             (GTK_TOGGLE_BUTTON(md->show_exclude_FSs));
 
@@ -831,6 +844,23 @@ mounter_create_options (XfcePanelPlugin *plugin, t_mounter *mt)
     gtk_container_border_width (GTK_CONTAINER(_vbox), BORDER);
     gtk_widget_show (_vbox);
 
+        /* use sudo checkbox */
+    _eventbox = gtk_event_box_new ();
+    gtk_box_pack_start (GTK_BOX (_vbox), GTK_WIDGET(_eventbox),
+                        FALSE, FALSE, 0);
+    gtk_widget_show (_eventbox);
+    gtk_tooltips_set_tip ( GTK_TOOLTIPS(tip), _eventbox,
+        _("Activate this option to use sudo(8) to run mount/umount commands. Needs to be configured without password."),
+        NULL );
+
+    md->show_use_sudo = gtk_check_button_new_with_mnemonic (
+                                _("Use _sudo") );
+
+    gtk_widget_show (md->show_use_sudo);
+    gtk_container_add (GTK_CONTAINER (_eventbox), md->show_use_sudo );
+    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(md->show_use_sudo),
+                                mt->use_sudo);
+
         /* After-mount command */
     _eventbox = gtk_event_box_new ();
     gtk_box_pack_start (GTK_BOX (_vbox), GTK_WIDGET(_eventbox),
diff --git a/panel-plugin/mount-plugin.h b/panel-plugin/mount-plugin.h
index 54f7181..8371a54 100644
--- a/panel-plugin/mount-plugin.h
+++ b/panel-plugin/mount-plugin.h
@@ -63,6 +63,7 @@ typedef struct
     gboolean exclude_FSs;
     gboolean exclude_devicenames;
     gboolean eject_drives;
+    gboolean use_sudo;
     gboolean showed_fstab_dialog;
     GtkWidget *button;
     GtkWidget *image;
@@ -105,6 +106,7 @@ typedef struct
     GtkWidget *show_include_NFSs;
     GtkWidget *show_exclude_FSs;
     GtkWidget *show_eject_drives;
+    GtkWidget *show_use_sudo;
     GtkWidget *show_exclude_devicenames;
     GtkWidget *string_excluded_filesystems;
 }


More information about the Xfce4-commits mailing list