7 /** \file playlist.c Functions for loading and saving playlists. */
9 /** Structure used for adding entries to a playlist. */
10 struct playlist_info {
11 /** The name of the playlist. */
13 /** The number of entries currently in the playlist. */
16 static struct playlist_info current_playlist;
19 * Re-insert an audio file into the tree of admissible files.
21 * \param aft_row Determines the audio file.
23 * \return The return value of score_update().
25 int playlist_update_audio_file(struct osl_row *aft_row)
27 /* always re-insert to the top of the tree */
28 return score_update(aft_row, 0);
31 static int add_playlist_entry(char *line, void *data)
33 struct playlist_info *playlist = data;
34 struct osl_row *aft_row;
35 int ret = aft_get_row_of_path(line, &aft_row);
38 PARA_NOTICE_LOG("path not found in audio file table: %s\n",
42 ret = score_add(aft_row, -playlist->length);
44 PARA_ERROR_LOG("failed to add %s: %d\n", line, ret);
51 static int load_playlist(struct osl_row *row, struct playlist_info *playlist)
53 struct osl_object obj;
56 ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
59 playlist->name = para_strdup(obj.data);
61 ret = osl_open_disk_object(playlists_table, row, BLOBCOL_DEF, &obj);
64 ret = for_each_line_ro(obj.data, obj.size, add_playlist_entry,
66 osl_close_disk_object(&obj);
69 ret = -E_PLAYLIST_EMPTY;
70 if (!playlist->length)
72 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
80 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
81 static int playlist_loop(struct osl_row *row, void *data)
83 struct playlist_info *playlist = data;
84 int ret = load_playlist(row, playlist);
86 if (ret != -E_DUMMY_ROW)
87 PARA_NOTICE_LOG("unable to load playlist, trying next\n");
90 return -E_PLAYLIST_LOADED;
93 static int load_first_available_playlist(struct playlist_info *playlist)
95 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
97 if (ret == -E_PLAYLIST_LOADED) /* success */
100 return ret; /* error */
101 PARA_NOTICE_LOG("no valid playlist found\n");
102 return -E_NO_PLAYLIST;
106 * Close the current playlist.
108 * \sa playlist_open().
110 void playlist_close(void)
112 free(current_playlist.name);
113 current_playlist.name = NULL;
117 * Open the given playlist.
119 * \param name The name of the playlist to open.
121 * If name is \p NULL, load the first available playlist. Files which are
122 * listed in the playlist, but not contained in the database are ignored.
123 * This is not considered an error.
125 * \return Positive on success, negative on errors. Possible errors
126 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
128 int playlist_open(char *name)
130 struct osl_object obj;
135 return load_first_available_playlist(¤t_playlist);
137 obj.size = strlen(obj.data);
138 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
140 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
143 return load_playlist(row, ¤t_playlist);