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