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