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