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_ERROR
) {
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
);
111 void hdaps_set_tooltip(HdapsPlugin
*hdaps
, int status
) {
113 if (status
== HDAPS_ERROR
) {
114 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS Error", NULL
);
116 else if (status
== HDAPS_OFF
) {
117 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS Off", NULL
);
120 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, "HDAPS On", NULL
);
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
;
133 static void hdaps_read(HdapsPlugin
*hdaps
) {
137 const gchar
*saved_device_name
;
139 /* Get the plugin config file location. XFCE should know this. */
140 file
= xfce_panel_plugin_save_location(hdaps
->plugin
, TRUE
);
142 if (G_UNLIKELY(file
== NULL
)) {
143 DBG("Something went wrong getting the configuration file location.");
144 DBG("Retaining default settings.");
148 /* Open the config file read-only. */
149 rc
= xfce_rc_simple_open(file
, TRUE
);
151 /* Don't need this anymore if we've got a file handle. */
154 if (G_UNLIKELY(rc
== NULL
)) {
155 DBG("There was an error opening the configuration file.");
156 DBG("Retaining default settings.");
160 /* Read the settings, one at a time. */
162 /* We use saved_device_name here because we need
163 to dupe the string after we read it in from the
165 saved_device_name
= xfce_rc_read_entry(rc
,
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
);
171 /* Integers are easier. */
172 hdaps
->poll_frequency
= xfce_rc_read_int_entry(rc
,
174 DEFAULT_POLL_FREQUENCY
);
176 /* And close the config file. */
183 static HdapsPlugin
*hdaps_new(XfcePanelPlugin
*plugin
) {
185 GtkOrientation orientation
;
187 /* Allocate memory for the plugin struct, and zero it. */
188 hdaps
= panel_slice_new0(HdapsPlugin
);
190 /* The HdapsPlugin gets a copy of the XfcePanelPlugin. */
191 hdaps
->plugin
= plugin
;
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
196 hdaps_set_defaults(hdaps
);
198 /* Read any user settings into the HdapsPlugin. */
201 /* Get the current orientation. Magic. */
202 orientation
= xfce_panel_plugin_get_orientation(plugin
);
204 /* This creates the event box and shows it. This
208 b) Interact with it via right-click, etc.
210 hdaps
->eventbox
= gtk_event_box_new();
211 gtk_widget_show(hdaps
->eventbox
);
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
);
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
);
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.
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
);
238 static void hdaps_free(XfcePanelPlugin
*plugin
,
239 HdapsPlugin
*hdaps
) {
243 /* Destroy the dialog if it's still open. */
244 dialog
= g_object_get_data(G_OBJECT(plugin
), "dialog");
246 if (G_UNLIKELY(dialog
!= NULL
)) {
247 gtk_widget_destroy(dialog
);
250 /* Destroy the panel widgets. When dumb comments like these
251 are left over, they're from the sample panel applet,
253 gtk_widget_destroy(hdaps
->hvbox
);
255 /* Remove the timeout that was set during creation. */
256 if (hdaps
->timeout
) {
257 g_source_remove(hdaps
->timeout
);
260 /* And free the string (if any) containing
262 if (G_LIKELY(hdaps
->device_name
!= NULL
)) {
263 g_free(hdaps
->device_name
);
266 /* Goodbye, tooltips. */
267 gtk_tooltips_set_tip(hdaps
->tooltip
, hdaps
->eventbox
, NULL
, NULL
);
268 g_object_unref(G_OBJECT(hdaps
->tooltip
));
270 /* ...and finally free the plugin structure. */
271 panel_slice_free(HdapsPlugin
, hdaps
);
276 static void hdaps_orientation_changed(XfcePanelPlugin
*plugin
,
277 GtkOrientation orientation
,
278 HdapsPlugin
*hdaps
) {
280 /* Change the plugin's orientation. Basically magic to me. */
281 xfce_hvbox_set_orientation(XFCE_HVBOX(hdaps
->hvbox
), orientation
);
286 static gboolean
hdaps_size_changed(XfcePanelPlugin
*plugin
,
288 HdapsPlugin
*hdaps
) {
290 GtkOrientation orientation
;
292 /* Get the current orientation of the plugin. */
293 orientation
= xfce_panel_plugin_get_orientation(plugin
);
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
);
301 gtk_widget_set_size_request(GTK_WIDGET(plugin
), size
, -1);
304 /* We handled the change, so we're supposed to return TRUE. */
310 static gboolean
hdaps_update_status(HdapsPlugin
*hdaps
) {
311 /* This checks the status of HDAPS and updates the
312 widget accordingly. */
314 /* This just gets the status. */
315 int status
= parse_int_from_file(hdaps
->sysfs_file
);
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
;
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
);
336 hdaps
->timeout
= g_timeout_add(hdaps
->poll_frequency
, (GSourceFunc
)hdaps_update_status
, hdaps
);
341 static void hdaps_construct(XfcePanelPlugin
*plugin
) {
344 /* Set the "translation domain". I don't know what that does. */
345 xfce_textdomain(GETTEXT_PACKAGE
, PACKAGE_LOCALE_DIR
, "UTF-8");
347 /* First, create the plugin. */
348 hdaps
= hdaps_new(plugin
);
350 /* Add the plugin's eventbox to the panel. */
351 gtk_container_add(GTK_CONTAINER(plugin
), hdaps
->eventbox
);
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
);
357 /* Connect the common event handlers. */
358 g_signal_connect(G_OBJECT(plugin
), "free-data",
359 G_CALLBACK(hdaps_free
), hdaps
);
361 g_signal_connect (G_OBJECT(plugin
), "save",
362 G_CALLBACK(hdaps_save
), hdaps
);
364 g_signal_connect (G_OBJECT(plugin
), "size-changed",
365 G_CALLBACK(hdaps_size_changed
), hdaps
);
367 g_signal_connect (G_OBJECT(plugin
), "orientation-changed",
368 G_CALLBACK(hdaps_orientation_changed
), hdaps
);
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
);
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
);
382 /* Set the timeout for the function which checks the
384 hdaps_reset_timeout(hdaps
);