Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / ui_sensorlist.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "ui.h"
23 #include "ui_pref.h"
24 #include "ui_sensorlist.h"
25 #include "ui_sensorpref.h"
26 #include "cfg.h"
27 #include "ui_color.h"
28 #include "compat.h"
29
30 enum {
31         COL_NAME = 0,
32         COL_TEMP,
33         COL_TEMP_MIN,
34         COL_TEMP_MAX,
35         COL_COLOR,
36         COL_COLOR_STR,
37         COL_ENABLED,
38         COL_EMPTY,
39         COLS_COUNT
40 };
41
42 struct cb_data {
43         struct ui_psensor *ui;
44         struct psensor *sensor;
45 };
46
47 static int col_index_to_col(int idx)
48 {
49         if (idx == 5)
50                 return COL_ENABLED;
51         else if (idx > 5)
52                 return -1;
53
54         return idx;
55 }
56
57 void ui_sensorlist_update(struct ui_psensor *ui)
58 {
59         GtkTreeIter iter;
60         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
61         GtkTreeModel *model
62             = gtk_tree_view_get_model(ui_sl->treeview);
63         gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
64         struct psensor **sensor = ui->sensors;
65
66         while (valid && *sensor) {
67                 struct psensor *s = *sensor;
68
69                 char *str;
70
71                 str = psensor_value_to_string(s->type,
72                                               s->measures[s->values_max_length -
73                                                           1].value.d_num);
74
75                 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
76                                    -1);
77                 free(str);
78
79                 str = psensor_value_to_string(s->type, s->min);
80                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
81                                    COL_TEMP_MIN, str, -1);
82                 free(str);
83
84                 str = psensor_value_to_string(s->type, s->max);
85                 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
86                                    COL_TEMP_MAX, str, -1);
87                 free(str);
88
89                 valid = gtk_tree_model_iter_next(model, &iter);
90                 sensor++;
91         }
92 }
93
94 /*
95  * Returns the sensor corresponding to the x/y position
96  * in the table.
97  *
98  * <null> if none.
99  */
100 static struct psensor *
101 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
102 {
103         GtkTreePath *path;
104
105         gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
106
107         if (path) {
108                 gint *i = gtk_tree_path_get_indices(path);
109                 if (i)
110                         return *(sensors + *i);
111         }
112         return NULL;
113 }
114
115 /*
116  * Returns the index of the column corresponding
117  * to the x position in the table.
118  *
119  * -1 if none
120  */
121 static int get_col_index_at_pos(GtkTreeView *view, int x)
122 {
123         GList *columns = gtk_tree_view_get_columns(view);
124         GList *node;
125         int colx = 0;
126         int coli = 0;
127
128         for (node = columns; node; node = node->next) {
129                 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
130
131                 if (x >= colx &&
132                     x < (colx + gtk_tree_view_column_get_width(checkcol)))
133                         return coli;
134                 else
135                         colx += gtk_tree_view_column_get_width(checkcol);
136
137                 coli++;
138         }
139
140         return -1;
141 }
142
143 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
144 {
145         GtkTreeIter iter;
146         GtkTreeModel *model
147             = gtk_tree_view_get_model(ui->ui_sensorlist->treeview);
148         gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
149         struct psensor **sensor = ui->ui_sensorlist->sensors;
150
151         while (valid && *sensor) {
152                 GdkColor color;
153                 gchar *scolor;
154
155                 color.red = (*sensor)->color->red;
156                 color.green = (*sensor)->color->green;
157                 color.blue = (*sensor)->color->blue;
158
159                 scolor = gdk_color_to_string(&color);
160
161                 gtk_list_store_set(GTK_LIST_STORE(model),
162                                    &iter, COL_NAME, (*sensor)->name, -1);
163
164                 gtk_list_store_set(GTK_LIST_STORE(model),
165                                    &iter, COL_COLOR_STR, scolor, -1);
166
167                 gtk_list_store_set(GTK_LIST_STORE(model),
168                                    &iter, COL_ENABLED, (*sensor)->enabled, -1);
169
170                 free(scolor);
171
172                 valid = gtk_tree_model_iter_next(model, &iter);
173                 sensor++;
174         }
175 }
176
177 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
178 {
179         struct cb_data *cb_data = data;
180
181         ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
182 }
183
184 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
185                                       struct psensor *sensor)
186 {
187         GtkWidget *menu;
188         GtkWidget *item;
189         GtkWidget *separator;
190         struct cb_data *data;
191
192         menu = gtk_menu_new();
193
194         item = gtk_menu_item_new_with_label(sensor->name);
195         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
196
197         separator = gtk_separator_menu_item_new();
198         gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
199
200         item = gtk_menu_item_new_with_label(_("Preferences"));
201         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
202
203         data = malloc(sizeof(struct cb_data));
204         data->ui = ui;
205         data->sensor = sensor;
206
207         g_signal_connect(item,
208                          "activate",
209                          G_CALLBACK(on_preferences_activated), data);
210
211         gtk_widget_show_all(menu);
212
213         return menu;
214 }
215
216 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
217 {
218         struct ui_psensor *ui = (struct ui_psensor *)data;
219         GtkTreeView *view = ui->ui_sensorlist->treeview;
220
221         struct psensor *sensor = get_sensor_at_pos(view,
222                                                    event->x,
223                                                    event->y,
224                                                    ui->sensors);
225
226         if (sensor) {
227                 int coli = col_index_to_col(get_col_index_at_pos(view,
228                                                                  event->x));
229
230                 if (coli == COL_COLOR) {
231                         if (ui_change_color(_("Select foreground color"),
232                                             sensor->color)) {
233                                 ui_sensorlist_update_sensors_preferences(ui);
234                                 config_set_sensor_color(sensor->id,
235                                                         sensor->color);
236                         }
237                 } else if (coli >= 0 && coli != COL_ENABLED) {
238                         GtkWidget *menu = create_sensor_popup(ui,
239                                                               sensor);
240
241                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
242                                        event->button, event->time);
243
244                 }
245
246         }
247         return FALSE;
248 }
249
250 static void
251 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
252 {
253         struct ui_sensorlist *list = (struct ui_sensorlist *)data;
254         GtkTreeModel *model
255             = gtk_tree_view_get_model(list->treeview);
256         GtkTreeIter iter;
257         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
258         gboolean fixed;
259         gint *i;
260
261         gtk_tree_model_get_iter(model, &iter, path);
262         gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
263
264         fixed ^= 1;
265
266         i = gtk_tree_path_get_indices(path);
267         if (i) {
268                 int n = *i;
269                 struct psensor **sensor = list->sensors;
270                 while (n--)
271                         sensor++;
272                 (*sensor)->enabled = fixed;
273                 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
274         }
275
276         gtk_list_store_set(GTK_LIST_STORE(model),
277                            &iter, COL_ENABLED, fixed, -1);
278
279         gtk_tree_path_free(path);
280 }
281
282 static void create_widget(struct ui_psensor *ui)
283 {
284         GtkListStore *store;
285         GtkCellRenderer *renderer;
286         struct psensor **s_cur = ui->sensors;
287         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
288
289         store = gtk_list_store_new(COLS_COUNT,
290                                    G_TYPE_STRING,
291                                    G_TYPE_STRING,
292                                    G_TYPE_STRING,
293                                    G_TYPE_STRING,
294                                    G_TYPE_STRING,
295                                    G_TYPE_STRING,
296                                    G_TYPE_BOOLEAN, G_TYPE_STRING);
297
298         ui_sl->treeview = GTK_TREE_VIEW
299                 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
300
301         gtk_tree_selection_set_mode
302                 (gtk_tree_view_get_selection(ui_sl->treeview),
303                  GTK_SELECTION_NONE);
304
305         renderer = gtk_cell_renderer_text_new();
306         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
307                                                     -1,
308                                                     _("Sensor"),
309                                                     renderer,
310                                                     "text", COL_NAME, NULL);
311
312         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
313                                                     -1,
314                                                     _("Value"),
315                                                     renderer,
316                                                     "text", COL_TEMP, NULL);
317
318         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
319                                                     -1,
320                                                     _("Min"),
321                                                     renderer,
322                                                     "text", COL_TEMP_MIN, NULL);
323
324         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
325                                                     -1,
326                                                     _("Max"),
327                                                     renderer,
328                                                     "text", COL_TEMP_MAX, NULL);
329
330         renderer = gtk_cell_renderer_text_new();
331         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
332                                                     -1,
333                                                     _("Color"),
334                                                     renderer,
335                                                     "text", COL_COLOR,
336                                                     "background", COL_COLOR_STR,
337                                                     NULL);
338
339         g_signal_connect(ui_sl->treeview,
340                          "button-press-event", (GCallback) on_clicked, ui);
341
342         renderer = gtk_cell_renderer_toggle_new();
343         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
344                                                     -1,
345                                                     _("Enabled"),
346                                                     renderer,
347                                                     "active", COL_ENABLED,
348                                                     NULL);
349         g_signal_connect(G_OBJECT(renderer),
350                          "toggled", (GCallback) on_toggled, ui_sl);
351
352         renderer = gtk_cell_renderer_text_new();
353         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
354                                                     -1,
355                                                     "",
356                                                     renderer,
357                                                     "text", COL_EMPTY, NULL);
358
359         while (*s_cur) {
360                 GtkTreeIter iter;
361                 GdkColor color;
362                 gchar *scolor;
363                 struct psensor *s = *s_cur;
364
365                 color.red = s->color->red;
366                 color.green = s->color->green;
367                 color.blue = s->color->blue;
368
369                 scolor = gdk_color_to_string(&color);
370
371                 gtk_list_store_append(store, &iter);
372                 gtk_list_store_set(store, &iter,
373                                    COL_NAME, s->name,
374                                    COL_TEMP, _("N/A"),
375                                    COL_TEMP_MIN, _("N/A"),
376                                    COL_TEMP_MAX, _("N/A"),
377                                    COL_COLOR_STR, scolor,
378                                    COL_ENABLED, s->enabled, -1);
379
380                 free(scolor);
381
382                 s_cur++;
383         }
384
385         ui_sl->widget = gtk_scrolled_window_new(NULL, NULL);
386         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui_sl->widget),
387                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
388         gtk_container_add(GTK_CONTAINER(ui_sl->widget),
389                           GTK_WIDGET(ui_sl->treeview));
390 }
391
392 void ui_sensorlist_create(struct ui_psensor *ui)
393 {
394         ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
395         ui->ui_sensorlist->sensors = ui->sensors;
396
397         create_widget(ui);
398 }