appindicator displays unit of sensor values
[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
35 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
36 {
37         struct ui_psensor *ui = (struct ui_psensor *)data;
38
39         gtk_window_present(GTK_WINDOW(ui->main_window));
40 }
41
42 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
43 {
44         ui_psensor_quit(data);
45 }
46
47 static void
48 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
62 cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
63 {
64         struct ui_psensor *ui = data;
65
66 #ifdef HAVE_APPINDICATOR_029
67         gdk_threads_enter();
68 #endif
69
70         if (ui->sensors && *ui->sensors)
71                 ui_sensorpref_dialog_run(*ui->sensors, ui);
72
73 #ifdef HAVE_APPINDICATOR_029
74         gdk_threads_leave();
75 #endif
76 }
77
78 static const char *menu_desc =
79 "<ui>"
80 "  <popup name='MainMenu'>"
81 "      <menuitem name='Show' action='ShowAction' />"
82 "      <separator />"
83 "      <separator />"
84 "      <menuitem name='Preferences' action='PreferencesAction' />"
85 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
86 "      <separator />"
87 "      <menuitem name='Quit' action='QuitAction' />"
88 "  </popup>"
89 "</ui>";
90
91 static GtkActionEntry entries[] = {
92   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
93
94   { "ShowAction", NULL,        /* name, stock id */
95     "_Show", NULL, /* label, accelerator */
96     "Show",                    /* tooltip */
97     G_CALLBACK(cb_menu_show) },
98
99   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
100     "_Preferences", NULL,                           /* label, accelerator */
101     "Preferences",                                  /* tooltip */
102     G_CALLBACK(cb_menu_preferences) },
103
104   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
105     "S_ensor Preferences",
106     NULL,
107     "SensorPreferences",
108     G_CALLBACK(cb_sensor_preferences) },
109
110   { "QuitAction",
111     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
112 };
113 static guint n_entries = G_N_ELEMENTS(entries);
114
115 static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
116 {
117         gchar *str;
118
119         str = g_strdup_printf("%s: %2.f %s",
120                               s->name,
121                               psensor_get_current_value(s),
122                               psensor_type_to_unit_str(s->type));
123
124         gtk_menu_item_set_label(item, str);
125
126         g_free(str);
127 }
128
129 static void update_sensor_menu_items(struct psensor **sensors)
130 {
131         int n = psensor_list_size(sensors);
132         int i;
133
134         for (i = 0; i < n; i++)
135                 update_sensor_menu_item(sensor_menu_items[i],
136                                         sensors[i]);
137 }
138
139 static GtkWidget *get_menu(struct ui_psensor *ui)
140 {
141         GtkActionGroup *action_group;
142         GtkUIManager *menu_manager;
143         GError *error;
144         GtkMenu *menu;
145         int i;
146         int n = psensor_list_size(ui->sensors);
147         struct psensor **sensors = ui->sensors;
148
149
150         action_group = gtk_action_group_new("PsensorActions");
151         gtk_action_group_set_translation_domain(action_group, PACKAGE);
152         menu_manager = gtk_ui_manager_new();
153
154         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
155         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
156
157         error = NULL;
158         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
159
160         if (error)
161                 g_error(_("building menus failed: %s"), error->message);
162
163         menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu"));
164
165         sensor_menu_items = malloc(sizeof(GtkWidget *)*n);
166         for (i = 0; i < n; i++) {
167                 struct psensor *s = sensors[i];
168
169                 sensor_menu_items[i]
170                         = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name));
171
172                 gtk_menu_shell_insert(GTK_MENU_SHELL(menu),
173                                       GTK_WIDGET(sensor_menu_items[i]),
174                                       i+2);
175
176                 update_sensor_menu_item(sensor_menu_items[i],
177                                         s);
178         }
179
180
181         return GTK_WIDGET(menu);
182 }
183
184
185 void ui_appindicator_update(struct ui_psensor *ui)
186 {
187         struct psensor **sensor_cur = ui->sensors;
188         AppIndicatorStatus status;
189         int attention = 0;
190
191         if (!ui->indicator)
192                 return;
193
194         while (*sensor_cur) {
195                 struct psensor *s = *sensor_cur;
196
197                 if (s->alarm_enabled && s->alarm_raised) {
198                         attention = 1;
199                         break;
200                 }
201
202                 sensor_cur++;
203         }
204
205         status = app_indicator_get_status(ui->indicator);
206
207         if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
208                 app_indicator_set_status
209                     (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
210
211         if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
212                 app_indicator_set_status
213                     (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
214
215         update_sensor_menu_items(ui->sensors);
216 }
217
218 void ui_appindicator_init(struct ui_psensor *ui)
219 {
220         GtkWidget *indicatormenu;
221
222         ui->indicator
223             = app_indicator_new("psensor",
224                                 "psensor",
225                                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
226
227         app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
228         app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
229
230         indicatormenu = get_menu(ui);
231
232         gtk_widget_show_all(indicatormenu);
233
234         app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
235 }