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