1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
/*
* Caja
*
* Copyright (C) 1999, 2000 Red Hat, Inc.
* Copyright (C) 1999, 2000 Eazel, Inc.
*
* Caja is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* Caja is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors: Elliot Lee <sopwith@redhat.com>,
* Darin Adler <darin@bentspoon.com>,
* John Sullivan <sullivan@eazel.com>
*
*/
/* caja-main.c: Implementation of the routines that drive program lifecycle and main window creation/destruction. */
#include <config.h>
#include "caja-main.h"
#include "caja-application.h"
#include "caja-self-check-functions.h"
#include "caja-window.h"
#include <dlfcn.h>
#include <signal.h>
#include <eel/eel-debug.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-self-checks.h>
#include <libegg/eggsmclient.h>
#include <libegg/eggdesktopfile.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
#include <libcaja-private/caja-debug-log.h>
#include <libcaja-private/caja-global-preferences.h>
#include <libcaja-private/caja-lib-self-check-functions.h>
#include <libcaja-private/caja-icon-names.h>
#include <libxml/parser.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_EXEMPI
#include <exempi/xmp.h>
#endif
/* Keeps track of everyone who wants the main event loop kept active */
static GSList* event_loop_registrants;
static gboolean exit_with_last_window = TRUE;
static gboolean is_event_loop_needed(void)
{
return event_loop_registrants != NULL || !exit_with_last_window;
}
static int quit_if_in_main_loop (gpointer callback_data)
{
guint level;
g_assert (callback_data == NULL);
level = gtk_main_level ();
/* We can be called even outside the main loop,
* so check that we are in a loop before calling quit.
*/
if (level != 0)
{
gtk_main_quit ();
}
/* We need to be called again if we quit a nested loop. */
return level > 1;
}
static void eel_gtk_main_quit_all (void)
{
/* Calling gtk_main_quit directly only kills the current/top event loop.
* This idler will be run by the current event loop, killing it, and then
* by the next event loop, ...
*/
g_idle_add (quit_if_in_main_loop, NULL);
}
static void event_loop_unregister (GtkWidget *object)
{
event_loop_registrants = g_slist_remove (event_loop_registrants, object);
if (!is_event_loop_needed ())
{
eel_gtk_main_quit_all ();
}
}
#if GTK_CHECK_VERSION(3, 0, 0)
void caja_main_event_loop_register (GtkWidget *object)
#else
void caja_main_event_loop_register (GtkObject *object)
#endif
{
g_signal_connect (object, "destroy", G_CALLBACK (event_loop_unregister), NULL);
event_loop_registrants = g_slist_prepend (event_loop_registrants, GTK_WIDGET (object));
}
gboolean caja_main_is_event_loop_mainstay (GtkWidget *object)
{
return g_slist_length (event_loop_registrants) == 1
&& event_loop_registrants->data == object;
}
void caja_main_event_loop_quit (gboolean explicit)
{
if (explicit)
{
/* Explicit --quit, make sure we don't restart */
/* To quit all instances, reset exit_with_last_window */
exit_with_last_window = TRUE;
if (event_loop_registrants == NULL)
{
/* If this is reached, caja must run in "daemon" mode
* (i.e. !exit_with_last_window) with no windows open.
* We need to quit_all here because the below loop won't
* trigger a quit.
*/
eel_gtk_main_quit_all();
}
/* TODO: With the old session we needed to set restart
style to MATE_RESTART_IF_RUNNING here, but i don't think we need
that now since mate-session doesn't restart apps except on startup. */
}
while (event_loop_registrants != NULL)
{
gtk_widget_destroy (event_loop_registrants->data);
}
}
static void dump_debug_log (void)
{
char *filename;
filename = g_build_filename (g_get_home_dir (), "caja-debug-log.txt", NULL);
caja_debug_log_dump (filename, NULL); /* NULL GError */
g_free (filename);
}
static int debug_log_pipes[2];
static gboolean debug_log_io_cb (GIOChannel *io, GIOCondition condition, gpointer data)
{
char a;
while (read (debug_log_pipes[0], &a, 1) != 1)
;
caja_debug_log (TRUE, CAJA_DEBUG_LOG_DOMAIN_USER,
"user requested dump of debug log");
dump_debug_log ();
return FALSE;
}
static void sigusr1_handler (int sig)
{
while (write (debug_log_pipes[1], "a", 1) != 1)
;
}
/* This is totally broken as we're using non-signal safe
* calls in sigfatal_handler. Disable by default. */
#ifdef USE_SEGV_HANDLER
/* sigaction structures for the old handlers of these signals */
static struct sigaction old_segv_sa;
static struct sigaction old_abrt_sa;
static struct sigaction old_trap_sa;
static struct sigaction old_fpe_sa;
static struct sigaction old_bus_sa;
static void
sigfatal_handler (int sig)
{
void (* func) (int);
/* FIXME: is this totally busted? We do malloc() inside these functions,
* and yet we are inside a signal handler...
*/
caja_debug_log (TRUE, CAJA_DEBUG_LOG_DOMAIN_USER,
"debug log dumped due to signal %d", sig);
dump_debug_log ();
switch (sig)
{
case SIGSEGV:
func = old_segv_sa.sa_handler;
break;
case SIGABRT:
func = old_abrt_sa.sa_handler;
break;
case SIGTRAP:
func = old_trap_sa.sa_handler;
break;
case SIGFPE:
func = old_fpe_sa.sa_handler;
break;
case SIGBUS:
func = old_bus_sa.sa_handler;
break;
default:
func = NULL;
break;
}
/* this scares me */
if (func != NULL && func != SIG_IGN && func != SIG_DFL)
(* func) (sig);
}
#endif
static void
setup_debug_log_signals (void)
{
struct sigaction sa;
GIOChannel *io;
if (pipe (debug_log_pipes) == -1)
g_error ("Could not create pipe() for debug log");
io = g_io_channel_unix_new (debug_log_pipes[0]);
g_io_add_watch (io, G_IO_IN, debug_log_io_cb, NULL);
sa.sa_handler = sigusr1_handler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigaction (SIGUSR1, &sa, NULL);
/* This is totally broken as we're using non-signal safe
* calls in sigfatal_handler. Disable by default. */
#ifdef USE_SEGV_HANDLER
sa.sa_handler = sigfatal_handler;
sigemptyset (&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGSEGV, &sa, &old_segv_sa);
sigaction(SIGABRT, &sa, &old_abrt_sa);
sigaction(SIGTRAP, &sa, &old_trap_sa);
sigaction(SIGFPE, &sa, &old_fpe_sa);
sigaction(SIGBUS, &sa, &old_bus_sa);
#endif
}
static GLogFunc default_log_handler;
static void
log_override_cb (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
gboolean is_debug;
gboolean is_milestone;
is_debug = ((log_level & G_LOG_LEVEL_DEBUG) != 0);
is_milestone = !is_debug;
caja_debug_log (is_milestone, CAJA_DEBUG_LOG_DOMAIN_GLOG, "%s", message);
if (!is_debug)
(* default_log_handler) (log_domain, log_level, message, user_data);
}
static void
setup_debug_log_glog (void)
{
default_log_handler = g_log_set_default_handler (log_override_cb, NULL);
}
static void
setup_debug_log (void)
{
char *config_filename;
config_filename = g_build_filename (g_get_home_dir (), "caja-debug-log.conf", NULL);
caja_debug_log_load_configuration (config_filename, NULL); /* NULL GError */
g_free (config_filename);
setup_debug_log_signals ();
setup_debug_log_glog ();
}
static gboolean
running_in_mate (void)
{
return (g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "MATE") == 0)
|| (g_strcmp0 (g_getenv ("XDG_SESSION_DESKTOP"), "MATE") == 0)
|| (g_strcmp0 (g_getenv ("DESKTOP_SESSION"), "MATE") == 0);
}
static gboolean
running_as_root (void)
{
return geteuid () == 0;
}
int
main (int argc, char *argv[])
{
gboolean kill_shell;
gboolean no_default_window;
gboolean browser_window;
gboolean no_desktop;
gboolean version;
gboolean autostart_mode;
const char *autostart_id;
gchar *geometry;
gchar **remaining;
gboolean perform_self_check;
CajaApplication *application;
GOptionContext *context;
GFile *file = NULL;
GFileInfo *fileinfo = NULL;
GAppInfo *appinfo = NULL;
char *uri = NULL;
char **uris = NULL;
GPtrArray *uris_array;
GError *error;
int i;
const GOptionEntry options[] =
{
#ifndef CAJA_OMIT_SELF_CHECK
{
"check", 'c', 0, G_OPTION_ARG_NONE, &perform_self_check,
N_("Perform a quick set of self-check tests."), NULL
},
#endif
{
"version", '\0', 0, G_OPTION_ARG_NONE, &version,
N_("Show the version of the program."), NULL
},
{
"geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry,
N_("Create the initial window with the given geometry."), N_("GEOMETRY")
},
{
"no-default-window", 'n', 0, G_OPTION_ARG_NONE, &no_default_window,
N_("Only create windows for explicitly specified URIs."), NULL
},
{
"no-desktop", '\0', 0, G_OPTION_ARG_NONE, &no_desktop,
N_("Do not manage the desktop (ignore the preference set in the preferences dialog)."), NULL
},
{
"browser", '\0', 0, G_OPTION_ARG_NONE, &browser_window,
N_("open a browser window."), NULL
},
{
"quit", 'q', 0, G_OPTION_ARG_NONE, &kill_shell,
N_("Quit Caja."), NULL
},
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining, NULL, N_("[URI...]") },
{ NULL }
};
#if defined (HAVE_MALLOPT) && defined(M_MMAP_THRESHOLD)
/* Caja uses lots and lots of small and medium size allocations,
* and then a few large ones for the desktop background. By default
* glibc uses a dynamic treshold for how large allocations should
* be mmaped. Unfortunately this triggers quickly for caja when
* it does the desktop background allocations, raising the limit
* such that a lot of temporary large allocations end up on the
* heap and are thus not returned to the OS. To fix this we set
* a hardcoded limit. I don't know what a good value is, but 128K
* was the old glibc static limit, lets use that.
*/
mallopt (M_MMAP_THRESHOLD, 128 *1024);
#endif
#if !GLIB_CHECK_VERSION (2, 42, 0)
/* This will be done by gtk+ later, but for now, force it to MATE */
g_desktop_app_info_set_desktop_env ("MATE");
#endif
if (g_getenv ("CAJA_DEBUG") != NULL)
{
eel_make_warnings_and_criticals_stop_in_debugger ();
}
/* Initialize gettext support */
bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
autostart_mode = FALSE;
autostart_id = g_getenv ("DESKTOP_AUTOSTART_ID");
if (autostart_id != NULL && *autostart_id != '\0')
{
autostart_mode = TRUE;
}
/* Get parameters. */
remaining = NULL;
geometry = NULL;
version = FALSE;
kill_shell = FALSE;
no_default_window = FALSE;
no_desktop = FALSE;
perform_self_check = FALSE;
browser_window = FALSE;
g_set_prgname ("caja");
#if GTK_CHECK_VERSION(3, 0, 0)
gdk_set_allowed_backends ("x11");
#endif
if (g_file_test (DATADIR "/applications/caja.desktop", G_FILE_TEST_EXISTS))
{
egg_set_desktop_file (DATADIR "/applications/caja.desktop");
}
context = g_option_context_new (_("\n\nBrowse the file system with the file manager"));
g_option_context_add_main_entries (context, options, NULL);
g_option_context_add_group (context, gtk_get_option_group (TRUE));
g_option_context_add_group (context, egg_sm_client_get_option_group ());
error = NULL;
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("Could not parse arguments: %s\n", error->message);
g_error_free (error);
return 1;
}
g_option_context_free (context);
if (version)
{
g_print ("MATE caja " PACKAGE_VERSION "\n");
return 0;
}
#ifdef HAVE_EXEMPI
xmp_init();
#endif
setup_debug_log ();
/* If in autostart mode (aka started by mate-session), we need to ensure
* caja starts with the correct options.
*/
if (autostart_mode)
{
no_default_window = TRUE;
no_desktop = FALSE;
}
else if (running_as_root () || !running_in_mate ())
{
/* do not manage desktop when running as root or on other desktops */
no_desktop = TRUE;
/* set smclient mode to "no restart" when running as root or on other desktops */
egg_sm_client_set_mode (EGG_SM_CLIENT_MODE_NO_RESTART);
}
if (perform_self_check && remaining != NULL)
{
/* translators: %s is an option (e.g. --check) */
fprintf (stderr, _("caja: %s cannot be used with URIs.\n"),
"--check");
return EXIT_FAILURE;
}
if (perform_self_check && kill_shell)
{
fprintf (stderr, _("caja: --check cannot be used with other options.\n"));
return EXIT_FAILURE;
}
if (kill_shell && remaining != NULL)
{
fprintf (stderr, _("caja: %s cannot be used with URIs.\n"),
"--quit");
return EXIT_FAILURE;
}
if (geometry != NULL && remaining != NULL && remaining[0] != NULL && remaining[1] != NULL)
{
fprintf (stderr, _("caja: --geometry cannot be used with more than one URI.\n"));
return EXIT_FAILURE;
}
/* Initialize the services that we use. */
LIBXML_TEST_VERSION
/* Initialize preferences. This is needed so that proper
* defaults are available before any preference peeking
* happens.
*/
caja_global_preferences_init ();
/* exit_with_last_window is already set to TRUE, and we need to keep that value
* on other desktops or when running caja as root. Otherwise, we read the value
* from the configuration.
*/
if (running_in_mate () && !running_as_root())
{
exit_with_last_window = g_settings_get_boolean (caja_preferences, CAJA_PREFERENCES_EXIT_WITH_LAST_WINDOW);
}
application = NULL;
/* Do either the self-check or the real work. */
if (perform_self_check)
{
#ifndef CAJA_OMIT_SELF_CHECK
/* Run the checks (each twice) for caja and libcaja-private. */
caja_run_self_checks ();
caja_run_lib_self_checks ();
eel_exit_if_self_checks_failed ();
caja_run_self_checks ();
caja_run_lib_self_checks ();
eel_exit_if_self_checks_failed ();
#endif
}
else
{
/* Convert args to URIs */
if (remaining != NULL)
{
uris_array = g_ptr_array_new ();
for (i = 0; remaining[i] != NULL; i++)
{
file = g_file_new_for_commandline_arg (remaining[i]);
if (file != NULL)
{
uri = g_file_get_uri (file);
if (uri)
{
fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (fileinfo && g_file_info_get_file_type(fileinfo) == G_FILE_TYPE_DIRECTORY)
{
g_ptr_array_add (uris_array, uri);
}
else
{
if (fileinfo)
g_object_unref (fileinfo);
fileinfo = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (fileinfo)
{
appinfo = g_app_info_get_default_for_type (g_file_info_get_content_type (fileinfo), TRUE);
if (appinfo)
{
if (g_strcmp0 (g_app_info_get_executable (appinfo), "caja") != 0)
{
g_app_info_launch_default_for_uri (uri, NULL, NULL);
}
else
{
fprintf (stderr, _("caja: set erroneously as default application for '%s' content type.\n"),
g_file_info_get_content_type (fileinfo));
}
g_object_unref (appinfo);
}
g_free (uri);
}
else
{
g_ptr_array_add (uris_array, uri);
}
}
if (fileinfo)
g_object_unref (fileinfo);
}
if (file)
g_object_unref (file);
}
}
if (uris_array->len == 0)
{
/* Caja is being used only to open files (not directories), so closing */
g_strfreev (remaining);
return EXIT_SUCCESS;
}
g_ptr_array_add (uris_array, NULL);
uris = (char**) g_ptr_array_free (uris_array, FALSE);
g_strfreev (remaining);
}
/* Run the caja application. */
application = caja_application_new ();
if (egg_sm_client_is_resumed (application->smclient))
{
no_default_window = TRUE;
}
caja_application_startup
(application,
kill_shell, no_default_window, no_desktop,
browser_window,
geometry,
uris);
g_strfreev (uris);
if (unique_app_is_running (application->unique_app) ||
kill_shell)
{
exit_with_last_window = TRUE;
}
if (is_event_loop_needed ())
{
gtk_main ();
}
}
caja_icon_info_clear_caches ();
if (application != NULL)
{
g_object_unref (application);
}
eel_debug_shut_down ();
caja_application_save_accel_map (NULL);
return EXIT_SUCCESS;
}
|