chiark / gitweb /
Remove CPPery for playlists
[disorder] / disobedience / properties.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 Richard Kettlewell
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 3 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,
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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18/** @file disobedience/properties.c
19 * @brief Track properties editor
20 */
21#include "disobedience.h"
22
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);
28static void set_edited_namepart(struct prefdata *f, const char *value);
29static void set_namepart(struct prefdata *f, const char *value);
30static void set_namepart_completed(void *v, const char *err);
31
32static void kickoff_string(struct prefdata *f);
33static void completed_string(struct prefdata *f);
34static const char *get_edited_string(struct prefdata *f);
35static void set_edited_string(struct prefdata *f, const char *value);
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);
41static void set_edited_boolean(struct prefdata *f, const char *value);
42static void set_boolean(struct prefdata *f, const char *value);
43
44static void prefdata_completed(void *v, const char *err, const char *value);
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
50static void properties_logged_in(const char *event,
51 void *eventdata,
52 void *callbackdata);
53
54/** @brief Data for a single preference */
55struct prefdata {
56 const char *track;
57 int row;
58 const struct pref *p; /**< @brief kind of preference */
59 const char *value; /**< @brief value from server */
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
75 /** @brief Update the edited value */
76 void (*set_edited)(struct prefdata *f, const char *value);
77
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,
87 set_edited_namepart,
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,
96 set_edited_string,
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,
105 set_edited_boolean,
106 set_boolean
107};
108
109/* @brief The known prefs for each track */
110static const struct pref {
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 */
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 },
120 { "Weight", "weight", "90000", &preftype_string },
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 */
127static struct button buttons[] = {
128 {
129 GTK_STOCK_OK,
130 properties_ok,
131 "Apply all changes and close window",
132 0
133 },
134 {
135 GTK_STOCK_APPLY,
136 properties_apply,
137 "Apply all changes and keep window open",
138 0
139 },
140 {
141 GTK_STOCK_CANCEL,
142 properties_cancel,
143 "Discard all changes and close window",
144 0
145 },
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;
155static struct progress_window *pw;
156static event_handle properties_event;
157
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
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
189void properties(int ntracks, const char **tracks,
190 GtkWidget *parent) {
191 int n, m;
192 struct prefdata *f;
193 GtkWidget *buttonbox, *vbox, *label, *entry, *propagate;
194
195 /* If no tracks, do nothign */
196 if(!ntracks)
197 return;
198 /* If there is a properties window open then just bring it to the
199 * front. It might not have the right values in... */
200 if(properties_window) {
201 if(!prefs_unfilled)
202 gtk_window_present(GTK_WINDOW(properties_window));
203 return;
204 }
205 assert(properties_table == 0);
206 if(ntracks > INT_MAX / NPREFS) {
207 popup_msg(GTK_MESSAGE_ERROR, "Too many tracks selected");
208 return;
209 }
210 properties_event = event_register("logged-in", properties_logged_in, 0);
211 /* Create a new properties window */
212 properties_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
213 gtk_widget_set_style(properties_window, tool_style);
214 g_signal_connect(properties_window, "destroy",
215 G_CALLBACK(gtk_widget_destroyed), &properties_window);
216 /* Keyboard shortcuts */
217 g_signal_connect(properties_window, "key-press-event",
218 G_CALLBACK(properties_keypress), 0);
219 /* Most of the action is the table of preferences */
220 properties_table = gtk_table_new((NPREFS + 1) * ntracks, 2 + ntracks > 1,
221 FALSE);
222 gtk_widget_set_style(properties_table, tool_style);
223 g_signal_connect(properties_table, "destroy",
224 G_CALLBACK(gtk_widget_destroyed), &properties_table);
225 gtk_window_set_title(GTK_WINDOW(properties_window), "Track Properties");
226 /* Create labels for each pref of each track and kick off requests to the
227 * server to fill in the values */
228 prefs_total = NPREFS * ntracks;
229 prefdatas = xcalloc(prefs_total, sizeof *prefdatas);
230 for(n = 0; n < ntracks; ++n) {
231 /* The track itself */
232 /* Caption */
233 label = gtk_label_new("Track");
234 gtk_widget_set_style(label, tool_style);
235 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
236 gtk_table_attach(GTK_TABLE(properties_table),
237 label,
238 0, 1,
239 (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
240 GTK_FILL, 0,
241 1, 1);
242 /* The track name */
243 entry = gtk_entry_new();
244 gtk_widget_set_style(entry, tool_style);
245 gtk_entry_set_text(GTK_ENTRY(entry), tracks[n]);
246 gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
247 gtk_table_attach(GTK_TABLE(properties_table),
248 entry,
249 1, 2,
250 (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
251 GTK_EXPAND|GTK_FILL, 0,
252 1, 1);
253 /* Each preference */
254 for(m = 0; m < NPREFS; ++m) {
255 /* Caption */
256 label = gtk_label_new(prefs[m].label);
257 gtk_widget_set_style(label, tool_style);
258 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
259 gtk_table_attach(GTK_TABLE(properties_table),
260 label,
261 0, 1,
262 (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
263 GTK_FILL/*xoptions*/, 0/*yoptions*/,
264 1, 1);
265 /* Editing the preference is specific */
266 f = &prefdatas[NPREFS * n + m];
267 f->track = tracks[n];
268 f->row = (NPREFS + 1) * n + 1 + m;
269 f->p = &prefs[m];
270 prefs[m].type->kickoff(f);
271 if(ntracks > 1) {
272 /* Propagation button */
273 propagate = iconbutton("propagate.png", "Copy to other tracks");
274 g_signal_connect(G_OBJECT(propagate), "clicked",
275 G_CALLBACK(propagate_clicked), f);
276 gtk_table_attach(GTK_TABLE(properties_table),
277 propagate,
278 2/*left*/, 3/*right*/,
279 (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
280 GTK_FILL/*xoptions*/, 0/*yoptions*/,
281 1/*xpadding*/, 1/*ypadding*/);
282 }
283 }
284 }
285 prefs_unfilled = prefs_total;
286 /* Buttons */
287 buttonbox = create_buttons(buttons, NBUTTONS);
288 /* Put it all together */
289 vbox = gtk_vbox_new(FALSE, 1);
290 gtk_box_pack_start(GTK_BOX(vbox),
291 scroll_widget(properties_table),
292 TRUE, TRUE, 1);
293 gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 1);
294 gtk_container_add(GTK_CONTAINER(properties_window), frame_widget(vbox, NULL));
295 /* The table only really wants to be vertically scrollable */
296 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table)->parent->parent),
297 GTK_POLICY_NEVER,
298 GTK_POLICY_AUTOMATIC);
299 /* Zot any pre-existing progress window just in case */
300 if(pw)
301 progress_window_progress(pw, 0, 0);
302 /* Pop up a progress bar while we're waiting */
303 while(parent->parent)
304 parent = parent->parent;
305 pw = progress_window_new("Fetching Track Properties", parent);
306}
307
308/* Everything is filled in now */
309static void prefdata_alldone(void) {
310 if(pw) {
311 progress_window_progress(pw, 0, 0);
312 pw = 0;
313 }
314 /* Default size may be too small */
315 gtk_window_set_default_size(GTK_WINDOW(properties_window), 480, 512);
316 /* TODO: relate default size to required size more closely */
317 gtk_widget_show_all(properties_window);
318}
319
320/* Namepart preferences ---------------------------------------------------- */
321
322static void kickoff_namepart(struct prefdata *f) {
323 /* We ask for the display name part. This is a bit bizarre if what we really
324 * wanted was the underlying preference, but in fact it should always match
325 * and will supply a sane default without having to know how to parse tracks
326 * names (which implies knowing collection roots). */
327 disorder_eclient_namepart(client, prefdata_completed,
328 f->track, "display", f->p->part, f);
329}
330
331static void completed_namepart(struct prefdata *f) {
332 if(!f->value) {
333 /* No setting */
334 f->value = "";
335 }
336 f->widget = gtk_entry_new();
337}
338
339static const char *get_edited_namepart(struct prefdata *f) {
340 return gtk_entry_get_text(GTK_ENTRY(f->widget));
341}
342
343static void set_edited_namepart(struct prefdata *f, const char *value) {
344 gtk_entry_set_text(GTK_ENTRY(f->widget), value);
345}
346
347static void set_namepart(struct prefdata *f, const char *value) {
348 char *s;
349
350 byte_xasprintf(&s, "trackname_display_%s", f->p->part);
351 /* We don't know what the default is so can never unset. This is a bug
352 * relative to the original design, which is supposed to only ever allow for
353 * non-trivial namepart preferences. I suppose the server could spot a
354 * default being set and translate it into an unset. */
355 disorder_eclient_set(client, set_namepart_completed, f->track, s, value,
356 f);
357}
358
359/* Called when we've set a namepart */
360static void set_namepart_completed(void *v, const char *err) {
361 if(err)
362 popup_protocol_error(0, err);
363 else {
364 struct prefdata *f = v;
365
366 namepart_update(f->track, "display", f->p->part);
367 }
368}
369
370/* String preferences ------------------------------------------------------ */
371
372static void kickoff_string(struct prefdata *f) {
373 disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
374}
375
376static void completed_string(struct prefdata *f) {
377 if(!f->value)
378 /* No setting, use the default value instead */
379 f->value = f->p->default_value;
380 f->widget = gtk_entry_new();
381}
382
383static const char *get_edited_string(struct prefdata *f) {
384 return gtk_entry_get_text(GTK_ENTRY(f->widget));
385}
386
387static void set_edited_string(struct prefdata *f, const char *value) {
388 gtk_entry_set_text(GTK_ENTRY(f->widget), value);
389}
390
391static void set_string_completed(void attribute((unused)) *v,
392 const char *err) {
393 if(err)
394 popup_protocol_error(0, err);
395}
396
397static void set_string(struct prefdata *f, const char *value) {
398 disorder_eclient_set(client, set_string_completed, f->track, f->p->part,
399 value, 0/*v*/);
400}
401
402/* Boolean preferences ----------------------------------------------------- */
403
404static void kickoff_boolean(struct prefdata *f) {
405 disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
406}
407
408static void completed_boolean(struct prefdata *f) {
409 f->widget = gtk_check_button_new();
410 gtk_widget_set_style(f->widget, tool_style);
411 if(!f->value)
412 /* Not set, use the default */
413 f->value = f->p->default_value;
414}
415
416static const char *get_edited_boolean(struct prefdata *f) {
417 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
418 ? "1" : "0");
419}
420
421static void set_edited_boolean(struct prefdata *f, const char *value) {
422 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
423 strcmp(value, "0"));
424}
425
426#define set_boolean_completed set_string_completed
427
428static void set_boolean(struct prefdata *f, const char *value) {
429 char *s;
430
431 byte_xasprintf(&s, "trackname_display_%s", f->p->part);
432 disorder_eclient_set(client, set_boolean_completed,
433 f->track, f->p->part, value, 0/*v*/);
434}
435
436/* Querying preferences ---------------------------------------------------- */
437
438static void prefdata_completed(void *v, const char *err, const char *value) {
439 struct prefdata *const f = v;
440
441 if(err)
442 popup_protocol_error(0, err);
443 f->value = value;
444 f->p->type->completed(f);
445 f->p->type->set_edited(f, f->value);
446 assert(f->value != 0); /* Had better set a default */
447 gtk_table_attach(GTK_TABLE(properties_table), f->widget,
448 1, 2,
449 f->row, f->row + 1,
450 GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
451 1, 1);
452 --prefs_unfilled;
453 if(prefs_total)
454 progress_window_progress(pw, prefs_total - prefs_unfilled, prefs_total);
455 if(!prefs_unfilled)
456 prefdata_alldone();
457}
458
459/* Button callbacks -------------------------------------------------------- */
460
461static void properties_ok(GtkButton *button,
462 gpointer userdata) {
463 properties_apply(button, userdata);
464 properties_cancel(button, userdata);
465}
466
467static void properties_apply(GtkButton attribute((unused)) *button,
468 gpointer attribute((unused)) userdata) {
469 int n;
470 const char *edited;
471 struct prefdata *f;
472
473 /* For each possible property we see if we've changed it and if so tell the
474 * server */
475 for(n = 0; n < prefs_total; ++n) {
476 f = &prefdatas[n];
477 edited = f->p->type->get_edited(f);
478 if(strcmp(edited, f->value)) {
479 /* The value has changed */
480 f->p->type->set(f, edited);
481 f->value = xstrdup(edited);
482 }
483 }
484}
485
486static void properties_cancel(GtkButton attribute((unused)) *button,
487 gpointer attribute((unused)) userdata) {
488 gtk_widget_destroy(properties_window);
489 event_cancel(properties_event);
490 properties_event = 0;
491}
492
493/** @brief Called when we've just logged in
494 *
495 * Destroys the current properties window.
496 */
497static void properties_logged_in(const char attribute((unused)) *event,
498 void attribute((unused)) *eventdata,
499 void attribute((unused)) *callbackdata) {
500 if(properties_window)
501 gtk_widget_destroy(properties_window);
502}
503
504/*
505Local Variables:
506c-basic-offset:2
507comment-column:40
508fill-column:79
509indent-tabs-mode:nil
510End:
511*/