2 Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
25 #include <libappindicator/app-indicator.h>
29 #include "ui_appindicator.h"
30 #include "ui_sensorpref.h"
33 static GtkMenuItem **sensor_menu_items;
34 static GtkWidget *main_window;
35 static int appindicator_supported = 1;
37 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
39 struct ui_psensor *ui = (struct ui_psensor *)data;
41 gtk_window_present(GTK_WINDOW(ui->main_window));
44 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
46 ui_psensor_quit(data);
50 cb_menu_preferences(GtkMenuItem *mi, gpointer data)
52 #ifdef HAVE_APPINDICATOR_029
56 ui_pref_dialog_run((struct ui_psensor *)data);
58 #ifdef HAVE_APPINDICATOR_029
64 cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
66 struct ui_psensor *ui = data;
68 #ifdef HAVE_APPINDICATOR_029
72 if (ui->sensors && *ui->sensors)
73 ui_sensorpref_dialog_run(*ui->sensors, ui);
75 #ifdef HAVE_APPINDICATOR_029
80 static void cb_about(GtkMenuItem *mi, gpointer data)
82 ui_show_about_dialog();
85 static const char *menu_desc =
87 " <popup name='MainMenu'>"
88 " <menuitem name='Show' action='ShowAction' />"
91 " <menuitem name='Preferences' action='PreferencesAction' />"
92 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
94 " <menuitem name='About' action='AboutAction' />"
96 " <menuitem name='Quit' action='QuitAction' />"
100 static GtkActionEntry entries[] = {
101 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
103 { "ShowAction", NULL, /* name, stock id */
104 "_Show", NULL, /* label, accelerator */
105 "Show", /* tooltip */
106 G_CALLBACK(cb_menu_show) },
108 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
109 "_Preferences", NULL, /* label, accelerator */
110 "Preferences", /* tooltip */
111 G_CALLBACK(cb_menu_preferences) },
113 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
114 "S_ensor Preferences",
117 G_CALLBACK(cb_sensor_preferences) },
119 { "AboutAction", NULL,
123 G_CALLBACK(cb_about) },
126 GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
128 static guint n_entries = G_N_ELEMENTS(entries);
130 static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
134 str = g_strdup_printf("%s: %2.f %s",
136 psensor_get_current_value(s),
137 psensor_type_to_unit_str(s->type));
139 gtk_menu_item_set_label(item, str);
144 static void update_sensor_menu_items(struct psensor **sensors)
146 int n = psensor_list_size(sensors);
149 for (i = 0; i < n; i++)
150 update_sensor_menu_item(sensor_menu_items[i],
154 static GtkWidget *get_menu(struct ui_psensor *ui)
156 GtkActionGroup *action_group;
157 GtkUIManager *menu_manager;
161 int n = psensor_list_size(ui->sensors);
162 struct psensor **sensors = ui->sensors;
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();
169 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
170 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
173 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
176 g_error(_("building menus failed: %s"), error->message);
178 menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
180 sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
181 for (i = 0; i < n; i++) {
182 struct psensor *s = sensors[i];
185 = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
187 gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
188 GTK_WIDGET(sensor_menu_items[i]),
191 update_sensor_menu_item(sensor_menu_items[i],
196 return GTK_WIDGET(menu);
200 void ui_appindicator_update(struct ui_psensor *ui)
202 struct psensor **sensor_cur = ui->sensors;
203 AppIndicatorStatus status;
209 while (*sensor_cur) {
210 struct psensor *s = *sensor_cur;
212 if (s->alarm_enabled && s->alarm_raised) {
220 status = app_indicator_get_status(ui->indicator);
222 if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
223 app_indicator_set_status
224 (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
226 if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
227 app_indicator_set_status
228 (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
230 update_sensor_menu_items(ui->sensors);
233 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
235 gtk_widget_show_all(main_window);
237 appindicator_supported = 0;
242 static void unity_unfallback(AppIndicator *indicator,
243 GtkStatusIcon *status_icon)
245 appindicator_supported = 1;
249 void ui_appindicator_init(struct ui_psensor *ui)
251 GtkWidget *indicatormenu;
253 main_window = ui->main_window;
256 = app_indicator_new("psensor",
258 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
260 APP_INDICATOR_GET_CLASS(ui->indicator)->fallback = unity_fallback;
261 APP_INDICATOR_GET_CLASS(ui->indicator)->unfallback = unity_unfallback;
263 app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
264 app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
266 indicatormenu = get_menu(ui);
268 gtk_widget_show_all(indicatormenu);
270 app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
273 int is_appindicator_supported()
275 return appindicator_supported;