2 * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
19 /** \file playlist.c Functions for loading and saving playlists. */
21 /** Structure used for adding entries to a playlist. */
22 struct playlist_info {
23 /** The name of the playlist. */
25 /** The number of entries currently in the playlist. */
28 static struct playlist_info current_playlist;
31 * Re-insert an audio file into the tree of admissible files.
33 * \param aft_row Determines the audio file.
35 * \return The return value of score_update().
37 static int playlist_update_audio_file(const struct osl_row *aft_row)
39 /* always re-insert to the top of the tree */
40 return score_update(aft_row, 0);
43 static int add_playlist_entry(char *path, void *data)
45 struct playlist_info *playlist = data;
46 struct osl_row *aft_row;
47 int ret = aft_get_row_of_path(path, &aft_row);
50 PARA_NOTICE_LOG("%s: %s\n", path, para_strerror(-ret));
53 ret = score_add(aft_row, -playlist->length);
55 PARA_ERROR_LOG("failed to add %s: %s\n", path, para_strerror(-ret));
62 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
63 static int load_playlist(struct osl_row *row, void *data)
65 struct playlist_info *playlist = data;
66 struct osl_object playlist_def;
70 ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
74 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
75 playlist_def.size, add_playlist_entry, playlist);
76 osl_close_disk_object(&playlist_def);
79 ret = -E_PLAYLIST_EMPTY;
80 if (!playlist->length)
82 playlist->name = para_strdup(playlist_name);
83 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
85 return -E_PLAYLIST_LOADED;
87 if (ret != -E_DUMMY_ROW)
88 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
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));
101 return 1; /* do not fail the loop on bad paths */
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);
111 if (ret < 0) { /* log error, but continue */
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(FELF_READ_ONLY, playlist_def.data,
119 playlist_def.size, check_playlist_path, pb);
121 osl_close_disk_object(&playlist_def);
126 * Check the playlist table for inconsistencies.
128 * \param aca This callback ignores ->query.
130 * \return Standard. Invalid paths are reported, but are not considered an
133 int playlist_check_callback(struct afs_callback_arg *aca)
135 para_printf(&aca->pbout, "checking playlists...\n");
136 return osl(osl_rbtree_loop(playlists_table, BLOBCOL_ID, &aca->pbout,
141 * Close the current playlist.
143 * \sa \ref playlist_open().
145 void playlist_close(void)
147 if (!current_playlist.name)
149 free(current_playlist.name);
150 current_playlist.name = NULL;
154 * Open the given playlist.
156 * \param name The name of the playlist to open.
158 * Files which are listed in the playlist, but not contained in the database
159 * are ignored. This is not considered an error.
163 int playlist_open(const char *name)
165 struct osl_object obj;
169 obj.data = (char *)name;
170 obj.size = strlen(obj.data);
171 ret = osl(osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row));
173 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
177 ret = load_playlist(row, ¤t_playlist);
178 return (ret == -E_PLAYLIST_LOADED)? current_playlist.length : ret;
181 static int search_path(char *path, void *data)
183 if (strcmp(path, data))
185 return -E_PATH_FOUND;
188 static int handle_audio_file_event(enum afs_events event, void *data)
190 int ret, was_admissible = 0, is_admissible;
191 struct osl_object playlist_def;
193 const struct osl_row *row = data;
195 if (event == AUDIO_FILE_RENAME) {
196 ret = row_belongs_to_score_table(row, NULL);
199 was_admissible = ret;
201 ret = get_audio_file_path_of_row(row, &new_path);
204 ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
207 ret = for_each_line(FELF_READ_ONLY, playlist_def.data,
208 playlist_def.size, search_path, new_path);
209 osl_close_disk_object(&playlist_def);
210 is_admissible = (ret < 0);
211 if (was_admissible && is_admissible)
213 if (!was_admissible && !is_admissible)
215 if (was_admissible && !is_admissible) {
216 current_playlist.length--;
217 return score_delete(row);
219 /* !was_admissible && is_admissible */
220 current_playlist.length++;
221 return score_add(row, 0); /* play it immediately */
225 * Handle afs events relevant to playlists.
227 * \param event The event type.
229 * \param data Depends on the event type.
233 int playlists_event_handler(enum afs_events event,
234 __a_unused struct para_buffer *pb, void *data)
237 struct afsi_change_event_data *aced = data;
239 if (!current_playlist.name)
243 return playlist_update_audio_file(aced->aft_row);
244 case AUDIO_FILE_RENAME:
246 return handle_audio_file_event(event, data);
247 case AUDIO_FILE_REMOVE:
248 ret = row_belongs_to_score_table(data, NULL);
253 current_playlist.length--;
254 return score_delete(data);