Use non-blocking API for signal handling.
[paraslash.git] / playlist.c
index 429ad2b..b04598b 100644 (file)
@@ -1,8 +1,18 @@
+/*
+ * Copyright (C) 2007-2010 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+#include <regex.h>
+#include <osl.h>
+
 #include "para.h"
 #include "error.h"
+#include "string.h"
 #include "afh.h"
 #include "afs.h"
-#include "string.h"
+#include "ipc.h"
 
 /** \file playlist.c Functions for loading and saving playlists. */
 
@@ -22,7 +32,7 @@ static struct playlist_info current_playlist;
  *
  * \return The return value of score_update().
  */
-int playlist_update_audio_file(struct osl_row *aft_row)
+static int playlist_update_audio_file(const struct osl_row *aft_row)
 {
        /* always re-insert to the top of the tree */
        return score_update(aft_row, 0);
@@ -35,30 +45,18 @@ static int add_playlist_entry(char *path, void *data)
        int ret = aft_get_row_of_path(path, &aft_row);
 
        if (ret < 0) {
-               PARA_NOTICE_LOG("%s: %s\n", path, PARA_STRERROR(-ret));
+               PARA_NOTICE_LOG("%s: %s\n", path, para_strerror(-ret));
                return 1;
        }
        ret = score_add(aft_row, -playlist->length);
        if (ret < 0) {
-               PARA_ERROR_LOG("failed to add %s: %d\n", path, ret);
+               PARA_ERROR_LOG("failed to add %s: %s\n", path, para_strerror(-ret));
                return ret;
        }
        playlist->length++;
        return 1;
 }
 
-static int get_playlist_data(struct osl_row *row, char **playlist_name,
-               struct osl_object *playlist_def)
-{
-       struct osl_object obj;
-       int ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
-       if (ret < 0)
-               return ret;
-       *playlist_name = obj.data;
-       return osl_open_disk_object(playlists_table, row, BLOBCOL_DEF,
-               playlist_def);
-}
-
 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
 static int load_playlist(struct osl_row *row, void *data)
 {
@@ -67,7 +65,7 @@ static int load_playlist(struct osl_row *row, void *data)
        char *playlist_name;
        int ret;
 
-       ret = get_playlist_data(row, &playlist_name, &playlist_def);
+       ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
        if (ret < 0)
                goto err;
        playlist->length = 0;
@@ -86,31 +84,19 @@ static int load_playlist(struct osl_row *row, void *data)
 err:
        if (ret != -E_DUMMY_ROW)
                PARA_NOTICE_LOG("unable to load playlist (%s)\n",
-                       PARA_STRERROR(-ret));
+                       para_strerror(-ret));
        return 1;
 }
 
-static int load_first_available_playlist(struct playlist_info *playlist)
-{
-       int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
-               load_playlist);
-       if (ret == -E_PLAYLIST_LOADED) /* success */
-               return 1;
-       if (ret < 0)
-               return ret; /* error */
-       PARA_NOTICE_LOG("no valid playlist found\n");
-       return -E_NO_PLAYLIST;
-}
-
 static int check_playlist_path(char *path, void *data)
 {
        struct para_buffer *pb = data;
        struct osl_row *aft_row;
        int ret = aft_get_row_of_path(path, &aft_row);
 
-       if (ret < 0)
-               para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
-       return 1;
+       if (ret >= 0)
+               return 1;
+       return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
 }
 
 static int check_playlist(struct osl_row *row, void *data)
@@ -118,33 +104,46 @@ static int check_playlist(struct osl_row *row, void *data)
        struct para_buffer *pb = data;
        struct osl_object playlist_def;
        char *playlist_name;
-       int ret = get_playlist_data(row, &playlist_name, &playlist_def);
+       int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
 
-       if (ret < 0) {
-               para_printf(pb, "failed to get playlist data: %s\n",
-                       PARA_STRERROR(-ret));
-               return 1;
-       }
+       if (ret < 0)
+               return para_printf(pb, "failed to get playlist data: %s\n",
+                       para_strerror(-ret));
        if (*playlist_name) { /* skip dummy row */
-               para_printf(pb, "checking playlist %s...\n", playlist_name);
-               for_each_line_ro(playlist_def.data, playlist_def.size,
+               ret = para_printf(pb, "checking playlist %s...\n", playlist_name);
+               if (ret < 0)
+                       return ret;
+               ret = for_each_line_ro(playlist_def.data, playlist_def.size,
                        check_playlist_path, pb);
        }
        osl_close_disk_object(&playlist_def);
-       return 1;
+       return ret;
 }
 
