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