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