2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 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
20 /** @file disobedience/properties.c
21 * @brief Track properties editor
24 * - return and escape keys should work
26 #include "disobedience.h"
30 static void kickoff_namepart(struct prefdata
*f
);
31 static void completed_namepart(struct prefdata
*f
);
32 static const char *get_edited_namepart(struct prefdata
*f
);
33 static void set_edited_namepart(struct prefdata
*f
, const char *value
);
34 static void set_namepart(struct prefdata
*f
, const char *value
);
35 static void set_namepart_completed(void *v
, const char *err
);
37 static void kickoff_string(struct prefdata
*f
);
38 static void completed_string(struct prefdata
*f
);
39 static const char *get_edited_string(struct prefdata
*f
);
40 static void set_edited_string(struct prefdata
*f
, const char *value
);
41 static void set_string(struct prefdata
*f
, const char *value
);
43 static void kickoff_boolean(struct prefdata
*f
);
44 static void completed_boolean(struct prefdata
*f
);
45 static const char *get_edited_boolean(struct prefdata
*f
);
46 static void set_edited_boolean(struct prefdata
*f
, const char *value
);
47 static void set_boolean(struct prefdata
*f
, const char *value
);
49 static void prefdata_completed(void *v
, const char *err
, const char *value
);
50 static void prefdata_onerror(struct callbackdata
*cbd
,
53 static struct callbackdata
*make_callbackdata(struct prefdata
*f
);
54 static void prefdata_completed_common(struct prefdata
*f
,
57 static void properties_ok(GtkButton
*button
, gpointer userdata
);
58 static void properties_apply(GtkButton
*button
, gpointer userdata
);
59 static void properties_cancel(GtkButton
*button
, gpointer userdata
);
61 static void properties_logged_in(const char *event
,
65 /** @brief Data for a single preference */
69 const struct pref
*p
; /**< @brief kind of preference */
70 const char *value
; /**< @brief value from server */
74 /* The type of a preference is the collection of callbacks needed to get,
75 * display and set it */
77 void (*kickoff
)(struct prefdata
*f
);
78 /* Kick off the request to fetch the pref from the server. */
80 void (*completed
)(struct prefdata
*f
);
81 /* Called when the value comes back in; creates the widget. */
83 const char *(*get_edited
)(struct prefdata
*f
);
84 /* Get the edited value from the widget. */
86 /** @brief Update the edited value */
87 void (*set_edited
)(struct prefdata
*f
, const char *value
);
89 void (*set
)(struct prefdata
*f
, const char *value
);
90 /* Set the new value and (if necessary) arrange for our display to update. */
94 static const struct preftype preftype_namepart
= {
103 static const struct preftype preftype_string
= {
112 static const struct preftype preftype_boolean
= {
120 /* @brief The known prefs for each track */
121 static const struct pref
{
122 const char *label
; /**< @brief user-level description */
123 const char *part
; /**< @brief protocol-level tag */
124 const char *default_value
; /**< @brief default value or NULL */
125 const struct preftype
*type
; /**< @brief underlying data type */
127 { "Artist", "artist", 0, &preftype_namepart
},
128 { "Album", "album", 0, &preftype_namepart
},
129 { "Title", "title", 0, &preftype_namepart
},
130 { "Tags", "tags", "", &preftype_string
},
131 { "Weight", "weight", "90000", &preftype_string
},
132 { "Random", "pick_at_random", "1", &preftype_boolean
},
135 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
137 /* Buttons that appear at the bottom of the window */
138 static struct button buttons
[] = {
142 "Apply all changes and close window",
148 "Apply all changes and keep window open",
154 "Discard all changes and close window",
159 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
161 static int prefs_unfilled
; /* Prefs remaining to get */
162 static int prefs_total
; /* Total prefs */
163 static struct prefdata
*prefdatas
; /* Current prefdatas */
164 static GtkWidget
*properties_window
;
165 static GtkWidget
*properties_table
;
166 static struct progress_window
*pw
;
167 static event_handle properties_event
;
169 static void propagate_clicked(GtkButton
attribute((unused
)) *button
,
171 struct prefdata
*f
= (struct prefdata
*)userdata
, *g
;
173 const char *value
= f
->p
->type
->get_edited(f
);
175 for(p
= 0; p
< prefs_total
; ++p
) {
177 if(f
->p
== g
->p
&& f
!= g
)
178 g
->p
->type
->set_edited(g
, value
);
182 void properties(int ntracks
, const char **tracks
) {
185 GtkWidget
*buttonbox
, *vbox
, *label
, *entry
, *propagate
;
187 /* If no tracks, do nothign */
190 /* If there is a properties window open then just bring it to the
191 * front. It might not have the right values in... */
192 if(properties_window
) {
194 gtk_window_present(GTK_WINDOW(properties_window
));
197 assert(properties_table
== 0);
198 if(ntracks
> INT_MAX
/ NPREFS
) {
199 popup_msg(GTK_MESSAGE_ERROR
, "Too many tracks selected");
202 properties_event
= event_register("logged-in", properties_logged_in
, 0);
203 /* Create a new properties window */
204 properties_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
205 gtk_widget_set_style(properties_window
, tool_style
);
206 g_signal_connect(properties_window
, "destroy",
207 G_CALLBACK(gtk_widget_destroyed
), &properties_window
);
208 /* Most of the action is the table of preferences */
209 properties_table
= gtk_table_new((NPREFS
+ 1) * ntracks
, 2 + ntracks
> 1,
211 gtk_widget_set_style(properties_table
, tool_style
);
212 g_signal_connect(properties_table
, "destroy",
213 G_CALLBACK(gtk_widget_destroyed
), &properties_table
);
214 gtk_window_set_title(GTK_WINDOW(properties_window
), "Track Properties");
215 /* Create labels for each pref of each track and kick off requests to the
216 * server to fill in the values */
217 prefs_total
= NPREFS
* ntracks
;
218 prefdatas
= xcalloc(prefs_total
, sizeof *prefdatas
);
219 for(n
= 0; n
< ntracks
; ++n
) {
220 /* The track itself */
222 label
= gtk_label_new("Track");
223 gtk_widget_set_style(label
, tool_style
);
224 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
225 gtk_table_attach(GTK_TABLE(properties_table
),
228 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
232 entry
= gtk_entry_new();
233 gtk_widget_set_style(entry
, tool_style
);
234 gtk_entry_set_text(GTK_ENTRY(entry
), tracks
[n
]);
235 gtk_editable_set_editable(GTK_EDITABLE(entry
), FALSE
);
236 gtk_table_attach(GTK_TABLE(properties_table
),
239 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
240 GTK_EXPAND
|GTK_FILL
, 0,
242 /* Each preference */
243 for(m
= 0; m
< NPREFS
; ++m
) {
245 label
= gtk_label_new(prefs
[m
].label
);
246 gtk_widget_set_style(label
, tool_style
);
247 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
248 gtk_table_attach(GTK_TABLE(properties_table
),
251 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
252 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
254 /* Editing the preference is specific */
255 f
= &prefdatas
[NPREFS
* n
+ m
];
256 f
->track
= tracks
[n
];
257 f
->row
= (NPREFS
+ 1) * n
+ 1 + m
;
259 prefs
[m
].type
->kickoff(f
);
261 /* Propagation button */
262 propagate
= iconbutton("propagate.png", "Copy to other tracks");
263 g_signal_connect(G_OBJECT(propagate
), "clicked",
264 G_CALLBACK(propagate_clicked
), f
);
265 gtk_table_attach(GTK_TABLE(properties_table
),
267 2/*left*/, 3/*right*/,
268 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
269 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
270 1/*xpadding*/, 1/*ypadding*/);
274 prefs_unfilled
= prefs_total
;
276 buttonbox
= create_buttons(buttons
, NBUTTONS
);
277 /* Put it all together */
278 vbox
= gtk_vbox_new(FALSE
, 1);
279 gtk_box_pack_start(GTK_BOX(vbox
),
280 scroll_widget(properties_table
),
282 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 1);
283 gtk_container_add(GTK_CONTAINER(properties_window
), frame_widget(vbox
, NULL
));
284 /* The table only really wants to be vertically scrollable */
285 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table
)->parent
->parent
),
287 GTK_POLICY_AUTOMATIC
);
288 /* Zot any pre-existing progress window just in case */
290 progress_window_progress(pw
, 0, 0);
291 /* Pop up a progress bar while we're waiting */
292 pw
= progress_window_new("Fetching Track Properties");
295 /* Everything is filled in now */
296 static void prefdata_alldone(void) {
298 progress_window_progress(pw
, 0, 0);
301 /* Default size may be too small */
302 gtk_window_set_default_size(GTK_WINDOW(properties_window
), 480, 512);
303 /* TODO: relate default size to required size more closely */
304 gtk_widget_show_all(properties_window
);
307 /* Namepart preferences ---------------------------------------------------- */
309 static void kickoff_namepart(struct prefdata
*f
) {
310 /* We ask for the display name part. This is a bit bizarre if what we really
311 * wanted was the underlying preference, but in fact it should always match
312 * and will supply a sane default without having to know how to parse tracks
313 * names (which implies knowing collection roots). */
314 disorder_eclient_namepart(client
, prefdata_completed
, f
->track
, "display", f
->p
->part
,
315 make_callbackdata(f
));
318 static void completed_namepart(struct prefdata
*f
) {
323 f
->widget
= gtk_entry_new();
326 static const char *get_edited_namepart(struct prefdata
*f
) {
327 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
330 static void set_edited_namepart(struct prefdata
*f
, const char *value
) {
331 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
334 static void set_namepart(struct prefdata
*f
, const char *value
) {
337 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
338 /* We don't know what the default is so can never unset. This is a bug
339 * relative to the original design, which is supposed to only ever allow for
340 * non-trivial namepart preferences. I suppose the server could spot a
341 * default being set and translate it into an unset. */
342 disorder_eclient_set(client
, set_namepart_completed
, f
->track
, s
, value
,
346 /* Called when we've set a namepart */
347 static void set_namepart_completed(void *v
, const char *err
) {
349 popup_protocol_error(0, err
);
351 struct prefdata
*f
= v
;
353 namepart_update(f
->track
, "display", f
->p
->part
);
357 /* String preferences ------------------------------------------------------ */
359 static void kickoff_string(struct prefdata
*f
) {
360 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
361 make_callbackdata(f
));
364 static void completed_string(struct prefdata
*f
) {
366 /* No setting, use the default value instead */
367 f
->value
= f
->p
->default_value
;
368 f
->widget
= gtk_entry_new();
371 static const char *get_edited_string(struct prefdata
*f
) {
372 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
375 static void set_edited_string(struct prefdata
*f
, const char *value
) {
376 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
379 static void set_string_completed(void attribute((unused
)) *v
,
382 popup_protocol_error(0, err
);
385 static void set_string(struct prefdata
*f
, const char *value
) {
386 disorder_eclient_set(client
, set_string_completed
, f
->track
, f
->p
->part
,
390 /* Boolean preferences ----------------------------------------------------- */
392 static void kickoff_boolean(struct prefdata
*f
) {
393 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
,
394 make_callbackdata(f
));
397 static void completed_boolean(struct prefdata
*f
) {
398 f
->widget
= gtk_check_button_new();
399 gtk_widget_set_style(f
->widget
, tool_style
);
401 /* Not set, use the default */
402 f
->value
= f
->p
->default_value
;
405 static const char *get_edited_boolean(struct prefdata
*f
) {
406 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f
->widget
))
410 static void set_edited_boolean(struct prefdata
*f
, const char *value
) {
411 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f
->widget
),
415 #define set_boolean_completed set_string_completed
417 static void set_boolean(struct prefdata
*f
, const char *value
) {
420 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
421 disorder_eclient_set(client
, set_boolean_completed
,
422 f
->track
, f
->p
->part
, value
, 0/*v*/);
425 /* Querying preferences ---------------------------------------------------- */
427 /* Make a suitable callbackdata */
428 static struct callbackdata
*make_callbackdata(struct prefdata
*f
) {
429 struct callbackdata
*cbd
= xmalloc(sizeof *cbd
);
431 cbd
->onerror
= prefdata_onerror
;
436 /* No pref was set */
437 static void prefdata_onerror(struct callbackdata
*cbd
,
438 int attribute((unused
)) code
,
439 const char attribute((unused
)) *msg
) {
440 prefdata_completed_common(cbd
->u
.f
, 0);
443 /* Got the value of a pref */
444 static void prefdata_completed(void *v
, const char *err
, const char *value
) {
447 struct callbackdata
*cbd
= v
;
449 prefdata_completed_common(cbd
->u
.f
, value
);
453 static void prefdata_completed_common(struct prefdata
*f
,
456 f
->p
->type
->completed(f
);
457 f
->p
->type
->set_edited(f
, f
->value
);
458 assert(f
->value
!= 0); /* Had better set a default */
459 gtk_table_attach(GTK_TABLE(properties_table
), f
->widget
,
462 GTK_EXPAND
|GTK_FILL
/*xoptions*/, 0/*yoptions*/,
466 progress_window_progress(pw
, prefs_total
- prefs_unfilled
, prefs_total
);
471 /* Button callbacks -------------------------------------------------------- */
473 static void properties_ok(GtkButton
*button
,
475 properties_apply(button
, userdata
);
476 properties_cancel(button
, userdata
);
479 static void properties_apply(GtkButton
attribute((unused
)) *button
,
480 gpointer
attribute((unused
)) userdata
) {
485 /* For each possible property we see if we've changed it and if so tell the
487 for(n
= 0; n
< prefs_total
; ++n
) {
489 edited
= f
->p
->type
->get_edited(f
);
490 if(strcmp(edited
, f
->value
)) {
491 /* The value has changed */
492 f
->p
->type
->set(f
, edited
);
493 f
->value
= xstrdup(edited
);
498 static void properties_cancel(GtkButton
attribute((unused
)) *button
,
499 gpointer
attribute((unused
)) userdata
) {
500 gtk_widget_destroy(properties_window
);
501 event_cancel(properties_event
);
502 properties_event
= 0;
505 /** @brief Called when we've just logged in
507 * Destroys the current properties window.
509 static void properties_logged_in(const char attribute((unused
)) *event
,
510 void attribute((unused
)) *eventdata
,
511 void attribute((unused
)) *callbackdata
) {
512 if(properties_window
)
513 gtk_widget_destroy(properties_window
);