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