r228: Added a nice list displaying the found files. Clicking opens a preview window
[rox-filer.git] / ROX-Filer / src / main.c
blobd0c4652ecaa0e4de0aae8310d8c900b7fabeb45e
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, 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>
31 #include <pwd.h>
32 #include <grp.h>
34 #ifdef HAVE_GETOPT_LONG
35 # include <getopt.h>
36 #endif
38 #include <gtk/gtk.h>
39 #include "collection.h"
41 #include "main.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "filer.h"
45 #include "mount.h"
46 #include "menu.h"
47 #include "dnd.h"
48 #include "options.h"
49 #include "choices.h"
50 #include "type.h"
51 #include "pixmaps.h"
52 #include "dir.h"
53 #include "action.h"
55 int number_of_windows = 0; /* Quit when this reaches 0 again... */
56 int to_error_log = -1; /* Write here to log errors */
58 uid_t euid;
59 gid_t egid;
60 int ngroups; /* Number of supplemental groups */
61 gid_t *supplemental_groups = NULL;
62 char *home_dir;
64 #define VERSION "ROX-Filer 0.1.18\n" \
65 "Copyright (C) 1999 Thomas Leonard.\n" \
66 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
67 "to the extent permitted by law.\n" \
68 "You may redistribute copies of ROX-Filer\n" \
69 "under the terms of the GNU General Public License.\n" \
70 "For more information about these matters, " \
71 "see the file named COPYING.\n"
73 #ifdef HAVE_GETOPT_LONG
74 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
75 # define SHORT_ONLY_WARNING ""
76 #else
77 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
78 # define SHORT_ONLY_WARNING \
79 "NOTE: Your system does not support long options - \n" \
80 "you must use the short versions instead.\n\n"
81 #endif
83 #define SHORT_OPS "t:b:ohv"
85 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
86 "Open filer windows showing each directory listed, or $HOME \n" \
87 "if no directories are given.\n\n" \
88 " -h, --help display this help and exit\n" \
89 " -v, --version display the version information and exit\n" \
90 " -t, --top [DIR] open DIR as a top-edge panel\n" \
91 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
92 " -o, --override override window manager control of panels\n" \
93 "\n" SHORT_ONLY_WARNING \
94 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
96 #ifdef HAVE_GETOPT_LONG
97 static struct option long_opts[] =
99 {"top", 1, NULL, 't'},
100 {"bottom", 1, NULL, 'b'},
101 {"override", 0, NULL, 'o'},
102 {"help", 0, NULL, 'h'},
103 {"version", 0, NULL, 'v'},
104 {NULL, 0, NULL, 0},
106 #endif
108 /* Take control of panels away from WM? */
109 gboolean override_redirect = FALSE;
111 static void child_died(int signum)
113 int status;
114 int child;
116 /* Find out which children exited and allow them to die */
119 child = waitpid(-1, &status, WNOHANG);
121 if (child == 0 || child == -1)
122 return;
124 /* fprintf(stderr, "Child %d exited\n", child); */
126 } while (1);
129 #define BUFLEN 40
130 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
132 char buf[BUFLEN];
133 static GtkWidget *log = NULL;
134 static GtkWidget *window = NULL;
135 ssize_t len;
137 if (!window)
139 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
140 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
141 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
142 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
143 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
144 gtk_widget_hide, GTK_OBJECT(window));
145 log = gtk_text_new(NULL, NULL);
146 gtk_container_add(GTK_CONTAINER(window), log);
149 if (!GTK_WIDGET_MAPPED(window))
150 gtk_widget_show_all(window);
152 len = read(source, buf, BUFLEN);
153 if (len > 0)
154 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
157 int main(int argc, char **argv)
159 int stderr_pipe[2];
160 struct sigaction act;
161 GList *panel_dirs = NULL;
162 GList *panel_sides = NULL;
164 home_dir = g_get_home_dir();
166 #ifdef HAVE_LIBVFS
167 mc_vfs_init();
168 #endif
169 gtk_init(&argc, &argv);
171 while (1)
173 int c;
174 #ifdef HAVE_GETOPT_LONG
175 int long_index;
176 c = getopt_long(argc, argv, SHORT_OPS,
177 long_opts, &long_index);
178 #else
179 c = getopt(argc, argv, SHORT_OPS);
180 #endif
182 if (c == EOF)
183 break; /* No more options */
185 switch (c)
187 case 'o':
188 override_redirect = TRUE;
189 break;
190 case 'v':
191 printf(VERSION);
192 return EXIT_SUCCESS;
193 case 'h':
194 printf(HELP);
195 return EXIT_SUCCESS;
196 case 't':
197 case 'b':
198 panel_sides = g_list_prepend(panel_sides,
199 (gpointer) c);
200 panel_dirs = g_list_prepend(panel_dirs,
201 *optarg == '=' ? optarg + 1
202 : optarg);
203 break;
204 default:
205 printf(USAGE);
206 return EXIT_FAILURE;
210 choices_init("ROX-Filer");
212 gui_support_init();
213 pixmaps_init();
214 dir_init();
215 menu_init();
216 dnd_init();
217 filer_init();
218 mount_init();
219 options_init();
220 type_init();
221 action_init();
223 options_load();
225 /* Let child processes die */
226 act.sa_handler = child_died;
227 sigemptyset(&act.sa_mask);
228 act.sa_flags = SA_NOCLDSTOP;
229 sigaction(SIGCHLD, &act, NULL);
231 /* Ignore SIGPIPE - check for EPIPE errors instead */
232 act.sa_handler = SIG_IGN;
233 sigemptyset(&act.sa_mask);
234 act.sa_flags = 0;
235 sigaction(SIGPIPE, &act, NULL);
237 euid = geteuid();
238 egid = getegid();
239 ngroups = getgroups(0, NULL);
240 if (ngroups < 0)
241 ngroups = 0;
242 else if (ngroups > 0)
244 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
245 getgroups(ngroups, supplemental_groups);
248 if (euid == 0)
250 if (get_choice("!!!DANGER!!!",
251 "Running ROX-Filer as root is VERY dangerous. If I "
252 "had a warranty (I don't) then doing this would "
253 "void it", 2,
254 "Don't click here", "Quit") != 0)
255 exit(EXIT_SUCCESS);
258 if (optind == argc && !panel_dirs)
259 filer_opendir(home_dir, PANEL_NO);
260 else
262 int i = optind;
263 GList *dir = panel_dirs;
264 GList *side = panel_sides;
266 while (dir)
268 int c = (int) side->data;
270 filer_opendir((char *) dir->data,
271 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
272 dir = dir->next;
273 side = side->next;
276 g_list_free(dir);
277 g_list_free(side);
279 while (i < argc)
280 filer_opendir(argv[i++], PANEL_NO);
283 pipe(stderr_pipe);
284 close_on_exec(stderr_pipe[0], TRUE);
285 close_on_exec(stderr_pipe[1], TRUE);
286 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
287 to_error_log = stderr_pipe[1];
289 gtk_main();
291 return EXIT_SUCCESS;