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