2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
4 * Copyright Michael Orlitzky
6 * http://michael.orlitzky.com/
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * http://www.fsf.org/licensing/licenses/gpl.html
27 #include <libxfce4ui/libxfce4ui.h>
28 #include <libxfce4panel/xfce-panel-plugin.h>
29 #include "xfce4-hdaps-dialogs.h"
32 /* Wild guess. Actually borrowed from some code
33 I copy-and-pasted. Doesn't seem too ugly. */
34 #define DEFAULT_BORDER_WIDTH 8
36 /* If people can't read the README, well maybe
37 * they can do it /online/.
39 #define PLUGIN_WEBSITE "http://michael.orlitzky.com/code/xfce4-hdaps.php"
42 static void hdaps_configure_response(GtkWidget
*dialog
,
46 if (response
== GTK_RESPONSE_HELP
) {
47 /* Launch the user's web browser and direct them to
48 the plugin's webpage. */
49 gboolean spawn_result
= g_spawn_command_line_async("exo-open --launch WebBrowser " PLUGIN_WEBSITE
, NULL
);
51 if (G_UNLIKELY(spawn_result
== FALSE
)) {
52 g_warning(_("Unable to open the following url: %s"), PLUGIN_WEBSITE
);
59 if (response
== GTK_RESPONSE_OK
) {
60 /* This corresponds to the "Save" button, so we
61 want to save any settings that may have changed. */
62 g_object_set_data(G_OBJECT(hdaps
->plugin
), "dialog", NULL
);
63 hdaps
->poll_frequency
= gtk_spin_button_get_value_as_int((GtkSpinButton
*)hdaps
->sb_poll_frequency
);
64 hdaps
->device_name
= gtk_combo_box_get_active_text((GtkComboBox
*)hdaps
->cb_device_name
);
65 snprintf(hdaps
->sysfs_file
, FILENAME_MAX
, UNLOAD_HEADS_FMT
, hdaps
->device_name
);
66 hdaps_save(hdaps
->plugin
, hdaps
);
67 hdaps_reset_timeout(hdaps
);
71 /* Since there is already a "save" button, we should ignore
72 any changes that were made if the user presses "cancel"
74 g_object_set_data(G_OBJECT(hdaps
->plugin
), "dialog", NULL
);
75 xfce_panel_plugin_unblock_menu(hdaps
->plugin
);
76 gtk_widget_destroy(dialog
);
83 void hdaps_configure(XfcePanelPlugin
*plugin
,
86 /* Here comes a bunch of GTK garbage to create the
95 /* Block the plugin menu while the configuration
96 dialogue is open. Don't forget to unblock it before
98 xfce_panel_plugin_block_menu(plugin
);
100 /* Create the dialog */
101 dialog
= xfce_titled_dialog_new_with_buttons(_("Hdaps Plugin"),
102 GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (plugin
))),
103 GTK_DIALOG_DESTROY_WITH_PARENT
| GTK_DIALOG_NO_SEPARATOR
,
104 GTK_STOCK_HELP
, GTK_RESPONSE_HELP
,
105 GTK_STOCK_SAVE
, GTK_RESPONSE_OK
,
106 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
109 /* Center the dialog on screen. */
110 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_CENTER
);
112 /* Set dialog icon to a generic settings icon. */
113 gtk_window_set_icon_name(GTK_WINDOW(dialog
), "xfce4-settings");
115 /* Link the dialog to the plugin, so we can destroy it in case
116 * the plugin is closed while the dialog is still open. */
117 g_object_set_data(G_OBJECT(plugin
), "dialog", dialog
);
119 /* Connect the reponse signal to the dialog. */
120 g_signal_connect(G_OBJECT (dialog
), "response",
121 G_CALLBACK(hdaps_configure_response
), hdaps
);
124 vbox
= gtk_vbox_new(FALSE
, DEFAULT_BORDER_WIDTH
);
125 gtk_container_set_border_width(GTK_CONTAINER(vbox
), DEFAULT_BORDER_WIDTH
- 2);
126 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog
)->vbox
), vbox
, TRUE
, TRUE
, 0);
128 /* Create size group to keep widgets aligned */
129 sg
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
131 hbox
= gtk_hbox_new(FALSE
, DEFAULT_BORDER_WIDTH
);
132 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
134 label
= gtk_label_new(_("Poll Frequency:"));
135 gtk_size_group_add_widget(sg
, label
);
136 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
137 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
139 hdaps
->sb_poll_frequency
= gtk_spin_button_new_with_range(100, 5000, 100);
140 gtk_spin_button_set_value((GtkSpinButton
*)hdaps
->sb_poll_frequency
, hdaps
->poll_frequency
);
142 gtk_box_pack_start(GTK_BOX(hbox
),
143 hdaps
->sb_poll_frequency
,
148 /* Create the device name dropdown. */
149 hbox
= gtk_hbox_new(FALSE
, DEFAULT_BORDER_WIDTH
);
150 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
152 label
= gtk_label_new(_("Device Name:"));
153 gtk_size_group_add_widget(sg
, label
);
154 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
155 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
157 hdaps
->cb_device_name
= gtk_combo_box_new_text();
159 /* Add the current device name, it should always be available.
160 Oh, and it should be the default, too. */
161 gtk_combo_box_append_text((GtkComboBox
*)hdaps
->cb_device_name
, hdaps
->device_name
);
163 /* This function takes an index, and 0 should be the first
164 (only) index at this point. */
165 gtk_combo_box_set_active((GtkComboBox
*)hdaps
->cb_device_name
, 0);
167 /* Now loop through the list of available devices, adding each
168 to the list as we go. */
169 char hdaps_devices
[MAX_HDAPS_DEVICES
][FILENAME_MAX
];
170 int found_devices
= get_hdaps_device_list(hdaps_devices
);
173 for (list_idx
= 0; list_idx
< found_devices
; list_idx
++) {
174 /* We don't want to add duplicate entries to the combo box.
175 However, at this point, the current device name should be
176 the only entry. Therefore, to avoid duplicates, we only
177 have to avoid adding the current device name a second time. */
178 if (strcmp(hdaps_devices
[list_idx
], hdaps
->device_name
) != 0) {
179 gtk_combo_box_append_text((GtkComboBox
*)hdaps
->cb_device_name
, hdaps_devices
[list_idx
]);
183 gtk_box_pack_start(GTK_BOX(hbox
), hdaps
->cb_device_name
, FALSE
, FALSE
, 0);
185 /* Show the dialog and all of its widgets. */
186 gtk_widget_show_all(dialog
);
192 /* URL handler used on the About dialog. */
193 static void hdaps_url_handler(GtkAboutDialog
*about
,
198 cmd
= g_strdup_printf("%s %s","xdg-open", link
);
200 /* Stolen from xfce4-power-manager. */
201 if (!g_spawn_command_line_async(cmd
, NULL
)) {
203 cmd
= g_strdup_printf("%s %s","xfbrowser4", link
);
204 g_spawn_command_line_async(cmd
, NULL
);
210 /* Email address handler used on the About dialog. */
211 static void hdaps_mailto_handler(GtkAboutDialog
*about
,
214 gchar
*cmd
= g_strdup_printf( "%s %s", "xdg-email", link
);
216 g_spawn_command_line_async(cmd
, NULL
);
221 void hdaps_about(XfcePanelPlugin
*plugin
) {
222 const gchar
*authors
[] = { "Michael Orlitzky <michael@orlitzky.com>", NULL
};
223 const gchar
*copyright
= "Copyright \302\251 2012 Michael Orlitzky";
225 gtk_about_dialog_set_url_hook(hdaps_url_handler
, NULL
, NULL
);
226 gtk_about_dialog_set_email_hook(hdaps_mailto_handler
, NULL
, NULL
);
228 gtk_show_about_dialog(NULL
,
230 "copyright", copyright
,
231 "destroy-with-parent", TRUE
,
232 "license", LICENSE_GPL3
,
233 "logo-icon-name", PACKAGE_NAME
,
234 "icon-name", PACKAGE_NAME
,
235 "name", PACKAGE_NAME
,
236 "version", PACKAGE_VERSION
,
237 "website", PLUGIN_WEBSITE
,
238 "website-label", "xfce4-hdaps homepage",