merged from plib
[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         printf("%s\n", label);
224         printf("%s\n", guide);
225
226         app_indicator_set_label(indicator, label, guide);
227 }
228
229 void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
230 {
231         AppIndicatorStatus status;
232
233         if (!indicator)
234                 return;
235
236         update_label(ui);
237
238         status = app_indicator_get_status(indicator);
239
240         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
241                 app_indicator_set_status(indicator,
242                                          APP_INDICATOR_STATUS_ACTIVE);
243
244         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
245                 app_indicator_set_status(indicator,
246                                          APP_INDICATOR_STATUS_ATTENTION);
247
248         update_menu_items(ui->config->temperature_unit == CELCIUS);
249 }
250
251 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
252 {
253         GtkStatusIcon *ico;
254
255         log_debug("ui_appindicator.unity_fallback()");
256
257         appindicator_supported = 0;
258
259         ico = ui_status_get_icon(ui_psensor);
260
261         ui_status_set_visible(1);
262
263         return ico;
264 }
265
266 static void
267 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
268 {
269         log_debug("ui_appindicator.unity_unfallback()");
270
271         ui_status_set_visible(0);
272
273         appindicator_supported = 1;
274 }
275
276 void ui_appindicator_update_menu(struct ui_psensor *ui)
277 {
278         GtkWidget *menu;
279
280         menu = get_menu(ui);
281         app_indicator_set_menu(indicator, GTK_MENU(menu));
282
283         gtk_widget_show_all(menu);
284 }
285
286 void ui_appindicator_init(struct ui_psensor *ui)
287 {
288         ui_psensor = ui;
289
290         indicator = app_indicator_new
291                 ("psensor",
292                  ICON,
293                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
294
295         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
296         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
297
298         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
299         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
300
301         ui_appindicator_update_menu(ui);
302 }
303
304 int is_appindicator_supported()
305 {
306         return appindicator_supported;
307 }
308
309 void ui_appindicator_cleanup()
310 {
311         free(sensors);
312 }