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