7 /** \file playlist.c Functions for loading and saving playlists. */
9 /** Structure used for adding entries to a playlist. */
10 struct playlist_info
{
11 /** The name of the playlist. */
13 /** The number of entries currently in the playlist. */
16 static struct playlist_info current_playlist
;
19 * Re-insert an audio file into the tree of admissible files.
21 * \param aft_row Determines the audio file.
23 * \return The return value of score_update().
25 int playlist_update_audio_file(struct osl_row
*aft_row
)
27 /* always re-insert to the top of the tree */
28 return score_update(aft_row
, 0);
31 static int add_playlist_entry(char *path
, void *data
)
33 struct playlist_info
*playlist
= data
;
34 struct osl_row
*aft_row
;
35 int ret
= aft_get_row_of_path(path
, &aft_row
);
38 PARA_NOTICE_LOG("%s: %s\n", path
, PARA_STRERROR(-ret
));
41 ret
= score_add(aft_row
, -playlist
->length
);
43 PARA_ERROR_LOG("failed to add %s: %d\n", path
, ret
);
50 static int get_playlist_data(struct osl_row
*row
, char **playlist_name
,
51 struct osl_object
*playlist_def
)
53 struct osl_object obj
;
54 int ret
= osl_get_object(playlists_table
, row
, BLOBCOL_NAME
, &obj
);
57 *playlist_name
= obj
.data
;
58 return osl_open_disk_object(playlists_table
, row
, BLOBCOL_DEF
,
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
= get_playlist_data(row
, &playlist_name
, &playlist_def
);
74 ret
= for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
75 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 load_first_available_playlist(struct playlist_info
*playlist
)
95 int ret
= osl_rbtree_loop(playlists_table
, BLOBCOL_NAME
, playlist
,
97 if (ret
== -E_PLAYLIST_LOADED
) /* success */
100 return ret
; /* error */
101 PARA_NOTICE_LOG("no valid playlist found\n");
102 return -E_NO_PLAYLIST
;
105 static int check_playlist_path(char *path
, void *data
)
107 struct para_buffer
*pb
= data
;
108 struct osl_row
*aft_row
;
109 int ret
= aft_get_row_of_path(path
, &aft_row
);
112 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
116 static int check_playlist(struct osl_row
*row
, void *data
)
118 struct para_buffer
*pb
= data
;
119 struct osl_object playlist_def
;
121 int ret
= get_playlist_data(row
, &playlist_name
, &playlist_def
);
124 para_printf(pb
, "failed to get playlist data: %s\n",
125 PARA_STRERROR(-ret
));
128 if (*playlist_name
) { /* skip dummy row */
129 para_printf(pb
, "checking playlist %s...\n", playlist_name
);
130 for_each_line_ro(playlist_def
.data
, playlist_def
.size
,
131 check_playlist_path
, pb
);
133 osl_close_disk_object(&playlist_def
);
137 int playlist_check_callback(__a_unused
const struct osl_object
*query
,
138 struct osl_object
*result
)
140 struct para_buffer pb
= {.buf
= NULL
};
142 para_printf(&pb
, "checking playlists...\n");
143 osl_rbtree_loop(playlists_table
, BLOBCOL_ID
, &pb
,
145 result
->data
= pb
.buf
;
146 result
->size
= pb
.size
;
151 * Close the current playlist.
153 * \sa playlist_open().
155 void playlist_close(void)
157 free(current_playlist
.name
);
158 current_playlist
.name
= NULL
;
162 * Open the given playlist.
164 * \param name The name of the playlist to open.
166 * If name is \p NULL, load the first available playlist. Files which are
167 * listed in the playlist, but not contained in the database are ignored.
168 * This is not considered an error.
170 * \return Positive on success, negative on errors. Possible errors
171 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
173 int playlist_open(char *name
)
175 struct osl_object obj
;
180 return load_first_available_playlist(¤t_playlist
);
182 obj
.size
= strlen(obj
.data
);
183 ret
= osl_get_row(playlists_table
, BLOBCOL_NAME
, &obj
, &row
);
185 PARA_NOTICE_LOG("failed to load playlist %s\n", name
);
188 return load_playlist(row
, ¤t_playlist
);