Split the mounting logic out of row_activated
authorMikko Rasa <tdb@tdb.fi>
Sat, 29 Aug 2015 16:03:12 +0000 (19:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 29 Aug 2015 16:05:04 +0000 (19:05 +0300)
main.c

diff --git a/main.c b/main.c
index 26dbca32f208a84be2832d8f7c61a0191cc4e886..9e02fcab676fe849170e52789d483d49e8023e76 100644 (file)
--- a/main.c
+++ b/main.c
@@ -614,30 +614,19 @@ void free_devices(Device *devices)
 }
 
 /**
-Callback for activating a row in the device list.  Mounts or unmounts the
-device depending on operating mode.
+Mounts a device if it was not mounted, or unmounts if it was.
 */
-void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+int toggle_device(Device *device, char *out_buf, int out_size)
 {
-       GtkTreeModel *model;
-       GtkTreeIter iter;
-       int umount;
-       char buf[1024];
+       int umount = !!device->mounted;
        int pos = 0;
        int status = 0;
        fd_set fds;
        struct timeval timeout;
-       Device *device;
        int pid;
        int pipe_fd[2];
 
-       model = gtk_tree_view_get_model(list);
-
-       if(!gtk_tree_model_get_iter(model, &iter, path))
-               return;
-
-       gtk_tree_model_get(model, &iter, 1, &device, -1);
-       umount = !!device->mounted;
+       out_buf[0] = 0;
 
        pipe(pipe_fd);
 
@@ -664,7 +653,7 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
                _exit(1);
        }
        else if(pid<0)
-               return;
+               return -1;
 
        /* Parent process */
 
@@ -682,7 +671,7 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
                {
                        int len;
 
-                       len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
+                       len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
                        if(len<=0)
                                break;
                        pos += len;
@@ -698,7 +687,7 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
        if(pid)
                waitpid(pid, &status, 0);
 
-       buf[pos] = 0;
+       out_buf[pos] = 0;
 
        if(verbosity>=1)
        {
@@ -716,12 +705,40 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
        }
 
        if(!WIFEXITED(status) || WEXITSTATUS(status))
+               return -1;
+
+       device->mounted = !device->mounted;
+
+       return 0;
+}
+
+/**
+Callback for activating a row in the device list.  Mounts or unmounts the
+device depending on operating mode.
+*/
+void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+{
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       Device *device;
+       int ret;
+       char output[1024];
+       int pid;
+
+       model = gtk_tree_view_get_model(list);
+
+       if(!gtk_tree_model_get_iter(model, &iter, path))
+               return;
+
+       gtk_tree_model_get(model, &iter, 1, &device, -1);
+       ret = toggle_device(device, output, sizeof(output));
+       if(ret)
        {
                GtkWidget *dialog;
 
                /* Pmount terminated with nonzero status or a signal.  Display an
                error to the user. */
-               dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
+               dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", output);
                g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
                gtk_widget_show_all(dialog);
                return;
@@ -729,7 +746,7 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
 
        gtk_main_quit();
 
-       if(post_mount_command && !umount)
+       if(post_mount_command && device->mounted)
        {
                char workdir[256];
                int len;