use the new notification for collections
[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
573         remote_close(remote_connection);
574
575         collect_manager_flush();
576
577         sync_options_with_current_state(options);
578         save_options(options);
579         keys_save();
580
581         path = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
582         pathl = path_from_utf8(path);
583         gtk_accel_map_save(pathl);
584         g_free(pathl);
585         g_free(path);
586
587         gtk_main_quit();
588 }
589
590 static GenericDialog *exit_dialog = NULL;
591
592 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
593 {
594         exit_dialog = NULL;
595         generic_dialog_close(gd);
596 }
597
598 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
599 {
600         exit_dialog = NULL;
601         generic_dialog_close(gd);
602         exit_program_final();
603 }
604
605 static gint exit_confirm_dlg(void)
606 {
607         GtkWidget *parent;
608         LayoutWindow *lw;
609         gchar *msg;
610
611         if (exit_dialog)
612                 {
613                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
614                 return TRUE;
615                 }
616
617         if (!collection_window_modified_exists()) return FALSE;
618
619         parent = NULL;
620         lw = NULL;
621         if (layout_valid(&lw))
622                 {
623                 parent = lw->window;
624                 }
625
626         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
627         exit_dialog = generic_dialog_new(msg,
628                                 GQ_WMCLASS, "exit", parent, FALSE,
629                                 exit_confirm_cancel_cb, NULL);
630         g_free(msg);
631         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
632         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
633                                    msg, _("Collections have been modified. Quit anyway?"));
634         g_free(msg);
635         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
636
637         gtk_widget_show(exit_dialog->dialog);
638
639         return TRUE;
640 }
641
642 void exit_program(void)
643 {
644         layout_image_full_screen_stop(NULL);
645
646         if (exit_confirm_dlg()) return;
647
648         exit_program_final();
649 }
650
651 int main(int argc, char *argv[])
652 {
653         LayoutWindow *lw;
654         gchar *path = NULL;
655         gchar *cmd_path = NULL;
656         gchar *cmd_file = NULL;
657         GList *cmd_list = NULL;
658         GList *collection_list = NULL;
659         CollectionData *first_collection = NULL;
660         gchar *geometry = NULL;
661         gchar *buf;
662         gchar *bufl;
663         CollectionData *cd = NULL;
664
665         /* init execution time counter (debug only) */
666         init_exec_time();
667
668         /* setup locale, i18n */
669         gtk_set_locale();
670
671 #ifdef ENABLE_NLS
672         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
673         bind_textdomain_codeset(PACKAGE, "UTF-8");
674         textdomain(PACKAGE);
675 #endif
676     
677         /* setup random seed for random slideshow */
678         srand(time(NULL));
679
680 #if 1
681         log_printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
682 #endif
683
684         /* register global notify functions */
685         file_data_register_notify_func(cache_notify_cb, NULL, NOTIFY_PRIORITY_HIGH);
686         file_data_register_notify_func(collect_manager_notify_cb, NULL, NOTIFY_PRIORITY_LOW);
687
688         parse_command_line_for_debug_option(argc, argv);
689
690         options = init_options(NULL);
691         setup_default_options(options);
692         load_options(options);
693
694         parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
695
696         /* If a gtkrc file exists in the rc directory, add it to the
697          * list of files to be parsed at the end of gtk_init() */
698         buf = g_build_filename(homedir(), GQ_RC_DIR, "gtkrc", NULL);
699         bufl = path_from_utf8(buf);
700         if (access(bufl, R_OK) == 0)
701                 gtk_rc_add_default_file(bufl);
702         g_free(bufl);
703         g_free(buf);
704
705         gtk_init(&argc, &argv);
706
707         if (gtk_major_version < GTK_MAJOR_VERSION ||
708             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
709                 {
710                 log_printf("!!! This is a friendly warning.\n");
711                 log_printf("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
712                 log_printf("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
713                 log_printf("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
714                 log_printf("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
715                 }
716
717         check_for_home_path(GQ_RC_DIR);
718         check_for_home_path(GQ_RC_DIR_COLLECTIONS);
719         check_for_home_path(GQ_CACHE_RC_THUMB);
720         check_for_home_path(GQ_CACHE_RC_METADATA);
721
722         keys_load();
723         filter_add_defaults();
724         filter_rebuild();
725
726         buf = g_build_filename(homedir(), GQ_RC_DIR, "accels", NULL);
727         bufl = path_from_utf8(buf);
728         gtk_accel_map_load(bufl);
729         g_free(bufl);
730         g_free(buf);
731
732         if (startup_blank)
733                 {
734                 g_free(cmd_path);
735                 cmd_path = NULL;
736                 g_free(cmd_file);
737                 cmd_file = NULL;
738                 filelist_free(cmd_list);
739                 cmd_list = NULL;
740                 string_list_free(collection_list);
741                 collection_list = NULL;
742
743                 path = NULL;
744                 }
745         else if (cmd_path)
746                 {
747                 path = g_strdup(cmd_path);
748                 }
749         else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
750                 {
751                 path = g_strdup(options->startup.path);
752                 }
753         else
754                 {
755                 path = get_current_dir();
756                 }
757
758         lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
759         layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
760
761         if (collection_list && !startup_command_line_collection)
762                 {
763                 GList *work;
764
765                 work = collection_list;
766                 while (work)
767                         {
768                         CollectWindow *cw;
769                         const gchar *path;
770
771                         path = work->data;
772                         work = work->next;
773
774                         cw = collection_window_new(path);
775                         if (!first_collection && cw) first_collection = cw->cd;
776                         }
777                 }
778
779         if (cmd_list ||
780             (startup_command_line_collection && collection_list))
781                 {
782                 GList *work;
783
784                 if (startup_command_line_collection)
785                         {
786                         CollectWindow *cw;
787
788                         cw = collection_window_new("");
789                         cd = cw->cd;
790                         }
791                 else
792                         {
793                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
794                         }
795
796                 g_free(cd->path);
797                 cd->path = NULL;
798                 g_free(cd->name);
799                 cd->name = g_strdup(_("Command line"));
800
801                 collection_path_changed(cd);
802
803                 work = cmd_list;
804                 while (work)
805                         {
806                         collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
807                         work = work->next;
808                         }
809
810                 work = collection_list;
811                 while (work)
812                         {
813                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
814                         work = work->next;
815                         }
816
817                 layout_set_path(lw, path);
818                 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
819
820                 /* mem leak, we never unref this collection when !startup_command_line_collection
821                  * (the image view of the main window does not hold a ref to the collection)
822                  * this is sort of unavoidable, for if it did hold a ref, next/back
823                  * may not work as expected when closing collection windows.
824                  *
825                  * collection_unref(cd);
826                  */
827
828                 }
829         else if (cmd_file)
830                 {
831                 layout_set_path(lw, cmd_file);
832                 }
833         else
834                 {
835                 layout_set_path(lw, path);
836                 if (first_collection)
837                         {
838                         layout_image_set_collection(lw, first_collection,
839                                                     collection_get_first(first_collection));
840                         }
841                 }
842
843         image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
844
845         g_free(geometry);
846         g_free(cmd_path);
847         g_free(cmd_file);
848         filelist_free(cmd_list);
849         string_list_free(collection_list);
850         g_free(path);
851
852         if (startup_full_screen) layout_image_full_screen_start(lw);
853         if (startup_in_slideshow) layout_image_slideshow_start(lw);
854
855         buf = g_build_filename(homedir(), GQ_RC_DIR, ".command", NULL);
856         remote_connection = remote_server_init(buf, cd);
857         g_free(buf);
858
859         gtk_main();
860         return 0;
861 }