dee79a0f531547e0e29e9cae07c56d292ef8012b
[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_DIR "/apps/Maemo/speedometer/"
29
30 static LocationGPSDevice *device = NULL;
31 static LocationGPSDControl *control = NULL;
32
33 void start_gps(AppData* appdata) {
34 #ifdef __arm__
35         if(!device) {
36                 device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
37                 g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
38         }
39         control = location_gpsd_control_get_default();
40         location_gpsd_control_request_status(control);
41         if(control->can_control) {
42                 location_gpsd_control_start(control);
43         }
44 #endif // __arm__
45 }
46
47 void stop_gps(AppData* appdata) {
48 #ifdef __arm__
49         control = location_gpsd_control_get_default();
50         location_gpsd_control_request_status(control);
51         if(control->can_control) {
52                 location_gpsd_control_stop(control);
53         }
54 #endif // __arm__
55 }
56
57 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
58
59         // if speed is below one then it's zero
60         if(speed < 1) {
61                 set_nth_digit(appdata, 0, 0);
62                 set_nth_digit(appdata, 1, 0);
63                 set_nth_digit(appdata, 2, 0);
64                 return;
65         }
66
67         // speed => 1
68         // first let's convert the number to string
69         gchar* cspeed = g_malloc(30); // alloc
70         g_sprintf(cspeed, "%f", speed);
71
72         // split the string using comma as delimiter
73         gchar** splitted = g_strsplit(cspeed, ",", 2); // alloc
74
75         gchar* ckm = splitted[0]; // contains the km/h part e.g 123 assuming speed was 123,034510
76         gchar* cm = splitted[1]; // contains the m/h part e.g 034510 assuming speed was 123,034510
77
78         g_print("Original speed %c, splitted speed %c and %c\n", cspeed, ckm, cm);
79
80         // we need to pad km part in order to ensure it is *atleast* 3 digits long
81         gchar* padding = "00";
82         gchar* padded = g_strconcat(padding, cm, NULL); // alloc
83
84         g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
85
86
87         guint i = 2;
88         guint pspeedl = strlen(padded);
89
90         while(i+1) {
91                 guint value = g_ascii_digit_value(padded[pspeedl]);
92                 set_nth_digit(appdata, i, value);
93                 i--;
94         }
95         repaint_all_digits(appdata);
96
97         g_free(padded);
98         g_free(cspeed);
99         g_strfreev(splitted);
100 }
101
102 void show_postcard_dialog() {
103         GConfClient* client = gconf_client_get_default();
104         g_assert(GCONF_IS_CLIENT(client));
105
106
107         GConfValue* gcvalue = NULL;
108         gcvalue = gconf_client_get_without_default(client, GCONF_DIR "disclaimer", NULL);
109
110         if(gcvalue == NULL) {
111                 g_warning("key %sdisclaimer not found\n", GCONF_DIR);
112                 gconf_client_set_bool(client, GCONF_DIR "disclaimer", TRUE, NULL);
113         }
114         else {
115                 g_print("key %sdisclaimer was found\n", GCONF_DIR);
116         }
117         g_object_unref(client);
118 }
119