Merge merge requests 660, 658, 655 and 648
[geeqie.git] / src / main.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <gdk/gdkkeysyms.h> /* for keyboard values */
23
24 #include <signal.h>
25 #include <sys/mman.h>
26
27 #include <math.h>
28 #ifdef G_OS_UNIX
29 #include <pwd.h>
30 #endif
31 #include <locale.h>
32
33 #include "main.h"
34
35 #include "cache.h"
36 #include "collect.h"
37 #include "collect-io.h"
38 #include "filedata.h"
39 #include "filefilter.h"
40 #include "history_list.h"
41 #include "image-overlay.h"
42 #include "layout.h"
43 #include "layout_image.h"
44 #include "layout_util.h"
45 #include "options.h"
46 #include "remote.h"
47 #include "secure_save.h"
48 #include "similar.h"
49 #include "ui_fileops.h"
50 #include "ui_utildlg.h"
51 #include "cache_maint.h"
52 #include "thumb.h"
53 #include "metadata.h"
54 #include "editors.h"
55 #include "exif.h"
56 #include "histogram.h"
57 #include "pixbuf_util.h"
58 #include "glua.h"
59
60 #ifdef HAVE_CLUTTER
61 #include <clutter-gtk/clutter-gtk.h>
62 #endif
63
64
65 gboolean thumb_format_changed = FALSE;
66 static RemoteConnection *remote_connection = NULL;
67
68 /*
69  *-----------------------------------------------------------------------------
70  * keyboard functions
71  *-----------------------------------------------------------------------------
72  */
73
74 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
75 {
76         static gint delta = 0;
77         static guint32 time_old = 0;
78         static guint keyval_old = 0;
79
80         if (event->state & GDK_CONTROL_MASK)
81                 {
82                 if (*x < 0) *x = G_MININT / 2;
83                 if (*x > 0) *x = G_MAXINT / 2;
84                 if (*y < 0) *y = G_MININT / 2;
85                 if (*y > 0) *y = G_MAXINT / 2;
86
87                 return;
88                 }
89
90         if (options->progressive_key_scrolling)
91                 {
92                 guint32 time_diff;
93
94                 time_diff = event->time - time_old;
95
96                 /* key pressed within 125ms ? (1/8 second) */
97                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
98
99                 time_old = event->time;
100                 keyval_old = event->keyval;
101
102                 delta += 2;
103                 }
104         else
105                 {
106                 delta = 8;
107                 }
108
109         *x = *x * delta * options->keyboard_scroll_step;
110         *y = *y * delta * options->keyboard_scroll_step;
111 }
112
113
114
115 /*
116  *-----------------------------------------------------------------------------
117  * command line parser (private) hehe, who needs popt anyway?
118  *-----------------------------------------------------------------------------
119  */
120
121 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
122                                         GList **list, GList **collection_list)
123 {
124         gchar *path_parsed;
125
126         path_parsed = g_strdup(file_path);
127         parse_out_relatives(path_parsed);
128
129         if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
130                 {
131                 *collection_list = g_list_append(*collection_list, path_parsed);
132                 }
133         else
134                 {
135                 if (!*path) *path = remove_level_from_path(path_parsed);
136                 if (!*file) *file = g_strdup(path_parsed);
137                 *list = g_list_prepend(*list, file_data_new_group(path_parsed));
138                 }
139 }
140
141 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
142                                        GList **list)
143 {
144 #if 0
145         /* This is broken because file filter is not initialized yet.
146         */
147         GList *files;
148         gchar *path_parsed;
149         FileData *dir_fd;
150
151         path_parsed = g_strdup(dir);
152         parse_out_relatives(path_parsed);
153         dir_fd = file_data_new_dir(path_parsed);
154
155
156         if (filelist_read(dir_fd, &files, NULL))
157                 {
158                 GList *work;
159
160                 files = filelist_filter(files, FALSE);
161                 files = filelist_sort_path(files);
162
163                 work = files;
164                 while (work)
165                         {
166                         FileData *fd = work->data;
167                         if (!*path) *path = remove_level_from_path(fd->path);
168                         if (!*file) *file = g_strdup(fd->path);
169                         *list = g_list_prepend(*list, fd);
170
171                         work = work->next;
172                         }
173
174                 g_list_free(files);
175                 }
176
177         g_free(path_parsed);
178         file_data_unref(dir_fd);
179 #else
180         DEBUG_1("multiple directories specified, ignoring: %s", dir);
181 #endif
182 }
183
184 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
185                                            GList **list, gchar **first_dir)
186 {
187
188         if (!*list && !*first_dir)
189                 {
190                 *first_dir = g_strdup(dir);
191                 }
192         else
193                 {
194                 if (*first_dir)
195                         {
196                         parse_command_line_add_dir(*first_dir, path, file, list);
197                         g_free(*first_dir);
198                         *first_dir = NULL;
199                         }
200                 parse_command_line_add_dir(dir, path, file, list);
201                 }
202 }
203
204 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
205                                             GList **list, GList **collection_list, gchar **first_dir)
206 {
207
208         if (*first_dir)
209                 {
210                 parse_command_line_add_dir(*first_dir, path, file, list);
211                 g_free(*first_dir);
212                 *first_dir = NULL;
213                 }
214         parse_command_line_add_file(file_path, path, file, list, collection_list);
215 }
216
217 static void parse_command_line(gint argc, gchar *argv[])
218 {
219         GList *list = NULL;
220         GList *remote_list = NULL;
221         GList *remote_errors = NULL;
222         gboolean remote_do = FALSE;
223         gchar *first_dir = NULL;
224         gchar *app_lock;
225         gchar *pwd;
226         gchar *current_dir;
227
228         command_line = g_new0(CommandLine, 1);
229
230         command_line->argc = argc;
231         command_line->argv = argv;
232         command_line->regexp = NULL;
233
234         if (argc > 1)
235                 {
236                 gint i;
237                 gchar *base_dir = get_current_dir();
238                 i = 1;
239                 while (i < argc)
240                         {
241                         gchar *cmd_line = path_to_utf8(argv[i]);
242                         gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
243
244                         if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
245                                 {
246                                 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
247                                 }
248                         else if (isdir(cmd_all))
249                                 {
250                                 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
251                                 }
252                         else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
253                                 {
254                                 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
255                                                                 &list, &command_line->collection_list, &first_dir);
256                                 }
257                         else if (isfile(cmd_all))
258                                 {
259                                 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
260                                                                 &list, &command_line->collection_list, &first_dir);
261                                 }
262                         else if (is_collection(cmd_line))
263                                 {
264                                 gchar *path = NULL;
265
266                                 path = collection_path(cmd_line);
267                                 parse_command_line_process_file(path, &command_line->path, &command_line->file,
268                                                                 &list, &command_line->collection_list, &first_dir);
269                                 g_free(path);
270                                 }
271                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
272                                 {
273                                 /* do nothing but do not produce warnings */
274                                 }
275                         else if (strcmp(cmd_line, "+t") == 0 ||
276                                  strcmp(cmd_line, "--with-tools") == 0)
277                                 {
278                                 command_line->tools_show = TRUE;
279
280                                 remote_list = g_list_append(remote_list, "+t");
281                                 }
282                         else if (strcmp(cmd_line, "-t") == 0 ||
283                                  strcmp(cmd_line, "--without-tools") == 0)
284                                 {
285                                 command_line->tools_hide = TRUE;
286
287                                 remote_list = g_list_append(remote_list, "-t");
288                                 }
289                         else if (strcmp(cmd_line, "-f") == 0 ||
290                                  strcmp(cmd_line, "--fullscreen") == 0)
291                                 {
292                                 command_line->startup_full_screen = TRUE;
293                                 }
294                         else if (strcmp(cmd_line, "-s") == 0 ||
295                                  strcmp(cmd_line, "--slideshow") == 0)
296                                 {
297                                 command_line->startup_in_slideshow = TRUE;
298                                 }
299                         else if (strcmp(cmd_line, "-l") == 0 ||
300                                  strcmp(cmd_line, "--list") == 0)
301                                 {
302                                 command_line->startup_command_line_collection = TRUE;
303                                 }
304                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
305                                 {
306                                 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
307                                 }
308                         else if (strcmp(cmd_line, "-r") == 0 ||
309                                  strcmp(cmd_line, "--remote") == 0)
310                                 {
311                                 if (!remote_do)
312                                         {
313                                         remote_do = TRUE;
314                                         remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
315                                         }
316                                 }
317                         else if ((strcmp(cmd_line, "+w") == 0) ||
318                                                 strcmp(cmd_line, "--show-log-window") == 0)
319                                 {
320                                 command_line->log_window_show = TRUE;
321                                 }
322                         else if (strncmp(cmd_line, "-o:", 3) == 0)
323                                 {
324                                 command_line->log_file = g_strdup(cmd_line + 3);
325                                 }
326                         else if (strncmp(cmd_line, "--log-file:", 11) == 0)
327                                 {
328                                 command_line->log_file = g_strdup(cmd_line + 11);
329                                 }
330                         else if (strncmp(cmd_line, "-g:", 3) == 0)
331                                 {
332                                 set_regexp(g_strdup(cmd_line+3));
333                                 }
334                         else if (strncmp(cmd_line, "-grep:", 6) == 0)
335                                 {
336                                 set_regexp(g_strdup(cmd_line+3));
337                                 }
338                         else if (strcmp(cmd_line, "-rh") == 0 ||
339                                  strcmp(cmd_line, "--remote-help") == 0)
340                                 {
341                                 remote_help();
342                                 exit(0);
343                                 }
344                         else if (strcmp(cmd_line, "--blank") == 0)
345                                 {
346                                 command_line->startup_blank = TRUE;
347                                 }
348                         else if (strcmp(cmd_line, "-v") == 0 ||
349                                  strcmp(cmd_line, "--version") == 0)
350                                 {
351                                 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
352                                 exit(0);
353                                 }
354                         else if (strcmp(cmd_line, "--alternate") == 0)
355                                 {
356                                 /* enable faster experimental algorithm */
357                                 log_printf("Alternate similarity algorithm enabled\n");
358                                 image_sim_alternate_set(TRUE);
359                                 }
360                         else if (strcmp(cmd_line, "-h") == 0 ||
361                                  strcmp(cmd_line, "--help") == 0)
362                                 {
363                                 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
364                                 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
365                                 print_term(FALSE, _("valid options are:\n"));
366                                 print_term(FALSE, _("  +t, --with-tools                 force show of tools\n"));
367                                 print_term(FALSE, _("  -t, --without-tools              force hide of tools\n"));
368                                 print_term(FALSE, _("  -f, --fullscreen                 start in full screen mode\n"));
369                                 print_term(FALSE, _("  -s, --slideshow                  start in slideshow mode\n"));
370                                 print_term(FALSE, _("  -l, --list [files] [collections] open collection window for command line\n"));
371                                 print_term(FALSE, _("      --blank                      start with blank file list\n"));
372                                 print_term(FALSE, _("      --geometry=XxY+XOFF+YOFF     set main window location\n"));
373                                 print_term(FALSE, _("  -r, --remote                     send following commands to open window\n"));
374                                 print_term(FALSE, _("  -rh,--remote-help                print remote command list\n"));
375 #ifdef DEBUG
376                                 print_term(FALSE, _("      --debug[=level]              turn on debug output\n"));
377                                 print_term(FALSE, _("  -g:<regexp>, --grep:<regexp>     filter debug output\n"));
378 #endif
379                                 print_term(FALSE, _("  +w, --show-log-window            show log window\n"));
380                                 print_term(FALSE, _("  -o:<file>, --log-file:<file>     save log data to file\n"));
381                                 print_term(FALSE, _("  -v, --version                    print version info\n"));
382                                 print_term(FALSE, _("  -h, --help                       show this message\n\n"));
383
384 #if 0
385                                 /* these options are not officially supported!
386                                  * only for testing new features, no need to translate them */
387                                 print_term(FALSE, "  --alternate                use alternate similarity algorithm\n");
388 #endif
389
390
391                                 exit(0);
392                                 }
393                         else if (!remote_do)
394                                 {
395                                 printf_term(TRUE, _("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
396                                 }
397
398                         g_free(cmd_all);
399                         g_free(cmd_line);
400                         i++;
401                         }
402                 g_free(base_dir);
403                 parse_out_relatives(command_line->path);
404                 parse_out_relatives(command_line->file);
405                 }
406
407         list = g_list_reverse(list);
408
409         if (!command_line->path && first_dir)
410                 {
411                 command_line->path = first_dir;
412                 first_dir = NULL;
413
414                 parse_out_relatives(command_line->path);
415                 }
416         g_free(first_dir);
417
418         /* If Geeqie is already running, prevent a second instance
419          * from being started. Open a new window instead.
420          */
421         app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
422         if (remote_server_exists(app_lock) && !remote_do)
423                 {
424                 remote_do = TRUE;
425                 remote_list = g_list_append(remote_list, "--new-window");
426         }
427         g_free(app_lock);
428
429         if (remote_do)
430                 {
431                 if (remote_errors)
432                         {
433                         GList *work = remote_errors;
434
435                         printf_term(TRUE,_("Invalid or ignored remote options: "));
436                         while (work)
437                                 {
438                                 gchar *opt = work->data;
439
440                                 printf_term(TRUE, "%s%s", (work == remote_errors) ? "" : ", ", opt);
441                                 work = work->next;
442                                 }
443
444                         printf_term(TRUE, _("\nUse --remote-help for valid remote options.\n"));
445                         }
446
447                 /* prepend the current dir the remote command was made from,
448                  * for use by any remote command that needs it
449                  */
450                 current_dir = g_get_current_dir();
451                 pwd = g_strconcat("--PWD:", current_dir, NULL);
452                 remote_list = g_list_prepend(remote_list, pwd);
453
454                 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
455                 g_free(pwd);
456                 g_free(current_dir);
457                 }
458         g_list_free(remote_list);
459
460         if (list && list->next)
461                 {
462                 command_line->cmd_list = list;
463                 }
464         else
465                 {
466                 filelist_free(list);
467                 command_line->cmd_list = NULL;
468                 }
469
470         if (command_line->startup_blank)
471                 {
472                 g_free(command_line->path);
473                 command_line->path = NULL;
474                 g_free(command_line->file);
475                 command_line->file = NULL;
476                 filelist_free(command_line->cmd_list);
477                 command_line->cmd_list = NULL;
478                 string_list_free(command_line->collection_list);
479                 command_line->collection_list = NULL;
480                 }
481 }
482
483 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
484 {
485 #ifdef DEBUG
486         const gchar *debug_option = "--debug";
487         gint len = strlen(debug_option);
488
489         if (argc > 1)
490                 {
491                 gint i;
492
493                 for (i = 1; i < argc; i++)
494                         {
495                         const gchar *cmd_line = argv[i];
496                         if (strncmp(cmd_line, debug_option, len) == 0)
497                                 {
498                                 gint cmd_line_len = strlen(cmd_line);
499
500                                 /* we now increment the debug state for verbosity */
501                                 if (cmd_line_len == len)
502                                         debug_level_add(1);
503                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
504                                         {
505                                         gint n = atoi(cmd_line + len + 1);
506                                         if (n < 0) n = 1;
507                                         debug_level_add(n);
508                                         }
509                                 }
510                         }
511                 }
512
513         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
514 #endif
515 }
516
517 /*
518  *-----------------------------------------------------------------------------
519  * startup, init, and exit
520  *-----------------------------------------------------------------------------
521  */
522
523 #define RC_HISTORY_NAME "history"
524 #define RC_MARKS_NAME "marks"
525
526 static void setup_env_path(void)
527 {
528         const gchar *old_path = g_getenv("PATH");
529         gchar *path = g_strconcat(GQ_BIN_DIR, ":", old_path, NULL);
530         g_setenv("PATH", path, TRUE);
531         g_free(path);
532 }
533
534 static void keys_load(void)
535 {
536         gchar *path;
537
538         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
539         history_list_load(path);
540         g_free(path);
541 }
542
543 static void keys_save(void)
544 {
545         gchar *path;
546
547         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
548         history_list_save(path);
549         g_free(path);
550 }
551
552 static void marks_load(void)
553 {
554         gchar *path;
555
556         path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
557         marks_list_load(path);
558         g_free(path);
559 }
560
561 static void marks_save(gboolean save)
562 {
563         gchar *path;
564
565         path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
566         marks_list_save(path, save);
567         g_free(path);
568 }
569
570 static void mkdir_if_not_exists(const gchar *path)
571 {
572         if (isdir(path)) return;
573
574         log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
575
576         if (!recursive_mkdir_if_not_exists(path, 0755))
577                 {
578                 log_printf(_("Could not create dir:%s\n"), path);
579                 }
580 }
581
582
583 /* We add to duplicate and modify  gtk_accel_map_print() and gtk_accel_map_save()
584  * to improve the reliability in special cases (especially when disk is full)
585  * These functions are now using secure saving stuff.
586  */
587 static void gq_accel_map_print(
588                     gpointer    data,
589                     const gchar *accel_path,
590                     guint       accel_key,
591                     GdkModifierType accel_mods,
592                     gboolean    changed)
593 {
594         GString *gstring = g_string_new(changed ? NULL : "; ");
595         SecureSaveInfo *ssi = data;
596         gchar *tmp, *name;
597
598         g_string_append(gstring, "(gtk_accel_path \"");
599
600         tmp = g_strescape(accel_path, NULL);
601         g_string_append(gstring, tmp);
602         g_free(tmp);
603
604         g_string_append(gstring, "\" \"");
605
606         name = gtk_accelerator_name(accel_key, accel_mods);
607         tmp = g_strescape(name, NULL);
608         g_free(name);
609         g_string_append(gstring, tmp);
610         g_free(tmp);
611
612         g_string_append(gstring, "\")\n");
613
614         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
615
616         g_string_free(gstring, TRUE);
617 }
618
619 static gboolean gq_accel_map_save(const gchar *path)
620 {
621         gchar *pathl;
622         SecureSaveInfo *ssi;
623         GString *gstring;
624
625         pathl = path_from_utf8(path);
626         ssi = secure_open(pathl);
627         g_free(pathl);
628         if (!ssi)
629                 {
630                 log_printf(_("error saving file: %s\n"), path);
631                 return FALSE;
632                 }
633
634         gstring = g_string_new("; ");
635         if (g_get_prgname())
636                 g_string_append(gstring, g_get_prgname());
637         g_string_append(gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
638         g_string_append(gstring, "; this file is an automated accelerator map dump\n");
639         g_string_append(gstring, ";\n");
640
641         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
642
643         g_string_free(gstring, TRUE);
644
645         gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
646
647         if (secure_close(ssi))
648                 {
649                 log_printf(_("error saving file: %s\nerror: %s\n"), path,
650                            secsave_strerror(secsave_errno));
651                 return FALSE;
652                 }
653
654         return TRUE;
655 }
656
657 static gchar *accep_map_filename(void)
658 {
659         return g_build_filename(get_rc_dir(), "accels", NULL);
660 }
661
662 static void accel_map_save(void)
663 {
664         gchar *path;
665
666         path = accep_map_filename();
667         gq_accel_map_save(path);
668         g_free(path);
669 }
670
671 static void accel_map_load(void)
672 {
673         gchar *path;
674         gchar *pathl;
675
676         path = accep_map_filename();
677         pathl = path_from_utf8(path);
678         gtk_accel_map_load(pathl);
679         g_free(pathl);
680         g_free(path);
681 }
682
683 static void gtkrc_load(void)
684 {
685         gchar *path;
686         gchar *pathl;
687
688         /* If a gtkrc file exists in the rc directory, add it to the
689          * list of files to be parsed at the end of gtk_init() */
690         path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
691         pathl = path_from_utf8(path);
692         if (access(pathl, R_OK) == 0)
693                 gtk_rc_add_default_file(pathl);
694         g_free(pathl);
695         g_free(path);
696 }
697
698 static void exit_program_final(void)
699 {
700         LayoutWindow *lw = NULL;
701
702          /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
703         layout_editors_reload_finish();
704
705         remote_close(remote_connection);
706
707         collect_manager_flush();
708
709         save_options(options);
710         keys_save();
711         accel_map_save();
712
713         if (layout_valid(&lw))
714                 {
715                 layout_free(lw);
716                 }
717
718         secure_close(command_line->ssi);
719
720         gtk_main_quit();
721 }
722
723 static GenericDialog *exit_dialog = NULL;
724
725 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
726 {
727         exit_dialog = NULL;
728         generic_dialog_close(gd);
729 }
730
731 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
732 {
733         exit_dialog = NULL;
734         generic_dialog_close(gd);
735         exit_program_final();
736 }
737
738 static gint exit_confirm_dlg(void)
739 {
740         GtkWidget *parent;
741         LayoutWindow *lw;
742         gchar *msg;
743
744         if (exit_dialog)
745                 {
746                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
747                 return TRUE;
748                 }
749
750         if (!collection_window_modified_exists()) return FALSE;
751
752         parent = NULL;
753         lw = NULL;
754         if (layout_valid(&lw))
755                 {
756                 parent = lw->window;
757                 }
758
759         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
760         exit_dialog = generic_dialog_new(msg,
761                                 "exit", parent, FALSE,
762                                 exit_confirm_cancel_cb, NULL);
763         g_free(msg);
764         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
765         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
766                                    msg, _("Collections have been modified. Quit anyway?"), TRUE);
767         g_free(msg);
768         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
769
770         gtk_widget_show(exit_dialog->dialog);
771
772         return TRUE;
773 }
774
775 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
776 {
777         if (success) exit_program();
778 }
779
780 void exit_program(void)
781 {
782         layout_image_full_screen_stop(NULL);
783
784         if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
785
786         options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
787
788         if (exit_confirm_dlg()) return;
789
790         exit_program_final();
791 }
792
793 /* This code is supposed to handle situation when a file mmaped by image_loader
794  * or by exif loader is truncated by some other process.
795  * This is probably not completely correct according to posix, because
796  * mmap is not in the list of calls that can be used safely in signal handler,
797  * but anyway, the handler is called in situation when the application would
798  * crash otherwise.
799  * Ideas for improvement are welcome ;)
800  */
801 /* FIXME: this probably needs some better ifdefs. Please report any compilation problems */
802
803 #if defined(SIGBUS) && defined(SA_SIGINFO)
804 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
805 {
806         unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
807         DEBUG_1("SIGBUS %p", info->si_addr);
808         mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
809 }
810 #endif
811
812 static void setup_sigbus_handler(void)
813 {
814 #if defined(SIGBUS) && defined(SA_SIGINFO)
815         struct sigaction sigbus_action;
816         sigfillset(&sigbus_action.sa_mask);
817         sigbus_action.sa_sigaction = sigbus_handler_cb;
818         sigbus_action.sa_flags = SA_SIGINFO;
819
820         sigaction(SIGBUS, &sigbus_action, NULL);
821 #endif
822 }
823
824 gint main(gint argc, gchar *argv[])
825 {
826         CollectionData *first_collection = NULL;
827         gchar *buf;
828         CollectionData *cd = NULL;
829         gchar *app_lock;
830
831 #ifdef HAVE_GTHREAD
832 #if !GLIB_CHECK_VERSION(2,32,0)
833         g_thread_init(NULL);
834 #endif
835         gdk_threads_init();
836         gdk_threads_enter();
837
838 #endif
839
840         /* init execution time counter (debug only) */
841         init_exec_time();
842
843         /* setup locale, i18n */
844         setlocale(LC_ALL, "");
845
846 #ifdef ENABLE_NLS
847         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
848         bind_textdomain_codeset(PACKAGE, "UTF-8");
849         textdomain(PACKAGE);
850 #endif
851
852         exif_init();
853
854 #ifdef HAVE_LUA
855         lua_init();
856 #endif
857
858         /* setup random seed for random slideshow */
859         srand(time(NULL));
860
861         setup_sigbus_handler();
862
863         /* register global notify functions */
864         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
865         file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
866         file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
867         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
868         file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
869
870
871         gtkrc_load();
872
873         parse_command_line_for_debug_option(argc, argv);
874         DEBUG_1("%s main: gtk_init", get_exec_time());
875 #ifdef HAVE_CLUTTER
876         if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
877                 {
878                 log_printf("Can't initialize clutter-gtk.\n");
879                 exit(1);
880                 }
881 #else
882         gtk_init(&argc, &argv);
883 #endif
884
885         if (gtk_major_version < GTK_MAJOR_VERSION ||
886             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
887                 {
888                 log_printf("!!! This is a friendly warning.\n");
889                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
890                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
891                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
892                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
893                 }
894
895         DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
896         pixbuf_inline_register_stock_icons();
897
898         DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
899         options = init_options(NULL);
900         setup_default_options(options);
901
902         DEBUG_1("%s main: parse_command_line", get_exec_time());
903         parse_command_line(argc, argv);
904
905         DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
906         /* these functions don't depend on config file */
907         mkdir_if_not_exists(get_rc_dir());
908         mkdir_if_not_exists(get_collections_dir());
909         mkdir_if_not_exists(get_thumbnails_cache_dir());
910         mkdir_if_not_exists(get_metadata_cache_dir());
911
912         setup_env_path();
913
914         keys_load();
915         accel_map_load();
916
917         /* restore session from the config file */
918
919
920         DEBUG_1("%s main: load_options", get_exec_time());
921         if (!load_options(options))
922                 {
923                 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
924                 filter_add_defaults();
925                 filter_rebuild();
926                 }
927
928         /* handle missing config file and commandline additions*/
929         if (!layout_window_list)
930                 {
931                 /* broken or no config file */
932                 layout_new_from_config(NULL, NULL, TRUE);
933                 }
934
935         layout_editors_reload_start();
936
937         if (command_line->collection_list && !command_line->startup_command_line_collection)
938                 {
939                 GList *work;
940
941                 work = command_line->collection_list;
942                 while (work)
943                         {
944                         CollectWindow *cw;
945                         const gchar *path;
946
947                         path = work->data;
948                         work = work->next;
949
950                         cw = collection_window_new(path);
951                         if (!first_collection && cw) first_collection = cw->cd;
952                         }
953                 }
954
955         if (command_line->log_file)
956                 {
957                 gchar *pathl;
958                 gchar *path = g_strdup(command_line->log_file);
959
960                 pathl = path_from_utf8(path);
961                 command_line->ssi = secure_open(pathl);
962                 }
963
964         if (command_line->cmd_list ||
965             (command_line->startup_command_line_collection && command_line->collection_list))
966                 {
967                 GList *work;
968
969                 if (command_line->startup_command_line_collection)
970                         {
971                         CollectWindow *cw;
972
973                         cw = collection_window_new("");
974                         cd = cw->cd;
975                         }
976                 else
977                         {
978                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
979                         }
980
981                 g_free(cd->path);
982                 cd->path = NULL;
983                 g_free(cd->name);
984                 cd->name = g_strdup(_("Command line"));
985
986                 collection_path_changed(cd);
987
988                 work = command_line->cmd_list;
989                 while (work)
990                         {
991                         collection_add(cd, (FileData *)work->data, FALSE);
992                         work = work->next;
993                         }
994
995                 work = command_line->collection_list;
996                 while (work)
997                         {
998                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
999                         work = work->next;
1000                         }
1001
1002                 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1003
1004                 /* mem leak, we never unref this collection when !startup_command_line_collection
1005                  * (the image view of the main window does not hold a ref to the collection)
1006                  * this is sort of unavoidable, for if it did hold a ref, next/back
1007                  * may not work as expected when closing collection windows.
1008                  *
1009                  * collection_unref(cd);
1010                  */
1011
1012                 }
1013         else if (first_collection)
1014                 {
1015                 layout_image_set_collection(NULL, first_collection,
1016                                             collection_get_first(first_collection));
1017                 }
1018
1019         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1020         remote_connection = remote_server_init(buf, cd);
1021         g_free(buf);
1022
1023         marks_load();
1024
1025         DEBUG_1("%s main: gtk_main", get_exec_time());
1026         gtk_main();
1027 #ifdef HAVE_GTHREAD
1028         gdk_threads_leave();
1029 #endif
1030         return 0;
1031 }
1032 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */