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