chiark / gitweb /
tests for cache.c
[disorder] / server / dcgi.c
index 2892a43189f8e0ea200321f59c84ac9d729a213b..07e1825e705f409e9cb1f211c856a010c9725219 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
+ * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -46,7 +46,6 @@
 #include "queue.h"
 #include "plugin.h"
 #include "split.h"
-#include "words.h"
 #include "wstat.h"
 #include "kvp.h"
 #include "syscalls.h"
@@ -54,6 +53,7 @@
 #include "regsub.h"
 #include "defs.h"
 #include "trackname.h"
+#include "charset.h"
 
 static void expand(cgi_sink *output,
                   const char *template,
@@ -117,6 +117,8 @@ static void lookups(dcgi_state *ds, unsigned want) {
       disorder_queue(ds->g->client, &ds->g->queue);
     if(need & DC_PLAYING)
       disorder_playing(ds->g->client, &ds->g->playing);
+    if(need & DC_NEW)
+      disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
     if(need & DC_RECENT) {
       /* we need to reverse the order of the list */
       disorder_recent(ds->g->client, &r);
@@ -483,7 +485,7 @@ static void exp_length(int attribute((unused)) nargs,
                       cgi_sink *output,
                       void *u) {
   dcgi_state *ds = u;
-  long length;
+  long length = 0;
 
   if(ds->track
      && (ds->track->state == playing_started
@@ -491,8 +493,11 @@ static void exp_length(int attribute((unused)) nargs,
      && ds->track->sofar >= 0)
     cgi_output(output, "%ld:%02ld/",
               ds->track->sofar / 60, ds->track->sofar % 60);
-  if(!ds->track || disorder_length(ds->g->client, ds->track->track, &length))
-    length = 0;
+  length = 0;
+  if(ds->track)
+    disorder_length(ds->g->client, ds->track->track, &length);
+  else if(ds->tracks)
+    disorder_length(ds->g->client, ds->tracks[0], &length);
   if(length)
     cgi_output(output, "%ld:%02ld", length / 60, length % 60);
   else
@@ -562,8 +567,11 @@ static void exp_part(int nargs,
     default:
       abort();
     }
-    if(disorder_part(ds->g->client, (char **)&s, track, context, part))
+    if(disorder_part(ds->g->client, (char **)&s, track,
+                    !strcmp(context, "short") ? "display" : context, part))
       fatal(0, "disorder_part() failed");
+    if(!strcmp(context, "short"))
+      s = truncate_for_display(s, config->short_display);
     cgi_output(output, "%s", s);
   } else
     sink_printf(output->sink, " ");
@@ -627,6 +635,25 @@ static void exp_recent(int attribute((unused)) nargs,
   }
 }
 
+static void exp_new(int attribute((unused)) nargs,
+                   char **args,
+                   cgi_sink *output,
+                   void  *u) {
+  dcgi_state *ds = u;
+  dcgi_state s;
+
+  lookups(ds, DC_NEW);
+  memset(&s, 0, sizeof s);
+  s.g = ds->g;
+  s.first = 1;
+  for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
+    s.last = s.index + 1 < ds->g->nnew;
+    s.tracks = &ds->g->new[s.index];
+    expandstring(output, args[0], &s);
+    s.first = 0;
+  }
+}
+
 static void exp_url(int attribute((unused)) nargs,
                    char attribute((unused)) **args,
                    cgi_sink *output,
@@ -918,6 +945,16 @@ static void exp_isrecent(int attribute((unused)) nargs,
   sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
 }
 
+static void exp_isnew(int attribute((unused)) nargs,
+                     char attribute((unused)) **args,
+                     cgi_sink *output,
+                     void *u) {
+  dcgi_state *ds = u;
+
+  lookups(ds, DC_NEW);
+  sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
+}
+
 static void exp_id(int attribute((unused)) nargs,
                   char attribute((unused)) **args,
                   cgi_sink *output,
@@ -1385,6 +1422,7 @@ static const struct cgi_expansion expansions[] = {
   { "isfiles", 0, 0, 0, exp_isfiles },
   { "isfirst", 0, 0, 0, exp_isfirst },
   { "islast", 0, 0, 0, exp_islast },
+  { "isnew", 0, 0, 0, exp_isnew },
   { "isplaying", 0, 0, 0, exp_isplaying },
   { "isqueue", 0, 0, 0, exp_isqueue },
   { "isrecent", 0, 0, 0, exp_isrecent },
@@ -1392,6 +1430,7 @@ static const struct cgi_expansion expansions[] = {
   { "length", 0, 0, 0, exp_length },
   { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
   { "ne", 2, 2, 0, exp_ne },
+  { "new", 1, 1, EXP_MAGIC, exp_new },
   { "nfiles", 0, 0, 0, exp_nfiles },
   { "nonce", 0, 0, 0, exp_nonce },
   { "not", 1, 1, 0, exp_not },