/* * mate-calc-application.c - GtkApplication for MATE Calculator * * Copyright (C) 2024 MATE Desktop Team * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 2 of the License, or (at your option) any later * version. See http://www.gnu.org/copyleft/gpl.html the full text of the * license. */ #include #include #include #include "mate-calc-application.h" #include "mate-calc-settings.h" #include "mate-calc-actions.h" #include "math-window.h" #include "mp-equation.h" #include "unit-manager.h" /* Global settings variable for backward compatibility with existing code */ GSettings *g_settings_var = NULL; struct _MateCalcApplication { GtkApplication parent_instance; GSettings *settings; }; G_DEFINE_TYPE(MateCalcApplication, mate_calc_application, GTK_TYPE_APPLICATION) static void mate_calc_application_startup(GApplication *application) { MateCalcApplication *app = MATE_CALC_APPLICATION(application); G_APPLICATION_CLASS(mate_calc_application_parent_class)->startup(application); /* Initialize settings */ app->settings = g_settings_new("org.mate.calc"); g_settings_var = app->settings; /* Setup application actions and accelerators */ mate_calc_actions_setup(GTK_APPLICATION(app)); } static void mate_calc_application_activate(GApplication *application) { MateCalcApplication *app = MATE_CALC_APPLICATION(application); MathEquation *equation; GtkWindow *window; MathButtons *buttons; /* Create a new equation for each window */ equation = math_equation_new(); mate_calc_settings_apply_to_equation(app->settings, equation); /* Create new window */ window = GTK_WINDOW(math_window_new(equation)); gtk_application_add_window(GTK_APPLICATION(app), window); /* Setup window actions */ mate_calc_actions_setup_window(window, GTK_APPLICATION(app)); /* Pass settings to window for state persistence */ math_window_set_settings(MATH_WINDOW(window), app->settings); /* Apply settings to window */ buttons = math_window_get_buttons(MATH_WINDOW(window)); math_window_set_show_history(MATH_WINDOW(window), g_settings_get_boolean(app->settings, "show-history")); math_buttons_set_programming_base(buttons, g_settings_get_int(app->settings, "base")); math_buttons_set_mode(buttons, g_settings_get_enum(app->settings, "button-mode")); gtk_widget_show(GTK_WIDGET(window)); } static void mate_calc_application_shutdown(GApplication *application) { MateCalcApplication *app = MATE_CALC_APPLICATION(application); g_settings_var = NULL; g_clear_object(&app->settings); G_APPLICATION_CLASS(mate_calc_application_parent_class)->shutdown(application); } static gint mate_calc_application_handle_local_options(GApplication *application, GVariantDict *options) { if (g_variant_dict_contains(options, "version")) { g_print("%s %s\n", "mate-calc", VERSION); return 0; } /* Let the default handler continue */ return -1; } static gint mate_calc_application_command_line(GApplication *application, GApplicationCommandLine *command_line) { GVariantDict *options; const gchar *solve_expr = NULL; options = g_application_command_line_get_options_dict(command_line); if (g_variant_dict_lookup(options, "solve", "&s", &solve_expr)) { /* Solve mode - handled by mate-calc-cmd or inline */ MPEquationOptions mp_options; MPErrorCode error; MPNumber result = mp_new(); char *result_str; memset(&mp_options, 0, sizeof(mp_options)); mp_options.base = 10; mp_options.wordlen = 32; mp_options.angle_units = MP_DEGREES; mp_options.convert = NULL; error = mp_equation_parse(solve_expr, &mp_options, &result, NULL); if (error == PARSER_ERR_MP) { g_application_command_line_printerr(command_line, "Error: %s\n", mp_get_error()); mp_clear(&result); return 1; } else if (error != 0) { g_application_command_line_printerr(command_line, "Error: %s\n", mp_error_code_to_string(error)); mp_clear(&result); return 1; } else { result_str = mp_serializer_to_string( mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9), &result); g_application_command_line_print(command_line, "%s\n", result_str); g_free(result_str); mp_clear(&result); return 0; } } /* Activate normal GUI mode */ g_application_activate(application); return 0; } static void mate_calc_application_init(MateCalcApplication *app) { /* Command line options */ const GOptionEntry options[] = { { "version", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL, N_("Show release version"), NULL }, { "solve", 's', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, N_("Solve the given equation"), N_("EQUATION") }, { NULL } }; g_application_add_main_option_entries(G_APPLICATION(app), options); } static void mate_calc_application_class_init(MateCalcApplicationClass *klass) { GApplicationClass *app_class = G_APPLICATION_CLASS(klass); app_class->startup = mate_calc_application_startup; app_class->activate = mate_calc_application_activate; app_class->shutdown = mate_calc_application_shutdown; app_class->handle_local_options = mate_calc_application_handle_local_options; app_class->command_line = mate_calc_application_command_line; } MateCalcApplication * mate_calc_application_new(void) { return g_object_new(MATE_CALC_TYPE_APPLICATION, "application-id", "org.mate.Calculator", "flags", G_APPLICATION_HANDLES_COMMAND_LINE, NULL); } GSettings * mate_calc_application_get_settings(MateCalcApplication *app) { g_return_val_if_fail(MATE_CALC_IS_APPLICATION(app), NULL); return app->settings; }