chiark / gitweb /
Add documentation and administrivia about `disorder-gstdecode'.
[disorder] / disobedience / globals.c
CommitLineData
7791f6cf
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2011 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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/>.
17 */
e5cce183
RK
18/** @file disobedience/globals.c
19 * @brief Track global preferences
7791f6cf
RK
20 */
21
22#include "disobedience.h"
23
e5cce183 24static GtkWidget *globals_window;
7791f6cf 25
02d50cbd
RK
26struct globals_row;
27
28/** @brief Handler for the presentation form of a global preference */
29struct global_handler {
30 /** @brief Initialize */
31 void (*init)(struct globals_row *row);
32
33 /** @brief Convert presentation form to string */
34 const char *(*get)(struct globals_row *row);
35
36 /** @brief Convert string to presentation form */
37 void (*set)(struct globals_row *row, const char *value);
38};
39
40/** @brief Definition of a global preference */
41struct globals_row {
7791f6cf
RK
42 const char *label;
43 const char *pref;
02d50cbd
RK
44 GtkWidget *widget;
45 const struct global_handler *handler;
0c3b38f7 46 int initialized;
02d50cbd
RK
47};
48
49static void globals_close(GtkButton attribute((unused)) *button,
50 gpointer attribute((unused)) userdata);
51static void globals_row_changed(struct globals_row *row);
52
53/** @brief Called when the user changes the contents of a string entry */
54static void global_string_entry_changed(GtkEditable attribute((unused)) *editable,
55 gpointer user_data) {
56 struct globals_row *row = user_data;
57 globals_row_changed(row);
58}
59
60/** @brief Initialize a global presented as a string */
61static void global_string_init(struct globals_row *row) {
62 row->widget = gtk_entry_new();
63 g_signal_connect(row->widget, "changed",
64 G_CALLBACK(global_string_entry_changed), row);
65}
66
67static const char *global_string_get(struct globals_row *row) {
68 return gtk_entry_get_text(GTK_ENTRY(row->widget));
69}
70
71static void global_string_set(struct globals_row *row, const char *value) {
72 /* Identify unset and empty lists */
73 if(!value)
74 value = "";
75 /* Skip trivial updates (we'll see one as a consequence of each
76 * update we make...) */
77 if(strcmp(gtk_entry_get_text(GTK_ENTRY(row->widget)), value))
78 gtk_entry_set_text(GTK_ENTRY(row->widget), value);
79}
80
81/** @brief String global preference */
82static const struct global_handler global_string = {
83 global_string_init,
84 global_string_get,
85 global_string_set,
86};
87
88/** @brief Called when the user changes the contents of a string entry */
89static void global_boolean_toggled(GtkToggleButton attribute((unused)) *button,
90 gpointer user_data) {
91 struct globals_row *row = user_data;
92 globals_row_changed(row);
93}
94
95static void global_boolean_init(struct globals_row *row) {
96 row->widget = gtk_check_button_new();
97 g_signal_connect(row->widget, "toggled",
98 G_CALLBACK(global_boolean_toggled), row);
99}
100
101static const char *global_boolean_get(struct globals_row *row) {
102 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget)) ? "yes" : "no";
103}
104
105static void global_boolean_set(struct globals_row *row, const char *value) {
106 gboolean new_state = !(value && strcmp(value, "yes"));
107 if(new_state != gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(row->widget)))
108 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(row->widget), new_state);
109}
110
111/** @brief Boolean global preference */
112static const struct global_handler global_boolean = {
113 global_boolean_init,
114 global_boolean_get,
115 global_boolean_set,
116};
117
118/** @brief Table of global preferences */
119static struct globals_row globals_rows[] = {
0c3b38f7
RK
120 { "Required tags", "required-tags", NULL, &global_string, 0 },
121 { "Prohibited tags", "prohibited-tags", NULL, &global_string, 0 },
122 { "Playing", "playing", NULL, &global_boolean, 0 },
123 { "Random play", "random-play", NULL, &global_boolean, 0 },
7791f6cf 124};
e5cce183 125#define NGLOBALS (sizeof globals_rows / sizeof *globals_rows)
7791f6cf 126
e5cce183
RK
127/** @brief Buttons for globals popup */
128static struct button globals_buttons[] = {
4e6b1953
RK
129 {
130 .stock = GTK_STOCK_CLOSE,
e5cce183 131 .clicked = globals_close,
4e6b1953
RK
132 .tip = "Close window",
133 .pack = gtk_box_pack_end,
134 },
135};
e5cce183 136#define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons)
4e6b1953 137
e5cce183 138static void globals_close(GtkButton attribute((unused)) *button,
4e6b1953 139 gpointer attribute((unused)) userdata) {
e5cce183 140 gtk_widget_destroy(globals_window);
4e6b1953 141}
7791f6cf
RK
142
143/** @brief Called with the latest setting for a row */
e5cce183 144static void globals_get_completed(void *v, const char *err,
7791f6cf
RK
145 const char *value) {
146 if(err)
147 popup_protocol_error(0, err);
e5cce183
RK
148 else if(globals_window) {
149 struct globals_row *row = v;
02d50cbd 150 row->handler->set(row, value);
0c3b38f7 151 row->initialized = 1;
7791f6cf
RK
152 }
153}
154
155/** @brief Retrieve the latest setting for @p row */
e5cce183
RK
156static void globals_get(struct globals_row *row) {
157 disorder_eclient_get_global(client, globals_get_completed, row->pref, row);
7791f6cf
RK
158}
159
02d50cbd 160static void globals_row_changed(struct globals_row *row) {
0c3b38f7
RK
161 if(row->initialized) {
162 const char *new_value = row->handler->get(row);
163 if(new_value)
164 disorder_eclient_set_global(client, NULL, row->pref, new_value, row);
165 else
166 disorder_eclient_unset_global(client, NULL, row->pref, row);
167 }
7791f6cf
RK
168}
169
e5cce183
RK
170/** @brief Display the globals window */
171void popup_globals(void) {
4e6b1953 172 GtkWidget *label, *table, *hbox;
7791f6cf 173 /* Pop up the window if it already exists */
e5cce183
RK
174 if(globals_window) {
175 gtk_window_present(GTK_WINDOW(globals_window));
7791f6cf
RK
176 return;
177 }
178 /* Create the window */
179 /* TODO loads of this is very similar to (copied from!) users.c - can we
180 * de-dupe? */
e5cce183
RK
181 globals_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
182 gtk_widget_set_style(globals_window, tool_style);
183 gtk_window_set_title(GTK_WINDOW(globals_window), "Globals");
184 g_signal_connect(globals_window, "destroy",
185 G_CALLBACK(gtk_widget_destroyed), &globals_window);
186 table = gtk_table_new(NGLOBALS + 1/*rows*/, 2/*cols*/, FALSE/*homogeneous*/);
7791f6cf
RK
187 gtk_widget_set_style(table, tool_style);\
188
e5cce183 189 for(size_t n = 0; n < NGLOBALS; ++n) {
0c3b38f7 190 globals_rows[n].initialized = 0;
e5cce183 191 label = gtk_label_new(globals_rows[n].label);
7791f6cf
RK
192 gtk_widget_set_style(label, tool_style);
193 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
194 gtk_table_attach(GTK_TABLE(table), label,
195 0, 1, /* left/right_attach */
196 n, n+1, /* top/bottom_attach */
197 GTK_FILL, 0, /* x/yoptions */
198 1, 1); /* x/ypadding */
02d50cbd
RK
199 globals_rows[n].handler->init(&globals_rows[n]);
200 gtk_widget_set_style(globals_rows[n].widget, tool_style);
201 gtk_table_attach(GTK_TABLE(table), globals_rows[n].widget,
7791f6cf
RK
202 1, 2, /* left/right_attach */
203 n, n+1, /* top/bottom_attach */
204 GTK_FILL, 0, /* x/yoptions */
205 1, 1); /* x/ypadding */
e5cce183 206 globals_get(&globals_rows[n]);
7791f6cf 207 }
e5cce183
RK
208 hbox = create_buttons_box(globals_buttons,
209 NGLOBALS_BUTTONS,
4e6b1953
RK
210 gtk_hbox_new(FALSE, 1));
211 gtk_table_attach_defaults(GTK_TABLE(table), hbox,
212 0, 2, /* left/right_attach */
e5cce183 213 NGLOBALS, NGLOBALS+1); /* top/bottom_attach */
7791f6cf 214
e5cce183
RK
215 gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL));
216 gtk_widget_show_all(globals_window);
7791f6cf
RK
217}
218
219/** @brief Called when any global pref changes */
ad131c25 220static void globals_pref_changed(const char attribute((unused)) *event,
e5cce183 221 void *eventdata,
ad131c25 222 void attribute((unused)) *callbackdata) {
7791f6cf 223 const char *pref = eventdata;
e5cce183 224 if(!globals_window)
7791f6cf 225 return; /* not paying attention */
e5cce183
RK
226 for(size_t n = 0; n < NGLOBALS; ++n) {
227 if(!strcmp(pref, globals_rows[n].pref))
228 globals_get(&globals_rows[n]);
7791f6cf
RK
229 }
230}
231
e5cce183
RK
232/** @brief Initialize globals infrastructure */
233void globals_init() {
234 event_register("global-pref", globals_pref_changed, NULL);
7791f6cf 235}