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;
36 static AppIndicator *indicator;
38 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
40 ui_window_show((struct ui_psensor *)data);
43 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
45 ui_psensor_quit(data);
48 static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
50 #ifdef HAVE_APPINDICATOR_029
54 ui_pref_dialog_run((struct ui_psensor *)data);
56 #ifdef HAVE_APPINDICATOR_029
62 cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
64 struct ui_psensor *ui = data;
66 #ifdef HAVE_APPINDICATOR_029
70 if (ui->sensors && *ui->sensors)
71 ui_sensorpref_dialog_run(*ui->sensors, ui);
73 #ifdef HAVE_APPINDICATOR_029
78 static void cb_about(GtkMenuItem *mi, gpointer data)
80 ui_show_about_dialog();
83 static const char *menu_desc =
85 " <popup name='MainMenu'>"
86 " <menuitem name='Show' action='ShowAction' />"
89 " <menuitem name='Preferences' action='PreferencesAction' />"
90 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
92 " <menuitem name='About' action='AboutAction' />"
94 " <menuitem name='Quit' action='QuitAction' />"
98 static GtkActionEntry entries[] = {
99 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
101 { "ShowAction", NULL, /* name, stock id */
102 "_Show", NULL, /* label, accelerator */
103 "Show", /* tooltip */
104 G_CALLBACK(cb_menu_show) },
106 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
107 "_Preferences", NULL, /* label, accelerator */
108 "Preferences", /* tooltip */
109 G_CALLBACK(cb_menu_preferences) },
111 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
112 "S_ensor Preferences",
115 G_CALLBACK(cb_sensor_preferences) },
117 { "AboutAction", NULL,
121 G_CALLBACK(cb_about) },
124 GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
126 static guint n_entries = G_N_ELEMENTS(entries);
128 static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
132 str = g_strdup_printf("%s: %2.f %s",
134 psensor_get_current_value(s),
135 psensor_type_to_unit_str(s->type));
137 gtk_menu_item_set_label(item, str);
142 static void update_sensor_menu_items(struct psensor **sensors)
144 int n = psensor_list_size(sensors);
147 for (i = 0; i < n; i++)
148 update_sensor_menu_item(sensor_menu_items[i],
152 static GtkWidget *get_menu(struct ui_psensor *ui)
154 GtkActionGroup *action_group;
155 GtkUIManager *menu_manager;
159 int n = psensor_list_size(ui->sensors);
160 struct psensor **sensors = ui->sensors;
163 action_group = gtk_action_group_new("PsensorActions");
164 gtk_action_group_set_translation_domain(action_group, PACKAGE);
165 menu_manager = gtk_ui_manager_new();
167 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
168 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
171 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
174 g_error(_("building menus failed: %s"), error->message);
176 menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
178 sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
179 for (i = 0; i < n; i++) {
180 struct psensor *s = sensors[i];
183 = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
185 gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
186 GTK_WIDGET(sensor_menu_items[i]),
189 update_sensor_menu_item(sensor_menu_items[i],
194 return GTK_WIDGET(menu);
197 void ui_appindicator_update(struct ui_psensor *ui,
198 unsigned int attention)
200 AppIndicatorStatus status;
205 status = app_indicator_get_status(indicator);
207 if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
208 app_indicator_set_status
209 (indicator, APP_INDICATOR_STATUS_ACTIVE);
211 if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
212 app_indicator_set_status
213 (indicator, APP_INDICATOR_STATUS_ATTENTION);
215 update_sensor_menu_items(ui->sensors);
218 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
220 log_printf(LOG_DEBUG, "ui_appindicator#unity_fallback");
222 gtk_widget_show_all(main_window);
224 appindicator_supported = 0;
229 static void unity_unfallback(AppIndicator *indicator,
230 GtkStatusIcon *status_icon)
232 log_printf(LOG_DEBUG, "ui_appindicator#unity_unfallback");
234 appindicator_supported = 1;
237 void ui_appindicator_init(struct ui_psensor *ui)
241 main_window = ui->main_window;
243 indicator = app_indicator_new
246 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
248 APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
249 APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
251 app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
252 app_indicator_set_attention_icon(indicator, "psensor_hot");
255 app_indicator_set_menu(indicator, GTK_MENU(menu));
257 gtk_widget_show_all(menu);
260 int is_appindicator_supported()
262 return appindicator_supported;
265 void ui_appindicator_cleanup()
267 /* TODO: cleanup menu items. */