renamed alarm_limit to alarm_high_thresold
[psensor.git] / src / ui_sensorpref.c
index a3b97b2..286b25f 100644 (file)
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2012 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <stdlib.h>
 
 #include <gtk/gtk.h>
@@ -33,7 +32,7 @@ struct sensor_pref {
        int enabled;
        struct color *color;
        int alarm_enabled;
-       double alarm_limit;
+       int alarm_high_thresold;
 };
 
 struct cb_data {
@@ -42,7 +41,8 @@ struct cb_data {
        struct sensor_pref **prefs;
 };
 
-static struct sensor_pref *sensor_pref_new(struct psensor *s)
+static struct sensor_pref *sensor_pref_new(struct psensor *s,
+                                          struct config *cfg)
 {
        struct sensor_pref *p = malloc(sizeof(struct sensor_pref));
 
@@ -51,9 +51,14 @@ static struct sensor_pref *sensor_pref_new(struct psensor *s)
        p->name = strdup(s->name);
        p->enabled = s->enabled;
        p->alarm_enabled = s->alarm_enabled;
-       p->alarm_limit = s->alarm_limit;
        p->color = color_dup(s->color);
 
+       if (cfg->temperature_unit == CELCIUS)
+               p->alarm_high_thresold = s->alarm_high_thresold;
+       else
+               p->alarm_high_thresold
+                       = celcius_to_fahrenheit(s->alarm_high_thresold);
+
        return p;
 }
 
@@ -68,7 +73,8 @@ static void sensor_pref_free(struct sensor_pref *p)
        free(p);
 }
 
-static struct sensor_pref **sensor_pref_list_new(struct psensor **sensors)
+static struct sensor_pref **sensor_pref_list_new(struct psensor **sensors,
+                                                struct config *cfg)
 {
        int n, i;
        struct sensor_pref **pref_list;
@@ -77,7 +83,8 @@ static struct sensor_pref **sensor_pref_list_new(struct psensor **sensors)
        pref_list = malloc(sizeof(struct sensor_pref *) * (n+1));
 
        for (i = 0; i < n; i++)
-               pref_list[i] = sensor_pref_new(sensors[i]);
+               pref_list[i] = sensor_pref_new(sensors[i],
+                                              cfg);
 
        pref_list[n] = NULL;
 
@@ -115,7 +122,7 @@ sensor_pref_get(struct sensor_pref **ps, struct psensor *s)
 }
 
 static struct sensor_pref *
