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