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
23 #include "disobedience.h"
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 *err
);
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 *err
, const char *value
);
48 static void properties_ok(GtkButton
*button
, gpointer userdata
);
49 static void properties_apply(GtkButton
*button
, gpointer userdata
);
50 static void properties_cancel(GtkButton
*button
, gpointer userdata
);
52 static void properties_logged_in(const char *event
,
56 /** @brief Data for a single preference */
60 const struct pref
*p
; /**< @brief kind of preference */
61 const char *value
; /**< @brief value from server */
65 /* The type of a preference is the collection of callbacks needed to get,
66 * display and set it */
68 void (*kickoff
)(struct prefdata
*f
);
69 /* Kick off the request to fetch the pref from the server. */
71 void (*completed
)(struct prefdata
*f
);
72 /* Called when the value comes back in; creates the widget. */
74 const char *(*get_edited
)(struct prefdata
*f
);
75 /* Get the edited value from the widget. */
77 /** @brief Update the edited value */
78 void (*set_edited
)(struct prefdata
*f
, const char *value
);
80 void (*set
)(struct prefdata
*f
, const char *value
);
81 /* Set the new value and (if necessary) arrange for our display to update. */
85 static const struct preftype preftype_namepart
= {
94 static const struct preftype preftype_string
= {
103 static const struct preftype preftype_boolean
= {
111 /* @brief The known prefs for each track */
112 static const struct pref
{
113 const char *label
; /**< @brief user-level description */
114 const char *part
; /**< @brief protocol-level tag */
115 const char *default_value
; /**< @brief default value or NULL */
116 const struct preftype
*type
; /**< @brief underlying data type */
118 { "Artist", "artist", 0, &preftype_namepart
},
119 { "Album", "album", 0, &preftype_namepart
},
120 { "Title", "title", 0, &preftype_namepart
},
121 { "Tags", "tags", "", &preftype_string
},
122 { "Weight", "weight", "90000", &preftype_string
},
123 { "Random", "pick_at_random", "1", &preftype_boolean
},
126 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
128 /* Buttons that appear at the bottom of the window */
129 static struct button buttons
[] = {
133 "Apply all changes and close window",
139 "Apply all changes and keep window open",
145 "Discard all changes and close window",
150 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
152 static int prefs_unfilled
; /* Prefs remaining to get */
153 static int prefs_total
; /* Total prefs */
154 static struct prefdata
*prefdatas
; /* Current prefdatas */
155 static GtkWidget
*properties_window
;
156 static GtkWidget
*properties_table
;
157 static struct progress_window
*pw
;
158 static event_handle properties_event
;
160 static void propagate_clicked(GtkButton
attribute((unused
)) *button
,
162 struct prefdata
*f
= (struct prefdata
*)userdata
, *g
;
164 const char *value
= f
->p
->type
->get_edited(f
);
166 for(p
= 0; p
< prefs_total
; ++p
) {
168 if(f
->p
== g
->p
&& f
!= g
)
169 g
->p
->type
->set_edited(g
, value
);
173 /** @brief Keypress handler */
174 static gboolean
properties_keypress(GtkWidget
attribute((unused
)) *widget
,
176 gpointer
attribute((unused
)) user_data
) {
179 switch(event
->keyval
) {
184 properties_cancel(0, 0);
191 void properties(int ntracks
, const char **tracks
) {
194 GtkWidget
*buttonbox
, *vbox
, *label
, *entry
, *propagate
;
196 /* If no tracks, do nothign */
199 /* If there is a properties window open then just bring it to the
200 * front. It might not have the right values in... */
201 if(properties_window
) {
203 gtk_window_present(GTK_WINDOW(properties_window
));
206 assert(properties_table
== 0);
207 if(ntracks
> INT_MAX
/ NPREFS
) {
208 popup_msg(GTK_MESSAGE_ERROR
, "Too many tracks selected");
211 properties_event
= event_register("logged-in", properties_logged_in
, 0);
212 /* Create a new properties window */
213 properties_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
214 gtk_widget_set_style(properties_window
, tool_style
);
215 g_signal_connect(properties_window
, "destroy",
216 G_CALLBACK(gtk_widget_destroyed
), &properties_window
);
217 /* Keyboard shortcuts */
218 g_signal_connect(properties_window
, "key-press-event",
219 G_CALLBACK(properties_keypress
), 0);
220 /* Most of the action is the table of preferences */
221 properties_table
= gtk_table_new((NPREFS
+ 1) * ntracks
, 2 + ntracks
> 1,
223 gtk_widget_set_style(properties_table
, tool_style
);
224 g_signal_connect(properties_table
, "destroy",
225 G_CALLBACK(gtk_widget_destroyed
), &properties_table
);
226 gtk_window_set_title(GTK_WINDOW(properties_window
), "Track Properties");
227 /* Create labels for each pref of each track and kick off requests to the
228 * server to fill in the values */
229 prefs_total
= NPREFS
* ntracks
;
230 prefdatas
= xcalloc(prefs_total
, sizeof *prefdatas
);
231 for(n
= 0; n
< ntracks
; ++n
) {
232 /* The track itself */
234 label
= gtk_label_new("Track");
235 gtk_widget_set_style(label
, tool_style
);
236 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
237 gtk_table_attach(GTK_TABLE(properties_table
),
240 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
244 entry
= gtk_entry_new();
245 gtk_widget_set_style(entry
, tool_style
);
246 gtk_entry_set_text(GTK_ENTRY(entry
), tracks
[n
]);
247 gtk_editable_set_editable(GTK_EDITABLE(entry
), FALSE
);
248 gtk_table_attach(GTK_TABLE(properties_table
),
251 (NPREFS
+ 1) * n
, (NPREFS
+ 1) * n
+ 1,
252 GTK_EXPAND
|GTK_FILL
, 0,
254 /* Each preference */
255 for(m
= 0; m
< NPREFS
; ++m
) {
257 label
= gtk_label_new(prefs
[m
].label
);
258 gtk_widget_set_style(label
, tool_style
);
259 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
260 gtk_table_attach(GTK_TABLE(properties_table
),
263 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
264 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
266 /* Editing the preference is specific */
267 f
= &prefdatas
[NPREFS
* n
+ m
];
268 f
->track
= tracks
[n
];
269 f
->row
= (NPREFS
+ 1) * n
+ 1 + m
;
271 prefs
[m
].type
->kickoff(f
);
273 /* Propagation button */
274 propagate
= iconbutton("propagate.png", "Copy to other tracks");
275 g_signal_connect(G_OBJECT(propagate
), "clicked",
276 G_CALLBACK(propagate_clicked
), f
);
277 gtk_table_attach(GTK_TABLE(properties_table
),
279 2/*left*/, 3/*right*/,
280 (NPREFS
+ 1) * n
+ 1 + m
, (NPREFS
+ 1) * n
+ 2 + m
,
281 GTK_FILL
/*xoptions*/, 0/*yoptions*/,
282 1/*xpadding*/, 1/*ypadding*/);
286 prefs_unfilled
= prefs_total
;
288 buttonbox
= create_buttons(buttons
, NBUTTONS
);
289 /* Put it all together */
290 vbox
= gtk_vbox_new(FALSE
, 1);
291 gtk_box_pack_start(GTK_BOX(vbox
),
292 scroll_widget(properties_table
),
294 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
, FALSE
, FALSE
, 1);
295 gtk_container_add(GTK_CONTAINER(properties_window
), frame_widget(vbox
, NULL
));
296 /* The table only really wants to be vertically scrollable */
297 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table
)->parent
->parent
),
299 GTK_POLICY_AUTOMATIC
);
300 /* Zot any pre-existing progress window just in case */
302 progress_window_progress(pw
, 0, 0);
303 /* Pop up a progress bar while we're waiting */
304 pw
= progress_window_new("Fetching Track Properties");
307 /* Everything is filled in now */
308 static void prefdata_alldone(void) {
310 progress_window_progress(pw
, 0, 0);
313 /* Default size may be too small */
314 gtk_window_set_default_size(GTK_WINDOW(properties_window
), 480, 512);
315 /* TODO: relate default size to required size more closely */
316 gtk_widget_show_all(properties_window
);
319 /* Namepart preferences ---------------------------------------------------- */
321 static void kickoff_namepart(struct prefdata
*f
) {
322 /* We ask for the display name part. This is a bit bizarre if what we really
323 * wanted was the underlying preference, but in fact it should always match
324 * and will supply a sane default without having to know how to parse tracks
325 * names (which implies knowing collection roots). */
326 disorder_eclient_namepart(client
, prefdata_completed
,
327 f
->track
, "display", f
->p
->part
, f
);
330 static void completed_namepart(struct prefdata
*f
) {
335 f
->widget
= gtk_entry_new();
338 static const char *get_edited_namepart(struct prefdata
*f
) {
339 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
342 static void set_edited_namepart(struct prefdata
*f
, const char *value
) {
343 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
346 static void set_namepart(struct prefdata
*f
, const char *value
) {
349 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
350 /* We don't know what the default is so can never unset. This is a bug
351 * relative to the original design, which is supposed to only ever allow for
352 * non-trivial namepart preferences. I suppose the server could spot a
353 * default being set and translate it into an unset. */
354 disorder_eclient_set(client
, set_namepart_completed
, f
->track
, s
, value
,
358 /* Called when we've set a namepart */
359 static void set_namepart_completed(void *v
, const char *err
) {
361 popup_protocol_error(0, err
);
363 struct prefdata
*f
= v
;
365 namepart_update(f
->track
, "display", f
->p
->part
);
369 /* String preferences ------------------------------------------------------ */
371 static void kickoff_string(struct prefdata
*f
) {
372 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
, f
);
375 static void completed_string(struct prefdata
*f
) {
377 /* No setting, use the default value instead */
378 f
->value
= f
->p
->default_value
;
379 f
->widget
= gtk_entry_new();
382 static const char *get_edited_string(struct prefdata
*f
) {
383 return gtk_entry_get_text(GTK_ENTRY(f
->widget
));
386 static void set_edited_string(struct prefdata
*f
, const char *value
) {
387 gtk_entry_set_text(GTK_ENTRY(f
->widget
), value
);
390 static void set_string_completed(void attribute((unused
)) *v
,
393 popup_protocol_error(0, err
);
396 static void set_string(struct prefdata
*f
, const char *value
) {
397 disorder_eclient_set(client
, set_string_completed
, f
->track
, f
->p
->part
,
401 /* Boolean preferences ----------------------------------------------------- */
403 static void kickoff_boolean(struct prefdata
*f
) {
404 disorder_eclient_get(client
, prefdata_completed
, f
->track
, f
->p
->part
, f
);
407 static void completed_boolean(struct prefdata
*f
) {
408 f
->widget
= gtk_check_button_new();
409 gtk_widget_set_style(f
->widget
, tool_style
);
411 /* Not set, use the default */
412 f
->value
= f
->p
->default_value
;
415 static const char *get_edited_boolean(struct prefdata
*f
) {
416 return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f
->widget
))
420 static void set_edited_boolean(struct prefdata
*f
, const char *value
) {
421 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f
->widget
),
425 #define set_boolean_completed set_string_completed
427 static void set_boolean(struct prefdata
*f
, const char *value
) {
430 byte_xasprintf(&s
, "trackname_display_%s", f
->p
->part
);
431 disorder_eclient_set(client
, set_boolean_completed
,
432 f
->track
, f
->p
->part
, value
, 0/*v*/);
435 /* Querying preferences ---------------------------------------------------- */
437 static void prefdata_completed(void *v
, const char *err
, const char *value
) {
438 struct prefdata
*const f
= v
;
441 popup_protocol_error(0, err
);
443 f
->p
->type
->completed(f
);
444 f
->p
->type
->set_edited(f
, f
->value
);
445 assert(f
->value
!= 0); /* Had better set a default */
446 gtk_table_attach(GTK_TABLE(properties_table
), f
->widget
,
449 GTK_EXPAND
|GTK_FILL
/*xoptions*/, 0/*yoptions*/,
453 progress_window_progress(pw
, prefs_total
- prefs_unfilled
, prefs_total
);
458 /* Button callbacks -------------------------------------------------------- */
460 static void properties_ok(GtkButton
*button
,
462 properties_apply(button
, userdata
);
463 properties_cancel(button
, userdata
);
466 static void properties_apply(GtkButton
attribute((unused
)) *button
,
467 gpointer
attribute((unused
)) userdata
) {
472 /* For each possible property we see if we've changed it and if so tell the
474 for(n
= 0; n
< prefs_total
; ++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
);
485 static void properties_cancel(GtkButton
attribute((unused
)) *button
,
486 gpointer
attribute((unused
)) userdata
) {
487 gtk_widget_destroy(properties_window
);
488 event_cancel(properties_event
);
489 properties_event
= 0;
492 /** @brief Called when we've just logged in
494 * Destroys the current properties window.
496 static void properties_logged_in(const char attribute((unused
)) *event
,
497 void attribute((unused
)) *eventdata
,
498 void attribute((unused
)) *callbackdata
) {
499 if(properties_window
)
500 gtk_widget_destroy(properties_window
);