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