Auto-generate the man page
[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.h"
42 #include "image-overlay.h"
43 #include "img-view.h"
44 #include "layout.h"
45 #include "layout_image.h"
46 #include "layout_util.h"
47 #include "misc.h"
48 #include "options.h"
49 #include "rcfile.h"
50 #include "remote.h"
51 #include "secure_save.h"
52 #include "similar.h"
53 #include "ui_fileops.h"
54 #include "ui_utildlg.h"
55 #include "cache_maint.h"
56 #include "thumb.h"
57 #include "metadata.h"
58 #include "editors.h"
59 #include "exif.h"
60 #include "histogram.h"
61 #include "pixbuf_util.h"
62 #include "glua.h"
63 #include "whereami.h"
64
65 #ifdef HAVE_CLUTTER
66 #include <clutter-gtk/clutter-gtk.h>
67 #endif
68
69 #ifdef HAVE_GTHREAD
70 /** @FIXME see below */
71 #include <X11/Xlib.h>
72 #endif
73
74 /**
75  * @page diagrams Diagrams
76  * @section options_overview Options Overview
77  * 
78  * #_ConfOptions  #_LayoutOptions
79  * 
80  * @startuml
81  * 
82  * object options.h
83  * object typedefs.h
84  * 
85  * options.h : ConfOptions
86  * options.h : \n
87  * options.h : Options applicable to **all** Layout Windows
88  * options.h : These are in the <global> section of geeqierc.xml
89  * options.h : Available to all modules via the global variable **options**
90  * typedefs.h : LayoutOptions
91  * typedefs.h : \n
92  * typedefs.h : Options applicable to **each** Layout Window
93  * typedefs.h : These are in the <layout> section of geeqierc.xml
94  * typedefs.h : There is one <layout> section for each Layout Window displayed
95  * typedefs.h : Available via **<layout_window>->options**
96  * 
97  * @enduml
98  */
99
100 /**
101  * @page diagrams Diagrams
102  * @section options_diagrams_main Options - New Window From Main
103  * #main  
104  * #init_options  
105  * #layout_new_from_default  
106  * #load_config_from_file  
107  * #load_options  
108  * #setup_default_options
109  * 
110  * @startuml
111  * group main.c
112  * start
113  * group options.c
114  * : **init_options()**
115  * 
116  * Set **options** = ConfOptions from hard-coded init values;
117  * end group
118  * 
119  * group options.c
120  * : **setup_default_options()**
121  * 
122  * set hard-coded ConfOptions:
123  * 
124  * bookmarks:
125  * * dot dir
126  * * Home
127  * * Desktop
128  * * Collections
129  * safe delete path
130  * OSD template string
131  * sidecar extensions
132  * shell path and options
133  * marks tooltips
134  * help search engine;
135  * end group
136  * 
137  * if (first entry
138  * or
139  * --new-instance) then (yes)
140  * group options.c
141  * : **load_options()**
142  * ;
143  * 
144  * split
145  * : GQ_SYSTEM_WIDE_DIR
146  * /geeqierc.xml;
147  * split again
148  * : XDG_CONFIG_HOME
149  * /geeqierc.xml;
150  * split again
151  * : HOME
152  * /.geeqie/geeqierc.xml;
153  * end split
154  * 
155  * group rcfile.c
156  * : **load_config_from_file()**
157  * 
158  * set  **options** from file
159  * and all <layout window>->options  in file;
160  * end group
161  * 
162  * end group
163  * 
164  * if (broken config. file
165  * or no config file
166  * or no layout section loaded
167  * (i.e. session not saved)) then (yes)
168  * group layout.c
169  * : **layout_new_from_default()**;
170  * if (default.xml exists) then (yes)
171  * : Load user-saved
172  *  layout_window default options
173  *  from default.xml file;
174  * else (no)
175  * : Load hard-coded
176  *  layout_window default options;
177  * endif
178  * end group
179  * endif
180  * 
181  * else (no)
182  * : Send --new-window request to remote
183  *  No return to this point
184  *  This instance terminates;
185  * stop
186  * endif
187  * 
188  * : Enter gtk main loop;
189  * 
190  * end group
191  * @enduml
192  */
193
194 /**
195  * @page diagrams Diagrams
196  * @section options_diagrams_remote Options - New Window From Remote
197  * #layout_new_from_default  
198  * @startuml
199  * 
200  * group remote.c
201  * start
202  * group layout.c
203  * : **layout_new_from_default()**;
204  * if (default.xml exists) then (yes)
205  * : Load user-saved
206  *  layout_window default options
207  *  from default.xml file;
208  * else (no)
209  * : Load hard-coded
210  *  layout_window default options;
211  * endif
212  * end group
213  * : set path from PWD;
214  * @enduml
215  */
216
217 /**
218  * @page diagrams Diagrams
219  * @section options_diagrams_menu Options - New Window From Menu
220  * #layout_menu_new_window_cb  
221  * #layout_menu_window_from_current_cb  
222  * #layout_new_from_default  
223  * @startuml
224  * 
225  * group layout_util.c
226  * start
227  * 
228  * split
229  * : default;
230  * group layout.c
231  * : **layout_new_from_default()**;
232  * if (default.xml exists) then (yes)
233  * : Load user-saved
234  *  layout_window default options
235  *  from default.xml file;
236  * else (no)
237  * : Load hard-coded
238  *  layout_window default options;
239  * endif
240  * end group
241  * 
242  * split again
243  * : from current
244  * 
245  * **layout_menu_window_from_current_cb()**
246  * copy layout_window options
247  * from current window;
248  * 
249  * split again
250  * : named
251  * 
252  * **layout_menu_new_window_cb()**
253  * load layout_window options
254  * from saved xml file list;
255  * end split
256  * 
257  * end group
258  * @enduml
259  */
260   /**
261   * @file
262   * @ref options_overview Options Overview
263   */
264
265
266 gboolean thumb_format_changed = FALSE;
267 static RemoteConnection *remote_connection = NULL;
268
269 gchar *gq_prefix;
270 gchar *gq_localedir;
271 gchar *gq_helpdir;
272 gchar *gq_htmldir;
273 gchar *gq_app_dir;
274 gchar *gq_bin_dir;
275 gchar *gq_executable_path;
276 gchar *desktop_file_template;
277 gchar *instance_identifier;
278
279 /*
280  *-----------------------------------------------------------------------------
281  * keyboard functions
282  *-----------------------------------------------------------------------------
283  */
284
285 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
286 {
287         static gint delta = 0;
288         static guint32 time_old = 0;
289         static guint keyval_old = 0;
290
291         if (event->state & GDK_CONTROL_MASK)
292                 {
293                 if (*x < 0) *x = G_MININT / 2;
294                 if (*x > 0) *x = G_MAXINT / 2;
295                 if (*y < 0) *y = G_MININT / 2;
296                 if (*y > 0) *y = G_MAXINT / 2;
297
298                 return;
299                 }
300
301         if (options->progressive_key_scrolling)
302                 {
303                 guint32 time_diff;
304
305                 time_diff = event->time - time_old;
306
307                 /* key pressed within 125ms ? (1/8 second) */
308                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
309
310                 time_old = event->time;
311                 keyval_old = event->keyval;
312
313                 delta += 2;
314                 }
315         else
316                 {
317                 delta = 8;
318                 }
319
320         *x = *x * delta * options->keyboard_scroll_step;
321         *y = *y * delta * options->keyboard_scroll_step;
322 }
323
324
325
326 /*
327  *-----------------------------------------------------------------------------
328  * command line parser (private) hehe, who needs popt anyway?
329  *-----------------------------------------------------------------------------
330  */
331
332 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
333                                         GList **list, GList **collection_list)
334 {
335         gchar *path_parsed;
336
337         path_parsed = g_strdup(file_path);
338         parse_out_relatives(path_parsed);
339
340         if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
341                 {
342                 *collection_list = g_list_append(*collection_list, path_parsed);
343                 }
344         else
345                 {
346                 if (!*path) *path = remove_level_from_path(path_parsed);
347                 if (!*file) *file = g_strdup(path_parsed);
348                 *list = g_list_prepend(*list, path_parsed);
349                 }
350 }
351
352 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
353                                        GList **list)
354 {
355 #if 0
356         /* This is broken because file filter is not initialized yet.
357         */
358         GList *files;
359         gchar *path_parsed;
360         FileData *dir_fd;
361
362         path_parsed = g_strdup(dir);
363         parse_out_relatives(path_parsed);
364         dir_fd = file_data_new_dir(path_parsed);
365
366
367         if (filelist_read(dir_fd, &files, NULL))
368                 {
369                 GList *work;
370
371                 files = filelist_filter(files, FALSE);
372                 files = filelist_sort_path(files);
373
374                 work = files;
375                 while (work)
376                         {
377                         FileData *fd = work->data;
378                         if (!*path) *path = remove_level_from_path(fd->path);
379                         if (!*file) *file = g_strdup(fd->path);
380                         *list = g_list_prepend(*list, fd);
381
382                         work = work->next;
383                         }
384
385                 g_list_free(files);
386                 }
387
388         g_free(path_parsed);
389         file_data_unref(dir_fd);
390 #else
391         DEBUG_1("multiple directories specified, ignoring: %s", dir);
392 #endif
393 }
394
395 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
396                                            GList **list, gchar **first_dir)
397 {
398
399         if (!*list && !*first_dir)
400                 {
401                 *first_dir = g_strdup(dir);
402                 }
403         else
404                 {
405                 if (*first_dir)
406                         {
407                         parse_command_line_add_dir(*first_dir, path, file, list);
408                         g_free(*first_dir);
409                         *first_dir = NULL;
410                         }
411                 parse_command_line_add_dir(dir, path, file, list);
412                 }
413 }
414
415 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
416                                             GList **list, GList **collection_list, gchar **first_dir)
417 {
418
419         if (*first_dir)
420                 {
421                 parse_command_line_add_dir(*first_dir, path, file, list);
422                 g_free(*first_dir);
423                 *first_dir = NULL;
424                 }
425         parse_command_line_add_file(file_path, path, file, list, collection_list);
426 }
427
428 static void parse_command_line(gint argc, gchar *argv[])
429 {
430         GList *list = NULL;
431         GList *remote_list = NULL;
432         GList *remote_errors = NULL;
433         gboolean remote_do = FALSE;
434         gchar *first_dir = NULL;
435         gchar *app_lock;
436         gchar *pwd;
437         gchar *current_dir;
438         gchar *geometry = NULL;
439         GtkWidget *dialog_warning;
440         GString *command_line_errors = g_string_new(NULL);
441
442         command_line = g_new0(CommandLine, 1);
443
444         command_line->argc = argc;
445         command_line->argv = argv;
446         command_line->regexp = NULL;
447
448         if (argc > 1)
449                 {
450                 gint i;
451                 gchar *base_dir = get_current_dir();
452                 i = 1;
453                 while (i < argc)
454                         {
455                         gchar *cmd_line = path_to_utf8(argv[i]);
456                         gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
457
458                         if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
459                                 {
460                                 parse_command_line_process_dir(cmd_line, &command_line->path, &command_line->file, &list, &first_dir);
461                                 }
462                         else if (isdir(cmd_all))
463                                 {
464                                 parse_command_line_process_dir(cmd_all, &command_line->path, &command_line->file, &list, &first_dir);
465                                 }
466                         else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
467                                 {
468                                 parse_command_line_process_file(cmd_line, &command_line->path, &command_line->file,
469                                                                 &list, &command_line->collection_list, &first_dir);
470                                 }
471                         else if (isfile(cmd_all))
472                                 {
473                                 parse_command_line_process_file(cmd_all, &command_line->path, &command_line->file,
474                                                                 &list, &command_line->collection_list, &first_dir);
475                                 }
476                         else if (download_web_file(cmd_line, FALSE, NULL))
477                                 {
478                                 }
479                         else if (is_collection(cmd_line))
480                                 {
481                                 gchar *path = NULL;
482
483                                 path = collection_path(cmd_line);
484                                 parse_command_line_process_file(path, &command_line->path, &command_line->file,
485                                                                 &list, &command_line->collection_list, &first_dir);
486                                 g_free(path);
487                                 }
488                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
489                                 {
490                                 /* do nothing but do not produce warnings */
491                                 }
492                         else if (strncmp(cmd_line, "--disable-clutter", 17) == 0 && (cmd_line[17] == '\0'))
493                                 {
494                                 /* do nothing but do not produce warnings */
495                                 }
496                         else if (strcmp(cmd_line, "+t") == 0 ||
497                                  strcmp(cmd_line, "--with-tools") == 0)
498                                 {
499                                 command_line->tools_show = TRUE;
500
501                                 remote_list = g_list_append(remote_list, "+t");
502                                 }
503                         else if (strcmp(cmd_line, "-t") == 0 ||
504                                  strcmp(cmd_line, "--without-tools") == 0)
505                                 {
506                                 command_line->tools_hide = TRUE;
507
508                                 remote_list = g_list_append(remote_list, "-t");
509                                 }
510                         else if (strcmp(cmd_line, "-f") == 0 ||
511                                  strcmp(cmd_line, "--fullscreen") == 0)
512                                 {
513                                 command_line->startup_full_screen = TRUE;
514                                 }
515                         else if (strcmp(cmd_line, "-s") == 0 ||
516                                  strcmp(cmd_line, "--slideshow") == 0)
517                                 {
518                                 command_line->startup_in_slideshow = TRUE;
519                                 }
520                         else if (strcmp(cmd_line, "-l") == 0 ||
521                                  strcmp(cmd_line, "--list") == 0)
522                                 {
523                                 command_line->startup_command_line_collection = TRUE;
524                                 }
525                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
526                                 {
527                                 if (!command_line->geometry) command_line->geometry = g_strdup(cmd_line + 11);
528                                 }
529                         else if (strcmp(cmd_line, "-r") == 0 ||
530                                  strcmp(cmd_line, "--remote") == 0)
531                                 {
532                                 if (!remote_do)
533                                         {
534                                         remote_do = TRUE;
535                                         remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
536                                         }
537                                 }
538                         else if ((strcmp(cmd_line, "+w") == 0) ||
539                                                 strcmp(cmd_line, "--show-log-window") == 0)
540                                 {
541                                 command_line->log_window_show = TRUE;
542                                 }
543                         else if (strncmp(cmd_line, "-o:", 3) == 0)
544                                 {
545                                 command_line->log_file = g_strdup(cmd_line + 3);
546                                 }
547                         else if (strncmp(cmd_line, "--log-file:", 11) == 0)
548                                 {
549                                 command_line->log_file = g_strdup(cmd_line + 11);
550                                 }
551                         else if (strncmp(cmd_line, "-g:", 3) == 0)
552                                 {
553                                 set_regexp(g_strdup(cmd_line+3));
554                                 }
555                         else if (strncmp(cmd_line, "-grep:", 6) == 0)
556                                 {
557                                 set_regexp(g_strdup(cmd_line+3));
558                                 }
559                         else if (strncmp(cmd_line, "-n", 2) == 0)
560                                 {
561                                 command_line->new_instance = TRUE;
562                                 }
563                         else if (strncmp(cmd_line, "--new-instance", 14) == 0)
564                                 {
565                                 command_line->new_instance = TRUE;
566                                 }
567                         else if (strcmp(cmd_line, "-rh") == 0 ||
568                                  strcmp(cmd_line, "--remote-help") == 0)
569                                 {
570                                 remote_help();
571                                 exit(0);
572                                 }
573                         else if (strcmp(cmd_line, "--blank") == 0)
574                                 {
575                                 command_line->startup_blank = TRUE;
576                                 }
577                         else if (strcmp(cmd_line, "-v") == 0 ||
578                                  strcmp(cmd_line, "--version") == 0)
579                                 {
580                                 printf_term(FALSE, "%s %s GTK%d\n", GQ_APPNAME, VERSION, gtk_major_version);
581                                 exit(0);
582                                 }
583                         else if (strcmp(cmd_line, "--alternate") == 0)
584                                 {
585                                 /* enable faster experimental algorithm */
586                                 log_printf("Alternate similarity algorithm enabled\n");
587                                 image_sim_alternate_set(TRUE);
588                                 }
589                         else if (strcmp(cmd_line, "-h") == 0 ||
590                                  strcmp(cmd_line, "--help") == 0)
591                                 {
592                                 printf_term(FALSE, "%s %s\n", GQ_APPNAME, VERSION);
593                                 printf_term(FALSE, _("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
594                                 print_term(FALSE, _("Valid options:\n"));
595                                 print_term(FALSE, _("      --blank                      start with blank file list\n"));
596                                 print_term(FALSE, _("      --cache-maintenance <path>   run cache maintenance in non-GUI mode\n"));
597                                 print_term(FALSE, _("      --disable-clutter            disable use of Clutter library (i.e. GPU accel.)\n"));
598                                 print_term(FALSE, _("  -f, --fullscreen                 start in full screen mode\n"));
599                                 print_term(FALSE, _("      --geometry=WxH+XOFF+YOFF     set main window location\n"));
600                                 print_term(FALSE, _("  -h, --help                       show this message\n"));
601                                 print_term(FALSE, _("  -l, --list [files] [collections] open collection window for command line\n"));
602                                 print_term(FALSE, _("  -n, --new-instance               open a new instance of Geeqie\n"));
603                                 print_term(FALSE, _("  -o:, --log-file:<file>     save log data to file\n"));
604                                 print_term(FALSE, _("  -r, --remote                     send following commands to open window\n"));
605                                 print_term(FALSE, _("  -rh, --remote-help               print remote command list\n"));
606                                 print_term(FALSE, _("  -s, --slideshow                  start in slideshow mode\n"));
607                                 print_term(FALSE, _("  +t, --with-tools                 force show of tools\n"));
608                                 print_term(FALSE, _("  -t, --without-tools              force hide of tools\n"));
609                                 print_term(FALSE, _("  -v, --version                    print version info\n"));
610                                 print_term(FALSE, _("  +w, --show-log-window            show log window\n"));
611 #ifdef DEBUG
612                                 print_term(FALSE, _("      --debug[=level]              turn on debug output\n"));
613                                 print_term(FALSE, _("  -g:, --grep:<regexp>     filter debug output\n"));
614 #endif
615
616 #if 0
617                                 /* these options are not officially supported!
618                                  * only for testing new features, no need to translate them */
619                                 print_term(FALSE, "  --alternate                use alternate similarity algorithm\n");
620 #endif
621                                 print_term(FALSE, "\n");
622
623                                 remote_help();
624
625
626                                 exit(0);
627                                 }
628                         else if (!remote_do)
629                                 {
630                                 command_line_errors = g_string_append(command_line_errors, cmd_line);
631                                 command_line_errors = g_string_append(command_line_errors, "\n");
632                                 }
633
634                         g_free(cmd_all);
635                         g_free(cmd_line);
636                         i++;
637                         }
638
639                 if (command_line_errors->len > 0)
640                         {
641                         dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
642                         gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
643                         gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
644                         gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
645                         gtk_dialog_run(GTK_DIALOG(dialog_warning));
646                         gtk_widget_destroy(dialog_warning);
647                         g_string_free(command_line_errors, TRUE);
648
649                         exit(EXIT_FAILURE);
650                         }
651
652                 g_free(base_dir);
653                 parse_out_relatives(command_line->path);
654                 parse_out_relatives(command_line->file);
655                 }
656
657         list = g_list_reverse(list);
658
659         if (!command_line->path && first_dir)
660                 {
661                 command_line->path = first_dir;
662                 first_dir = NULL;
663
664                 parse_out_relatives(command_line->path);
665                 }
666         g_free(first_dir);
667
668         if (!command_line->new_instance)
669                 {
670                 /* If Geeqie is already running, prevent a second instance
671                  * from being started. Open a new window instead.
672                  */
673                 app_lock = g_build_filename(get_rc_dir(), ".command", NULL);
674                 if (remote_server_exists(app_lock) && !remote_do)
675                         {
676                         remote_do = TRUE;
677                         if (command_line->geometry)
678                                 {
679                                 geometry = g_strdup_printf("--geometry=%s", command_line->geometry);
680                                 remote_list = g_list_prepend(remote_list, geometry);
681                                 }
682                         remote_list = g_list_prepend(remote_list, "--new-window");
683                         }
684                 g_free(app_lock);
685                 }
686
687         if (remote_do)
688                 {
689                 if (remote_errors)
690                         {
691                         GList *work = remote_errors;
692
693                         while (work)
694                                 {
695                                 gchar *opt = work->data;
696
697                                 command_line_errors = g_string_append(command_line_errors, opt);
698                                 command_line_errors = g_string_append(command_line_errors, "\n");
699                                 work = work->next;
700                                 }
701
702                         dialog_warning = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", "Invalid parameter(s):");
703                         gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog_warning), "%s", command_line_errors->str);
704                         gtk_window_set_title(GTK_WINDOW(dialog_warning), GQ_APPNAME);
705                         gtk_window_set_keep_above(GTK_WINDOW(dialog_warning), TRUE);
706                         gtk_dialog_run(GTK_DIALOG(dialog_warning));
707                         gtk_widget_destroy(dialog_warning);
708                         g_string_free(command_line_errors, TRUE);
709
710                         exit(EXIT_FAILURE);
711                         }
712
713                 /* prepend the current dir the remote command was made from,
714                  * for use by any remote command that needs it
715                  */
716                 current_dir = g_get_current_dir();
717                 pwd = g_strconcat("--PWD:", current_dir, NULL);
718                 remote_list = g_list_prepend(remote_list, pwd);
719
720                 remote_control(argv[0], remote_list, command_line->path, list, command_line->collection_list);
721                 /* There is no return to this point
722                  */
723                 g_free(pwd);
724                 g_free(current_dir);
725                 }
726         g_free(geometry);
727         g_list_free(remote_list);
728
729         if (list && list->next)
730                 {
731                 command_line->cmd_list = list;
732                 }
733         else
734                 {
735                 string_list_free(list);
736                 command_line->cmd_list = NULL;
737                 }
738
739         if (command_line->startup_blank)
740                 {
741                 g_free(command_line->path);
742                 command_line->path = NULL;
743                 g_free(command_line->file);
744                 command_line->file = NULL;
745                 filelist_free(command_line->cmd_list);
746                 command_line->cmd_list = NULL;
747                 string_list_free(command_line->collection_list);
748                 command_line->collection_list = NULL;
749                 }
750 }
751
752 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
753 {
754 #ifdef DEBUG
755         const gchar *debug_option = "--debug";
756         gint len = strlen(debug_option);
757
758         if (argc > 1)
759                 {
760                 gint i;
761
762                 for (i = 1; i < argc; i++)
763                         {
764                         const gchar *cmd_line = argv[i];
765                         if (strncmp(cmd_line, debug_option, len) == 0)
766                                 {
767                                 gint cmd_line_len = strlen(cmd_line);
768
769                                 /* we now increment the debug state for verbosity */
770                                 if (cmd_line_len == len)
771                                         debug_level_add(1);
772                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
773                                         {
774                                         gint n = atoi(cmd_line + len + 1);
775                                         if (n < 0) n = 1;
776                                         debug_level_add(n);
777                                         }
778                                 }
779                         }
780                 }
781
782         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
783 #endif
784 }
785
786 #ifdef HAVE_CLUTTER
787 static gboolean parse_command_line_for_clutter_option(gint argc, gchar *argv[])
788 {
789         const gchar *clutter_option = "--disable-clutter";
790         gint len = strlen(clutter_option);
791         gboolean ret = FALSE;
792
793         if (argc > 1)
794                 {
795                 gint i;
796
797                 for (i = 1; i < argc; i++)
798                         {
799                         const gchar *cmd_line = argv[i];
800                         if (strncmp(cmd_line, clutter_option, len) == 0)
801                                 {
802                                 ret = TRUE;
803                                 }
804                         }
805                 }
806
807         return ret;
808 }
809 #endif
810
811 static gboolean parse_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
812 {
813         const gchar *cache_maintenance_option = "--cache-maintenance";
814         gint len = strlen(cache_maintenance_option);
815         gboolean ret = FALSE;
816
817         if (argc >= 2)
818                 {
819                 const gchar *cmd_line = argv[1];
820                 if (strncmp(cmd_line, cache_maintenance_option, len) == 0)
821                         {
822                         ret = TRUE;
823                         }
824                 }
825
826         return ret;
827 }
828
829 static void process_command_line_for_cache_maintenance_option(gint argc, gchar *argv[])
830 {
831         gchar *rc_path;
832         gchar *folder_path = NULL;
833         gsize size;
834         gsize i = 0;
835         gchar *buf_config_file;
836         gint diff_count;
837
838         if (argc >= 3)
839                 {
840                 folder_path = expand_tilde(argv[2]);
841
842                 if (isdir(folder_path))
843                         {
844                         rc_path = g_build_filename(get_rc_dir(), RC_FILE_NAME, NULL);
845
846                         if (isfile(rc_path))
847                                 {
848                                 if (g_file_get_contents(rc_path, &buf_config_file, &size, NULL))
849                                         {
850                                         while (i < size)
851                                                 {
852                                                 diff_count = strncmp("</global>", &buf_config_file[i], 9);
853                                                 if (diff_count == 0)
854                                                         {
855                                                         break;
856                                                         }
857                                                 i++;
858                                                 }
859                                         /* Load only the <global> section */
860                                         load_config_from_buf(buf_config_file, i + 9, FALSE);
861
862                                         if (options->thumbnails.enable_caching)
863                                                 {
864                                                 cache_maintenance(folder_path);
865                                                 }
866                                         else
867                                                 {
868                                                 print_term(TRUE, "Caching not enabled\n");
869                                                 exit(EXIT_FAILURE);
870                                                 }
871                                         g_free(buf_config_file);
872                                         }
873                                 else
874                                         {
875                                         print_term(TRUE, g_strconcat(_("Cannot load "), rc_path, "\n", NULL));
876                                         exit(EXIT_FAILURE);
877                                         }
878                                 }
879                         else
880                                 {
881                                 print_term(TRUE, g_strconcat(_("Configuration file path "), rc_path, _(" is not a file\n"), NULL));
882                                 exit(EXIT_FAILURE);
883                                 }
884                         g_free(rc_path);
885                         }
886                 else
887                         {
888                         print_term(TRUE, g_strconcat(argv[2], _(" is not a folder\n"), NULL));
889                         exit(EXIT_FAILURE);
890                         }
891                 g_free(folder_path);
892                 }
893         else
894                 {
895                 print_term(TRUE, _("No path parameter given\n"));
896                 exit(EXIT_FAILURE);
897                 }
898 }
899
900 /*
901  *-----------------------------------------------------------------------------
902  * startup, init, and exit
903  *-----------------------------------------------------------------------------
904  */
905
906 #define RC_HISTORY_NAME "history"
907 #define RC_MARKS_NAME "marks"
908
909 static void setup_env_path(void)
910 {
911         const gchar *old_path = g_getenv("PATH");
912         gchar *path = g_strconcat(gq_bin_dir, ":", old_path, NULL);
913         g_setenv("PATH", path, TRUE);
914         g_free(path);
915 }
916
917 static void keys_load(void)
918 {
919         gchar *path;
920
921         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
922         history_list_load(path);
923         g_free(path);
924 }
925
926 static void keys_save(void)
927 {
928         gchar *path;
929
930         path = g_build_filename(get_rc_dir(), RC_HISTORY_NAME, NULL);
931         history_list_save(path);
932         g_free(path);
933 }
934
935 static void marks_load(void)
936 {
937         gchar *path;
938
939         path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
940         marks_list_load(path);
941         g_free(path);
942 }
943
944 static void marks_save(gboolean save)
945 {
946         gchar *path;
947
948         path = g_build_filename(get_rc_dir(), RC_MARKS_NAME, NULL);
949         marks_list_save(path, save);
950         g_free(path);
951 }
952
953 static void mkdir_if_not_exists(const gchar *path)
954 {
955         if (isdir(path)) return;
956
957         log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);
958
959         if (!recursive_mkdir_if_not_exists(path, 0755))
960                 {
961                 log_printf(_("Could not create dir:%s\n"), path);
962                 }
963 }
964
965
966 /* We add to duplicate and modify  gtk_accel_map_print() and gtk_accel_map_save()
967  * to improve the reliability in special cases (especially when disk is full)
968  * These functions are now using secure saving stuff.
969  */
970 static void gq_accel_map_print(
971                     gpointer    data,
972                     const gchar *accel_path,
973                     guint       accel_key,
974                     GdkModifierType accel_mods,
975                     gboolean    changed)
976 {
977         GString *gstring = g_string_new(changed ? NULL : "; ");
978         SecureSaveInfo *ssi = data;
979         gchar *tmp, *name;
980
981         g_string_append(gstring, "(gtk_accel_path \"");
982
983         tmp = g_strescape(accel_path, NULL);
984         g_string_append(gstring, tmp);
985         g_free(tmp);
986
987         g_string_append(gstring, "\" \"");
988
989         name = gtk_accelerator_name(accel_key, accel_mods);
990         tmp = g_strescape(name, NULL);
991         g_free(name);
992         g_string_append(gstring, tmp);
993         g_free(tmp);
994
995         g_string_append(gstring, "\")\n");
996
997         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
998
999         g_string_free(gstring, TRUE);
1000 }
1001
1002 static gboolean gq_accel_map_save(const gchar *path)
1003 {
1004         gchar *pathl;
1005         SecureSaveInfo *ssi;
1006         GString *gstring;
1007
1008         pathl = path_from_utf8(path);
1009         ssi = secure_open(pathl);
1010         g_free(pathl);
1011         if (!ssi)
1012                 {
1013                 log_printf(_("error saving file: %s\n"), path);
1014                 return FALSE;
1015                 }
1016
1017         gstring = g_string_new("; ");
1018         if (g_get_prgname())
1019                 g_string_append(gstring, g_get_prgname());
1020         g_string_append(gstring, " GtkAccelMap rc-file         -*- scheme -*-\n");
1021         g_string_append(gstring, "; this file is an automated accelerator map dump\n");
1022         g_string_append(gstring, ";\n");
1023
1024         secure_fwrite(gstring->str, sizeof(*gstring->str), gstring->len, ssi);
1025
1026         g_string_free(gstring, TRUE);
1027
1028         gtk_accel_map_foreach((gpointer) ssi, gq_accel_map_print);
1029
1030         if (secure_close(ssi))
1031                 {
1032                 log_printf(_("error saving file: %s\nerror: %s\n"), path,
1033                            secsave_strerror(secsave_errno));
1034                 return FALSE;
1035                 }
1036
1037         return TRUE;
1038 }
1039
1040 static gchar *accep_map_filename(void)
1041 {
1042         return g_build_filename(get_rc_dir(), "accels", NULL);
1043 }
1044
1045 static void accel_map_save(void)
1046 {
1047         gchar *path;
1048
1049         path = accep_map_filename();
1050         gq_accel_map_save(path);
1051         g_free(path);
1052 }
1053
1054 static void accel_map_load(void)
1055 {
1056         gchar *path;
1057         gchar *pathl;
1058
1059         path = accep_map_filename();
1060         pathl = path_from_utf8(path);
1061         gtk_accel_map_load(pathl);
1062         g_free(pathl);
1063         g_free(path);
1064 }
1065
1066 static void gtkrc_load(void)
1067 {
1068         gchar *path;
1069         gchar *pathl;
1070
1071         /* If a gtkrc file exists in the rc directory, add it to the
1072          * list of files to be parsed at the end of gtk_init() */
1073         path = g_build_filename(get_rc_dir(), "gtkrc", NULL);
1074         pathl = path_from_utf8(path);
1075         if (access(pathl, R_OK) == 0)
1076                 gtk_rc_add_default_file(pathl);
1077         g_free(pathl);
1078         g_free(path);
1079 }
1080
1081 static void exit_program_final(void)
1082 {
1083         LayoutWindow *lw = NULL;
1084         GList *list;
1085         LayoutWindow *tmp_lw;
1086         gchar *archive_dir;
1087         GFile *archive_file;
1088
1089          /* make sure that external editors are loaded, we would save incomplete configuration otherwise */
1090         layout_editors_reload_finish();
1091
1092         remote_close(remote_connection);
1093
1094         collect_manager_flush();
1095
1096         /* Save the named windows */
1097         if (layout_window_list && layout_window_list->next)
1098                 {
1099                 list = layout_window_list;
1100                 while (list)
1101                         {
1102                         tmp_lw = list->data;
1103                         if (!g_str_has_prefix(tmp_lw->options.id, "lw"))
1104                                 {
1105                                 save_layout(list->data);
1106                                 }
1107                         list = list->next;
1108                         }
1109                 }
1110
1111         save_options(options);
1112         keys_save();
1113         accel_map_save();
1114
1115         if (layout_valid(&lw))
1116                 {
1117                 layout_free(lw);
1118                 }
1119
1120         /* Delete any files/folders in /tmp that have been created by the open archive function */
1121         archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, NULL);
1122         if (isdir(archive_dir))
1123                 {
1124                 archive_file = g_file_new_for_path(archive_dir);
1125                 rmdir_recursive(archive_file, NULL, NULL);
1126                 g_free(archive_dir);
1127                 g_object_unref(archive_file);
1128                 }
1129
1130         /* If there are still sub-dirs created by another instance, this will fail
1131          * but that does not matter */
1132         archive_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, NULL);
1133         if (isdir(archive_dir))
1134                 {
1135                 archive_file = g_file_new_for_path(archive_dir);
1136                 g_file_delete(archive_file, NULL, NULL);
1137                 g_free(archive_dir);
1138                 g_object_unref(archive_file);
1139                 }
1140
1141         secure_close(command_line->ssi);
1142
1143         gtk_main_quit();
1144 }
1145
1146 static GenericDialog *exit_dialog = NULL;
1147
1148 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
1149 {
1150         exit_dialog = NULL;
1151         generic_dialog_close(gd);
1152 }
1153
1154 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
1155 {
1156         exit_dialog = NULL;
1157         generic_dialog_close(gd);
1158         exit_program_final();
1159 }
1160
1161 static gint exit_confirm_dlg(void)
1162 {
1163         GtkWidget *parent;
1164         LayoutWindow *lw;
1165         gchar *msg;
1166
1167         if (exit_dialog)
1168                 {
1169                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1170                 return TRUE;
1171                 }
1172
1173         if (!collection_window_modified_exists()) return FALSE;
1174
1175         parent = NULL;
1176         lw = NULL;
1177         if (layout_valid(&lw))
1178                 {
1179                 parent = lw->window;
1180                 }
1181
1182         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1183         exit_dialog = generic_dialog_new(msg,
1184                                 "exit", parent, FALSE,
1185                                 exit_confirm_cancel_cb, NULL);
1186         g_free(msg);
1187         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1188         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
1189                                    msg, _("Collections have been modified. Quit anyway?"), TRUE);
1190         g_free(msg);
1191         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
1192
1193         gtk_widget_show(exit_dialog->dialog);
1194
1195         return TRUE;
1196 }
1197
1198 static void exit_program_write_metadata_cb(gint success, const gchar *dest_path, gpointer data)
1199 {
1200         if (success) exit_program();
1201 }
1202
1203 void exit_program(void)
1204 {
1205         layout_image_full_screen_stop(NULL);
1206
1207         if (metadata_write_queue_confirm(FALSE, exit_program_write_metadata_cb, NULL)) return;
1208
1209         options->marks_save ? marks_save(TRUE) : marks_save(FALSE);
1210
1211         if (exit_confirm_dlg()) return;
1212
1213         exit_program_final();
1214 }
1215
1216 /* This code is supposed to handle situation when a file mmaped by image_loader
1217  * or by exif loader is truncated by some other process.
1218  * This is probably not completely correct according to posix, because
1219  * mmap is not in the list of calls that can be used safely in signal handler,
1220  * but anyway, the handler is called in situation when the application would
1221  * crash otherwise.
1222  * Ideas for improvement are welcome ;)
1223  */
1224 /** @FIXME this probably needs some better ifdefs. Please report any compilation problems */
1225
1226 #if defined(SIGBUS) && defined(SA_SIGINFO)
1227 static void sigbus_handler_cb(int signum, siginfo_t *info, void *context)
1228 {
1229         unsigned long pagesize = sysconf(_SC_PAGE_SIZE);
1230         DEBUG_1("SIGBUS %p", info->si_addr);
1231         mmap((void *)(((unsigned long)info->si_addr / pagesize) * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1232 }
1233 #endif
1234
1235 static void setup_sigbus_handler(void)
1236 {
1237 #if defined(SIGBUS) && defined(SA_SIGINFO)
1238         struct sigaction sigbus_action;
1239         sigfillset(&sigbus_action.sa_mask);
1240         sigbus_action.sa_sigaction = sigbus_handler_cb;
1241         sigbus_action.sa_flags = SA_SIGINFO;
1242
1243         sigaction(SIGBUS, &sigbus_action, NULL);
1244 #endif
1245 }
1246
1247 static void set_theme_bg_color()
1248 {
1249 #if GTK_CHECK_VERSION(3,0,0)
1250         GdkRGBA bg_color;
1251         GdkColor theme_color;
1252         GtkStyleContext *style_context;
1253         GList *work;
1254         LayoutWindow *lw;
1255
1256         if (!options->image.use_custom_border_color)
1257                 {
1258                 work = layout_window_list;
1259                 lw = work->data;
1260
1261                 style_context = gtk_widget_get_style_context(lw->window);
1262                 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &bg_color);
1263
1264                 theme_color.red = bg_color.red * 65535;
1265                 theme_color.green = bg_color.green * 65535;
1266                 theme_color.blue = bg_color.blue * 65535;
1267
1268                 while (work)
1269                         {
1270                         lw = work->data;
1271                         image_background_set_color(lw->image, &theme_color);
1272                         work = work->next;
1273                         }
1274                 }
1275
1276         view_window_colors_update();
1277 #endif
1278 }
1279
1280 static gboolean theme_change_cb(GObject *gobject, GParamSpec *pspec, gpointer data)
1281 {
1282         set_theme_bg_color();
1283
1284         return FALSE;
1285 }
1286
1287 /**
1288  * @brief Set up the application paths
1289  * 
1290  * This function is required for use of AppImages. AppImages are
1291  * relocatable, and therefore cannot use fixed paths to various components.
1292  * These paths were originally #defines created during compilation.
1293  * They are now variables, all defined relative to one level above the
1294  * directory that the executable is run from.
1295  */
1296 static void create_application_paths(gchar *argv[])
1297 {
1298         gchar *dirname;
1299         gchar *tmp;
1300         gint length;
1301         gchar *path;
1302
1303         length = wai_getExecutablePath(NULL, 0, NULL);
1304         path = (gchar *)malloc(length + 1);
1305         wai_getExecutablePath(path, length, NULL);
1306         path[length] = '\0';
1307
1308         gq_executable_path = g_strdup(path);
1309         dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/
1310         gq_prefix = g_path_get_dirname(dirname);
1311
1312         gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL);
1313         tmp = g_build_filename(gq_prefix, "share", "doc", NULL);
1314         gq_helpdir = g_strconcat(tmp, G_DIR_SEPARATOR_S, "geeqie-", VERSION, NULL);
1315         gq_htmldir = g_build_filename(gq_helpdir, "html", NULL);
1316         gq_app_dir = g_build_filename(gq_prefix, "share", "geeqie", NULL);
1317         gq_bin_dir = g_build_filename(gq_prefix, "lib", "geeqie", NULL);
1318         desktop_file_template = g_build_filename(gq_app_dir, "template.desktop", NULL);
1319
1320         g_free(tmp);
1321         g_free(dirname);
1322         g_free(path);
1323 }
1324
1325 gboolean stderr_channel_cb(GIOChannel *source, GIOCondition condition, gpointer data)
1326 {
1327         static GString *message_str = NULL;
1328         gchar buf[10] = {0};
1329         gsize count;
1330
1331         if (!message_str)
1332                 {
1333                 message_str = g_string_new(NULL);
1334                 }
1335
1336         g_io_channel_read_chars(source, buf, 1, &count, NULL);
1337
1338         if (count > 0)
1339                 {
1340                 if (buf[0] == '\n')
1341                         {
1342                         log_printf("%s", message_str->str);
1343                         g_string_free(message_str, TRUE);
1344                         message_str = NULL;
1345                         }
1346                 else
1347                         {
1348                         message_str = g_string_append_c(message_str, buf[0]);
1349                         }
1350                 return TRUE;
1351                 }
1352         else
1353                 {
1354                 return FALSE;
1355                 }
1356 }
1357
1358 gint main(gint argc, gchar *argv[])
1359 {
1360         CollectionData *first_collection = NULL;
1361         gchar *buf;
1362         CollectionData *cd = NULL;
1363         gboolean disable_clutter = FALSE;
1364         gboolean single_dir = TRUE;
1365         LayoutWindow *lw;
1366         GtkSettings *default_settings;
1367         gint fd_stderr[2];
1368         GIOChannel *stderr_channel;
1369
1370 #ifdef HAVE_GTHREAD
1371 #if !GLIB_CHECK_VERSION(2,32,0)
1372         g_thread_init(NULL);
1373 #endif
1374         gdk_threads_init();
1375         gdk_threads_enter();
1376
1377 #endif
1378
1379         /* init execution time counter (debug only) */
1380         init_exec_time();
1381
1382         create_application_paths(argv);
1383
1384         /* setup locale, i18n */
1385         setlocale(LC_ALL, "");
1386
1387 #ifdef ENABLE_NLS
1388         bindtextdomain(PACKAGE, gq_localedir);
1389         bind_textdomain_codeset(PACKAGE, "UTF-8");
1390         textdomain(PACKAGE);
1391 #endif
1392
1393         /* Tee stderr to log window */
1394         if (pipe(fd_stderr) == 0)
1395                 {
1396                 if (dup2(fd_stderr[1], fileno(stderr)) != -1)
1397                         {
1398                         close(fd_stderr[1]);
1399                         stderr_channel = g_io_channel_unix_new(fd_stderr[0]);
1400                         g_io_add_watch(stderr_channel, G_IO_IN, (GIOFunc)stderr_channel_cb, NULL);
1401                         }
1402                 }
1403
1404         exif_init();
1405
1406 #ifdef HAVE_LUA
1407         lua_init();
1408 #endif
1409
1410         /* setup random seed for random slideshow */
1411         srand(time(NULL));
1412
1413         setup_sigbus_handler();
1414
1415         /* register global notify functions */
1416         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1417         file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1418         file_data_register_notify_func(histogram_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
1419         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1420         file_data_register_notify_func(metadata_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
1421
1422
1423         gtkrc_load();
1424
1425         parse_command_line_for_debug_option(argc, argv);
1426         DEBUG_1("%s main: gtk_init", get_exec_time());
1427 #ifdef HAVE_CLUTTER
1428         if (parse_command_line_for_clutter_option(argc, argv))
1429                 {
1430                 disable_clutter = TRUE;
1431                 gtk_init(&argc, &argv);
1432                 }
1433         else
1434                 {
1435                 if (gtk_clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
1436                         {
1437                         log_printf("Can't initialize clutter-gtk.\nStart Geeqie with the option \"geeqie --disable-clutter\"");
1438                         runcmd("zenity --error --title=\"Geeqie\" --text \"Can't initialize clutter-gtk.\n\nStart Geeqie with the option:\n geeqie --disable-clutter\" --width=300");
1439                         exit(1);
1440                         }
1441                 }
1442 #else
1443         gtk_init(&argc, &argv);
1444 #endif
1445
1446         if (gtk_major_version < GTK_MAJOR_VERSION ||
1447             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1448                 {
1449                 log_printf("!!! This is a friendly warning.\n");
1450                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1451                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1452                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1453                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1454                 }
1455
1456         DEBUG_1("%s main: pixbuf_inline_register_stock_icons", get_exec_time());
1457         pixbuf_inline_register_stock_icons();
1458
1459         DEBUG_1("%s main: setting default options before commandline handling", get_exec_time());
1460         options = init_options(NULL);
1461         setup_default_options(options);
1462         if (disable_clutter)
1463                 {
1464                 options->disable_gpu = TRUE;
1465                 }
1466
1467         /* Generate a unique identifier used by the open archive function */
1468         instance_identifier = g_strdup_printf("%x", g_random_int());
1469
1470         DEBUG_1("%s main: mkdir_if_not_exists", get_exec_time());
1471         /* these functions don't depend on config file */
1472         mkdir_if_not_exists(get_rc_dir());
1473         mkdir_if_not_exists(get_collections_dir());
1474         mkdir_if_not_exists(get_thumbnails_cache_dir());
1475         mkdir_if_not_exists(get_metadata_cache_dir());
1476         mkdir_if_not_exists(get_window_layouts_dir());
1477
1478         setup_env_path();
1479
1480         if (parse_command_line_for_cache_maintenance_option(argc, argv))
1481                 {
1482                 process_command_line_for_cache_maintenance_option(argc, argv);
1483                 }
1484         else
1485                 {
1486                 DEBUG_1("%s main: parse_command_line", get_exec_time());
1487                 parse_command_line(argc, argv);
1488
1489                 keys_load();
1490                 accel_map_load();
1491
1492                 /* restore session from the config file */
1493
1494
1495                 DEBUG_1("%s main: load_options", get_exec_time());
1496                 if (!load_options(options))
1497                         {
1498                         /* load_options calls these functions after it parses global options, we have to call it here if it fails */
1499                         filter_add_defaults();
1500                         filter_rebuild();
1501                         }
1502
1503         #ifdef HAVE_CLUTTER
1504         /** @FIXME For the background of this see:
1505          * https://github.com/BestImageViewer/geeqie/issues/397
1506          * The feature CLUTTER_FEATURE_SWAP_EVENTS indictates if the
1507          * system is liable to exhibit this problem.
1508          * The user is provided with an override in Preferences/Behavior
1509          */
1510                 if (!options->override_disable_gpu && !options->disable_gpu)
1511                         {
1512                         DEBUG_1("CLUTTER_FEATURE_SWAP_EVENTS %d",clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS));
1513                         if (clutter_feature_available(CLUTTER_FEATURE_SWAP_EVENTS) != 0)
1514                                 {
1515                                 options->disable_gpu = TRUE;
1516                                 }
1517                         }
1518         #endif
1519
1520                 /* handle missing config file and commandline additions*/
1521                 if (!layout_window_list)
1522                         {
1523                         /* broken or no config file or no <layout> section */
1524                         layout_new_from_default();
1525                         }
1526
1527                 layout_editors_reload_start();
1528
1529                 /* If no --list option, open a separate collection window for each
1530                  * .gqv file on the command line
1531                  */
1532                 if (command_line->collection_list && !command_line->startup_command_line_collection)
1533                         {
1534                         GList *work;
1535
1536                         work = command_line->collection_list;
1537                         while (work)
1538                                 {
1539                                 CollectWindow *cw;
1540                                 const gchar *path;
1541
1542                                 path = work->data;
1543                                 work = work->next;
1544
1545                                 cw = collection_window_new(path);
1546                                 if (!first_collection && cw) first_collection = cw->cd;
1547                                 }
1548                         }
1549
1550                 if (command_line->log_file)
1551                         {
1552                         gchar *pathl;
1553                         gchar *path = g_strdup(command_line->log_file);
1554
1555                         pathl = path_from_utf8(path);
1556                         command_line->ssi = secure_open(pathl);
1557                         }
1558
1559                 /* If there is a files list on the command line and no --list option,
1560                  * check if they are all in the same folder
1561                  */
1562                 if (command_line->cmd_list && !(command_line->startup_command_line_collection))
1563                         {
1564                         GList *work;
1565                         gchar *path = NULL;
1566
1567                         work = command_line->cmd_list;
1568
1569                         while (work && single_dir)
1570                                 {
1571                                 gchar *dirname;
1572
1573                                 dirname = g_path_get_dirname(work->data);
1574                                 if (!path)
1575                                         {
1576                                         path = g_strdup(dirname);
1577                                         }
1578                                 else
1579                                         {
1580                                         if (g_strcmp0(path, dirname) != 0)
1581                                                 {
1582                                                 single_dir = FALSE;
1583                                                 }
1584                                         }
1585                                 g_free(dirname);
1586                                 work = work->next;
1587                                 }
1588                         g_free(path);
1589                         }
1590
1591                 /* Files from multiple folders, or --list option given
1592                  * then open an unnamed collection and insert all files
1593                  */
1594                 if ((command_line->cmd_list && !single_dir) || (command_line->startup_command_line_collection && command_line->cmd_list))
1595                         {
1596                         GList *work;
1597                         CollectWindow *cw;
1598
1599                         cw = collection_window_new(NULL);
1600                         cd = cw->cd;
1601
1602                         collection_path_changed(cd);
1603
1604                         work = command_line->cmd_list;
1605                         while (work)
1606                                 {
1607                                 FileData *fd;
1608
1609                                 fd = file_data_new_simple(work->data);
1610                                 collection_add(cd, fd, FALSE);
1611                                 file_data_unref(fd);
1612                                 work = work->next;
1613                                 }
1614
1615                         work = command_line->collection_list;
1616                         while (work)
1617                                 {
1618                                 collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1619                                 work = work->next;
1620                                 }
1621
1622                         if (cd->list) layout_image_set_collection(NULL, cd, cd->list->data);
1623
1624                         /* mem leak, we never unref this collection when !startup_command_line_collection
1625                          * (the image view of the main window does not hold a ref to the collection)
1626                          * this is sort of unavoidable, for if it did hold a ref, next/back
1627                          * may not work as expected when closing collection windows.
1628                          *
1629                          * collection_unref(cd);
1630                          */
1631
1632                         }
1633                 else if (first_collection)
1634                         {
1635                         layout_image_set_collection(NULL, first_collection,
1636                                                     collection_get_first(first_collection));
1637                         }
1638
1639                 /* If the files on the command line are from one folder, select those files
1640                  * unless it is a command line collection - then leave focus on collection window
1641                  */
1642                 lw = NULL;
1643                 layout_valid(&lw);
1644
1645                 if (single_dir && command_line->cmd_list && !command_line->startup_command_line_collection)
1646                         {
1647                         GList *work;
1648                         GList *selected;
1649                         FileData *fd;
1650
1651                         selected = NULL;
1652                         work = command_line->cmd_list;
1653                         while (work)
1654                                 {
1655                                 fd = file_data_new_simple((gchar *)work->data);
1656                                 selected = g_list_append(selected, fd);
1657                                 file_data_unref(fd);
1658                                 work = work->next;
1659                                 }
1660                         layout_select_list(lw, selected);
1661                         }
1662
1663                 buf = g_build_filename(get_rc_dir(), ".command", NULL);
1664                 remote_connection = remote_server_init(buf, cd);
1665                 g_free(buf);
1666
1667                 marks_load();
1668
1669                 default_settings = gtk_settings_get_default();
1670                 g_signal_connect(default_settings, "notify::gtk-theme-name", G_CALLBACK(theme_change_cb), NULL);
1671                 set_theme_bg_color();
1672                 }
1673
1674         DEBUG_1("%s main: gtk_main", get_exec_time());
1675         gtk_main();
1676 #ifdef HAVE_GTHREAD
1677         gdk_threads_leave();
1678 #endif
1679         return 0;
1680 }
1681 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */