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