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