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