Commit | Line | Data |
---|---|---|
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 | ||
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); | |
348ef780 | 30 | static void set_edited_namepart(struct prefdata *f, const char *value); |
460b9539 | 31 | static void set_namepart(struct prefdata *f, const char *value); |
32 | static void set_namepart_completed(void *v); | |
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); | |
348ef780 | 37 | static void set_edited_string(struct prefdata *f, const char *value); |
460b9539 | 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); | |
348ef780 | 43 | static void set_edited_boolean(struct prefdata *f, const char *value); |
460b9539 | 44 | static void set_boolean(struct prefdata *f, const char *value); |
45 | ||
46 | static void prefdata_completed(void *v, 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 | ||
13aba192 | 58 | /** @brief Data for a single preference */ |
460b9539 | 59 | struct 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 */ | |
69 | struct 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 */ | |
87 | static 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 */ | |
96 | static 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 */ | |
105 | static 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 | 114 | static 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 }, | |
6a881c39 | 124 | { "Weight", "weight", "90000", &preftype_string }, |
460b9539 | 125 | { "Random", "pick_at_random", "1", &preftype_boolean }, |
126 | }; | |
127 | ||
128 | #define NPREFS (int)(sizeof prefs / sizeof *prefs) | |
129 | ||
130 | /* Buttons that appear at the bottom of the window */ | |
73f1b9f3 | 131 | static const struct button buttons[] = { |
f4f5a474 RK |
132 | { |
133 | GTK_STOCK_OK, | |
134 | properties_ok, | |
135 | "Apply all changes and close window" | |
136 | }, | |
137 | { | |
138 | GTK_STOCK_APPLY, | |
139 | properties_apply, | |
140 | "Apply all changes and keep window open" | |
141 | }, | |
142 | { | |
143 | GTK_STOCK_CANCEL, | |
144 | properties_cancel, | |
145 | "Discard all changes and close window" | |
146 | }, | |
460b9539 | 147 | }; |
148 | ||
149 | #define NBUTTONS (int)(sizeof buttons / sizeof *buttons) | |
150 | ||
151 | static int prefs_unfilled; /* Prefs remaining to get */ | |
152 | static int prefs_total; /* Total prefs */ | |
153 | static struct prefdata *prefdatas; /* Current prefdatas */ | |
154 | static GtkWidget *properties_window; | |
155 | static GtkWidget *properties_table; | |
c3df9503 | 156 | static struct progress_window *pw; |
460b9539 | 157 | |
348ef780 RK |
158 | static 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 | ||
16e145a5 | 171 | void properties(int ntracks, const char **tracks) { |
460b9539 | 172 | int n, m; |
173 | struct prefdata *f; | |
33288048 | 174 | GtkWidget *buttonbox, *vbox, *label, *entry, *propagate; |
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); | |
33288048 | 193 | gtk_widget_set_style(properties_window, tool_style); |
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); | |
33288048 | 199 | gtk_widget_set_style(properties_table, tool_style); |
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"); |
33288048 | 211 | gtk_widget_set_style(label, tool_style); |
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(); |
33288048 | 221 | gtk_widget_set_style(entry, tool_style); |
460b9539 | 222 | gtk_entry_set_text(GTK_ENTRY(entry), tracks[n]); |
223 | gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE); | |
224 | gtk_table_attach(GTK_TABLE(properties_table), | |
225 | entry, | |
226 | 1, 2, | |
227 | (NPREFS + 1) * n, (NPREFS + 1) * n + 1, | |
228 | GTK_EXPAND|GTK_FILL, 0, | |
229 | 1, 1); | |
348ef780 | 230 | /* Each preference */ |
460b9539 | 231 | for(m = 0; m < NPREFS; ++m) { |
348ef780 | 232 | /* Caption */ |
460b9539 | 233 | label = gtk_label_new(prefs[m].label); |
33288048 | 234 | gtk_widget_set_style(label, tool_style); |
460b9539 | 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 + 1 + m, (NPREFS + 1) * n + 2 + m, | |
240 | GTK_FILL/*xoptions*/, 0/*yoptions*/, | |
241 | 1, 1); | |
348ef780 | 242 | /* Editing the preference is specific */ |
460b9539 | 243 | f = &prefdatas[NPREFS * n + m]; |
244 | f->track = tracks[n]; | |
245 | f->row = (NPREFS + 1) * n + 1 + m; | |
246 | f->p = &prefs[m]; | |
247 | prefs[m].type->kickoff(f); | |
348ef780 RK |
248 | if(ntracks > 1) { |
249 | /* Propagation button */ | |
33288048 | 250 | propagate = iconbutton("propagate.png", "Copy to other tracks"); |
348ef780 RK |
251 | g_signal_connect(G_OBJECT(propagate), "clicked", |
252 | G_CALLBACK(propagate_clicked), f); | |
253 | gtk_table_attach(GTK_TABLE(properties_table), | |
254 | propagate, | |
255 | 2/*left*/, 3/*right*/, | |
256 | (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m, | |
257 | GTK_FILL/*xoptions*/, 0/*yoptions*/, | |
258 | 1/*xpadding*/, 1/*ypadding*/); | |
259 | } | |
460b9539 | 260 | } |
261 | } | |
262 | prefs_unfilled = prefs_total; | |
263 | /* Buttons */ | |
73f1b9f3 | 264 | buttonbox = create_buttons(buttons, NBUTTONS); |
460b9539 | 265 | /* Put it all together */ |
266 | vbox = gtk_vbox_new(FALSE, 1); | |
267 | gtk_box_pack_start(GTK_BOX(vbox), | |
d4ef4132 | 268 | scroll_widget(properties_table), |
460b9539 | 269 | TRUE, TRUE, 1); |
73f1b9f3 | 270 | gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 1); |
460b9539 | 271 | gtk_container_add(GTK_CONTAINER(properties_window), vbox); |
272 | /* The table only really wants to be vertically scrollable */ | |
273 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table)->parent->parent), | |
274 | GTK_POLICY_NEVER, | |
275 | GTK_POLICY_AUTOMATIC); | |
c3df9503 RK |
276 | /* Zot any pre-existing progress window just in case */ |
277 | if(pw) | |
278 | progress_window_progress(pw, 0, 0); | |
460b9539 | 279 | /* Pop up a progress bar while we're waiting */ |
c3df9503 | 280 | pw = progress_window_new("Fetching Track Properties"); |
460b9539 | 281 | } |
282 | ||
283 | /* Everything is filled in now */ | |
284 | static void prefdata_alldone(void) { | |
c3df9503 RK |
285 | if(pw) { |
286 | progress_window_progress(pw, 0, 0); | |
287 | pw = 0; | |
288 | } | |
460b9539 | 289 | /* Default size may be too small */ |
290 | gtk_window_set_default_size(GTK_WINDOW(properties_window), 480, 512); | |
291 | /* TODO: relate default size to required size more closely */ | |
292 | gtk_widget_show_all(properties_window); | |
293 | } | |
294 | ||
295 | /* Namepart preferences ---------------------------------------------------- */ | |
296 | ||
297 | static void kickoff_namepart(struct prefdata *f) { | |
13aba192 | 298 | /* We ask for the display name part. This is a bit bizarre if what we really |
299 | * wanted was the underlying preference, but in fact it should always match | |
300 | * and will supply a sane default without having to know how to parse tracks | |
301 | * names (which implies knowing collection roots). */ | |
302 | disorder_eclient_namepart(client, prefdata_completed, f->track, "display", f->p->part, | |
303 | make_callbackdata(f)); | |
460b9539 | 304 | } |
305 | ||
306 | static void completed_namepart(struct prefdata *f) { | |
13aba192 | 307 | if(!f->value) { |
308 | /* No setting */ | |
309 | f->value = ""; | |
310 | } | |
460b9539 | 311 | f->widget = gtk_entry_new(); |
460b9539 | 312 | } |
313 | ||
314 | static const char *get_edited_namepart(struct prefdata *f) { | |
315 | return gtk_entry_get_text(GTK_ENTRY(f->widget)); | |
316 | } | |
317 | ||
348ef780 RK |
318 | static void set_edited_namepart(struct prefdata *f, const char *value) { |
319 | gtk_entry_set_text(GTK_ENTRY(f->widget), value); | |
320 | } | |
321 | ||
460b9539 | 322 | static void set_namepart(struct prefdata *f, const char *value) { |
323 | char *s; | |
324 | struct callbackdata *cbd = xmalloc(sizeof *cbd); | |
325 | ||
326 | cbd->u.f = f; | |
327 | byte_xasprintf(&s, "trackname_display_%s", f->p->part); | |
13aba192 | 328 | /* We don't know what the default is so can never unset. This is a bug |
329 | * relative to the original design, which is supposed to only ever allow for | |
330 | * non-trivial namepart preferences. I suppose the server could spot a | |
331 | * default being set and translate it into an unset. */ | |
332 | disorder_eclient_set(client, set_namepart_completed, f->track, s, value, | |
333 | cbd); | |
460b9539 | 334 | } |
335 | ||
336 | /* Called when we've set a namepart */ | |
337 | static void set_namepart_completed(void *v) { | |
338 | struct callbackdata *cbd = v; | |
339 | struct prefdata *f = cbd->u.f; | |
340 | ||
341 | namepart_update(f->track, "display", f->p->part); | |
342 | } | |
343 | ||
344 | /* String preferences ------------------------------------------------------ */ | |
345 | ||
346 | static void kickoff_string(struct prefdata *f) { | |
347 | disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, | |
348 | make_callbackdata(f)); | |
349 | } | |
350 | ||
351 | static void completed_string(struct prefdata *f) { | |
352 | if(!f->value) | |
353 | /* No setting, use the default value instead */ | |
354 | f->value = f->p->default_value; | |
355 | f->widget = gtk_entry_new(); | |
460b9539 | 356 | } |
357 | ||
358 | static const char *get_edited_string(struct prefdata *f) { | |
359 | return gtk_entry_get_text(GTK_ENTRY(f->widget)); | |
360 | } | |
361 | ||
348ef780 RK |
362 | static void set_edited_string(struct prefdata *f, const char *value) { |
363 | gtk_entry_set_text(GTK_ENTRY(f->widget), value); | |
364 | } | |
365 | ||
460b9539 | 366 | static void set_string(struct prefdata *f, const char *value) { |
367 | if(strcmp(f->p->default_value, value)) | |
368 | /* Different from default, set it */ | |
369 | disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part, | |
370 | value, 0/*v*/); | |
371 | else | |
372 | /* Same as default, just unset */ | |
373 | disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part, | |
374 | 0/*v*/); | |
375 | } | |
376 | ||
377 | /* Boolean preferences ----------------------------------------------------- */ | |
378 | ||
379 | static void kickoff_boolean(struct prefdata *f) { | |
380 | disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, | |
381 | make_callbackdata(f)); | |
382 | } | |
383 | ||
384 | static void completed_boolean(struct prefdata *f) { | |
385 | f->widget = gtk_check_button_new(); | |
33288048 | 386 | gtk_widget_set_style(f->widget, tool_style); |
460b9539 | 387 | if(!f->value) |
388 | /* Not set, use the default */ | |
389 | f->value = f->p->default_value; | |
460b9539 | 390 | } |
391 | ||
392 | static const char *get_edited_boolean(struct prefdata *f) { | |
393 | return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget)) | |
394 | ? "1" : "0"); | |
395 | } | |
396 | ||
348ef780 RK |
397 | static void set_edited_boolean(struct prefdata *f, const char *value) { |
398 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget), | |
399 | strcmp(value, "0")); | |
400 | } | |
401 | ||
460b9539 | 402 | static void set_boolean(struct prefdata *f, const char *value) { |
403 | char *s; | |
404 | ||
405 | byte_xasprintf(&s, "trackname_display_%s", f->p->part); | |
406 | if(strcmp(value, f->p->default_value)) | |
407 | disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part, value, | |
408 | 0/*v*/); | |
409 | else | |
410 | /* If default value then delete the pref */ | |
411 | disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part, | |
412 | 0/*v*/); | |
413 | } | |
414 | ||
415 | /* Querying preferences ---------------------------------------------------- */ | |
416 | ||
417 | /* Make a suitable callbackdata */ | |
418 | static struct callbackdata *make_callbackdata(struct prefdata *f) { | |
419 | struct callbackdata *cbd = xmalloc(sizeof *cbd); | |
420 | ||
421 | cbd->onerror = prefdata_onerror; | |
422 | cbd->u.f = f; | |
423 | return cbd; | |
424 | } | |
425 | ||
426 | /* No pref was set */ | |
427 | static void prefdata_onerror(struct callbackdata *cbd, | |
428 | int attribute((unused)) code, | |
429 | const char attribute((unused)) *msg) { | |
430 | prefdata_completed_common(cbd->u.f, 0); | |
431 | } | |
432 | ||
433 | /* Got the value of a pref */ | |
434 | static void prefdata_completed(void *v, const char *value) { | |
435 | struct callbackdata *cbd = v; | |
436 | ||
437 | prefdata_completed_common(cbd->u.f, value); | |
438 | } | |
439 | ||
440 | static void prefdata_completed_common(struct prefdata *f, | |
441 | const char *value) { | |
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 | ||
460 | static void properties_ok(GtkButton *button, | |
461 | gpointer userdata) { | |
462 | properties_apply(button, userdata); | |
463 | properties_cancel(button, userdata); | |
464 | } | |
465 | ||
466 | static 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 | ||
485 | static void properties_cancel(GtkButton attribute((unused)) *button, | |
486 | gpointer attribute((unused)) userdata) { | |
487 | gtk_widget_destroy(properties_window); | |
488 | } | |
489 | ||
73f1b9f3 RK |
490 | /** @brief Called on client reset |
491 | * | |
492 | * Destroys the current properties window. | |
493 | */ | |
494 | void properties_reset(void) { | |
495 | if(properties_window) | |
496 | gtk_widget_destroy(properties_window); | |
497 | } | |
498 | ||
460b9539 | 499 | /* |
500 | Local Variables: | |
501 | c-basic-offset:2 | |
502 | comment-column:40 | |
503 | fill-column:79 | |
504 | indent-tabs-mode:nil | |
505 | End: | |
506 | */ |