2 * Copyright (C) 2010-2012 jeanfi@gmail.com
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.
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.
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
24 #include <libappindicator/app-indicator.h>
28 #include "ui_appindicator.h"
29 #include "ui_sensorpref.h"
30 #include "ui_status.h"
33 static const char *ICON = "psensor_normal";
34 static const char *ATTENTION_ICON = "psensor_hot";
36 static GtkMenuItem **sensor_menu_items;
37 static GtkWidget *main_window;
38 static int appindicator_supported = 1;
39 static AppIndicator *indicator;
40 static struct ui_psensor *ui_psensor;
42 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
44 ui_window_show((struct ui_psensor *)data);
47 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
49 ui_psensor_quit(data);
52 static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
54 #ifdef HAVE_APPINDICATOR_029
58 ui_pref_dialog_run((struct ui_psensor *)data);
60 #ifdef HAVE_APPINDICATOR_029
65 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
67 struct ui_psensor *ui = data;
69 #ifdef HAVE_APPINDICATOR_029
73 if (ui->sensors && *ui->sensors)
74 ui_sensorpref_dialog_run(*ui->sensors, ui);
76 #ifdef HAVE_APPINDICATOR_029
81 static void cb_about(GtkMenuItem *mi, gpointer data)
83 ui_show_about_dialog();
86 static const char *menu_desc =
88 " <popup name='MainMenu'>"
89 " <menuitem name='Show' action='ShowAction' />"
92 " <menuitem name='Preferences' action='PreferencesAction' />"
93 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
95 " <menuitem name='About' action='AboutAction' />"
97 " <menuitem name='Quit' action='QuitAction' />"
101 static GtkActionEntry entries[] = {
102 { "PsensorMenuAction", NULL, "_Psensor" },
104 { "ShowAction", NULL,
107 G_CALLBACK(cb_menu_show) },
109 { "PreferencesAction", GTK_STOCK_PREFERENCES,
110 "_Preferences", NULL,
112 G_CALLBACK(cb_menu_preferences) },
114 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
115 "S_ensor Preferences",
118 G_CALLBACK(cb_sensor_preferences) },
120 { "AboutAction", NULL,
124 G_CALLBACK(cb_about) },
127 GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
129 static guint n_entries = G_N_ELEMENTS(entries);
131 static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
135 str = g_strdup_printf("%s: %2.f %s",
137 psensor_get_current_value(s),
138 psensor_type_to_unit_str(s->type));
140 gtk_menu_item_set_label(item, str);
145 static void update_sensor_menu_items(struct psensor **sensors)
149 n = psensor_list_size(sensors);
150 for (i = 0; i < n; i++)
151 update_sensor_menu_item(sensor_menu_items[i], sensors[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);
199 void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
201 AppIndicatorStatus status;
206 status = app_indicator_get_status(indicator);
208 if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
209 app_indicator_set_status
210 (indicator, APP_INDICATOR_STATUS_ACTIVE);
212 if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
213 app_indicator_set_status
214 (indicator, APP_INDICATOR_STATUS_ATTENTION);
216 update_sensor_menu_items(ui->sensors);
219 static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
221 log_debug("ui_appindicator#unity_fallback");
223 appindicator_supported = 0;
225 return ui_status_get_icon(ui_psensor);
229 unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
231 log_debug("ui_appindicator#unity_unfallback");
233 appindicator_supported = 1;
236 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, ATTENTION_ICON);
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. */