Commit | Line | Data |
---|---|---|
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 | 24 | static GtkWidget *globals_window; |
7791f6cf | 25 | |
02d50cbd RK |
26 | struct globals_row; |
27 | ||
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); | |
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 */ | |
41 | struct globals_row { | |
7791f6cf RK |
42 | const char *label; |
43 | const char *pref; | |
02d50cbd RK |
44 | GtkWidget *widget; |
45 | const struct global_handler *handler; | |
46 | }; | |
47 | ||
48 | static void globals_close(GtkButton attribute((unused)) *button, | |
49 | gpointer attribute((unused)) userdata); | |
50 | static void globals_row_changed(struct globals_row *row); | |
51 | ||
52 | /** @brief Called when the user changes the contents of a string entry */ | |
53 | static void global_string_entry_changed(GtkEditable attribute((unused)) *editable, | |
54 | gpointer user_data) { | |
55 | struct globals_row *row = user_data; | |
56 | globals_row_changed(row); | |
57 | } | |
58 | ||
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); | |
64 | } | |
65 | ||
66 | static const char *global_string_get(struct globals_row *row) { | |
67 | return gtk_entry_get_text(GTK_ENTRY(row->widget)); | |
68 | } | |
69 | ||
70 | static void global_string_set(struct globals_row *row, const char *value) { | |
71 | /* Identify unset and empty lists */ | |
72 | if(!value) | |
73 | value = ""; | |
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); | |
78 | } | |
79 | ||
80 | /** @brief String global preference */ | |
81 | static const struct global_handler global_string = { | |
82 | global_string_init, | |
83 | global_string_get, | |
84 | global_string_set, | |
85 | }; | |
86 | ||
87 | /** @brief Called when the user changes the contents of a string entry */ | |
88 | static void global_boolean_toggled(GtkToggleButton attribute((unused)) *button, | |
89 | gpointer user_data) { | |
90 | struct globals_row *row = user_data; | |
91 | globals_row_changed(row); | |
92 | } | |
93 | ||
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); | |
98 | } | |
99 | ||
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"; | |
102 | } | |
103 | ||
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); | |
108 | } | |
109 | ||
110 | /** @brief Boolean global preference */ | |
111 | static const struct global_handler global_boolean = { | |
112 | global_boolean_init, | |
113 | global_boolean_get, | |
114 | global_boolean_set, | |
115 | }; | |
116 | ||
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 }, | |
7791f6cf | 123 | }; |
e5cce183 | 124 | #define NGLOBALS (sizeof globals_rows / sizeof *globals_rows) |
7791f6cf | 125 | |
e5cce183 RK |
126 | /** @brief Buttons for globals popup */ |
127 | static struct button globals_buttons[] = { | |
4e6b1953 RK |
128 | { |
129 | .stock = GTK_STOCK_CLOSE, | |
e5cce183 | 130 | .clicked = globals_close, |
4e6b1953 RK |
131 | .tip = "Close window", |
132 | .pack = gtk_box_pack_end, | |
133 | }, | |
134 | }; | |
e5cce183 | 135 | #define NGLOBALS_BUTTONS (sizeof globals_buttons / sizeof *globals_buttons) |
4e6b1953 | 136 | |
e5cce183 | 137 | static void globals_close(GtkButton attribute((unused)) *button, |
4e6b1953 | 138 | gpointer attribute((unused)) userdata) { |
e5cce183 | 139 | gtk_widget_destroy(globals_window); |
4e6b1953 | 140 | } |
7791f6cf RK |
141 | |
142 | /** @brief Called with the latest setting for a row */ | |
e5cce183 | 143 | static void globals_get_completed(void *v, const char *err, |
7791f6cf RK |
144 | const char *value) { |
145 | if(err) | |
146 | popup_protocol_error(0, err); | |
e5cce183 RK |
147 | else if(globals_window) { |
148 | struct globals_row *row = v; | |
02d50cbd | 149 | row->handler->set(row, value); |
7791f6cf RK |
150 | } |
151 | } | |
152 | ||
153 | /** @brief Retrieve the latest setting for @p row */ | |
e5cce183 RK |
154 | static void globals_get(struct globals_row *row) { |
155 | disorder_eclient_get_global(client, globals_get_completed, row->pref, row); | |
7791f6cf RK |
156 | } |
157 | ||
02d50cbd RK |
158 | static void globals_row_changed(struct globals_row *row) { |
159 | const char *new_value = row->handler->get(row); | |
160 | if(new_value) | |
7791f6cf RK |
161 | disorder_eclient_set_global(client, NULL, row->pref, new_value, row); |
162 | else | |
163 | disorder_eclient_unset_global(client, NULL, row->pref, row); | |
164 | } | |
165 | ||
e5cce183 RK |
166 | /** @brief Display the globals window */ |
167 | void popup_globals(void) { | |
4e6b1953 | 168 | GtkWidget *label, *table, *hbox; |
7791f6cf | 169 | /* Pop up the window if it already exists */ |
e5cce183 RK |
170 | if(globals_window) { |
171 | gtk_window_present(GTK_WINDOW(globals_window)); | |
7791f6cf RK |
172 | return; |
173 | } | |
174 | /* Create the window */ | |
175 | /* TODO loads of this is very similar to (copied from!) users.c - can we | |
176 | * de-dupe? */ | |
e5cce183 RK |
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*/); | |
7791f6cf RK |
183 | gtk_widget_set_style(table, tool_style);\ |
184 | ||
e5cce183 RK |
185 | for(size_t n = 0; n < NGLOBALS; ++n) { |
186 | label = gtk_label_new(globals_rows[n].label); | |
7791f6cf RK |
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 */ | |
02d50cbd RK |
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, | |
7791f6cf RK |
197 | 1, 2, /* left/right_attach */ |
198 | n, n+1, /* top/bottom_attach */ | |
199 | GTK_FILL, 0, /* x/yoptions */ | |
200 | 1, 1); /* x/ypadding */ | |
e5cce183 | 201 | globals_get(&globals_rows[n]); |
7791f6cf | 202 | } |
e5cce183 RK |
203 | hbox = create_buttons_box(globals_buttons, |
204 | NGLOBALS_BUTTONS, | |
4e6b1953 RK |
205 | gtk_hbox_new(FALSE, 1)); |
206 | gtk_table_attach_defaults(GTK_TABLE(table), hbox, | |
207 | 0, 2, /* left/right_attach */ | |
e5cce183 | 208 | NGLOBALS, NGLOBALS+1); /* top/bottom_attach */ |
7791f6cf | 209 | |
e5cce183 RK |
210 | gtk_container_add(GTK_CONTAINER(globals_window), frame_widget(table, NULL)); |
211 | gtk_widget_show_all(globals_window); | |
7791f6cf RK |
212 | } |
213 | ||
214 | /** @brief Called when any global pref changes */ | |
e5cce183 RK |
215 | static void globals_pref_changed(const char *event, |
216 | void *eventdata, | |
217 | void *callbackdata) { | |
7791f6cf | 218 | const char *pref = eventdata; |
e5cce183 | 219 | if(!globals_window) |
7791f6cf | 220 | return; /* not paying attention */ |
e5cce183 RK |
221 | for(size_t n = 0; n < NGLOBALS; ++n) { |
222 | if(!strcmp(pref, globals_rows[n].pref)) | |
223 | globals_get(&globals_rows[n]); | |
7791f6cf RK |
224 | } |
225 | } | |
226 | ||
e5cce183 RK |
227 | /** @brief Initialize globals infrastructure */ |
228 | void globals_init() { | |
229 | event_register("global-pref", globals_pref_changed, NULL); | |
7791f6cf | 230 | } |