chiark / gitweb /
build fix
[disorder] / disobedience / properties.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006, 2007 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
21 #include "disobedience.h"
22
23 /* Track properties -------------------------------------------------------- */
24
25 struct prefdata;
26
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);
33
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);
39
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);
45
46 static void prefdata_completed(void *v, const char *value);
47 static void prefdata_onerror(struct callbackdata *cbd,
48                              int code,
49                              const char *msg);
50 static struct callbackdata *make_callbackdata(struct prefdata *f);
51 static void prefdata_completed_common(struct prefdata *f,
52                                       const char *value);
53
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);
57
58 /** @brief Data for a single preference */
59 struct prefdata {
60   const char *track;
61   int row;
62   const struct pref *p;                 /**< @brief kind of preference */
63   const char *value;                    /**< @brief value from server  */
64   GtkWidget *widget;
65 };
66
67 /* The type of a preference is the collection of callbacks needed to get,
68  * display and set it */
69 struct preftype {
70   void (*kickoff)(struct prefdata *f);
71   /* Kick off the request to fetch the pref from the server. */
72
73   void (*completed)(struct prefdata *f);
74   /* Called when the value comes back in; creates the widget. */
75
76   const char *(*get_edited)(struct prefdata *f);
77   /* Get the edited value from the widget. */
78
79   /** @brief Update the edited value */
80   void (*set_edited)(struct prefdata *f, const char *value);
81
82   void (*set)(struct prefdata *f, const char *value);
83   /* Set the new value and (if necessary) arrange for our display to update. */
84 };
85
86 /* A namepart pref */
87 static const struct preftype preftype_namepart = {
88   kickoff_namepart,
89   completed_namepart,
90   get_edited_namepart,
91   set_edited_namepart,
92   set_namepart
93 };
94
95 /* A string pref */
96 static const struct preftype preftype_string = {
97   kickoff_string,
98   completed_string,
99   get_edited_string,
100   set_edited_string,
101   set_string
102 };
103
104 /* A boolean pref */
105 static const struct preftype preftype_boolean = {
106   kickoff_boolean,
107   completed_boolean,
108   get_edited_boolean,
109   set_edited_boolean,
110   set_boolean
111 };
112
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 */
119 } prefs[] = {
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 },
125 };
126
127 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
128
129 /* Buttons that appear at the bottom of the window */
130 static const struct button buttons[] = {
131   {
132     GTK_STOCK_OK,
133     properties_ok,
134     "Apply all changes and close window"
135   },
136   {
137     GTK_STOCK_APPLY,
138     properties_apply,
139     "Apply all changes and keep window open"
140   },
141   {
142     GTK_STOCK_CANCEL,
143     properties_cancel,
144     "Discard all changes and close window"
145   },
146 };
147
148 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
149
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
157 static void propagate_clicked(GtkButton attribute((unused)) *button,
158                               gpointer userdata) {
159   struct prefdata *f = (struct prefdata *)userdata, *g;
160   int p;
161   const char *value = f->p->type->get_edited(f);
162   
163   for(p = 0; p < prefs_total; ++p) {
164     g = &prefdatas[p];
165     if(f->p == g->p && f != g)
166       g->p->type->set_edited(g, value);
167   }
168 }
169
170 void properties(int ntracks, const char **tracks) {
171   int n, m;
172   struct prefdata *f;
173   GtkWidget *buttonbox, *vbox, *label, *entry, *propagate, *content;
174   GdkPixbuf *pb;
175   
176   /* If no tracks, do nothign */
177   if(!ntracks)
178     return;
179   /* If there is a properties window open then just bring it to the
180    * front.  It might not have the right values in... */
181   if(properties_window) {
182     if(!prefs_unfilled)
183       gtk_window_present(GTK_WINDOW(properties_window));
184     return;
185   }
186   assert(properties_table == 0);
187   if(ntracks > INT_MAX / NPREFS) {
188     popup_msg(GTK_MESSAGE_ERROR, "Too many tracks selected");
189     return;
190   }
191   /* Create a new properties window */
192   properties_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
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,
197                                    FALSE);
198   g_signal_connect(properties_table, "destroy",
199                    G_CALLBACK(gtk_widget_destroyed), &properties_table);
200   gtk_window_set_title(GTK_WINDOW(properties_window), "Track Properties");
201   /* Create labels for each pref of each track and kick off requests to the
202    * server to fill in the values */
203   prefs_total = NPREFS * ntracks;
204   prefdatas = xcalloc(prefs_total, sizeof *prefdatas);
205   for(n = 0; n < ntracks; ++n) {
206     /* The track itself */
207     /* Caption */
208     label = gtk_label_new("Track");
209     gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
210     gtk_table_attach(GTK_TABLE(properties_table),
211                      label,
212                      0, 1,
213                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
214                      GTK_FILL, 0,
215                      1, 1);
216     /* The track name */
217     entry = gtk_entry_new();
218     gtk_entry_set_text(GTK_ENTRY(entry), tracks[n]);
219     gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
220     gtk_table_attach(GTK_TABLE(properties_table),
221                      entry,
222                      1, 2,
223                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
224                      GTK_EXPAND|GTK_FILL, 0,
225                      1, 1);
226     /* Each preference */
227     for(m = 0; m < NPREFS; ++m) {
228       /* Caption */
229       label = gtk_label_new(prefs[m].label);
230       gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
231       gtk_table_attach(GTK_TABLE(properties_table),
232                        label,
233                        0, 1,
234                        (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
235                        GTK_FILL/*xoptions*/, 0/*yoptions*/,
236                        1, 1);
237       /* Editing the preference is specific */
238       f = &prefdatas[NPREFS * n + m];
239       f->track = tracks[n];
240       f->row = (NPREFS + 1) * n + 1 + m;
241       f->p = &prefs[m];
242       prefs[m].type->kickoff(f);
243       if(ntracks > 1) {
244         /* Propagation button */
245         propagate = gtk_button_new();
246         if((pb = find_image("propagate.png")))
247           content = gtk_image_new_from_pixbuf(pb);
248         else
249           content = gtk_label_new("propagate.png");
250         gtk_container_add(GTK_CONTAINER(propagate), content);
251         gtk_tooltips_set_tip(tips, propagate, "Copy to other tracks", "");
252         g_signal_connect(G_OBJECT(propagate), "clicked",
253                          G_CALLBACK(propagate_clicked), f);
254         gtk_table_attach(GTK_TABLE(properties_table),
255                          propagate,
256                          2/*left*/, 3/*right*/,
257                          (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
258                          GTK_FILL/*xoptions*/, 0/*yoptions*/,
259                          1/*xpadding*/, 1/*ypadding*/);
260       }
261     }
262   }
263   prefs_unfilled = prefs_total;
264   /* Buttons */
265   buttonbox = create_buttons(buttons, NBUTTONS);
266   /* Put it all together */
267   vbox = gtk_vbox_new(FALSE, 1);
268   gtk_box_pack_start(GTK_BOX(vbox), 
269                      scroll_widget(properties_table,
270                                    "properties"),
271                      TRUE, TRUE, 1);
272   gtk_box_pack_start(GTK_BOX(vbox), buttonbox, FALSE, FALSE, 1);
273   gtk_container_add(GTK_CONTAINER(properties_window), vbox);
274   /* The table only really wants to be vertically scrollable */
275   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table)->parent->parent),
276                                  GTK_POLICY_NEVER,
277                                  GTK_POLICY_AUTOMATIC);
278   /* Zot any pre-existing progress window just in case */
279   if(pw)
280     progress_window_progress(pw, 0, 0);
281   /* Pop up a progress bar while we're waiting */
282   pw = progress_window_new("Fetching Track Properties");
283 }
284
285 /* Everything is filled in now */
286 static void prefdata_alldone(void) {
287   if(pw) {
288     progress_window_progress(pw, 0, 0);
289     pw = 0;
290   }
291   /* Default size may be too small */
292   gtk_window_set_default_size(GTK_WINDOW(properties_window), 480, 512);
293   /* TODO: relate default size to required size more closely */
294   gtk_widget_show_all(properties_window);
295 }
296
297 /* Namepart preferences ---------------------------------------------------- */
298
299 static void kickoff_namepart(struct prefdata *f) {
300   /* We ask for the display name part.  This is a bit bizarre if what we really
301    * wanted was the underlying preference, but in fact it should always match
302    * and will supply a sane default without having to know how to parse tracks
303    * names (which implies knowing collection roots). */
304   disorder_eclient_namepart(client, prefdata_completed, f->track, "display", f->p->part,
305                             make_callbackdata(f));
306 }
307
308 static void completed_namepart(struct prefdata *f) {
309   if(!f->value) {
310     /* No setting */
311     f->value = "";
312   }
313   f->widget = gtk_entry_new();
314 }
315
316 static const char *get_edited_namepart(struct prefdata *f) {
317   return gtk_entry_get_text(GTK_ENTRY(f->widget));
318 }
319
320 static void set_edited_namepart(struct prefdata *f, const char *value) {
321   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
322 }
323
324 static void set_namepart(struct prefdata *f, const char *value) {
325   char *s;
326   struct callbackdata *cbd = xmalloc(sizeof *cbd);
327
328   cbd->u.f = f;
329   byte_xasprintf(&s, "trackname_display_%s", f->p->part);
330   /* We don't know what the default is so can never unset.  This is a bug
331    * relative to the original design, which is supposed to only ever allow for
332    * non-trivial namepart preferences.  I suppose the server could spot a
333    * default being set and translate it into an unset. */
334   disorder_eclient_set(client, set_namepart_completed, f->track, s, value,
335                        cbd);
336 }
337
338 /* Called when we've set a namepart */
339 static void set_namepart_completed(void *v) {
340   struct callbackdata *cbd = v;
341   struct prefdata *f = cbd->u.f;
342
343   namepart_update(f->track, "display", f->p->part);
344 }
345
346 /* String preferences ------------------------------------------------------ */
347
348 static void kickoff_string(struct prefdata *f) {
349   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
350                        make_callbackdata(f));
351 }
352
353 static void completed_string(struct prefdata *f) {
354   if(!f->value)
355     /* No setting, use the default value instead */
356     f->value = f->p->default_value;
357   f->widget = gtk_entry_new();
358 }
359
360 static const char *get_edited_string(struct prefdata *f) {
361   return gtk_entry_get_text(GTK_ENTRY(f->widget));
362 }
363
364 static void set_edited_string(struct prefdata *f, const char *value) {
365   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
366 }
367
368 static void set_string(struct prefdata *f, const char *value) {
369   if(strcmp(f->p->default_value, value))
370     /* Different from default, set it */
371     disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part,
372                          value, 0/*v*/);
373   else
374     /* Same as default, just unset */
375     disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part,
376                            0/*v*/);
377 }
378
379 /* Boolean preferences ----------------------------------------------------- */
380
381 static void kickoff_boolean(struct prefdata *f) {
382   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
383                        make_callbackdata(f));
384 }
385
386 static void completed_boolean(struct prefdata *f) {
387   f->widget = gtk_check_button_new();
388   if(!f->value)
389     /* Not set, use the default */
390     f->value = f->p->default_value;
391 }
392
393 static const char *get_edited_boolean(struct prefdata *f) {
394   return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
395           ? "1" : "0");
396 }
397
398 static void set_edited_boolean(struct prefdata *f, const char *value) {
399   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
400                                strcmp(value, "0"));
401 }
402
403 static void set_boolean(struct prefdata *f, const char *value) {
404   char *s;
405
406   byte_xasprintf(&s, "trackname_display_%s", f->p->part);
407   if(strcmp(value, f->p->default_value))
408     disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part, value,
409                          0/*v*/);
410   else
411     /* If default value then delete the pref */
412     disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part,
413                            0/*v*/);
414 }
415
416 /* Querying preferences ---------------------------------------------------- */
417
418 /* Make a suitable callbackdata */
419 static struct callbackdata *make_callbackdata(struct prefdata *f) {
420   struct callbackdata *cbd = xmalloc(sizeof *cbd);
421
422   cbd->onerror = prefdata_onerror;
423   cbd->u.f = f;
424   return cbd;
425 }
426
427 /* No pref was set */
428 static void prefdata_onerror(struct callbackdata *cbd,
429                              int attribute((unused)) code,
430                              const char attribute((unused)) *msg) {
431   prefdata_completed_common(cbd->u.f, 0);
432 }
433
434 /* Got the value of a pref */
435 static void prefdata_completed(void *v, const char *value) {
436   struct callbackdata *cbd = v;
437
438   prefdata_completed_common(cbd->u.f, value);
439 }
440
441 static void prefdata_completed_common(struct prefdata *f,
442                                       const char *value) {
443   f->value = value;
444   f->p->type->completed(f);
445   f->p->type->set_edited(f, f->value);
446   assert(f->value != 0);                /* Had better set a default */
447   gtk_table_attach(GTK_TABLE(properties_table), f->widget,
448                    1, 2,
449                    f->row, f->row + 1,
450                    GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
451                    1, 1);
452   --prefs_unfilled;
453   if(prefs_total)
454     progress_window_progress(pw, prefs_total - prefs_unfilled, prefs_total);
455   if(!prefs_unfilled)
456     prefdata_alldone();
457 }
458
459 /* Button callbacks -------------------------------------------------------- */
460
461 static void properties_ok(GtkButton *button, 
462                           gpointer userdata) {
463   properties_apply(button, userdata);
464   properties_cancel(button, userdata);
465 }
466
467 static void properties_apply(GtkButton attribute((unused)) *button, 
468                              gpointer attribute((unused)) userdata) {
469   int n;
470   const char *edited;
471   struct prefdata *f;
472
473   /* For each possible property we see if we've changed it and if so tell the
474    * server */
475   for(n = 0; n < prefs_total; ++n) {
476     f = &prefdatas[n];
477     edited = f->p->type->get_edited(f);
478     if(strcmp(edited, f->value)) {
479       /* The value has changed */
480       f->p->type->set(f, edited);
481       f->value = xstrdup(edited);
482     }
483   }
484 }
485
486 static void properties_cancel(GtkButton attribute((unused)) *button,
487                               gpointer attribute((unused)) userdata) {
488   gtk_widget_destroy(properties_window);
489 }
490
491 /** @brief Called on client reset
492  *
493  * Destroys the current properties window.
494  */
495 void properties_reset(void) {
496   if(properties_window)
497     gtk_widget_destroy(properties_window);
498 }
499
500 /*
501 Local Variables:
502 c-basic-offset:2
503 comment-column:40
504 fill-column:79
505 indent-tabs-mode:nil
506 End:
507 */