2 * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
14 /** \file playlist.c Functions for loading and saving playlists. */
16 /** Structure used for adding entries to a playlist. */
17 struct playlist_info
{
18 /** The name of the playlist. */
20 /** The number of entries currently in the playlist. */
23 static struct playlist_info current_playlist
;
26 * Re-insert an audio file into the tree of admissible files.
28 * \param aft_row Determines the audio file.
30 * \return The return value of score_update().
32 static int playlist_update_audio_file(const struct osl_row
*aft_row
)
34 /* always re-insert to the top of the tree */
35 return score_update(aft_row
, 0);
38 static int add_playlist_entry(char *path
, void *data
)
40 struct playlist_info
*playlist
= data
;
41 struct osl_row
*aft_row
;
42 int ret
= aft_get_row_of_path(path
, &aft_row
);
45 PARA_NOTICE_LOG("%s: %s\n", path
, para_strerror(-ret
));
48 ret
= score_add(aft_row
, -playlist
->length
);
50 PARA_ERROR_LOG("failed to add %s: %s\n", path
, para_strerror(-ret
));
57 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
58 static int load_playlist(struct osl_row
*row
, void *data
)
60 struct playlist_info
*playlist
= data
;
61 struct osl_object playlist_def
;
65 ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
69 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
70 add_playlist_entry
, playlist
);
71 osl_close_disk_object(&playlist_def
);
74 ret
= -E_PLAYLIST_EMPTY
;
75 if (!playlist
->length
)
77 playlist
->name
= para_strdup(playlist_name
);
78 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist
->name
,
80 return -E_PLAYLIST_LOADED
;
82 if (ret
!= -E_DUMMY_ROW
)
83 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
88 static int check_playlist_path(char *path
, void *data
)
90 struct para_buffer
*pb
= data
;
91 struct osl_row
*aft_row
;
92 int ret
= aft_get_row_of_path(path
, &aft_row
);
96 return para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
99 static int check_playlist(struct osl_row
*row
, void *data
)
101 struct para_buffer
*pb
= data
;
102 struct osl_object playlist_def
;
104 int ret
= pl_get_name_and_def_by_row(row
, &playlist_name
, &playlist_def
);
107 return para_printf(pb
, "failed to get playlist data: %s\n",
108 para_strerror(-ret
));
109 if (*playlist_name
) { /* skip dummy row */
110 ret
= para_printf(pb
, "checking playlist %s...\n", playlist_name
);
113 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
114 check_playlist_path
, pb
);
116 osl_close_disk_object(&playlist_def
);
121 * Check the playlist table for inconsistencies.
123 * \param query Unused.
124 * \param result Contains messages about inconsistencies.
126 * \return The return value of the underlying call to osl_rbtree_loop().
128 void playlist_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
130 struct para_buffer pb
= {
133 .max_size_handler
= pass_buffer_as_shm
135 int ret
= para_printf(&pb
, "checking playlists...\n");
139 osl_rbtree_loop(playlists_table
, BLOBCOL_ID
, &pb
,
142 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
147 * Close the current playlist.
149 * \sa playlist_open().
151 void playlist_close(void)
153 if (!current_playlist
.name
)
155 free(current_playlist
.name
);
156 current_playlist
.name
= NULL
;
160 * Open the given playlist.
162 * \param name The name of the playlist to open.
164 * Files which are listed in the playlist, but not contained in the database
165 * are ignored. This is not considered an error.
169 int playlist_open(char *name
)
171 struct osl_object obj
;
176 obj
.size
= strlen(obj
.data
);
177 ret
= osl_get_row(playlists_table
, BLOBCOL_NAME
, &obj
, &row
);
179 PARA_NOTICE_LOG("failed to load playlist %s\n", name
);
183 ret
= load_playlist(row
, ¤t_playlist
);
184 return (ret
== -E_PLAYLIST_LOADED
)? current_playlist
.length
: ret
;
187 static int search_path(char *path
, void *data
)
189 if (strcmp(path
, data
))
191 return -E_PATH_FOUND
;
194 static int handle_audio_file_event(enum afs_events event
, void *data
)
196 int ret
, was_admissible
= 0, is_admissible
;
197 struct osl_object playlist_def
;
199 const struct osl_row
*row
= data
;
201 if (!current_playlist
.name
)
203 if (event
== AUDIO_FILE_RENAME
) {
204 ret
= row_belongs_to_score_table(row
, NULL
);
207 was_admissible
= ret
;
209 ret
= get_audio_file_path_of_row(row
, &new_path
);
212 ret
= pl_get_def_by_name(current_playlist
.name
, &playlist_def
);
215 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
216 search_path
, new_path
);
217 osl_close_disk_object(&playlist_def
);
218 is_admissible
= (ret
< 0);
219 if (was_admissible
&& is_admissible
)
221 if (!was_admissible
&& !is_admissible
)
223 if (was_admissible
&& !is_admissible
) {
224 current_playlist
.length
--;
225 return score_delete(row
);
227 /* !was_admissible && is_admissible */
228 current_playlist
.length
++;
229 return score_add(row
, 0); /* play it immediately */
233 * Handle afs events relevant to playlists.
235 * \param event The event type.
237 * \param data Depends on the event type.
241 int playlists_event_handler(enum afs_events event
,
242 __a_unused
struct para_buffer
*pb
, void *data
)
245 struct afsi_change_event_data
*aced
= data
;
249 return playlist_update_audio_file(aced
->aft_row
);
250 case AUDIO_FILE_RENAME
:
252 return handle_audio_file_event(event
, data
);
253 case AUDIO_FILE_REMOVE
:
254 ret
= row_belongs_to_score_table(data
, NULL
);
259 current_playlist
.length
--;
260 return score_delete(data
);