separate glade for appindicator to avoid attempt to bind callback which are not avail...
[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 bool appindicator_supported = true;
40 static AppIndicator *indicator;
41 static struct ui_psensor *ui_psensor;
42
43 void ui_appindicator_menu_show_cb(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         ui_pref_dialog_run((struct ui_psensor *)data);
51 }
52
53 void ui_appindicator_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
54 {
55         struct ui_psensor *ui = data;
56
57         if (ui->sensors && *ui->sensors)
58                 ui_sensorpref_dialog_run(*ui->sensors, ui);
59 }
60
61 static void
62 update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celsius)
63 {
64         gchar *str;
65         char *v;
66
67         v = psensor_current_value_to_str(s, use_celsius);
68
69         str = g_strdup_printf("%s: %s", s->name, v);
70
71         gtk_menu_item_set_label(item, str);
72
73         free(v);
74         g_free(str);
75 }
76
77 static void update_menu_items(int use_celsius)
78 {
79         struct psensor **s;
80         GtkMenuItem **m;
81
82         if (!sensors)
83                 return;
84
85         for (s = sensors, m = menu_items; *s; s++, m++)
86                 update_menu_item(*m, *s, use_celsius);
87 }
88
89 static void
90 build_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu)
91 {
92         int i, j, n, celsius;
93         const char *name;
94         struct psensor **sorted_sensors;
95
96         free(menu_items);
97
98         celsius  = ui->config->temperature_unit == CELSIUS;
99
100         sorted_sensors = ui_get_sensors_ordered_by_position(ui);
101         n = psensor_list_size(sorted_sensors);
102         menu_items = malloc(n * sizeof(GtkWidget *));
103         sensors = malloc((n + 1) * sizeof(struct psensor *));
104         for (i = 0, j = 0; i < n; i++) {
105                 if (config_is_appindicator_enabled(sorted_sensors[i]->id)) {
106                         sensors[j] = sorted_sensors[i];
107                         name = sensors[j]->name;
108
109                         menu_items[j] = GTK_MENU_ITEM
110                                 (gtk_menu_item_new_with_label(name));
111
112                         gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
113                                               GTK_WIDGET(menu_items[j]),
114                                               j+2);
115
116                         update_menu_item(menu_items[j], sensors[j], celsius);
117
118                         j++;
119                 }
120         }
121
122         sensors[j] = NULL;
123
124         free(sorted_sensors);
125 }
126
127 static GtkWidget *get_menu(struct ui_psensor *ui)
128 {
129         GError *error;
130         GtkMenu *menu;
131         guint ok;
132         GtkBuilder *builder;
133
134         log_fct_enter();
135
136         builder = gtk_builder_new();
137
138         error = NULL;
139         ok = gtk_builder_add_from_file
140         (builder,
141          PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade",
142          &error);
143
144         if (!ok) {
145                 log_err(_("Failed to load glade file %s: %s"),
146                         PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade",
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         build_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 GTK_WIDGET(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, unsigned int 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 void ui_appindicator_update_menu(struct ui_psensor *ui)
264 {
265         GtkWidget *menu;
266
267         menu = get_menu(ui);
268         app_indicator_set_menu(indicator, GTK_MENU(menu));
269
270         gtk_widget_show_all(menu);
271 }
272
273 void ui_appindicator_init(struct ui_psensor *ui)
274 {
275         ui_psensor = ui;
276
277         indicator = app_indicator_new
278                 ("psensor",
279                  ICON,
280                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
281
282         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
283         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
284
285         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
286         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
287
288         ui_appindicator_update_menu(ui);
289 }
290
291 bool is_appindicator_supported(void)
292 {
293         return appindicator_supported;
294 }
295
296 void ui_appindicator_cleanup(void)
297 {
298         free(sensors);
299 }