Updated Latvian translation and added Latvian translation for help by Viesturs Ružāns...
[gcalctool.git] / src / gcalctool.c
blobc3349b3c0f27242e4b912efda7cedd89784e7fd3
1 /*
2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2011 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9 * license.
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <locale.h>
16 #include <glib/gi18n.h>
18 #include "math-window.h"
19 #include "mp-equation.h"
20 #include "unit-manager.h"
22 static GSettings *settings = NULL;
24 static MathWindow *window;
26 static void
27 version(const gchar *progname)
29 /* NOTE: Is not translated so can be easily parsed */
30 fprintf(stderr, "%1$s %2$s\n", progname, VERSION);
34 static int
35 do_convert(const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z, void *data)
37 return unit_manager_convert_by_symbol(unit_manager_get_default(), x, x_units, z_units, z);
41 static void
42 solve(const char *equation)
44 MPEquationOptions options;
45 MPErrorCode error;
46 MPNumber result;
47 char *result_str;
49 memset(&options, 0, sizeof(options));
50 options.base = 10;
51 options.wordlen = 32;
52 options.angle_units = MP_DEGREES;
53 options.convert = do_convert;
55 error = mp_equation_parse(equation, &options, &result, NULL);
56 if(error == PARSER_ERR_MP) {
57 fprintf(stderr, "Error: %s\n", mp_get_error());
58 exit(1);
60 else if(error != 0) {
61 fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
62 exit(1);
64 else {
65 result_str = mp_serializer_to_string(mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9), &result);
66 printf("%s\n", result_str);
67 exit(0);
72 static void
73 usage(const gchar *progname, gboolean show_application, gboolean show_gtk)
75 fprintf(stderr,
76 /* Description on how to use gcalctool displayed on command-line */
77 _("Usage:\n"
78 " %s — Perform mathematical calculations"), progname);
80 fprintf(stderr,
81 "\n\n");
83 fprintf(stderr,
84 /* Description on gcalctool command-line help options displayed on command-line */
85 _("Help Options:\n"
86 " -v, --version Show release version\n"
87 " -h, -?, --help Show help options\n"
88 " --help-all Show all help options\n"
89 " --help-gtk Show GTK+ options"));
90 fprintf(stderr,
91 "\n\n");
93 if (show_gtk) {
94 fprintf(stderr,
95 /* Description on gcalctool command-line GTK+ options displayed on command-line */
96 _("GTK+ Options:\n"
97 " --class=CLASS Program class as used by the window manager\n"
98 " --name=NAME Program name as used by the window manager\n"
99 " --screen=SCREEN X screen to use\n"
100 " --sync Make X calls synchronous\n"
101 " --gtk-module=MODULES Load additional GTK+ modules\n"
102 " --g-fatal-warnings Make all warnings fatal"));
103 fprintf(stderr,
104 "\n\n");
107 if (show_application) {
108 fprintf(stderr,
109 /* Description on gcalctool application options displayed on command-line */
110 _("Application Options:\n"
111 " -s, --solve <equation> Solve the given equation"));
112 fprintf(stderr,
113 "\n\n");
118 static void
119 get_options(int argc, char *argv[])
121 int i;
122 char *progname, *arg;
124 progname = g_path_get_basename(argv[0]);
126 for (i = 1; i < argc; i++) {
127 arg = argv[i];
129 if (strcmp(arg, "-v") == 0 ||
130 strcmp(arg, "--version") == 0) {
131 version(progname);
132 exit(0);
134 else if (strcmp(arg, "-h") == 0 ||
135 strcmp(arg, "-?") == 0 ||
136 strcmp(arg, "--help") == 0) {
137 usage(progname, TRUE, FALSE);
138 exit(0);
140 else if (strcmp(arg, "--help-all") == 0) {
141 usage(progname, TRUE, TRUE);
142 exit(0);
144 else if (strcmp(arg, "--help-gtk") == 0) {
145 usage(progname, FALSE, TRUE);
146 exit(0);
148 else if (strcmp(arg, "-s") == 0 ||
149 strcmp(arg, "--solve") == 0) {
150 i++;
151 if (i >= argc) {
152 fprintf(stderr,
153 /* Error printed to stderr when user uses --solve argument without an equation */
154 _("Argument --solve requires an equation to solve"));
155 fprintf(stderr, "\n");
156 exit(1);
158 else
159 solve(argv[i]);
161 else {
162 fprintf(stderr,
163 /* Error printed to stderr when user provides an unknown command-line argument */
164 _("Unknown argument '%s'"), arg);
165 fprintf(stderr, "\n");
166 usage(progname, TRUE, FALSE);
167 exit(1);
173 static void
174 accuracy_cb(MathEquation *equation, GParamSpec *spec)
176 g_settings_set_int(settings, "accuracy", math_equation_get_accuracy(equation));
180 static void
181 word_size_cb(MathEquation *equation, GParamSpec *spec)
183 g_settings_set_int(settings, "word-size", math_equation_get_word_size(equation));
187 static void
188 show_thousands_separators_cb(MathEquation *equation, GParamSpec *spec)
190 g_settings_set_boolean(settings, "show-thousands", math_equation_get_show_thousands_separators(equation));
194 static void
195 show_trailing_zeroes_cb(MathEquation *equation, GParamSpec *spec)
197 g_settings_set_boolean(settings, "show-zeroes", math_equation_get_show_trailing_zeroes(equation));
201 static void
202 number_format_cb(MathEquation *equation, GParamSpec *spec)
204 g_settings_set_enum(settings, "number-format", math_equation_get_number_format(equation));
208 static void
209 angle_unit_cb(MathEquation *equation, GParamSpec *spec)
211 g_settings_set_enum(settings, "angle-units", math_equation_get_angle_units(equation));
215 static void
216 source_currency_cb(MathEquation *equation, GParamSpec *spec)
218 g_settings_set_string(settings, "source-currency", math_equation_get_source_currency(equation));
222 static void
223 target_currency_cb(MathEquation *equation, GParamSpec *spec)
225 g_settings_set_string(settings, "target-currency", math_equation_get_target_currency(equation));
229 static void
230 source_units_cb(MathEquation *equation, GParamSpec *spec)
232 g_settings_set_string(settings, "source-units", math_equation_get_source_units(equation));
236 static void
237 target_units_cb(MathEquation *equation, GParamSpec *spec)
239 g_settings_set_string(settings, "target-units", math_equation_get_target_units(equation));
243 static void
244 programming_base_cb(MathButtons *buttons, GParamSpec *spec)
246 g_settings_set_int(settings, "base", math_buttons_get_programming_base(buttons));
250 static void
251 mode_cb(MathButtons *buttons, GParamSpec *spec)
253 g_settings_set_enum(settings, "button-mode", math_buttons_get_mode(buttons));
257 static void
258 quit_cb(MathWindow *window)
260 gtk_main_quit();
265 main(int argc, char **argv)
267 MathEquation *equation;
268 MathButtons *buttons;
269 int accuracy = 9, word_size = 64, base = 10;
270 gboolean show_tsep = FALSE, show_zeroes = FALSE;
271 MpDisplayFormat number_format;
272 MPAngleUnit angle_units;
273 ButtonMode button_mode;
274 gchar *source_currency, *target_currency;
275 gchar *source_units, *target_units;
277 g_type_init();
279 setlocale(LC_ALL, "");
280 bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
281 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
282 textdomain(GETTEXT_PACKAGE);
284 /* Seed random number generator. */
285 srand48((long) time((time_t *) 0));
287 get_options(argc, argv);
289 settings = g_settings_new ("org.gnome.gcalctool");
290 accuracy = g_settings_get_int(settings, "accuracy");
291 word_size = g_settings_get_int(settings, "word-size");
292 base = g_settings_get_int(settings, "base");
293 show_tsep = g_settings_get_boolean(settings, "show-thousands");
294 show_zeroes = g_settings_get_boolean(settings, "show-zeroes");
295 number_format = g_settings_get_enum(settings, "number-format");
296 angle_units = g_settings_get_enum(settings, "angle-units");
297 button_mode = g_settings_get_enum(settings, "button-mode");
298 source_currency = g_settings_get_string(settings, "source-currency");
299 target_currency = g_settings_get_string(settings, "target-currency");
300 source_units = g_settings_get_string(settings, "source-units");
301 target_units = g_settings_get_string(settings, "target-units");
303 equation = math_equation_new();
304 math_equation_set_accuracy(equation, accuracy);
305 math_equation_set_word_size(equation, word_size);
306 math_equation_set_show_thousands_separators(equation, show_tsep);
307 math_equation_set_show_trailing_zeroes(equation, show_zeroes);
308 math_equation_set_number_format(equation, number_format);
309 math_equation_set_angle_units(equation, angle_units);
310 math_equation_set_source_currency(equation, source_currency);
311 math_equation_set_target_currency(equation, target_currency);
312 math_equation_set_source_units(equation, source_units);
313 math_equation_set_target_units(equation, target_units);
314 g_free(source_currency);
315 g_free(target_currency);
316 g_free(source_units);
317 g_free(target_units);
319 g_signal_connect(equation, "notify::accuracy", G_CALLBACK(accuracy_cb), NULL);
320 g_signal_connect(equation, "notify::word-size", G_CALLBACK(word_size_cb), NULL);
321 g_signal_connect(equation, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb), NULL);
322 g_signal_connect(equation, "notify::show-trailing-zeroes", G_CALLBACK(show_trailing_zeroes_cb), NULL);
323 g_signal_connect(equation, "notify::number-format", G_CALLBACK(number_format_cb), NULL);
324 g_signal_connect(equation, "notify::angle-units", G_CALLBACK(angle_unit_cb), NULL);
325 g_signal_connect(equation, "notify::source-currency", G_CALLBACK(source_currency_cb), NULL);
326 g_signal_connect(equation, "notify::target-currency", G_CALLBACK(target_currency_cb), NULL);
327 g_signal_connect(equation, "notify::source-units", G_CALLBACK(source_units_cb), NULL);
328 g_signal_connect(equation, "notify::target-units", G_CALLBACK(target_units_cb), NULL);
330 gtk_init(&argc, &argv);
332 window = math_window_new(equation);
333 buttons = math_window_get_buttons(window);
334 g_signal_connect(G_OBJECT(window), "quit", G_CALLBACK(quit_cb), NULL);
335 math_buttons_set_programming_base(buttons, base);
336 math_buttons_set_mode(buttons, button_mode); // FIXME: We load the basic buttons even if we immediately switch to the next type
337 g_signal_connect(buttons, "notify::programming-base", G_CALLBACK(programming_base_cb), NULL);
338 g_signal_connect(buttons, "notify::mode", G_CALLBACK(mode_cb), NULL);
340 gtk_widget_show(GTK_WIDGET(window));
341 gtk_main();
343 return(0);