chiark / gitweb /
fix size of state bit table
[disorder] / disobedience / properties.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2006 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 {
131   const gchar *stock;
132   void (*clicked)(GtkButton *button, gpointer userdata);
133 } buttons[] = {
134   { GTK_STOCK_OK, properties_ok },
135   { GTK_STOCK_APPLY, properties_apply },
136   { GTK_STOCK_CANCEL, properties_cancel },
137 };
138
139 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
140
141 static int prefs_unfilled;              /* Prefs remaining to get */
142 static int prefs_total;                 /* Total prefs */
143 static struct prefdata *prefdatas;      /* Current prefdatas */
144 static GtkWidget *properties_window;
145 static GtkWidget *properties_table;
146 static GtkWidget *progress_window, *progress_bar;
147
148 static void propagate_clicked(GtkButton attribute((unused)) *button,
149                               gpointer userdata) {
150   struct prefdata *f = (struct prefdata *)userdata, *g;
151   int p;
152   const char *value = f->p->type->get_edited(f);
153   
154   for(p = 0; p < prefs_total; ++p) {
155     g = &prefdatas[p];
156     if(f->p == g->p && f != g)
157       g->p->type->set_edited(g, value);
158   }
159 }
160
161 void properties(int ntracks, char **tracks) {
162   int n, m;
163   struct prefdata *f;
164   GtkWidget *hbox, *vbox, *button, *label, *entry, *propagate, *content;
165   GdkPixbuf *pb;
166
167   /* If there is a properties window open then just bring it to the
168    * front.  It might not have the right values in... */
169   if(properties_window) {
170     if(!prefs_unfilled)
171       gtk_window_present(GTK_WINDOW(properties_window));
172     return;
173   }
174   assert(properties_table == 0);
175   if(ntracks > INT_MAX / NPREFS) {
176     popup_error("Too many tracks selected");
177     return;
178   }
179   /* Create a new properties window */
180   properties_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
181   g_signal_connect(properties_window, "destroy",
182                    G_CALLBACK(gtk_widget_destroyed), &properties_window);
183   /* Most of the action is the table of preferences */
184   properties_table = gtk_table_new((NPREFS + 1) * ntracks, 2 + ntracks > 1,
185                                    FALSE);
186   g_signal_connect(properties_table, "destroy",
187                    G_CALLBACK(gtk_widget_destroyed), &properties_table);
188   gtk_window_set_title(GTK_WINDOW(properties_window), "Track Properties");
189   /* Create labels for each pref of each track and kick off requests to the
190    * server to fill in the values */
191   prefs_total = NPREFS * ntracks;
192   prefdatas = xcalloc(prefs_total, sizeof *prefdatas);
193   for(n = 0; n < ntracks; ++n) {
194     /* The track itself */
195     /* Caption */
196     label = gtk_label_new("Track");
197     gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
198     gtk_table_attach(GTK_TABLE(properties_table),
199                      label,
200                      0, 1,
201                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
202                      GTK_FILL, 0,
203                      1, 1);
204     /* The track name */
205     entry = gtk_entry_new();
206     gtk_entry_set_text(GTK_ENTRY(entry), tracks[n]);
207     gtk_editable_set_editable(GTK_EDITABLE(entry), FALSE);
208     gtk_table_attach(GTK_TABLE(properties_table),
209                      entry,
210                      1, 2,
211                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
212                      GTK_EXPAND|GTK_FILL, 0,
213                      1, 1);
214     /* Each preference */
215     for(m = 0; m < NPREFS; ++m) {
216       /* Caption */
217       label = gtk_label_new(prefs[m].label);
218       gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
219       gtk_table_attach(GTK_TABLE(properties_table),
220                        label,
221                        0, 1,
222                        (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
223                        GTK_FILL/*xoptions*/, 0/*yoptions*/,
224                        1, 1);
225       /* Editing the preference is specific */
226       f = &prefdatas[NPREFS * n + m];
227       f->track = tracks[n];
228       f->row = (NPREFS + 1) * n + 1 + m;
229       f->p = &prefs[m];
230       prefs[m].type->kickoff(f);
231       if(ntracks > 1) {
232         /* Propagation button */
233         propagate = gtk_button_new();
234         if((pb = find_image("propagate.png")))
235           content = gtk_image_new_from_pixbuf(pb);
236         else
237           content = gtk_label_new("propagate.png");
238         gtk_container_add(GTK_CONTAINER(propagate), content);
239         gtk_tooltips_set_tip(tips, propagate, "Copy to other tracks", "");
240         g_signal_connect(G_OBJECT(propagate), "clicked",
241                          G_CALLBACK(propagate_clicked), f);
242         gtk_table_attach(GTK_TABLE(properties_table),
243                          propagate,
244                          2/*left*/, 3/*right*/,
245                          (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
246                          GTK_FILL/*xoptions*/, 0/*yoptions*/,
247                          1/*xpadding*/, 1/*ypadding*/);
248       }
249     }
250   }
251   prefs_unfilled = prefs_total;
252   /* Buttons */
253   hbox = gtk_hbox_new(FALSE, 1);
254   for(n = 0; n < NBUTTONS; ++n) {
255     button = gtk_button_new_from_stock(buttons[n].stock);
256     g_signal_connect(G_OBJECT(button), "clicked",
257                      G_CALLBACK(buttons[n].clicked), 0);
258     gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
259   }
260   /* Put it all together */
261   vbox = gtk_vbox_new(FALSE, 1);
262   gtk_box_pack_start(GTK_BOX(vbox), 
263                      scroll_widget(properties_table,
264                                    "properties"),
265                      TRUE, TRUE, 1);
266   gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 1);
267   gtk_container_add(GTK_CONTAINER(properties_window), vbox);
268   /* The table only really wants to be vertically scrollable */
269   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(GTK_WIDGET(properties_table)->parent->parent),
270                                  GTK_POLICY_NEVER,
271                                  GTK_POLICY_AUTOMATIC);
272   /* Pop up a progress bar while we're waiting */
273   progress_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
274   g_signal_connect(progress_window, "destroy",
275                    G_CALLBACK(gtk_widget_destroyed), &progress_window);
276   gtk_window_set_default_size(GTK_WINDOW(progress_window), 360, -1);
277   gtk_window_set_title(GTK_WINDOW(progress_window),
278                        "Fetching Track Properties");
279   progress_bar = gtk_progress_bar_new();
280   gtk_container_add(GTK_CONTAINER(progress_window), progress_bar);
281   gtk_widget_show_all(progress_window);
282 }
283
284 /* Everything is filled in now */
285 static void prefdata_alldone(void) {
286   if(progress_window)
287     gtk_widget_destroy(progress_window);
288   /* Default size may be too small */
289   gtk_window_set_default_size(GTK_WINDOW(properties_window), 480, 512);
290   /* TODO: relate default size to required size more closely */
291   gtk_widget_show_all(properties_window);
292 }
293
294 /* Namepart preferences ---------------------------------------------------- */
295
296 static void kickoff_namepart(struct prefdata *f) {
297   /* We ask for the display name part.  This is a bit bizarre if what we really
298    * wanted was the underlying preference, but in fact it should always match
299    * and will supply a sane default without having to know how to parse tracks
300    * names (which implies knowing collection roots). */
301   disorder_eclient_namepart(client, prefdata_completed, f->track, "display", f->p->part,
302                             make_callbackdata(f));
303 }
304
305 static void completed_namepart(struct prefdata *f) {
306   if(!f->value) {
307     /* No setting */
308     f->value = "";
309   }
310   f->widget = gtk_entry_new();
311 }
312
313 static const char *get_edited_namepart(struct prefdata *f) {
314   return gtk_entry_get_text(GTK_ENTRY(f->widget));
315 }
316
317 static void set_edited_namepart(struct prefdata *f, const char *value) {
318   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
319 }
320
321 static void set_namepart(struct prefdata *f, const char *value) {
322   char *s;
323   struct callbackdata *cbd = xmalloc(sizeof *cbd);
324
325   cbd->u.f = f;
326   byte_xasprintf(&s, "trackname_display_%s", f->p->part);
327   /* We don't know what the default is so can never unset.  This is a bug
328    * relative to the original design, which is supposed to only ever allow for
329    * non-trivial namepart preferences.  I suppose the server could spot a
330    * default being set and translate it into an unset. */
331   disorder_eclient_set(client, set_namepart_completed, f->track, s, value,
332                        cbd);
333 }
334
335 /* Called when we've set a namepart */
336 static void set_namepart_completed(void *v) {
337   struct callbackdata *cbd = v;
338   struct prefdata *f = cbd->u.f;
339
340   namepart_update(f->track, "display", f->p->part);
341 }
342
343 /* String preferences ------------------------------------------------------ */
344
345 static void kickoff_string(struct prefdata *f) {
346   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
347                        make_callbackdata(f));
348 }
349
350 static void completed_string(struct prefdata *f) {
351   if(!f->value)
352     /* No setting, use the default value instead */
353     f->value = f->p->default_value;
354   f->widget = gtk_entry_new();
355 }
356
357 static const char *get_edited_string(struct prefdata *f) {
358   return gtk_entry_get_text(GTK_ENTRY(f->widget));
359 }
360
361 static void set_edited_string(struct prefdata *f, const char *value) {
362   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
363 }
364
365 static void set_string(struct prefdata *f, const char *value) {
366   if(strcmp(f->p->default_value, value))
367     /* Different from default, set it */
368     disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part,
369                          value, 0/*v*/);
370   else
371     /* Same as default, just unset */
372     disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part,
373                            0/*v*/);
374 }
375
376 /* Boolean preferences ----------------------------------------------------- */
377
378 static void kickoff_boolean(struct prefdata *f) {
379   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, 
380                        make_callbackdata(f));
381 }
382
383 static void completed_boolean(struct prefdata *f) {
384   f->widget = gtk_check_button_new();
385   if(!f->value)
386     /* Not set, use the default */
387     f->value = f->p->default_value;
388 }
389
390 static const char *get_edited_boolean(struct prefdata *f) {
391   return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
392           ? "1" : "0");
393 }
394
395 static void set_edited_boolean(struct prefdata *f, const char *value) {
396   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
397                                strcmp(value, "0"));
398 }
399
400 static void set_boolean(struct prefdata *f, const char *value) {
401   char *s;
402
403   byte_xasprintf(&s, "trackname_display_%s", f->p->part);
404   if(strcmp(value, f->p->default_value))
405     disorder_eclient_set(client, 0/*completed*/, f->track, f->p->part, value,
406                          0/*v*/);
407   else
408     /* If default value then delete the pref */
409     disorder_eclient_unset(client, 0/*completed*/, f->track, f->p->part,
410                            0/*v*/);
411 }
412
413 /* Querying preferences ---------------------------------------------------- */
414
415 /* Make a suitable callbackdata */
416 static struct callbackdata *make_callbackdata(struct prefdata *f) {
417   struct callbackdata *cbd = xmalloc(sizeof *cbd);
418
419   cbd->onerror = prefdata_onerror;
420   cbd->u.f = f;
421   return cbd;
422 }
423
424 /* No pref was set */
425 static void prefdata_onerror(struct callbackdata *cbd,
426                              int attribute((unused)) code,
427                              const char attribute((unused)) *msg) {
428   prefdata_completed_common(cbd->u.f, 0);
429 }
430
431 /* Got the value of a pref */
432 static void prefdata_completed(void *v, const char *value) {
433   struct callbackdata *cbd = v;
434
435   prefdata_completed_common(cbd->u.f, value);
436 }
437
438 static void prefdata_completed_common(struct prefdata *f,
439                                       const char *value) {
440   f->value = value;
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,
445                    1, 2,
446                    f->row, f->row + 1,
447                    GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
448                    1, 1);
449   --prefs_unfilled;
450   if(prefs_total && progress_window)
451     gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar),
452                                   1.0 - (double)prefs_unfilled / prefs_total);
453   if(!prefs_unfilled)
454     prefdata_alldone();
455 }
456
457 /* Button callbacks -------------------------------------------------------- */
458
459 static void properties_ok(GtkButton *button, 
460                           gpointer userdata) {
461   properties_apply(button, userdata);
462   properties_cancel(button, userdata);
463 }
464
465 static void properties_apply(GtkButton attribute((unused)) *button, 
466                              gpointer attribute((unused)) userdata) {
467   int n;
468   const char *edited;
469   struct prefdata *f;
470
471   /* For each possible property we see if we've changed it and if so tell the
472    * server */
473   for(n = 0; n < prefs_total; ++n) {
474     f = &prefdatas[n];
475     edited = f->p->type->get_edited(f);
476     if(strcmp(edited, f->value)) {
477       /* The value has changed */
478       f->p->type->set(f, edited);
479       f->value = xstrdup(edited);
480     }
481   }
482 }
483
484 static void properties_cancel(GtkButton attribute((unused)) *button,
485                               gpointer attribute((unused)) userdata) {
486   gtk_widget_destroy(properties_window);
487 }
488
489 /*
490 Local Variables:
491 c-basic-offset:2
492 comment-column:40
493 fill-column:79
494 indent-tabs-mode:nil
495 End:
496 */