Collections on command line
[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 gboolean is_collection(gchar *cmd_param)
218 {
219         gchar *path = NULL;
220         gchar *full_name = NULL;
221         gboolean result = FALSE;
222
223         if (file_extension_match(cmd_param, GQ_COLLECTION_EXT))
224                 {
225                 path = g_build_filename(get_collections_dir(), cmd_param, NULL);
226                 }
227         else if (file_extension_match(cmd_param, NULL))
228                 {
229                 full_name = g_strconcat(cmd_param, GQ_COLLECTION_EXT, NULL);
230                 path = g_build_filename(get_collections_dir(), full_name, NULL);
231                 }
232
233         if (isfile(path))
234                 {
235                 result = TRUE;
236                 }
237
238         g_free(path);
239         g_free(full_name);
240         return result;
241 }
242
243 static void parse_command_line(gint argc, gchar *argv[])
244 {
245         GList *list = NULL;
246         GList *remote_list = NULL;
247         GList *remote_errors = NULL;
248         gboolean remote_do = FALSE;
249         gchar *first_dir = NULL;
250
251         command_line = g_new0(CommandLine, 1);
252
253         command_line->argc = argc;
254         command_line->argv = argv;
255         command_line->regexp = NULL;
256
257         if (argc > 1)
258                 {
259                 gint i;
260                 gchar *base_dir = get_current_dir();
261                 i = 1;
262                 while (i < argc)
263                         {
264                         gchar *cmd_line = path_to_utf8(argv[i]);
265                         gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
266
267                         if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
268                                 {
269                                 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
270                                 }
271                         else if (isdir(cmd_all))
272                                 {
273                                 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
274                                 }
275                         else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
276                                 {
277                                 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
278                                                                 &list, &command_line->collection_list, &first_dir);
279                                 }
280                         else if (isfile(cmd_all))
281                                 {
282                                 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
283                                                                 &list, &command_line->collection_list, &first_dir);
284                                 }
285                         else if (is_collection(cmd_line))
286                                 {
287                                 gchar *path = NULL;
288                                 gchar *full_name = NULL;
289
290                                 if (file_extension_match(cmd_line, GQ_COLLECTION_EXT))
291                                         {
292                                         path = g_build_filename(get_collections_dir(), cmd_line, NULL);
293                                         }
294                                 else
295                                         {
296                                         full_name = g_strconcat(cmd_line, GQ_COLLECTION_EXT, NULL);
297                                         path = g_build_filename(get_collections_dir(), full_name, NULL);
298                                         }
299                                 parse_command_line_process_file(path, &command_line->path, &command_line->file,
300                                                                 &list, &command_line->collection_list, &first_dir);
301                                 g_free(path);
302                                 g_free(full_name);
303                                 }
304                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
305                                 {
306                                 /* do nothing but do not produce warnings */
307                                 }
308                         else if (strcmp(cmd_line, "+t") == 0 ||
309                                  strcmp(cmd_line, "--with-tools") == 0)
310                                 {
311                                 command_line->tools_show = TRUE;
312
313                                 remote_list = g_list_append(remote_list, "+t");
314                                 }
315                         else if (strcmp(cmd_line, "-t") == 0 ||
316                                  strcmp(cmd_line, "--without-tools") == 0)
317                                 {
318                                 command_line->tools_hide = TRUE;
319
320                                 remote_list = g_list_append(remote_list, "-t");
321                                 }
322                         else if (strcmp(cmd_line, "-f") == 0 ||
323                                  strcmp(cmd_line, "--fullscreen") == 0)
324                                 {
325                                 command_line->startup_full_screen = TRUE;
326                                 }
327                         else if (strcmp(cmd_line, "-s") == 0 ||
328                                  strcmp(cmd_line, "--slideshow") == 0)
329                                 {
330                                 command_line->startup_in_slideshow = TRUE;
331                                 }
332                         else if (strcmp(cmd_line, "-l") == 0 ||
333                                  strcmp(cmd_line, "--list") == 0)
334                                 {
335                                 command_line->startup_command_line_collection = TRUE;
336                                 }
337                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
338                                 {
339                                 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
340                                 }
341                         else if (strcmp(cmd_line, "-r") == 0 ||
342                                  strcmp(cmd_line, "--remote") == 0)
343                                 {
344                                 if (!remote_do)
345                                         {
346                                         remote_do = TRUE;
347                                         remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
348                                         }
349                                 }
350                         else if ((strcmp(cmd_line, "+w") == 0) ||
351                                                 strcmp(cmd_line, "--show-log-window") == 0)
352                                 {
353                                 command_line->log_window_show = TRUE;
354                                 }
355                         else if (strncmp(cmd_line, "-o:", 3) == 0)
356                                 {
357                                 command_line->log_file = g_strdup(cmd_line + 3);
358                                 }
359                         else if (strncmp(cmd_line, "--log-file:", 11) == 0)
360                                 {
361                                 command_line->log_file = g_strdup(cmd_line + 11);
362                                 }
363                         else if (strncmp(cmd_line, "-g:", 3) == 0)
364                                 {
365                                 set_regexp(g_strdup(cmd_line+3));
366                                 }
367                         else if (strncmp(cmd_line, "-grep:", 6) == 0)
368                                 {
369                                 set_regexp(g_strdup(cmd_line+3));
370                                 }
371                         else if (strcmp(cmd_line, "-rh") == 0 ||
372                                  strcmp(cmd_line, "--remote-help") == 0)
373                                 {
374                                 remote_help();
375                                 exit(0);
376                                 }
377                         else if (strcmp(cmd_line, "--blank") == 0)
378                                 {
379                                 command_line->startup_blank = TRUE;
380                                 }
381                         else if (strcmp(cmd_line, "-v") == 0 ||
382                                  strcmp(cmd_line, "--version") == 0)
383                                 {
384                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
385                                 exit(0);
386                                 }
387                         else if (strcmp(cmd_line, "--alternate") == 0)
388                                 {
389                                 /* enable faster experimental algorithm */
390                                 log_printf("Alternate similarity algorithm enabled\n");
391                                 image_sim_alternate_set(TRUE);
392                                 }
393                         else if (strcmp(cmd_line, "-h") == 0 ||
394                                  strcmp(cmd_line, "--help") == 0)
395                                 {
396                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
397                                 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
398                                 print_term(_("valid options are:\n"));
399                                 print_term(_("  +t, --with-tools                 force show of tools\n"));
400                                 print_term(_("  -t, --without-tools              force hide of tools\n"));
401                                 print_term(_("  -f, --fullscreen                 start in full screen mode\n"));
402                                 print_term(_("  -s, --slideshow                  start in slideshow mode\n"));
403                                 print_term(_("  -l, --list [files] [collections] open collection window for command line\n"));
404                                 print_term(_("      --blank                      start with blank file list\n"));
405                                 print_term(_("      --geometry=XxY+XOFF+YOFF     set main window location\n"));
406                                 print_term(_("  -r, --remote                     send following commands to open window\n"));
407                                 print_term(_("  -rh,--remote-help                print remote command list\n"));
408 #ifdef DEBUG
409                                 print_term(_("      --debug[=level]              turn on debug output\n"));
410                                 print_term(_("  -g:<regexp>, --grep:<regexp>     filter debug output\n"));
411 #endif
412                                 print_term(_("  +w, --show-log-window            show log window\n"));
413                                 print_term(_("  -o:<file>, --log-file:<file>     save log data to file\n"));
414                                 print_term(_("  -v, --version                    print version info\n"));
415                                 print_term(_("  -h, --help                       show this message\n\n"));
416
417 #if 0
418                                 /* these options are not officially supported!
419                                  * only for testing new features, no need to translate them */
420                                 print_term(  "  --alternate                use alternate similarity algorithm\n");
421 #endif
422
423
424                                 exit(0);
425                                 }
426                         else if (!remote_do)
427                                 {
428                                 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
429                                 }
430
431                         g_free(cmd_all);
432                         g_free(cmd_line);
433                         i++;
434                         }
435                 g_free(base_dir);
436                 parse_out_relatives(command_line->path);
437                 parse_out_relatives(command_line->file);
438                 }
439
440         list = g_list_reverse(list);
441
442         if (!command_line->path && first_dir)
443                 {
444                 command_line->path = first_dir;
445                 first_dir = NULL;
446
447                 parse_out_relatives(command_line->path);
448                 }
449         g_free(first_dir);
450
451         if (remote_do)
452                 {
453                 if (remote_errors)
454                         {
455                         GList *work = remote_errors;
456
457                         printf_term(_("Invalid or ignored remote options: "));
458                         while (work)
459                                 {
460                                 gchar *opt = work->data;
461
462                                 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
463                                 work = work->next;
464                                 }
465
466                         printf_term(_("\nUse --remote-help for valid remote options.\n"));
467                         }
468
469                 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
470                 }
471         g_list_free(remote_list);
472
473         if (list && list->next)
474                 {
475                 command_line->cmd_list = list;
476                 }
477         else
478                 {
479                 filelist_free(list);
480                 command_line->cmd_list = NULL;
481                 }
482
483         if (command_line->startup_blank)
484                 {
485                 g_free(command_line->path);
486                 command_line->path = NULL;
487                 g_free(command_line->file);
488                 command_line->file = NULL;
489                 filelist_free(command_line->cmd_list);
490                 command_line->cmd_list = NULL;
491                 string_list_free(command_line->collection_list);
492                 command_line->collection_list = NULL;
493                 }
494 }
495
496 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
497 {
498 #ifdef DEBUG
499         const gchar *debug_option = "--debug";
500         gint len = strlen(debug_option);
501
502         if (argc > 1)
503                 {
504                 gint i;
505
506                 for (i = 1; i < argc; i++)
507                         {
508                         const gchar *cmd_line = argv[i];
509                         if (strncmp(cmd_line, debug_option, len) == 0)
510                                 {
511                                 gint cmd_line_len = strlen(cmd_line);
512
513                                 /* we now increment the debug state for verbosity */
514                                 if (cmd_line_len == len)
515                                         debug_level_add(1);
516                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
517                                         {
518                                         gint n = atoi(cmd_line + len + 1);
519                                         if (n < 0) n = 1;
520                                         debug_level_add(n);
521                                         }
522                                 }
523                         }
524                 }
525
526         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
527 #endif
528 }
529
530 /*
531  *-----------------------------------------------------------------------------
532  * startup, init, and exit
533  *-----------------------------------------------------------------------------
534  */
535
536 #define RC_HISTORY_NAME "history"
537
538 static void setup_env_path(void)
539 {
540         const gchar *old_path = g_getenv("PATH");
541         gchar *path = g_strconcat(GQ_BIN_DIR, ":", old_path, NULL);
542         g_setenv("PATH", path, TRUE);
543         g_free(path);
544 }
545
546 static void keys_load(void)
547 {
548         gchar *path;
549
550         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
551         history_list_load(path);
552         g_free(path);
553 }
554
555 static void keys_save(void)
556 {
557         gchar *path;
558
559         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
560         history_list_save(path);
561         g_free(path);
562 }
563
564 static void mkdir_if_not_exists(const gchar *path)
565 {
566         if (isdir(path)) return;
567
568         log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
569
570         if (!recursive_mkdir_if_not_exists(path, 0755))
571                 {
572                 log_printf(_("Could not create dir:%s\n"), path);
573                 }
574 }
575
576
577 /* We add to duplicate and modify  gtk_accel_map_print() and gtk_accel_map_save()
578  * to improve the reliability in special cases (especially when disk is full)
579  * These functions are now using secure saving stuff.
580  */
581 static void gq_accel_map_print(
582                     gpointer    data,
583                     const gchar *accel_path,
584                     guint       accel_key,
585                     GdkModifierType accel_mods,
586                     gboolean    changed)
587 {
588         GString *gstring = g_string_new(changed ? NULL : "; ");
589         SecureSaveInfo *ssi = data;
590         gchar *tmp, *name;
591
592         g_string_append(gstring, "(gtk_accel_path \"");
593
594         tmp = g_strescape(accel_path, NULL);
595         g_string_append(gstring, tmp);
596         g_free(tmp);
597
598         g_string_append(gstring, "\" \"");
599
600         name = gtk_accelerator_name(accel_key, accel_mods);
601         tmp = g_strescape(name, NULL);
602         g_free(name);
603         g_string_append(gstring, tmp);
604         g_free(tmp);
605
606         g_string_append(gstring, "\")\n");
607
608         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
609
610         g_string_free(gstring, TRUE);
611 }
612
613 static gboolean gq_accel_map_save(const gchar *path)
614 {
615         gchar *pathl;
616         SecureSaveInfo *ssi;
617         GString *gstring;
618
619         pathl = path_from_utf8(path);
620         ssi = secure_open(pathl);
621         g_free(pathl);
622         if (!ssi)
623                 {
624                 log_printf(_("error saving file: %s\n"), path);
625                 return FALSE;
626                 }
627
628         gstring = g_string_new("; ");
629         if (g_get_prgname())
630                 g_string_append(gstring, g_get_prgname());
631         g_string_append(gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
632         g_string_append(gstring, "; this file is an automated accelerator map dump\n");
633         g_string_append(gstring, ";\n");
634
635         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
636
637         g_string_free(gstring, TRUE);
638
639         gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
640
641         if (secure_close(ssi))
642                 {
643                 log_printf(_("error saving file: %s\nerror: %s\n"), path,
644                            secsave_strerror(secsave_errno));
645                 return FALSE;
646                 }
647
648         return TRUE;
649 }
650
651 static gchar *accep_map_filename(void)
652 {
653         return g_build_filename(get_rc_dir(), "accels", NULL);
654 }
655
656 static void accel_map_save(void)
657 {
658         gchar *path;
659
660         path = accep_map_filename();
661         gq_accel_map_save(path);
662         g_free(path);
663 }
664
665 static void accel_map_load(void)
666 {
667         gchar *path;
668         gchar *pathl;
669
670         path = accep_map_filename();
671         pathl = path_from_utf8(path);
672         gtk_accel_map_load(pathl);
673         g_free(pathl);
674         g_free(path);
675 }
676
677 static void gtkrc_load(void)
678 {
679         gchar *path;
680         gchar *pathl;
681
682         /* If a gtkrc file exists in the rc directory, add it to the
683          * list of files to be parsed at the end of gtk_init() */
684         path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
685         pathl = path_from_utf8(path);
686         if (access(pathl, R_OK) == 0)
687                 gtk_rc_add_default_file(pathl);
688         g_free(pathl);
689         g_free(path);
690 }
691
692 static void exit_program_final(void)
693 {
694         LayoutWindow *lw = NULL;
695
696          /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
697         layout_editors_reload_finish();
698
699         remote_close(remote_connection);
700
701         collect_manager_flush();
702
703         save_options(options);
704         keys_save();
705         accel_map_save();
706
707         if (layout_valid(&lw))
708                 {
709                 layout_free(lw);
710                 }
711
712         secure_close(command_line->ssi);
713
714         gtk_main_quit();
715 }
716
717 static GenericDialog *exit_dialog = NULL;
718
719 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
720 {
721         exit_dialog = NULL;
722         generic_dialog_close(gd);
723 }
724
725 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
726 {
727         exit_dialog = NULL;
728         generic_dialog_close(gd);
729         exit_program_final();
730 }
731
732 static gint exit_confirm_dlg(void)
733 {
734         GtkWidget *parent;
735         LayoutWindow *lw;
736         gchar *msg;
737
738         if (exit_dialog)
739                 {
740                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
741                 return TRUE;
742                 }
743
744         if (!collection_window_modified_exists()) return FALSE;
745
746         parent = NULL;
747         lw = NULL;
748         if (layout_valid(&lw))
749                 {
750                 parent = lw->window;
751                 }
752
753         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
754         exit_dialog = generic_dialog_new(msg,
755                                 "exit", parent, FALSE,
756                                 exit_confirm_cancel_cb, NULL);
757         g_free(msg);
758         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
759         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
760                                    msg, _("Collections have been modified. Quit anyway?"));
761         g_free(msg);
762         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
763
764         gtk_widget_show(exit_dialog->dialog);
765
766         return TRUE;
767 }
768
769 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
770 {
771         if (success) exit_program();
772 }
773
774 void exit_program(void)
775 {
776         layout_image_full_screen_stop(NULL);
777
778         if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
779
780         if (exit_confirm_dlg()) return;
781
782         exit_program_final();
783 }
784
785 /* This code is supposed to handle situation when a file mmaped by image_loader
786  * or by exif loader is truncated by some other process.
787  * This is probably not completely correct according to posix, because
788  * mmap is not in the list of calls that can be used safely in signal handler,
789  * but anyway, the handler is called in situation when the application would
790  * crash otherwise.
791  * Ideas for improvement are welcome ;)
792  */
793 /* FIXME: this probably needs some better ifdefs. Please report any compilation problems */
794
795 #if defined(SIGBUS) && defined(SA_SIGINFO)
796 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
797 {
798         unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
799         DEBUG_1("SIGBUS %p", info->si_addr);
800         mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
801 }
802 #endif
803
804 static void setup_sigbus_handler(void)
805 {
806 #if defined(SIGBUS) && defined(SA_SIGINFO)
807         struct sigaction sigbus_action;
808         sigfillset(&sigbus_action.sa_mask);
809         sigbus_action.sa_sigaction = sigbus_handler_cb;
810         sigbus_action.sa_flags = SA_SIGINFO;
811
812         sigaction(SIGBUS, &sigbus_action, NULL);
813 #endif
814 }
815
816 gint main(gint argc, gchar *argv[])
817 {
818         CollectionData *first_collection = NULL;
819         gchar *buf;
820         CollectionData *cd = NULL;
821
822 #ifdef HAVE_GTHREAD
823 #if !GLIB_CHECK_VERSION(2,32,0)
824         g_thread_init(NULL);
825 #endif
826         gdk_threads_init();
827         gdk_threads_enter();
828
829 #endif
830
831         /* init execution time counter (debug only) */
832         init_exec_time();
833
834         /* setup locale, i18n */
835         setlocale(LC_ALL, "");
836
837 #ifdef ENABLE_NLS
838         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
839         bind_textdomain_codeset(PACKAGE, "UTF-8");
840         textdomain(PACKAGE);
841 #endif
842
843         exif_init();
844
845 #ifdef HAVE_LUA
846         lua_init();
847 #endif
848
849         /* setup random seed for random slideshow */
850         srand(time(NULL));
851
852         setup_sigbus_handler();
853
854         /* register global notify functions */
855         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
856         file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
857         file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
858         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
859         file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
860
861
862         gtkrc_load();
863
864         parse_command_line_for_debug_option(argc, argv);
865         DEBUG_1("%s main: gtk_init", get_exec_time());
866 #ifdef HAVE_CLUTTER
867         if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
868                 {
869                 log_printf("Can't initialize clutter-gtk.\n");
870                 exit(1);
871                 }
872 #else
873         gtk_init(&argc, &argv);
874 #endif
875
876         if (gtk_major_version < GTK_MAJOR_VERSION ||
877             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
878                 {
879                 log_printf("!!! This is a friendly warning.\n");
880                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
881                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
882                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
883                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
884                 }
885
886         DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
887         pixbuf_inline_register_stock_icons();
888
889         DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
890         options = init_options(NULL);
891         setup_default_options(options);
892
893         DEBUG_1("%s main: parse_command_line", get_exec_time());
894         parse_command_line(argc, argv);
895
896         DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
897         /* these functions don't depend on config file */
898         mkdir_if_not_exists(get_rc_dir());
899         mkdir_if_not_exists(get_collections_dir());
900         mkdir_if_not_exists(get_thumbnails_cache_dir());
901         mkdir_if_not_exists(get_metadata_cache_dir());
902
903         setup_env_path();
904
905         keys_load();
906         accel_map_load();
907
908         /* restore session from the config file */
909
910
911         DEBUG_1("%s main: load_options", get_exec_time());
912         if (!load_options(options))
913                 {
914                 /* load_options calls these functions after it parses global options, we have to call it here if it fails */
915                 filter_add_defaults();
916                 filter_rebuild();
917                 }
918
919         /* handle missing config file and commandline additions*/
920         if (!layout_window_list)
921                 {
922                 /* broken or no config file */
923                 layout_new_from_config(NULL, NULL, TRUE);
924                 }
925
926         layout_editors_reload_start();
927
928         if (command_line->collection_list && !command_line->startup_command_line_collection)
929                 {
930                 GList *work;
931
932                 work = command_line->collection_list;
933                 while (work)
934                         {
935                         CollectWindow *cw;
936                         const gchar *path;
937
938                         path = work->data;
939                         work = work->next;
940
941                         cw = collection_window_new(path);
942                         if (!first_collection && cw) first_collection = cw->cd;
943                         }
944                 }
945
946         if (command_line->log_file)
947                 {
948                 gchar *pathl;
949                 gchar *path = g_strdup(command_line->log_file);
950
951                 pathl = path_from_utf8(path);
952                 command_line->ssi = secure_open(pathl);
953                 }
954
955         if (command_line->cmd_list ||
956             (command_line->startup_command_line_collection && command_line->collection_list))
957                 {
958                 GList *work;
959
960                 if (command_line->startup_command_line_collection)
961                         {
962                         CollectWindow *cw;
963
964                         cw = collection_window_new("");
965                         cd = cw->cd;
966                         }
967                 else
968                         {
969                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
970                         }
971
972                 g_free(cd->path);
973                 cd->path = NULL;
974                 g_free(cd->name);
975                 cd->name = g_strdup(_("Command line"));
976
977                 collection_path_changed(cd);
978
979                 work = command_line->cmd_list;
980                 while (work)
981                         {
982                         collection_add(cd, (FileData *)work->data, FALSE);
983                         work = work->next;
984                         }
985
986                 work = command_line->collection_list;
987                 while (work)
988                         {
989                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
990                         work = work->next;
991                         }
992
993                 if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
994
995                 /* mem leak, we never unref this collection when !startup_command_line_collection
996                  * (the image view of the main window does not hold a ref to the collection)
997                  * this is sort of unavoidable, for if it did hold a ref, next/back
998                  * may not work as expected when closing collection windows.
999                  *
1000                  * collection_unref(cd);
1001                  */
1002
1003                 }
1004         else if (first_collection)
1005                 {
1006                 layout_image_set_collection(NULL, first_collection,
1007                                             collection_get_first(first_collection));
1008                 }
1009
1010         buf = g_build_filename(get_rc_dir(), ".command", NULL);
1011         remote_connection = remote_server_init(buf, cd);
1012         g_free(buf);
1013
1014         DEBUG_1("%s main: gtk_main", get_exec_time());
1015         gtk_main();
1016 #ifdef HAVE_GTHREAD
1017         gdk_threads_leave();
1018 #endif
1019         return 0;
1020 }
1021 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */