avoid config in the psensor struct
[psensor.git] / src / ui_sensorpref.c
index 8c45eaf..282eed4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 jeanfi@gmail.com
+ * Copyright (C) 2010-2014 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
 
 #include <gtk/gtk.h>
 
-#include "cfg.h"
-#include "ui_pref.h"
-#include "ui_sensorlist.h"
-#include "ui_sensorpref.h"
-#include "ui_color.h"
+#include <cfg.h>
+#include <temperature.h>
+#include <ui_appindicator.h>
+#include <ui_pref.h>
+#include <ui_sensorlist.h>
+#include <ui_sensorpref.h>
+#include <ui_color.h>
 
-#if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
-#include "ui_appindicator.h"
-#endif
 
 enum {
        COL_NAME = 0,
-       COL_SENSOR
+       COL_SENSOR_PREF
 };
 
 struct sensor_pref {
        struct psensor *sensor;
        char *name;
-       int enabled;
+       int graph_enabled;
        struct color *color;
        int alarm_enabled;
        int alarm_high_threshold;
        int alarm_low_threshold;
-       unsigned int appindicator_enabled;
+       bool appindicator_enabled;
+       unsigned int appindicator_label_enabled;
+       unsigned int display_enabled;
 };
 
 struct cb_data {
@@ -51,8 +52,8 @@ struct cb_data {
        GtkBuilder *builder;
 };
 
-static struct sensor_pref *sensor_pref_new(struct psensor *s,
-                                          struct config *cfg)
+static struct sensor_pref *
+sensor_pref_new(struct psensor *s, struct config *cfg)
 {
        struct sensor_pref *p;
 
@@ -60,21 +61,24 @@ static struct sensor_pref *sensor_pref_new(struct psensor *s,
 
        p->sensor = s;
        p->name = strdup(s->name);
-       p->enabled = s->enabled;
-       p->alarm_enabled = s->alarm_enabled;
+       p->graph_enabled = s->graph_enabled;
+       p->alarm_enabled = config_get_sensor_alarm_enabled(s->id);
        p->color = color_dup(s->color);
+       p->display_enabled = config_is_sensor_enabled(s->id);
 
-       if (cfg->temperature_unit == CELCIUS) {
+       if (cfg->temperature_unit == CELSIUS) {
                p->alarm_high_threshold = s->alarm_high_threshold;
                p->alarm_low_threshold = s->alarm_low_threshold;
        } else {
                p->alarm_high_threshold
-                       = celcius_to_fahrenheit(s->alarm_high_threshold);
+                       = celsius_to_fahrenheit(s->alarm_high_threshold);
                p->alarm_low_threshold
-                       = celcius_to_fahrenheit(s->alarm_low_threshold);
+                       = celsius_to_fahrenheit(s->alarm_low_threshold);
        }
 
-       p->appindicator_enabled = s->appindicator_enabled;
+       p->appindicator_enabled = config_is_appindicator_enabled(s->id);
+       p->appindicator_label_enabled
+               = config_is_appindicator_label_enabled(s->id);
 
        return p;
 }
@@ -82,7 +86,7 @@ static struct sensor_pref *sensor_pref_new(struct psensor *s,
 static void sensor_pref_free(struct sensor_pref *p)
 {
        if (!p)
-               return ;
+               return;
 
        free(p->name);
        free(p->color);
@@ -90,35 +94,30 @@ static void sensor_pref_free(struct sensor_pref *p)
        free(p);
 }
 
-static struct sensor_pref *
-get_selected_sensor_pref(GtkBuilder *builder)
+static struct sensor_pref *get_selected_sensor_pref(GtkTreeView *tree)
 {
        GtkTreeModel *model;
        GtkTreeIter iter;
        struct sensor_pref *pref;
        GtkTreeSelection *selection;
-       GtkTreeView *tree;
-
-       tree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "sensors_list"));
 
        selection = gtk_tree_view_get_selection(tree);
 
        pref = NULL;
        if (gtk_tree_selection_get_selected(selection, &model, &iter))
-               gtk_tree_model_get(model, &iter, COL_SENSOR, &pref, -1);
+               gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &pref, -1);
 
        return pref;
 }
 
-static void on_name_changed(GtkEntry *entry, gpointer data)
+void ui_sensorpref_name_changed_cb(GtkEntry *entry, gpointer data)
 {
-       struct cb_data *cbdata = data;
        struct sensor_pref *p;
        const char *str;
 
        str = gtk_entry_get_text(entry);
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p && strcmp(p->name, str)) {
                free(p->name);
@@ -126,127 +125,107 @@ static void on_name_changed(GtkEntry *entry, gpointer data)
        }
 }
 
-static void
-on_drawed_toggled(GtkToggleButton *btn, gpointer data)
+void ui_sensorpref_draw_toggled_cb(GtkToggleButton *btn, gpointer data)
 {
-       struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p)
-               p->enabled = gtk_toggle_button_get_active(btn);
+               p->graph_enabled = gtk_toggle_button_get_active(btn);
 }
 
-static void
-on_alarm_toggled(GtkToggleButton *btn, gpointer data)
+void ui_sensorpref_display_toggled_cb(GtkToggleButton *btn, gpointer data)
 {
-       struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
+
+       if (p)
+               p->display_enabled = gtk_toggle_button_get_active(btn);
+}
+
+void ui_sensorpref_alarm_toggled_cb(GtkToggleButton *btn, gpointer data)
+{
+       struct sensor_pref *p;
+
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p)
                p->alarm_enabled = gtk_toggle_button_get_active(btn);
 }
 
-static void
-on_appindicator_toggled(GtkToggleButton *btn, gpointer data)
+void
+ui_sensorpref_appindicator_menu_toggled_cb(GtkToggleButton *btn, gpointer data)
 {
-       struct cb_data *cbdata = data;
        struct sensor_pref *p;
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p)
                p->appindicator_enabled = gtk_toggle_button_get_active(btn);
 }
 
-static void on_color_set(GtkColorButton *widget, gpointer data)
+void
+ui_sensorpref_appindicator_label_toggled_cb(GtkToggleButton *btn, gpointer data)
+{
+       struct sensor_pref *p;
+
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
+
+       if (p)
+               p->appindicator_label_enabled
+                       = gtk_toggle_button_get_active(btn);
+}
+
+void ui_sensorpref_color_set_cb(GtkColorButton *widget, gpointer data)
 {
-       struct cb_data *cbdata = data;
        struct sensor_pref *p;
-       GdkColor color;
+       GdkRGBA color;
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p) {
-               gtk_color_button_get_color(widget, &color);
+               gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color);
                color_set(p->color, color.red, color.green, color.blue);
        }
 }
 
-static void on_alarm_high_threshold_changed(GtkSpinButton *btn, gpointer data)
+void
+ui_sensorpref_alarm_high_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
 {
-       struct cb_data *cbdata;
        struct sensor_pref *p;
 
-       cbdata = data;
-
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p)
                p->alarm_high_threshold = gtk_spin_button_get_value(btn);
 }
 
-static void on_alarm_low_threshold_changed(GtkSpinButton *btn, gpointer data)
+void
+ui_sensorpref_alarm_low_threshold_changed_cb(GtkSpinButton *btn, gpointer data)
 {
-       struct cb_data *cbdata;
        struct sensor_pref *p;
 
-       cbdata = data;
-
-       p = get_selected_sensor_pref(cbdata->builder);
+       p = get_selected_sensor_pref(GTK_TREE_VIEW(data));
 
        if (p)
                p->alarm_low_threshold = gtk_spin_button_get_value(btn);
 }
 
-static void connect_signals(GtkBuilder *builder, struct cb_data *cbdata)
-{
-       g_signal_connect(gtk_builder_get_object(builder, "sensor_name"),
-                        "changed", G_CALLBACK(on_name_changed), cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder, "sensor_draw"),
-                        "toggled", G_CALLBACK(on_drawed_toggled), cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder, "sensor_color"),
-                        "color-set", G_CALLBACK(on_color_set), cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder, "sensor_alarm"),
-                        "toggled", G_CALLBACK(on_alarm_toggled), cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder,
-                                               "sensor_alarm_high_threshold"),
-                        "value-changed",
-                        G_CALLBACK(on_alarm_high_threshold_changed),
-                        cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder,
-                                               "sensor_alarm_low_threshold"),
-                        "value-changed",
-                        G_CALLBACK(on_alarm_low_threshold_changed),
-                        cbdata);
-
-       g_signal_connect(gtk_builder_get_object(builder,
-                                               "indicator_checkbox"),
-                        "toggled",
-                        G_CALLBACK(on_appindicator_toggled),
-                        cbdata);
-}
-
 static void
 update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
 {
        GtkLabel *w_id, *w_type, *w_high_threshold_unit, *w_low_threshold_unit,
                *w_chipname;
        GtkEntry *w_name;
-       GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled;
+       GtkToggleButton *w_draw, *w_alarm, *w_appindicator_enabled,
+               *w_appindicator_label_enabled, *w_display;
        GtkColorButton *w_color;
        GtkSpinButton *w_high_threshold, *w_low_threshold;
-       GdkColor *color;
+       GdkRGBA color;
        struct psensor *s;
-       int use_celcius;
+       int use_celsius;
 
        s = p->sensor;
 
@@ -267,12 +246,17 @@ update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
 
        w_draw = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
                                                          "sensor_draw"));
-       gtk_toggle_button_set_active(w_draw, p->enabled);
+       gtk_toggle_button_set_active(w_draw, p->graph_enabled);
 
-       color = color_to_gdkcolor(p->color);
+       w_display = GTK_TOGGLE_BUTTON(gtk_builder_get_object
+                                     (builder,
+                                      "sensor_enable_checkbox"));
+       gtk_toggle_button_set_active(w_display, p->display_enabled);
+
+       color = color_to_GdkRGBA(p->color);
        w_color = GTK_COLOR_BUTTON(gtk_builder_get_object(builder,
                                                          "sensor_color"));
-       gtk_color_button_set_color(w_color, color);
+       gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(w_color), &color);
 
        w_alarm = GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder,
                                                           "sensor_alarm"));
@@ -290,38 +274,48 @@ update_pref(struct sensor_pref *p, struct config *cfg, GtkBuilder *builder)
                                        (builder,
                                         "sensor_alarm_low_threshold_unit"));
 
-       use_celcius = cfg->temperature_unit == CELCIUS ? 1 : 0;
+       use_celsius = cfg->temperature_unit == CELSIUS ? 1 : 0;
        gtk_label_set_text(w_high_threshold_unit,
                           psensor_type_to_unit_str(s->type,
-                                                   use_celcius));
+                                                   use_celsius));
        gtk_label_set_text(w_low_threshold_unit,
                           psensor_type_to_unit_str(s->type,
-                                                   use_celcius));
+                                                   use_celsius));
 
        w_appindicator_enabled = GTK_TOGGLE_BUTTON
                (gtk_builder_get_object(builder, "indicator_checkbox"));
+       w_appindicator_label_enabled = GTK_TOGGLE_BUTTON
+               (gtk_builder_get_object(builder, "indicator_label_checkbox"));
+
 
-       if (is_temp_type(s->type) || is_fan_type(s->type)) {
-               gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
-               gtk_spin_button_set_value(w_high_threshold,
-                                         p->alarm_high_threshold);
-               gtk_spin_button_set_value(w_low_threshold,
-                                         p->alarm_low_threshold);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
+       if (is_appindicator_supported()) {
+               gtk_widget_set_has_tooltip
+                       (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
+               gtk_widget_set_has_tooltip
+                       (GTK_WIDGET(w_appindicator_enabled), FALSE);
        } else {
-               gtk_toggle_button_set_active(w_alarm, 0);
-               gtk_spin_button_set_value(w_high_threshold, 0);
-               gtk_spin_button_set_value(w_low_threshold, 0);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), FALSE);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), FALSE);
-               gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), FALSE);
+               gtk_widget_set_sensitive
+                       (GTK_WIDGET(w_appindicator_label_enabled), FALSE);
+               gtk_widget_set_has_tooltip
+                       (GTK_WIDGET(w_appindicator_label_enabled), TRUE);
+               gtk_widget_set_sensitive
+                       (GTK_WIDGET(w_appindicator_enabled), FALSE);
+               gtk_widget_set_has_tooltip
+                       (GTK_WIDGET(w_appindicator_enabled), TRUE);
        }
 
