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