chiark / gitweb /
Merge compact mode volume slider changes.
[disorder] / disobedience / properties.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 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 */
18/** @file disobedience/properties.c
19 * @brief Track properties editor
20 */
21#include "disobedience.h"
22
23struct prefdata;
24
25static void kickoff_namepart(struct prefdata *f);
26static void completed_namepart(struct prefdata *f);
27static const char *get_edited_namepart(struct prefdata *f);
28static void set_edited_namepart(struct prefdata *f, const char *value);
29static void set_namepart(struct prefdata *f, const char *value);
30static void set_namepart_completed(void *v, const char *err);
31
32static void kickoff_string(struct prefdata *f);
33static void completed_string(struct prefdata *f);
34static const char *get_edited_string(struct prefdata *f);
35static void set_edited_string(struct prefdata *f, const char *value);
36static void set_string(struct prefdata *f, const char *value);
37
38static void kickoff_boolean(struct prefdata *f);
39static void completed_boolean(struct prefdata *f);
40static const char *get_edited_boolean(struct prefdata *f);
41static void set_edited_boolean(struct prefdata *f, const char *value);
42static void set_boolean(struct prefdata *f, const char *value);
43
44static void prefdata_completed(void *v, const char *err, const char *value);
45
46static void properties_ok(GtkButton *button, gpointer userdata);
47static void properties_apply(GtkButton *button, gpointer userdata);
48static void properties_cancel(GtkButton *button, gpointer userdata);
49static void properties_help(GtkButton *button, gpointer userdata);
50
51static void properties_logged_in(const char *event,
52 void *eventdata,
53 void *callbackdata);
54
55/** @brief Data for a single preference */
56struct prefdata {
57 const char *track;
58 int row;
59 const struct pref *p; /**< @brief kind of preference */
60 const char *value; /**< @brief value from server */
61 GtkWidget *widget;
62};
63
64/* The type of a preference is the collection of callbacks needed to get,
65 * display and set it */
66struct preftype {
67 void (*kickoff)(struct prefdata *f);
68 /* Kick off the request to fetch the pref from the server. */
69
70 void (*completed)(struct prefdata *f);
71 /* Called when the value comes back in; creates the widget. */
72
73 const char *(*get_edited)(struct prefdata *f);
74 /* Get the edited value from the widget. */
75
76 /** @brief Update the edited value */
77 void (*set_edited)(struct prefdata *f, const char *value);
78
79 void (*set)(struct prefdata *f, const char *value);
80 /* Set the new value and (if necessary) arrange for our display to update. */
81};
82
83/* A namepart pref */
84static const struct preftype preftype_namepart = {
85 kickoff_namepart,
86 completed_namepart,
87 get_edited_namepart,
88 set_edited_namepart,
89 set_namepart
90};
91
92/* A string pref */
93static const struct preftype preftype_string = {
94 kickoff_string,
95 completed_string,
96 get_edited_string,
97 set_edited_string,
98 set_string
99};
100
101/* A boolean pref */
102static const struct preftype preftype_boolean = {
103 kickoff_boolean,
104 completed_boolean,
105 get_edited_boolean,
106 set_edited_boolean,
107 set_boolean
108};
109
110/* @brief The known prefs for each track */
111static const struct pref {
112 const char *label; /**< @brief user-level description */
113 const char *part; /**< @brief protocol-level tag */
114 const char *default_value; /**< @brief default value or NULL */
115 const struct preftype *type; /**< @brief underlying data type */
116} prefs[] = {
117 { "Artist", "artist", 0, &preftype_namepart },
118 { "Album", "album", 0, &preftype_namepart },
119 { "Title", "title", 0, &preftype_namepart },
120 { "Tags", "tags", "", &preftype_string },
121 { "Weight", "weight", "90000", &preftype_string },
122 { "Random", "pick_at_random", "1", &preftype_boolean },
123};
124
125#define NPREFS (int)(sizeof prefs / sizeof *prefs)
126
127/* Buttons that appear at the bottom of the window */
128static struct button buttons[] = {
129 {
130 GTK_STOCK_HELP,
131 properties_help,
132 "Go to manual",
133 0,
134 gtk_box_pack_start,
135 },
136 {
137 GTK_STOCK_OK,
138 properties_ok,
139 "Apply all changes and close window",
140 0,
141 gtk_box_pack_end,
142 },
143 {
144 GTK_STOCK_CANCEL,
145 properties_cancel,
146 "Discard all changes and close window",
147 0,
148 gtk_box_pack_end
149 },
150 {
151 GTK_STOCK_APPLY,
152 properties_apply,
153 "Apply all changes and keep window open",
154 0,
155 gtk_box_pack_end,
156 },
157};
158
159#define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
160
161static int prefs_unfilled; /* Prefs remaining to get */
162static int prefs_total; /* Total prefs */
163static struct prefdata *prefdatas; /* Current prefdatas */
164static GtkWidget *properties_window;
165static GtkWidget *properties_table;
166static struct progress_window *pw;
167static event_handle properties_event;
168
169static void propagate_clicked(GtkButton attribute((unused)) *button,
170 gpointer userdata) {
171 struct prefdata *f = (struct prefdata *)userdata, *g;
172 int p;
173 const char *value = f->p->type->get_edited(f);
174
175 for(p = 0; p < prefs_total; ++p) {
176 g = &prefdatas[p];
177 if(f->p == g->p && f != g)
178 g->p->type->set_edited(g, value);
179 }
180}
181
182/** @brief Keypress handler */
183static gboolean properties_keypress(GtkWidget attribute((unused)) *widget,
184 GdkEventKey *event,
185 gpointer attribute((unused)) user_data) {
186 if(event->state)
187 return FALSE;
188 switch(event->keyval) {
189 case GDK_Return:
190 properties_ok(0, 0);
191 return TRUE;
192 case GDK_Escape:
193 properties_cancel(0, 0);
194 return TRUE;
195 default:
196 return FALSE;
197 }
198}
199
200void properties(int ntracks, const char **tracks,
201 GtkWidget *parent) {
202 int n, m;
203 struct prefdata *f;
204 GtkWidget *buttonbox, *vbox, *label, *entry, *propagate;
205
206 /* If no tracks, do nothign */
207 if(!ntracks)
208 return;
209 /* If there is a properties window open then just bring it to the
210 * front. It might not have the right values in... */
211 if(properties_window) {
212 if(!prefs_unfilled)
213 gtk_window_present(GTK_WINDOW(properties_window));
214 return;
215 }
216 assert(properties_table == 0);
217 if(ntracks > INT_MAX / NPREFS) {
218 popup_msg(GTK_MESSAGE_ERROR, "Too many tracks selected");
219 return;
220 }
221 properties_event = event_register("logged-in", properties_logged_in, 0);
222 /* Create a new properties window */
223 properties_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
224 gtk_widget_set_style(properties_window, tool_style);
225 g_signal_connect(properties_window, "destroy",
226 G_CALLBACK(gtk_widget_destroyed), &properties_window);
227 /* Keyboard shortcuts */
228 g_signal_connect(properties_window, "key-press-event",
229 G_CALLBACK(properties_keypress), 0);
230 /* Most of the action is the table of preferences */
231 properties_table = gtk_table_new((NPREFS + 1) * ntracks, 2 + ntracks > 1,
232 FALSE);
233 gtk_widget_set_style(properties_table, tool_style);
234 g_signal_connect(properties_table, "destroy",
235 G_CALLBACK(gtk_widget_destroyed), &properties_table);
236 gtk_window_set_title(GTK_WINDOW(properties_window), "Track Properties");
237 /* Create labels for each pref of each track and kick off requests to the
238 * server to fill in the values */
239 prefs_total = NPREFS * ntracks;
240 prefdatas = xcalloc(prefs_total, sizeof *prefdatas);
241 for(n = 0; n < ntracks; ++n) {
242 /* The track itself */
243 /* Caption */
244 label = gtk_label_new("Track");
245 gtk_widget_set_style(label, tool_style);
246 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
247 gtk_table_attach(GTK_TABLE(properties_table),
248 label,
249 0, 1,
250 (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
251 GTK_FILL, 0,
252 1, 1);
253 /* The track name */
254 entry = gtk_entry_new();
255 gtk_widget_set_style(entry, tool_style);
256 gtk_entry_set_text(GTK_ENTRY(entry), tracks[n]);
257 gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
258 gtk_table_attach(GTK_TABLE(properties_table),
259 entry,
260 1, 2,
261 (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
262 GTK_EXPAND|GTK_FILL, 0,
263 1, 1);
264 /* Each preference */
265 for(m = 0; m < NPREFS; ++m) {
266 /* Caption */
267 label = gtk_label_new(prefs[m].label);
268 gtk_widget_set_style(label, tool_style);
269 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
270 gtk_table_attach(GTK_TABLE(properties_table),
271 label,
272 0, 1,
273 (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
274 GTK_FILL/*xoptions*/, 0/*yoptions*/,
275 1, 1);
276 /* Editing the preference is specific */
277 f = &prefdatas[NPREFS * n + m];
278 f->track = tracks[n];
279 f->row = (NPREFS + 1) * n + 1 + m;
280 f->p = &prefs[m];
281 prefs[m].type->kickoff(f);
282 if(ntracks > 1) {
283 /* Propagation button */
284 propagate = iconbutton("propagate.png", "Copy to other tracks");
285 g_signal_connect(G_OBJECT(propagate), "clicked",
286 G_CALLBACK(propagate_clicked), f);
287 gtk_table_attach(GTK_TABLE(properties_table),
288 propagate,
289 2/*left*/, 3/*right*/,
290 (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
291 GTK_FILL/*xoptions*/, 0/*yoptions*/,
292 1/*xpadding*/, 1/*ypadding*/);
293 }
294 }
295 }
296 prefs_unfilled = prefs_total;
297 /* Buttons */
298 buttonbox = create_buttons(buttons, NBUTTONS);
299 /* Put it all together */
300 vbox = gtk_vbox_new(FALSE, 1);
301 gtk_box_pack_start(GTK_BOX(vbox),
302 scroll_widget(properties_table),
303 TRUE, TRUE, 1);
304 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 1);
305 gtk_container_add(GTK_CONTAINER(properties_window), frame_widget(vbox, NULL));
306 /* The table only really wants to be vertically scrollable */
307 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table)->parent->parent),
308 GTK_POLICY_NEVER,
309 GTK_POLICY_AUTOMATIC);
310 /* Zot any pre-existing progress window just in case */
311 if(pw)
312 progress_window_progress(pw, 0, 0);
313 /* Pop up a progress bar while we're waiting */
314 while(parent->parent)
315 parent = parent->parent;
316 pw = progress_window_new("Fetching Track Properties", parent);
317}
318
319/* Everything is filled in now */
320static void prefdata_alldone(void) {
321 if(pw) {
322 progress_window_progress(pw, 0, 0);
323 pw = 0;
324 }
325 /* Default size may be too small */
326 gtk_window_set_default_size(GTK_WINDOW(properties_window), 480, 512);
327 /* TODO: relate default size to required size more closely */
328 gtk_widget_show_all(properties_window);
329}
330
331/* Namepart preferences ---------------------------------------------------- */
332
333static void kickoff_namepart(struct prefdata *f) {
334 /* We ask for the display name part. This is a bit bizarre if what we really
335 * wanted was the underlying preference, but in fact it should always match
336 * and will supply a sane default without having to know how to parse tracks
337 * names (which implies knowing collection roots). */
338 disorder_eclient_namepart(client, prefdata_completed,
339 f->track, "display", f->p->part, f);
340}
341
342static void completed_namepart(struct prefdata *f) {
343 if(!f->value) {
344 /* No setting */
345 f->value = "";
346 }
347 f->widget = gtk_entry_new();
348}
349
350static const char *get_edited_namepart(struct prefdata *f) {
351 return gtk_entry_get_text(GTK_ENTRY(f->widget));
352}
353
354static void set_edited_namepart(struct prefdata *f, const char *value) {
355 gtk_entry_set_text(GTK_ENTRY(f->widget), value);
356}
357
358static void set_namepart(struct prefdata *f, const char *value) {
359 char *s;
360
361 byte_xasprintf(&s, "trackname_display_%s", f->p->part);
362 /* We don't know what the default is so can never unset. This is a bug
363 * relative to the original design, which is supposed to only ever allow for
364 * non-trivial namepart preferences. I suppose the server could spot a
365 * default being set and translate it into an unset. */
366 disorder_eclient_set(client, set_namepart_completed, f->track, s, value,
367 f);
368}
369
370/* Called when we've set a namepart */
371static void set_namepart_completed(void *v, const char *err) {
372 if(err)
373 popup_protocol_error(0, err);
374 else {
375 struct prefdata *f = v;
376
377 namepart_update(f->track, "display", f->p->part);
378 }
379}
380
381/* String preferences ------------------------------------------------------ */
382
383static void kickoff_string(struct prefdata *f) {
384 disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
385}
386
387static void completed_string(struct prefdata *f) {
388 if(!f->value)
389 /* No setting, use the default value instead */
390 f->value = f->p->default_value;
391 f->widget = gtk_entry_new();
392}
393
394static const char *get_edited_string(struct prefdata *f) {
395 return gtk_entry_get_text(GTK_ENTRY(f->widget));
396}
397
398static void set_edited_string(struct prefdata *f, const char *value) {
399 gtk_entry_set_text(GTK_ENTRY(f->widget), value);
400}
401
402static void set_string_completed(void attribute((unused)) *v,
403 const char *err) {
404 if(err)
405 popup_protocol_error(0, err);
406}
407
408static void set_string(struct prefdata *f, const char *value) {
409 disorder_eclient_set(client, set_string_completed, f->track, f->p->part,
410 value, 0/*v*/);
411}
412
413/* Boolean preferences ----------------------------------------------------- */
414
415static void kickoff_boolean(struct prefdata *f) {
416 disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
417}
418
419static void completed_boolean(struct prefdata *f) {
420 f->widget = gtk_check_button_new();
421 gtk_widget_set_style(f->widget, tool_style);
422 if(!f->value)
423 /* Not set, use the default */
424 f->value = f->p->default_value;
425}
426
427static const char *get_edited_boolean(struct prefdata *f) {
428 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
429 ? "1" : "0");
430}
431
432static void set_edited_boolean(struct prefdata *f, const char *value) {
433 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
434 strcmp(value, "0"));
435}
436
437#define set_boolean_completed set_string_completed
438
439static void set_boolean(struct prefdata *f, const char *value) {
440 char *s;
441
442 byte_xasprintf(&s, "trackname_display_%s", f->p->part);
443 disorder_eclient_set(client, set_boolean_completed,
444 f->track, f->p->part, value, 0/*v*/);
445}
446
447/* Querying preferences ---------------------------------------------------- */
448
449static void prefdata_completed(void *v, const char *err, const char *value) {
450 struct prefdata *const f = v;
451
452 if(err)
453 popup_protocol_error(0, err);
454 f->value = value;
455 f->p->type->completed(f);
456 f->p->type->set_edited(f, f->value);
457 assert(f->value != 0); /* Had better set a default */
458 gtk_table_attach(GTK_TABLE(properties_table), f->widget,
459 1, 2,
460 f->row, f->row + 1,
461 GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
462 1, 1);
463 --prefs_unfilled;
464 if(prefs_total)
465 progress_window_progress(pw, prefs_total - prefs_unfilled, prefs_total);
466 if(!prefs_unfilled)
467 prefdata_alldone();
468}
469
470/* Button callbacks -------------------------------------------------------- */
471
472static void properties_ok(GtkButton *button,
473 gpointer userdata) {
474 properties_apply(button, userdata);
475 properties_cancel(button, userdata);
476}
477
478static void properties_apply(GtkButton attribute((unused)) *button,
479 gpointer attribute((unused)) userdata) {
480 int n;
481 const char *edited;
482 struct prefdata *f;
483
484 /* For each possible property we see if we've changed it and if so tell the
485 * server */
486 for(n = 0; n < prefs_total; ++n) {
487 f = &prefdatas[n];
488 edited = f->p->type->get_edited(f);
489 if(strcmp(edited, f->value)) {
490 /* The value has changed */
491 f->p->type->set(f, edited);
492 f->value = xstrdup(edited);
493 }
494 }
495}
496
497static void properties_cancel(GtkButton attribute((unused)) *button,
498 gpointer attribute((unused)) userdata) {
499 gtk_widget_destroy(properties_window);
500 event_cancel(properties_event);
501 properties_event = 0;
502}
503
504static void properties_help(GtkButton attribute((unused)) *button,
505 gpointer attribute((unused)) userdata) {
506 popup_help("properties.html");
507}
508
509/** @brief Called when we've just logged in
510 *
511 * Destroys the current properties window.
512 */
513static void properties_logged_in(const char attribute((unused)) *event,
514 void attribute((unused)) *eventdata,
515 void attribute((unused)) *callbackdata) {
516 if(properties_window)
517 gtk_widget_destroy(properties_window);
518}
519
520/*
521Local Variables:
522c-basic-offset:2
523comment-column:40
524fill-column:79
525indent-tabs-mode:nil
526End:
527*/