Imported Upstream version 1.2.0
[psensor-pkg-debian.git] / src / ui_appindicator.c
1 /*
2  * Copyright (C) 2010-2016 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 const char *GLADE_FILE
38 = PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade";
39
40 static struct psensor **sensors;
41 static GtkMenuItem **menu_items;
42 static bool appindicator_supported = true;
43 static AppIndicator *indicator;
44 static struct ui_psensor *ui_psensor;
45
46 void ui_appindicator_menu_show_cb(GtkMenuItem *mi, gpointer data)
47 {
48         ui_window_show((struct ui_psensor *)data);
49 }
50
51 void ui_appindicator_cb_preferences(GtkMenuItem *mi, gpointer data)
52 {
53         ui_pref_dialog_run((struct ui_psensor *)data);
54 }
55
56 void ui_appindicator_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
57 {
58         struct ui_psensor *ui = data;
59
60         if (ui->sensors && *ui->sensors)
61                 ui_sensorpref_dialog_run(*ui->sensors, ui);
62 }
63
64 static void
65 update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celsius)
66 {
67         gchar *str;
68         char *v;
69
70         v = psensor_current_value_to_str(s, use_celsius);
71
72         str = g_strdup_printf("%s: %s", s->name, v);
73
74         gtk_menu_item_set_label(item, str);
75
76         free(v);
77         g_free(str);
78 }
79
80 static void update_menu_items(int use_celsius)
81 {
82         struct psensor **s;
83         GtkMenuItem **m;
84
85         if (!sensors)
86                 return;
87
88         for (s = sensors, m = menu_items; *s; s++, m++)
89                 update_menu_item(*m, *s, use_celsius);
90 }
91
92 static void
93 create_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu)
94 {
95         int i, j, n, celsius;
96         const char *name;
97         struct psensor **sorted_sensors;
98
99         if (config_get_temperature_unit() == CELSIUS)
100                 celsius = 1;
101         else
102                 celsius = 0;
103
104         sorted_sensors = ui_get_sensors_ordered_by_position(ui->sensors);
105         n = psensor_list_size(sorted_sensors);
106         menu_items = malloc((n + 1) * sizeof(GtkWidget *));
107
108         sensors = malloc((n + 1) * sizeof(struct psensor *));
109         for (i = 0, j = 0; i < n; i++) {
110                 if (config_is_appindicator_enabled(sorted_sensors[i]->id)) {
111                         sensors[j] = sorted_sensors[i];
112                         name = sensors[j]->name;
113
114                         menu_items[j] = GTK_MENU_ITEM
115                                 (gtk_menu_item_new_with_label(name));
116
117                         gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
118                                               GTK_WIDGET(menu_items[j]),
119                                               j+2);
120
121                         update_menu_item(menu_items[j], sensors[j], celsius);
122
123                         j++;
124                 }
125         }
126
127         sensors[j] = NULL;
128         menu_items[j] = NULL;
129
130         free(sorted_sensors);
131 }
132
133 static GtkMenu *load_menu(struct ui_psensor *ui)
134 {
135         GError *error;
136         GtkMenu *menu;
137         guint ok;
138         GtkBuilder *builder;
139
140         log_fct_enter();
141
142         builder = gtk_builder_new();
143
144         error = NULL;
145         ok = gtk_builder_add_from_file(builder, GLADE_FILE, &error);
146
147         if (!ok) {
148                 log_err(_("Failed to load glade file %s: %s"),
149                         GLADE_FILE,
150                         error->message);
151                 g_error_free(error);
152                 return NULL;
153         }
154
155         menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu"));
156         create_sensor_menu_items(ui, menu);
157         gtk_builder_connect_signals(builder, ui);
158
159         g_object_ref(G_OBJECT(menu));
160         g_object_unref(G_OBJECT(builder));
161
162         log_fct_exit();
163
164         return menu;
165 }
166
167 static void update_label(struct ui_psensor *ui)
168 {
169         char *label, *str, *tmp, *guide;
170         struct psensor **p;
171         int use_celsius;
172
173         p =  ui_get_sensors_ordered_by_position(ui->sensors);
174         label = NULL;
175         guide = NULL;
176
177         if (config_get_temperature_unit() == CELSIUS)
178                 use_celsius = 1;
179         else
180                 use_celsius = 0;
181
182         while (*p) {
183                 if (config_is_appindicator_label_enabled((*p)->id)) {
184                         str = psensor_current_value_to_str(*p, use_celsius);
185
186                         if (label == NULL) {
187                                 label = str;
188                         } else {
189                                 tmp = malloc(strlen(label)
190                                              + 1
191                                              + strlen(str)
192                                              + 1);
193                                 sprintf(tmp, "%s %s", label, str);
194                                 free(label);
195                                 free(str);
196                                 label = tmp;
197                         }
198
199                         if (is_temp_type((*p)->type))
200                                 str = "999UUU";
201                         else if ((*p)->type & SENSOR_TYPE_RPM)
202                                 str = "999UUU";
203                         else /* percent */
204                                 str = "999%";
205
206                         if (guide == NULL) {
207                                 guide = strdup(str);
208                         } else {
209                                 tmp = malloc(strlen(guide)
210                                              + 1
211                                              + strlen(str)
212                                              + 1);
213                                 sprintf(tmp, "%sW%s", guide, str);
214                                 free(guide);
215                                 guide = tmp;
216                         }
217
218                 }
219                 p++;
220         }
221
222         app_indicator_set_label(indicator, label, guide);
223 }
224
225 void ui_appindicator_update(struct ui_psensor *ui, bool attention)
226 {
227         AppIndicatorStatus status;
228
229         if (!indicator)
230                 return;
231
232         update_label(ui);
233
234         status = app_indicator_get_status(indicator);
235
236         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
237                 app_indicator_set_status(indicator,
238                                          APP_INDICATOR_STATUS_ACTIVE);
239
240         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
241                 app_indicator_set_status(indicator,
242                 APP_INDICATOR_STATUS_ATTENTION);
243
244         if (config_get_temperature_unit() == CELSIUS)
245                 update_menu_items(1);
246         else
247                 update_menu_items(0);
248 }
249
250 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
251 {
252         GtkStatusIcon *ico;
253
254         log_debug("ui_appindicator.unity_fallback()");
255
256         appindicator_supported = false;
257
258         ico = ui_status_get_icon(ui_psensor);
259
260         ui_status_set_visible(1);
261
262         return ico;
263 }
264
265 static void
266 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
267 {
268         log_debug("ui_appindicator.unity_unfallback()");
269
270         ui_status_set_visible(0);
271
272         appindicator_supported = true;
273 }
274
275 static void remove_sensor_menu_items(GtkMenu *menu)
276 {
277         GtkMenuItem **items;
278
279         if (!menu_items)
280                 return;
281
282         items = menu_items;
283         while (*items) {
284                 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(*items));
285
286                 items++;
287         }
288
289         free(menu_items);
290         free(sensors);
291 }
292
293 void ui_appindicator_update_menu(struct ui_psensor *ui)
294 {
295         GtkMenu *menu;
296
297         menu = GTK_MENU(app_indicator_get_menu(indicator));
298
299         if (menu) {
300                 remove_sensor_menu_items(menu);
301                 create_sensor_menu_items(ui, menu);
302         } else {
303                 menu = load_menu(ui);
304
305                 if (menu) {
306                         app_indicator_set_menu(indicator, menu);
307                         g_object_unref(G_OBJECT(menu));
308                 }
309         }
310
311         if (menu)
312                 gtk_widget_show_all(GTK_WIDGET(menu));
313 }
314
315 void ui_appindicator_init(struct ui_psensor *ui)
316 {
317         ui_psensor = ui;
318
319         indicator = app_indicator_new
320                 ("psensor",
321                  ICON,
322                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
323
324         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
325         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
326
327         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
328         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
329
330         ui_appindicator_update_menu(ui);
331 }
332
333 bool is_appindicator_supported(void)
334 {
335         return appindicator_supported;
336 }
337
338 void ui_appindicator_cleanup(void)
339 {
340         free(sensors);
341 }