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