diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/query-tool.c | 2 | ||||
-rw-r--r-- | src/tdv-gui.c (renamed from src/sdgui.c) | 40 | ||||
-rw-r--r-- | src/tdv-tui.c (renamed from src/sdtui.c) | 51 | ||||
-rw-r--r-- | src/tdv.c | 98 |
4 files changed, 108 insertions, 83 deletions
diff --git a/src/query-tool.c b/src/query-tool.c index 825bada..6cfdc66 100644 --- a/src/query-tool.c +++ b/src/query-tool.c @@ -6,7 +6,7 @@ * finalised with an empty line. Newlines are escaped with `\n', * backslashes with `\\'. * - * So far only the `m', `g`, and `x` fields are supported, as in sdtui. + * So far only the `m', `g`, and `x` fields are supported, as in tdv. * * Copyright (c) 2013 - 2021, Přemysl Eric Janouch <p@janouch.name> * diff --git a/src/sdgui.c b/src/tdv-gui.c index 993d1f5..fcb254f 100644 --- a/src/sdgui.c +++ b/src/tdv-gui.c @@ -19,7 +19,6 @@ #include <gtk/gtk.h> #include <glib/gi18n.h> -#include <locale.h> #include <stdlib.h> #include "config.h" @@ -27,9 +26,6 @@ #include "utils.h" #include "stardict-view.h" -#undef PROJECT_NAME -#define PROJECT_NAME "sdgui" - static struct { GtkWidget *window; ///< Top-level window @@ -452,42 +448,18 @@ die_with_dialog (const gchar *message) } int -main (int argc, char *argv[]) +gui_main (char *argv[]) { - if (!setlocale (LC_ALL, "")) - g_printerr ("%s: %s\n", _("Warning"), _("failed to set the locale")); - - bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME); - bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - textdomain (GETTEXT_PACKAGE); - - gchar **filenames = NULL; - GOptionEntry option_entries[] = - { - {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, - NULL, N_("[FILE]...")}, - {}, - }; - - GError *error = NULL; - gtk_init_with_args (&argc, &argv, N_("- StarDict GTK+ UI"), - option_entries, GETTEXT_PACKAGE, &error); - if (error) - { - g_warning ("%s", error->message); - g_error_free (error); - return 1; - } + // Just like with GtkApplication, argv has been parsed by the option group. + gtk_init (NULL, NULL); gtk_window_set_default_icon_name (PROJECT_NAME); + GError *error = NULL; GPtrArray *new_dictionaries = g_ptr_array_new_with_free_func ((GDestroyNotify) dictionary_destroy); - if (filenames) - { - load_from_filenames (new_dictionaries, filenames); - g_strfreev (filenames); - } + if (argv[0]) + load_from_filenames (new_dictionaries, argv); else if (!load_from_config (new_dictionaries, &error) && error) die_with_dialog (error->message); diff --git a/src/sdtui.c b/src/tdv-tui.c index c078f1a..2863884 100644 --- a/src/sdtui.c +++ b/src/tdv-tui.c @@ -18,16 +18,15 @@ #include <stdio.h> #include <stdlib.h> -#include <locale.h> #include <stdarg.h> #include <limits.h> #include <string.h> #include <glib.h> #include <glib-unix.h> +#include <glib/gi18n.h> #include <gio/gio.h> #include <pango/pango.h> -#include <glib/gi18n.h> #include <unistd.h> #include <poll.h> @@ -2438,53 +2437,10 @@ log_handler (const gchar *domain, GLogLevelFlags level, } int -main (int argc, char *argv[]) +tui_main (char *argv[]) { -G_GNUC_BEGIN_IGNORE_DEPRECATIONS - if (glib_check_version (2, 36, 0)) - g_type_init (); -G_GNUC_END_IGNORE_DEPRECATIONS - - gboolean show_version = FALSE; - GOptionEntry entries[] = - { - { "version", 0, G_OPTION_FLAG_IN_MAIN, - G_OPTION_ARG_NONE, &show_version, - N_("Output version information and exit"), NULL }, - { NULL } - }; - - if (!setlocale (LC_ALL, "")) - g_printerr ("%s: %s\n", _("Warning"), _("failed to set the locale")); - - bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME); - bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); - textdomain (GETTEXT_PACKAGE); - - GError *error = NULL; - GOptionContext *ctx = g_option_context_new - (N_("[dictionary.ifo...] - StarDict terminal UI")); - GOptionGroup *group = g_option_group_new ("", "", "", NULL, NULL); - g_option_group_add_entries (group, entries); - g_option_group_set_translation_domain (group, GETTEXT_PACKAGE); - g_option_context_add_group (ctx, group); - g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE); - if (!g_option_context_parse (ctx, &argc, &argv, &error)) - { - g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"), - error->message); - exit (EXIT_FAILURE); - } - g_option_context_free (ctx); - - if (show_version) - { - g_print (PROJECT_NAME " " PROJECT_VERSION "\n"); - exit (EXIT_SUCCESS); - } - Application app; - app_init (&app, argv + 1); + app_init (&app, argv); app_init_terminal (&app); app_redraw (&app); @@ -2528,4 +2484,3 @@ G_GNUC_END_IGNORE_DEPRECATIONS return 0; } - diff --git a/src/tdv.c b/src/tdv.c new file mode 100644 index 0000000..44484fa --- /dev/null +++ b/src/tdv.c @@ -0,0 +1,98 @@ +/* + * Translation dictionary viewer + * + * Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "config.h" + +#include <glib.h> +#include <glib/gi18n.h> +#ifdef WITH_GUI +#include <gtk/gtk.h> +#endif + +#include <locale.h> +#ifndef G_OS_WIN32 +#include <unistd.h> +#endif + +int tui_main (char *[]); +int gui_main (char *[]); + +int +main (int argc, char *argv[]) +{ + if (!setlocale (LC_ALL, "")) + g_printerr ("%s: %s\n", _("Warning"), _("failed to set the locale")); + + bindtextdomain (GETTEXT_PACKAGE, GETTEXT_DIRNAME); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + textdomain (GETTEXT_PACKAGE); + + gboolean show_version = FALSE; +#ifdef WITH_GUI +# ifndef G_OS_WIN32 + gboolean gui = FALSE; +# endif +#endif + GOptionEntry entries[] = + { + { "version", 0, G_OPTION_FLAG_IN_MAIN, + G_OPTION_ARG_NONE, &show_version, + N_("Output version information and exit"), NULL }, +#ifdef WITH_GUI +# ifndef G_OS_WIN32 + { "gui", 0, G_OPTION_FLAG_IN_MAIN, + G_OPTION_ARG_NONE, &gui, + N_("Launch the GUI even when run from a terminal"), NULL }, +# endif +#endif + { }, + }; + + GOptionContext *ctx = g_option_context_new + (N_("[dictionary.ifo...] - Translation dictionary viewer")); + g_option_context_add_main_entries (ctx, entries, GETTEXT_PACKAGE); +#ifdef WITH_GUI + g_option_context_add_group (ctx, gtk_get_option_group (FALSE)); +#endif + g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE); + + GError *error = NULL; + if (!g_option_context_parse (ctx, &argc, &argv, &error)) + { + g_printerr ("%s: %s: %s\n", _("Error"), _("option parsing failed"), + error->message); + exit (EXIT_FAILURE); + } + g_option_context_free (ctx); + + if (show_version) + { + g_print (PROJECT_NAME " " PROJECT_VERSION "\n"); + exit (EXIT_SUCCESS); + } + +#ifdef WITH_GUI +# ifndef G_OS_WIN32 + if (gui || !isatty (STDIN_FILENO)) +# endif + return gui_main (argv + 1); +#endif +#ifndef G_OS_WIN32 + return tui_main (argv + 1); +#endif +} |