Move some functions from main.[ch] to new window.[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 static CollectionData *command_collection = NULL;
52
53
54 /*
55  *-----------------------------------------------------------------------------
56  * misc (public)
57  *-----------------------------------------------------------------------------
58  */
59
60
61 gdouble get_zoom_increment(void)
62 {
63         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
64 }
65
66 gchar *utf8_validate_or_convert(gchar *text)
67 {
68         gint len;
69
70         if (!text) return NULL;
71         
72         len = strlen(text);
73         if (!g_utf8_validate(text, len, NULL))
74                 {
75                 gchar *conv_text;
76
77                 conv_text = g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
78                 g_free(text);
79                 text = conv_text;
80                 }
81
82         return text;
83 }
84
85 /*
86  *-----------------------------------------------------------------------------
87  * keyboard functions
88  *-----------------------------------------------------------------------------
89  */
90
91 void keyboard_scroll_calc(gint *x, gint *y, GdkEventKey *event)
92 {
93         static gint delta = 0;
94         static guint32 time_old = 0;
95         static guint keyval_old = 0;
96
97         if (event->state & GDK_CONTROL_MASK)
98                 {
99                 if (*x < 0) *x = G_MININT / 2;
100                 if (*x > 0) *x = G_MAXINT / 2;
101                 if (*y < 0) *y = G_MININT / 2;
102                 if (*y > 0) *y = G_MAXINT / 2;
103
104                 return;
105                 }
106
107         if (options->progressive_key_scrolling)
108                 {
109                 guint32 time_diff;
110
111                 time_diff = event->time - time_old;
112
113                 /* key pressed within 125ms ? (1/8 second) */
114                 if (time_diff > 125 || event->keyval != keyval_old) delta = 0;
115
116                 time_old = event->time;
117                 keyval_old = event->keyval;
118
119                 delta += 2;
120                 }
121         else
122                 {
123                 delta = 8;
124                 }
125
126         *x = *x * delta;
127         *y = *y * delta;
128 }
129
130
131 /*
132  *-----------------------------------------------------------------------------
133  * remote functions
134  *-----------------------------------------------------------------------------
135  */
136
137 static void gr_image_next(const gchar *text, gpointer data)
138 {
139         layout_image_next(NULL);
140 }
141
142 static void gr_image_prev(const gchar *text, gpointer data)
143 {
144         layout_image_prev(NULL);
145 }
146
147 static void gr_image_first(const gchar *text, gpointer data)
148 {
149         layout_image_first(NULL);
150 }
151
152 static void gr_image_last(const gchar *text, gpointer data)
153 {
154         layout_image_last(NULL);
155 }
156
157 static void gr_fullscreen_toggle(const gchar *text, gpointer data)
158 {
159         layout_image_full_screen_toggle(NULL);
160 }
161
162 static void gr_fullscreen_start(const gchar *text, gpointer data)
163 {
164         layout_image_full_screen_start(NULL);
165 }
166
167 static void gr_fullscreen_stop(const gchar *text, gpointer data)
168 {
169         layout_image_full_screen_stop(NULL);
170 }
171
172 static void gr_slideshow_start_rec(const gchar *text, gpointer data)
173 {
174         GList *list;
175
176         list = filelist_recursive(text);
177         if (!list) return;
178 //printf("length: %d\n", g_list_length(list));
179         layout_image_slideshow_stop(NULL);
180         layout_image_slideshow_start_from_list(NULL, list);
181 }
182
183 static void gr_slideshow_toggle(const gchar *text, gpointer data)
184 {
185         layout_image_slideshow_toggle(NULL);
186 }
187
188 static void gr_slideshow_start(const gchar *text, gpointer data)
189 {
190         layout_image_slideshow_start(NULL);
191 }
192
193 static void gr_slideshow_stop(const gchar *text, gpointer data)
194 {
195         layout_image_slideshow_stop(NULL);
196 }
197
198 static void gr_slideshow_delay(const gchar *text, gpointer data)
199 {
200         gdouble n;
201
202         n = strtod(text, NULL);
203         if (n < SLIDESHOW_MIN_SECONDS || n > SLIDESHOW_MAX_SECONDS)
204                 {
205                 printf_term("Remote slideshow delay out of range (%.1f to %.1f)\n",
206                             SLIDESHOW_MIN_SECONDS, SLIDESHOW_MAX_SECONDS);
207                 return;
208                 }
209         options->slideshow.delay = (gint)(n * 10.0 + 0.01);
210 }
211
212 static void gr_tools_show(const gchar *text, gpointer data)
213 {
214         gint popped;
215         gint hidden;
216
217         if (layout_tools_float_get(NULL, &popped, &hidden) && hidden)
218                 {
219                 layout_tools_float_set(NULL, popped, FALSE);
220                 }
221 }
222
223 static void gr_tools_hide(const gchar *text, gpointer data)
224 {
225         gint popped;
226         gint hidden;
227
228         if (layout_tools_float_get(NULL, &popped, &hidden) && !hidden)
229                 {
230                 layout_tools_float_set(NULL, popped, TRUE);
231                 }
232 }
233
234 static gint gr_quit_idle_cb(gpointer data)
235 {
236         exit_program();
237
238         return FALSE;
239 }
240
241 static void gr_quit(const gchar *text, gpointer data)
242 {
243         /* schedule exit when idle, if done from within a
244          * remote handler remote_close will crash
245          */
246         g_idle_add(gr_quit_idle_cb, NULL);
247 }
248
249 static void gr_file_load(const gchar *text, gpointer data)
250 {
251         if (isfile(text))
252                 {
253                 if (file_extension_match(text, ".gqv"))
254                         {
255                         collection_window_new(text);
256                         }
257                 else
258                         {
259                         layout_set_path(NULL, text);
260                         }
261                 }
262         else if (isdir(text))
263                 {
264                 layout_set_path(NULL, text);
265                 }
266         else
267                 {
268                 printf("remote sent filename that does not exist:\"%s\"\n", text);
269                 }
270 }
271
272 static void gr_file_view(const gchar *text, gpointer data)
273 {
274         view_window_new(file_data_new_simple(text));
275 }
276
277 static void gr_list_clear(const gchar *text, gpointer data)
278 {
279         if (command_collection) collection_unref(command_collection);
280         command_collection = NULL;
281 }
282
283 static void gr_list_add(const gchar *text, gpointer data)
284 {
285         gint new = TRUE;
286
287         if (!command_collection)
288                 {
289                 CollectionData *cd;
290
291                 cd = collection_new("");
292
293                 g_free(cd->path);
294                 cd->path = NULL;
295                 g_free(cd->name);
296                 cd->name = g_strdup(_("Command line"));
297
298                 command_collection = cd;
299                 }
300         else
301                 {
302                 new = (!collection_get_first(command_collection));
303                 }
304
305         if (collection_add(command_collection, file_data_new_simple(text), FALSE) && new)
306                 {
307                 layout_image_set_collection(NULL, command_collection,
308                                             collection_get_first(command_collection));
309                 }
310 }
311
312 static void gr_raise(const gchar *text, gpointer data)
313 {
314         LayoutWindow *lw = NULL;
315
316         if (layout_valid(&lw))
317                 {
318                 gtk_window_present(GTK_WINDOW(lw->window));
319                 }
320 }
321
322 typedef struct _RemoteCommandEntry RemoteCommandEntry;
323 struct _RemoteCommandEntry {
324         gchar *opt_s;
325         gchar *opt_l;
326         void (*func)(const gchar *text, gpointer data);
327         gint needs_extra;
328         gint prefer_command_line;
329         gchar *description;
330 };
331
332 static RemoteCommandEntry remote_commands[] = {
333         /* short, long                  callback,               extra, prefer,description */
334         { "-n", "--next",               gr_image_next,          FALSE, FALSE, N_("next image") },
335         { "-b", "--back",               gr_image_prev,          FALSE, FALSE, N_("previous image") },
336         { NULL, "--first",              gr_image_first,         FALSE, FALSE, N_("first image") },
337         { NULL, "--last",               gr_image_last,          FALSE, FALSE, N_("last image") },
338         { "-f", "--fullscreen",         gr_fullscreen_toggle,   FALSE, TRUE,  N_("toggle full screen") },
339         { "-fs","--fullscreen-start",   gr_fullscreen_start,    FALSE, FALSE, N_("start full screen") },
340         { "-fS","--fullscreen-stop",    gr_fullscreen_stop,     FALSE, FALSE, N_("stop full screen") },
341         { "-s", "--slideshow",          gr_slideshow_toggle,    FALSE, TRUE,  N_("toggle slide show") },
342         { "-ss","--slideshow-start",    gr_slideshow_start,     FALSE, FALSE, N_("start slide show") },
343         { "-sS","--slideshow-stop",     gr_slideshow_stop,      FALSE, FALSE, N_("stop slide show") },
344         { "-sr","--slideshow-recurse",  gr_slideshow_start_rec, TRUE,  FALSE, N_("start recursive slide show") },
345         { "-d", "--delay=",             gr_slideshow_delay,     TRUE,  FALSE, N_("set slide show delay in seconds") },
346         { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  N_("show tools") },
347         { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  N_("hide tools") },
348         { "-q", "--quit",               gr_quit,                FALSE, FALSE, N_("quit") },
349         { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("open file") },
350         { NULL, "view:",                gr_file_view,           TRUE,  FALSE, N_("open file in new window") },
351         { NULL, "--list-clear",         gr_list_clear,          FALSE, FALSE, NULL },
352         { NULL, "--list-add:",          gr_list_add,            TRUE,  FALSE, NULL },
353         { NULL, "raise",                gr_raise,               FALSE, FALSE, NULL },
354         { NULL, NULL, NULL, FALSE, FALSE, NULL }
355 };
356
357 static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **offset)
358 {
359         gint match = FALSE;
360         gint i;
361
362         i = 0;
363         while (!match && remote_commands[i].func != NULL)
364                 {
365                 if (remote_commands[i].needs_extra)
366                         {
367                         if (remote_commands[i].opt_s &&
368                             strncmp(remote_commands[i].opt_s, text, strlen(remote_commands[i].opt_s)) == 0)
369                                 {
370                                 if (offset) *offset = text + strlen(remote_commands[i].opt_s);
371                                 return &remote_commands[i];
372                                 }
373                         else if (remote_commands[i].opt_l &&
374                                  strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
375                                 {
376                                 if (offset) *offset = text + strlen(remote_commands[i].opt_l);
377                                 return &remote_commands[i];
378                                 }
379                         }
380                 else
381                         {
382                         if ((remote_commands[i].opt_s && strcmp(remote_commands[i].opt_s, text) == 0) ||
383                             (remote_commands[i].opt_l && strcmp(remote_commands[i].opt_l, text) == 0))
384                                 {
385                                 if (offset) *offset = text;
386                                 return &remote_commands[i];
387                                 }
388                         }
389
390                 i++;
391                 }
392
393         return NULL;
394 }
395
396 static void remote_cb(RemoteConnection *rc, const gchar *text, gpointer data)
397 {
398         RemoteCommandEntry *entry;
399         const gchar *offset;
400
401         entry = remote_command_find(text, &offset);
402         if (entry && entry->func)
403                 {
404                 entry->func(offset, data);
405                 }
406         else
407                 {
408                 printf("unknown remote command:%s\n", text);
409                 }
410 }
411
412 static void remote_help(void)
413 {
414         gint i;
415
416         print_term(_("Remote command list:\n"));
417
418         i = 0;
419         while (remote_commands[i].func != NULL)
420                 {
421                 if (remote_commands[i].description)
422                         {
423                         printf_term("  %-3s%s %-20s %s\n",
424                                     (remote_commands[i].opt_s) ? remote_commands[i].opt_s : "",
425                                     (remote_commands[i].opt_s && remote_commands[i].opt_l) ? "," : " ",
426                                     (remote_commands[i].opt_l) ? remote_commands[i].opt_l : "",
427                                     _(remote_commands[i].description));
428                         }
429                 i++;
430                 }
431 }
432
433 static GList *remote_build_list(GList *list, int argc, char *argv[])
434 {
435         gint i;
436
437         i = 1;
438         while (i < argc)
439                 {
440                 RemoteCommandEntry *entry;
441
442                 entry = remote_command_find(argv[i], NULL);
443                 if (entry)
444                         {
445                         list = g_list_append(list, argv[i]);
446                         }
447                 i++;
448                 }
449
450         return list;
451 }
452
453 static void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path,
454                                   GList *cmd_list, GList *collection_list)
455 {
456         RemoteConnection *rc;
457         gint started = FALSE;
458         gchar *buf;
459
460         buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
461         rc = remote_client_open(buf);
462         if (!rc)
463                 {
464                 GString *command;
465                 GList *work;
466                 gint retry_count = 12;
467                 gint blank = FALSE;
468
469                 printf_term(_("Remote %s not running, starting..."), GQ_APPNAME);
470
471                 command = g_string_new(arg_exec);
472
473                 work = remote_list;
474                 while (work)
475                         {
476                         gchar *text;
477                         RemoteCommandEntry *entry;
478
479                         text = work->data;
480                         work = work->next;
481
482                         entry = remote_command_find(text, NULL);
483                         if (entry)
484                                 {
485                                 if (entry->prefer_command_line)
486                                         {
487                                         remote_list = g_list_remove(remote_list, text);
488                                         g_string_append(command, " ");
489                                         g_string_append(command, text);
490                                         }
491                                 if (entry->opt_l && strcmp(entry->opt_l, "file:") == 0)
492                                         {
493                                         blank = TRUE;
494                                         }
495                                 }
496                         }
497
498                 if (blank || cmd_list || path) g_string_append(command, " --blank");
499                 if (get_debug_level()) g_string_append(command, " --debug");
500
501                 g_string_append(command, " &");
502                 system(command->str);
503                 g_string_free(command, TRUE);
504
505                 while (!rc && retry_count > 0)
506                         {
507                         usleep((retry_count > 10) ? 500000 : 1000000);
508                         rc = remote_client_open(buf);
509                         if (!rc) print_term(".");
510                         retry_count--;
511                         }
512
513                 print_term("\n");
514
515                 started = TRUE;
516                 }
517         g_free(buf);
518
519         if (rc)
520                 {
521                 GList *work;
522                 const gchar *prefix;
523                 gint use_path = TRUE;
524                 gint sent = FALSE;
525
526                 work = remote_list;
527                 while (work)
528                         {
529                         gchar *text;
530                         RemoteCommandEntry *entry;
531
532                         text = work->data;
533                         work = work->next;
534
535                         entry = remote_command_find(text, NULL);
536                         if (entry &&
537                             entry->opt_l &&
538                             strcmp(entry->opt_l, "file:") == 0) use_path = FALSE;
539
540                         remote_client_send(rc, text);
541
542                         sent = TRUE;
543                         }
544
545                 if (cmd_list && cmd_list->next)
546                         {
547                         prefix = "--list-add:";
548                         remote_client_send(rc, "--list-clear");
549                         }
550                 else
551                         {
552                         prefix = "file:";
553                         }
554
555                 work = cmd_list;
556                 while (work)
557                         {
558                         FileData *fd;
559                         gchar *text;
560
561                         fd = work->data;
562                         work = work->next;
563
564                         text = g_strconcat(prefix, fd->path, NULL);
565                         remote_client_send(rc, text);
566                         g_free(text);
567
568                         sent = TRUE;
569                         }
570
571                 if (path && !cmd_list && use_path)
572                         {
573                         gchar *text;
574
575                         text = g_strdup_printf("file:%s", path);
576                         remote_client_send(rc, text);
577                         g_free(text);
578
579                         sent = TRUE;
580                         }
581
582                 work = collection_list;
583                 while (work)
584                         {
585                         const gchar *name;
586                         gchar *text;
587
588                         name = work->data;
589                         work = work->next;
590
591                         text = g_strdup_printf("file:%s", name);
592                         remote_client_send(rc, text);
593                         g_free(text);
594
595                         sent = TRUE;
596                         }
597
598                 if (!started && !sent)
599                         {
600                         remote_client_send(rc, "raise");
601                         }
602                 }
603         else
604                 {
605                 print_term(_("Remote not available\n"));
606                 }
607
608         _exit(0);
609 }
610
611 /*
612  *-----------------------------------------------------------------------------
613  * command line parser (private) hehe, who needs popt anyway?
614  *-----------------------------------------------------------------------------
615  */
616
617 static gint startup_blank = FALSE;
618 static gint startup_full_screen = FALSE;
619 static gint startup_in_slideshow = FALSE;
620 static gint startup_command_line_collection = FALSE;
621
622
623 static void parse_command_line_add_file(const gchar *file_path, gchar **path, gchar **file,
624                                         GList **list, GList **collection_list)
625 {
626         gchar *path_parsed;
627
628         path_parsed = g_strdup(file_path);
629         parse_out_relatives(path_parsed);
630
631         if (file_extension_match(path_parsed, ".gqv"))
632                 {
633                 *collection_list = g_list_append(*collection_list, path_parsed);
634                 }
635         else
636                 {
637                 if (!*path) *path = remove_level_from_path(path_parsed);
638                 if (!*file) *file = g_strdup(path_parsed);
639                 *list = g_list_prepend(*list, file_data_new_simple(path_parsed));
640                 }
641 }
642
643 static void parse_command_line_add_dir(const gchar *dir, gchar **path, gchar **file,
644                                        GList **list)
645 {
646         GList *files = NULL;
647         gchar *path_parsed;
648
649         path_parsed = g_strdup(dir);
650         parse_out_relatives(path_parsed);
651
652         if (filelist_read(path_parsed, &files, NULL))
653                 {
654                 GList *work;
655
656                 files = filelist_filter(files, FALSE);
657                 files = filelist_sort_path(files);
658
659                 work = files;
660                 while (work)
661                         {
662                         FileData *fd = work->data;
663                         if (!*path) *path = remove_level_from_path(fd->path);
664                         if (!*file) *file = g_strdup(fd->path);
665                         *list = g_list_prepend(*list, fd);
666
667                         work = work->next;
668                         }
669
670                 g_list_free(files);
671                 }
672
673         g_free(path_parsed);
674 }
675
676 static void parse_command_line_process_dir(const gchar *dir, gchar **path, gchar **file,
677                                            GList **list, gchar **first_dir)
678 {
679
680         if (!*list && !*first_dir)
681                 {
682                 *first_dir = g_strdup(dir);
683                 }
684         else
685                 {
686                 if (*first_dir)
687                         {
688                         parse_command_line_add_dir(*first_dir, path, file, list);
689                         g_free(*first_dir);
690                         *first_dir = NULL;
691                         }
692                 parse_command_line_add_dir(dir, path, file, list);
693                 }
694 }
695
696 static void parse_command_line_process_file(const gchar *file_path, gchar **path, gchar **file,
697                                             GList **list, GList **collection_list, gchar **first_dir)
698 {
699
700         if (*first_dir)
701                 {
702                 parse_command_line_add_dir(*first_dir, path, file, list);
703                 g_free(*first_dir);
704                 *first_dir = NULL;
705                 }
706         parse_command_line_add_file(file_path, path, file, list, collection_list);
707 }
708
709 static void parse_command_line(int argc, char *argv[], gchar **path, gchar **file,
710                                GList **cmd_list, GList **collection_list,
711                                gchar **geometry)
712 {
713         GList *list = NULL;
714         GList *remote_list = NULL;
715         gint remote_do = FALSE;
716         gchar *first_dir = NULL;
717
718         if (argc > 1)
719                 {
720                 gint i;
721                 gchar *base_dir = get_current_dir();
722                 i = 1;
723                 while (i < argc)
724                         {
725                         const gchar *cmd_line = argv[i];
726                         gchar *cmd_all = concat_dir_and_file(base_dir, cmd_line);
727
728                         if (cmd_line[0] == '/' && isdir(cmd_line))
729                                 {
730                                 parse_command_line_process_dir(cmd_line, path, file, &list, &first_dir);
731                                 }
732                         else if (isdir(cmd_all))
733                                 {
734                                 parse_command_line_process_dir(cmd_all, path, file, &list, &first_dir);
735                                 }
736                         else if (cmd_line[0] == '/' && isfile(cmd_line))
737                                 {
738                                 parse_command_line_process_file(cmd_line, path, file,
739                                                                 &list, collection_list, &first_dir);
740                                 }
741                         else if (isfile(cmd_all))
742                                 {
743                                 parse_command_line_process_file(cmd_all, path, file,
744                                                                 &list, collection_list, &first_dir);
745                                 }
746                         else if (strncmp(cmd_line, "--debug", 7) == 0 && (cmd_line[7] == '\0' || cmd_line[7] == '='))
747                                 {
748                                 /* do nothing but do not produce warnings */
749                                 }
750                         else if (strcmp(cmd_line, "+t") == 0 ||
751                                  strcmp(cmd_line, "--with-tools") == 0)
752                                 {
753                                 options->layout.tools_float = FALSE;
754                                 options->layout.tools_hidden = FALSE;
755
756                                 remote_list = g_list_append(remote_list, "+t");
757                                 }
758                         else if (strcmp(cmd_line, "-t") == 0 ||
759                                  strcmp(cmd_line, "--without-tools") == 0)
760                                 {
761                                 options->layout.tools_hidden = TRUE;
762
763                                 remote_list = g_list_append(remote_list, "-t");
764                                 }
765                         else if (strcmp(cmd_line, "-f") == 0 ||
766                                  strcmp(cmd_line, "--fullscreen") == 0)
767                                 {
768                                 startup_full_screen = TRUE;
769                                 }
770                         else if (strcmp(cmd_line, "-s") == 0 ||
771                                  strcmp(cmd_line, "--slideshow") == 0)
772                                 {
773                                 startup_in_slideshow = TRUE;
774                                 }
775                         else if (strcmp(cmd_line, "-l") == 0 ||
776                                  strcmp(cmd_line, "--list") == 0)
777                                 {
778                                 startup_command_line_collection = TRUE;
779                                 }
780                         else if (strncmp(cmd_line, "--geometry=", 11) == 0)
781                                 {
782                                 if (!*geometry) *geometry = g_strdup(cmd_line + 11);
783                                 }
784                         else if (strcmp(cmd_line, "-r") == 0 ||
785                                  strcmp(cmd_line, "--remote") == 0)
786                                 {
787                                 if (!remote_do)
788                                         {
789                                         remote_do = TRUE;
790                                         remote_list = remote_build_list(remote_list, argc, argv);
791                                         }
792                                 }
793                         else if (strcmp(cmd_line, "-rh") == 0 ||
794                                  strcmp(cmd_line, "--remote-help") == 0)
795                                 {
796                                 remote_help();
797                                 exit(0);
798                                 }
799                         else if (strcmp(cmd_line, "--blank") == 0)
800                                 {
801                                 startup_blank = TRUE;
802                                 }
803                         else if (strcmp(cmd_line, "-v") == 0 ||
804                                  strcmp(cmd_line, "--version") == 0)
805                                 {
806                                 printf("%s %s\n", GQ_APPNAME, VERSION);
807                                 exit(0);
808                                 }
809                         else if (strcmp(cmd_line, "--alternate") == 0)
810                                 {
811                                 /* enable faster experimental algorithm */
812                                 printf("Alternate similarity algorithm enabled\n");
813                                 image_sim_alternate_set(TRUE);
814                                 }
815                         else if (strcmp(cmd_line, "-h") == 0 ||
816                                  strcmp(cmd_line, "--help") == 0)
817                                 {
818                                 printf("%s %s\n", GQ_APPNAME, VERSION);
819                                 printf_term(_("Usage: %s [options] [path]\n\n"), GQ_APPNAME_LC);
820                                 print_term(_("valid options are:\n"));
821                                 print_term(_("  +t, --with-tools           force show of tools\n"));
822                                 print_term(_("  -t, --without-tools        force hide of tools\n"));
823                                 print_term(_("  -f, --fullscreen           start in full screen mode\n"));
824                                 print_term(_("  -s, --slideshow            start in slideshow mode\n"));
825                                 print_term(_("  -l, --list                 open collection window for command line\n"));
826                                 print_term(_("      --geometry=GEOMETRY    set main window location\n"));
827                                 print_term(_("  -r, --remote               send following commands to open window\n"));
828                                 print_term(_("  -rh,--remote-help          print remote command list\n"));
829 #ifdef DEBUG
830                                 print_term(_("  --debug[=level]            turn on debug output\n"));
831 #endif
832                                 print_term(_("  -v, --version              print version info\n"));
833                                 print_term(_("  -h, --help                 show this message\n\n"));
834
835 #if 0
836                                 /* these options are not officially supported!
837                                  * only for testing new features, no need to translate them */
838                                 print_term(  "  --alternate                use alternate similarity algorithm\n");
839 #endif
840
841                                 exit(0);
842                                 }
843                         else if (!remote_do)
844                                 {
845                                 printf_term(_("invalid or ignored: %s\nUse --help for options\n"), cmd_line);
846                                 }
847
848                         g_free(cmd_all);
849                         i++;
850                         }
851                 g_free(base_dir);
852                 parse_out_relatives(*path);
853                 parse_out_relatives(*file);
854                 }
855
856         list = g_list_reverse(list);
857
858         if (!*path && first_dir)
859                 {
860                 *path = first_dir;
861                 first_dir = NULL;
862
863                 parse_out_relatives(*path);
864                 }
865         g_free(first_dir);
866
867         if (remote_do)
868                 {
869                 remote_control(argv[0], remote_list, *path, list, *collection_list);
870                 }
871         g_list_free(remote_list);
872
873         if (list && list->next)
874                 {
875                 *cmd_list = list;
876                 }
877         else
878                 {
879                 filelist_free(list);
880                 *cmd_list = NULL;
881                 }
882 }
883
884 static void parse_command_line_for_debug_option(int argc, char *argv[])
885 {
886 #ifdef DEBUG
887         const gchar *debug_option = "--debug";
888         gint len = strlen(debug_option);
889
890         if (argc > 1)
891                 {
892                 gint i;
893
894                 for (i = 1; i < argc; i++)
895                         {
896                         const gchar *cmd_line = argv[i];
897                         if (strncmp(cmd_line, debug_option, len) == 0)
898                                 {
899                                 gint cmd_line_len = strlen(cmd_line);
900
901                                 /* we now increment the debug state for verbosity */
902                                 if (cmd_line_len == len)
903                                         debug_level_add(1);
904                                 else if (cmd_line[len] == '=' && g_ascii_isdigit(cmd_line[len+1]))
905                                         {
906                                         gint n = atoi(cmd_line + len + 1);
907                                         if (n < 0) n = 1;
908                                         debug_level_add(n);
909                                         }
910                                 }
911                         }
912                 }
913
914         DEBUG_1("debugging output enabled (level %d)", get_debug_level());
915 #endif
916 }
917
918 /*
919  *-----------------------------------------------------------------------------
920  * startup, init, and exit
921  *-----------------------------------------------------------------------------
922  */
923
924 #define RC_HISTORY_NAME "history"
925
926 static void keys_load(void)
927 {
928         gchar *path;
929
930         path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
931         history_list_load(path);
932         g_free(path);
933 }
934
935 static void keys_save(void)
936 {
937         gchar *path;
938
939         path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/", RC_HISTORY_NAME, NULL);
940         history_list_save(path);
941         g_free(path);
942 }
943
944 static void check_for_home_path(gchar *path)
945 {
946         gchar *buf;
947
948         buf = g_strconcat(homedir(), "/", path, NULL);
949         if (!isdir(buf))
950                 {
951                 printf_term(_("Creating %s dir:%s\n"), GQ_APPNAME, buf);
952
953                 if (!mkdir_utf8(buf, 0755))
954                         {
955                         printf_term(_("Could not create dir:%s\n"), buf);
956                         }
957                 }
958         g_free(buf);
959 }
960
961 static void setup_default_options(void)
962 {
963         gchar *path;
964         gint i;
965
966         for (i = 0; i < GQ_EDITOR_SLOTS; i++)
967                 {
968                 options->editor_name[i] = NULL;
969                 options->editor_command[i] = NULL;
970                 }
971
972         editor_reset_defaults();
973
974         bookmark_add_default(_("Home"), homedir());
975         path = concat_dir_and_file(homedir(), "Desktop");
976         bookmark_add_default(_("Desktop"), path);
977         g_free(path);
978         path = concat_dir_and_file(homedir(), GQ_RC_DIR_COLLECTIONS);
979         bookmark_add_default(_("Collections"), path);
980         g_free(path);
981
982         g_free(options->file_ops.safe_delete_path);
983         options->file_ops.safe_delete_path = concat_dir_and_file(homedir(), GQ_RC_DIR_TRASH);
984
985         for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
986                 {
987                 options->color_profile.input_file[i] = NULL;
988                 options->color_profile.input_name[i] = NULL;
989                 }
990
991         set_default_image_overlay_template_string(options);
992         sidecar_ext_add_defaults();
993         options->layout.order = g_strdup("123");
994 }
995
996 static void exit_program_final(void)
997 {
998         gchar *path;
999         gchar *pathl;
1000         LayoutWindow *lw = NULL;
1001
1002         remote_close(remote_connection);
1003
1004         collect_manager_flush();
1005
1006         if (layout_valid(&lw))
1007                 {
1008                 options->layout.main_window.maximized =  window_maximized(lw->window);
1009                 if (!options->layout.main_window.maximized)
1010                         {
1011                         layout_geometry_get(NULL, &options->layout.main_window.x, &options->layout.main_window.y,
1012                                             &options->layout.main_window.w, &options->layout.main_window.h);
1013                         }
1014
1015                 options->image_overlay.common.state = image_osd_get(lw->image);
1016                 }
1017
1018         layout_geometry_get_dividers(NULL, &options->layout.main_window.hdivider_pos, &options->layout.main_window.vdivider_pos);
1019
1020         layout_views_get(NULL, &options->layout.dir_view_type, &options->layout.file_view_type);
1021
1022         options->layout.show_thumbnails = layout_thumb_get(NULL);
1023         options->layout.show_marks = layout_marks_get(NULL);
1024
1025         layout_sort_get(NULL, &options->file_sort.method, &options->file_sort.ascending);
1026
1027         layout_geometry_get_tools(NULL, &options->layout.float_window.x, &options->layout.float_window.y,
1028                                   &options->layout.float_window.w, &options->layout.float_window.h, &options->layout.float_window.vdivider_pos);
1029         layout_tools_float_get(NULL, &options->layout.tools_float, &options->layout.tools_hidden);
1030         options->layout.toolbar_hidden = layout_toolbar_hidden(NULL);
1031
1032         options->color_profile.enabled = layout_image_color_profile_get_use(NULL);
1033         layout_image_color_profile_get(NULL,
1034                                        &options->color_profile.input_type,
1035                                        &options->color_profile.screen_type,
1036                                        &options->color_profile.use_image);
1037
1038         if (options->startup.restore_path && options->startup.use_last_path)
1039                 {
1040                 g_free(options->startup.path);
1041                 options->startup.path = g_strdup(layout_get_path(NULL));
1042                 }
1043
1044         save_options();
1045         keys_save();
1046
1047         path = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
1048         pathl = path_from_utf8(path);
1049         gtk_accel_map_save(pathl);
1050         g_free(pathl);
1051         g_free(path);
1052
1053         gtk_main_quit();
1054 }
1055
1056 static GenericDialog *exit_dialog = NULL;
1057
1058 static void exit_confirm_cancel_cb(GenericDialog *gd, gpointer data)
1059 {
1060         exit_dialog = NULL;
1061         generic_dialog_close(gd);
1062 }
1063
1064 static void exit_confirm_exit_cb(GenericDialog *gd, gpointer data)
1065 {
1066         exit_dialog = NULL;
1067         generic_dialog_close(gd);
1068         exit_program_final();
1069 }
1070
1071 static gint exit_confirm_dlg(void)
1072 {
1073         GtkWidget *parent;
1074         LayoutWindow *lw;
1075         gchar *msg;
1076
1077         if (exit_dialog)
1078                 {
1079                 gtk_window_present(GTK_WINDOW(exit_dialog->dialog));
1080                 return TRUE;
1081                 }
1082
1083         if (!collection_window_modified_exists()) return FALSE;
1084
1085         parent = NULL;
1086         lw = NULL;
1087         if (layout_valid(&lw))
1088                 {
1089                 parent = lw->window;
1090                 }
1091
1092         msg = g_strdup_printf("%s - %s", GQ_APPNAME, _("exit"));
1093         exit_dialog = generic_dialog_new(msg,
1094                                 GQ_WMCLASS, "exit", parent, FALSE,
1095                                 exit_confirm_cancel_cb, NULL);
1096         g_free(msg);
1097         msg = g_strdup_printf(_("Quit %s"), GQ_APPNAME);
1098         generic_dialog_add_message(exit_dialog, GTK_STOCK_DIALOG_QUESTION,
1099                                    msg, _("Collections have been modified. Quit anyway?"));
1100         g_free(msg);
1101         generic_dialog_add_button(exit_dialog, GTK_STOCK_QUIT, NULL, exit_confirm_exit_cb, TRUE);
1102
1103         gtk_widget_show(exit_dialog->dialog);
1104
1105         return TRUE;
1106 }
1107
1108 void exit_program(void)
1109 {
1110         layout_image_full_screen_stop(NULL);
1111
1112         if (exit_confirm_dlg()) return;
1113
1114         exit_program_final();
1115 }
1116
1117 int main(int argc, char *argv[])
1118 {
1119         LayoutWindow *lw;
1120         gchar *path = NULL;
1121         gchar *cmd_path = NULL;
1122         gchar *cmd_file = NULL;
1123         GList *cmd_list = NULL;
1124         GList *collection_list = NULL;
1125         CollectionData *first_collection = NULL;
1126         gchar *geometry = NULL;
1127         gchar *buf;
1128         gchar *bufl;
1129
1130         /* init execution time counter (debug only) */
1131         init_exec_time();
1132
1133         /* setup locale, i18n */
1134         gtk_set_locale();
1135         bindtextdomain(PACKAGE, GQ_LOCALEDIR);
1136         bind_textdomain_codeset(PACKAGE, "UTF-8");
1137         textdomain(PACKAGE);
1138
1139         /* setup random seed for random slideshow */
1140         srand(time(NULL));
1141
1142 #if 1
1143         printf("%s %s, This is an alpha release.\n", GQ_APPNAME, VERSION);
1144 #endif
1145         parse_command_line_for_debug_option(argc, argv);
1146
1147         options = init_options(NULL);
1148         setup_default_options();
1149         load_options();
1150
1151         parse_command_line(argc, argv, &cmd_path, &cmd_file, &cmd_list, &collection_list, &geometry);
1152
1153         gtk_init(&argc, &argv);
1154
1155         if (gtk_major_version < GTK_MAJOR_VERSION ||
1156             (gtk_major_version == GTK_MAJOR_VERSION && gtk_minor_version < GTK_MINOR_VERSION) )
1157                 {
1158                 printf_term("!!! This is a friendly warning.\n");
1159                 printf_term("!!! The version of GTK+ in use now is older than when %s was compiled.\n", GQ_APPNAME);
1160                 printf_term("!!!  compiled with GTK+-%d.%d\n", GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
1161                 printf_term("!!!   running with GTK+-%d.%d\n", gtk_major_version, gtk_minor_version);
1162                 printf_term("!!! %s may quit unexpectedly with a relocation error.\n", GQ_APPNAME);
1163                 }
1164
1165         check_for_home_path(GQ_RC_DIR);
1166         check_for_home_path(GQ_RC_DIR_COLLECTIONS);
1167         check_for_home_path(GQ_CACHE_RC_THUMB);
1168         check_for_home_path(GQ_CACHE_RC_METADATA);
1169
1170         keys_load();
1171         filter_add_defaults();
1172         filter_rebuild();
1173
1174         buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/accels", NULL);
1175         bufl = path_from_utf8(buf);
1176         gtk_accel_map_load(bufl);
1177         g_free(bufl);
1178         g_free(buf);
1179
1180         if (startup_blank)
1181                 {
1182                 g_free(cmd_path);
1183                 cmd_path = NULL;
1184                 g_free(cmd_file);
1185                 cmd_file = NULL;
1186                 filelist_free(cmd_list);
1187                 cmd_list = NULL;
1188                 string_list_free(collection_list);
1189                 collection_list = NULL;
1190
1191                 path = NULL;
1192                 }
1193         else if (cmd_path)
1194                 {
1195                 path = g_strdup(cmd_path);
1196                 }
1197         else if (options->startup.restore_path && options->startup.path && isdir(options->startup.path))
1198                 {
1199                 path = g_strdup(options->startup.path);
1200                 }
1201         else
1202                 {
1203                 path = get_current_dir();
1204                 }
1205
1206         lw = layout_new_with_geometry(NULL, options->layout.tools_float, options->layout.tools_hidden, geometry);
1207         layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
1208
1209         if (collection_list && !startup_command_line_collection)
1210                 {
1211                 GList *work;
1212
1213                 work = collection_list;
1214                 while (work)
1215                         {
1216                         CollectWindow *cw;
1217                         const gchar *path;
1218
1219                         path = work->data;
1220                         work = work->next;
1221
1222                         cw = collection_window_new(path);
1223                         if (!first_collection && cw) first_collection = cw->cd;
1224                         }
1225                 }
1226
1227         if (cmd_list ||
1228             (startup_command_line_collection && collection_list))
1229                 {
1230                 CollectionData *cd;
1231                 GList *work;
1232
1233                 if (startup_command_line_collection)
1234                         {
1235                         CollectWindow *cw;
1236
1237                         cw = collection_window_new("");
1238                         cd = cw->cd;
1239                         }
1240                 else
1241                         {
1242                         cd = collection_new("");        /* if we pass NULL, untitled counter is falsely increm. */
1243                         command_collection = cd;
1244                         }
1245
1246                 g_free(cd->path);
1247                 cd->path = NULL;
1248                 g_free(cd->name);
1249                 cd->name = g_strdup(_("Command line"));
1250
1251                 collection_path_changed(cd);
1252
1253                 work = cmd_list;
1254                 while (work)
1255                         {
1256                         collection_add(cd, file_data_new_simple((gchar *)work->data), FALSE);
1257                         work = work->next;
1258                         }
1259
1260                 work = collection_list;
1261                 while (work)
1262                         {
1263                         collection_load(cd, (gchar *)work->data, COLLECTION_LOAD_APPEND);
1264                         work = work->next;
1265                         }
1266
1267                 layout_set_path(lw, path);
1268                 if (cd->list) layout_image_set_collection(lw, cd, cd->list->data);
1269
1270                 /* mem leak, we never unref this collection when !startup_command_line_collection
1271                  * (the image view of the main window does not hold a ref to the collection)
1272                  * this is sort of unavoidable, for if it did hold a ref, next/back
1273                  * may not work as expected when closing collection windows.
1274                  *
1275                  * collection_unref(cd);
1276                  */
1277
1278                 }
1279         else if (cmd_file)
1280                 {
1281                 layout_set_path(lw, cmd_file);
1282                 }
1283         else
1284                 {
1285                 layout_set_path(lw, path);
1286                 if (first_collection)
1287                         {
1288                         layout_image_set_collection(lw, first_collection,
1289                                                     collection_get_first(first_collection));
1290                         }
1291                 }
1292
1293         image_osd_set(lw->image, options->image_overlay.common.state | (options->image_overlay.common.show_at_startup ? OSD_SHOW_INFO : OSD_SHOW_NOTHING));
1294
1295         g_free(geometry);
1296         g_free(cmd_path);
1297         g_free(cmd_file);
1298         filelist_free(cmd_list);
1299         string_list_free(collection_list);
1300         g_free(path);
1301
1302         if (startup_full_screen) layout_image_full_screen_start(lw);
1303         if (startup_in_slideshow) layout_image_slideshow_start(lw);
1304
1305         buf = g_strconcat(homedir(), "/", GQ_RC_DIR, "/.command", NULL);
1306         remote_connection = remote_server_open(buf);
1307         remote_server_subscribe(remote_connection, remote_cb, NULL);
1308         g_free(buf);
1309
1310         gtk_main();
1311         return 0;
1312 }