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