2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
4 * Copyright (C) 2019 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 Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (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 Affero General Public License for more details:
18 * https://www.gnu.org/licenses/agpl-3.0.html
28 #include <libxfce4ui/libxfce4ui.h>
29 #include <libxfce4panel/xfce-panel-plugin.h>
30 #include "xfce4-hdaps-dialogs.h"
33 /* Wild guess. Actually borrowed from some code
34 I copy-and-pasted. Doesn't seem too ugly. */
35 #define DEFAULT_BORDER_WIDTH 8
37 /* If people can't read the README, well maybe
38 * they can do it /online/.
40 #define PLUGIN_WEBSITE "http://michael.orlitzky.com/code/xfce4-hdaps.xhtml"
43 static void hdaps_configure_response(GtkWidget
*dialog
,
47 if (response
== GTK_RESPONSE_HELP
) {
48 /* Launch the user's web browser and direct them to the plugin webpage. */
49 gboolean spawn_result
= g_spawn_command_line_async("xdg-open " 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_text_get_active_text((GtkComboBoxText
*)hdaps
->cb_device_name
);
65 snprintf(hdaps
->sysfs_file
,
69 hdaps_save(hdaps
->plugin
, hdaps
);
70 hdaps_reset_timeout(hdaps
);
74 /* Since there is already a "save" button, we should ignore
75 any changes that were made if the user presses "cancel"
77 g_object_set_data(G_OBJECT(hdaps
->plugin
), "dialog", NULL
);
78 xfce_panel_plugin_unblock_menu(hdaps
->plugin
);
79 gtk_widget_destroy(dialog
);
86 void hdaps_configure(XfcePanelPlugin
*plugin
,
89 /* Here comes a bunch of GTK garbage to create the
98 /* Block the plugin menu while the configuration
99 dialogue is open. Don't forget to unblock it before
101 xfce_panel_plugin_block_menu(plugin
);
103 /* Create the dialog */
104 dialog
= xfce_titled_dialog_new_with_buttons(
106 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(plugin
))),
107 GTK_DIALOG_DESTROY_WITH_PARENT
,
108 "gtk-help", GTK_RESPONSE_HELP
,
109 "gtk-save", GTK_RESPONSE_OK
,
110 "gtk-cancel", GTK_RESPONSE_CANCEL
,
113 /* Center the dialog on screen. */
114 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_CENTER
);
116 /* Set dialog icon to a generic settings icon. */
117 gtk_window_set_icon_name(GTK_WINDOW(dialog
), "xfce4-settings");
119 /* Link the dialog to the plugin, so we can destroy it in case
120 * the plugin is closed while the dialog is still open. */
121 g_object_set_data(G_OBJECT(plugin
), "dialog", dialog
);
123 /* Connect the reponse signal to the dialog. */
124 g_signal_connect(G_OBJECT (dialog
), "response",
125 G_CALLBACK(hdaps_configure_response
), hdaps
);
128 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, DEFAULT_BORDER_WIDTH
);
129 gtk_container_set_border_width(GTK_CONTAINER(vbox
), DEFAULT_BORDER_WIDTH
- 2);
130 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog
))),
131 vbox
, TRUE
, TRUE
, 0);
133 /* Create size group to keep widgets aligned */
134 sg
= gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL
);
136 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, DEFAULT_BORDER_WIDTH
);
137 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
139 label
= gtk_label_new(_("Poll Frequency:"));
140 gtk_size_group_add_widget(sg
, label
);
141 gtk_widget_set_halign(label
, 0.0);
142 gtk_widget_set_valign(label
, 0.5);
143 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
145 hdaps
->sb_poll_frequency
= gtk_spin_button_new_with_range(100, 5000, 100);
146 gtk_spin_button_set_value((GtkSpinButton
*)hdaps
->sb_poll_frequency
,
147 hdaps
->poll_frequency
);
149 gtk_box_pack_start(GTK_BOX(hbox
),
150 hdaps
->sb_poll_frequency
,
155 /* Create the device name dropdown. */
156 hbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, DEFAULT_BORDER_WIDTH
);
157 gtk_box_pack_start(GTK_BOX(vbox
), hbox
, FALSE
, FALSE
, 0);
159 label
= gtk_label_new(_("Device Name:"));
160 gtk_size_group_add_widget(sg
, label
);
161 gtk_widget_set_halign(label
, 0.0);
162 gtk_widget_set_valign(label
, 0.5);
163 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, FALSE
, 0);
165 hdaps
->cb_device_name
= gtk_combo_box_text_new();
167 /* Add the current device name, it should always be available.
168 Oh, and it should be the default, too. */
169 gtk_combo_box_text_append_text((GtkComboBoxText
*)hdaps
->cb_device_name
,
172 /* This function takes an index, and 0 should be the first
173 (only) index at this point. */
174 gtk_combo_box_set_active((GtkComboBox
*)hdaps
->cb_device_name
, 0);
176 /* Now loop through the list of available devices, adding each
177 to the list as we go. */
178 char hdaps_devices
[MAX_HDAPS_DEVICES
][FILENAME_MAX
];
179 int found_devices
= get_hdaps_device_list(hdaps_devices
);
182 for (list_idx
= 0; list_idx
< found_devices
; list_idx
++) {
183 /* We don't want to add duplicate entries to the combo box.
184 However, at this point, the current device name should be
185 the only entry. Therefore, to avoid duplicates, we only
186 have to avoid adding the current device name a second time. */
187 if (strcmp(hdaps_devices
[list_idx
], hdaps
->device_name
) != 0) {
188 gtk_combo_box_text_append_text((GtkComboBoxText
*)hdaps
->cb_device_name
,
189 hdaps_devices
[list_idx
]);
193 gtk_box_pack_start(GTK_BOX(hbox
), hdaps
->cb_device_name
, FALSE
, FALSE
, 0);
195 /* Show the dialog and all of its widgets. */
196 gtk_widget_show_all(dialog
);
202 /* URI (http(s):// and mailto://) handler used on the About dialog. */
203 static void hdaps_uri_handler(GtkAboutDialog
*about
,
207 gchar
*cmd
= g_strdup_printf("%s %s","xdg-open", uri
);
208 gboolean spawn_result
= g_spawn_command_line_async(cmd
, NULL
);
210 if (G_UNLIKELY(spawn_result
== FALSE
)) {
211 g_warning(_("Unable to open the following uri: %s"), uri
);
217 /* "Close" button handler for the About dialog. */
218 static void hdaps_close_handler (GtkAboutDialog
*about
,
220 gpointer user_data
) {
221 /* This will cause the dialog to be destroyed */
222 gtk_widget_destroy(GTK_WIDGET(about
));
225 void hdaps_about(XfcePanelPlugin
*plugin
) {
226 const gchar
*authors
[] = { "Michael Orlitzky <michael@orlitzky.com>", NULL
};
227 const gchar
*copyright
= "Copyright \302\251 2019 Michael Orlitzky";
229 GtkAboutDialog
*about
= GTK_ABOUT_DIALOG(gtk_about_dialog_new());
231 gtk_about_dialog_set_authors(about
, authors
);
232 gtk_about_dialog_set_copyright(about
, copyright
);
233 gtk_about_dialog_set_program_name(about
, PACKAGE_NAME
);
234 gtk_about_dialog_set_logo_icon_name(about
, PACKAGE_NAME
);
235 gtk_about_dialog_set_license(about
, LICENSE_AGPL3
);
236 gtk_about_dialog_set_version(about
, PACKAGE_VERSION
);
237 gtk_about_dialog_set_website(about
, PLUGIN_WEBSITE
);
238 gtk_about_dialog_set_website_label(about
, "xfce4-hdaps homepage");
240 /* Use a custom URI handler for http(s):// and mailto:// URIs. The
241 default behavior apparently needs gio/gvfs to work, so we want
242 to add some fallbacks. */
243 g_signal_connect(about
,
245 G_CALLBACK(hdaps_uri_handler
),
248 /* To close the dialog when "close" is clicked, we have to connect
249 * the response signal to a handler that actually does it.
251 g_signal_connect(about
,
253 G_CALLBACK(hdaps_close_handler
),
255 gtk_widget_show(GTK_WIDGET(about
));