Minor thingies..
[speedometer] / util.c
diff --git a/util.c b/util.c
index dee79a0..a20f0b1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -14,8 +14,9 @@
 
        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
- ****/
+****/
 
+#include <math.h>
 #include <location/location-gps-device.h>
 #include <location/location-gpsd-control.h>
 #include <gconf/gconf-client.h>
 #include "callbacks.h"
 #include "ui.h"
 
-#define GCONF_DIR "/apps/Maemo/speedometer/"
+
+gdouble unit_array[] = {3.6};
+
+#define GCONF_KEY "/apps/Maemo/speedometer/disclaimer"
 
 static LocationGPSDevice *device = NULL;
 static LocationGPSDControl *control = NULL;
 
 void start_gps(AppData* appdata) {
 #ifdef __arm__
+       g_assert(appdata);
        if(!device) {
                device = g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
                g_signal_connect(device, "changed", G_CALLBACK(location_changed), appdata);
@@ -46,6 +51,7 @@ void start_gps(AppData* appdata) {
 
 void stop_gps(AppData* appdata) {
 #ifdef __arm__
+       g_assert(appdata);
        control = location_gpsd_control_get_default();
        location_gpsd_control_request_status(control);
        if(control->can_control) {
@@ -55,64 +61,58 @@ void stop_gps(AppData* appdata) {
 }
 
 void interpret_speed_from_gps(AppData* appdata, gdouble speed) {
+       g_assert(appdata);
+       g_assert(!isnan(speed));
 
-       // 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
+       // convert float to a 6 digits (including dot) wide string with leading zeros
+       gchar* charspeed = g_malloc(10); // alloc
+       g_sprintf(charspeed, "%0*.2f", 6, speed);
 
-       g_print("Original speed %c, padded speed %c (km) and %c (m)\n", cspeed, padded, cm);
+       g_print("Speed is %s km/h\n", charspeed);
 
-
-       guint i = 2;
-       guint pspeedl = strlen(padded);
-
-       while(i+1) {
-               guint value = g_ascii_digit_value(padded[pspeedl]);
-               set_nth_digit(appdata, i, value);
+       // set the main digits
+       guint i = 3;
+       while(i) {
                i--;
+               set_nth_digit(appdata, i, g_ascii_digit_value(charspeed[i]));
        }
+
        repaint_all_digits(appdata);
 
-       g_free(padded);
-       g_free(cspeed);
-       g_strfreev(splitted);
+       g_free(charspeed);
+}
+
+
+static show_dialog() {
+       GtkWidget *dialog = gtk_message_dialog_new(
+                       NULL,
+                       GTK_DIALOG_MODAL,
+                       GTK_MESSAGE_INFO,
+                       GTK_BUTTONS_OK,
+                       "This program is licensed under GNU General Public License, "
+                       "which means (among other things) that you don't have to pay "
+                       "a dime for it. "
+                       "If you think, however, that this software is worth it, you "
+                       "can always drop me a postcard. (or a speeding ticket :)\n\n"
+                       "Wellu Mäkinen\n"
+                       "PO BOX\n"
+                       "33580 Tampere\n"
+                       "FINLAND");
+       gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_destroy(dialog);
 }
 
-void show_postcard_dialog() {
+void show_cardware_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);
+       gcvalue = gconf_client_get_without_default(client, GCONF_KEY, 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_print("GConf key not found so show dialog.");
+               show_dialog();
+               gconf_client_set_bool(client, GCONF_KEY, TRUE, NULL);
        }
        g_object_unref(client);
 }