removed useless static
[psensor.git] / src / ui_appindicator.c
1 /*
2  * Copyright (C) 2010-2012 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 GtkMenuItem **sensor_menu_items;
38 static int appindicator_supported = 1;
39 static AppIndicator *indicator;
40 static struct ui_psensor *ui_psensor;
41
42 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
43 {
44         ui_window_show((struct ui_psensor *)data);
45 }
46
47 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
48 {
49         ui_psensor_quit(data);
50 }
51
52 static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
53 {
54 #ifdef HAVE_APPINDICATOR_029
55         gdk_threads_enter();
56 #endif
57
58         ui_pref_dialog_run((struct ui_psensor *)data);
59
60 #ifdef HAVE_APPINDICATOR_029
61         gdk_threads_leave();
62 #endif
63 }
64
65 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
66 {
67         struct ui_psensor *ui = data;
68
69 #ifdef HAVE_APPINDICATOR_029
70         gdk_threads_enter();
71 #endif
72
73         if (ui->sensors && *ui->sensors)
74                 ui_sensorpref_dialog_run(*ui->sensors, ui);
75
76 #ifdef HAVE_APPINDICATOR_029
77         gdk_threads_leave();
78 #endif
79 }
80
81 static void cb_about(GtkMenuItem *mi, gpointer data)
82 {
83         ui_show_about_dialog();
84 }
85
86 static const char *menu_desc =
87 "<ui>"
88 "  <popup name='MainMenu'>"
89 "      <menuitem name='Show' action='ShowAction' />"
90 "      <separator />"
91 "      <separator />"
92 "      <menuitem name='Preferences' action='PreferencesAction' />"
93 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
94 "      <separator />"
95 "      <menuitem name='About' action='AboutAction' />"
96 "      <separator />"
97 "      <menuitem name='Quit' action='QuitAction' />"
98 "  </popup>"
99 "</ui>";
100
101 static GtkActionEntry entries[] = {
102         { "PsensorMenuAction", NULL, "_Psensor" },
103
104         { "ShowAction", NULL,
105           N_("_Show"), NULL,
106           N_("Show"),
107           G_CALLBACK(cb_menu_show) },
108
109         { "PreferencesAction", GTK_STOCK_PREFERENCES,
110           N_("_Preferences"), NULL,
111           N_("Preferences"),
112           G_CALLBACK(cb_menu_preferences) },
113
114         { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
115           N_("S_ensor Preferences"),
116           NULL,
117           N_("SensorPreferences"),
118           G_CALLBACK(cb_sensor_preferences) },
119
120         { "AboutAction", NULL,
121           N_("_About"),
122           NULL,
123           N_("About"),
124           G_CALLBACK(cb_about) },
125
126         { "QuitAction",
127           GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
128 };
129 static guint n_entries = G_N_ELEMENTS(entries);
130
131 static void update_sensor_menu_item(GtkMenuItem *item,
132                                     struct psensor *s,
133                                     int use_celcius)
134 {
135         gchar *str;
136         double v;
137
138         v = psensor_get_current_value(s);
139
140         if (is_temp_type(s->type) && !use_celcius)
141                 v = celcius_to_fahrenheit(v);
142
143         str = g_strdup_printf("%s: %2.f %s",
144                               s->name,
145                               v,
146                               psensor_type_to_unit_str(s->type, use_celcius));
147
148         gtk_menu_item_set_label(item, str);
149
150         g_free(str);
151 }
152
153 static void update_sensor_menu_items(struct psensor **sensors,
154                                      int use_celcius)
155 {
156         int n, i;
157
158         n = psensor_list_size(sensors);
159         for (i = 0; i < n; i++)
160                 update_sensor_menu_item(sensor_menu_items[i],
161                                         sensors[i],
162                                         use_celcius);
163 }
164
165 static GtkWidget *get_menu(struct ui_psensor *ui)
166 {
167         GtkActionGroup *action_group;
168         GtkUIManager *menu_manager;
169         GError *error;
170         GtkMenu *menu;
171         int i;
172         int n = psensor_list_size(ui->sensors);
173         struct psensor **sensors = ui->sensors;
174
175         action_group = gtk_action_group_new("PsensorActions");
176         gtk_action_group_set_translation_domain(action_group, PACKAGE);
177         menu_manager = gtk_ui_manager_new();
178
179         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
180         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
181
182         error = NULL;
183         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
184
185         if (error)
186                 g_error(_("building menus failed: %s"), error->message);
187
188         menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
189
190         sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
191         for (i = 0; i < n; i++) {
192                 struct psensor *s = sensors[i];
193
194                 sensor_menu_items[i]
195                         = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
196
197                 gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
198                                       GTK_WIDGET(sensor_menu_items[i]),
199                                       i+2);
200
201                 update_sensor_menu_item
202                         (sensor_menu_items[i],
203                          s,
204                          ui->config->temperature_unit == CELCIUS);
205         }
206
207         return GTK_WIDGET(menu);
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         status = app_indicator_get_status(indicator);
218
219         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
220                 app_indicator_set_status
221                         (indicator, APP_INDICATOR_STATUS_ACTIVE);
222
223         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
224                 app_indicator_set_status
225                     (indicator, APP_INDICATOR_STATUS_ATTENTION);
226
227         update_sensor_menu_items(ui->sensors,
228                                  ui->config->temperature_unit == CELCIUS);
229 }
230
231 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
232 {
233         GtkStatusIcon *ico;
234
235         log_debug("ui_appindicator#unity_fallback");
236
237         appindicator_supported = 0;
238
239         ico = ui_status_get_icon(ui_psensor);
240
241         ui_status_set_visible(1);
242
243         return ico;
244 }
245
246 static void
247 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
248 {
249         log_debug("ui_appindicator#unity_unfallback");
250
251         ui_status_set_visible(0);
252
253         appindicator_supported = 1;
254 }
255
256 void ui_appindicator_init(struct ui_psensor *ui)
257 {
258         GtkWidget *menu;
259
260         ui_psensor = ui;
261
262         indicator = app_indicator_new
263                 ("psensor",
264                  ICON,
265                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
266
267         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
268         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
269
270         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
271         app_indicator_set_attention_icon(indicator, ATTENTION_ICON);
272
273         menu = get_menu(ui);
274         app_indicator_set_menu(indicator, GTK_MENU(menu));
275
276         gtk_widget_show_all(menu);
277 }
278
279 int is_appindicator_supported()
280 {
281         return appindicator_supported;
282 }
283
284 void ui_appindicator_cleanup()
285 {
286         /* TODO: cleanup menu items. */
287 }