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