6 /** \file playlist.c Functions for loading and saving playlists. */
8 /** Structure used for adding entries to a playlist. */
10 /** The name of the playlist. */
12 /** The number of entries currently in the playlist. */
15 static struct playlist_info playlist;
18 * Re-insert an audio file into the tree of admissible files.
20 * \param aft_row Determines the audio file.
22 * \return The return value of score_update().
24 int playlist_update_audio_file(struct osl_row *aft_row)
26 /* always re-insert to the top of the tree */
27 return score_update(aft_row, 0);
30 static int add_playlist_entry(char *line, void *private_data)
32 struct osl_row *aft_row;
33 struct playlist_info *pli = private_data;
34 int ret = aft_get_row_of_path(line, &aft_row);
37 PARA_NOTICE_LOG("path not found in audio file table: %s\n",
41 ret = score_add(aft_row, -pli->length);
43 PARA_ERROR_LOG("failed to add %s: %d\n", line, ret);
50 static int load_playlist(struct osl_row *row)
52 struct osl_object obj;
55 ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
58 playlist.name = para_strdup(obj.data);
60 ret = osl_open_disk_object(playlists_table, row, BLOBCOL_DEF, &obj);
63 ret = for_each_line_ro(obj.data, obj.size, add_playlist_entry,
65 osl_close_disk_object(&obj);
68 ret = -E_PLAYLIST_EMPTY;
71 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist.name,
79 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
80 static int playlist_loop(struct osl_row *row, __a_unused void *private_data)
82 int ret = load_playlist(row);
84 if (ret != -E_DUMMY_ROW)
85 PARA_NOTICE_LOG("unable to load playlist, trying next\n");
88 return -E_PLAYLIST_LOADED;
91 static int load_first_available_playlist(void)
93 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, NULL,
95 if (ret == -E_PLAYLIST_LOADED) /* success */
98 return ret; /* error */
99 PARA_NOTICE_LOG("no valid playlist found\n");
100 return -E_NO_PLAYLIST;
104 * Close the current playlist.
106 * \sa playlist_open().
108 void playlist_close(void)
111 playlist.name = NULL;
115 * Open the given playlist.
117 * \param name The name of the playlist to open.
119 * If name is \p NULL, load the first available playlist. Files which are
120 * listed in the playlist, but not contained in the database are ignored.
121 * This is not considered an error.
123 * \return Positive on success, negative on errors. Possible errors
124 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
126 int playlist_open(char *name)
128 struct osl_object obj;
133 return load_first_available_playlist();
135 obj.size = strlen(obj.data);
136 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
138 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
141 return load_playlist(row);