2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
21 #include "disobedience.h"
23 /* Track properties -------------------------------------------------------- */
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 *error
);
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
);
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
);
46 static void prefdata_completed(void *v
, const char *error
, const char *value
);
47 static void prefdata_onerror(struct callbackdata
*cbd
,
50 static struct callbackdata
*make_callbackdata(struct prefdata
*f
);
51 static void prefdata_completed_common(struct prefdata
*f
,
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
);
58 static void properties_logged_in(const char *event
,
62 /** @brief Data for a single preference */
66 const struct pref
*p
; /**< @brief kind of preference */
67 const char *value
; /**< @brief value from server */
71 /* The type of a preference is the collection of callbacks needed to get,
72 * display and set it */
74 void (*kickoff
)(struct prefdata
*f
);
75 /* Kick off the request to fetch the pref from the server. */
77 void (*completed
)(struct prefdata
*f
);
78 /* Called when the value comes back in; creates the widget. */
80 const char *(*get_edited
)(struct prefdata
*f
);
81 /* Get the edited value from the widget. */
83 /** @brief Update the edited value */
84 void (*set_edited
)(struct prefdata
*f
, const char *value
);
86 void (*set
)(struct prefdata
*f
, const char *value
);
87 /* Set the new value and (if necessary) arrange for our display to update. */
91 static const struct preftype preftype_namepart
= {
100 static const struct preftype preftype_string
= {
109 static const struct preftype preftype_boolean
= {
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 */
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
},
132 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
134 /* Buttons that appear at the bottom of the window */
135 static struct button buttons
[] = {
139 "Apply all changes and close window",
145 "Apply all changes and keep window open",
151 "Discard all changes and close window",
156 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
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
;
166 static void propagate_clicked(GtkButton
attribute((unused
)) *button
,
168 struct prefdata
*f
= (struct prefdata
*)userdata
, *g
;
170 const char *value
= f
->p
->type
->get_edited(f
);
172 for(p
= 0; p
< prefs_total
; ++p
) {
174 if(f
->p
== g
->p
&& f
!= g
)
175 g
->p
->type
->set_edited(g
, value
);
179 void properties(int ntracks
, const char **tracks
) {
182 GtkWidget
*buttonbox
, *vbox
, *label
, *entry
, *propagate
;
184 /* If no tracks, do nothign */
187 /* If there is a properties window open then just bring it to the
188 * front. It might not have the right values in... */
189 if(properties_window
) {
191 gtk_window_present(GTK_WINDOW(properties_window
));
194 assert(properties_table
== 0);
195 if(ntracks
> INT_MAX
/ NPREFS
) {
196 popup_msg(GTK_MESSAGE_ERROR
, "Too many tracks selected");
199 properties_event
= event_register("logged-in", properties_logged_in
, 0);
200 /* Create a new properties window */
201 properties_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
202 gtk_widget_set_style(properties_window
, tool_style
);
203 g_signal_connect(properties_window
, "destroy",
204 G_CALLBACK(gtk_widget_destroyed
), &properties_window
);
205 /* Most of the action is the table of preferences */
206 properties_table
= gtk_table_new((NPREFS
+ 1) * ntracks
, 2 + ntracks
> 1,
208 gtk_widget_set_style(properties_table
, tool_style
);
209 g_signal_connect(properties_table
, "destroy",
210 G_CALLBACK(gtk_widget_destroyed
), &properties_table
);
211 gtk_window_set_title(GTK_WINDOW(properties_window
), "Track Properties");
212 /* Create labels for each pref of each track and kick off requests to the
213 * server to fill in the values */
214 prefs_total
= NPREFS
* ntracks
;
215 prefdatas
= xcalloc(prefs_total
, sizeof *prefdatas
);
216 for(n
= 0; n
< ntracks
; ++n
) {
217 /* The track itself */
219 label
= gtk_label_new("Track");
220 gtk_widget_set_style(label
, tool_style
);
221 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
222 gtk_table_attach(GTK_TABLE(properties_table
),
225 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
229 entry
= gtk_entry_new();
230 gtk_widget_set_style(entry
, tool_style
);
231 gtk_entry_set_text(GTK_ENTRY(entry
), tracks
[n
]);
232 gtk_editable_set_editable(GTK_EDITABLE(entry
), FALSE
);
233 gtk_table_attach(GTK_TABLE(properties_table
),
236 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
237 GTK_EXPAND
|GTK_FILL
, 0,
239 /* Each preference */
240 for(m
= 0; m
< NPREFS
; ++m
) {
242 label
= gtk_label_new(prefs
[m
].label
);
243 gtk_widget_set_style(label
, tool_style
);
244 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
245 gtk_table_attach(GTK_TABLE(properties_table
),
248 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
249 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
251 /* Editing the preference is specific */
252 f
= &prefdatas
[NPREFS
* n
+ m
];
253 f
->track
= tracks
[n
];
254 f
->row
= (NPREFS
+ 1) * n
+ 1 + m
;
256 prefs
[m
].type
->kickoff(f
);
258 /* Propagation button */
259 propagate
= iconbutton("propagate.png", "Copy to other tracks");
260 g_signal_connect(G_OBJECT(propagate
), "clicked",
261 G_CALLBACK(propagate_clicked
), f
);
262 gtk_table_attach(GTK_TABLE(properties_table
),
264 2/*left*/, 3/*right*/,
265 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
266 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
267 1/*xpadding*/, 1/*ypadding*/);
271 prefs_unfilled
= prefs_total
;
273 buttonbox
= create_buttons(buttons
, NBUTTONS
);
274 /* Put it all together */
275 vbox
= gtk_vbox_new(FALSE
, 1);
276 gtk_box_pack_start(GTK_BOX(vbox
),
277 scroll_widget(properties_table
),
279 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 1);
280 gtk_container_add(GTK_CONTAINER(properties_window
), frame_widget(vbox
, NULL
));
281 /* The table only really wants to be vertically scrollable */
282 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table
)->parent
->parent
),
284 GTK_POLICY_AUTOMATIC
);
285 /* Zot any pre-existing progress window just in case */
287 progress_window_progress(pw
, 0, 0);
288 /* Pop up a progress bar while we're waiting */
289 pw
= progress_window_new("Fetching Track Properties");
292 /* Everything is filled in now */
293 static void prefdata_alldone(void) {
295 progress_window_progress(pw
, 0, 0);
298 /* Default size may be too small */
299 gtk_window_set_default_size(GTK_WINDOW(properties_window
), 480, 512);
300 /* TODO: relate default size to required size more closely */
301 gtk_widget_show_all(properties_window
);
304 /* Namepart preferences ---------------------------------------------------- */
306 static void kickoff_namepart(struct prefdata
*f
) {
307 /* We ask for the display name part. This is a bit bizarre if what we really
308 * wanted was the underlying preference, but in fact it should always match
309 * and will supply a sane default without having to know how to parse tracks
310 * names (which implies knowing collection roots). */
311 disorder_eclient_namepart(client
, prefdata_completed
, f
->track
, "display", f
->p
->part
,
312 make_callbackdata(f
));
315 static void completed_namepart(struct prefdata
*f
) {
320 f
->widget
= gtk_entry_new();
323 static const char *get_edited_namepart(struct prefdata
*f
) {
324 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
327 static void set_edited_namepart(struct prefdata
*f
, const char *value
) {
328 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
331 static void set_namepart(struct prefdata
*f
, const char *value
) {
334 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
335 /* We don't know what the default is so can never unset. This is a bug
336 * relative to the original design, which is supposed to only ever allow for
337 * non-trivial namepart preferences. I suppose the server could spot a
338 * default being set and translate it into an unset. */
339 disorder_eclient_set(client
, set_namepart_completed
, f
->track
, s
, value
,
343 /* Called when we've set a namepart */
344 static void set_namepart_completed(void *v
, const char *error
) {
346 popup_protocol_error(0, error
);
348 struct prefdata
*f
= v
;
350 namepart_update(f
->track
, "display", f
->p
->part
);
354 /* String preferences ------------------------------------------------------ */
356 static void kickoff_string(struct prefdata
*f
) {
357 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
358 make_callbackdata(f
));
361 static void completed_string(struct prefdata
*f
) {
363 /* No setting, use the default value instead */
364 f
->value
= f
->p
->default_value
;
365 f
->widget
= gtk_entry_new();
368 static const char *get_edited_string(struct prefdata
*f
) {
369 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
372 static void set_edited_string(struct prefdata
*f
, const char *value
) {
373 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
376 static void set_string_completed(void attribute((unused
)) *v
,
379 popup_protocol_error(0, error
);
382 static void set_string(struct prefdata
*f
, const char *value
) {
383 disorder_eclient_set(client
, set_string_completed
, f
->track
, f
->p
->part
,
387 /* Boolean preferences ----------------------------------------------------- */
389 static void kickoff_boolean(struct prefdata
*f
) {
390 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
391 make_callbackdata(f
));
394 static void completed_boolean(struct prefdata
*f
) {
395 f
->widget
= gtk_check_button_new();
396 gtk_widget_set_style(f
->widget
, tool_style
);
398 /* Not set, use the default */
399 f
->value
= f
->p
->default_value
;
402 static const char *get_edited_boolean(struct prefdata
*f
) {
403 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f
->widget
))
407 static void set_edited_boolean(struct prefdata
*f
, const char *value
) {
408 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f
->widget
),
412 #define set_boolean_completed set_string_completed
414 static void set_boolean(struct prefdata
*f
, const char *value
) {
417 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
418 disorder_eclient_set(client
, set_boolean_completed
,
419 f
->track
, f
->p
->part
, value
, 0/*v*/);
422 /* Querying preferences ---------------------------------------------------- */
424 /* Make a suitable callbackdata */
425 static struct callbackdata
*make_callbackdata(struct prefdata
*f
) {
426 struct callbackdata
*cbd
= xmalloc(sizeof *cbd
);
428 cbd
->onerror
= prefdata_onerror
;
433 /* No pref was set */
434 static void prefdata_onerror(struct callbackdata
*cbd
,
435 int attribute((unused
)) code
,
436 const char attribute((unused
)) *msg
) {
437 prefdata_completed_common(cbd
->u
.f
, 0);
440 /* Got the value of a pref */
441 static void prefdata_completed(void *v
, const char *error
, const char *value
) {
444 struct callbackdata
*cbd
= v
;
446 prefdata_completed_common(cbd
->u
.f
, value
);
450 static void prefdata_completed_common(struct prefdata
*f
,
453 f
->p
->type
->completed(f
);
454 f
->p
->type
->set_edited(f
, f
->value
);
455 assert(f
->value
!= 0); /* Had better set a default */
456 gtk_table_attach(GTK_TABLE(properties_table
), f
->widget
,
459 GTK_EXPAND
|GTK_FILL
/*xoptions*/, 0/*yoptions*/,
463 progress_window_progress(pw
, prefs_total
- prefs_unfilled
, prefs_total
);
468 /* Button callbacks -------------------------------------------------------- */
470 static void properties_ok(GtkButton
*button
,
472 properties_apply(button
, userdata
);
473 properties_cancel(button
, userdata
);
476 static void properties_apply(GtkButton
attribute((unused
)) *button
,
477 gpointer
attribute((unused
)) userdata
) {
482 /* For each possible property we see if we've changed it and if so tell the
484 for(n
= 0; n
< prefs_total
; ++n
) {
486 edited
= f
->p
->type
->get_edited(f
);
487 if(strcmp(edited
, f
->value
)) {
488 /* The value has changed */
489 f
->p
->type
->set(f
, edited
);
490 f
->value
= xstrdup(edited
);
495 static void properties_cancel(GtkButton
attribute((unused
)) *button
,
496 gpointer
attribute((unused
)) userdata
) {
497 gtk_widget_destroy(properties_window
);
498 event_cancel(properties_event
);
499 properties_event
= 0;
502 /** @brief Called when we've just logged in
504 * Destroys the current properties window.
506 static void properties_logged_in(const char attribute((unused
)) *event
,
507 void attribute((unused
)) *eventdata
,
508 void attribute((unused
)) *callbackdata
) {
509 if(properties_window
)
510 gtk_widget_destroy(properties_window
);