chiark / gitweb /
7c1c816aaaa70580950995cade0f8f976d98f4d4
[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 #include "disobedience.h"
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, const char *err);
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 *err, const char *value);
47
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);
51
52 static void properties_logged_in(const char *event,
53                                  void *eventdata,
54                                  void *callbackdata);
55
56 /** @brief Data for a single preference */
57 struct prefdata {
58   const char *track;
59   int row;
60   const struct pref *p;                 /**< @brief kind of preference */
61   const char *value;                    /**< @brief value from server  */
62   GtkWidget *widget;
63 };
64
65 /* The type of a preference is the collection of callbacks needed to get,
66  * display and set it */
67 struct preftype {
68   void (*kickoff)(struct prefdata *f);
69   /* Kick off the request to fetch the pref from the server. */
70
71   void (*completed)(struct prefdata *f);
72   /* Called when the value comes back in; creates the widget. */
73
74   const char *(*get_edited)(struct prefdata *f);
75   /* Get the edited value from the widget. */
76
77   /** @brief Update the edited value */
78   void (*set_edited)(struct prefdata *f, const char *value);
79
80   void (*set)(struct prefdata *f, const char *value);
81   /* Set the new value and (if necessary) arrange for our display to update. */
82 };
83
84 /* A namepart pref */
85 static const struct preftype preftype_namepart = {
86   kickoff_namepart,
87   completed_namepart,
88   get_edited_namepart,
89   set_edited_namepart,
90   set_namepart
91 };
92
93 /* A string pref */
94 static const struct preftype preftype_string = {
95   kickoff_string,
96   completed_string,
97   get_edited_string,
98   set_edited_string,
99   set_string
100 };
101
102 /* A boolean pref */
103 static const struct preftype preftype_boolean = {
104   kickoff_boolean,
105   completed_boolean,
106   get_edited_boolean,
107   set_edited_boolean,
108   set_boolean
109 };
110
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 */
117 } prefs[] = {
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 },
124 };
125
126 #define NPREFS (int)(sizeof prefs / sizeof *prefs)
127
128 /* Buttons that appear at the bottom of the window */
129 static struct button buttons[] = {
130   {
131     GTK_STOCK_OK,
132     properties_ok,
133     "Apply all changes and close window",
134     0
135   },
136   {
137     GTK_STOCK_APPLY,
138     properties_apply,
139     "Apply all changes and keep window open",
140     0
141   },
142   {
143     GTK_STOCK_CANCEL,
144     properties_cancel,
145     "Discard all changes and close window",
146     0
147   },
148 };
149
150 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
151
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;
159
160 static void propagate_clicked(GtkButton attribute((unused)) *button,
161                               gpointer userdata) {
162   struct prefdata *f = (struct prefdata *)userdata, *g;
163   int p;
164   const char *value = f->p->type->get_edited(f);
165   
166   for(p = 0; p < prefs_total; ++p) {
167     g = &prefdatas[p];
168     if(f->p == g->p && f != g)
169       g->p->type->set_edited(g, value);
170   }
171 }
172
173 /** @brief Keypress handler */
174 static gboolean properties_keypress(GtkWidget attribute((unused)) *widget,
175                                     GdkEventKey *event,
176                                     gpointer attribute((unused)) user_data) {
177   if(event->state)
178     return FALSE;
179   switch(event->keyval) {
180   case GDK_Return:
181     properties_ok(0, 0);
182     return TRUE;
183   case GDK_Escape:
184     properties_cancel(0, 0);
185     return TRUE;
186   default:
187     return FALSE;
188   }
189 }
190
191 void properties(int ntracks, const char **tracks) {
192   int n, m;
193   struct prefdata *f;
194   GtkWidget *buttonbox, *vbox, *label, *entry, *propagate;
195   
196   /* If no tracks, do nothign */
197   if(!ntracks)
198     return;
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) {
202     if(!prefs_unfilled)
203       gtk_window_present(GTK_WINDOW(properties_window));
204     return;
205   }
206   assert(properties_table == 0);
207   if(ntracks > INT_MAX / NPREFS) {
208     popup_msg(GTK_MESSAGE_ERROR, "Too many tracks selected");
209     return;
210   }
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,
222                                    FALSE);
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 */
233     /* Caption */
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),
238                      label,
239                      0, 1,
240                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
241                      GTK_FILL, 0,
242                      1, 1);
243     /* The track name */
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),
249                      entry,
250                      1, 2,
251                      (NPREFS + 1) * n, (NPREFS + 1) * n + 1,
252                      GTK_EXPAND|GTK_FILL, 0,
253                      1, 1);
254     /* Each preference */
255     for(m = 0; m < NPREFS; ++m) {
256       /* Caption */
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),
261                        label,
262                        0, 1,
263                        (NPREFS + 1) * n + 1 + m, (NPREFS + 1) * n + 2 + m,
264                        GTK_FILL/*xoptions*/, 0/*yoptions*/,
265                        1, 1);
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;
270       f->p = &prefs[m];
271       prefs[m].type->kickoff(f);
272       if(ntracks > 1) {
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),
278                          propagate,
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*/);
283       }
284     }
285   }
286   prefs_unfilled = prefs_total;
287   /* Buttons */
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),
293                      TRUE, TRUE, 1);
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),
298                                  GTK_POLICY_NEVER,
299                                  GTK_POLICY_AUTOMATIC);
300   /* Zot any pre-existing progress window just in case */
301   if(pw)
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");
305 }
306
307 /* Everything is filled in now */
308 static void prefdata_alldone(void) {
309   if(pw) {
310     progress_window_progress(pw, 0, 0);
311     pw = 0;
312   }
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);
317 }
318
319 /* Namepart preferences ---------------------------------------------------- */
320
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);
328 }
329
330 static void completed_namepart(struct prefdata *f) {
331   if(!f->value) {
332     /* No setting */
333     f->value = "";
334   }
335   f->widget = gtk_entry_new();
336 }
337
338 static const char *get_edited_namepart(struct prefdata *f) {
339   return gtk_entry_get_text(GTK_ENTRY(f->widget));
340 }
341
342 static void set_edited_namepart(struct prefdata *f, const char *value) {
343   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
344 }
345
346 static void set_namepart(struct prefdata *f, const char *value) {
347   char *s;
348
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,
355                        f);
356 }
357
358 /* Called when we've set a namepart */
359 static void set_namepart_completed(void *v, const char *err) {
360   if(err)
361     popup_protocol_error(0, err);
362   else {
363     struct prefdata *f = v;
364     
365     namepart_update(f->track, "display", f->p->part);
366   }
367 }
368
369 /* String preferences ------------------------------------------------------ */
370
371 static void kickoff_string(struct prefdata *f) {
372   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
373 }
374
375 static void completed_string(struct prefdata *f) {
376   if(!f->value)
377     /* No setting, use the default value instead */
378     f->value = f->p->default_value;
379   f->widget = gtk_entry_new();
380 }
381
382 static const char *get_edited_string(struct prefdata *f) {
383   return gtk_entry_get_text(GTK_ENTRY(f->widget));
384 }
385
386 static void set_edited_string(struct prefdata *f, const char *value) {
387   gtk_entry_set_text(GTK_ENTRY(f->widget), value);
388 }
389
390 static void set_string_completed(void attribute((unused)) *v,
391                                  const char *err) {
392   if(err)
393     popup_protocol_error(0, err);
394 }
395
396 static void set_string(struct prefdata *f, const char *value) {
397   disorder_eclient_set(client, set_string_completed, f->track, f->p->part,
398                        value, 0/*v*/);
399 }
400
401 /* Boolean preferences ----------------------------------------------------- */
402
403 static void kickoff_boolean(struct prefdata *f) {
404   disorder_eclient_get(client, prefdata_completed, f->track, f->p->part, f);
405 }
406
407 static void completed_boolean(struct prefdata *f) {
408   f->widget = gtk_check_button_new();
409   gtk_widget_set_style(f->widget, tool_style);
410   if(!f->value)
411     /* Not set, use the default */
412     f->value = f->p->default_value;
413 }
414
415 static const char *get_edited_boolean(struct prefdata *f) {
416   return (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(f->widget))
417           ? "1" : "0");
418 }
419
420 static void set_edited_boolean(struct prefdata *f, const char *value) {
421   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(f->widget),
422                                strcmp(value, "0"));
423 }
424
425 #define set_boolean_completed set_string_completed
426
427 static void set_boolean(struct prefdata *f, const char *value) {
428   char *s;
429
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*/);
433 }
434
435 /* Querying preferences ---------------------------------------------------- */
436
437 static void prefdata_completed(void *v, const char *err, const char *value) {
438   struct prefdata *const f = v;
439
440   if(err)
441     popup_protocol_error(0, err);
442   f->value = value;
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,
447                    1, 2,
448                    f->row, f->row + 1,
449                    GTK_EXPAND|GTK_FILL/*xoptions*/, 0/*yoptions*/,
450                    1, 1);
451   --prefs_unfilled;
452   if(prefs_total)
453     progress_window_progress(pw, prefs_total - prefs_unfilled, prefs_total);
454   if(!prefs_unfilled)
455     prefdata_alldone();
456 }
457
458 /* Button callbacks -------------------------------------------------------- */
459
460 static void properties_ok(GtkButton *button, 
461                           gpointer userdata) {
462   properties_apply(button, userdata);
463   properties_cancel(button, userdata);
464 }
465
466 static void properties_apply(GtkButton attribute((unused)) *button, 
467                              gpointer attribute((unused)) userdata) {
468   int n;
469   const char *edited;
470   struct prefdata *f;
471
472   /* For each possible property we see if we've changed it and if so tell the
473    * server */
474   for(n = 0; n < prefs_total; ++n) {
475     f = &prefdatas[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);
481     }
482   }
483 }
484
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;
490 }
491
492 /** @brief Called when we've just logged in
493  *
494  * Destroys the current properties window.
495  */
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);
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 */