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