2 * Copyright (C) 2007-2014 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
18 /** \file playlist.c Functions for loading and saving playlists. */
20 /** Structure used for adding entries to a playlist. */
21 struct playlist_info {
22 /** The name of the playlist. */
24 /** The number of entries currently in the playlist. */
27 static struct playlist_info current_playlist;
30 * Re-insert an audio file into the tree of admissible files.
32 * \param aft_row Determines the audio file.
34 * \return The return value of score_update().
36 static int playlist_update_audio_file(const struct osl_row *aft_row)
38 /* always re-insert to the top of the tree */
39 return score_update(aft_row, 0);
42 static int add_playlist_entry(char *path, void *data)
44 struct playlist_info *playlist = data;
45 struct osl_row *aft_row;
46 int ret = aft_get_row_of_path(path, &aft_row);
49 PARA_NOTICE_LOG("%s: %s\n", path, para_strerror(-ret));
52 ret = score_add(aft_row, -playlist->length);
54 PARA_ERROR_LOG("failed to add %s: %s\n", path, para_strerror(-ret));
61 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
62 static int load_playlist(struct osl_row *row, void *data)
64 struct playlist_info *playlist = data;
65 struct osl_object playlist_def;
69 ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
73 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
74 playlist_def.size, add_playlist_entry, playlist);
75 osl_close_disk_object(&playlist_def);
78 ret = -E_PLAYLIST_EMPTY;
79 if (!playlist->length)
81 playlist->name = para_strdup(playlist_name);
82 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
84 return -E_PLAYLIST_LOADED;
86 if (ret != -E_DUMMY_ROW)
87 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
92 static int check_playlist_path(char *path, void *data)
94 struct para_buffer *pb = data;
95 struct osl_row *aft_row;
96 int ret = aft_get_row_of_path(path, &aft_row);
100 return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
103 static int check_playlist(struct osl_row *row, void *data)
105 struct para_buffer *pb = data;
106 struct osl_object playlist_def;
108 int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
111 return para_printf(pb, "failed to get playlist data: %s\n",
112 para_strerror(-ret));
113 if (*playlist_name) { /* skip dummy row */
114 ret = para_printf(pb, "checking playlist %s...\n", playlist_name);
117 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
118 playlist_def.size, check_playlist_path, pb);
120 osl_close_disk_object(&playlist_def);
125 * Check the playlist table for inconsistencies.
127 * \param fd The afs socket.
128 * \param query Unused.
130 * \return The return value of the underlying call to osl_rbtree_loop().
132 void playlist_check_callback(int fd, __a_unused const struct osl_object *query)
134 struct para_buffer pb = {
135 .max_size = shm_get_shmmax(),
136 .private_data = &(struct afs_max_size_handler_data) {
140 .max_size_handler = afs_max_size_handler,
142 int ret = para_printf(&pb, "checking playlists...\n");
146 osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
149 pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
154 * Close the current playlist.
156 * \sa playlist_open().
158 void playlist_close(void)
160 if (!current_playlist.name)
162 free(current_playlist.name);
163 current_playlist.name = NULL;
167 * Open the given playlist.
169 * \param name The name of the playlist to open.
171 * Files which are listed in the playlist, but not contained in the database
172 * are ignored. This is not considered an error.
176 int playlist_open(char *name)
178 struct osl_object obj;
183 obj.size = strlen(obj.data);
184 ret = osl(osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row));
186 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
190 ret = load_playlist(row, ¤t_playlist);
191 return (ret == -E_PLAYLIST_LOADED)? current_playlist.length : ret;
194 static int search_path(char *path, void *data)
196 if (strcmp(path, data))
198 return -E_PATH_FOUND;
201 static int handle_audio_file_event(enum afs_events event, void *data)
203 int ret, was_admissible = 0, is_admissible;
204 struct osl_object playlist_def;
206 const struct osl_row *row = data;
208 if (!current_playlist.name)
210 if (event == AUDIO_FILE_RENAME) {
211 ret = row_belongs_to_score_table(row, NULL);
214 was_admissible = ret;
216 ret = get_audio_file_path_of_row(row, &new_path);
219 ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
222 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
223 playlist_def.size, search_path, new_path);
224 osl_close_disk_object(&playlist_def);
225 is_admissible = (ret < 0);
226 if (was_admissible && is_admissible)
228 if (!was_admissible && !is_admissible)
230 if (was_admissible && !is_admissible) {
231 current_playlist.length--;
232 return score_delete(row);
234 /* !was_admissible && is_admissible */
235 current_playlist.length++;
236 return score_add(row, 0); /* play it immediately */
240 * Handle afs events relevant to playlists.
242 * \param event The event type.
244 * \param data Depends on the event type.
248 int playlists_event_handler(enum afs_events event,
249 __a_unused struct para_buffer *pb, void *data)
252 struct afsi_change_event_data *aced = data;
256 return playlist_update_audio_file(aced->aft_row);
257 case AUDIO_FILE_RENAME:
259 return handle_audio_file_event(event, data);
260 case AUDIO_FILE_REMOVE:
261 ret = row_belongs_to_score_table(data, NULL);
266 current_playlist.length--;
267 return score_delete(data);