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 *path, void *data)
33 struct playlist_info *playlist = data;
34 struct osl_row *aft_row;
35 int ret = aft_get_row_of_path(path, &aft_row);
38 PARA_NOTICE_LOG("%s: %s\n", path, PARA_STRERROR(-ret));
41 ret = score_add(aft_row, -playlist->length);
43 PARA_ERROR_LOG("failed to add %s: %d\n", path, ret);
50 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
51 static int load_playlist(struct osl_row *row, void *data)
53 struct playlist_info *playlist = data;
54 struct osl_object playlist_def;
58 ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
62 ret = for_each_line_ro(playlist_def.data, playlist_def.size,
63 add_playlist_entry, playlist);
64 osl_close_disk_object(&playlist_def);
67 ret = -E_PLAYLIST_EMPTY;
68 if (!playlist->length)
70 playlist->name = para_strdup(playlist_name);
71 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
73 return -E_PLAYLIST_LOADED;
75 if (ret != -E_DUMMY_ROW)
76 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
81 static int load_first_available_playlist(struct playlist_info *playlist)
83 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
85 if (ret == -E_PLAYLIST_LOADED) /* success */
88 return ret; /* error */
89 PARA_NOTICE_LOG("no valid playlist found\n");
90 return -E_NO_PLAYLIST;
93 static int check_playlist_path(char *path, void *data)
95 struct para_buffer *pb = data;
96 struct osl_row *aft_row;
97 int ret = aft_get_row_of_path(path, &aft_row);
100 para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
104 static int check_playlist(struct osl_row *row, void *data)
106 struct para_buffer *pb = data;
107 struct osl_object playlist_def;
109 int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
112 para_printf(pb, "failed to get playlist data: %s\n",
113 PARA_STRERROR(-ret));
116 if (*playlist_name) { /* skip dummy row */
117 para_printf(pb, "checking playlist %s...\n", playlist_name);
118 for_each_line_ro(playlist_def.data, playlist_def.size,
119 check_playlist_path, pb);
121 osl_close_disk_object(&playlist_def);
125 int playlist_check_callback(__a_unused const struct osl_object *query,
126 struct osl_object *result)
128 struct para_buffer pb = {.buf = NULL};
130 para_printf(&pb, "checking playlists...\n");
131 osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
133 result->data = pb.buf;
134 result->size = pb.size;
139 * Close the current playlist.
141 * \sa playlist_open().
143 void playlist_close(void)
145 free(current_playlist.name);
146 current_playlist.name = NULL;
150 * Open the given playlist.
152 * \param name The name of the playlist to open.
154 * If name is \p NULL, load the first available playlist. Files which are
155 * listed in the playlist, but not contained in the database are ignored.
156 * This is not considered an error.
158 * \return Positive on success, negative on errors. Possible errors
159 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
161 int playlist_open(char *name)
163 struct osl_object obj;
168 return load_first_available_playlist(¤t_playlist);
170 obj.size = strlen(obj.data);
171 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
173 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
176 return load_playlist(row, ¤t_playlist);