5 #include "xfce4-hdaps.h"
6 #include "xfce4-hdaps-dialogs.h"
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"
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
18 /* Prototype, needed to register below. */
19 static void hdaps_construct(XfcePanelPlugin
*plugin
);
21 /* Register the plugin with the panel. */
22 XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL(hdaps_construct
);
25 void hdaps_save(XfcePanelPlugin
*plugin
, HdapsPlugin
*hdaps
) {
30 /* Get the config file location. XFCE should know this. */
31 file
= xfce_panel_plugin_save_location(plugin
, TRUE
);
33 if (G_UNLIKELY(file
== NULL
)) {
34 DBG("Failed to find the configuration file. Bailing.");
38 /* Open the config file read/write. */
39 rc
= xfce_rc_simple_open(file
, FALSE
);
41 /* And we can free the file path now that we've
45 if (G_UNLIKELY(rc
== NULL
)) {
46 DBG("Failed to open the configuration file. Bailing.");
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
);
56 xfce_rc_write_int_entry(rc
, "poll_frequency", hdaps
->poll_frequency
);
58 /* close the rc file */
64 void hdaps_set_icon(HdapsPlugin
*hdaps
, int status
) {
68 /* Panel info magic. */
69 size
= xfce_panel_plugin_get_size(hdaps
->plugin
) - 6;
71 /* Try to load an icon from the current icon theme. */
72 if (status
< HDAPS_OFF
) {
74 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
78 else if (status
== HDAPS_OFF
) {
79 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
84 /* status > HDAPS_OFF means it's on. */
85 icon
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
90 /* Get rid of the previous icon. */
92 gtk_widget_destroy(hdaps
->icon
);
96 hdaps
->icon
= gtk_image_new_from_pixbuf(icon
);
97 g_object_unref(G_OBJECT(icon
));
100 hdaps
->icon
= gtk_image_new_from_icon_name("dialog-warning", GTK_ICON_SIZE_BUTTON
);
103 gtk_box_pack_start(GTK_BOX(hdaps
->hvbox
), GTK_WIDGET(hdaps
->icon
), FALSE
, FALSE
, 0);
104 gtk_widget_show(hdaps
->icon
);
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
;
118 static void hdaps_read(HdapsPlugin
*hdaps
) {
122 const gchar
*saved_device_name
;
124 /* Get the plugin config file location. XFCE should know this. */
125 file
= xfce_panel_plugin_save_location(hdaps
->plugin
, TRUE
);
127 if (G_UNLIKELY(file
== NULL
)) {
128 DBG("Something went wrong getting the configuration file location.");
129 DBG("Retaining default settings.");
133 /* Open the config file read-only. */
134 rc
= xfce_rc_simple_open(file
, TRUE
);
136 /* Don't need this anymore if we've got a file handle. */
139 if (G_UNLIKELY(rc
== NULL
)) {
140 DBG("There was an error opening the configuration file.");
141 DBG("Retaining default settings.");
145 /* Read the settings, one at a time. */
147 /* We use saved_device_name here because we need
148 to dupe the string after we read it in from the
150 saved_device_name
= xfce_rc_read_entry(rc
,
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
);
156 /* Integers are easier. */
157 hdaps
->poll_frequency
= xfce_rc_read_int_entry(rc
,
159 DEFAULT_POLL_FREQUENCY
);
161 /* And close the config file. */
168 static HdapsPlugin
*hdaps_new(XfcePanelPlugin
*plugin
) {
170 GtkOrientation orientation
;
172 /* Allocate memory for the plugin struct, and zero it. */
173 hdaps
= panel_slice_new0(HdapsPlugin
);
175 /* The HdapsPlugin gets a copy of the XfcePanelPlugin. */
176 hdaps
->plugin
= plugin
;
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
181 hdaps_set_defaults(hdaps
);
183 /* Read any user settings into the HdapsPlugin. */
186 /* Get the current orientation. Magic. */
187 orientation
= xfce_panel_plugin_get_orientation(plugin
);
189 /* This creates the event box and shows it. This
193 b) Interact with it via right-click, etc.
195 hdaps
->eventbox
= gtk_event_box_new();
196 gtk_widget_show(hdaps
->eventbox
);
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
);
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
);
214 static void hdaps_free(XfcePanelPlugin
*plugin
,
215 HdapsPlugin
*hdaps
) {
219 /* Destroy the dialog if it's still open. */
220 dialog
= g_object_get_data(G_OBJECT(plugin
), "dialog");
222 if (G_UNLIKELY(dialog
!= NULL
)) {
223 gtk_widget_destroy(dialog
);
226 /* Destroy the panel widgets. When dumb comments like these
227 are left over, they're from the sample panel applet,
229 gtk_widget_destroy(hdaps
->hvbox
);
231 /* Remove the timeout that was set during creation. */
232 if (hdaps
->timeout
) {
233 g_source_remove(hdaps
->timeout
);
236 /* And free the string (if any) containing
238 if (G_LIKELY(hdaps
->device_name
!= NULL
)) {
239 g_free(hdaps
->device_name
);
242 /* ...and finally free the plugin structure. */
243 panel_slice_free(HdapsPlugin
, hdaps
);
248 static void hdaps_orientation_changed(XfcePanelPlugin
*plugin
,
249 GtkOrientation orientation
,
250 HdapsPlugin
*hdaps
) {
252 /* Change the plugin's orientation. Basically magic to me. */
253 xfce_hvbox_set_orientation(XFCE_HVBOX(hdaps
->hvbox
), orientation
);
258 static gboolean
hdaps_size_changed(XfcePanelPlugin
*plugin
,
260 HdapsPlugin
*hdaps
) {
262 GtkOrientation orientation
;
264 /* Get the current orientation of the plugin. */
265 orientation
= xfce_panel_plugin_get_orientation(plugin
);
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
);
273 gtk_widget_set_size_request(GTK_WIDGET(plugin
), size
, -1);
276 /* We handled the change, so we're supposed to return TRUE. */
282 static gboolean
hdaps_update_status(HdapsPlugin
*hdaps
) {
283 /* This checks the status of HDAPS and updates the
284 widget accordingly. */
286 /* This just gets the status. */
287 int status
= parse_int_from_file(hdaps
->sysfs_file
);
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
;
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
);
307 hdaps
->timeout
= g_timeout_add(hdaps
->poll_frequency
, (GSourceFunc
)hdaps_update_status
, hdaps
);
312 static void hdaps_construct(XfcePanelPlugin
*plugin
) {
315 /* Set the "translation domain". I don't know what that does. */
316 xfce_textdomain(GETTEXT_PACKAGE
, PACKAGE_LOCALE_DIR
, "UTF-8");
318 /* First, create the plugin. */
319 hdaps
= hdaps_new(plugin
);
321 /* Add the plugin's eventbox to the panel. */
322 gtk_container_add(GTK_CONTAINER(plugin
), hdaps
->eventbox
);
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
);
328 /* Connect the common event handlers. */
329 g_signal_connect(G_OBJECT(plugin
), "free-data",
330 G_CALLBACK(hdaps_free
), hdaps
);
332 g_signal_connect (G_OBJECT(plugin
), "save",
333 G_CALLBACK(hdaps_save
), hdaps
);
335 g_signal_connect (G_OBJECT(plugin
), "size-changed",
336 G_CALLBACK(hdaps_size_changed
), hdaps
);
338 g_signal_connect (G_OBJECT(plugin
), "orientation-changed",
339 G_CALLBACK(hdaps_orientation_changed
), hdaps
);
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
);
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
);
353 /* Set the timeout for the function which checks the
355 hdaps_reset_timeout(hdaps
);