]> git.tdb.fi Git - pmount-gui.git/blobdiff - main.c
Split the mounting logic out of row_activated
[pmount-gui.git] / main.c
diff --git a/main.c b/main.c
index 49239cf9af722127c3355b57f42c87ef2667173c..9e02fcab676fe849170e52789d483d49e8023e76 100644 (file)
--- a/main.c
+++ b/main.c
@@ -29,6 +29,7 @@ typedef struct sDevice
 } Device;
 
 int verbosity = 0;
+char *post_mount_command = NULL;
 
 /**
 Parses a string of the form name=value and places the components in a Property
@@ -613,29 +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 = *(int *)user_data;
-       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);
+       out_buf[0] = 0;
 
        pipe(pipe_fd);
 
@@ -662,7 +653,7 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
                _exit(1);
        }
        else if(pid<0)
-               return;
+               return -1;
 
        /* Parent process */
 
@@ -680,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;
@@ -692,10 +683,11 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
                }
        }
 
+       close(pipe_fd[0]);
        if(pid)
                waitpid(pid, &status, 0);
 
-       buf[pos] = 0;
+       out_buf[pos] = 0;
 
        if(verbosity>=1)
        {
@@ -713,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;
@@ -726,7 +746,28 @@ void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *colu
 
        gtk_main_quit();
 
+       if(post_mount_command && device->mounted)
+       {
+               char workdir[256];
+               int len;
+               len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label);
+               if(len<(int)sizeof(workdir))
+               {
+                       if(verbosity>=1)
+                               printf("Running %s in %s\n", post_mount_command, workdir);
+
+                       pid = fork();
+                       if(pid==0)
+                       {
+                               chdir(workdir);
+                               execlp(post_mount_command, post_mount_command, NULL);
+                               _exit(1);
+                       }
+               }
+       }
+
        (void)column;
+       (void)user_data;
 }
 
 /**
@@ -767,6 +808,18 @@ gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
        return FALSE;
 }
 
+void show_help(void)
+{
+       printf("pmount-gui\n"
+               "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
+               "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
+               "Options:\n"
+               "  -v  Increase verbosity\n"
+               "  -u  Unmount a device (default is mount)\n"
+               "  -r  Run a command after mounting\n"
+               "  -h  Display this help\n");
+}
+
 int main(int argc, char **argv)
 {
        GtkWidget *window;
@@ -786,7 +839,7 @@ int main(int argc, char **argv)
 
        gtk_init(&argc, &argv);
 
-       while((opt = getopt(argc, argv, "vu"))!=-1) switch(opt)
+       while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
        {
        case 'v':
                ++verbosity;
@@ -794,6 +847,12 @@ int main(int argc, char **argv)
        case 'u':
                umount = 1;
                break;
+       case 'r':
+               post_mount_command = optarg;
+               break;
+       case 'h':
+               show_help();
+               return 0;
        }
 
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@@ -810,7 +869,7 @@ int main(int argc, char **argv)
 
        list = gtk_tree_view_new();
        gtk_container_add(GTK_CONTAINER(viewport), list);
-       g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), &umount);
+       g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), NULL);
 
        store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
        gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));