chiark / gitweb /
Translate preferences page to the new order.
[disorder] / lib / trackdb.c
index 6a93fbaba33562676ea7fd0f828ca64855d2dd10..8e775d5626bc15033c2bf554a784d6c51dc97c24 100644 (file)
@@ -162,6 +162,16 @@ static int compare(DB attribute((unused)) *db_,
   return compare_path_raw(a->data, a->size, b->data, b->size);
 }
 
+/** @brief Test whether the track database can be read
+ * @return 1 if it can, 0 if it cannot
+ */
+int trackdb_readable(void) {
+  char *usersdb;
+
+  byte_xasprintf(&usersdb, "%s/users.db", config->home);
+  return access(usersdb, R_OK) == 0;
+}
+
 /** @brief Open database environment
  * @param flags Flags word
  *
@@ -1004,7 +1014,8 @@ int trackdb_notice_tid(const char *track,
   int err, n;
   struct kvp *t, *a, *p;
   int t_changed, ret;
-  char *alias, **w;
+  char *alias, **w, *noticed;
+  time_t now;
 
   /* notice whether the tracks.db entry changes */
   t_changed = 0;
@@ -1015,6 +1026,12 @@ int trackdb_notice_tid(const char *track,
   /* this is a real track */
   t_changed += kvp_set(&t, "_alias_for", 0);
   t_changed += kvp_set(&t, "_path", path);
+  time(&now);
+  if(ret == DB_NOTFOUND) {
+    /* It's a new track; record the time */
+    byte_xasprintf(&noticed, "%lld", (long long)now);
+    t_changed += kvp_set(&t, "_noticed", noticed);
+  }
   /* if we have an alias record it in the database */
   if((err = compute_alias(&alias, track, p, tid))) return err;
   if(alias) {
@@ -1039,10 +1056,8 @@ int trackdb_notice_tid(const char *track,
     return err;
   if(ret == DB_NOTFOUND) {
     uint32_t timestamp[2];
-    time_t now;
     DBT key, data;
 
-    time(&now);
     timestamp[0] = htonl((uint64_t)now >> 32);
     timestamp[1] = htonl((uint32_t)now);
     memset(&key, 0, sizeof key);
@@ -1372,6 +1387,60 @@ void trackdb_stats_subprocess(ev_source *ev,
   ev_reader_new(ev, p[0], stats_read, stats_error, d, "disorder-stats reader");
 }
 
+/** @brief Parse a track name part preference
+ * @param name Preference name
+ * @param partp Where to store part name
+ * @param contextp Where to store context name
+ * @return 0 on success, non-0 if parse fails
+ */
+static int trackdb__parse_namepref(const char *name,
+                                   char **partp,
+                                   char **contextp) {
+  char *c;
+  static const char prefix[] = "trackname_";
+  
+  if(strncmp(name, prefix, strlen(prefix)))
+    return -1;                          /* not trackname_* at all */
+  name += strlen(prefix);
+  /* There had better be a _ between context and part */
+  c = strchr(name, '_');
+  if(!c)
+    return -1;
+  /* Context is first in the pref name even though most APIs have the part
+   * first.  Confusing; sorry. */
+  *contextp = xstrndup(name, c - name);
+  ++c;
+  /* There had better NOT be a second _ */
+  if(strchr(c, '_'))
+    return -1;
+  *partp = xstrdup(c);
+  return 0;
+}
+
+/** @brief Compute the default value for a track preference
+ * @param track Track name
+ * @param name Preference name
+ * @return Default value or 0 if none/not known
+ */
+static const char *trackdb__default(const char *track, const char *name) {
+  char *context, *part;
+  
+  if(!trackdb__parse_namepref(name, &part, &context)) {
+    /* We can work out the default for a trackname_ pref */
+    return trackname_part(track, context, part);
+  } else if(!strcmp(name, "weight")) {
+    /* We know the default weight */
+    return "90000";
+  } else if(!strcmp(name, "pick_at_random")) {
+    /* By default everything is eligible for picking at random */
+    return "1";
+  } else if(!strcmp(name, "tags")) {
+    /* By default everything no track has any tags */
+    return "";
+  }
+  return 0;
+}
+
 /* set a pref (remove if value=0) */
 int trackdb_set(const char *track,
                 const char *name,
@@ -1380,9 +1449,15 @@ int trackdb_set(const char *track,
   DB_TXN *tid;
   int err, cmp;
   char *oldalias, *newalias, **oldtags = 0, **newtags;
+  const char *def;
 
+  /* If the value matches the default then unset instead, to keep the database
+   * tidy.  Older versions did not have this feature so your database may yet
+   * have some default values stored in it. */
   if(value) {
-    /* TODO: if value matches default then set value=0 */
+    def = trackdb__default(track, name);
+    if(def && !strcmp(value, def))
+      value = 0;
   }
 
   for(;;) {
@@ -2630,10 +2705,13 @@ int trackdb_edituserinfo(const char *user,
       return -1;
     }
   } else if(!strcmp(key, "email")) {
-    if(!strchr(value, '@')) {
-      error(0, "invalid email address '%s' for user '%s'", user, value);
-      return -1;
-    }
+    if(*value) {
+      if(!strchr(value, '@')) {
+        error(0, "invalid email address '%s' for user '%s'", user, value);
+        return -1;
+      }
+    } else
+      value = 0;                        /* no email -> remove key */
   } else if(!strcmp(key, "created")) {
     error(0, "cannot change creation date for user '%s'", user);
     return -1;