+       gtk_toggle_button_set_active(w_alarm, p->alarm_enabled);
+       gtk_spin_button_set_value(w_high_threshold, p->alarm_high_threshold);
+       gtk_spin_button_set_value(w_low_threshold, p->alarm_low_threshold);
+       gtk_widget_set_sensitive(GTK_WIDGET(w_alarm), TRUE);
+       gtk_widget_set_sensitive(GTK_WIDGET(w_high_threshold), TRUE);
+       gtk_widget_set_sensitive(GTK_WIDGET(w_low_threshold), TRUE);
+
        gtk_toggle_button_set_active(w_appindicator_enabled,
                                     p->appindicator_enabled);
 
+       gtk_toggle_button_set_active(w_appindicator_label_enabled,
+                                    p->appindicator_label_enabled);
 }
 
 static void on_changed(GtkTreeSelection *selection, gpointer data)
@@ -329,37 +323,38 @@ static void on_changed(GtkTreeSelection *selection, gpointer data)
        struct cb_data *cbdata = data;
        struct ui_psensor *ui = cbdata->ui;
        struct sensor_pref *p;
+       GtkTreeView *tree;
 
-       p = get_selected_sensor_pref(cbdata->builder);
+       tree = GTK_TREE_VIEW(gtk_builder_get_object(cbdata->builder,
+                                                   "sensors_list"));
+       p = get_selected_sensor_pref(tree);
        update_pref(p, ui->config, cbdata->builder);
 }
 
 static void
 select_sensor(struct psensor *s, struct psensor **sensors, GtkTreeView *tree)
 {
-       struct psensor **s_cur = sensors;
-       int i = 0;
-       GtkTreePath *p = NULL;
+       struct psensor **s_cur;
+       int i;
+       GtkTreePath *p;
+       GtkTreeSelection *sel;
 
-       while (*s_cur) {
+       p = NULL;
+       for (s_cur = sensors, i = 0; *s_cur; s_cur++, i++)
                if (s == *s_cur) {
                        p = gtk_tree_path_new_from_indices(i, -1);
                        break;
                }
 
-               i++;
-               s_cur++;
-       }
-
        if (p) {
-               GtkTreeSelection *s = gtk_tree_view_get_selection(tree);
+               sel = gtk_tree_view_get_selection(tree);
 
-               gtk_tree_selection_select_path(s, p);
+               gtk_tree_selection_select_path(sel, p);
                gtk_tree_path_free(p);
        }
 }
 
-static void apply_pref(struct sensor_pref *p, struct config *cfg)
+static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
 {
        struct psensor *s;
 
@@ -371,17 +366,16 @@ static void apply_pref(struct sensor_pref *p, struct config *cfg)
                config_set_sensor_name(s->id, s->name);
        }
 
