removed debug traces
[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 static void update_label(struct ui_psensor *ui)
176 {
177         char *label, *str, *tmp, *guide;
178         struct psensor **p;
179
180         p =  ui_get_sensors_ordered_by_position(ui);
181         label = NULL;
182         guide = NULL;
183         while (*p) {
184                 if (config_is_appindicator_label_enabled((*p)->id)) {
185                         str = psensor_current_value_to_str
186                                 (*p, ui->config->temperature_unit == CELCIUS);
187
188                         if (label == NULL) {
189                                 label = str;
190                         } else {
191                                 tmp = malloc(strlen(label)
192                                              + 1
193                                              + strlen(str)
194                                              + 1);
195                                 sprintf(tmp, "%s %s", label, str);
196                                 free(label);
197                                 free(str);
198                                 label = tmp;
199                         }
200
201                         if (is_temp_type((*p)->type))
202                                 str = "999UUU";
203                         else if (is_fan_type((*p)->type))
204                                 str = "999UUU";
205                         else /* cpu load */
206                                 str = "999%";
207
208                         if (guide == NULL) {
209                                 guide = strdup(str);
210                         } else {
211                                 tmp = malloc(strlen(guide)
212                                              + 1
213                                              + strlen(str)
214                                              + 1);
215                                 sprintf(tmp, "%sW%s", guide, str);
216                                 free(guide);
217                                 guide = tmp;
218                         }
219
220                 }
221                 p++;
222         }
223
224         app_indicator_set_label(indicator, label, guide);
225 }
226
227 void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
228 {
229         AppIndicatorStatus status;
230
231         if (!indicator)
232                 return;
233
234         update_label(ui);
235
236         status = app_indicator_get_status(indicator);
237
238         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
239                 app_indicator_set_status(indicator,
240                                          APP_INDICATOR_STATUS_ACTIVE);
241
242         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
243                 app_indicator_set_status(indicator,
244                                          APP_INDICATOR_STATUS_ATTENTION);
245
246         update_menu_items(ui->config->temperature_unit == CELCIUS);
247 }
248
249 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
250 {
251         GtkStatusIcon *ico;
252
253         log_debug("ui_appindicator.unity_fallback()");
254
255         appindicator_supported = 0;
256
257         ico = ui_status_get_icon(ui_psensor);
258
259         ui_status_set_visible(1);
260
261         return ico;
262 }
263
264 static void
265 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
266 {
267         log_debug("ui_appindicator.unity_unfallback()");
268
269         ui_status_set_visible(0);
270
271         appindicator_supported = 1;
272 }
273
274 void ui_appindicator_update_menu(struct ui_psensor *ui)
275 {
276         GtkWidget *menu;
277
278         menu = get_menu(ui);
279         app_indicator_set_menu(indicator, GTK_MENU(menu));
280
281         gtk_widget_show_all(menu);
282 }
283
284 void ui_appindicator_init(struct ui_psensor *ui)
285 {
286         ui_psensor = ui;
287
288         indicator = app_indicator_new
289                 ("psensor",
290                  ICON,
291                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
292
293         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
294         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
295
296         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
297         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
298
299         ui_appindicator_update_menu(ui);
300 }
301
302 int is_appindicator_supported()
303 {
304         return appindicator_supported;
305 }
306
307 void ui_appindicator_cleanup()
308 {
309         free(sensors);
310 }