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