-int playlist_check_callback(__a_unused const struct osl_object *query,
-               struct osl_object *result)
+/**
+ * Check the playlist table for inconsistencies.
+ *
+ * \param fd The afs socket.
+ * \param query Unused.
+ *
+ * \return The return value of the underlying call to osl_rbtree_loop().
+ */
+void playlist_check_callback(int fd, __a_unused const struct osl_object *query)
 {
-       struct para_buffer pb = {.buf = NULL};
+       struct para_buffer pb = {
+               .max_size = SHMMAX,
+               .private_data = &fd,
+               .max_size_handler = pass_buffer_as_shm
+       };
+       int ret = para_printf(&pb, "checking playlists...\n");
 
-       para_printf(&pb, "checking playlists...\n");
+       if (ret < 0)
+               return;
        osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
                check_playlist);
-       result->data = pb.buf;
-       result->size = pb.size;
-       return 1;
+       if (pb.offset)
+               pass_buffer_as_shm(pb.buf, pb.offset, &fd);
+       free(pb.buf);
 }
 
 /**
@@ -154,6 +153,8 @@ int playlist_check_callback(__a_unused const struct osl_object *query,
  */
 void playlist_close(void)
 {
+       if (!current_playlist.name)
+               return;
        free(current_playlist.name);
        current_playlist.name = NULL;
 }
@@ -163,12 +164,10 @@ void playlist_close(void)
  *
  * \param name The name of the playlist to open.
  *
- * If name is \p NULL, load the first available playlist. Files which are
- * listed in the playlist, but not contained in the database are ignored.
- * This is not considered an error.
+ * Files which are listed in the playlist, but not contained in the database
+ * are ignored.  This is not considered an error.
  *
- * \return Positive on success, negative on errors. Possible errors
- * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
+ * \return Standard.
  */
 int playlist_open(char *name)
 {
@@ -176,14 +175,93 @@ int playlist_open(char *name)
        int ret;
        struct osl_row *row;
 
-       if (!name)
-               return load_first_available_playlist(&current_playlist);
        obj.data = name;
        obj.size = strlen(obj.data);
-       ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
+       ret = osl(osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row));
        if (ret < 0) {
                PARA_NOTICE_LOG("failed to load playlist %s\n", name);
                return ret;
        }
-       return load_playlist(row, &current_playlist);
+       playlist_close();
+       ret = load_playlist(row, &current_playlist);
+       return (ret == -E_PLAYLIST_LOADED)? current_playlist.length : ret;
+}
+
+static int search_path(char *path, void *data)
+{
+       if (strcmp(path, data))
+               return 1;
+       return -E_PATH_FOUND;
+}
+
+static int handle_audio_file_event(enum afs_events event, void *data)
+{
+       int ret, was_admissible = 0, is_admissible;
+       struct osl_object playlist_def;
+       char *new_path;
+       const struct osl_row *row = data;
+
+       if (!current_playlist.name)
+               return 1;
+       if (event == AUDIO_FILE_RENAME) {
+               ret = row_belongs_to_score_table(row, NULL);
+               if (ret < 0)
+                       return ret;
+               was_admissible = ret;
+       }
+       ret = get_audio_file_path_of_row(row, &new_path);
+       if (ret < 0)
+               return ret;
+       ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
+       if (ret < 0)
+               return ret;
+       ret = for_each_line_ro(playlist_def.data, playlist_def.size,
+               search_path, new_path);
+       osl_close_disk_object(&playlist_def);
+       is_admissible = (ret < 0);
+       if (was_admissible && is_admissible)
+               return 1;
+       if (!was_admissible && !is_admissible)
+               return 1;
+       if (was_admissible && !is_admissible) {
+               current_playlist.length--;
+               return score_delete(row);
+       }
+       /* !was_admissible && is_admissible */
+       current_playlist.length++;
+       return score_add(row, 0); /* play it immediately */
+}
+
+/**
+ * Handle afs events relevant to playlists.
+ *
+ * \param event The event type.
+ * \param pb Unused.
+ * \param data Depends on the event type.
+ *
+ * \return Standard.
+ */
+int playlists_event_handler(enum afs_events event,
+       __a_unused struct para_buffer *pb, void *data)
+{
+       int ret;
+       struct afsi_change_event_data *aced = data;
+
+       switch(event) {
+       case AFSI_CHANGE:
+               return playlist_update_audio_file(aced->aft_row);
+       case AUDIO_FILE_RENAME:
+       case AUDIO_FILE_ADD:
+               return handle_audio_file_event(event, data);
+       case AUDIO_FILE_REMOVE:
+               ret = row_belongs_to_score_table(data, NULL);
+               if (ret < 0)
+                       return ret;
+               if (!ret)
+                       return 1;
+               current_playlist.length--;
+               return score_delete(data);
+       default:
+               return 1;
+       }
 }