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