r201: Allowed dragging to a subdirectory to save into it.
[rox-filer.git] / ROX-Filer / src / main.c
blobe688a46e86c9e187df0002ec892a55550a44bc68
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
32 #ifdef HAVE_GETOPT_LONG
33 # include <getopt.h>
34 #endif
36 #include <gtk/gtk.h>
37 #include "collection.h"
39 #include "main.h"
40 #include "support.h"
41 #include "gui_support.h"
42 #include "filer.h"
43 #include "mount.h"
44 #include "menu.h"
45 #include "dnd.h"
46 #include "options.h"
47 #include "choices.h"
48 #include "savebox.h"
49 #include "type.h"
50 #include "pixmaps.h"
51 #include "dir.h"
52 #include "action.h"
54 int number_of_windows = 0; /* Quit when this reaches 0 again... */
55 int to_error_log = -1; /* Write here to log errors */
57 uid_t euid;
58 gid_t egid;
59 int ngroups; /* Number of supplemental groups */
60 gid_t *supplemental_groups = NULL;
62 #define VERSION "ROX-Filer 0.1.16\n" \
63 "Copyright (C) 1999 Thomas Leonard.\n" \
64 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
65 "to the extent permitted by law.\n" \
66 "You may redistribute copies of ROX-Filer\n" \
67 "under the terms of the GNU General Public License.\n" \
68 "For more information about these matters, " \
69 "see the file named COPYING.\n"
71 #ifdef HAVE_GETOPT_LONG
72 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
73 # define SHORT_ONLY_WARNING ""
74 #else
75 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
76 # define SHORT_ONLY_WARNING \
77 "NOTE: Your system does not support long options - \n" \
78 "you must use the short versions instead.\n\n"
79 #endif
81 #define SHORT_OPS "t:b:ohv"
83 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
84 "Open filer windows showing each directory listed, or $HOME \n" \
85 "if no directories are given.\n\n" \
86 " -h, --help display this help and exit\n" \
87 " -v, --version display the version information and exit\n" \
88 " -t, --top [DIR] open DIR as a top-edge panel\n" \
89 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
90 " -o, --override override window manager control of panels\n" \
91 "\n" SHORT_ONLY_WARNING \
92 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
94 #ifdef HAVE_GETOPT_LONG
95 static struct option long_opts[] =
97 {"top", 1, NULL, 't'},
98 {"bottom", 1, NULL, 'b'},
99 {"override", 0, NULL, 'o'},
100 {"help", 0, NULL, 'h'},
101 {"version", 0, NULL, 'v'},
102 {NULL, 0, NULL, 0},
104 #endif
106 /* Take control of panels away from WM? */
107 gboolean override_redirect = FALSE;
109 static void child_died(int signum)
111 int status;
112 int child;
114 /* Find out which children exited and allow them to die */
117 child = waitpid(-1, &status, WNOHANG);
119 if (child == 0 || child == -1)
120 return;
122 /* fprintf(stderr, "Child %d exited\n", child); */
124 } while (1);
127 #define BUFLEN 40
128 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
130 char buf[BUFLEN];
131 static GtkWidget *log = NULL;
132 static GtkWidget *window = NULL;
133 ssize_t len;
135 if (!window)
137 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
138 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
139 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
140 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
141 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
142 gtk_widget_hide, GTK_OBJECT(window));
143 log = gtk_text_new(NULL, NULL);
144 gtk_container_add(GTK_CONTAINER(window), log);
147 if (!GTK_WIDGET_MAPPED(window))
148 gtk_widget_show_all(window);
150 len = read(source, buf, BUFLEN);
151 if (len > 0)
152 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
155 int main(int argc, char **argv)
157 int stderr_pipe[2];
158 struct sigaction act;
159 GList *panel_dirs = NULL;
160 GList *panel_sides = NULL;
162 gtk_init(&argc, &argv);
164 while (1)
166 int c;
167 #ifdef HAVE_GETOPT_LONG
168 int long_index;
169 c = getopt_long(argc, argv, SHORT_OPS,
170 long_opts, &long_index);
171 #else
172 c = getopt(argc, argv, SHORT_OPS);
173 #endif
175 if (c == EOF)
176 break; /* No more options */
178 switch (c)
180 case 'o':
181 override_redirect = TRUE;
182 break;
183 case 'v':
184 printf(VERSION);
185 return EXIT_SUCCESS;
186 case 'h':
187 printf(HELP);
188 return EXIT_SUCCESS;
189 case 't':
190 case 'b':
191 panel_sides = g_list_prepend(panel_sides,
192 (gpointer) c);
193 panel_dirs = g_list_prepend(panel_dirs,
194 *optarg == '=' ? optarg + 1
195 : optarg);
196 break;
197 default:
198 printf(USAGE);
199 return EXIT_FAILURE;
203 choices_init("ROX-Filer");
205 gui_support_init();
206 pixmaps_init();
207 dir_init();
208 menu_init();
209 dnd_init();
210 filer_init();
211 mount_init();
212 options_init();
213 savebox_init();
214 type_init();
215 action_init();
217 options_load();
219 /* Let child processes die */
220 act.sa_handler = child_died;
221 sigemptyset(&act.sa_mask);
222 act.sa_flags = SA_NOCLDSTOP;
223 sigaction(SIGCHLD, &act, NULL);
225 /* Ignore SIGPIPE - check for EPIPE errors instead */
226 act.sa_handler = SIG_IGN;
227 sigemptyset(&act.sa_mask);
228 act.sa_flags = 0;
229 sigaction(SIGPIPE, &act, NULL);
231 euid = geteuid();
232 egid = getegid();
233 ngroups = getgroups(0, NULL);
234 if (ngroups < 0)
235 ngroups = 0;
236 else if (ngroups > 0)
238 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
239 getgroups(ngroups, supplemental_groups);
242 if (euid == 0)
244 if (get_choice("!!!DANGER!!!",
245 "Running ROX-Filer as root is VERY dangerous. If I "
246 "had a warranty (I don't) then doing this would "
247 "void it", 2,
248 "Don't click here", "Quit") != 0)
249 exit(EXIT_SUCCESS);
252 if (optind == argc && !panel_dirs)
253 filer_opendir(getenv("HOME"), PANEL_NO);
254 else
256 int i = optind;
257 GList *dir = panel_dirs;
258 GList *side = panel_sides;
260 while (dir)
262 int c = (int) side->data;
264 filer_opendir((char *) dir->data,
265 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
266 dir = dir->next;
267 side = side->next;
270 g_list_free(dir);
271 g_list_free(side);
273 while (i < argc)
274 filer_opendir(argv[i++], PANEL_NO);
277 pipe(stderr_pipe);
278 close_on_exec(stderr_pipe[0], TRUE);
279 close_on_exec(stderr_pipe[1], TRUE);
280 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
281 to_error_log = stderr_pipe[1];
283 gtk_main();
285 return EXIT_SUCCESS;