]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/xfce4-hdaps.c
Added tooltip support for the three states.
[xfce4-hdaps.git] / panel-plugin / xfce4-hdaps.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include "xfce4-hdaps.h"
6 #include "xfce4-hdaps-dialogs.h"
7
8 /* We need a default device. Since we require kernels
9 newer than 2.6.28, it's probably sdX, and I'm guessing
10 that sda is most likely. */
11 #define DEFAULT_DEVICE_NAME "sda"
12
13 /* How often do we poll, in milliseconds?
14 This should be configured, but we set the default here. */
15 #define DEFAULT_POLL_FREQUENCY 500
16
17
18 /* Prototype, needed to register below. */
19 static void hdaps_construct(XfcePanelPlugin *plugin);
20
21 /* Register the plugin with the panel. */
22 XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(hdaps_construct);
23
24
25 void hdaps_save(XfcePanelPlugin *plugin, HdapsPlugin *hdaps) {
26
27 XfceRc *rc;
28 gchar *file;
29
30 /* Get the config file location. XFCE should know this. */
31 file = xfce_panel_plugin_save_location(plugin, TRUE);
32
33 if (G_UNLIKELY(file == NULL)) {
34 DBG("Failed to find the configuration file. Bailing.");
35 return;
36 }
37
38 /* Open the config file read/write. */
39 rc = xfce_rc_simple_open(file, FALSE);
40
41 /* And we can free the file path now that we've
42 opened the file. */
43 g_free(file);
44
45 if (G_UNLIKELY(rc == NULL)) {
46 DBG("Failed to open the configuration file. Bailing.");
47 return;
48 }
49
50
51 /* Write any user-configured values to the resource file. */
52 if (hdaps->device_name) {
53 xfce_rc_write_entry(rc, "device_name", hdaps->device_name);
54 }
55
56 xfce_rc_write_int_entry(rc, "poll_frequency", hdaps->poll_frequency);
57
58 /* close the rc file */
59 xfce_rc_close(rc);
60 }
61
62
63
64 void hdaps_set_icon(HdapsPlugin *hdaps, int status) {
65 GdkPixbuf *icon;
66 gint size;
67
68 /* Panel info magic. */
69 size = xfce_panel_plugin_get_size(hdaps->plugin) - 6;
70
71 /* Try to load an icon from the current icon theme. */
72 if (status == HDAPS_ERROR) {
73 /* Error */
74 icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
75 "emblem-noread",
76 size, 0, NULL);
77 }
78 else if (status == HDAPS_OFF) {
79 icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
80 "drive-harddisk",
81 size, 0, NULL);
82 }
83 else {
84 /* status > HDAPS_OFF means it's on. */
85 icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
86 "emblem-nowrite",
87 size, 0, NULL);
88 }
89
90 /* Get rid of the previous icon. */
91 if (hdaps->icon) {
92 gtk_widget_destroy(hdaps->icon);
93 }
94
95 if (icon) {
96 hdaps->icon = gtk_image_new_from_pixbuf(icon);
97 g_object_unref(G_OBJECT(icon));
98 }
99 else {
100 hdaps->icon = gtk_image_new_from_icon_name("dialog-warning", GTK_ICON_SIZE_BUTTON);
101 }
102
103 gtk_box_pack_start(GTK_BOX(hdaps->hvbox), GTK_WIDGET(hdaps->icon), FALSE, FALSE, 0);
104 gtk_widget_show(hdaps->icon);
105
106 return;
107 }
108
109
110
111 void hdaps_set_tooltip(HdapsPlugin *hdaps, int status) {
112
113 if (status == HDAPS_ERROR) {
114 gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS Error", NULL);
115 }
116 else if (status == HDAPS_OFF) {
117 gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS Off", NULL);
118 }
119 else {
120 gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, "HDAPS On", NULL);
121 }
122 }
123
124
125 static void hdaps_set_defaults(HdapsPlugin *hdaps) {
126 DBG("Configuring all settings to defaults.");
127 hdaps->device_name = g_strdup(DEFAULT_DEVICE_NAME);
128 snprintf(hdaps->sysfs_file, FILENAME_MAX, UNLOAD_HEADS_FMT, hdaps->device_name);
129 hdaps->poll_frequency = DEFAULT_POLL_FREQUENCY;
130 }
131
132
133 static void hdaps_read(HdapsPlugin *hdaps) {
134
135 XfceRc *rc;
136 gchar *file;
137 const gchar *saved_device_name;
138
139 /* Get the plugin config file location. XFCE should know this. */
140 file = xfce_panel_plugin_save_location(hdaps->plugin, TRUE);
141
142 if (G_UNLIKELY(file == NULL)) {
143 DBG("Something went wrong getting the configuration file location.");
144 DBG("Retaining default settings.");
145 return;
146 }
147
148 /* Open the config file read-only. */
149 rc = xfce_rc_simple_open(file, TRUE);
150
151 /* Don't need this anymore if we've got a file handle. */
152 g_free(file);
153
154 if (G_UNLIKELY(rc == NULL)) {
155 DBG("There was an error opening the configuration file.");
156 DBG("Retaining default settings.");
157 return;
158 }
159
160 /* Read the settings, one at a time. */
161
162 /* We use saved_device_name here because we need
163 to dupe the string after we read it in from the
164 config file. */
165 saved_device_name = xfce_rc_read_entry(rc,
166 "device_name",
167 DEFAULT_DEVICE_NAME);
168 hdaps->device_name = g_strdup(saved_device_name);
169 snprintf(hdaps->sysfs_file, FILENAME_MAX, UNLOAD_HEADS_FMT, hdaps->device_name);
170
171 /* Integers are easier. */
172 hdaps->poll_frequency = xfce_rc_read_int_entry(rc,
173 "poll_frequency",
174 DEFAULT_POLL_FREQUENCY);
175
176 /* And close the config file. */
177 xfce_rc_close(rc);
178 }
179
180
181
182
183 static HdapsPlugin *hdaps_new(XfcePanelPlugin *plugin) {
184 HdapsPlugin *hdaps;
185 GtkOrientation orientation;
186
187 /* Allocate memory for the plugin struct, and zero it. */
188 hdaps = panel_slice_new0(HdapsPlugin);
189
190 /* The HdapsPlugin gets a copy of the XfcePanelPlugin. */
191 hdaps->plugin = plugin;
192
193 /* Set default values right before reading in the user's settings.
194 This way, hdaps_read() doesn't have to set defaults on error
195 conditions. */
196 hdaps_set_defaults(hdaps);
197
198 /* Read any user settings into the HdapsPlugin. */
199 hdaps_read(hdaps);
200
201 /* Get the current orientation. Magic. */
202 orientation = xfce_panel_plugin_get_orientation(plugin);
203
204 /* This creates the event box and shows it. This
205 is necessary to,
206
207 a) See the widget
208 b) Interact with it via right-click, etc.
209 */
210 hdaps->eventbox = gtk_event_box_new();
211 gtk_widget_show(hdaps->eventbox);
212
213 /* Set up the hvbox for the widget, which supports
214 both horizontal and vertical (hv) orientations. */
215 hdaps->hvbox = xfce_hvbox_new(orientation, FALSE, 2);
216 gtk_widget_show(hdaps->hvbox);
217 gtk_container_add(GTK_CONTAINER(hdaps->eventbox), hdaps->hvbox);
218
219 /* We only change the icon when the status has changed,
220 so it's important that they start out n*sync. */
221 hdaps->previous_status = HDAPS_OFF;
222 hdaps_set_icon(hdaps, HDAPS_OFF);
223
224 /* Create the tooltip widget, and set its initial value.
225 * The first couple of lines are stolen from the battery
226 * status plugin. I make no claim as to their correctness.
227 */
228 hdaps->tooltip = gtk_tooltips_new();
229 g_object_ref(G_OBJECT(hdaps->tooltip));
230 gtk_object_sink(GTK_OBJECT(hdaps->tooltip));
231 hdaps_set_tooltip(hdaps, HDAPS_OFF);
232
233 return hdaps;
234 }
235
236
237
238 static void hdaps_free(XfcePanelPlugin *plugin,
239 HdapsPlugin *hdaps) {
240
241 GtkWidget *dialog;
242
243 /* Destroy the dialog if it's still open. */
244 dialog = g_object_get_data(G_OBJECT(plugin), "dialog");
245
246 if (G_UNLIKELY(dialog != NULL)) {
247 gtk_widget_destroy(dialog);
248 }
249
250 /* Destroy the panel widgets. When dumb comments like these
251 are left over, they're from the sample panel applet,
252 I swear. */
253 gtk_widget_destroy(hdaps->hvbox);
254
255 /* Remove the timeout that was set during creation. */
256 if (hdaps->timeout) {
257 g_source_remove(hdaps->timeout);
258 }
259
260 /* And free the string (if any) containing
261 the device name. */
262 if (G_LIKELY(hdaps->device_name != NULL)) {
263 g_free(hdaps->device_name);
264 }
265
266 /* Goodbye, tooltips. */
267 gtk_tooltips_set_tip(hdaps->tooltip, hdaps->eventbox, NULL, NULL);
268 g_object_unref(G_OBJECT(hdaps->tooltip));
269
270 /* ...and finally free the plugin structure. */
271 panel_slice_free(HdapsPlugin, hdaps);
272 }
273
274
275
276 static void hdaps_orientation_changed(XfcePanelPlugin *plugin,
277 GtkOrientation orientation,
278 HdapsPlugin *hdaps) {
279
280 /* Change the plugin's orientation. Basically magic to me. */
281 xfce_hvbox_set_orientation(XFCE_HVBOX(hdaps->hvbox), orientation);
282 }
283
284
285
286 static gboolean hdaps_size_changed(XfcePanelPlugin *plugin,
287 gint size,
288 HdapsPlugin *hdaps) {
289
290 GtkOrientation orientation;
291
292 /* Get the current orientation of the plugin. */
293 orientation = xfce_panel_plugin_get_orientation(plugin);
294
295 /* We want to make the widget "bigger" in the direction
296 of its orientation. Or is it the other way around? */
297 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
298 gtk_widget_set_size_request(GTK_WIDGET(plugin), -1, size);
299 }
300 else {
301 gtk_widget_set_size_request(GTK_WIDGET(plugin), size, -1);
302 }
303
304 /* We handled the change, so we're supposed to return TRUE. */
305 return TRUE;
306 }
307
308
309
310 static gboolean hdaps_update_status(HdapsPlugin *hdaps) {
311 /* This checks the status of HDAPS and updates the
312 widget accordingly. */
313
314 /* This just gets the status. */
315 int status = parse_int_from_file(hdaps->sysfs_file);
316
317 if (status != hdaps->previous_status) {
318 /* And we only update the icon if we need to. */
319 hdaps_set_icon(hdaps, status);
320 hdaps_set_tooltip(hdaps, status);
321 hdaps->previous_status = status;
322 }
323
324 return TRUE;
325 }
326
327
328
329 void hdaps_reset_timeout(HdapsPlugin *hdaps) {
330 /* Remove, and then re-set the timeout function. Useful
331 for changing the poll frequency. */
332 if (hdaps->timeout) {
333 g_source_remove(hdaps->timeout);
334 }
335
336 hdaps->timeout = g_timeout_add(hdaps->poll_frequency, (GSourceFunc)hdaps_update_status, hdaps);
337 }
338
339
340
341 static void hdaps_construct(XfcePanelPlugin *plugin) {
342 HdapsPlugin *hdaps;
343
344 /* Set the "translation domain". I don't know what that does. */
345 xfce_textdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
346
347 /* First, create the plugin. */
348 hdaps = hdaps_new(plugin);
349
350 /* Add the plugin's eventbox to the panel. */
351 gtk_container_add(GTK_CONTAINER(plugin), hdaps->eventbox);
352
353 /* This configures the right-click menu to appear
354 on the plugin's eventbox. */
355 xfce_panel_plugin_add_action_widget(plugin, hdaps->eventbox);
356
357 /* Connect the common event handlers. */
358 g_signal_connect(G_OBJECT(plugin), "free-data",
359 G_CALLBACK(hdaps_free), hdaps);
360
361 g_signal_connect (G_OBJECT(plugin), "save",
362 G_CALLBACK(hdaps_save), hdaps);
363
364 g_signal_connect (G_OBJECT(plugin), "size-changed",
365 G_CALLBACK(hdaps_size_changed), hdaps);
366
367 g_signal_connect (G_OBJECT(plugin), "orientation-changed",
368 G_CALLBACK(hdaps_orientation_changed), hdaps);
369
370 /* Show the "configure" right-click menu item, and
371 connect its event handler. */
372 xfce_panel_plugin_menu_show_configure(plugin);
373 g_signal_connect(G_OBJECT(plugin), "configure-plugin",
374 G_CALLBACK(hdaps_configure), hdaps);
375
376 /* Show the "about" right-click menu item, and
377 connect its event handler. */
378 xfce_panel_plugin_menu_show_about(plugin);
379 g_signal_connect(G_OBJECT(plugin), "about",
380 G_CALLBACK(hdaps_about), hdaps);
381
382 /* Set the timeout for the function which checks the
383 HDAPS status. */
384 hdaps_reset_timeout(hdaps);
385
386 return;
387 }