r345: Fixed a bug that prevented i18n from working.
[rox-filer/ma.git] / ROX-Filer / src / main.c
blob87d7dea1536e247c3e2d43bcf9f953ddb4b12e04
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
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 "global.h"
43 #include "main.h"
44 #include "support.h"
45 #include "gui_support.h"
46 #include "filer.h"
47 #include "display.h"
48 #include "mount.h"
49 #include "menu.h"
50 #include "dnd.h"
51 #include "options.h"
52 #include "choices.h"
53 #include "type.h"
54 #include "pixmaps.h"
55 #include "dir.h"
56 #include "action.h"
57 #include "i18n.h"
58 #include "remote.h"
59 #include "pinboard.h"
60 #include "run.h"
61 #include "toolbar.h"
63 int number_of_windows = 0; /* Quit when this reaches 0 again... */
64 int to_error_log = -1; /* Write here to log errors */
66 uid_t euid;
67 gid_t egid;
68 int ngroups; /* Number of supplemental groups */
69 gid_t *supplemental_groups = NULL;
70 char *home_dir, *app_dir;
72 /* Static prototypes */
73 static void show_features(void);
76 #define COPYING \
77 N_("Copyright (C) 2000 Thomas Leonard.\n" \
78 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
79 "to the extent permitted by law.\n" \
80 "You may redistribute copies of ROX-Filer\n" \
81 "under the terms of the GNU General Public License.\n" \
82 "For more information about these matters, " \
83 "see the file named COPYING.\n")
85 #ifdef HAVE_GETOPT_LONG
86 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
87 # define SHORT_ONLY_WARNING ""
88 #else
89 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
90 # define SHORT_ONLY_WARNING \
91 _("NOTE: Your system does not support long options - \n" \
92 "you must use the short versions instead.\n\n")
93 #endif
95 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
96 "Open each directory or file listed, or the current working\n" \
97 "directory if no arguments are given.\n\n" \
98 " -b, --bottom=DIR open DIR as a bottom-edge panel\n" \
99 " -h, --help display this help and exit\n" \
100 " -n, --new start a new filer, even if already running\n" \
101 " -o, --override override window manager control of panels\n" \
102 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
103 " -t, --top=DIR open DIR as a top-edge panel\n" \
104 " -v, --version display the version information and exit\n" \
105 "\nThe latest version can be found at:\n" \
106 "\thttp://rox.sourceforge.net\n" \
107 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
109 #define SHORT_OPS "t:b:op:hvn"
111 #ifdef HAVE_GETOPT_LONG
112 static struct option long_opts[] =
114 {"top", 1, NULL, 't'},
115 {"bottom", 1, NULL, 'b'},
116 {"override", 0, NULL, 'o'},
117 {"pinboard", 1, NULL, 'p'},
118 {"help", 0, NULL, 'h'},
119 {"version", 0, NULL, 'v'},
120 {"new", 0, NULL, 'n'},
121 {NULL, 0, NULL, 0},
123 #endif
125 /* Take control of panels away from WM? */
126 gboolean override_redirect = FALSE;
128 /* Always start a new filer, even if one seems to be already running */
129 gboolean new_copy = FALSE;
131 /* Maps child PIDs to Callback pointers */
132 static GHashTable *death_callbacks = NULL;
133 static gboolean child_died_flag = FALSE;
135 /* This is called as a signal handler; simply ensures that
136 * child_died_callback() will get called later.
138 static void child_died(int signum)
140 child_died_flag = TRUE;
141 write(to_error_log, '\0', 1); /* Wake up! */
144 static void child_died_callback(void)
146 int status;
147 int child;
149 child_died_flag = FALSE;
151 /* Find out which children exited and allow them to die */
154 Callback *cb;
156 child = waitpid(-1, &status, WNOHANG);
158 if (child == 0 || child == -1)
159 return;
161 cb = g_hash_table_lookup(death_callbacks, (gpointer) child);
162 if (cb)
164 cb->callback(cb->data);
165 g_hash_table_remove(death_callbacks, (gpointer) child);
168 } while (1);
171 #define BUFLEN 40
172 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
174 char buf[BUFLEN];
175 static GtkWidget *log = NULL;
176 static GtkWidget *window = NULL;
177 ssize_t len;
179 if (!window)
181 GtkWidget *hbox, *scrollbar;
183 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
184 gtk_window_set_title(GTK_WINDOW(window),
185 _("ROX-Filer message log"));
186 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
187 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
188 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
189 gtk_widget_hide, GTK_OBJECT(window));
192 hbox = gtk_hbox_new(FALSE, 0);
193 gtk_container_add(GTK_CONTAINER(window), hbox);
194 scrollbar = gtk_vscrollbar_new(NULL);
196 log = gtk_text_new(NULL,
197 gtk_range_get_adjustment(GTK_RANGE(scrollbar)));
198 gtk_box_pack_start(GTK_BOX(hbox), log, TRUE, TRUE, 0);
199 gtk_box_pack_start(GTK_BOX(hbox), scrollbar, FALSE, TRUE, 0);
202 len = read(source, buf, BUFLEN);
204 if (child_died_flag)
206 child_died_callback();
207 if (len == 1 && !*buf)
208 return;
211 if (len > 0)
213 if (!GTK_WIDGET_MAPPED(window))
214 gtk_widget_show_all(window);
215 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
219 /* The value that goes with an option */
220 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
222 int main(int argc, char **argv)
224 int stderr_pipe[2];
225 int i;
226 struct sigaction act;
227 guchar *tmp;
229 /* This is a list of \0 separated strings. Each string starts with a
230 * character indicating what kind of operation to perform:
232 * fFILE open this file (or directory)
233 * dDIR open this dir (even if it looks like an app)
234 * pPIN display this pinboard
235 * tDIR open DIR as a top-panel
236 * bDIR open DIR as a bottom-panel
238 GString *to_open;
240 to_open = g_string_new(NULL);
242 home_dir = g_get_home_dir();
243 app_dir = g_strdup(getenv("APP_DIR"));
245 choices_init();
246 i18n_init();
248 if (app_dir)
249 unsetenv("APP_DIR");
250 else
252 g_warning("APP_DIR environment variable was unset!\n"
253 "Use the AppRun script to invoke ROX-Filer...\n");
254 app_dir = g_get_current_dir();
257 death_callbacks = g_hash_table_new(NULL, NULL);
259 #ifdef HAVE_LIBVFS
260 mc_vfs_init();
261 #endif
262 gtk_init(&argc, &argv);
264 while (1)
266 int c;
267 #ifdef HAVE_GETOPT_LONG
268 int long_index;
269 c = getopt_long(argc, argv, SHORT_OPS,
270 long_opts, &long_index);
271 #else
272 c = getopt(argc, argv, SHORT_OPS);
273 #endif
275 if (c == EOF)
276 break; /* No more options */
278 switch (c)
280 case 'n':
281 new_copy = TRUE;
282 break;
283 case 'o':
284 override_redirect = TRUE;
285 break;
286 case 'v':
287 fprintf(stderr, "ROX-Filer %s\n", VERSION);
288 fprintf(stderr, _(COPYING));
289 show_features();
290 return EXIT_SUCCESS;
291 case 'h':
292 fprintf(stderr, _(HELP));
293 fprintf(stderr, _(SHORT_ONLY_WARNING));
294 return EXIT_SUCCESS;
295 case 't':
296 case 'b':
297 g_string_append_c(to_open, '<');
298 g_string_append_c(to_open, c);
299 g_string_append_c(to_open, '>');
300 tmp = pathdup(VALUE);
301 g_string_append(to_open, tmp);
302 g_free(tmp);
303 break;
304 case 'p':
305 g_string_append(to_open, "<p>");
306 g_string_append(to_open, VALUE);
307 break;
308 default:
309 printf(_(USAGE));
310 return EXIT_FAILURE;
314 i = optind;
315 while (i < argc)
317 tmp = pathdup(argv[i++]);
319 g_string_append(to_open, "<f>");
320 g_string_append(to_open, tmp);
323 if (to_open->len == 0)
325 guchar *dir;
327 dir = g_get_current_dir();
328 g_string_sprintf(to_open, "<d>%s", dir);
329 g_free(dir);
332 gui_support_init();
333 if (remote_init(to_open, new_copy))
334 return EXIT_SUCCESS; /* Already running */
335 pixmaps_init();
337 dir_init();
338 menu_init();
339 dnd_init();
340 filer_init();
341 toolbar_init();
342 display_init();
343 mount_init();
344 options_init();
345 type_init();
346 action_init();
347 pinboard_init();
349 options_load();
351 /* Let child processes die */
352 act.sa_handler = child_died;
353 sigemptyset(&act.sa_mask);
354 act.sa_flags = SA_NOCLDSTOP;
355 sigaction(SIGCHLD, &act, NULL);
357 /* Ignore SIGPIPE - check for EPIPE errors instead */
358 act.sa_handler = SIG_IGN;
359 sigemptyset(&act.sa_mask);
360 act.sa_flags = 0;
361 sigaction(SIGPIPE, &act, NULL);
363 euid = geteuid();
364 egid = getegid();
365 ngroups = getgroups(0, NULL);
366 if (ngroups < 0)
367 ngroups = 0;
368 else if (ngroups > 0)
370 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
371 getgroups(ngroups, supplemental_groups);
374 if (euid == 0)
376 if (get_choice(_("!!!DANGER!!!"),
377 _("Running ROX-Filer as root is VERY dangerous. If I "
378 "had a warranty (I don't) then doing this would "
379 "void it"), 2,
380 _("Don't click here"), _("Quit")) != 0)
381 exit(EXIT_SUCCESS);
384 run_list(to_open->str);
385 g_string_free(to_open, TRUE);
387 pipe(stderr_pipe);
388 close_on_exec(stderr_pipe[0], TRUE);
389 close_on_exec(stderr_pipe[1], TRUE);
390 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
391 to_error_log = stderr_pipe[1];
393 if (number_of_windows > 0)
394 gtk_main();
396 return EXIT_SUCCESS;
399 static void show_features(void)
401 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
402 g_printerr("%s... %s\n", _("VFS support"),
403 #ifdef HAVE_LIBVFS
404 _("Yes")
405 #else
406 _("No (couldn't find a valid libvfs)")
407 #endif
409 g_printerr("%s... %s\n", _("ImLib support"),
410 #ifdef HAVE_IMLIB
411 _("Yes")
412 #else
413 _("No (the imlib-config command didn't work)")
414 #endif
418 /* Register a function to be called when process number 'child' dies. */
419 void on_child_death(int child, CallbackFn callback, gpointer data)
421 Callback *cb;
423 g_return_if_fail(callback != NULL);
425 cb = g_new(Callback, 1);
427 cb->callback = callback;
428 cb->data = data;
430 g_hash_table_insert(death_callbacks, (gpointer) child, cb);