r947: Added new SOAP methods Copy, Move, Link, Mount and FileType (Stephen Watson).
[rox-filer.git] / ROX-Filer / src / main.c
blob4f131e43fdbd9e14183dc61ce066236de3fab393
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <grp.h>
35 #ifdef HAVE_GETOPT_LONG
36 # include <getopt.h>
37 #endif
39 #include <gtk/gtk.h>
40 #include "collection.h"
42 #include "global.h"
44 #include "main.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "filer.h"
48 #include "display.h"
49 #include "mount.h"
50 #include "menu.h"
51 #include "dnd.h"
52 #include "options.h"
53 #include "choices.h"
54 #include "type.h"
55 #include "pixmaps.h"
56 #include "dir.h"
57 #include "action.h"
58 #include "i18n.h"
59 #include "remote.h"
60 #include "pinboard.h"
61 #include "run.h"
62 #include "toolbar.h"
63 #include "bind.h"
64 #include "icon.h"
65 #include "appinfo.h"
66 #include "panel.h"
67 #include "session.h"
69 int number_of_windows = 0; /* Quit when this reaches 0 again... */
70 static int to_wakeup_pipe = -1; /* Write here to get noticed */
72 uid_t euid;
73 gid_t egid;
74 int ngroups; /* Number of supplemental groups */
75 gid_t *supplemental_groups = NULL;
77 /* Message to display at the top of each filer window */
78 guchar *show_user_message = NULL;
80 int home_dir_len;
81 char *home_dir, *app_dir;
83 GtkTooltips *tooltips = NULL;
85 #define COPYING \
86 N_("Copyright (C) 2001 Thomas Leonard.\n" \
87 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
88 "to the extent permitted by law.\n" \
89 "You may redistribute copies of ROX-Filer\n" \
90 "under the terms of the GNU General Public License.\n" \
91 "For more information about these matters, " \
92 "see the file named COPYING.\n")
94 #ifdef HAVE_GETOPT_LONG
95 # define USAGE N_("Try `ROX-Filer/AppRun --help' for more information.\n")
96 # define SHORT_ONLY_WARNING ""
97 #else
98 # define USAGE N_("Try `ROX-Filer/AppRun -h' for more information.\n")
99 # define SHORT_ONLY_WARNING \
100 _("NOTE: Your system does not support long options - \n" \
101 "you must use the short versions instead.\n\n")
102 #endif
104 #define HELP N_("Usage: ROX-Filer/AppRun [OPTION]... [FILE]...\n" \
105 "Open each directory or file listed, or the current working\n" \
106 "directory if no arguments are given.\n\n" \
107 " -b, --bottom=PANEL open PAN as a bottom-edge panel\n" \
108 " -c, --client-id=ID used for session management\n" \
109 " -d, --dir=DIR open DIR as directory (not application)\n" \
110 " -D, --close=DIR close DIR and its subdirectories\n" \
111 " -h, --help display this help and exit\n" \
112 " -l, --left=PANEL open PAN as a left-edge panel\n" \
113 " -m, --mime-type=FILE print MIME type of FILE and exit\n" \
114 " -n, --new start a new filer, even if already running\n" \
115 " -o, --override override window manager control of panels\n" \
116 " -p, --pinboard=PIN use pinboard PIN as the pinboard\n" \
117 " -r, --right=PANEL open PAN as a right-edge panel\n" \
118 " -R, --RPC invoke method call read from stdin\n" \
119 " -s, --show=FILE open a directory showing FILE\n" \
120 " -t, --top=PANEL open PANEL as a top-edge panel\n" \
121 " -u, --user show user name in each window \n" \
122 " -v, --version display the version information and exit\n" \
123 " -x, --examine=FILE FILE has changed - re-examine it\n" \
124 "\nThe latest version can be found at:\n" \
125 "\thttp://rox.sourceforge.net\n" \
126 "\nReport bugs to <tal197@users.sourceforge.net>.\n")
128 #define SHORT_OPS "d:t:b:l:r:op:s:hvnux:m:D:R"
130 #ifdef HAVE_GETOPT_LONG
131 static struct option long_opts[] =
133 {"dir", 1, NULL, 'd'},
134 {"top", 1, NULL, 't'},
135 {"bottom", 1, NULL, 'b'},
136 {"left", 1, NULL, 'l'},
137 {"override", 0, NULL, 'o'},
138 {"pinboard", 1, NULL, 'p'},
139 {"right", 1, NULL, 'r'},
140 {"help", 0, NULL, 'h'},
141 {"version", 0, NULL, 'v'},
142 {"user", 0, NULL, 'u'},
143 {"new", 0, NULL, 'n'},
144 {"RPC", 0, NULL, 'R'},
145 {"show", 1, NULL, 's'},
146 {"examine", 1, NULL, 'x'},
147 {"close", 1, NULL, 'D'},
148 {"mime-type", 1, NULL, 'm'},
149 {"client-id", 1, NULL, 'c'},
150 {NULL, 0, NULL, 0},
152 #endif
154 /* Take control of panels away from WM? */
155 gboolean override_redirect = FALSE;
157 /* Always start a new filer, even if one seems to be already running */
158 gboolean new_copy = FALSE;
160 /* Maps child PIDs to Callback pointers */
161 static GHashTable *death_callbacks = NULL;
162 static gboolean child_died_flag = FALSE;
164 /* Static prototypes */
165 static void show_features(void);
166 static void soap_add(xmlNodePtr body,
167 xmlChar *function,
168 xmlChar *arg1_name, xmlChar *arg1_value,
169 xmlChar *arg2_name, xmlChar *arg2_value);
170 static void child_died(int signum);
171 static void child_died_callback(void);
172 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition);
174 /****************************************************************
175 * EXTERNAL INTERFACE *
176 ****************************************************************/
178 /* The value that goes with an option */
179 #define VALUE (*optarg == '=' ? optarg + 1 : optarg)
181 int main(int argc, char **argv)
183 int wakeup_pipe[2];
184 int i;
185 struct sigaction act;
186 guchar *tmp, *dir, *slash;
187 gchar *client_id = NULL;
188 gboolean show_user = FALSE;
189 xmlDocPtr rpc, soap_rpc = NULL, reply;
190 xmlNodePtr body;
192 home_dir = g_get_home_dir();
193 home_dir_len = strlen(home_dir);
194 app_dir = g_strdup(getenv("APP_DIR"));
196 choices_init();
197 i18n_init();
199 if (!app_dir)
201 g_warning("APP_DIR environment variable was unset!\n"
202 "Use the AppRun script to invoke ROX-Filer...\n");
203 app_dir = g_get_current_dir();
205 #ifdef HAVE_UNSETENV
206 else
208 /* Don't pass it on to our child processes... */
209 unsetenv("APP_DIR");
211 #endif
213 death_callbacks = g_hash_table_new(NULL, NULL);
215 #ifdef HAVE_LIBVFS
216 mc_vfs_init();
217 #endif
219 euid = geteuid();
220 egid = getegid();
221 ngroups = getgroups(0, NULL);
222 if (ngroups < 0)
223 ngroups = 0;
224 else if (ngroups > 0)
226 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
227 getgroups(ngroups, supplemental_groups);
230 /* The idea here is to convert the command-line arguments
231 * into a SOAP RPC.
232 * We attempt to invoke the call on an already-running copy of
233 * the filer if possible, or execute it ourselves if not.
235 rpc = soap_new(&body);
237 while (1)
239 int c;
240 #ifdef HAVE_GETOPT_LONG
241 int long_index;
242 c = getopt_long(argc, argv, SHORT_OPS,
243 long_opts, &long_index);
244 #else
245 c = getopt(argc, argv, SHORT_OPS);
246 #endif
248 if (c == EOF)
249 break; /* No more options */
251 switch (c)
253 case 'n':
254 new_copy = TRUE;
255 break;
256 case 'o':
257 override_redirect = TRUE;
258 break;
259 case 'v':
260 fprintf(stderr, "ROX-Filer %s\n", VERSION);
261 fprintf(stderr, _(COPYING));
262 show_features();
263 return EXIT_SUCCESS;
264 case 'h':
265 fprintf(stderr, _(HELP));
266 fprintf(stderr, _(SHORT_ONLY_WARNING));
267 return EXIT_SUCCESS;
268 case 'D':
269 case 'd':
270 case 'x':
271 /* Argument is a path */
272 tmp = pathdup(VALUE);
273 soap_add(body,
274 c == 'D' ? "CloseDir" :
275 c == 'd' ? "OpenDir" :
276 c == 'x' ? "Examine" : "Unknown",
277 "Filename", tmp,
278 NULL, NULL);
279 g_free(tmp);
280 break;
281 case 's':
282 tmp = g_strdup(VALUE);
283 slash = strrchr(tmp, '/');
284 if (slash)
286 *slash = '\0';
287 slash++;
288 dir = pathdup(tmp);
290 else
292 slash = tmp;
293 dir = pathdup(".");
296 soap_add(body, "Show",
297 "Directory", dir,
298 "Leafname", slash);
299 g_free(tmp);
300 g_free(dir);
301 break;
302 case 'l':
303 case 'r':
304 case 't':
305 case 'b':
306 /* Argument is a leaf (or starts with /) */
307 soap_add(body, "Panel", "Name", VALUE,
308 "Side", c == 'l' ? "Left" :
309 c == 'r' ? "Right" :
310 c == 't' ? "Top" :
311 c == 'b' ? "Bottom" :
312 "Unkown");
313 break;
314 case 'p':
315 soap_add(body, "Pinboard",
316 "Name", VALUE, NULL, NULL);
317 break;
318 case 'u':
319 show_user = TRUE;
320 break;
321 case 'm':
323 MIME_type *type;
324 type_init();
325 type = type_get_type(VALUE);
326 printf("%s/%s\n", type->media_type,
327 type->subtype);
329 return EXIT_SUCCESS;
330 case 'c':
331 client_id = g_strdup(VALUE);
332 break;
333 case 'R':
334 soap_rpc = xmlParseFile("-");
335 if (!soap_rpc)
336 g_error("Invalid XML in RPC");
337 break;
338 default:
339 printf(_(USAGE));
340 return EXIT_FAILURE;
344 add_default_styles();
345 gtk_init(&argc, &argv);
346 tooltips = gtk_tooltips_new();
348 if (euid == 0 || show_user)
349 show_user_message = g_strdup_printf( _("Running as user '%s'"),
350 user_name(euid));
352 i = optind;
353 while (i < argc)
355 tmp = pathdup(argv[i++]);
357 soap_add(body, "Run", "Filename", tmp, NULL, NULL);
359 g_free(tmp);
362 if (soap_rpc)
364 if (body->xmlChildrenNode)
365 g_error("Can't use -R with other options - sorry!");
366 xmlFreeDoc(rpc);
367 body = NULL;
368 rpc = soap_rpc;
370 else if (!body->xmlChildrenNode)
372 guchar *dir;
374 dir = g_get_current_dir();
375 soap_add(body, "OpenDir", "Filename", dir, NULL, NULL);
376 g_free(dir);
379 option_add_int("dnd_no_hostnames", 1, NULL);
381 gui_support_init();
382 if (remote_init(rpc, new_copy))
383 return EXIT_SUCCESS; /* Already running */
385 /* Put ourselves into the background (so 'rox' always works the
386 * same, whether we're already running or not).
387 * Not for -n, though (helps when debugging).
389 if (!new_copy)
391 pid_t child;
393 child = fork();
394 if (child > 0)
395 _exit(0); /* Parent exits */
396 /* Otherwise we're the child (or an error occurred - ignore
397 * it!).
401 /* Close stdin. We don't need it, and it can cause problems if
402 * a child process wants a password, etc...
405 int fd;
406 fd = open("/dev/null", O_RDONLY);
407 if (fd > 0)
409 close(0);
410 dup2(fd, 0);
411 close(fd);
415 pixmaps_init();
417 dnd_init();
418 bind_init();
419 dir_init();
420 diritem_init();
421 menu_init();
422 minibuffer_init();
423 filer_init();
424 toolbar_init();
425 display_init();
426 mount_init();
427 options_init();
428 type_init();
429 action_init();
430 appinfo_init();
432 icon_init();
433 pinboard_init();
434 panel_init();
436 options_load();
438 pipe(wakeup_pipe);
439 close_on_exec(wakeup_pipe[0], TRUE);
440 close_on_exec(wakeup_pipe[1], TRUE);
441 gdk_input_add(wakeup_pipe[0], GDK_INPUT_READ, wake_up_cb, NULL);
442 to_wakeup_pipe = wakeup_pipe[1];
444 /* If the pipe is full then we're going to get woken up anyway... */
445 set_blocking(to_wakeup_pipe, FALSE);
447 /* Let child processes die */
448 act.sa_handler = child_died;
449 sigemptyset(&act.sa_mask);
450 act.sa_flags = SA_NOCLDSTOP;
451 sigaction(SIGCHLD, &act, NULL);
453 /* Ignore SIGPIPE - check for EPIPE errors instead */
454 act.sa_handler = SIG_IGN;
455 sigemptyset(&act.sa_mask);
456 act.sa_flags = 0;
457 sigaction(SIGPIPE, &act, NULL);
459 /* Set up session managament if available */
460 session_init(client_id);
461 g_free(client_id);
463 reply = run_soap(rpc);
464 xmlFreeDoc(rpc);
465 if (reply)
467 save_xml_file(reply, "-");
468 xmlFreeDoc(reply);
471 if (number_of_windows > 0)
472 gtk_main();
474 return EXIT_SUCCESS;
477 /* Register a function to be called when process number 'child' dies. */
478 void on_child_death(gint child, CallbackFn callback, gpointer data)
480 Callback *cb;
482 g_return_if_fail(callback != NULL);
484 cb = g_new(Callback, 1);
486 cb->callback = callback;
487 cb->data = data;
489 g_hash_table_insert(death_callbacks, GINT_TO_POINTER(child), cb);
492 /****************************************************************
493 * INTERNAL FUNCTIONS *
494 ****************************************************************/
496 static void show_features(void)
498 g_printerr("\n-- %s --\n\n", _("features set at compile time"));
499 g_printerr("%s... %s\n", _("VFS support"),
500 #ifdef HAVE_LIBVFS
501 _("Yes")
502 #else
503 _("No (couldn't find a valid libvfs)")
504 #endif
508 static void soap_add(xmlNodePtr body,
509 xmlChar *function,
510 xmlChar *arg1_name, xmlChar *arg1_value,
511 xmlChar *arg2_name, xmlChar *arg2_value)
513 xmlNodePtr node;
514 xmlNs *rox;
516 rox = xmlSearchNsByHref(body->doc, body, ROX_NS);
518 node = xmlNewChild(body, rox, function, NULL);
520 if (arg1_name)
522 xmlNewTextChild(node, rox, arg1_name, arg1_value);
523 if (arg2_name)
524 xmlNewTextChild(node, rox, arg2_name, arg2_value);
528 /* This is called as a signal handler; simply ensures that
529 * child_died_callback() will get called later.
531 static void child_died(int signum)
533 child_died_flag = TRUE;
534 write(to_wakeup_pipe, "\0", 1); /* Wake up! */
537 static void child_died_callback(void)
539 int status;
540 gint child;
542 child_died_flag = FALSE;
544 /* Find out which children exited and allow them to die */
547 Callback *cb;
549 child = waitpid(-1, &status, WNOHANG);
551 if (child == 0 || child == -1)
552 return;
554 cb = g_hash_table_lookup(death_callbacks,
555 GINT_TO_POINTER(child));
556 if (cb)
558 cb->callback(cb->data);
559 g_hash_table_remove(death_callbacks,
560 GINT_TO_POINTER(child));
563 } while (1);
566 #define BUFLEN 40
567 /* When data is written to_wakeup_pipe, this gets called from the event
568 * loop some time later. Useful for getting out of signal handlers, etc.
570 static void wake_up_cb(gpointer data, gint source, GdkInputCondition condition)
572 char buf[BUFLEN];
574 read(source, buf, BUFLEN);
576 if (child_died_flag)
577 child_died_callback();