chiark / gitweb /
tracks + dirs expansions
[disorder] / server / macros-disorder.c
index e58324bf6cc3bdf0f0b719fb3c5e68631ea5a1ca..92fcb754f785e1f39b260f74b96d6428eb2f25fd 100644 (file)
 /** @brief For error template */
 char *dcgi_error_string;
 
-/** @brief Locate a track by ID */
-static struct queue_entry *findtrack(const char *id) {
-  struct queue_entry *q;
-
-  dcgi_lookup(DCGI_PLAYING);
-  if(dcgi_playing && !strcmp(dcgi_playing->id, id))
-    return dcgi_playing;
-  dcgi_lookup(DCGI_QUEUE);
-  for(q = dcgi_queue; q; q = q->next)
-    if(!strcmp(q->id, id))
-      return q;
-  dcgi_lookup(DCGI_RECENT);
-  for(q = dcgi_recent; q; q = q->next)
-    if(!strcmp(q->id, id))
-      return q;
-  return NULL;
-}
-
 /** @brief Return @p i as a string */
 static const char *make_index(int i) {
   char *s;
@@ -150,7 +132,7 @@ static int exp_part(int nargs,
   char *s;
 
   if(track[0] != '/') {
-    struct queue_entry *q = findtrack(track);
+    struct queue_entry *q = dcgi_findtrack(track);
 
     if(q)
       track = q->track;
@@ -187,7 +169,7 @@ static int exp_who(int attribute((unused)) nargs,
                    char **args,
                    struct sink *output,
                    void attribute((unused)) *u) {
-  struct queue_entry *q = findtrack(args[0]);
+  struct queue_entry *q = dcgi_findtrack(args[0]);
 
   if(q && q->submitter)
     return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
@@ -203,7 +185,7 @@ static int exp_when(int attribute((unused)) nargs,
                    char **args,
                    struct sink *output,
                     void attribute((unused)) *u) {
-  struct queue_entry *q = findtrack(args[0]);
+  struct queue_entry *q = dcgi_findtrack(args[0]);
   const struct tm *w = 0;
 
   if(q) {
@@ -250,7 +232,7 @@ static int exp_length(int attribute((unused)) nargs,
     name = args[0];
   else {
     /* Track identified by queue ID */
-    if(!(q = findtrack(args[0])))
+    if(!(q = dcgi_findtrack(args[0])))
       return 0;
     if(q->state == playing_started || q->state == playing_paused)
       if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
@@ -272,7 +254,7 @@ static int exp_removable(int attribute((unused)) nargs,
                          char **args,
                          struct sink *output,
                          void attribute((unused)) *u) {
-  struct queue_entry *q = findtrack(args[0]);
+  struct queue_entry *q = dcgi_findtrack(args[0]);
   /* TODO would be better to reject recent */
 
   if(!q || !dcgi_client)
@@ -283,29 +265,44 @@ static int exp_removable(int attribute((unused)) nargs,
                             (dcgi_rights, disorder_user(dcgi_client), q));
 }
 
-/* @movable{ID}
+/* @movable{ID}{DIR}
  *
  * Expands to "true" if track ID is movable and "false" otherwise.
+ *
+ * DIR (which is optional) should be a non-zero integer.  If it is negative
+ * then the intended move is down (later in the queue), if it is positive then
+ * the intended move is up (earlier in the queue).  The first track is not
+ * movable up and the last track not movable down.
  */
-static int exp_movable(int attribute((unused)) nargs,
+static int exp_movable(int  nargs,
                        char **args,
                        struct sink *output,
                        void attribute((unused)) *u) {
-  struct queue_entry *q = findtrack(args[0]);
+  struct queue_entry *q = dcgi_findtrack(args[0]);
   /* TODO would be better to recent playing/recent */
 
   if(!q || !dcgi_client)
     return mx_bool_result(output, 0);
+  if(nargs > 1) {
+    const long dir = atoi(args[1]);
+
+    if(dir > 0 && q == dcgi_queue)
+      return mx_bool_result(output, 0);
+    if(dir < 0 && q->next == 0) 
+      return mx_bool_result(output, 0);
+  }
   dcgi_lookup(DCGI_RIGHTS);
   return mx_bool_result(output,
-                        right_movable(dcgi_rights, disorder_user(dcgi_client), q));
+                        right_movable(dcgi_rights,
+                                      disorder_user(dcgi_client),
+                                      q));
 }
 
 /* @playing{TEMPLATE}
  *
  * Expands to TEMPLATE, with:
- * - @id@ expanded to the queue ID of the playing track
- * - @track@ expanded to its UNQUOTED name
+ * - @id expanded to the queue ID of the playing track
+ * - @track expanded to its UNQUOTED name
  *
  * If no track is playing expands to nothing.
  *
@@ -331,12 +328,12 @@ static int exp_playing(int nargs,
 /* @queue{TEMPLATE}
  *
  * For each track in the queue, expands TEMPLATE with the following expansions:
- * - @id@ to the queue ID of the track
- * - @track@ to the UNQUOTED track name
- * - @index@ to the track number from 0
- * - @parity@ to "even" or "odd" alternately
- * - @first@ to "true" on the first track and "false" otherwise
- * - @last@ to "true" on the last track and "false" otherwise
+ * - @id to the queue ID of the track
+ * - @track to the UNQUOTED track name
+ * - @index to the track number from 0
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first track and "false" otherwise
+ * - @last to "true" on the last track and "false" otherwise
  */
 static int exp_queue(int attribute((unused)) nargs,
                      const struct mx_node **args,
@@ -364,12 +361,12 @@ static int exp_queue(int attribute((unused)) nargs,
  *
  * For each track in the recently played list, expands TEMPLATE with the
  * following expansions:
- * - @id@ to the queue ID of the track
- * - @track@  to the UNQUOTED track name
- * - @index@ to the track number from 0
- * - @parity@ to "even" or "odd" alternately
- * - @first@ to "true" on the first track and "false" otherwise
- * - @last@ to "true" on the last track and "false" otherwise
+ * - @id to the queue ID of the track
+ * - @track  to the UNQUOTED track name
+ * - @index to the track number from 0
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first track and "false" otherwise
+ * - @last to "true" on the last track and "false" otherwise
  */
 static int exp_recent(int attribute((unused)) nargs,
                       const struct mx_node **args,
@@ -397,14 +394,14 @@ static int exp_recent(int attribute((unused)) nargs,
  *
  * For each track in the newly added list, expands TEMPLATE wit the following
  * expansions:
- * - @track@ to the UNQUOTED track name
- * - @index@ to the track number from 0
- * - @parity@ to "even" or "odd" alternately
- * - @first@ to "true" on the first track and "false" otherwise
- * - @last@ to "true" on the last track and "false" otherwise
+ * - @track to the UNQUOTED track name
+ * - @index to the track number from 0
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first track and "false" otherwise
+ * - @last to "true" on the last track and "false" otherwise
  *
- * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
- * superficially similar, there is no @id@ sub-expansion here.
+ * Note that unlike @playing, @queue and @recent which are otherwise
+ * superficially similar, there is no @id sub-expansion here.
  */
 static int exp_new(int attribute((unused)) nargs,
                    const struct mx_node **args,
@@ -511,14 +508,14 @@ static int exp_pref(int attribute((unused)) nargs,
  *
  * For each track preference of track TRACK, expands TEMPLATE with the
  * following expansions:
- * - @name@ to the UNQUOTED preference name
- * - @index@ to the preference number from 0
- * - @value@ to the UNQUOTED preference value
- * - @parity@ to "even" or "odd" alternately
- * - @first@ to "true" on the first preference and "false" otherwise
- * - @last@ to "true" on the last preference and "false" otherwise
+ * - @name to the UNQUOTED preference name
+ * - @index to the preference number from 0
+ * - @value to the UNQUOTED preference value
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first preference and "false" otherwise
+ * - @last to "true" on the last preference and "false" otherwise
  *
- * Use @quote@ to quote preference names and values where necessary; see below.
+ * Use @quote to quote preference names and values where necessary; see below.
  */
 static int exp_prefs(int attribute((unused)) nargs,
                      const struct mx_node **args,
@@ -668,7 +665,7 @@ static int exp_state(int attribute((unused)) nargs,
                      char **args,
                      struct sink *output,
                      void attribute((unused)) *u) {
-  struct queue_entry *q = findtrack(args[0]);
+  struct queue_entry *q = dcgi_findtrack(args[0]);
 
   if(q)
     return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
@@ -737,7 +734,159 @@ static int exp_error(int attribute((unused)) nargs,
                      char attribute((unused)) **args,
                      struct sink *output,
                      void attribute((unused)) *u) {
-  return sink_writes(output, cgi_sgmlquote(dcgi_error_string)) < 0 ? -1 : 0;
+  return sink_writes(output, dcgi_error_string) < 0 ? -1 : 0;
+}
+
+/* @image{NAME}
+ *
+ * Expands to the URL of the image called NAME.
+ *
+ * Firstly if there is a label called images.NAME then the image stem will be
+ * the value of that label.  Otherwise the stem will be NAME.png.
+ *
+ * If the label url.static exists then it will give the base URL for images.
+ * Otherwise the base url will be /disorder/.
+ */
+static int exp_image(int attribute((unused)) nargs,
+                     char **args,
+                     struct sink *output,
+                     void attribute((unused)) *u) {
+  const char *url, *stem;
+  char *labelname;
+
+  /* Compute the stem */
+  byte_xasprintf(&labelname, "images.%s", args[0]);
+  if(option_label_exists(labelname))
+    stem = option_label(labelname);
+  else
+    byte_xasprintf((char **)&stem, "%s.png", args[0]);
+  /* If the stem looks like it's reasonalby complete, use that */
+  if(stem[0] == '/'
+     || !strncmp(stem, "http:", 5)
+     || !strncmp(stem, "https:", 6))
+    url = stem;
+  else {
+    /* Compute the URL */
+    if(option_label_exists("url.static"))
+      byte_xasprintf((char **)&url, "%s/%s",
+                     option_label("url.static"), stem);
+    else
+      /* Default base is /disorder */
+      byte_xasprintf((char **)&url, "/disorder/%s", stem);
+  }
+  return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
+}
+
+/** @brief Entry in a list of tracks or directories */
+struct entry {
+  /** @brief Track name */
+  const char *track;
+  /** @brief Sort key */
+  const char *sort;
+  /** @brief Display key */
+  const char *display;
+};
+
+/** @brief Compare two @ref entry objects */
+static int compare_entry(const void *a, const void *b) {
+  const struct entry *ea = a, *eb = b;
+
+  return compare_tracks(ea->sort, eb->sort,
+                       ea->display, eb->display,
+                       ea->track, eb->track);
+}
+
+/** @brief Implementation of exp_tracks() and exp_dirs() */
+static int exp__files_dirs(int nargs,
+                           const struct mx_node **args,
+                           struct sink *output,
+                           void *u,
+                           const char *type,
+                           int (*fn)(disorder_client *client,
+                                     const char *name,
+                                     const char *re,
+                                     char ***vecp,
+                                     int *nvecp)) {
+  char **tracks, *dir, *re;
+  int n, ntracks, rc;
+  const struct mx_node *m;
+  struct entry *e;
+
+  if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
+    return rc;
+  if(nargs == 3)  {
+    if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
+      return rc;
+    m = args[2];
+  } else {
+    re = 0;
+    m = args[1];
+  }
+  if(!dcgi_client)
+    return 0;
+  /* Get the list */
+  if(fn(dcgi_client, dir, re, &tracks, &ntracks))
+    return 0;
+  /* Sort it.  NB trackname_transform() does not go to the server. */
+  e = xcalloc(ntracks, sizeof *e);
+  for(n = 0; n < ntracks; ++n) {
+    e->track = tracks[n];
+    e[n].track = tracks[n];
+    e[n].sort = trackname_transform(type, tracks[n], "sort");
+    e[n].display = trackname_transform(type, tracks[n], "display");
+  }
+  qsort(e, ntracks, sizeof (struct entry), compare_entry);
+  /* Expand the subsiduary templates */
+  for(n = 0; n < ntracks; ++n)
+    if((rc = mx_expand(mx_rewritel(m,
+                                   "index", make_index(n),
+                                   "parity", n % 2 ? "odd" : "even",
+                                   "track", tracks[n],
+                                   "first", n == 0 ? "true" : "false",
+                                   "last", n + 1 == ntracks ? "false" : "true",
+                                   (char *)0),
+                       output, u)))
+      return rc;
+  return 0;
+
+}
+
+/** @tracks{DIR}{RE}{TEMPLATE}
+ *
+ * For each track below DIR, expands TEMPLATE with the
+ * following expansions:
+ * - @track to the UNQUOTED track name
+ * - @index to the track number from 0
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first track and "false" otherwise
+ * - @last to "true" on the last track and "false" otherwise
+ *
+ * RE is optional and if present is the regexp to match against.
+ */
+static int exp_tracks(int nargs,
+                      const struct mx_node **args,
+                      struct sink *output,
+                      void *u) {
+  return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
+}
+
+/** @dirs{DIR}{RE}{TEMPLATE}
+ *
+ * For each directory below DIR, expands TEMPLATE with the
+ * following expansions:
+ * - @track to the UNQUOTED directory name
+ * - @index to the directory number from 0
+ * - @parity to "even" or "odd" alternately
+ * - @first to "true" on the first directory and "false" otherwise
+ * - @last to "true" on the last directory and "false" otherwise
+ *
+ * RE is optional and if present is the regexp to match against.
+ */
+static int exp_dirs(int nargs,
+                    const struct mx_node **args,
+                    struct sink *output,
+                    void *u) {
+  return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
 }
 
 /** @brief Register DisOrder-specific expansions */
@@ -745,6 +894,7 @@ void dcgi_expansions(void) {
   mx_register("arg", 1, 1, exp_arg);
   mx_register("enabled", 0, 0, exp_enabled);
   mx_register("error", 0, 0, exp_error);
+  mx_register("image", 1, 1, exp_image);
   mx_register("isnew", 0, 0, exp_isnew);
   mx_register("isplaying", 0, 0, exp_isplaying);
   mx_register("isplaying", 0, 0, exp_isqueue);
@@ -770,12 +920,14 @@ void dcgi_expansions(void) {
   mx_register("volume", 1, 1, exp_volume);
   mx_register("when", 1, 1, exp_when);
   mx_register("who", 1, 1, exp_who);
+  mx_register_magic("dirs", 2, 3, exp_dirs);
   mx_register_magic("new", 1, 1, exp_new);
   mx_register_magic("playing", 0, 1, exp_playing);
   mx_register_magic("prefs", 2, 2, exp_prefs);
   mx_register_magic("queue", 1, 1, exp_queue);
   mx_register_magic("recent", 1, 1, exp_recent);
   mx_register_magic("right", 1, 3, exp_right);
+  mx_register_magic("tracks", 2, 3, exp_tracks);
 }
 
 /*