From 496c7467da5ead48762da6ab05f86b1ffbf58ff6 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Thu, 5 Aug 1999 20:29:17 +0000 Subject: [PATCH] r46: Mount now reports an error is there is nothing to do. Started rewriting delete code to use an action window. --- ROX-Filer/src/action.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++ ROX-Filer/src/action.h | 15 ++++++ ROX-Filer/src/menu.c | 24 +++++---- 3 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 ROX-Filer/src/action.c create mode 100644 ROX-Filer/src/action.h diff --git a/ROX-Filer/src/action.c b/ROX-Filer/src/action.c new file mode 100644 index 00000000..9ba0048a --- /dev/null +++ b/ROX-Filer/src/action.c @@ -0,0 +1,138 @@ +/* vi: set cindent: + * $Id$ + * + * ROX-Filer, filer for the ROX desktop project + * By Thomas Leonard, . + */ + +/* action.c - code for handling the filer action windows. + * These routines generally fork() and talk to us via pipes. + */ + +#include +#include +#include + +#include "gui_support.h" +#include "filer.h" + +#define BUFLEN 40 + +typedef struct _GUIside GUIside; +typedef void ActionChild(FilerWindow *filer_window, + int write_to_parent, int read_from_parent); + +struct _GUIside +{ + int from_child; + int to_child; + int input_tag; + GtkWidget *log; +}; + +/* Create two pipes, fork() a child and return a pointer to a GUIside struct + * (NULL on failure). The child calls func(). + */ +static GUIside *start_action(FilerWindow *filer_window, ActionChild *func) +{ + int filedes[4]; /* 0 and 2 are for reading */ + GUIside *retval; + + if (pipe(filedes)) + { + report_error("ROX-Filer", g_strerror(errno)); + return NULL; + } + + if (pipe(filedes + 2)) + { + close(filedes[0]); + close(filedes[1]); + report_error("ROX-Filer", g_strerror(errno)); + return NULL; + } + + switch (fork()) + { + case -1: + report_error("ROX-Filer", g_strerror(errno)); + return NULL; + case 0: + /* We are the child */ + close(filedes[0]); + close(filedes[3]); + func(filer_window, filedes[1], filedes[2]); + _exit(0); + } + + /* We are the parent */ + close(filedes[1]); + close(filedes[2]); + retval = g_malloc(sizeof(GUIside)); + retval->from_child = filedes[0]; + retval->to_child = filedes[3]; + retval->log = NULL; + + return retval; +} + +static void delete_cb(FilerWindow *filer_window, int to_parent, int from_parent) +{ + write(to_parent, "I am the child!!\n", 17); + sleep(2); +} + +static void got_delete_data(gpointer data, + gint source, + GdkInputCondition condition) +{ + char buf[BUFLEN]; + ssize_t len; + GUIside *gui_side = (GUIside *) data; + GtkWidget *log = gui_side->log; + + len = read(source, buf, BUFLEN); + if (len > 0) + gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len); + else + { + gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, + "Pipe closed by child.\n", -1); + close(gui_side->to_child); + close(gui_side->from_child); + gdk_input_remove(gui_side->input_tag); + } +} + +void action_delete(FilerWindow *filer_window) +{ + GtkWidget *window, *log; + GUIside *gui_side; + Collection *collection; + + collection = window_with_focus->collection; + + if (collection->number_selected < 1) + { + report_error("ROX-Filer", "You need to select some items " + "first"); + return; + } + + gui_side = start_action(filer_window, delete_cb); + if (!gui_side) + return; + + window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + + log = gtk_text_new(NULL, NULL); + gtk_container_add(GTK_CONTAINER(window), log); + gui_side->log = log; + + gui_side->input_tag = gdk_input_add(gui_side->from_child, + GDK_INPUT_READ, + got_delete_data, gui_side); + + gtk_widget_show_all(window); +} + diff --git a/ROX-Filer/src/action.h b/ROX-Filer/src/action.h new file mode 100644 index 00000000..afef26b8 --- /dev/null +++ b/ROX-Filer/src/action.h @@ -0,0 +1,15 @@ +/* vi: set cindent: + * $Id$ + * + * ROX-Filer, filer for the ROX desktop project + * By Thomas Leonard, . + */ + +#ifndef _ACTION_H +#define _ACTION_H + +#include "filer.h" + +void action_delete(FilerWindow *filer_window); + +#endif /* _ACTION_H */ diff --git a/ROX-Filer/src/menu.c b/ROX-Filer/src/menu.c index 60897fd8..d35562c9 100644 --- a/ROX-Filer/src/menu.c +++ b/ROX-Filer/src/menu.c @@ -19,6 +19,7 @@ #include #include "apps.h" +#include "action.h" #include "filer.h" #include "type.h" #include "support.h" @@ -400,6 +401,13 @@ static void refresh(gpointer data, guint action, GtkWidget *widget) static void delete(gpointer data, guint action, GtkWidget *widget) { + g_return_if_fail(window_with_focus != NULL); + + action_delete(window_with_focus); +} + +void bin(gpointer data, guint action, GtkWidget *widget) +{ const char *start_args[] = {"xterm", "-wf", "-e", "rm", "-vir"}; int argc = sizeof(start_args) / sizeof(char *); @@ -411,15 +419,6 @@ static void delete(gpointer data, guint action, GtkWidget *widget) g_return_if_fail(window_with_focus != NULL); - collection = window_with_focus->collection; - - if (collection->number_selected < 1) - { - report_error("ROX-Filer", "You need to select some items " - "first"); - return; - } - argv = g_malloc(sizeof(start_args) + sizeof(char *) * (collection->number_selected + 1)); memcpy(argv, start_args, sizeof(start_args)); @@ -555,6 +554,7 @@ static void mount(gpointer data, guint action, GtkWidget *widget) int i; Collection *collection; char *error = NULL; + int count = 0; g_return_if_fail(window_with_focus != NULL); @@ -569,6 +569,7 @@ static void mount(gpointer data, guint action, GtkWidget *widget) char *argv[] = {"mount", NULL, NULL}; int child; + count++; if (item->flags & ITEM_FLAG_MOUNTED) argv[0] = "umount"; argv[1] = make_path(window_with_focus->path, @@ -580,7 +581,10 @@ static void mount(gpointer data, guint action, GtkWidget *widget) error = "Failed to run mount/umount"; } } - scan_dir(window_with_focus); + if (count) + scan_dir(window_with_focus); + else if (!error) + error = "You must select some mount points first!"; if (error) report_error("ROX-Filer", error); -- 2.11.4.GIT