run image loader in separate thread
[geeqie.git] / src / main.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15
16 #include "cache.h"
17 #include "collect.h"
18 #include "collect-io.h"
19 #include "filedata.h"
20 #include "filefilter.h"
21 #include "history_list.h"
22 #include "image-overlay.h"
23 #include "layout.h"
24 #include "layout_image.h"
25 #include "rcfile.h"
26 #include "remote.h"
27 #include "similar.h"
28 #include "ui_fileops.h"
29 #include "ui_utildlg.h"
30 #include "cache_maint.h"
31 #include "thumb.h"
32
33 #include <gdk/gdkkeysyms.h> /* for keyboard values */
34
35
36 #include <math.h>
37 #ifdef G_OS_UNIX
38 #include <pwd.h>
39 #endif
40
41
42 static RemoteConnection *remote_connection = NULL;
43
44
45 /*
46  *-----------------------------------------------------------------------------
47  * misc (public)
48  *-----------------------------------------------------------------------------
49  */
50
51
52 gdouble get_zoom_increment(void)
53 {
54         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
55 }
56
57 gchar *utf8_validate_or_convert(const gchar *text)
58 {
59         gint len;
60
61         if (!text) return NULL;
62         
63         len = strlen(text);
64         if (!g_utf8_validate(text, len, NULL))
65                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
66
67         return g_strdup(text);
68 }
69
70 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
71 {
72         gchar *s1_key, *s2_key;
73         gchar *s1_t, *s2_t;
74         gint ret;
75
76         g_assert(g_utf8_validate(s1, -1, NULL));
77         g_assert(g_utf8_validate(s2, -1, NULL));
78
79         if (!case_sensitive)
80                 {
81                 s1_t = g_utf8_casefold(s1, -1);
82                 s2_t = g_utf8_casefold(s2, -1);
83                 }
84
85         s1_key = g_utf8_collate_key(s1_t, -1);
86         s2_key = g_utf8_collate_key(s2_t, -1);
87
88         ret = strcmp(s1_key, s2_key);
89
90         g_free(s1_key);
91         g_free(s2_key);
92
93         if (!case_sensitive)
94                 {
95                 g_free(s1_t);
96                 g_free(s2_t);
97                 }
98
99         return ret;
100 }
101
102 /* Borrowed from gtkfilesystemunix.c */
103 gchar *expand_tilde(const gchar *filename)
104 {
105 #ifndef G_OS_UNIX
106         return g_strdup(filename);
107 #else
108         const gchar *notilde;
109         const gchar *slash;
110         const gchar *home;
111
112         if (filename[0] != '~')
113                 return g_strdup(filename);
114
115         notilde = filename + 1;
116         slash = strchr(notilde, G_DIR_SEPARATOR);
117         if (slash == notilde || !*notilde)
118                 {
119                 home = g_get_home_dir();
120                 if (!home)
121                         return g_strdup(filename);
122                 }
123         else
124                 {
125                 gchar *username;
126                 struct passwd *passwd;
127
128                 if (slash)
129                         username = g_strndup(notilde, slash - notilde);
130                 else
131                         username = g_strdup(notilde);
132
133                 passwd = getpwnam(username);
134                 g_free(username);
135
136                 if (!passwd)
137                         return g_strdup(filename);
138
139                 home = passwd->pw_dir;
140                 }
141
142         if (slash)
143                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
144         else
145                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
146 #endif
147 }
148
149
150 /*
151  *-----------------------------------------------------------------------------
152  * keyboard functions
153  *-----------------------------------------------------------------------------
154  */
155
156 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
157 {
158         static gint delta = 0;
159         static guint32 time_old = 0;
160         static guint keyval_old = 0;
161
162         if (event->state & GDK_CONTROL_MASK)
163                 {
164                 if (*x < 0) *x = G_MININT / 2;
165                 if (*x > 0) *x = G_MAXINT / 2;
166                 if (*y < 0) *y = G_MININT / 2;
167                 if (*y > 0) *y = G_MAXINT / 2;
168
169                 return;
170                 }
171
172         if (options->progressive_key_scrolling)
173                 {
174                 guint32 time_diff;
175
176                 time_diff = event->time - time_old;
177
178                 /* key pressed within 125ms ? (1/8 second) */
179                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
180
181                 time_old = event->time;
182                 keyval_old = event->keyval;
183
184                 delta += 2;
185                 }
186         else
187                 {
188                 delta = 8;
189                 }
190
191         *x = *x * delta;
192         *y = *y * delta;
193 }
194
195
196
197 /*
198  *-----------------------------------------------------------------------------
199  * command line parser (private) hehe, who needs popt anyway?
200  *-----------------------------------------------------------------------------
201  */
202
203 static gint startup_blank = FALSE;
204 static gint startup_full_screen = FALSE;
205 static gint startup_in_slideshow = FALSE;
206 static gint startup_command_line_collection = FALSE;
207
208
209 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
210                                         GList **list, GList **collection_list)
211 {
212         gchar *path_parsed;
213
214         path_parsed = g_strdup(file_path);
215         parse_out_relatives(path_parsed);
216
217         if (file_extension_match(path_parsed, GQ_COLLECTION_EXT))
218                 {
219                 *collection_list = g_list_append(*collection_list, path_parsed);
220                 }
221         else
222                 {
223                 if (!*path) *path = remove_level_from_path(path_parsed);
224                 if (!*file) *file = g_strdup(path_parsed);
225                 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
226                 }
227 }
228
229 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
230                                        GList **list)
231 {
232         GList *files;
233         gchar *path_parsed;
234         FileData *dir_fd;
235
236         path_parsed = g_strdup(dir);
237         parse_out_relatives(path_parsed);
238         dir_fd = file_data_new_simple(path_parsed);
239         
240
241         if (filelist_read(dir_fd, &files, NULL))
242                 {
243                 GList *work;
244
245                 files = filelist_filter(files, FALSE);
246                 files = filelist_sort_path(files);
247
248                 work = files;
249                 while (work)
250                         {
251                         FileData *fd = work->data;
252                         if (!*path) *path = remove_level_from_path(fd->path);
253                         if (!*file) *file = g_strdup(fd->path);
254                         *list = g_list_prepend(*list, fd);
255
256                         work = work->next;
257                         }
258
259                 g_list_free(files);
260                 }
261
262         g_free(path_parsed);
263         file_data_unref(dir_fd);
264 }
265
266 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
267                                            GList **list, gchar **first_dir)
268 {
269
270         if (!*list && !*first_dir)
271                 {
272                 *first_dir = g_strdup(dir);
273                 }
274         else
275                 {
276                 if (*first_dir)
277                         {
278                         parse_command_line_add_dir(*first_dir, path, file, list);
279                         g_free(*first_dir);
280                         *first_dir = NULL;
281                         }
282                 parse_command_line_add_dir(dir, path, file, list);
283                 }
284 }
285
286 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
287                                             GList **list, GList **collection_list, gchar **first_dir)
288 {
289
290         if (*first_dir)
291                 {
292                 parse_command_line_add_dir(*first_dir, path, file, list);
293                 g_free(*first_dir);
294                 *first_dir = NULL;
295                 }
296         parse_command_line_add_file(file_path, path, file, list, collection_list);
297 }
298
299 static void parse_command_line(gint argc, gchar *argv[], gchar **path, gchar **file,
300                                GList **cmd_list, GList **collection_list,
301                                gchar **geometry)
302 {
303         GList *list = NULL;
304         GList *remote_list = NULL;
305         GList *remote_errors = NULL;
306         gint remote_do = FALSE;
307         gchar *first_dir = NULL;
308
309         if (argc > 1)
310                 {
311                 gint i;
312                 gchar *base_dir = get_current_dir();
313                 i = 1;
314                 while (i < argc)
315                         {
316                         const gchar *cmd_line = argv[i];
317                         gchar *cmd_all = g_build_filename(base_dir, cmd_line, NULL);
318
319                         if (cmd_line[0] == G_DIR_SEPARATOR && isdir(cmd_line))
320                                 {
321                                 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
322                                 }
323                         else if (isdir(cmd_all))
324                                 {
325                                 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
326                                 }
327                         else if (cmd_line[0] == G_DIR_SEPARATOR && isfile(cmd_line))
328                                 {
329                                 parse_command_line_process_file(cmd_line, path, file,
330                                                                 &list, collection_list, &first_dir);
331                                 }
332                         else if (isfile(cmd_all))
333                                 {
334                                 parse_command_line_process_file(cmd_all, path, file,
335                                                                 &list, collection_list, &first_dir);
336                                 }
337                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
338                                 {
339                                 /* do nothing but do not produce warnings */
340                                 }
341                         else if (strcmp(cmd_line, "+t") == 0 ||
342                                  strcmp(cmd_line, "--with-tools") == 0)
343                                 {
344                                 options->layout.tools_float = FALSE;
345                                 options->layout.tools_hidden = FALSE;
346
347                                 remote_list = g_list_append(remote_list, "+t");
348                                 }
349                         else if (strcmp(cmd_line, "-t") == 0 ||
350                                  strcmp(cmd_line, "--without-tools") == 0)
351                                 {
352                                 options->layout.tools_hidden = TRUE;
353
354                                 remote_list = g_list_append(remote_list, "-t");
355                                 }
356                         else if (strcmp(cmd_line, "-f") == 0 ||
357                                  strcmp(cmd_line, "--fullscreen") == 0)
358                                 {
359                                 startup_full_screen = TRUE;
360                                 }
361                         else if (strcmp(cmd_line, "-s") == 0 ||
362                                  strcmp(cmd_line, "--slideshow") == 0)
363                                 {
364                                 startup_in_slideshow = TRUE;
365                                 }
366                         else if (strcmp(cmd_line, "-l") == 0 ||
367                                  strcmp(cmd_line, "--list") == 0)
368                                 {
369                                 startup_command_line_collection = TRUE;
370                                 }
371                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
372                                 {
373                                 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
374                                 }
375                         else if (strcmp(cmd_line, "-r") == 0 ||
376                                  strcmp(cmd_line, "--remote") == 0)
377                                 {
378                                 if (!remote_do)
379                                         {
380                                         remote_do = TRUE;
381                                         remote_list = remote_build_list(remote_list, argc - i, &argv[i], &remote_errors);
382                                         }
383                                 }
384                         else if (strcmp(cmd_line, "-rh") == 0 ||
385                                  strcmp(cmd_line, "--remote-help") == 0)
386                                 {
387                                 remote_help();
388                                 exit(0);
389                                 }
390                         else if (strcmp(cmd_line, "--blank") == 0)
391                                 {
392                                 startup_blank = TRUE;
393                                 }
394                         else if (strcmp(cmd_line, "-v") == 0 ||
395                                  strcmp(cmd_line, "--version") == 0)
396                                 {
397                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
398                                 exit(0);
399                                 }
400                         else if (strcmp(cmd_line, "--alternate") == 0)
401                                 {
402                                 /* enable faster experimental algorithm */
403                                 log_printf("Alternate similarity algorithm enabled\n");
404                                 image_sim_alternate_set(TRUE);
405                                 }
406                         else if (strcmp(cmd_line, "-h") == 0 ||
407                                  strcmp(cmd_line, "--help") == 0)
408                                 {
409                                 printf_term("%s %s\n", GQ_APPNAME, VERSION);
410                                 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
411                                 print_term(_("valid options are:\n"));
412                                 print_term(_("  +t, --with-tools           force show of tools\n"));
413                                 print_term(_("  -t, --without-tools        force hide of tools\n"));
414                                 print_term(_("  -f, --fullscreen           start in full screen mode\n"));
415                                 print_term(_("  -s, --slideshow            start in slideshow mode\n"));
416                                 print_term(_("  -l, --list                 open collection window for command line\n"));
417                                 print_term(_("      --geometry=GEOMETRY    set main window location\n"));
418                                 print_term(_("  -r, --remote               send following commands to open window\n"));
419                                 print_term(_("  -rh,--remote-help          print remote command list\n"));
420 #ifdef DEBUG
421                                 print_term(_("  --debug[=level]            turn on debug output\n"));
422 #endif
423                                 print_term(_("  -v, --version              print version info\n"));
424                                 print_term(_("  -h, --help                 show this message\n\n"));
425
426 #if 0
427                                 /* these options are not officially supported!
428                                  * only for testing new features, no need to translate them */
429                                 print_term(  "  --alternate                use alternate similarity algorithm\n");
430 #endif
431
432                                 exit(0);
433                                 }
434                         else if (!remote_do)
435                                 {
436                                 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
437                                 }
438
439                         g_free(cmd_all);
440                         i++;
441                         }
442                 g_free(base_dir);
443                 parse_out_relatives(*path);
444                 parse_out_relatives(*file);
445                 }
446
447         list = g_list_reverse(list);
448
449         if (!*path && first_dir)
450                 {
451                 *path = first_dir;
452                 first_dir = NULL;
453
454                 parse_out_relatives(*path);
455                 }
456         g_free(first_dir);
457
458         if (remote_do)
459                 {
460                 if (remote_errors)
461                         {
462                         GList *work = remote_errors;
463                         
464                         printf_term(_("Invalid or ignored remote options: "));
465                         while (work)
466                                 {
467                                 gchar *opt = work->data;
468                                                 
469                                 printf_term("%s%s", (work == remote_errors) ? "" : ", ", opt);
470                                 work = work->next;
471                                 }
472
473                         printf_term(_("\nUse --remote-help for valid remote options.\n"));
474                         }
475
476                 remote_control(argv[0], remote_list, *path, list, *collection_list);
477                 }
478         g_list_free(remote_list);
479
480         if (list && list->next)
481                 {
482                 *cmd_list = list;
483                 }
484         else
485                 {
486                 filelist_free(list);
487                 *cmd_list = NULL;
488                 }
489 }
490
491 static void parse_command_line_for_debug_option(gint argc, gchar *argv[])
492 {
493 #ifdef DEBUG
494         const gchar *debug_option = "--debug";
495         gint len = strlen(debug_option);
496
497         if (argc > 1)
498                 {
499                 gint i;
500
501                 for (i = 1; i < argc; i++)
502                         {
503                         const gchar *cmd_line = argv[i];
504                         if (strncmp(cmd_line, debug_option, len) == 0)
505                                 {
506                                 gint cmd_line_len = strlen(cmd_line);
507
508                                 /* we now increment the debug state for verbosity */
509                                 if (cmd_line_len == len)
510                                         debug_level_add(1);
511                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
512                                         {
513                                         gint n = atoi(cmd_line + len + 1);
514                                         if (n < 0) n = 1;
515                                         debug_level_add(n);
516                                         }
517                                 }
518                         }
519                 }
520
521         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
522 #endif
523 }
524
525 /*
526  *-----------------------------------------------------------------------------
527  * startup, init, and exit
528  *-----------------------------------------------------------------------------
529  */
530
531 #define RC_HISTORY_NAME "history"
532
533 static void keys_load(void)
534 {
535         gchar *path;
536
537         path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
538         history_list_load(path);
539         g_free(path);
540 }
541
542 static void keys_save(void)
543 {
544         gchar *path;
545
546         path = g_build_filename(homedir(), GQ_RC_DIR, RC_HISTORY_NAME, NULL);
547         history_list_save(path);
548         g_free(path);
549 }
550
551 static void check_for_home_path(gchar *path)
552 {
553         gchar *buf;
554
555         buf = g_build_filename(homedir(), path, NULL);
556         if (!isdir(buf))
557                 {
558                 log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
559
560                 if (!mkdir_utf8(buf, 0755))
561                         {
562                         log_printf(_("Could not create dir:%s\n"), buf);
563                         }
564                 }
565         g_free(buf);
566 }
567
568
569 static void exit_program_final(void)
570 {
571         gchar *path;
572         gchar *pathl;
573         LayoutWindow *lw = NULL;
574
575         remote_close(remote_connection);
576
577         collect_manager_flush();
578
579         sync_options_with_current_state(options);
580         save_options(options);
581         keys_save();
582
583         path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
584         pathl = path_from_utf8(path);
585         gtk_accel_map_save(pathl);
586         g_free(pathl);
587         g_free(path);
588
589         if (layout_valid(&lw))
590                 {
591                 layout_free(lw);
592                 }
593
594         gtk_main_quit();
595 }
596
597 static GenericDialog *exit_dialog = NULL;
598
599 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
600 {
601         exit_dialog = NULL;
602         generic_dialog_close(gd);
603 }
604
605 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
606 {
607         exit_dialog = NULL;
608         generic_dialog_close(gd);
609         exit_program_final();
610 }
611
612 static gint exit_confirm_dlg(void)
613 {
614         GtkWidget *parent;
615         LayoutWindow *lw;
616         gchar *msg;
617
618         if (exit_dialog)
619                 {
620                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
621                 return TRUE;
622                 }
623
624         if (!collection_window_modified_exists()) return FALSE;
625
626         parent = NULL;
627         lw = NULL;
628         if (layout_valid(&lw))
629                 {
630                 parent = lw->window;
631                 }
632
633         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
634         exit_dialog = generic_dialog_new(msg,
635                                 GQ_WMCLASS, "exit", parent, FALSE,
636                                 exit_confirm_cancel_cb, NULL);
637         g_free(msg);
638         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
639         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
640                                    msg, _("Collections have been modified. Quit anyway?"));
641         g_free(msg);
642         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
643
644         gtk_widget_show(exit_dialog->dialog);
645
646         return TRUE;
647 }
648
649 void exit_program(void)
650 {
651         layout_image_full_screen_stop(NULL);
652
653         if (exit_confirm_dlg()) return;
654
655         exit_program_final();
656 }
657
658 gint main(gint argc, gchar *argv[])
659 {
660         LayoutWindow *lw;
661         gchar *path = NULL;
662         gchar *cmd_path = NULL;
663         gchar *cmd_file = NULL;
664         GList *cmd_list = NULL;
665         GList *collection_list = NULL;
666         CollectionData *first_collection = NULL;
667         gchar *geometry = NULL;
668         gchar *buf;
669         gchar *bufl;
670         CollectionData *cd = NULL;
671
672 #ifdef HAVE_GTHREAD
673         g_thread_init (NULL);
674 #endif
675         
676         /* init execution time counter (debug only) */
677         init_exec_time();
678
679         /* setup locale, i18n */
680         gtk_set_locale();
681
682 #ifdef ENABLE_NLS
683         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
684         bind_textdomain_codeset(PACKAGE, "UTF-8");
685         textdomain(PACKAGE);
686 #endif
687
688         /* setup random seed for random slideshow */
689         srand(time(NULL));
690
691 #if 1
692         log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
693 #endif
694
695         /* register global notify functions */
696         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
697         file_data_register_notify_func(thumb_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
698         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
699
700         parse_command_line_for_debug_option(argc, argv);
701
702         options = init_options(NULL);
703         setup_default_options(options);
704         load_options(options);
705
706         parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
707
708         /* If a gtkrc file exists in the rc directory, add it to the
709          * list of files to be parsed at the end of gtk_init() */
710         buf = g_build_filename(homedir(), GQ_RC_DIR, "gtkrc", NULL);
711         bufl = path_from_utf8(buf);
712         if (access(bufl, R_OK) == 0)
713                 gtk_rc_add_default_file(bufl);
714         g_free(bufl);
715         g_free(buf);
716
717         gtk_init(&argc, &argv);
718
719         if (gtk_major_version < GTK_MAJOR_VERSION ||
720             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
721                 {
722                 log_printf("!!! This is a friendly warning.\n");
723                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
724                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
725                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
726                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
727                 }
728
729         check_for_home_path(GQ_RC_DIR);
730         check_for_home_path(GQ_RC_DIR_COLLECTIONS);
731         check_for_home_path(GQ_CACHE_RC_THUMB);
732         check_for_home_path(GQ_CACHE_RC_METADATA);
733
734         keys_load();
735         filter_add_defaults();
736         filter_rebuild();
737
738         buf = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
739         bufl = path_from_utf8(buf);
740         gtk_accel_map_load(bufl);
741         g_free(bufl);
742         g_free(buf);
743
744         if (startup_blank)
745                 {
746                 g_free(cmd_path);
747                 cmd_path = NULL;
748                 g_free(cmd_file);
749                 cmd_file = NULL;
750                 filelist_free(cmd_list);
751                 cmd_list = NULL;
752                 string_list_free(collection_list);
753                 collection_list = NULL;
754
755                 path = NULL;
756                 }
757         else if (cmd_path)
758                 {
759                 path = g_strdup(cmd_path);
760                 }
761         else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
762                 {
763                 path = g_strdup(options->startup.path);
764                 }
765         else
766                 {
767                 path = get_current_dir();
768                 }
769
770         lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
771         layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
772
773         if (collection_list && !startup_command_line_collection)
774                 {
775                 GList *work;
776
777                 work = collection_list;
778                 while (work)
779                         {
780                         CollectWindow *cw;
781                         const gchar *path;
782
783                         path = work->data;
784                         work = work->next;
785
786                         cw = collection_window_new(path);
787                         if (!first_collection && cw) first_collection = cw->cd;
788                         }
789                 }
790
791         if (cmd_list ||
792             (startup_command_line_collection && collection_list))
793                 {
794                 GList *work;
795
796                 if (startup_command_line_collection)
797                         {
798                         CollectWindow *cw;
799
800                         cw = collection_window_new("");
801                         cd = cw->cd;
802                         }
803                 else
804                         {
805                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
806                         }
807
808                 g_free(cd->path);
809                 cd->path = NULL;
810                 g_free(cd->name);
811                 cd->name = g_strdup(_("Command line"));
812
813                 collection_path_changed(cd);
814
815                 work = cmd_list;
816                 while (work)
817                         {
818                         collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
819                         work = work->next;
820                         }
821
822                 work = collection_list;
823                 while (work)
824                         {
825                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
826                         work = work->next;
827                         }
828
829                 layout_set_path(lw, path);
830                 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
831
832                 /* mem leak, we never unref this collection when !startup_command_line_collection
833                  * (the image view of the main window does not hold a ref to the collection)
834                  * this is sort of unavoidable, for if it did hold a ref, next/back
835                  * may not work as expected when closing collection windows.
836                  *
837                  * collection_unref(cd);
838                  */
839
840                 }
841         else if (cmd_file)
842                 {
843                 layout_set_path(lw, cmd_file);
844                 }
845         else
846                 {
847                 layout_set_path(lw, path);
848                 if (first_collection)
849                         {
850                         layout_image_set_collection(lw, first_collection,
851                                                     collection_get_first(first_collection));
852                         }
853                 }
854
855         image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
856
857         g_free(geometry);
858         g_free(cmd_path);
859         g_free(cmd_file);
860         filelist_free(cmd_list);
861         string_list_free(collection_list);
862         g_free(path);
863
864         if (startup_full_screen) layout_image_full_screen_start(lw);
865         if (startup_in_slideshow) layout_image_slideshow_start(lw);
866
867         buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
868         remote_connection = remote_server_init(buf, cd);
869         g_free(buf);
870
871         gtk_main();
872         return 0;
873 }