r2918: Doing a Refresh in a directory under /uri/0install triggers a remote refresh.
[rox-filer.git] / ROX-Filer / src / main.c
blob9c949ad6b0fd2619fa2751e9999b8a1e734e38a2
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
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 /* main.c - parses command-line options and parameters, plus some global
23 * housekeeping.
25 * New to the code and feeling lost? Read global.h now.
28 #include "config.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <pwd.h>
39 #include <grp.h>
40 #include <libxml/parser.h>
42 #ifdef HAVE_GETOPT_LONG
43 # include <getopt.h>
44 #endif
46 #include <gtk/gtk.h>
48 #include "global.h"
50 #include "main.h"
51 #include "support.h"
52 #include "gui_support.h"
53 #include "filer.h"
54 #include "display.h"
55 #include "mount.h"
56 #include "menu.h"
57 #include "dnd.h"
58 #include "options.h"
59 #include "choices.h"
60 #include "type.h"
61 #include "pixmaps.h"
62 #include "dir.h"
63 #include "diritem.h"
64 #include "action.h"
65 #include "i18n.h"
66 #include "remote.h"
67 #include "pinboard.h"
68 #include "run.h"
69 #include "toolbar.h"
70 #include "bind.h"
71 #include "panel.h"
72 #include "session.h"
73 #include "minibuffer.h"
75 int number_of_windows = 0; /* Quit when this reaches 0 again... */
76 int to_wakeup_pipe = -1; /* Write here to get noticed */
78 /* Information about the ROX-Filer process */
79 uid_t euid;
80 gid_t egid;
81 int ngroups; /* Number of supplemental groups */
82 gid_t *supplemental_groups = NULL;
84 /* Message to display at the top of each filer window */
85 const gchar *show_user_message = NULL;
87 int home_dir_len;
88 const char *home_dir, *app_dir;
90 GtkTooltips *tooltips = NULL;
92 #define COPYING \
93 N_("Copyright (C) 2003 Thomas Leonard.\n" \
94 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
95 "to the extent permitted by law.\n" \
96 "You may redistribute copies of ROX-Filer\n" \
97 "under the terms of the GNU General Public License.\n" \
98 "For more information about these matters, " \
99 "see the file named COPYING.\n")
101 #ifdef HAVE_GETOPT_LONG
102 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
103 # define SHORT_ONLY_WARNING ""
104 #else
105 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
106 # define SHORT_ONLY_WARNING \
107 _("NOTE: Your system does not support long options - \n" \
108 "you must use the short versions instead.\n\n")
109 #endif
111 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
112 "Open each directory or file listed, or the current working\n" \
113 "directory if no arguments are given.\n\n" \
114 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
115 " -c, --client-id=ID used for session management\n" \
116 " -d, --dir=DIR open DIR as directory (not application)\n" \
117 " -D, --close=DIR close DIR and its subdirectories\n" \
118 " -h, --help display this help and exit\n" \
119 " -l, --left=PANEL open PAN as a left-edge panel\n" \
120 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
121 " -n, --new start new copy; for debugging the filer\n" \
122 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
123 " -r, --right=PANEL open PAN as a right-edge panel\n" \
124 " -R, --RPC invoke method call read from stdin\n" \
125 " -s, --show=FILE open a directory showing FILE\n" \
126 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
127 " -u, --user show user name in each window \n" \
128 " -v, --version display the version information and exit\n" \
129 " -x, --examine=FILE FILE has changed - re-examine it\n" \
130 "\nReport bugs to <tal197@users.sourceforge.net>.\n" \
131 "Home page (including updated versions): http://rox.sourceforge.net/\n")
133 #define SHORT_OPS "c:d:t:b:l:r:op:s:hvnux:m:D:R"
135 #ifdef HAVE_GETOPT_LONG
136 static struct option long_opts[] =
138 {"dir", 1, NULL, 'd'},
139 {"top", 1, NULL, 't'},
140 {"bottom", 1, NULL, 'b'},
141 {"left", 1, NULL, 'l'},
142 {"override", 0, NULL, 'o'},
143 {"pinboard", 1, NULL, 'p'},
144 {"right", 1, NULL, 'r'},
145 {"help", 0, NULL, 'h'},
146 {"version", 0, NULL, 'v'},
147 {"user", 0, NULL, 'u'},
148 {"new", 0, NULL, 'n'},
149 {"RPC", 0, NULL, 'R'},
150 {"show", 1, NULL, 's'},
151 {"examine", 1, NULL, 'x'},
152 {"close", 1, NULL, 'D'},
153 {"mime-type", 1, NULL, 'm'},
154 {"client-id", 1, NULL, 'c'},
155 {NULL, 0, NULL, 0},
157 #endif
159 /* Take control of panels away from WM? */
160 Option o_override_redirect;
162 /* Always start a new filer, even if one seems to be already running */
163 gboolean new_copy = FALSE;
165 /* Maps child PIDs to Callback pointers */
166 static GHashTable *death_callbacks = NULL;
167 static gboolean child_died_flag = FALSE;
169 Option o_dnd_no_hostnames;
171 /* Static prototypes */
172 static void show_features(void);
173 static void soap_add(xmlNodePtr body,
174 xmlChar *function,
175 const xmlChar *arg1_name, const xmlChar *arg1_value,
176 const xmlChar *arg2_name, const xmlChar *arg2_value);
177 static void child_died(int signum);
178 static void child_died_callback(void);
179 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
181 /****************************************************************
182 * EXTERNAL INTERFACE *
183 ****************************************************************/
185 /* The value that goes with an option */
186 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
188 /* Parses the command-line to work out what the user wants to do.
189 * Tries to send the request to an already-running copy of the filer.
190 * If that fails, it initialises all the other modules and executes the
191 * request itself.
193 int main(int argc, char **argv)
195 int wakeup_pipe[2];
196 int i;
197 struct sigaction act;
198 guchar *tmp, *dir;
199 gchar *client_id = NULL;
200 gboolean show_user = FALSE;
201 xmlDocPtr rpc, soap_rpc = NULL, reply;
202 xmlNodePtr body;
204 home_dir = g_get_home_dir();
205 home_dir_len = strlen(home_dir);
206 app_dir = g_strdup(getenv("APP_DIR"));
208 /* Get internationalisation up and running. This requires the
209 * choices system, to discover the user's preferred language.
211 choices_init();
212 options_init();
213 i18n_init();
215 if (!app_dir)
217 g_warning("APP_DIR environment variable was unset!\n"
218 "Use the AppRun script to invoke ROX-Filer...\n");
219 app_dir = g_get_current_dir();
221 #ifdef HAVE_UNSETENV
222 else
224 /* Don't pass it on to our child processes... */
225 unsetenv("APP_DIR");
227 #endif
229 /* Sometimes we want to take special action when a child
230 * process exits. This hash table is used to convert the
231 * child's PID to the callback function.
233 death_callbacks = g_hash_table_new(NULL, NULL);
235 /* Find out some information about ourself */
236 euid = geteuid();
237 egid = getegid();
238 ngroups = getgroups(0, NULL);
239 if (ngroups < 0)
240 ngroups = 0;
241 else if (ngroups > 0)
243 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
244 getgroups(ngroups, supplemental_groups);
247 if (argc == 2 && strcmp(argv[1], "-v") == 0)
249 /* This is used by install.sh to test if the filer
250 * compiled OK. Do this test before gtk_init so that
251 * we don't need an X server to install.
253 g_print("ROX-Filer %s\n", VERSION);
254 g_print(_(COPYING));
255 show_features();
256 return EXIT_SUCCESS;
259 option_add_int(&o_override_redirect, "override_redirect", FALSE);
261 /* The idea here is to convert the command-line arguments
262 * into a SOAP RPC.
263 * We attempt to invoke the call on an already-running copy of
264 * the filer if possible, or execute it ourselves if not.
266 rpc = soap_new(&body);
268 /* Note: must do this before checking our options,
269 * otherwise we report an error for Gtk's options.
271 gtk_init(&argc, &argv);
272 /* Set a default style for the collection widget */
273 gtk_rc_parse_string("style \"rox-default-collection-style\" {\n"
274 " bg[NORMAL] = \"#f3f3f3\"\n"
275 " fg[NORMAL] = \"#000000\"\n"
276 " bg[INSENSITIVE] = \"#bfbfbf\"\n"
277 " fg[INSENSITIVE] = \"#000000\"\n"
278 "}\n"
279 "style \"rox-default-pinboard-style\" {\n"
280 " bg[NORMAL] = \"#666666\"\n"
281 "}\n"
282 "widget \"rox-pinboard\" style : gtk "
283 "\"rox-default-pinboard-style\"\n"
285 "class \"Collection\" style : gtk "
286 "\"rox-default-collection-style\"\n");
288 /* Process each option in turn */
289 while (1)
291 int c;
292 #ifdef HAVE_GETOPT_LONG
293 int long_index;
294 c = getopt_long(argc, argv, SHORT_OPS,
295 long_opts, &long_index);
296 #else
297 c = getopt(argc, argv, SHORT_OPS);
298 #endif
300 if (c == EOF)
301 break; /* No more options */
303 switch (c)
305 case 'n':
306 new_copy = TRUE;
307 break;
308 case 'o':
309 info_message(_("The -o argument is no longer "
310 "used. You can turn on override "
311 "redirect from the Options box "
312 "instead."));
313 break;
314 case 'v':
315 g_print("ROX-Filer %s\n", VERSION);
316 g_print("%s", _(COPYING));
317 show_features();
318 return EXIT_SUCCESS;
319 case 'h':
320 g_print("%s", _(HELP));
321 g_print("%s", _(SHORT_ONLY_WARNING));
322 return EXIT_SUCCESS;
323 case 'D':
324 case 'd':
325 case 'x':
326 /* Argument is a path */
327 if (c == 'd' && VALUE[0] == '/')
328 tmp = g_strdup(VALUE);
329 else
330 tmp = pathdup(VALUE);
331 soap_add(body,
332 c == 'D' ? "CloseDir" :
333 c == 'd' ? "OpenDir" :
334 c == 'x' ? "Examine" : "Unknown",
335 "Filename", tmp,
336 NULL, NULL);
337 g_free(tmp);
338 break;
339 case 's':
340 tmp = g_path_get_dirname(VALUE);
342 if (tmp[0] == '/')
343 dir = NULL;
344 else
345 dir = pathdup(tmp);
347 soap_add(body, "Show",
348 "Directory", dir ? dir : tmp,
349 "Leafname", g_basename(VALUE));
350 g_free(tmp);
351 g_free(dir);
352 break;
353 case 'l':
354 case 'r':
355 case 't':
356 case 'b':
357 /* Argument is a leaf (or starts with /) */
358 soap_add(body, "Panel", "Name", VALUE,
359 "Side", c == 'l' ? "Left" :
360 c == 'r' ? "Right" :
361 c == 't' ? "Top" :
362 c == 'b' ? "Bottom" :
363 "Unkown");
364 break;
365 case 'p':
366 soap_add(body, "Pinboard",
367 "Name", VALUE, NULL, NULL);
368 break;
369 case 'u':
370 show_user = TRUE;
371 break;
372 case 'm':
374 MIME_type *type;
375 type_init();
376 type = type_get_type(VALUE);
377 printf("%s/%s\n", type->media_type,
378 type->subtype);
379 return EXIT_SUCCESS;
381 case 'c':
382 client_id = g_strdup(VALUE);
383 break;
384 case 'R':
385 soap_rpc = xmlParseFile("-");
386 if (!soap_rpc)
387 g_error("Invalid XML in RPC");
388 break;
389 default:
390 printf(_(USAGE));
391 return EXIT_FAILURE;
395 tooltips = gtk_tooltips_new();
397 if (euid == 0 || show_user)
398 show_user_message = g_strdup_printf(_("Running as user '%s'"),
399 user_name(euid));
401 /* Add each remaining (non-option) argument to the list of files
402 * to run.
404 i = optind;
405 while (i < argc)
407 tmp = pathdup(argv[i++]);
409 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
411 g_free(tmp);
414 if (soap_rpc)
416 if (body->xmlChildrenNode)
417 g_error("Can't use -R with other options - sorry!");
418 xmlFreeDoc(rpc);
419 body = NULL;
420 rpc = soap_rpc;
422 else if (!body->xmlChildrenNode)
424 /* The user didn't request any action. Open the current
425 * directory.
427 guchar *dir;
429 dir = g_get_current_dir();
430 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
431 g_free(dir);
434 option_add_int(&o_dnd_no_hostnames, "dnd_no_hostnames", 1);
436 /* Try to send the request to an already-running copy of the filer */
437 gui_support_init();
438 if (remote_init(rpc, new_copy))
439 return EXIT_SUCCESS; /* It worked - exit */
441 /* Put ourselves into the background (so 'rox' always works the
442 * same, whether we're already running or not).
443 * Not for -n, though (helps when debugging).
445 if (!new_copy)
447 int fd;
448 pid_t child;
450 child = fork();
451 if (child > 0)
452 _exit(0); /* Parent exits */
453 /* Otherwise we're the child (or an error occurred - ignore
454 * it!).
457 /* Close stdin. We don't need it, and it can cause problems if
458 * a child process wants a password, etc...
460 fd = open("/dev/null", O_RDONLY);
461 if (fd > 0)
463 close(0);
464 dup2(fd, 0);
465 close(fd);
469 /* Initialize the rest of the filer... */
471 pixmaps_init();
473 dnd_init();
474 bind_init();
475 dir_init();
476 diritem_init();
477 menu_init();
478 minibuffer_init();
479 filer_init();
480 toolbar_init();
481 display_init();
482 mount_init();
483 type_init();
484 action_init();
486 pinboard_init();
487 panel_init();
489 /* Let everyone update */
490 options_notify();
492 /* When we get a signal, we can't do much right then. Instead,
493 * we send a char down this pipe, which causes the main loop to
494 * deal with the event next time we're idle.
496 pipe(wakeup_pipe);
497 close_on_exec(wakeup_pipe[0], TRUE);
498 close_on_exec(wakeup_pipe[1], TRUE);
499 gtk_input_add_full(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb,
500 NULL, NULL, NULL);
501 to_wakeup_pipe = wakeup_pipe[1];
503 /* If the pipe is full then we're going to get woken up anyway... */
504 set_blocking(to_wakeup_pipe, FALSE);
506 /* Let child processes die */
507 act.sa_handler = child_died;
508 sigemptyset(&act.sa_mask);
509 act.sa_flags = SA_NOCLDSTOP;
510 sigaction(SIGCHLD, &act, NULL);
512 /* Ignore SIGPIPE - check for EPIPE errors instead */
513 act.sa_handler = SIG_IGN;
514 sigemptyset(&act.sa_mask);
515 act.sa_flags = 0;
516 sigaction(SIGPIPE, &act, NULL);
518 /* Set up session managament if available */
519 session_init(client_id);
520 g_free(client_id);
522 /* Finally, execute the request */
523 reply = run_soap(rpc);
524 xmlFreeDoc(rpc);
525 if (reply)
527 /* Write the result, if any, to stdout */
528 save_xml_file(reply, "-");
529 xmlFreeDoc(reply);
532 /* Enter the main loop, processing events until all our windows
533 * are closed.
535 if (number_of_windows > 0)
536 gtk_main();
538 return EXIT_SUCCESS;
541 /* Register a function to be called when process number 'child' dies. */
542 void on_child_death(gint child, CallbackFn callback, gpointer data)
544 Callback *cb;
546 g_return_if_fail(callback != NULL);
548 cb = g_new(Callback, 1);
550 cb->callback = callback;
551 cb->data = data;
553 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
556 void one_less_window(void)
558 if (--number_of_windows < 1)
559 gtk_main_quit();
562 /****************************************************************
563 * INTERNAL FUNCTIONS *
564 ****************************************************************/
566 static void show_features(void)
568 g_print("\n");
569 g_print(_("Compiled with GTK version %s\n"), GTK_VERSION);
570 g_print(_("Running with GTK version %d.%d.%d\n"),
571 gtk_major_version,
572 gtk_minor_version,
573 gtk_micro_version);
574 g_print("\n-- %s --\n\n", _("features set at compile time"));
575 g_print("%s... %s\n", _("Large File Support"),
576 #ifdef LARGE_FILE_SUPPORT
577 _("Yes")
578 #else
579 _("No")
580 #endif
582 #if 0
583 g_print("%s... %s\n", _("GNOME-VFS library"),
584 # ifdef WITH_GNOMEVFS
585 _("Yes")
586 # else
587 _("No (gnome-vfs-config not found)")
588 # endif
590 #endif
591 g_print("%s... %s\n", _("Dnotify support"),
592 #ifdef USE_DNOTIFY
593 _("Yes")
594 #else
595 _("No")
596 #endif
600 static void soap_add(xmlNodePtr body,
601 xmlChar *function,
602 const xmlChar *arg1_name, const xmlChar *arg1_value,
603 const xmlChar *arg2_name, const xmlChar *arg2_value)
605 xmlNodePtr node;
606 xmlNs *rox;
608 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
610 node = xmlNewChild(body, rox, function, NULL);
612 if (arg1_name)
614 xmlNewTextChild(node, rox, arg1_name, arg1_value);
615 if (arg2_name)
616 xmlNewTextChild(node, rox, arg2_name, arg2_value);
620 /* This is called as a signal handler; simply ensures that
621 * child_died_callback() will get called later.
623 static void child_died(int signum)
625 child_died_flag = TRUE;
626 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
629 static void child_died_callback(void)
631 int status;
632 gint child;
634 child_died_flag = FALSE;
636 /* Find out which children exited and allow them to die */
639 Callback *cb;
641 child = waitpid(-1, &status, WNOHANG);
643 if (child == 0 || child == -1)
644 return;
646 cb = g_hash_table_lookup(death_callbacks,
647 GINT_TO_POINTER(child));
648 if (cb)
650 cb->callback(cb->data);
651 g_hash_table_remove(death_callbacks,
652 GINT_TO_POINTER(child));
655 } while (1);
658 #define BUFLEN 40
659 /* When data is written to_wakeup_pipe, this gets called from the event
660 * loop some time later. Useful for getting out of signal handlers, etc.
662 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
664 char buf[BUFLEN];
666 read(source, buf, BUFLEN);
668 if (child_died_flag)
669 child_died_callback();
670 #ifdef USE_DNOTIFY
671 if (dnotify_wakeup)
672 dnotify_wakeup();
673 #endif