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