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