-get_seleted_sensor_pref(GtkBuilder *builder, struct sensor_pref **ps)
+get_selected_sensor_pref(GtkBuilder *builder, struct sensor_pref **ps)
 {
        GtkTreeModel *model;
        GtkTreeIter iter;
@@ -148,7 +155,7 @@ static void on_name_changed(GtkEntry *entry, gpointer data)
 
        str = gtk_entry_get_text(entry);
 
-       p = get_seleted_sensor_pref(cbdata->builder, cbdata->prefs);
+       p = get_selected_sensor_pref(cbdata->builder, cbdata->prefs);
 
        if (p && strcmp(p->name, str)) {
                free(p->name);
@@ -162,7 +169,7 @@ on_drawed_toggled(GtkToggleButton *btn, gpointer data)
        struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_seleted_sensor_pref(cbdata->builder, cbdata->prefs);
+       p = get_selected_sensor_pref(cbdata->builder, cbdata->prefs);
 
        if (p)
                p->enabled = gtk_toggle_button_get_active(btn);
@@ -174,7 +181,7 @@ on_alarm_toggled(GtkToggleButton *btn, gpointer data)
        struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_seleted_sensor_pref(cbdata->builder, cbdata->prefs);
+       p = get_selected_sensor_pref(cbdata->builder, cbdata->prefs);
 
        if (p)
                p->alarm_enabled = gtk_toggle_button_get_active(btn);
@@ -186,7 +193,7 @@ static void on_color_set(GtkColorButton *widget, gpointer data)
        struct sensor_pref *p;
        GdkColor color;
 
-       p = get_seleted_sensor_pref(cbdata->builder, cbdata->prefs);
+       p = get_selected_sensor_pref(cbdata->builder, cbdata->prefs);
 
        if (p) {
                gtk_color_button_get_color(widget, &color);
@@ -199,10 +206,10 @@ static void on_temp_limit_changed(GtkSpinButton *btn, gpointer data)
        struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_seleted_sensor_pref(cbdata->builder, cbdata->prefs);
+       p = get_selected_sensor_pref(cbdata->builder, cbdata->prefs);
 
        if (p)
-               p->alarm_limit = gtk_spin_button_get_value(btn);
+               p->alarm_high_thresold = gtk_spin_button_get_value(btn);
 }
 
 static void connect_signals(GtkBuilder *builder, struct cb_data *cbdata)
@@ -225,15 +232,19 @@ static void connect_signals(GtkBuilder *builder, struct cb_data *cbdata)
 }
 
 static void
-update_pref(struct psensor *s, struct sensor_pref **prefs, GtkBuilder *builder)
+update_pref(struct psensor *s,
+           struct sensor_pref **prefs,
+           struct config *cfg,
+           GtkBuilder *builder)
 {
-       GtkLabel *w_id, *w_type;
+       GtkLabel *w_id, *w_type, *w_temp_unit;
        GtkEntry *w_name;
        GtkToggleButton *w_draw, *w_alarm;
        GtkColorButton *w_color;
        GtkSpinButton *w_temp_limit;
        GdkColor *color;
        struct sensor_pref *p = sensor_pref_get(prefs, s);
+       int use_celcius;
 
        w_id = GTK_LABEL(gtk_builder_get_object(builder, "sensor_id"));
        gtk_label_set_text(w_id, s->id);
@@ -259,9 +270,18 @@ update_pref(struct psensor *s, struct sensor_pref **prefs, GtkBuilder *builder)
                = GTK_SPIN_BUTTON(gtk_builder_get_object(builder,
                                                         "sensor_temp_limit"));
 
+       w_temp_unit
+               = GTK_LABEL(gtk_builder_get_object(builder,
+                                                  "sensor_temp_unit"));
+
+       use_celcius = cfg->temperature_unit == CELCIUS ? 1 : 0;
+       gtk_label_set_text(w_temp_unit,
+                          psensor_type_to_unit_str(s->type,
+                                                   use_celcius));
+
        if (is_temp_type(s->type)) {
                gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
-               gtk_spin_button_set_value(w_temp_limit, p->alarm_limit);
+               gtk_spin_button_set_value(w_temp_limit, p->alarm_high_thresold);
                gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
                gtk_widget_set_sensitive(GTK_WIDGET(w_temp_limit), TRUE);
        } else {
@@ -284,7 +304,10 @@ static void on_changed(GtkTreeSelection *selection, gpointer data)
                gint *indices = gtk_tree_path_get_indices(p);
                struct psensor *s = *(ui->sensors + *indices);
 
-               update_pref(s, cbdata->prefs, cbdata->builder);
+               update_pref(s,
+                           cbdata->prefs,
+                           ui->config,
+                           cbdata->builder);
 
                gtk_tree_path_free(p);
        }
@@ -316,7 +339,9 @@ select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
 }
 
 static void
-apply_prefs(struct sensor_pref **prefs, struct psensor **sensors)
+apply_prefs(struct sensor_pref **prefs,
+           struct psensor **sensors,
+           struct config *cfg)
 {
        int n = psensor_list_size(sensors);
        int i;
@@ -336,11 +361,14 @@ apply_prefs(struct sensor_pref **prefs, struct psensor **sensors)
                        config_set_sensor_enabled(s->id, s->enabled);
                }
 
-               if (s->alarm_limit != p->alarm_limit) {
-                       s->alarm_limit = p->alarm_limit;
-                       config_set_sensor_alarm_limit(s->id,
-                                                     s->alarm_limit);
-               }
+               if (cfg->temperature_unit == CELCIUS)
+                       s->alarm_high_thresold = p->alarm_high_thresold;
+               else
+                       s->alarm_high_thresold = fahrenheit_to_celcius
+                               (p->alarm_high_thresold);
+
+               config_set_sensor_alarm_high_thresold(s->id,
+                                                     s->alarm_high_thresold);
 
                if (s->alarm_enabled != p->alarm_enabled) {
                        s->alarm_enabled = p->alarm_enabled;
@@ -369,7 +397,8 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
        struct cb_data cbdata;
 
        cbdata.ui = ui;
-       cbdata.prefs = sensor_pref_list_new(ui->sensors);
+       cbdata.prefs = sensor_pref_list_new(ui->sensors,
+                                           ui->config);
 
        builder = gtk_builder_new();
        cbdata.builder = builder;
@@ -385,7 +414,7 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
                return ;
        }
 
-       update_pref(sensor, cbdata.prefs, builder);
+       update_pref(sensor, cbdata.prefs, ui->config, builder);
        connect_signals(builder, &cbdata);
 
        w_sensors_list
@@ -420,7 +449,7 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
        result = gtk_dialog_run(diag);
 
        if (result == GTK_RESPONSE_ACCEPT) {
-               apply_prefs(cbdata.prefs, ui->sensors);
+               apply_prefs(cbdata.prefs, ui->sensors, ui->config);
                ui_sensorlist_update_sensors_preferences(ui);
        }