]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/xfce4-hdaps.c
Initial commit.
[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_OFF) {
73 /* Error */
74 icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
75 "dialog-error",
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 static void hdaps_set_defaults(HdapsPlugin *hdaps) {
111 DBG("Configuring all settings to defaults.");
112 hdaps->device_name = g_strdup(DEFAULT_DEVICE_NAME);
113 snprintf(hdaps->sysfs_file, FILENAME_MAX, UNLOAD_HEADS_FMT, hdaps->device_name);
114 hdaps->poll_frequency = DEFAULT_POLL_FREQUENCY;
115 }
116
117
118 static void hdaps_read(HdapsPlugin *hdaps) {
119
120 XfceRc *rc;
121 gchar *file;
122 const gchar *saved_device_name;
123
124 /* Get the plugin config file location. XFCE should know this. */
125 file = xfce_panel_plugin_save_location(hdaps->plugin, TRUE);
126
127 if (G_UNLIKELY(file == NULL)) {
128 DBG("Something went wrong getting the configuration file location.");
129 DBG("Retaining default settings.");
130 return;
131 }
132
133 /* Open the config file read-only. */
134 rc = xfce_rc_simple_open(file, TRUE);
135
136 /* Don't need this anymore if we've got a file handle. */
137 g_free(file);
138
139 if (G_UNLIKELY(rc == NULL)) {
140 DBG("There was an error opening the configuration file.");
141 DBG("Retaining default settings.");
142 return;
143 }
144
145 /* Read the settings, one at a time. */
146
147 /* We use saved_device_name here because we need
148 to dupe the string after we read it in from the
149 config file. */
150 saved_device_name = xfce_rc_read_entry(rc,
151 "device_name",
152 DEFAULT_DEVICE_NAME);
153 hdaps->device_name = g_strdup(saved_device_name);
154 snprintf(hdaps->sysfs_file, FILENAME_MAX, UNLOAD_HEADS_FMT, hdaps->device_name);
155
156 /* Integers are easier. */
157 hdaps->poll_frequency = xfce_rc_read_int_entry(rc,
158 "poll_frequency",
159 DEFAULT_POLL_FREQUENCY);
160
161 /* And close the config file. */
162 xfce_rc_close(rc);
163 }
164
165
166
167
168 static HdapsPlugin *hdaps_new(XfcePanelPlugin *plugin) {
169 HdapsPlugin *hdaps;
170 GtkOrientation orientation;
171
172 /* Allocate memory for the plugin struct, and zero it. */
173 hdaps = panel_slice_new0(HdapsPlugin);
174
175 /* The HdapsPlugin gets a copy of the XfcePanelPlugin. */
176 hdaps->plugin = plugin;
177
178 /* Set default values right before reading in the user's settings.
179 This way, hdaps_read() doesn't have to set defaults on error
180 conditions. */
181 hdaps_set_defaults(hdaps);
182
183 /* Read any user settings into the HdapsPlugin. */
184 hdaps_read(hdaps);
185
186 /* Get the current orientation. Magic. */
187 orientation = xfce_panel_plugin_get_orientation(plugin);
188
189 /* This creates the event box and shows it. This
190 is necessary to,
191
192 a) See the widget
193 b) Interact with it via right-click, etc.
194 */
195 hdaps->eventbox = gtk_event_box_new();
196 gtk_widget_show(hdaps->eventbox);
197
198 /* Set up the hvbox for the widget, which supports
199 both horizontal and vertical (hv) orientations. */
200 hdaps->hvbox = xfce_hvbox_new(orientation, FALSE, 2);
201 gtk_widget_show(hdaps->hvbox);
202 gtk_container_add(GTK_CONTAINER(hdaps->eventbox), hdaps->hvbox);
203
204 /* We only change the icon when the status has changed,
205 so it's important that they start out n*sync. */
206 hdaps->previous_status = HDAPS_OFF;
207 hdaps_set_icon(hdaps, HDAPS_OFF);
208
209 return hdaps;
210 }
211
212
213
214 static void hdaps_free(XfcePanelPlugin *plugin,
215 HdapsPlugin *hdaps) {
216
217 GtkWidget *dialog;
218
219 /* Destroy the dialog if it's still open. */
220 dialog = g_object_get_data(G_OBJECT(plugin), "dialog");
221
222 if (G_UNLIKELY(dialog != NULL)) {
223 gtk_widget_destroy(dialog);
224 }
225
226 /* Destroy the panel widgets. When dumb comments like these
227 are left over, they're from the sample panel applet,
228 I swear. */
229 gtk_widget_destroy(hdaps->hvbox);
230
231 /* Remove the timeout that was set during creation. */
232 if (hdaps->timeout) {
233 g_source_remove(hdaps->timeout);
234 }
235
236 /* And free the string (if any) containing
237 the device name. */
238 if (G_LIKELY(hdaps->device_name != NULL)) {
239 g_free(hdaps->device_name);
240 }
241
242 /* ...and finally free the plugin structure. */
243 panel_slice_free(HdapsPlugin, hdaps);
244 }
245
246
247
248 static void hdaps_orientation_changed(XfcePanelPlugin *plugin,
249 GtkOrientation orientation,
250 HdapsPlugin *hdaps) {
251
252 /* Change the plugin's orientation. Basically magic to me. */
253 xfce_hvbox_set_orientation(XFCE_HVBOX(hdaps->hvbox), orientation);
254 }
255
256
257
258 static gboolean hdaps_size_changed(XfcePanelPlugin *plugin,
259 gint size,
260 HdapsPlugin *hdaps) {
261
262 GtkOrientation orientation;
263
264 /* Get the current orientation of the plugin. */
265 orientation = xfce_panel_plugin_get_orientation(plugin);
266
267 /* We want to make the widget "bigger" in the direction
268 of its orientation. Or is it the other way around? */
269 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
270 gtk_widget_set_size_request(GTK_WIDGET(plugin), -1, size);
271 }
272 else {
273 gtk_widget_set_size_request(GTK_WIDGET(plugin), size, -1);
274 }
275
276 /* We handled the change, so we're supposed to return TRUE. */
277 return TRUE;
278 }
279
280
281
282 static gboolean hdaps_update_status(HdapsPlugin *hdaps) {
283 /* This checks the status of HDAPS and updates the
284 widget accordingly. */
285
286 /* This just gets the status. */
287 int status = parse_int_from_file(hdaps->sysfs_file);
288
289 if (status != hdaps->previous_status) {
290 /* And we only update the icon if we need to. */
291 hdaps_set_icon(hdaps, status);
292 hdaps->previous_status = status;
293 }
294
295 return TRUE;
296 }
297
298
299
300 void hdaps_reset_timeout(HdapsPlugin *hdaps) {
301 /* Remove, and then re-set the timeout function. Useful
302 for changing the poll frequency. */
303 if (hdaps->timeout) {
304 g_source_remove(hdaps->timeout);
305 }
306
307 hdaps->timeout = g_timeout_add(hdaps->poll_frequency, (GSourceFunc)hdaps_update_status, hdaps);
308 }
309
310
311
312 static void hdaps_construct(XfcePanelPlugin *plugin) {
313 HdapsPlugin *hdaps;
314
315 /* Set the "translation domain". I don't know what that does. */
316 xfce_textdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
317
318 /* First, create the plugin. */
319 hdaps = hdaps_new(plugin);
320
321 /* Add the plugin's eventbox to the panel. */
322 gtk_container_add(GTK_CONTAINER(plugin), hdaps->eventbox);
323
324 /* This configures the right-click menu to appear
325 on the plugin's eventbox. */
326 xfce_panel_plugin_add_action_widget(plugin, hdaps->eventbox);
327
328 /* Connect the common event handlers. */
329 g_signal_connect(G_OBJECT(plugin), "free-data",
330 G_CALLBACK(hdaps_free), hdaps);
331
332 g_signal_connect (G_OBJECT(plugin), "save",
333 G_CALLBACK(hdaps_save), hdaps);
334
335 g_signal_connect (G_OBJECT(plugin), "size-changed",
336 G_CALLBACK(hdaps_size_changed), hdaps);
337
338 g_signal_connect (G_OBJECT(plugin), "orientation-changed",
339 G_CALLBACK(hdaps_orientation_changed), hdaps);
340
341 /* Show the "configure" right-click menu item, and
342 connect its event handler. */
343 xfce_panel_plugin_menu_show_configure(plugin);
344 g_signal_connect(G_OBJECT(plugin), "configure-plugin",
345 G_CALLBACK(hdaps_configure), hdaps);
346
347 /* Show the "about" right-click menu item, and
348 connect its event handler. */
349 xfce_panel_plugin_menu_show_about(plugin);
350 g_signal_connect(G_OBJECT(plugin), "about",
351 G_CALLBACK(hdaps_about), hdaps);
352
353 /* Set the timeout for the function which checks the
354 HDAPS status. */
355 hdaps_reset_timeout(hdaps);
356
357 return;
358 }