]> gitweb.michael.orlitzky.com - xfce4-hdaps.git/blob - panel-plugin/xfce4-hdaps-dialogs.c
046beac60daeaa2857a77d4a611ba0cc8a256c73
[xfce4-hdaps.git] / panel-plugin / xfce4-hdaps-dialogs.c
1 /*
2 * xfce4-hdaps, an XFCE4 panel plugin for the HDAPS system.
3 *
4 * Copyright 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 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.
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 General Public License for more details.
17 *
18 * http://www.fsf.org/licensing/licenses/gpl.html
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <gtk/gtk.h>
26 #include <libxfcegui4/libxfcegui4.h>
27 #include <libxfce4panel/xfce-panel-plugin.h>
28 #include "xfce4-hdaps-dialogs.h"
29
30 /* Wild guess. Actually borrowed from some code
31 I copy-and-pasted. Doesn't seem too ugly. */
32 #define DEFAULT_BORDER_WIDTH 8
33
34 /* If people can't read the README, well maybe
35 * they can do it /online/.
36 */
37 #define PLUGIN_WEBSITE "http://michael.orlitzky.com/code/xfce4-hdaps.php"
38
39
40 static void hdaps_configure_response(GtkWidget *dialog,
41 gint response,
42 HdapsPlugin *hdaps) {
43
44 if (response == GTK_RESPONSE_HELP) {
45 /* Launch the user's web browser and direct them to
46 the plugin's webpage. */
47 gboolean spawn_result = g_spawn_command_line_async("exo-open --launch WebBrowser " PLUGIN_WEBSITE, NULL);
48
49 if (G_UNLIKELY(spawn_result == FALSE)) {
50 g_warning(_("Unable to open the following url: %s"), PLUGIN_WEBSITE);
51 }
52
53 return;
54 }
55
56
57 if (response == GTK_RESPONSE_OK) {
58 /* This corresponds to the "Save" button, so we
59 want to save any settings that may have changed. */
60 g_object_set_data(G_OBJECT(hdaps->plugin), "dialog", NULL);
61 hdaps->poll_frequency = gtk_spin_button_get_value_as_int((GtkSpinButton*)hdaps->sb_poll_frequency);
62 hdaps->device_name = gtk_combo_box_get_active_text((GtkComboBox*)hdaps->cb_device_name);
63 snprintf(hdaps->sysfs_file, FILENAME_MAX, UNLOAD_HEADS_FMT, hdaps->device_name);
64 hdaps_save(hdaps->plugin, hdaps);
65 hdaps_reset_timeout(hdaps);
66 }
67
68
69 /* Since there is already a "save" button, we should ignore
70 any changes that were made if the user presses "cancel"
71 instead. */
72 g_object_set_data(G_OBJECT(hdaps->plugin), "dialog", NULL);
73 xfce_panel_plugin_unblock_menu(hdaps->plugin);
74 gtk_widget_destroy(dialog);
75
76 return;
77 }
78
79
80
81 void hdaps_configure(XfcePanelPlugin *plugin,
82 HdapsPlugin *hdaps) {
83
84 /* Here comes a bunch of GTK garbage to create the
85 settings dialog. */
86
87 GtkWidget *dialog;
88 GtkWidget *label;
89 GtkWidget *vbox;
90 GtkWidget *hbox;
91 GtkSizeGroup *sg;
92
93 /* Block the plugin menu while the configuration
94 dialogue is open. Don't forget to unblock it before
95 we close. */
96 xfce_panel_plugin_block_menu(plugin);
97
98 /* Create the dialog */
99 dialog = xfce_titled_dialog_new_with_buttons(_("Hdaps Plugin"),
100 GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (plugin))),
101 GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
102 GTK_STOCK_HELP, GTK_RESPONSE_HELP,
103 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
104 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
105 NULL);
106
107 /* Center the dialog on screen. */
108 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
109
110 /* Set dialog icon to a generic settings icon. */
111 gtk_window_set_icon_name(GTK_WINDOW(dialog), "xfce4-settings");
112
113 /* Link the dialog to the plugin, so we can destroy it in case
114 * the plugin is closed while the dialog is still open. */
115 g_object_set_data(G_OBJECT(plugin), "dialog", dialog);
116
117 /* Connect the reponse signal to the dialog. */
118 g_signal_connect(G_OBJECT (dialog), "response",
119 G_CALLBACK(hdaps_configure_response), hdaps);
120
121
122 vbox = gtk_vbox_new(FALSE, DEFAULT_BORDER_WIDTH);
123 gtk_container_set_border_width(GTK_CONTAINER(vbox), DEFAULT_BORDER_WIDTH - 2);
124 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox, TRUE, TRUE, 0);
125
126 /* Create size group to keep widgets aligned */
127 sg = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
128
129 hbox = gtk_hbox_new(FALSE, DEFAULT_BORDER_WIDTH);
130 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
131
132 label = gtk_label_new(_("Poll Frequency:"));
133 gtk_size_group_add_widget(sg, label);
134 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
135 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
136
137 hdaps->sb_poll_frequency = gtk_spin_button_new_with_range(100, 5000, 100);
138 gtk_spin_button_set_value((GtkSpinButton*)hdaps->sb_poll_frequency, hdaps->poll_frequency);
139
140 gtk_box_pack_start(GTK_BOX(hbox),
141 hdaps->sb_poll_frequency,
142 FALSE,
143 FALSE,
144 0);
145
146 /* Create the device name dropdown. */
147 hbox = gtk_hbox_new(FALSE, DEFAULT_BORDER_WIDTH);
148 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
149
150 label = gtk_label_new(_("Device Name:"));
151 gtk_size_group_add_widget(sg, label);
152 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
153 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
154
155 hdaps->cb_device_name = gtk_combo_box_new_text();
156
157 /* Add the current device name, it should always be available.
158 Oh, and it should be the default, too. */
159 gtk_combo_box_append_text((GtkComboBox*)hdaps->cb_device_name, hdaps->device_name);
160
161 /* This function takes an index, and 0 should be the first
162 (only) index at this point. */
163 gtk_combo_box_set_active((GtkComboBox*)hdaps->cb_device_name, 0);
164
165 /* Now loop through the list of available devices, adding each
166 to the list as we go. */
167 char hdaps_devices[MAX_HDAPS_DEVICES][FILENAME_MAX];
168 int found_devices = get_hdaps_device_list(hdaps_devices);
169 int list_idx = 0;
170
171 for (list_idx = 0; list_idx < found_devices; list_idx++) {
172 /* We don't want to add duplicate entries to the combo box.
173 However, at this point, the current device name should be
174 the only entry. Therefore, to avoid duplicates, we only
175 have to avoid adding the current device name a second time. */
176 if (strcmp(hdaps_devices[list_idx], hdaps->device_name) != 0) {
177 gtk_combo_box_append_text((GtkComboBox*)hdaps->cb_device_name, hdaps_devices[list_idx]);
178 }
179 }
180
181 gtk_box_pack_start(GTK_BOX(hbox), hdaps->cb_device_name, FALSE, FALSE, 0);
182
183 /* Show the dialog and all of its widgets. */
184 gtk_widget_show_all(dialog);
185 }
186
187
188
189 void hdaps_about(XfcePanelPlugin *plugin) {
190 GdkPixbuf *icon;
191 GtkWidget *dialog;
192 XfceAboutInfo *about;
193
194 /* Send NULL as our license, since GPL >= 3 is not
195 supported yet. I don't particularly care if people
196 want to re-license under GPL2, but I also don't want
197 to cause any unnecessary confusion. */
198 about = xfce_about_info_new("xfce4-hdaps",
199 VERSION,
200 _("An HDAPS Plugin for XFCE4"),
201 XFCE_COPYRIGHT_TEXT("2009", "Michael Orlitzky"),
202 NULL);
203
204 xfce_about_info_set_homepage(about, PLUGIN_WEBSITE);
205
206 xfce_about_info_add_credit(about,
207 "Michael Orlitzky",
208 "michael@orlitzky.com",
209 _("A Reasonable Man"));
210
211 icon = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(),
212 "xfce4-hdaps",
213 48, 0, NULL);
214
215 dialog = xfce_about_dialog_new_with_values(NULL, about, icon);
216 xfce_about_info_free(about);
217 gtk_dialog_run(GTK_DIALOG(dialog));
218 gtk_widget_destroy(dialog);
219 g_object_unref(G_OBJECT(icon));
220 }