fixed memory leaks in appindicator support when sensor preferences are changed.
[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 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         celsius  = ui->config->temperature_unit == CELSIUS;
100
101         sorted_sensors = ui_get_sensors_ordered_by_position(ui);
102         n = psensor_list_size(sorted_sensors);
103         menu_items = malloc((n + 1) * sizeof(GtkWidget *));
104
105         sensors = malloc((n + 1) * sizeof(struct psensor *));
106         for (i = 0, j = 0; i < n; i++) {
107                 if (config_is_appindicator_enabled(sorted_sensors[i]->id)) {
108                         sensors[j] = sorted_sensors[i];
109                         name = sensors[j]->name;
110
111                         menu_items[j] = GTK_MENU_ITEM
112                                 (gtk_menu_item_new_with_label(name));
113
114                         gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
115                                               GTK_WIDGET(menu_items[j]),
116                                               j+2);
117
118                         update_menu_item(menu_items[j], sensors[j], celsius);
119
120                         j++;
121                 }
122         }
123
124         sensors[j] = NULL;
125         menu_items[j] = NULL;
126
127         free(sorted_sensors);
128 }
129
130 static GtkMenu *load_menu(struct ui_psensor *ui)
131 {
132         GError *error;
133         GtkMenu *menu;
134         guint ok;
135         GtkBuilder *builder;
136
137         log_fct_enter();
138
139         builder = gtk_builder_new();
140
141         error = NULL;
142         ok = gtk_builder_add_from_file(builder, GLADE_FILE, &error);
143
144         if (!ok) {
145                 log_err(_("Failed to load glade file %s: %s"),
146                         GLADE_FILE,
147                         error->message);
148                 g_error_free(error);
149                 return NULL;
150         }
151
152         menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu"));
153         create_sensor_menu_items(ui, menu);
154         gtk_builder_connect_signals(builder, ui);
155
156         g_object_ref(G_OBJECT(menu));
157         g_object_unref(G_OBJECT(builder));
158
159         log_fct_exit();
160
161         return menu;
162 }
163
164 static void update_label(struct ui_psensor *ui)
165 {
166         char *label, *str, *tmp, *guide;
167         struct psensor **p;
168
169         p =  ui_get_sensors_ordered_by_position(ui);
170         label = NULL;
171         guide = NULL;
172         while (*p) {
173                 if (config_is_appindicator_label_enabled((*p)->id)) {
174                         str = psensor_current_value_to_str
175                                 (*p, ui->config->temperature_unit == CELSIUS);
176
177                         if (label == NULL) {
178                                 label = str;
179                         } else {
180                                 tmp = malloc(strlen(label)
181                                              + 1
182                                              + strlen(str)
183                                              + 1);
184                                 sprintf(tmp, "%s %s", label, str);
185                                 free(label);
186                                 free(str);
187                                 label = tmp;
188                         }
189
190                         if (is_temp_type((*p)->type))
191                                 str = "999UUU";
192                         else if ((*p)->type & SENSOR_TYPE_RPM)
193                                 str = "999UUU";
194                         else /* percent */
195                                 str = "999%";
196
197                         if (guide == NULL) {
198                                 guide = strdup(str);
199                         } else {
200                                 tmp = malloc(strlen(guide)
201                                              + 1
202                                              + strlen(str)
203                                              + 1);
204                                 sprintf(tmp, "%sW%s", guide, str);
205                                 free(guide);
206                                 guide = tmp;
207                         }
208
209                 }
210                 p++;
211         }
212
213         app_indicator_set_label(indicator, label, guide);
214 }
215
216 void ui_appindicator_update(struct ui_psensor *ui, bool attention)
217 {
218         AppIndicatorStatus status;
219
220         if (!indicator)
221                 return;
222
223         update_label(ui);
224
225         status = app_indicator_get_status(indicator);
226
227         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
228                 app_indicator_set_status(indicator,
229                                          APP_INDICATOR_STATUS_ACTIVE);
230
231         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
232                 app_indicator_set_status(indicator,
233                 APP_INDICATOR_STATUS_ATTENTION);
234
235         update_menu_items(ui->config->temperature_unit == CELSIUS);
236 }
237
238 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
239 {
240         GtkStatusIcon *ico;
241
242         log_debug("ui_appindicator.unity_fallback()");
243
244         appindicator_supported = false;
245
246         ico = ui_status_get_icon(ui_psensor);
247
248         ui_status_set_visible(1);
249
250         return ico;
251 }
252
253 static void
254 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
255 {
256         log_debug("ui_appindicator.unity_unfallback()");
257
258         ui_status_set_visible(0);
259
260         appindicator_supported = true;
261 }
262
263 static void remove_sensor_menu_items(GtkMenu *menu)
264 {
265         GtkMenuItem **items;
266
267         if (!menu_items)
268                 return;
269
270         items = menu_items;
271         while (*items) {
272                 gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(*items));
273
274                 items++;
275         }
276
277         free(menu_items);
278         free(sensors);
279 }
280
281 void ui_appindicator_update_menu(struct ui_psensor *ui)
282 {
283         GtkMenu *menu;
284
285         menu = GTK_MENU(app_indicator_get_menu(indicator));
286
287         if (menu) {
288                 remove_sensor_menu_items(menu);
289                 create_sensor_menu_items(ui, menu);
290         } else {
291                 menu = load_menu(ui);
292
293                 if (menu) {
294                         app_indicator_set_menu(indicator, menu);
295                         g_object_unref(G_OBJECT(menu));
296                 }
297         }
298
299         if (menu)
300                 gtk_widget_show_all(GTK_WIDGET(menu));
301 }
302
303 void ui_appindicator_init(struct ui_psensor *ui)
304 {
305         ui_psensor = ui;
306
307         indicator = app_indicator_new
308                 ("psensor",
309                  ICON,
310                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
311
312         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
313         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
314
315         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
316         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
317
318         ui_appindicator_update_menu(ui);
319 }
320
321 bool is_appindicator_supported(void)
322 {
323         return appindicator_supported;
324 }
325
326 void ui_appindicator_cleanup(void)
327 {
328         free(sensors);
329 }