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