chiark / gitweb /
more TODOs
[disorder] / disobedience / properties.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006-2008 Richard Kettlewell
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20 /** @file disobedience/properties.c
21  * @brief Track properties editor
22  *
23  * TODO:
24  * - return and escape keys should work 
25  */
26 #include "disobedience.h"
27
28 struct prefdata;
29
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 *error);
36
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);
42
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);
48
49 static void prefdata_completed(void *v, const char *error, const char *value);
50 static void prefdata_onerror(struct callbackdata *cbd,
51                              int code,
52                              const char *msg);
53 static struct callbackdata *make_callbackdata(struct prefdata *f);
54 static void prefdata_completed_common(struct prefdata *f,
55                                       const char *value);
56
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);
60
61 static void properties_logged_in(const char *event,
62                                  void *eventdata,
63                                  void *callbackdata);
64
65 /** @brief Data for a single preference */
66 struct prefdata {
67   const char *track;
68   int row;
69   const struct pref *p;                 /**< @brief kind of preference */
70   const char *value;                    /**< @brief value from server  */
71   GtkWidget *widget;
72 };
73
74 /* The type of a preference is the collection of callbacks needed to get,
75  * display and set it */
76 struct preftype {
77   void (*kickoff)(struct prefdata *f);
78   /* Kick off the request to fetch the pref from the server. */
79
80   void (*completed)(struct prefdata *f);
81   /* Called when the value comes back in; creates the widget. */
82
83   const char *(*get_edited)(struct prefdata *f);
84   /* Get the edited value from the widget. */
85
86   /** @brief Update the edited value */
87   void (*set_edited)(struct prefdata *f, const char *value);
88
89   void (*set)(struct prefdata *f, const char *value);
90   /* Set the new value and (if necessary) arrange for our display to update. */
91 };
92
93 /* A namepart pref */
94 static const struct preftype preftype_namepart = {
95   kickoff_namepart,
96   completed_namepart,
97   get_edited_namepart,
98   set_edited_namepart,
99   set_namepart
100 };
101
102 /* A string pref */
103 static const struct preftype preftype_string = {
104   kickoff_string,
105   completed_string,
106   get_edited_string,
107   set_edited_string,
108   set_string
109 };
110
111 /* A boolean pref */
112 static const struct preftype preftype_boolean = {
113   kickoff_boolean,
114   completed_boolean,
115   get_edited_boolean,
116   set_edited_boolean,
117   set_boolean
118 };
119
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 */
126 } prefs[] = {
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 },
133 };
134
135 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
136
137 /* Buttons that appear at the bottom of the window */
138 static struct button buttons[] = {
139   {
140     GTK_STOCK_OK,
141     properties_ok,
142     "Apply all changes and close window",
143     0
144   },
145   {
146     GTK_STOCK_APPLY,
147     properties_apply,
148     "Apply all changes and keep window open",
149     0
150   },
151   {
152     GTK_STOCK_CANCEL,
153     properties_cancel,
154     "Discard all changes and close window",
155     0
156   },
157 };
158
159 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
160
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;
168
169 static void propagate_clicked(GtkButton attribute((unused)) *button,
170                               gpointer userdata) {
171   struct prefdata *f = (struct prefdata *)userdata, *g;
172   int p;
173   const char *value = f->p->type->get_edited(f);
174   
175   for(p = 0; p < prefs_total; ++p) {
176     g = &prefdatas[p];
177     if(f->p == g->p && f != g)
178       g->p->type->set_edited(g, value);
179   }
180 }
181
182 void properties(int ntracks, const char **tracks) {
183   int n, m;
184   struct prefdata *f;
185   GtkWidget *buttonbox, *vbox, *label, *entry, *propagate;
186   
187   /* If no tracks, do nothign */
188   if(!ntracks)
189     return;
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) {
193     if(!prefs_unfilled)
194       gtk_window_present(GTK_WINDOW(properties_window));
195     return;
196   }
197   assert(properties_table == 0);
198   if(ntracks > INT_MAX / NPREFS) {
199     popup_msg(GTK_MESSAGE_ERROR, "Too many tracks selected");
200     return;
201   }
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,
210                                    FALSE);
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 */
221     /* Caption */
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),
226                      label,
227                      0, 1,
228                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
229                      GTK_FILL, 0,
230                      1, 1);
231     /* The track name */
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),
237                      entry,
238                      1, 2,
239                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
240                      GTK_EXPAND|GTK_FILL, 0,
241                      1, 1);
242     /* Each preference */
243     for(m = 0; m < NPREFS; ++m) {
244       /* Caption */
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),
249                        label,
250                        0, 1,
251                        (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
252                        GTK_FILL/*xoptions*/, 0/*yoptions*/,
253                        1, 1);
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;
258       f->p = &prefs[m];
259       prefs[m].type->kickoff(f);
260       if(ntracks > 1) {
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),
266                          propagate,
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*/);
271       }
272     }
273   }
274   prefs_unfilled = prefs_total;
275   /* Buttons */
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),
281                      TRUE, TRUE, 1);
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),
286                                  GTK_POLICY_NEVER,
287                                  GTK_POLICY_AUTOMATIC);
288   /* Zot any pre-existing progress window just in case */
289   if(pw)
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");
293 }
294
295 /* Everything is filled in now */
296 static void prefdata_alldone(void) {
297   if(pw) {
298     progress_window_progress(pw, 0, 0);
299     pw = 0;
300   }
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);
305 }
306
307 /* Namepart preferences ---------------------------------------------------- */
308
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));
316 }
317
318 static void completed_namepart(struct prefdata *f) {
319   if(!f->value) {
320     /* No setting */
321     f->value = "";
322   }
323   f->widget = gtk_entry_new();
324 }
325
326 static const char *get_edited_namepart(struct prefdata *f) {
327   return gtk_entry_get_text(GTK_ENTRY(f->widget));
328 }
329
330 static void set_edited_namepart(struct prefdata *f, const char *value) {
331   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
332 }
333
334 static void set_namepart(struct prefdata *f, const char *value) {
335   char *s;
336
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,
343                        f);
344 }
345
346 /* Called when we've set a namepart */
347 static void set_namepart_completed(void *v, const char *error) {
348   if(error)
349     popup_protocol_error(0, error);
350   else {
351     struct prefdata *f = v;
352     
353     namepart_update(f->track, "display", f->p->part);
354   }
355 }
356
357 /* String preferences ------------------------------------------------------ */
358
359 static void kickoff_string(struct prefdata *f) {
360   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
361                        make_callbackdata(f));
362 }
363
364 static void completed_string(struct prefdata *f) {
365   if(!f->value)
366     /* No setting, use the default value instead */
367     f->value = f->p->default_value;
368   f->widget = gtk_entry_new();
369 }
370
371 static const char *get_edited_string(struct prefdata *f) {
372   return gtk_entry_get_text(GTK_ENTRY(f->widget));
373 }
374
375 static void set_edited_string(struct prefdata *f, const char *value) {
376   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
377 }
378
379 static void set_string_completed(void attribute((unused)) *v,
380                                  const char *error) {
381   if(error)
382     popup_protocol_error(0, error);
383 }
384
385 static void set_string(struct prefdata *f, const char *value) {
386   disorder_eclient_set(client, set_string_completed, f->track, f->p->part,
387                        value, 0/*v*/);
388 }
389
390 /* Boolean preferences ----------------------------------------------------- */
391
392 static void kickoff_boolean(struct prefdata *f) {
393   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
394                        make_callbackdata(f));
395 }
396
397 static void completed_boolean(struct prefdata *f) {
398   f->widget = gtk_check_button_new();
399   gtk_widget_set_style(f->widget, tool_style);
400   if(!f->value)
401     /* Not set, use the default */
402     f->value = f->p->default_value;
403 }
404
405 static const char *get_edited_boolean(struct prefdata *f) {
406   return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
407           ? "1" : "0");
408 }
409
410 static void set_edited_boolean(struct prefdata *f, const char *value) {
411   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
412                                strcmp(value, "0"));
413 }
414
415 #define set_boolean_completed set_string_completed
416
417 static void set_boolean(struct prefdata *f, const char *value) {
418   char *s;
419
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*/);
423 }
424
425 /* Querying preferences ---------------------------------------------------- */
426
427 /* Make a suitable callbackdata */
428 static struct callbackdata *make_callbackdata(struct prefdata *f) {
429   struct callbackdata *cbd = xmalloc(sizeof *cbd);
430
431   cbd->onerror = prefdata_onerror;
432   cbd->u.f = f;
433   return cbd;
434 }
435
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);
441 }
442
443 /* Got the value of a pref */
444 static void prefdata_completed(void *v, const char *error, const char *value) {
445   if(error) {
446   } else {
447     struct callbackdata *cbd = v;
448     
449     prefdata_completed_common(cbd->u.f, value);
450   }
451 }
452
453 static void prefdata_completed_common(struct prefdata *f,
454                                       const char *value) {
455   f->value = value;
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,
460                    1, 2,
461                    f->row, f->row + 1,
462                    GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
463                    1, 1);
464   --prefs_unfilled;
465   if(prefs_total)
466     progress_window_progress(pw, prefs_total - prefs_unfilled, prefs_total);
467   if(!prefs_unfilled)
468     prefdata_alldone();
469 }
470
471 /* Button callbacks -------------------------------------------------------- */
472
473 static void properties_ok(GtkButton *button, 
474                           gpointer userdata) {
475   properties_apply(button, userdata);
476   properties_cancel(button, userdata);
477 }
478
479 static void properties_apply(GtkButton attribute((unused)) *button, 
480                              gpointer attribute((unused)) userdata) {
481   int n;
482   const char *edited;
483   struct prefdata *f;
484
485   /* For each possible property we see if we've changed it and if so tell the
486    * server */
487   for(n = 0; n < prefs_total; ++n) {
488     f = &prefdatas[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);
494     }
495   }
496 }
497
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;
503 }
504
505 /** @brief Called when we've just logged in
506  *
507  * Destroys the current properties window.
508  */
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);
514 }
515
516 /*
517 Local Variables:
518 c-basic-offset:2
519 comment-column:40
520 fill-column:79
521 indent-tabs-mode:nil
522 End:
523 */