2 * This file is part of DisOrder.
3 * Copyright (C) 2011 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/globals.c
19 * @brief Track global preferences
22 #include "disobedience.h"
24 static GtkWidget *globals_window;
28 /** @brief Handler for the presentation form of a global preference */
29 struct global_handler {
30 /** @brief Initialize */
31 void (*init)(struct globals_row *row);
33 /** @brief Convert presentation form to string */
34 const char *(*get)(struct globals_row *row);
36 /** @brief Convert string to presentation form */
37 void (*set)(struct globals_row *row, const char *value);
40 /** @brief Definition of a global preference */
45 const struct global_handler *handler;
48 static void globals_close(GtkButton attribute((unused)) *button,
49 gpointer attribute((unused)) userdata);
50 static void globals_row_changed(struct globals_row *row);
52 /** @brief Called when the user changes the contents of a string entry */
53 static void global_string_entry_changed(GtkEditable attribute((unused)) *editable,
55 struct globals_row *row = user_data;
56 globals_row_changed(row);
59 /** @brief Initialize a global presented as a string */
60 static void global_string_init(struct globals_row *row) {
61 row->widget = gtk_entry_new();
62 g_signal_connect(row->widget, "changed",
63 G_CALLBACK(global_string_entry_changed), row);
66 static const char *global_string_get(struct globals_row *row) {
67 return gtk_entry_get_text(GTK_ENTRY(row->widget));
70 static void global_string_set(struct globals_row *row, const char *value) {
71 /* Identify unset and empty lists */
74 /* Skip trivial updates (we'll see one as a consequence of each
75 * update we make...) */
76 if(strcmp(gtk_entry_get_text(GTK_ENTRY(row->widget)), value))
77 gtk_entry_set_text(GTK_ENTRY(row->widget), value);
80 /** @brief String global preference */
81 static const struct global_handler global_string = {
87 /** @brief Called when the user changes the contents of a string entry */
88 static void global_boolean_toggled(GtkToggleButton attribute((unused)) *button,
90 struct globals_row *row = user_data;
91 globals_row_changed(row);
94 static void global_boolean_init(struct globals_row *row) {
95 row->widget = gtk_check_button_new();
96 g_signal_connect(row->widget, "toggled",
97 G_CALLBACK(global_boolean_toggled), row);
100 static const char *global_boolean_get(struct globals_row *row) {
101 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget)) ? "yes" : "no";
104 static void global_boolean_set(struct globals_row *row, const char *value) {
105 gboolean new_state = !(value && strcmp(value, "yes"));
106 if(new_state != gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget)))
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->widget), new_state);
110 /** @brief Boolean global preference */
111 static const struct global_handler global_boolean = {
117 /** @brief Table of global preferences */
118 static struct globals_row globals_rows[] = {
119 { "Required tags", "required-tags", NULL, &global_string },
120 { "Prohibited tags", "prohibited-tags", NULL, &global_string },
121 { "Playing", "playing", NULL, &global_boolean },
122 { "Random play", "random-play", NULL, &global_boolean },
124 #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows)
126 /** @brief Buttons for globals popup */
127 static struct button globals_buttons[] = {
129 .stock = GTK_STOCK_CLOSE,
130 .clicked = globals_close,
131 .tip = "Close window",
132 .pack = gtk_box_pack_end,
135 #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons)
137 static void globals_close(GtkButton attribute((unused)) *button,
138 gpointer attribute((unused)) userdata) {
139 gtk_widget_destroy(globals_window);
142 /** @brief Called with the latest setting for a row */
143 static void globals_get_completed(void *v, const char *err,
146 popup_protocol_error(0, err);
147 else if(globals_window) {
148 struct globals_row *row = v;
149 row->handler->set(row, value);
153 /** @brief Retrieve the latest setting for @p row */
154 static void globals_get(struct globals_row *row) {
155 disorder_eclient_get_global(client, globals_get_completed, row->pref, row);
158 static void globals_row_changed(struct globals_row *row) {
159 const char *new_value = row->handler->get(row);
161 disorder_eclient_set_global(client, NULL, row->pref, new_value, row);
163 disorder_eclient_unset_global(client, NULL, row->pref, row);
166 /** @brief Display the globals window */
167 void popup_globals(void) {
168 GtkWidget *label, *table, *hbox;
169 /* Pop up the window if it already exists */
171 gtk_window_present(GTK_WINDOW(globals_window));
174 /* Create the window */
175 /* TODO loads of this is very similar to (copied from!) users.c - can we
177 globals_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
178 gtk_widget_set_style(globals_window, tool_style);
179 gtk_window_set_title(GTK_WINDOW(globals_window), "Globals");
180 g_signal_connect(globals_window, "destroy",
181 G_CALLBACK(gtk_widget_destroyed), &globals_window);
182 table = gtk_table_new(NGLOBALS + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/);
183 gtk_widget_set_style(table, tool_style);\
185 for(size_t n = 0; n < NGLOBALS; ++n) {
186 label = gtk_label_new(globals_rows[n].label);
187 gtk_widget_set_style(label, tool_style);
188 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
189 gtk_table_attach(GTK_TABLE(table), label,
190 0, 1, /* left/right_attach */
191 n, n+1, /* top/bottom_attach */
192 GTK_FILL, 0, /* x/yoptions */
193 1, 1); /* x/ypadding */
194 globals_rows[n].handler->init(&globals_rows[n]);
195 gtk_widget_set_style(globals_rows[n].widget, tool_style);
196 gtk_table_attach(GTK_TABLE(table), globals_rows[n].widget,
197 1, 2, /* left/right_attach */
198 n, n+1, /* top/bottom_attach */
199 GTK_FILL, 0, /* x/yoptions */
200 1, 1); /* x/ypadding */
201 globals_get(&globals_rows[n]);
203 hbox = create_buttons_box(globals_buttons,
205 gtk_hbox_new(FALSE, 1));
206 gtk_table_attach_defaults(GTK_TABLE(table), hbox,
207 0, 2, /* left/right_attach */
208 NGLOBALS, NGLOBALS+1); /* top/bottom_attach */
210 gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL));
211 gtk_widget_show_all(globals_window);
214 /** @brief Called when any global pref changes */
215 static void globals_pref_changed(const char *event,
217 void *callbackdata) {
218 const char *pref = eventdata;
220 return; /* not paying attention */
221 for(size_t n = 0; n < NGLOBALS; ++n) {
222 if(!strcmp(pref, globals_rows[n].pref))
223 globals_get(&globals_rows[n]);
227 /** @brief Initialize globals infrastructure */
228 void globals_init() {
229 event_register("global-pref", globals_pref_changed, NULL);