r250: Completed marking translatable strings. Tested by reversing all the strings
[rox-filer/ma.git] / ROX-Filer / src / main.c
blob68cc36cbcaeda78e466c9401ec58f716046ea8bc
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"
54 #include "i18n.h"
56 int number_of_windows = 0; /* Quit when this reaches 0 again... */
57 int to_error_log = -1; /* Write here to log errors */
59 uid_t euid;
60 gid_t egid;
61 int ngroups; /* Number of supplemental groups */
62 gid_t *supplemental_groups = NULL;
63 char *home_dir;
65 /* Static prototypes */
66 static void show_features(void);
69 #define VERSION "ROX-Filer 0.1.20\n"
70 #define COPYING \
71 N_("Copyright (C) 2000 Thomas Leonard.\n" \
72 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
73 "to the extent permitted by law.\n" \
74 "You may redistribute copies of ROX-Filer\n" \
75 "under the terms of the GNU General Public License.\n" \
76 "For more information about these matters, " \
77 "see the file named COPYING.\n")
79 #ifdef HAVE_GETOPT_LONG
80 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
81 # define SHORT_ONLY_WARNING ""
82 #else
83 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
84 # define SHORT_ONLY_WARNING \
85 _("NOTE: Your system does not support long options - \n" \
86 "you must use the short versions instead.\n\n")
87 #endif
89 #define SHORT_OPS "t:b:ohv"
91 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
92 "Open filer windows showing each directory listed, or $HOME \n" \
93 "if no directories are given.\n\n" \
94 " -h, --help display this help and exit\n" \
95 " -v, --version display the version information and exit\n" \
96 " -t, --top [DIR] open DIR as a top-edge panel\n" \
97 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
98 " -o, --override override window manager control of panels\n" \
99 "\nReport bugs to <tal197@ecs.soton.ac.uk>.\n")
101 #ifdef HAVE_GETOPT_LONG
102 static struct option long_opts[] =
104 {"top", 1, NULL, 't'},
105 {"bottom", 1, NULL, 'b'},
106 {"override", 0, NULL, 'o'},
107 {"help", 0, NULL, 'h'},
108 {"version", 0, NULL, 'v'},
109 {NULL, 0, NULL, 0},
111 #endif
113 /* Take control of panels away from WM? */
114 gboolean override_redirect = FALSE;
116 /* Maps child PIDs to Callback pointers */
117 static GHashTable *death_callbacks = NULL;
118 static gboolean child_died_flag = FALSE;
120 /* This is called as a signal handler; simply ensures that
121 * child_died_callback() will get called later.
123 static void child_died(int signum)
125 child_died_flag = TRUE;
126 write(to_error_log, '\0', 1); /* Wake up! */
129 static void child_died_callback(void)
131 int status;
132 int child;
134 child_died_flag = FALSE;
136 /* Find out which children exited and allow them to die */
139 Callback *cb;
141 child = waitpid(-1, &status, WNOHANG);
143 if (child == 0 || child == -1)
144 return;
146 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
147 if (cb)
149 cb->callback(cb->data);
150 g_hash_table_remove(death_callbacks, (gpointer) child);
153 } while (1);
156 #define BUFLEN 40
157 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
159 char buf[BUFLEN];
160 static GtkWidget *log = NULL;
161 static GtkWidget *window = NULL;
162 ssize_t len;
164 if (!window)
166 GtkWidget *hbox, *scrollbar;
168 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
169 gtk_window_set_title(GTK_WINDOW(window),
170 _("ROX-Filer message log"));
171 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
172 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
173 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
174 gtk_widget_hide, GTK_OBJECT(window));
177 hbox = gtk_hbox_new(FALSE, 0);
178 gtk_container_add(GTK_CONTAINER(window), hbox);
179 scrollbar = gtk_vscrollbar_new(NULL);
181 log = gtk_text_new(NULL,
182 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
183 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
184 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
187 len = read(source, buf, BUFLEN);
189 if (child_died_flag)
191 child_died_callback();
192 if (len == 1 && !*buf)
193 return;
196 if (len > 0)
198 if (!GTK_WIDGET_MAPPED(window))
199 gtk_widget_show_all(window);
200 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
204 int main(int argc, char **argv)
206 int stderr_pipe[2];
207 struct sigaction act;
208 GList *panel_dirs = NULL;
209 GList *panel_sides = NULL;
211 choices_init();
212 i18n_init();
214 home_dir = g_get_home_dir();
215 death_callbacks = g_hash_table_new(NULL, NULL);
217 #ifdef HAVE_LIBVFS
218 mc_vfs_init();
219 #endif
220 gtk_init(&argc, &argv);
222 while (1)
224 int c;
225 #ifdef HAVE_GETOPT_LONG
226 int long_index;
227 c = getopt_long(argc, argv, SHORT_OPS,
228 long_opts, &long_index);
229 #else
230 c = getopt(argc, argv, SHORT_OPS);
231 #endif
233 if (c == EOF)
234 break; /* No more options */
236 switch (c)
238 case 'o':
239 override_redirect = TRUE;
240 break;
241 case 'v':
242 printf(VERSION);
243 printf(_(COPYING));
244 show_features();
245 return EXIT_SUCCESS;
246 case 'h':
247 printf(_(HELP));
248 printf(_(SHORT_ONLY_WARNING));
249 return EXIT_SUCCESS;
250 case 't':
251 case 'b':
252 panel_sides = g_list_prepend(panel_sides,
253 (gpointer) c);
254 panel_dirs = g_list_prepend(panel_dirs,
255 *optarg == '=' ? optarg + 1
256 : optarg);
257 break;
258 default:
259 printf(_(USAGE));
260 return EXIT_FAILURE;
264 gui_support_init();
265 pixmaps_init();
266 dir_init();
267 menu_init();
268 dnd_init();
269 filer_init();
270 mount_init();
271 options_init();
272 type_init();
273 action_init();
275 options_load();
277 /* Let child processes die */
278 act.sa_handler = child_died;
279 sigemptyset(&act.sa_mask);
280 act.sa_flags = SA_NOCLDSTOP;
281 sigaction(SIGCHLD, &act, NULL);
283 /* Ignore SIGPIPE - check for EPIPE errors instead */
284 act.sa_handler = SIG_IGN;
285 sigemptyset(&act.sa_mask);
286 act.sa_flags = 0;
287 sigaction(SIGPIPE, &act, NULL);
289 euid = geteuid();
290 egid = getegid();
291 ngroups = getgroups(0, NULL);
292 if (ngroups < 0)
293 ngroups = 0;
294 else if (ngroups > 0)
296 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
297 getgroups(ngroups, supplemental_groups);
300 if (euid == 0)
302 if (get_choice(_("!!!DANGER!!!"),
303 _("Running ROX-Filer as root is VERY dangerous. If I "
304 "had a warranty (I don't) then doing this would "
305 "void it"), 2,
306 _("Don't click here"), _("Quit")) != 0)
307 exit(EXIT_SUCCESS);
310 if (optind == argc && !panel_dirs)
311 filer_opendir(home_dir, PANEL_NO);
312 else
314 int i = optind;
315 GList *dir = panel_dirs;
316 GList *side = panel_sides;
318 while (dir)
320 int c = (int) side->data;
322 filer_opendir((char *) dir->data,
323 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
324 dir = dir->next;
325 side = side->next;
328 g_list_free(dir);
329 g_list_free(side);
331 while (i < argc)
332 filer_opendir(argv[i++], PANEL_NO);
335 pipe(stderr_pipe);
336 close_on_exec(stderr_pipe[0], TRUE);
337 close_on_exec(stderr_pipe[1], TRUE);
338 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
339 to_error_log = stderr_pipe[1];
341 gtk_main();
343 return EXIT_SUCCESS;
346 static void show_features(void)
348 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
349 g_printerr("%s... %s\n", _("VFS support"),
350 #ifdef HAVE_LIBVFS
351 _("Yes")
352 #else
353 _("No (couldn't find a valid libvfs)")
354 #endif
356 g_printerr("%s... %s\n", _("ImLib support"),
357 #ifdef HAVE_IMLIB
358 _("Yes")
359 #else
360 _("No (the imlib-config command didn't work)")
361 #endif
363 g_printerr("%s... %s\n", _("Internationalisation support"),
364 #ifdef HAVE_GETTEXT
365 _("Yes")
366 #else
367 _("No (could not find gettext() function)")
368 #endif
372 /* Register a function to be called when process number 'child' dies. */
373 void on_child_death(int child, CallbackFn callback, gpointer data)
375 Callback *cb;
377 g_return_if_fail(callback != NULL);
379 cb = g_new(Callback, 1);
381 cb->callback = callback;
382 cb->data = data;
384 g_hash_table_insert(death_callbacks, (gpointer) child, cb);