ANSI decl
[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         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         builder = gtk_builder_new();
135
136         error = NULL;
137         ok = gtk_builder_add_from_file
138                 (builder,
139                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
140                  &error);
141
142         if (!ok) {
143                 log_printf(LOG_ERR, error->message);
144                 g_error_free(error);
145                 return NULL;
146         }
147
148         menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu"));
149         build_sensor_menu_items(ui, menu);
150         gtk_builder_connect_signals(builder, ui);
151
152         g_object_ref(G_OBJECT(menu));
153         g_object_unref(G_OBJECT(builder));
154
155         return GTK_WIDGET(menu);
156 }
157
158 static void update_label(struct ui_psensor *ui)
159 {
160         char *label, *str, *tmp, *guide;
161         struct psensor **p;
162
163         p =  ui_get_sensors_ordered_by_position(ui);
164         label = NULL;
165         guide = NULL;
166         while (*p) {
167                 if (config_is_appindicator_label_enabled((*p)->id)) {
168                         str = psensor_current_value_to_str
169                                 (*p, ui->config->temperature_unit == CELSIUS);
170
171                         if (label == NULL) {
172                                 label = str;
173                         } else {
174                                 tmp = malloc(strlen(label)
175                                              + 1
176                                              + strlen(str)
177                                              + 1);
178                                 sprintf(tmp, "%s %s", label, str);
179                                 free(label);
180                                 free(str);
181                                 label = tmp;
182                         }
183
184                         if (is_temp_type((*p)->type))
185                                 str = "999UUU";
186                         else if ((*p)->type & SENSOR_TYPE_RPM)
187                                 str = "999UUU";
188                         else /* percent */
189                                 str = "999%";
190
191                         if (guide == NULL) {
192                                 guide = strdup(str);
193                         } else {
194                                 tmp = malloc(strlen(guide)
195                                              + 1
196                                              + strlen(str)
197                                              + 1);
198                                 sprintf(tmp, "%sW%s", guide, str);
199                                 free(guide);
200                                 guide = tmp;
201                         }
202
203                 }
204                 p++;
205         }
206
207         app_indicator_set_label(indicator, label, guide);
208 }
209
210 void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
211 {
212         AppIndicatorStatus status;
213
214         if (!indicator)
215                 return;
216
217         update_label(ui);
218
219         status = app_indicator_get_status(indicator);
220
221         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
222                 app_indicator_set_status(indicator,
223                                          APP_INDICATOR_STATUS_ACTIVE);
224
225         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
226                 app_indicator_set_status(indicator,
227                                          APP_INDICATOR_STATUS_ATTENTION);
228
229         update_menu_items(ui->config->temperature_unit == CELSIUS);
230 }
231
232 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
233 {
234         GtkStatusIcon *ico;
235
236         log_debug("ui_appindicator.unity_fallback()");
237
238         appindicator_supported = 0;
239
240         ico = ui_status_get_icon(ui_psensor);
241
242         ui_status_set_visible(1);
243
244         return ico;
245 }
246
247 static void
248 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
249 {
250         log_debug("ui_appindicator.unity_unfallback()");
251
252         ui_status_set_visible(0);
253
254         appindicator_supported = 1;
255 }
256
257 void ui_appindicator_update_menu(struct ui_psensor *ui)
258 {
259         GtkWidget *menu;
260
261         menu = get_menu(ui);
262         app_indicator_set_menu(indicator, GTK_MENU(menu));
263
264         gtk_widget_show_all(menu);
265 }
266
267 void ui_appindicator_init(struct ui_psensor *ui)
268 {
269         ui_psensor = ui;
270
271         indicator = app_indicator_new
272                 ("psensor",
273                  ICON,
274                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
275
276         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
277         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
278
279         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
280         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
281
282         ui_appindicator_update_menu(ui);
283 }
284
285 int is_appindicator_supported(void)
286 {
287         return appindicator_supported;
288 }
289
290 void ui_appindicator_cleanup(void)
291 {
292         free(sensors);
293 }