]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/xfce4-hdaps-dialogs.c
6a7fff98b41179baae678fb80975403af409794f
[xfce4-hdaps.git] / panel-plugin / xfce4-hdaps-dialogs.c
1 /*
2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
3 *
4 * Copyright (C) 2019 Michael Orlitzky
5 *
6 * http://michael.orlitzky.com/
7 *
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.
12 *
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:
17 *
18 * https://www.gnu.org/licenses/agpl-3.0.html
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <libxfce4ui/libxfce4ui.h>
29 #include <libxfce4panel/xfce-panel-plugin.h>
30 #include "xfce4-hdaps-dialogs.h"
31 #include "hdaps.h"
32
33 /* Wild guess. Actually borrowed from some code
34 I copy-and-pasted. Doesn't seem too ugly. */
35 #define DEFAULT_BORDER_WIDTH 8
36
37 /* If people can't read the README, well maybe
38 * they can do it /online/.
39 */
40 #define PLUGIN_WEBSITE "http://michael.orlitzky.com/code/xfce4-hdaps.xhtml"
41
42
43 static void hdaps_configure_response(GtkWidget *dialog,
44 gint response,
45 HdapsPlugin *hdaps) {
46
47 if (response == GTK_RESPONSE_HELP) {
48 /* Launch the user's web browser and direct them to
49 the plugin's webpage. */
50 gboolean spawn_result = g_spawn_command_line_async("exo-open --launch WebBrowser " PLUGIN_WEBSITE, NULL);
51
52 if (G_UNLIKELY(spawn_result == FALSE)) {
53 g_warning(_("Unable to open the following url: %s"), PLUGIN_WEBSITE);
54 }
55
56 return;
57 }
58
59
60 if (response == GTK_RESPONSE_OK) {
61 /* This corresponds to the "Save" button, so we
62 want to save any settings that may have changed. */
63 g_object_set_data(G_OBJECT(hdaps->plugin), "dialog", NULL);
64 hdaps->poll_frequency = gtk_spin_button_get_value_as_int((GtkSpinButton*)hdaps->sb_poll_frequency);
65 hdaps->device_name = gtk_combo_box_text_get_active_text((GtkComboBoxText*)hdaps->cb_device_name);
66 snprintf(hdaps->sysfs_file,
67 FILENAME_MAX,
68 UNLOAD_HEADS_FMT,
69 hdaps->device_name);
70 hdaps_save(hdaps->plugin, hdaps);
71 hdaps_reset_timeout(hdaps);
72 }
73
74
75 /* Since there is already a "save" button, we should ignore
76 any changes that were made if the user presses "cancel"
77 instead. */
78 g_object_set_data(G_OBJECT(hdaps->plugin), "dialog", NULL);
79 xfce_panel_plugin_unblock_menu(hdaps->plugin);
80 gtk_widget_destroy(dialog);
81
82 return;
83 }
84
85
86
87 void hdaps_configure(XfcePanelPlugin *plugin,
88 HdapsPlugin *hdaps) {
89
90 /* Here comes a bunch of GTK garbage to create the
91 settings dialog. */
92
93 GtkWidget *dialog;
94 GtkWidget *label;
95 GtkWidget *vbox;
96 GtkWidget *hbox;
97 GtkSizeGroup *sg;
98
99 /* Block the plugin menu while the configuration
100 dialogue is open. Don't forget to unblock it before
101 we close. */
102 xfce_panel_plugin_block_menu(plugin);
103
104 /* Create the dialog */
105 dialog = xfce_titled_dialog_new_with_buttons(
106 _("Hdaps Plugin"),
107 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(plugin))),
108 GTK_DIALOG_DESTROY_WITH_PARENT,
109 "gtk-help", GTK_RESPONSE_HELP,
110 "gtk-save", GTK_RESPONSE_OK,
111 "gtk-cancel", GTK_RESPONSE_CANCEL,
112 NULL);
113
114 /* Center the dialog on screen. */
115 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
116
117 /* Set dialog icon to a generic settings icon. */
118 gtk_window_set_icon_name(GTK_WINDOW(dialog), "xfce4-settings");
119
120 /* Link the dialog to the plugin, so we can destroy it in case
121 * the plugin is closed while the dialog is still open. */
122 g_object_set_data(G_OBJECT(plugin), "dialog", dialog);
123
124 /* Connect the reponse signal to the dialog. */
125 g_signal_connect(G_OBJECT (dialog), "response",
126 G_CALLBACK(hdaps_configure_response), hdaps);
127
128
129 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, DEFAULT_BORDER_WIDTH);
130 gtk_container_set_border_width(GTK_CONTAINER(vbox), DEFAULT_BORDER_WIDTH - 2);
131 gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
132 vbox, TRUE, TRUE, 0);
133
134 /* Create size group to keep widgets aligned */
135 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
136
137 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, DEFAULT_BORDER_WIDTH);
138 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
139
140 label = gtk_label_new(_("Poll Frequency:"));
141 gtk_size_group_add_widget(sg, label);
142 gtk_widget_set_halign(label, 0.0);
143 gtk_widget_set_valign(label, 0.5);
144 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
145
146 hdaps->sb_poll_frequency = gtk_spin_button_new_with_range(100, 5000, 100);
147 gtk_spin_button_set_value((GtkSpinButton*)hdaps->sb_poll_frequency,
148 hdaps->poll_frequency);
149
150 gtk_box_pack_start(GTK_BOX(hbox),
151 hdaps->sb_poll_frequency,
152 FALSE,
153 FALSE,
154 0);
155
156 /* Create the device name dropdown. */
157 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, DEFAULT_BORDER_WIDTH);
158 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
159
160 label = gtk_label_new(_("Device Name:"));
161 gtk_size_group_add_widget(sg, label);
162 gtk_widget_set_halign(label, 0.0);
163 gtk_widget_set_valign(label, 0.5);
164 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
165
166 hdaps->cb_device_name = gtk_combo_box_text_new();
167
168 /* Add the current device name, it should always be available.
169 Oh, and it should be the default, too. */
170 gtk_combo_box_text_append_text((GtkComboBoxText*)hdaps->cb_device_name,
171 hdaps->device_name);
172
173 /* This function takes an index, and 0 should be the first
174 (only) index at this point. */
175 gtk_combo_box_set_active((GtkComboBox*)hdaps->cb_device_name, 0);
176
177 /* Now loop through the list of available devices, adding each
178 to the list as we go. */
179 char hdaps_devices[MAX_HDAPS_DEVICES][FILENAME_MAX];
180 int found_devices = get_hdaps_device_list(hdaps_devices);
181 int list_idx = 0;
182
183 for (list_idx = 0; list_idx < found_devices; list_idx++) {
184 /* We don't want to add duplicate entries to the combo box.
185 However, at this point, the current device name should be
186 the only entry. Therefore, to avoid duplicates, we only
187 have to avoid adding the current device name a second time. */
188 if (strcmp(hdaps_devices[list_idx], hdaps->device_name) != 0) {
189 gtk_combo_box_text_append_text((GtkComboBoxText*)hdaps->cb_device_name,
190 hdaps_devices[list_idx]);
191 }
192 }
193
194 gtk_box_pack_start(GTK_BOX(hbox), hdaps->cb_device_name, FALSE, FALSE, 0);
195
196 /* Show the dialog and all of its widgets. */
197 gtk_widget_show_all(dialog);
198 }
199
200
201
202
203 /* URI (http(s):// and mailto://) handler used on the About dialog. */
204 static void hdaps_uri_handler(GtkAboutDialog *about,
205 const gchar *uri,
206 gpointer data) {
207 gchar *cmd;
208
209 cmd = g_strdup_printf("%s %s","xdg-open", uri);
210
211 if (!g_spawn_command_line_async(cmd, NULL)) {
212 g_free(cmd);
213 cmd = g_strdup_printf("%s %s","xdg-open", uri);
214 g_spawn_command_line_async(cmd, NULL);
215 }
216 g_free(cmd);
217 }
218
219
220 void hdaps_about(XfcePanelPlugin *plugin) {
221 const gchar *authors[] = { "Michael Orlitzky <michael@orlitzky.com>", NULL };
222 const gchar *copyright = "Copyright \302\251 2019 Michael Orlitzky";
223
224 /* Use a custom URI handler for http(s):// and mailto:// URIs. The
225 default behavior apparently needs gio/gvfs to work, and I don't
226 have those installed. */
227 g_signal_connect(plugin,
228 "activate-link",
229 G_CALLBACK(hdaps_uri_handler),
230 NULL);
231
232 gtk_show_about_dialog(NULL,
233 "authors", authors,
234 "copyright", copyright,
235 "destroy-with-parent", TRUE,
236 "license", LICENSE_AGPL3,
237 "logo-icon-name", PACKAGE_NAME,
238 "icon-name", PACKAGE_NAME,
239 "program-name", PACKAGE_NAME,
240 "version", PACKAGE_VERSION,
241 "website", PLUGIN_WEBSITE,
242 "website-label", "xfce4-hdaps homepage",
243 NULL);
244 }