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
);
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 *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 /** @brief Data for a single preference */
62 const struct pref
*p
; /**< @brief kind of preference */
63 const char *value
; /**< @brief value from server */
67 /* The type of a preference is the collection of callbacks needed to get,
68 * display and set it */
70 void (*kickoff
)(struct prefdata
*f
);
71 /* Kick off the request to fetch the pref from the server. */
73 void (*completed
)(struct prefdata
*f
);
74 /* Called when the value comes back in; creates the widget. */
76 const char *(*get_edited
)(struct prefdata
*f
);
77 /* Get the edited value from the widget. */
79 /** @brief Update the edited value */
80 void (*set_edited
)(struct prefdata
*f
, const char *value
);
82 void (*set
)(struct prefdata
*f
, const char *value
);
83 /* Set the new value and (if necessary) arrange for our display to update. */
87 static const struct preftype preftype_namepart
= {
96 static const struct preftype preftype_string
= {
105 static const struct preftype preftype_boolean
= {
113 /* @brief The known prefs for each track */
114 static const struct pref
{
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 */
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
},
127 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
129 /* Buttons that appear at the bottom of the window */
130 static const struct button buttons
[] = {
134 "Apply all changes and close window"
139 "Apply all changes and keep window open"
144 "Discard all changes and close window"
148 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
150 static int prefs_unfilled
; /* Prefs remaining to get */
151 static int prefs_total
; /* Total prefs */
152 static struct prefdata
*prefdatas
; /* Current prefdatas */
153 static GtkWidget
*properties_window
;
154 static GtkWidget
*properties_table
;
155 static struct progress_window
*pw
;
157 static void propagate_clicked(GtkButton
attribute((unused
)) *button
,
159 struct prefdata
*f
= (struct prefdata
*)userdata
, *g
;
161 const char *value
= f
->p
->type
->get_edited(f
);
163 for(p
= 0; p
< prefs_total
; ++p
) {
165 if(f
->p
== g
->p
&& f
!= g
)
166 g
->p
->type
->set_edited(g
, value
);
170 void properties(int ntracks
, const char **tracks
) {
173 GtkWidget
*buttonbox
, *vbox
, *label
, *entry
, *propagate
;
175 /* If no tracks, do nothign */
178 /* If there is a properties window open then just bring it to the
179 * front. It might not have the right values in... */
180 if(properties_window
) {
182 gtk_window_present(GTK_WINDOW(properties_window
));
185 assert(properties_table
== 0);
186 if(ntracks
> INT_MAX
/ NPREFS
) {
187 popup_msg(GTK_MESSAGE_ERROR
, "Too many tracks selected");
190 /* Create a new properties window */
191 properties_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
192 gtk_widget_set_style(properties_window
, tool_style
);
193 g_signal_connect(properties_window
, "destroy",
194 G_CALLBACK(gtk_widget_destroyed
), &properties_window
);
195 /* Most of the action is the table of preferences */
196 properties_table
= gtk_table_new((NPREFS
+ 1) * ntracks
, 2 + ntracks
> 1,
198 gtk_widget_set_style(properties_table
, tool_style
);
199 g_signal_connect(properties_table
, "destroy",
200 G_CALLBACK(gtk_widget_destroyed
), &properties_table
);
201 gtk_window_set_title(GTK_WINDOW(properties_window
), "Track Properties");
202 /* Create labels for each pref of each track and kick off requests to the
203 * server to fill in the values */
204 prefs_total
= NPREFS
* ntracks
;
205 prefdatas
= xcalloc(prefs_total
, sizeof *prefdatas
);
206 for(n
= 0; n
< ntracks
; ++n
) {
207 /* The track itself */
209 label
= gtk_label_new("Track");
210 gtk_widget_set_style(label
, tool_style
);
211 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
212 gtk_table_attach(GTK_TABLE(properties_table
),
215 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
219 entry
= gtk_entry_new();
220 gtk_widget_set_style(entry
, tool_style
);
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
),
226 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
227 GTK_EXPAND
|GTK_FILL
, 0,
229 /* Each preference */
230 for(m
= 0; m
< NPREFS
; ++m
) {
232 label
= gtk_label_new(prefs
[m
].label
);
233 gtk_widget_set_style(label
, tool_style
);
234 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
235 gtk_table_attach(GTK_TABLE(properties_table
),
238 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
239 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
241 /* Editing the preference is specific */
242 f
= &prefdatas
[NPREFS
* n
+ m
];
243 f
->track
= tracks
[n
];
244 f
->row
= (NPREFS
+ 1) * n
+ 1 + m
;
246 prefs
[m
].type
->kickoff(f
);
248 /* Propagation button */
249 propagate
= iconbutton("propagate.png", "Copy to other tracks");
250 g_signal_connect(G_OBJECT(propagate
), "clicked",
251 G_CALLBACK(propagate_clicked
), f
);
252 gtk_table_attach(GTK_TABLE(properties_table
),
254 2/*left*/, 3/*right*/,
255 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
256 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
257 1/*xpadding*/, 1/*ypadding*/);
261 prefs_unfilled
= prefs_total
;
263 buttonbox
= create_buttons(buttons
, NBUTTONS
);
264 /* Put it all together */
265 vbox
= gtk_vbox_new(FALSE
, 1);
266 gtk_box_pack_start(GTK_BOX(vbox
),
267 scroll_widget(properties_table
),
269 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 1);
270 gtk_container_add(GTK_CONTAINER(properties_window
), vbox
);
271 /* The table only really wants to be vertically scrollable */
272 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table
)->parent
->parent
),
274 GTK_POLICY_AUTOMATIC
);
275 /* Zot any pre-existing progress window just in case */
277 progress_window_progress(pw
, 0, 0);
278 /* Pop up a progress bar while we're waiting */
279 pw
= progress_window_new("Fetching Track Properties");
282 /* Everything is filled in now */
283 static void prefdata_alldone(void) {
285 progress_window_progress(pw
, 0, 0);
288 /* Default size may be too small */
289 gtk_window_set_default_size(GTK_WINDOW(properties_window
), 480, 512);
290 /* TODO: relate default size to required size more closely */
291 gtk_widget_show_all(properties_window
);
294 /* Namepart preferences ---------------------------------------------------- */
296 static void kickoff_namepart(struct prefdata
*f
) {
297 /* We ask for the display name part. This is a bit bizarre if what we really
298 * wanted was the underlying preference, but in fact it should always match
299 * and will supply a sane default without having to know how to parse tracks
300 * names (which implies knowing collection roots). */
301 disorder_eclient_namepart(client
, prefdata_completed
, f
->track
, "display", f
->p
->part
,
302 make_callbackdata(f
));
305 static void completed_namepart(struct prefdata
*f
) {
310 f
->widget
= gtk_entry_new();
313 static const char *get_edited_namepart(struct prefdata
*f
) {
314 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
317 static void set_edited_namepart(struct prefdata
*f
, const char *value
) {
318 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
321 static void set_namepart(struct prefdata
*f
, const char *value
) {
323 struct callbackdata
*cbd
= xmalloc(sizeof *cbd
);
326 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
327 /* We don't know what the default is so can never unset. This is a bug
328 * relative to the original design, which is supposed to only ever allow for
329 * non-trivial namepart preferences. I suppose the server could spot a
330 * default being set and translate it into an unset. */
331 disorder_eclient_set(client
, set_namepart_completed
, f
->track
, s
, value
,
335 /* Called when we've set a namepart */
336 static void set_namepart_completed(void *v
) {
337 struct callbackdata
*cbd
= v
;
338 struct prefdata
*f
= cbd
->u
.f
;
340 namepart_update(f
->track
, "display", f
->p
->part
);
343 /* String preferences ------------------------------------------------------ */
345 static void kickoff_string(struct prefdata
*f
) {
346 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
347 make_callbackdata(f
));
350 static void completed_string(struct prefdata
*f
) {
352 /* No setting, use the default value instead */
353 f
->value
= f
->p
->default_value
;
354 f
->widget
= gtk_entry_new();
357 static const char *get_edited_string(struct prefdata
*f
) {
358 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
361 static void set_edited_string(struct prefdata
*f
, const char *value
) {
362 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
365 static void set_string(struct prefdata
*f
, const char *value
) {
366 if(strcmp(f
->p
->default_value
, value
))
367 /* Different from default, set it */
368 disorder_eclient_set(client
, 0/*completed*/, f
->track
, f
->p
->part
,
371 /* Same as default, just unset */
372 disorder_eclient_unset(client
, 0/*completed*/, f
->track
, f
->p
->part
,
376 /* Boolean preferences ----------------------------------------------------- */
378 static void kickoff_boolean(struct prefdata
*f
) {
379 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
380 make_callbackdata(f
));
383 static void completed_boolean(struct prefdata
*f
) {
384 f
->widget
= gtk_check_button_new();
385 gtk_widget_set_style(f
->widget
, tool_style
);
387 /* Not set, use the default */
388 f
->value
= f
->p
->default_value
;
391 static const char *get_edited_boolean(struct prefdata
*f
) {
392 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f
->widget
))
396 static void set_edited_boolean(struct prefdata
*f
, const char *value
) {
397 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f
->widget
),
401 static void set_boolean(struct prefdata
*f
, const char *value
) {
404 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
405 if(strcmp(value
, f
->p
->default_value
))
406 disorder_eclient_set(client
, 0/*completed*/, f
->track
, f
->p
->part
, value
,
409 /* If default value then delete the pref */
410 disorder_eclient_unset(client
, 0/*completed*/, f
->track
, f
->p
->part
,
414 /* Querying preferences ---------------------------------------------------- */
416 /* Make a suitable callbackdata */
417 static struct callbackdata
*make_callbackdata(struct prefdata
*f
) {
418 struct callbackdata
*cbd
= xmalloc(sizeof *cbd
);
420 cbd
->onerror
= prefdata_onerror
;
425 /* No pref was set */
426 static void prefdata_onerror(struct callbackdata
*cbd
,
427 int attribute((unused
)) code
,
428 const char attribute((unused
)) *msg
) {
429 prefdata_completed_common(cbd
->u
.f
, 0);
432 /* Got the value of a pref */
433 static void prefdata_completed(void *v
, const char *value
) {
434 struct callbackdata
*cbd
= v
;
436 prefdata_completed_common(cbd
->u
.f
, value
);
439 static void prefdata_completed_common(struct prefdata
*f
,
442 f
->p
->type
->completed(f
);
443 f
->p
->type
->set_edited(f
, f
->value
);
444 assert(f
->value
!= 0); /* Had better set a default */
445 gtk_table_attach(GTK_TABLE(properties_table
), f
->widget
,
448 GTK_EXPAND
|GTK_FILL
/*xoptions*/, 0/*yoptions*/,
452 progress_window_progress(pw
, prefs_total
- prefs_unfilled
, prefs_total
);
457 /* Button callbacks -------------------------------------------------------- */
459 static void properties_ok(GtkButton
*button
,
461 properties_apply(button
, userdata
);
462 properties_cancel(button
, userdata
);
465 static void properties_apply(GtkButton
attribute((unused
)) *button
,
466 gpointer
attribute((unused
)) userdata
) {
471 /* For each possible property we see if we've changed it and if so tell the
473 for(n
= 0; n
< prefs_total
; ++n
) {
475 edited
= f
->p
->type
->get_edited(f
);
476 if(strcmp(edited
, f
->value
)) {
477 /* The value has changed */
478 f
->p
->type
->set(f
, edited
);
479 f
->value
= xstrdup(edited
);
484 static void properties_cancel(GtkButton
attribute((unused
)) *button
,
485 gpointer
attribute((unused
)) userdata
) {
486 gtk_widget_destroy(properties_window
);
489 /** @brief Called on client reset
491 * Destroys the current properties window.
493 void properties_reset(void) {
494 if(properties_window
)
495 gtk_widget_destroy(properties_window
);