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