support of fahrenheit for the indicator label
[psensor.git] / src / ui_appindicator.c
1 /*
2  * Copyright (C) 2010-2014 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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <gtk/gtk.h>
24 #include <libappindicator/app-indicator.h>
25
26 #include "cfg.h"
27 #include "psensor.h"
28 #include "ui.h"
29 #include "ui_appindicator.h"
30 #include "ui_sensorpref.h"
31 #include "ui_status.h"
32 #include "ui_pref.h"
33
34 static const char *ICON = "psensor_normal";
35 static const char *ATTENTION_ICON = "psensor_hot";
36
37 static struct psensor **sensors;
38 static GtkMenuItem **menu_items;
39 static int appindicator_supported = 1;
40 static AppIndicator *indicator;
41 static struct ui_psensor *ui_psensor;
42
43 void cb_menu_show(GtkMenuItem *mi, gpointer data)
44 {
45         ui_window_show((struct ui_psensor *)data);
46 }
47
48 void ui_appindicator_cb_preferences(GtkMenuItem *mi, gpointer data)
49 {
50 #ifdef HAVE_APPINDICATOR_029
51         gdk_threads_enter();
52 #endif
53
54         ui_pref_dialog_run((struct ui_psensor *)data);
55
56 #ifdef HAVE_APPINDICATOR_029
57         gdk_threads_leave();
58 #endif
59 }
60
61 void ui_appindicator_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
62 {
63         struct ui_psensor *ui = data;
64
65 #ifdef HAVE_APPINDICATOR_029
66         gdk_threads_enter();
67 #endif
68
69         if (ui->sensors && *ui->sensors)
70                 ui_sensorpref_dialog_run(*ui->sensors, ui);
71
72 #ifdef HAVE_APPINDICATOR_029
73         gdk_threads_leave();
74 #endif
75 }
76
77 static void
78 update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celcius)
79 {
80         gchar *str;
81         char *v;
82
83         v = psensor_current_value_to_str(s, use_celcius);
84
85         str = g_strdup_printf("%s: %s", s->name, v);
86
87         gtk_menu_item_set_label(item, str);
88
89         free(v);
90         g_free(str);
91 }
92
93 static void update_menu_items(int use_celcius)
94 {
95         struct psensor **s;
96         GtkMenuItem **m;
97
98         if (!sensors)
99                 return ;
100
101         for (s = sensors, m = menu_items; *s; s++, m++)
102                 update_menu_item(*m, *s, use_celcius);
103 }
104
105 static void
106 build_sensor_menu_items(const struct ui_psensor *ui,
107                         GtkMenu *menu)
108 {
109         int i, j, n, celcius;
110         const char *name;
111         struct psensor **sorted_sensors;
112
113         free(menu_items);
114
115         celcius  = ui->config->temperature_unit == CELCIUS;
116
117         sorted_sensors = ui_get_sensors_ordered_by_position(ui);
118         n = psensor_list_size(sorted_sensors);
119         menu_items = malloc(n * sizeof(GtkWidget *));
120         sensors = malloc((n + 1) * sizeof(struct psensor *));
121         for (i = 0, j = 0; i < n; i++) {
122                 if (config_is_appindicator_enabled(sorted_sensors[i]->id)) {
123                         sensors[j] = sorted_sensors[i];
124                         name = sensors[j]->name;
125
126                         menu_items[j] = GTK_MENU_ITEM
127                                 (gtk_menu_item_new_with_label(name));
128
129                         gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
130                                               GTK_WIDGET(menu_items[j]),
131                                               j+2);
132
133                         update_menu_item(menu_items[j], sensors[j], celcius);
134
135                         j++;
136                 }
137         }
138
139         sensors[j] = NULL;
140
141         free(sorted_sensors);
142 }
143
144 static GtkWidget *get_menu(struct ui_psensor *ui)
145 {
146         GError *error;
147         GtkMenu *menu;
148         guint ok;
149         GtkBuilder *builder;
150
151         builder = gtk_builder_new();
152
153         error = NULL;
154         ok = gtk_builder_add_from_file
155                 (builder,
156                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
157                  &error);
158
159         if (!ok) {
160                 log_printf(LOG_ERR, error->message);
161                 g_error_free(error);
162                 return NULL;
163         }
164
165         menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu"));
166         build_sensor_menu_items(ui, menu);
167         gtk_builder_connect_signals(builder, ui);
168
169         g_object_ref(G_OBJECT(menu));
170         g_object_unref(G_OBJECT(builder));
171
172         return GTK_WIDGET(menu);
173 }
174
175 void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
176 {
177         AppIndicatorStatus status;
178         char *label, *str, *tmp;
179         struct psensor **p;
180
181         if (!indicator)
182                 return;
183
184         status = app_indicator_get_status(indicator);
185
186         p =  ui_get_sensors_ordered_by_position(ui);
187         label = NULL;
188         while (*p) {
189                 if (config_is_appindicator_label_enabled((*p)->id)) {
190                         str = psensor_current_value_to_str
191                                 (*p, ui->config->temperature_unit == CELCIUS);
192                         if (label == NULL) {
193                                 label = str;
194                         } else {
195                                 tmp = malloc(strlen(label)
196                                              + 1
197                                              + strlen(str)
198                                              + 1);
199                                 sprintf(tmp, "%s %s", label, str);
200                                 free(label);
201                                 free(str);
202                                 label = tmp;
203                         }
204                 }
205                 p++;
206         }
207
208         app_indicator_set_label(indicator, label, NULL);
209
210         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
211                 app_indicator_set_status(indicator,
212                                          APP_INDICATOR_STATUS_ACTIVE);
213
214         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
215                 app_indicator_set_status(indicator,
216                                          APP_INDICATOR_STATUS_ATTENTION);
217
218         update_menu_items(ui->config->temperature_unit == CELCIUS);
219 }
220
221 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
222 {
223         GtkStatusIcon *ico;
224
225         log_debug("ui_appindicator.unity_fallback()");
226
227         appindicator_supported = 0;
228
229         ico = ui_status_get_icon(ui_psensor);
230
231         ui_status_set_visible(1);
232
233         return ico;
234 }
235
236 static void
237 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
238 {
239         log_debug("ui_appindicator.unity_unfallback()");
240
241         ui_status_set_visible(0);
242
243         appindicator_supported = 1;
244 }
245
246 void ui_appindicator_update_menu(struct ui_psensor *ui)
247 {
248         GtkWidget *menu;
249
250         menu = get_menu(ui);
251         app_indicator_set_menu(indicator, GTK_MENU(menu));
252
253         gtk_widget_show_all(menu);
254 }
255
256 void ui_appindicator_init(struct ui_psensor *ui)
257 {
258         ui_psensor = ui;
259
260         indicator = app_indicator_new
261                 ("psensor",
262                  ICON,
263                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
264
265         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
266         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
267
268         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
269         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
270
271         ui_appindicator_update_menu(ui);
272 }
273
274 int is_appindicator_supported()
275 {
276         return appindicator_supported;
277 }
278
279 void ui_appindicator_cleanup()
280 {
281         free(sensors);
282 }