GConf support.
[speedometer] / util.c
diff --git a/util.c b/util.c
index b53b8aa..dee79a0 100644 (file)
--- a/util.c
+++ b/util.c
 
 #include <location/location-gps-device.h>
 #include <location/location-gpsd-control.h>
+#include <gconf/gconf-client.h>
 
 #include "util.h"
 #include "appdata.h"
 #include "callbacks.h"
 #include "ui.h"
 
+#define GCONF_DIR "/apps/Maemo/speedometer/"
+
 static LocationGPSDevice *device = NULL;
 static LocationGPSDControl *control = NULL;
 
@@ -37,7 +40,7 @@ void start_gps(AppData* appdata) {
        location_gpsd_control_request_status(control);
        if(control->can_control) {
                location_gpsd_control_start(control);
-               }
+       }
 #endif // __arm__
 }
 
@@ -53,19 +56,64 @@ void stop_gps(AppData* appdata) {
 
 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
 
-       // first let's convert the number to characters
-       gchar* c_digits = calloc(30, 1);
+       // if speed is below one then it's zero
+       if(speed < 1) {
+               set_nth_digit(appdata, 0, 0);
+               set_nth_digit(appdata, 1, 0);
+               set_nth_digit(appdata, 2, 0);
+               return;
+       }
+
+       // speed => 1
+       // first let's convert the number to string
+       gchar* cspeed = g_malloc(30); // alloc
+       g_sprintf(cspeed, "%f", speed);
+
+       // split the string using comma as delimiter
+       gchar** splitted = g_strsplit(cspeed, ",", 2); // alloc
+
+       gchar* ckm = splitted[0]; // contains the km/h part e.g 123 assuming speed was 123,034510
+       gchar* cm = splitted[1]; // contains the m/h part e.g 034510 assuming speed was 123,034510
+
+       g_print("Original speed %c, splitted speed %c and %c\n", cspeed, ckm, cm);
+
+       // we need to pad km part in order to ensure it is *atleast* 3 digits long
+       gchar* padding = "00";
+       gchar* padded = g_strconcat(padding, cm, NULL); // alloc
+
+       g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
 
-       g_sprintf(c_digits, "%f", speed);
 
-       g_print("interpret_speed_from_gps: %s", c_digits);
+       guint i = 2;
+       guint pspeedl = strlen(padded);
 
-       int i = 0;
-       while(i < 3) {
-               guint value = g_ascii_digit_value(c_digits[i]);
+       while(i+1) {
+               guint value = g_ascii_digit_value(padded[pspeedl]);
                set_nth_digit(appdata, i, value);
-               i++;
+               i--;
+       }
+       repaint_all_digits(appdata);
+
+       g_free(padded);
+       g_free(cspeed);
+       g_strfreev(splitted);
+}
+
+void show_postcard_dialog() {
+       GConfClient* client = gconf_client_get_default();
+       g_assert(GCONF_IS_CLIENT(client));
+
+
+       GConfValue* gcvalue = NULL;
+       gcvalue = gconf_client_get_without_default(client, GCONF_DIR "disclaimer", NULL);
+
+       if(gcvalue == NULL) {
+               g_warning("key %sdisclaimer not found\n", GCONF_DIR);
+               gconf_client_set_bool(client, GCONF_DIR "disclaimer", TRUE, NULL);
+       }
+       else {
+               g_print("key %sdisclaimer was found\n", GCONF_DIR);
        }
-       g_free(c_digits);
+       g_object_unref(client);
 }