2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
13 /** \file playlist.c Functions for loading and saving playlists. */
15 /** Structure used for adding entries to a playlist. */
16 struct playlist_info
{
17 /** The name of the playlist. */
19 /** The number of entries currently in the playlist. */
22 static struct playlist_info current_playlist
;
25 * Re-insert an audio file into the tree of admissible files.
27 * \param aft_row Determines the audio file.
29 * \return The return value of score_update().
31 int playlist_update_audio_file(struct osl_row
*aft_row
)
33 /* always re-insert to the top of the tree */
34 return score_update(aft_row
, 0);
37 static int add_playlist_entry(char *path
, void *data
)
39 struct playlist_info
*playlist
= data
;
40 struct osl_row
*aft_row
;
41 int ret
= aft_get_row_of_path(path
, &aft_row
);
44 PARA_NOTICE_LOG("%s: %s\n", path
, PARA_STRERROR(-ret
));
47 ret
= score_add(aft_row
, -playlist
->length
);
49 PARA_ERROR_LOG("failed to add %s: %s\n", path
, PARA_STRERROR(-ret
));
56 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
57 static int load_playlist(struct osl_row
*row
, void *data
)
59 struct playlist_info
*playlist
= data
;
60 struct osl_object playlist_def
;
64 ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
68 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
69 add_playlist_entry
, playlist
);
70 osl_close_disk_object(&playlist_def
);
73 ret
= -E_PLAYLIST_EMPTY
;
74 if (!playlist
->length
)
76 playlist
->name
= para_strdup(playlist_name
);
77 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist
->name
,
79 return -E_PLAYLIST_LOADED
;
81 if (ret
!= -E_DUMMY_ROW
)
82 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
87 static int load_first_available_playlist(struct playlist_info
*playlist
)
89 int ret
= osl_rbtree_loop(playlists_table
, BLOBCOL_NAME
, playlist
,
91 if (ret
== -E_PLAYLIST_LOADED
) /* success */
94 return ret
; /* error */
95 PARA_NOTICE_LOG("no valid playlist found\n");
96 return -E_NO_PLAYLIST
;
99 static int check_playlist_path(char *path
, void *data
)
101 struct para_buffer
*pb
= data
;
102 struct osl_row
*aft_row
;
103 int ret
= aft_get_row_of_path(path
, &aft_row
);
106 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
110 static int check_playlist(struct osl_row
*row
, void *data
)
112 struct para_buffer
*pb
= data
;
113 struct osl_object playlist_def
;
115 int ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
118 para_printf(pb
, "failed to get playlist data: %s\n",
119 PARA_STRERROR(-ret
));
122 if (*playlist_name
) { /* skip dummy row */
123 para_printf(pb
, "checking playlist %s...\n", playlist_name
);
124 for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
125 check_playlist_path
, pb
);
127 osl_close_disk_object(&playlist_def
);
132 * Check the playlist table for inconsistencies.
134 * \param query Unused.
135 * \param result Contains messages about inconsistencies.
137 * \return The return value of the underlying call to osl_rbtree_loop().
139 int playlist_check_callback(__a_unused
const struct osl_object
*query
,
140 struct osl_object
*result
)
142 struct para_buffer pb
= {.buf
= NULL
};
144 para_printf(&pb
, "checking playlists...\n");
145 osl_rbtree_loop(playlists_table
, BLOBCOL_ID
, &pb
,
147 result
->data
= pb
.buf
;
148 result
->size
= pb
.size
;
153 * Close the current playlist.
155 * \sa playlist_open().
157 void playlist_close(void)
159 free(current_playlist
.name
);
160 current_playlist
.name
= NULL
;
164 * Open the given playlist.
166 * \param name The name of the playlist to open.
168 * If name is \p NULL, load the first available playlist. Files which are
169 * listed in the playlist, but not contained in the database are ignored.
170 * This is not considered an error.
172 * \return Positive on success, negative on errors. Possible errors
173 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
175 int playlist_open(char *name
)
177 struct osl_object obj
;
182 return load_first_available_playlist(¤t_playlist
);
184 obj
.size
= strlen(obj
.data
);
185 ret
= osl_get_row(playlists_table
, BLOBCOL_NAME
, &obj
, &row
);
187 PARA_NOTICE_LOG("failed to load playlist %s\n", name
);
190 ret
= load_playlist(row
, ¤t_playlist
);
191 return (ret
== -E_PLAYLIST_LOADED
)? 1 : 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_ro(playlist_def
.data
, playlist_def
.size
,
223 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 */
239 int playlists_event_handler(enum afs_events event
, struct para_buffer
*pb
,
245 case AUDIO_FILE_RENAME
:
247 return handle_audio_file_event(event
, data
);
248 case AUDIO_FILE_REMOVE
:
249 ret
= row_belongs_to_score_table(data
, NULL
);
254 current_playlist
.length
--;
255 return score_delete(data
);