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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/properties.c
19 * @brief Track properties editor
21 #include "disobedience.h"
25 static void kickoff_namepart(struct prefdata
*f
);
26 static void completed_namepart(struct prefdata
*f
);
27 static const char *get_edited_namepart(struct prefdata
*f
);
28 static void set_edited_namepart(struct prefdata
*f
, const char *value
);
29 static void set_namepart(struct prefdata
*f
, const char *value
);
30 static void set_namepart_completed(void *v
, const char *err
);
32 static void kickoff_string(struct prefdata
*f
);
33 static void completed_string(struct prefdata
*f
);
34 static const char *get_edited_string(struct prefdata
*f
);
35 static void set_edited_string(struct prefdata
*f
, const char *value
);
36 static void set_string(struct prefdata
*f
, const char *value
);
38 static void kickoff_boolean(struct prefdata
*f
);
39 static void completed_boolean(struct prefdata
*f
);
40 static const char *get_edited_boolean(struct prefdata
*f
);
41 static void set_edited_boolean(struct prefdata
*f
, const char *value
);
42 static void set_boolean(struct prefdata
*f
, const char *value
);
44 static void prefdata_completed(void *v
, const char *err
, const char *value
);
46 static void properties_ok(GtkButton
*button
, gpointer userdata
);
47 static void properties_apply(GtkButton
*button
, gpointer userdata
);
48 static void properties_cancel(GtkButton
*button
, gpointer userdata
);
50 static void properties_logged_in(const char *event
,
54 /** @brief Data for a single preference */
58 const struct pref
*p
; /**< @brief kind of preference */
59 const char *value
; /**< @brief value from server */
63 /* The type of a preference is the collection of callbacks needed to get,
64 * display and set it */
66 void (*kickoff
)(struct prefdata
*f
);
67 /* Kick off the request to fetch the pref from the server. */
69 void (*completed
)(struct prefdata
*f
);
70 /* Called when the value comes back in; creates the widget. */
72 const char *(*get_edited
)(struct prefdata
*f
);
73 /* Get the edited value from the widget. */
75 /** @brief Update the edited value */
76 void (*set_edited
)(struct prefdata
*f
, const char *value
);
78 void (*set
)(struct prefdata
*f
, const char *value
);
79 /* Set the new value and (if necessary) arrange for our display to update. */
83 static const struct preftype preftype_namepart
= {
92 static const struct preftype preftype_string
= {
101 static const struct preftype preftype_boolean
= {
109 /* @brief The known prefs for each track */
110 static const struct pref
{
111 const char *label
; /**< @brief user-level description */
112 const char *part
; /**< @brief protocol-level tag */
113 const char *default_value
; /**< @brief default value or NULL */
114 const struct preftype
*type
; /**< @brief underlying data type */
116 { "Artist", "artist", 0, &preftype_namepart
},
117 { "Album", "album", 0, &preftype_namepart
},
118 { "Title", "title", 0, &preftype_namepart
},
119 { "Tags", "tags", "", &preftype_string
},
120 { "Weight", "weight", "90000", &preftype_string
},
121 { "Random", "pick_at_random", "1", &preftype_boolean
},
124 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
126 /* Buttons that appear at the bottom of the window */
127 static struct button buttons
[] = {
131 "Apply all changes and close window",
137 "Apply all changes and keep window open",
143 "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
;
156 static event_handle properties_event
;
158 static void propagate_clicked(GtkButton
attribute((unused
)) *button
,
160 struct prefdata
*f
= (struct prefdata
*)userdata
, *g
;
162 const char *value
= f
->p
->type
->get_edited(f
);
164 for(p
= 0; p
< prefs_total
; ++p
) {
166 if(f
->p
== g
->p
&& f
!= g
)
167 g
->p
->type
->set_edited(g
, value
);
171 /** @brief Keypress handler */
172 static gboolean
properties_keypress(GtkWidget
attribute((unused
)) *widget
,
174 gpointer
attribute((unused
)) user_data
) {
177 switch(event
->keyval
) {
182 properties_cancel(0, 0);
189 void properties(int ntracks
, const char **tracks
) {
192 GtkWidget
*buttonbox
, *vbox
, *label
, *entry
, *propagate
;
194 /* If no tracks, do nothign */
197 /* If there is a properties window open then just bring it to the
198 * front. It might not have the right values in... */
199 if(properties_window
) {
201 gtk_window_present(GTK_WINDOW(properties_window
));
204 assert(properties_table
== 0);
205 if(ntracks
> INT_MAX
/ NPREFS
) {
206 popup_msg(GTK_MESSAGE_ERROR
, "Too many tracks selected");
209 properties_event
= event_register("logged-in", properties_logged_in
, 0);
210 /* Create a new properties window */
211 properties_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
212 gtk_widget_set_style(properties_window
, tool_style
);
213 g_signal_connect(properties_window
, "destroy",
214 G_CALLBACK(gtk_widget_destroyed
), &properties_window
);
215 /* Keyboard shortcuts */
216 g_signal_connect(properties_window
, "key-press-event",
217 G_CALLBACK(properties_keypress
), 0);
218 /* Most of the action is the table of preferences */
219 properties_table
= gtk_table_new((NPREFS
+ 1) * ntracks
, 2 + ntracks
> 1,
221 gtk_widget_set_style(properties_table
, tool_style
);
222 g_signal_connect(properties_table
, "destroy",
223 G_CALLBACK(gtk_widget_destroyed
), &properties_table
);
224 gtk_window_set_title(GTK_WINDOW(properties_window
), "Track Properties");
225 /* Create labels for each pref of each track and kick off requests to the
226 * server to fill in the values */
227 prefs_total
= NPREFS
* ntracks
;
228 prefdatas
= xcalloc(prefs_total
, sizeof *prefdatas
);
229 for(n
= 0; n
< ntracks
; ++n
) {
230 /* The track itself */
232 label
= gtk_label_new("Track");
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
, (NPREFS
+ 1) * n
+ 1,
242 entry
= gtk_entry_new();
243 gtk_widget_set_style(entry
, tool_style
);
244 gtk_entry_set_text(GTK_ENTRY(entry
), tracks
[n
]);
245 gtk_editable_set_editable(GTK_EDITABLE(entry
), FALSE
);
246 gtk_table_attach(GTK_TABLE(properties_table
),
249 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
250 GTK_EXPAND
|GTK_FILL
, 0,
252 /* Each preference */
253 for(m
= 0; m
< NPREFS
; ++m
) {
255 label
= gtk_label_new(prefs
[m
].label
);
256 gtk_widget_set_style(label
, tool_style
);
257 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
258 gtk_table_attach(GTK_TABLE(properties_table
),
261 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
262 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
264 /* Editing the preference is specific */
265 f
= &prefdatas
[NPREFS
* n
+ m
];
266 f
->track
= tracks
[n
];
267 f
->row
= (NPREFS
+ 1) * n
+ 1 + m
;
269 prefs
[m
].type
->kickoff(f
);
271 /* Propagation button */
272 propagate
= iconbutton("propagate.png", "Copy to other tracks");
273 g_signal_connect(G_OBJECT(propagate
), "clicked",
274 G_CALLBACK(propagate_clicked
), f
);
275 gtk_table_attach(GTK_TABLE(properties_table
),
277 2/*left*/, 3/*right*/,
278 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
279 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
280 1/*xpadding*/, 1/*ypadding*/);
284 prefs_unfilled
= prefs_total
;
286 buttonbox
= create_buttons(buttons
, NBUTTONS
);
287 /* Put it all together */
288 vbox
= gtk_vbox_new(FALSE
, 1);
289 gtk_box_pack_start(GTK_BOX(vbox
),
290 scroll_widget(properties_table
),
292 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 1);
293 gtk_container_add(GTK_CONTAINER(properties_window
), frame_widget(vbox
, NULL
));
294 /* The table only really wants to be vertically scrollable */
295 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table
)->parent
->parent
),
297 GTK_POLICY_AUTOMATIC
);
298 /* Zot any pre-existing progress window just in case */
300 progress_window_progress(pw
, 0, 0);
301 /* Pop up a progress bar while we're waiting */
302 pw
= progress_window_new("Fetching Track Properties");
305 /* Everything is filled in now */
306 static void prefdata_alldone(void) {
308 progress_window_progress(pw
, 0, 0);
311 /* Default size may be too small */
312 gtk_window_set_default_size(GTK_WINDOW(properties_window
), 480, 512);
313 /* TODO: relate default size to required size more closely */
314 gtk_widget_show_all(properties_window
);
317 /* Namepart preferences ---------------------------------------------------- */
319 static void kickoff_namepart(struct prefdata
*f
) {
320 /* We ask for the display name part. This is a bit bizarre if what we really
321 * wanted was the underlying preference, but in fact it should always match
322 * and will supply a sane default without having to know how to parse tracks
323 * names (which implies knowing collection roots). */
324 disorder_eclient_namepart(client
, prefdata_completed
,
325 f
->track
, "display", f
->p
->part
, f
);
328 static void completed_namepart(struct prefdata
*f
) {
333 f
->widget
= gtk_entry_new();
336 static const char *get_edited_namepart(struct prefdata
*f
) {
337 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
340 static void set_edited_namepart(struct prefdata
*f
, const char *value
) {
341 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
344 static void set_namepart(struct prefdata
*f
, const char *value
) {
347 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
348 /* We don't know what the default is so can never unset. This is a bug
349 * relative to the original design, which is supposed to only ever allow for
350 * non-trivial namepart preferences. I suppose the server could spot a
351 * default being set and translate it into an unset. */
352 disorder_eclient_set(client
, set_namepart_completed
, f
->track
, s
, value
,
356 /* Called when we've set a namepart */
357 static void set_namepart_completed(void *v
, const char *err
) {
359 popup_protocol_error(0, err
);
361 struct prefdata
*f
= v
;
363 namepart_update(f
->track
, "display", f
->p
->part
);
367 /* String preferences ------------------------------------------------------ */
369 static void kickoff_string(struct prefdata
*f
) {
370 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
, f
);
373 static void completed_string(struct prefdata
*f
) {
375 /* No setting, use the default value instead */
376 f
->value
= f
->p
->default_value
;
377 f
->widget
= gtk_entry_new();
380 static const char *get_edited_string(struct prefdata
*f
) {
381 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
384 static void set_edited_string(struct prefdata
*f
, const char *value
) {
385 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
388 static void set_string_completed(void attribute((unused
)) *v
,
391 popup_protocol_error(0, err
);
394 static void set_string(struct prefdata
*f
, const char *value
) {
395 disorder_eclient_set(client
, set_string_completed
, f
->track
, f
->p
->part
,
399 /* Boolean preferences ----------------------------------------------------- */
401 static void kickoff_boolean(struct prefdata
*f
) {
402 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
, f
);
405 static void completed_boolean(struct prefdata
*f
) {
406 f
->widget
= gtk_check_button_new();
407 gtk_widget_set_style(f
->widget
, tool_style
);
409 /* Not set, use the default */
410 f
->value
= f
->p
->default_value
;
413 static const char *get_edited_boolean(struct prefdata
*f
) {
414 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f
->widget
))
418 static void set_edited_boolean(struct prefdata
*f
, const char *value
) {
419 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f
->widget
),
423 #define set_boolean_completed set_string_completed
425 static void set_boolean(struct prefdata
*f
, const char *value
) {
428 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
429 disorder_eclient_set(client
, set_boolean_completed
,
430 f
->track
, f
->p
->part
, value
, 0/*v*/);
433 /* Querying preferences ---------------------------------------------------- */
435 static void prefdata_completed(void *v
, const char *err
, const char *value
) {
436 struct prefdata
*const f
= v
;
439 popup_protocol_error(0, err
);
441 f
->p
->type
->completed(f
);
442 f
->p
->type
->set_edited(f
, f
->value
);
443 assert(f
->value
!= 0); /* Had better set a default */
444 gtk_table_attach(GTK_TABLE(properties_table
), f
->widget
,
447 GTK_EXPAND
|GTK_FILL
/*xoptions*/, 0/*yoptions*/,
451 progress_window_progress(pw
, prefs_total
- prefs_unfilled
, prefs_total
);
456 /* Button callbacks -------------------------------------------------------- */
458 static void properties_ok(GtkButton
*button
,
460 properties_apply(button
, userdata
);
461 properties_cancel(button
, userdata
);
464 static void properties_apply(GtkButton
attribute((unused
)) *button
,
465 gpointer
attribute((unused
)) userdata
) {
470 /* For each possible property we see if we've changed it and if so tell the
472 for(n
= 0; n
< prefs_total
; ++n
) {
474 edited
= f
->p
->type
->get_edited(f
);
475 if(strcmp(edited
, f
->value
)) {
476 /* The value has changed */
477 f
->p
->type
->set(f
, edited
);
478 f
->value
= xstrdup(edited
);
483 static void properties_cancel(GtkButton
attribute((unused
)) *button
,
484 gpointer
attribute((unused
)) userdata
) {
485 gtk_widget_destroy(properties_window
);
486 event_cancel(properties_event
);
487 properties_event
= 0;
490 /** @brief Called when we've just logged in
492 * Destroys the current properties window.
494 static void properties_logged_in(const char attribute((unused
)) *event
,
495 void attribute((unused
)) *eventdata
,
496 void attribute((unused
)) *callbackdata
) {
497 if(properties_window
)
498 gtk_widget_destroy(properties_window
);