summaryrefslogtreecommitdiff
path: root/src/mate-calc-application.c
blob: 8b0af1560983a3aad86d6d485300b8ef235d6600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * 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 <config.h>

#include <string.h>
#include <glib/gi18n.h>

#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;
}