2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
17 /** \file playlist.c Functions for loading and saving playlists. */
19 /** Structure used for adding entries to a playlist. */
20 struct playlist_info
{
21 /** The name of the playlist. */
23 /** The number of entries currently in the playlist. */
26 static struct playlist_info current_playlist
;
29 * Re-insert an audio file into the tree of admissible files.
31 * \param aft_row Determines the audio file.
33 * \return The return value of score_update().
35 static int playlist_update_audio_file(const struct osl_row
*aft_row
)
37 /* always re-insert to the top of the tree */
38 return score_update(aft_row
, 0);
41 static int add_playlist_entry(char *path
, void *data
)
43 struct playlist_info
*playlist
= data
;
44 struct osl_row
*aft_row
;
45 int ret
= aft_get_row_of_path(path
, &aft_row
);
48 PARA_NOTICE_LOG("%s: %s\n", path
, para_strerror(-ret
));
51 ret
= score_add(aft_row
, -playlist
->length
);
53 PARA_ERROR_LOG("failed to add %s: %s\n", path
, para_strerror(-ret
));
60 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
61 static int load_playlist(struct osl_row
*row
, void *data
)
63 struct playlist_info
*playlist
= data
;
64 struct osl_object playlist_def
;
68 ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
72 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
73 add_playlist_entry
, playlist
);
74 osl_close_disk_object(&playlist_def
);
77 ret
= -E_PLAYLIST_EMPTY
;
78 if (!playlist
->length
)
80 playlist
->name
= para_strdup(playlist_name
);
81 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist
->name
,
83 return -E_PLAYLIST_LOADED
;
85 if (ret
!= -E_DUMMY_ROW
)
86 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
91 static int check_playlist_path(char *path
, void *data
)
93 struct para_buffer
*pb
= data
;
94 struct osl_row
*aft_row
;
95 int ret
= aft_get_row_of_path(path
, &aft_row
);
99 return para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
102 static int check_playlist(struct osl_row
*row
, void *data
)
104 struct para_buffer
*pb
= data
;
105 struct osl_object playlist_def
;
107 int ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
110 return para_printf(pb
, "failed to get playlist data: %s\n",
111 para_strerror(-ret
));
112 if (*playlist_name
) { /* skip dummy row */
113 ret
= para_printf(pb
, "checking playlist %s...\n", playlist_name
);
116 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
117 check_playlist_path
, pb
);
119 osl_close_disk_object(&playlist_def
);
124 * Check the playlist table for inconsistencies.
126 * \param fd The afs socket.
127 * \param query Unused.
129 * \return The return value of the underlying call to osl_rbtree_loop().
131 void playlist_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
133 struct para_buffer pb
= {
136 .max_size_handler
= pass_buffer_as_shm
138 int ret
= para_printf(&pb
, "checking playlists...\n");
142 osl_rbtree_loop(playlists_table
, BLOBCOL_ID
, &pb
,
145 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
150 * Close the current playlist.
152 * \sa playlist_open().
154 void playlist_close(void)
156 if (!current_playlist
.name
)
158 free(current_playlist
.name
);
159 current_playlist
.name
= NULL
;
163 * Open the given playlist.
165 * \param name The name of the playlist to open.
167 * Files which are listed in the playlist, but not contained in the database
168 * are ignored. This is not considered an error.
172 int playlist_open(char *name
)
174 struct osl_object obj
;
179 obj
.size
= strlen(obj
.data
);
180 ret
= osl(osl_get_row(playlists_table
, BLOBCOL_NAME
, &obj
, &row
));
182 PARA_NOTICE_LOG("failed to load playlist %s\n", name
);
186 ret
= load_playlist(row
, ¤t_playlist
);
187 return (ret
== -E_PLAYLIST_LOADED
)? current_playlist
.length
: ret
;
190 static int search_path(char *path
, void *data
)
192 if (strcmp(path
, data
))
194 return -E_PATH_FOUND
;
197 static int handle_audio_file_event(enum afs_events event
, void *data
)
199 int ret
, was_admissible
= 0, is_admissible
;
200 struct osl_object playlist_def
;
202 const struct osl_row
*row
= data
;
204 if (!current_playlist
.name
)
206 if (event
== AUDIO_FILE_RENAME
) {
207 ret
= row_belongs_to_score_table(row
, NULL
);
210 was_admissible
= ret
;
212 ret
= get_audio_file_path_of_row(row
, &new_path
);
215 ret
= pl_get_def_by_name(current_playlist
.name
, &playlist_def
);
218 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
219 search_path
, new_path
);
220 osl_close_disk_object(&playlist_def
);
221 is_admissible
= (ret
< 0);
222 if (was_admissible
&& is_admissible
)
224 if (!was_admissible
&& !is_admissible
)
226 if (was_admissible
&& !is_admissible
) {
227 current_playlist
.length
--;
228 return score_delete(row
);
230 /* !was_admissible && is_admissible */
231 current_playlist
.length
++;
232 return score_add(row
, 0); /* play it immediately */
236 * Handle afs events relevant to playlists.
238 * \param event The event type.
240 * \param data Depends on the event type.
244 int playlists_event_handler(enum afs_events event
,
245 __a_unused
struct para_buffer
*pb
, void *data
)
248 struct afsi_change_event_data
*aced
= data
;
252 return playlist_update_audio_file(aced
->aft_row
);
253 case AUDIO_FILE_RENAME
:
255 return handle_audio_file_event(event
, data
);
256 case AUDIO_FILE_REMOVE
:
257 ret
= row_belongs_to_score_table(data
, NULL
);
262 current_playlist
.length
--;
263 return score_delete(data
);