3c90f9f9fffaa6390b7bf1396a3511448cb0f63a
[speedometer] / util.c
1 /****
2         Speedometer, shows your current speed using GPS
3         Copyright (C) 2008 Wellu Mäkinen <wellu@wellu.org>
4
5         This program is free software: you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation, either version 3 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  ****/
18
19 #include <location/location-gps-device.h>
20 #include <location/location-gpsd-control.h>
21 #include <gconf/gconf-client.h>
22
23 #include "util.h"
24 #include "appdata.h"
25 #include "callbacks.h"
26 #include "ui.h"
27
28 #define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
29
30 static LocationGPSDevice *device = NULL;
31 static LocationGPSDControl *control = NULL;
32
33 void start_gps(AppData* appdata) {
34 #ifdef __arm__
35         g_assert(appdata);
36         if(!device) {
37                 device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
38                 g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
39         }
40         control = location_gpsd_control_get_default();
41         location_gpsd_control_request_status(control);
42         if(control->can_control) {
43                 location_gpsd_control_start(control);
44         }
45 #endif // __arm__
46 }
47
48 void stop_gps(AppData* appdata) {
49 #ifdef __arm__
50         g_assert(appdata);
51         control = location_gpsd_control_get_default();
52         location_gpsd_control_request_status(control);
53         if(control->can_control) {
54                 location_gpsd_control_stop(control);
55         }
56 #endif // __arm__
57 }
58
59 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
60         g_assert(appdata);
61
62         // if speed is below one then it's zero
63         if(speed < 1) {
64                 set_nth_digit(appdata, 0, 0);
65                 set_nth_digit(appdata, 1, 0);
66                 set_nth_digit(appdata, 2, 0);
67                 return;
68         }
69
70         // speed => 1
71         // first let's convert the number to string
72         gchar* cspeed = g_malloc(30); // alloc
73         g_sprintf(cspeed, "%f", speed);
74
75         // split the string using comma as delimiter
76         gchar** splitted = g_strsplit(cspeed, ",", 2); // alloc
77
78         gchar* ckm = splitted[0]; // contains the km/h part e.g 123 assuming speed was 123,034510
79         gchar* cm = splitted[1]; // contains the m/h part e.g 034510 assuming speed was 123,034510
80
81         g_print("Original speed %c, splitted speed %c and %c\n", cspeed, ckm, cm);
82
83         // we need to pad km part in order to ensure it is *atleast* 3 digits long
84         gchar* padding = "00";
85         gchar* padded = g_strconcat(padding, cm, NULL); // alloc
86
87         g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
88
89
90         guint i = 2;
91         guint pspeedl = strlen(padded);
92
93         while(i+1) {
94                 guint value = g_ascii_digit_value(padded[pspeedl]);
95                 set_nth_digit(appdata, i, value);
96                 i--;
97         }
98         repaint_all_digits(appdata);
99
100         g_free(padded);
101         g_free(cspeed);
102         g_strfreev(splitted);
103 }
104
105 static show_dialog() {
106         GtkWidget *dialog = gtk_message_dialog_new(
107                         NULL,
108                         GTK_DIALOG_MODAL,
109                         GTK_MESSAGE_INFO,
110                         GTK_BUTTONS_OK,
111                         "This program is licensed under GNU General Public License, "
112                         "which means (among other things) that you don't have to pay "
113                         "a dime for it. "
114                         "If you think, however, that this software is worth it, you "
115                         "can always drop me a postcard.\n\n"
116                         "Wellu Mäkinen\n"
117                         "PO BOX\n"
118                         "33580 Tampere\n"
119                         "FINLAND");
120         gtk_dialog_run(GTK_DIALOG(dialog));
121         gtk_widget_destroy(dialog);
122 }
123
124 void show_cardware_dialog() {
125         GConfClient* client = gconf_client_get_default();
126         g_assert(GCONF_IS_CLIENT(client));
127
128         GConfValue* gcvalue = NULL;
129         gcvalue = gconf_client_get_without_default(client, GCONF_KEY, NULL);
130
131         if(gcvalue == NULL) {
132                 g_print("GConf key not found so show dialog.");
133                 show_dialog();
134                 gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
135         }
136         g_object_unref(client);
137 }
138