status icon
[psensor.git] / src / ui_appindicator.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gtk/gtk.h>
25 #include <libappindicator/app-indicator.h>
26
27 #include "psensor.h"
28 #include "ui.h"
29 #include "ui_appindicator.h"
30 #include "ui_sensorpref.h"
31 #include "ui_pref.h"
32
33 static GtkMenuItem **sensor_menu_items;
34 static GtkWidget *main_window;
35 static int appindicator_supported = 1;
36
37 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
38 {
39         struct ui_psensor *ui = (struct ui_psensor *)data;
40
41         gtk_window_present(GTK_WINDOW(ui->main_window));
42 }
43
44 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
45 {
46         ui_psensor_quit(data);
47 }
48
49 static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
50 {
51 #ifdef HAVE_APPINDICATOR_029
52         gdk_threads_enter();
53 #endif
54
55         ui_pref_dialog_run((struct ui_psensor *)data);
56
57 #ifdef HAVE_APPINDICATOR_029
58         gdk_threads_leave();
59 #endif
60 }
61
62 static void
63 cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
64 {
65         struct ui_psensor *ui = data;
66
67 #ifdef HAVE_APPINDICATOR_029
68         gdk_threads_enter();
69 #endif
70
71         if (ui->sensors && *ui->sensors)
72                 ui_sensorpref_dialog_run(*ui->sensors, ui);
73
74 #ifdef HAVE_APPINDICATOR_029
75         gdk_threads_leave();
76 #endif
77 }
78
79 static void cb_about(GtkMenuItem *mi, gpointer data)
80 {
81         ui_show_about_dialog();
82 }
83
84 static const char *menu_desc =
85 "<ui>"
86 "  <popup name='MainMenu'>"
87 "      <menuitem name='Show' action='ShowAction' />"
88 "      <separator />"
89 "      <separator />"
90 "      <menuitem name='Preferences' action='PreferencesAction' />"
91 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
92 "      <separator />"
93 "      <menuitem name='About' action='AboutAction' />"
94 "      <separator />"
95 "      <menuitem name='Quit' action='QuitAction' />"
96 "  </popup>"
97 "</ui>";
98
99 static GtkActionEntry entries[] = {
100   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
101
102   { "ShowAction", NULL,        /* name, stock id */
103     "_Show", NULL, /* label, accelerator */
104     "Show",                    /* tooltip */
105     G_CALLBACK(cb_menu_show) },
106
107   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
108     "_Preferences", NULL,                           /* label, accelerator */
109     "Preferences",                                  /* tooltip */
110     G_CALLBACK(cb_menu_preferences) },
111
112   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
113     "S_ensor Preferences",
114     NULL,
115     "SensorPreferences",
116     G_CALLBACK(cb_sensor_preferences) },
117
118   { "AboutAction", NULL,
119     "_About",
120     NULL,
121     "About",
122     G_CALLBACK(cb_about) },
123
124   { "QuitAction",
125     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
126 };
127 static guint n_entries = G_N_ELEMENTS(entries);
128
129 static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
130 {
131         gchar *str;
132
133         str = g_strdup_printf("%s: %2.f %s",
134                               s->name,
135                               psensor_get_current_value(s),
136                               psensor_type_to_unit_str(s->type));
137
138         gtk_menu_item_set_label(item, str);
139
140         g_free(str);
141 }
142
143 static void update_sensor_menu_items(struct psensor **sensors)
144 {
145         int n = psensor_list_size(sensors);
146         int i;
147
148         for (i = 0; i < n; i++)
149                 update_sensor_menu_item(sensor_menu_items[i],
150                                         sensors[i]);
151 }
152
153 static GtkWidget *get_menu(struct ui_psensor *ui)
154 {
155         GtkActionGroup *action_group;
156         GtkUIManager *menu_manager;
157         GError *error;
158         GtkMenu *menu;
159         int i;
160         int n = psensor_list_size(ui->sensors);
161         struct psensor **sensors = ui->sensors;
162
163
164         action_group = gtk_action_group_new("PsensorActions");
165         gtk_action_group_set_translation_domain(action_group, PACKAGE);
166         menu_manager = gtk_ui_manager_new();
167
168         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
169         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
170
171         error = NULL;
172         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
173
174         if (error)
175                 g_error(_("building menus failed: %s"), error->message);
176
177         menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
178
179         sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
180         for (i = 0; i < n; i++) {
181                 struct psensor *s = sensors[i];
182
183                 sensor_menu_items[i]
184                         = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
185
186                 gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
187                                       GTK_WIDGET(sensor_menu_items[i]),
188                                       i+2);
189
190                 update_sensor_menu_item(sensor_menu_items[i],
191                                         s);
192         }
193
194
195         return GTK_WIDGET(menu);
196 }
197
198 void ui_appindicator_update(struct ui_psensor *ui,
199                             unsigned int attention)
200 {
201         AppIndicatorStatus status;
202
203         if (!ui->indicator)
204                 return;
205
206         status = app_indicator_get_status(ui->indicator);
207
208         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
209                 app_indicator_set_status
210                     (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
211
212         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
213                 app_indicator_set_status
214                     (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
215
216         update_sensor_menu_items(ui->sensors);
217 }
218
219 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
220 {
221         log_printf(LOG_DEBUG, "ui_appindicator#unity_fallback");
222
223         gtk_widget_show_all(main_window);
224
225         appindicator_supported = 0;
226
227         return NULL;
228 }
229
230 static void unity_unfallback(AppIndicator *indicator,
231                              GtkStatusIcon *status_icon)
232 {
233         log_printf(LOG_DEBUG, "ui_appindicator#unity_unfallback");
234
235         appindicator_supported = 1;
236 }
237
238 void ui_appindicator_init(struct ui_psensor *ui)
239 {
240         GtkWidget *menu;
241         AppIndicator *indicator;
242
243         main_window = ui->main_window;
244
245         indicator = app_indicator_new
246                 ("psensor",
247                  "psensor_normal",
248                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
249
250         APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
251         APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
252
253         app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
254         app_indicator_set_attention_icon(indicator, "psensor_hot");
255
256         menu = get_menu(ui);
257         app_indicator_set_menu(indicator, GTK_MENU(menu));
258
259         ui->indicator = indicator;
260
261         gtk_widget_show_all(menu);
262 }
263
264 int is_appindicator_supported()
265 {
266         return appindicator_supported;
267 }
268
269 void ui_appindicator_cleanup()
270 {
271         /* TODO: cleanup menu items. */
272 }