1 /*
2  * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3  * Copyright (C) 2008-2011 Robert Ancell
4  *
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.
10  */
11 
12 #ifdef HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <locale.h>
20 #include <glib/gi18n.h>
21 
22 #include "math-window.h"
23 #include "math-preferences.h"
24 #include "mp-equation.h"
25 #include "unit-manager.h"
26 #include "utility.h"
27 
28 GSettings *g_settings_var = NULL;
29 
30 static MathWindow *window;
31 
32 static void
version(const gchar * progname)33 version(const gchar *progname)
34 {
35     /* NOTE: Is not translated so can be easily parsed */
36     fprintf(stderr, "%1$s %2$s\n", progname, VERSION);
37 }
38 
39 
40 static int
do_convert(const MPNumber * x,const char * x_units,const char * z_units,MPNumber * z,void * data)41 do_convert(const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z, void *data)
42 {
43     return unit_manager_convert_by_symbol(unit_manager_get_default(), x, x_units, z_units, z);
44 }
45 
46 
47 static void
solve(const char * equation)48 solve(const char *equation)
49 {
50     MPEquationOptions options;
51     MPErrorCode error;
52     MPNumber result = mp_new();
53     char *result_str;
54 
55     memset(&options, 0, sizeof(options));
56     options.base = 10;
57     options.wordlen = 32;
58     options.angle_units = MP_DEGREES;
59     options.convert = do_convert;
60 
61     error = mp_equation_parse(equation, &options, &result, NULL);
62     if(error == PARSER_ERR_MP) {
63         fprintf(stderr, "Error: %s\n", mp_get_error());
64         mp_clear(&result);
65         exit(1);
66     }
67     else if(error != 0) {
68         fprintf(stderr, "Error: %s\n", mp_error_code_to_string(error));
69         mp_clear(&result);
70         exit(1);
71     }
72     else {
73         result_str = mp_serializer_to_string(mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC, 10, 9), &result);
74         printf("%s\n", result_str);
75         mp_clear(&result);
76         exit(0);
77     }
78 }
79 
80 
81 static void
usage(const gchar * progname,gboolean show_application,gboolean show_gtk)82 usage(const gchar *progname, gboolean show_application, gboolean show_gtk)
83 {
84     fprintf(stderr,
85             /* Description on how to use mate-calc displayed on command-line */
86             _("Usage:\n"
87               "  %s — Perform mathematical calculations"), progname);
88 
89     fprintf(stderr,
90             "\n\n");
91 
92     fprintf(stderr,
93             /* Description on mate-calc command-line help options displayed on command-line */
94             _("Help Options:\n"
95               "  -v, --version                   Show release version\n"
96               "  -h, -?, --help                  Show help options\n"
97               "  --help-all                      Show all help options\n"
98               "  --help-gtk                      Show GTK+ options"));
99     fprintf(stderr,
100             "\n\n");
101 
102     if (show_gtk) {
103         fprintf(stderr,
104                 /* Description on mate-calc command-line GTK+ options displayed on command-line */
105                 _("GTK+ Options:\n"
106                   "  --class=CLASS                   Program class as used by the window manager\n"
107                   "  --name=NAME                     Program name as used by the window manager\n"
108                   "  --screen=SCREEN                 X screen to use\n"
109                   "  --sync                          Make X calls synchronous\n"
110                   "  --gtk-module=MODULES            Load additional GTK+ modules\n"
111                   "  --g-fatal-warnings              Make all warnings fatal"));
112         fprintf(stderr,
113                 "\n\n");
114     }
115 
116     if (show_application) {
117         fprintf(stderr,
118                 /* Description on mate-calc application options displayed on command-line */
119                 _("Application Options:\n"
120                   "  -s, --solve <equation>          Solve the given equation"));
121         fprintf(stderr,
122                 "\n\n");
123     }
124 }
125 
126 
127 static void
get_options(int argc,char * argv[])128 get_options(int argc, char *argv[])
129 {
130     int i;
131     char *progname, *arg;
132 
133     progname = g_path_get_basename(argv[0]);
134 
135     for (i = 1; i < argc; i++) {
136         arg = argv[i];
137 
138         if (strcmp(arg, "-v") == 0 ||
139             strcmp(arg, "--version") == 0) {
140             version(progname);
141             g_free(progname);
142             exit(0);
143         }
144         else if (strcmp(arg, "-h") == 0 ||
145                  strcmp(arg, "-?") == 0 ||
146                  strcmp(arg, "--help") == 0) {
147             usage(progname, TRUE, FALSE);
148             g_free(progname);
149             exit(0);
150         }
151         else if (strcmp(arg, "--help-all") == 0) {
152             usage(progname, TRUE, TRUE);
153             g_free(progname);
154             exit(0);
155         }
156         else if (strcmp(arg, "--help-gtk") == 0) {
157             usage(progname, FALSE, TRUE);
158             g_free(progname);
159             exit(0);
160         }
161         else if (strcmp(arg, "-s") == 0 ||
162             strcmp(arg, "--solve") == 0) {
163             i++;
164             if (i >= argc) {
165                 fprintf(stderr,
166                         /* Error printed to stderr when user uses --solve argument without an equation */
167                         _("Argument --solve requires an equation to solve"));
168                 fprintf(stderr, "\n");
169                 g_free(progname);
170                 exit(1);
171             }
172             else
173                 solve(argv[i]);
174         }
175         else {
176             fprintf(stderr,
177                     /* Error printed to stderr when user provides an unknown command-line argument */
178                     _("Unknown argument '%s'"), arg);
179             fprintf(stderr, "\n");
180             usage(progname, TRUE, FALSE);
181             g_free(progname);
182             exit(1);
183         }
184     }
185 
186     g_free(progname);
187 }
188 
189 static void
quit_cb(MathWindow * win)190 quit_cb(MathWindow *win)
191 {
192     gtk_main_quit();
193 }
194 
main(int argc,char ** argv)195 int main(int argc, char **argv)
196 {
197     MathEquation *equation;
198     MathButtons *buttons;
199     int accuracy = 9, word_size = 64, base = 10;
200     gboolean show_tsep = FALSE, show_zeroes = FALSE, show_hist = FALSE;
201     MpDisplayFormat number_format;
202     MPAngleUnit angle_units;
203     ButtonMode button_mode;
204     gchar *source_currency, *target_currency;
205     gchar *source_units, *target_units;
206 
207     setlocale(LC_ALL, "");
208     bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
209     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
210     textdomain(GETTEXT_PACKAGE);
211 
212     /* Seed random number generator. */
213     srand48((long) time((time_t *) 0));
214 
215     gtk_init(&argc, &argv);
216 
217     g_settings_var = g_settings_new ("org.mate.calc");
218     accuracy = g_settings_get_int(g_settings_var, "accuracy");
219     word_size = g_settings_get_int(g_settings_var, "word-size");
220     base = g_settings_get_int(g_settings_var, "base");
221     show_tsep = g_settings_get_boolean(g_settings_var, "show-thousands");
222     show_zeroes = g_settings_get_boolean(g_settings_var, "show-zeroes");
223     show_hist = g_settings_get_boolean(g_settings_var, "show-history");
224     number_format = g_settings_get_enum(g_settings_var, "number-format");
225     angle_units = g_settings_get_enum(g_settings_var, "angle-units");
226     button_mode = g_settings_get_enum(g_settings_var, "button-mode");
227     source_currency = g_settings_get_string(g_settings_var, "source-currency");
228     target_currency = g_settings_get_string(g_settings_var, "target-currency");
229     source_units = g_settings_get_string(g_settings_var, "source-units");
230     target_units = g_settings_get_string(g_settings_var, "target-units");
231 
232     equation = math_equation_new();
233     math_equation_set_accuracy(equation, accuracy);
234     math_equation_set_word_size(equation, word_size);
235     math_equation_set_show_thousands_separators(equation, show_tsep);
236     math_equation_set_show_trailing_zeroes(equation, show_zeroes);
237     math_equation_set_number_format(equation, number_format);
238     math_equation_set_angle_units(equation, angle_units);
239     math_equation_set_source_currency(equation, source_currency);
240     math_equation_set_target_currency(equation, target_currency);
241     math_equation_set_source_units(equation, source_units);
242     math_equation_set_target_units(equation, target_units);
243     g_free(source_currency);
244     g_free(target_currency);
245     g_free(source_units);
246     g_free(target_units);
247 
248     get_options(argc, argv);
249 
250     //gtk_window_set_default_icon_name("accessories-calculator");
251 
252     window = math_window_new(equation);
253     buttons = math_window_get_buttons(window);
254     g_signal_connect(G_OBJECT(window), "quit", G_CALLBACK(quit_cb), NULL);
255     math_window_set_show_history(window, show_hist);
256     math_buttons_set_programming_base(buttons, base);
257     math_buttons_set_mode(buttons, button_mode); // FIXME: We load the basic buttons even if we immediately switch to the next type
258 
259     gtk_widget_show(GTK_WIDGET(window));
260     gtk_main();
261 
262     return 0;
263 }
264