-       if (s->enabled != p->enabled) {
-               s->enabled = p->enabled;
-               config_set_sensor_enabled(s->id, s->enabled);
+       if (s->graph_enabled != p->graph_enabled) {
+               s->graph_enabled = p->graph_enabled;
+               config_set_sensor_graph_enabled(s->id, s->graph_enabled);
        }
 
-       if (is_temp_type(s->type)
-           && cfg->temperature_unit == FAHRENHEIT) {
+       if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
                s->alarm_high_threshold
-                       = fahrenheit_to_celcius(p->alarm_high_threshold);
+                       = fahrenheit_to_celsius(p->alarm_high_threshold);
                s->alarm_low_threshold
-                       = fahrenheit_to_celcius(p->alarm_low_threshold);
+                       = fahrenheit_to_celsius(p->alarm_low_threshold);
        } else {
                s->alarm_high_threshold = p->alarm_high_threshold;
                s->alarm_low_threshold = p->alarm_low_threshold;
@@ -390,45 +384,52 @@ static void apply_pref(struct sensor_pref *p, struct config *cfg)
        config_set_sensor_alarm_high_threshold(s->id, s->alarm_high_threshold);
        config_set_sensor_alarm_low_threshold(s->id, s->alarm_low_threshold);
 
-       if (s->alarm_enabled != p->alarm_enabled) {
-               s->alarm_enabled = p->alarm_enabled;
-               config_set_sensor_alarm_enabled(s->id, s->alarm_enabled);
-       }
+       config_set_sensor_alarm_enabled(s->id, p->alarm_enabled);
 
-       color_set(s->color, p->color->red, p->color->green, p->color->blue);
+       color_set(s->color,
+                 p->color->red,
+                 p->color->green,
+                 p->color->blue);
        config_set_sensor_color(s->id, s->color);
 
-       if (s->appindicator_enabled != p->appindicator_enabled) {
-               s->appindicator_enabled = p->appindicator_enabled;
-               config_set_appindicator_enabled(s->id, s->appindicator_enabled);
-       }
+       config_set_appindicator_enabled(s->id, p->appindicator_enabled);
+
+       config_set_appindicator_label_enabled(s->id,
+                                             p->appindicator_label_enabled);
+
+       config_set_sensor_position(s->id, pos);
+
+       config_set_sensor_enabled(s->id, p->display_enabled);
 }
 
-static void
-apply_prefs(GtkTreeModel *model, struct config *cfg)
+static void apply_prefs(GtkTreeModel *model, struct config *cfg)
 {
        gboolean valid;
        struct sensor_pref *spref;
        GtkTreeIter iter;
+       int i;
 
        valid = gtk_tree_model_get_iter_first(model, &iter);
+       i = 0;
        while (valid) {
-               gtk_tree_model_get(model, &iter, COL_SENSOR, &spref, -1);
-               apply_pref(spref, cfg);
+               gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
+               apply_pref(spref, i, cfg);
                valid = gtk_tree_model_iter_next(model, &iter);
+               i++;
        }
+       config_sync();
 }
 
 void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
 {
        GtkDialog *diag;
        gint result;
+       guint ok;
        GtkBuilder *builder;
-       GError *error = NULL;
+       GError *error;
        GtkTreeView *w_sensors_list;
-       guint ok;
        GtkListStore *store;
-       struct psensor **s_cur, *s;
+       struct psensor **s_cur, *s, **ordered_sensors;
        GtkTreeSelection *selection;
        struct cb_data cbdata;
        GtkTreeIter iter;
@@ -441,6 +442,7 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
        builder = gtk_builder_new();
        cbdata.builder = builder;
 
+       error = NULL;
        ok = gtk_builder_add_from_file
                (builder,
                 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "sensor-edit.glade",
@@ -449,26 +451,26 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
        if (!ok) {
                log_printf(LOG_ERR, error->message);
                g_error_free(error);
-               return ;
+               return;
        }
 
-       connect_signals(builder, &cbdata);
-
        w_sensors_list
                = GTK_TREE_VIEW(gtk_builder_get_object(builder,
                                                       "sensors_list"));
+       gtk_builder_connect_signals(builder, w_sensors_list);
 
        store = GTK_LIST_STORE(gtk_builder_get_object(builder,
                                                      "sensors_liststore"));
 
-       for (s_cur = ui->sensors; *s_cur; s_cur++) {
+       ordered_sensors = ui_get_sensors_ordered_by_position(ui);
+       for (s_cur = ordered_sensors; *s_cur; s_cur++) {
                s = *s_cur;
                gtk_list_store_append(store, &iter);
 
                spref = sensor_pref_new(s, ui->config);
                gtk_list_store_set(store, &iter,
                                   COL_NAME, s->name,
-                                  COL_SENSOR, spref,
+                                  COL_SENSOR_PREF, spref,
                                   -1);
 
                if (s == sensor)
@@ -477,7 +479,9 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
 
        selection = gtk_tree_view_get_selection(w_sensors_list);
        g_signal_connect(selection, "changed", G_CALLBACK(on_changed), &cbdata);
-       select_sensor(sensor, ui->sensors, w_sensors_list);
+       select_sensor(sensor, ordered_sensors, w_sensors_list);
+
+       free(ordered_sensors);
 
        diag = GTK_DIALOG(gtk_builder_get_object(builder, "dialog1"));
        result = gtk_dialog_run(diag);
@@ -487,14 +491,14 @@ void ui_sensorpref_dialog_run(struct psensor *sensor, struct ui_psensor *ui)
        if (result == GTK_RESPONSE_ACCEPT) {
                apply_prefs(model, ui->config);
                ui_sensorlist_update(ui, 1);
-#if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
+#if defined(HAVE_APPINDICATOR)
                ui_appindicator_update_menu(ui);
 #endif
        }
 
        valid = gtk_tree_model_get_iter_first(model, &iter);
        while (valid) {
-               gtk_tree_model_get(model, &iter, COL_SENSOR, &spref, -1);
+               gtk_tree_model_get(model, &iter, COL_SENSOR_PREF, &spref, -1);
                sensor_pref_free(spref);
                valid = gtk_tree_model_iter_next(model, &iter);
        }