Implement check for invalid playlist entries.
[paraslash.git] / playlist.c
1 #include "para.h"
2 #include "error.h"
3 #include "afh.h"
4 #include "afs.h"
5 #include "string.h"
6
7 /** \file playlist.c Functions for loading and saving playlists. */
8
9 /** Structure used for adding entries to a playlist. */
10 struct playlist_info {
11 /** The name of the playlist. */
12 char *name;
13 /** The number of entries currently in the playlist. */
14 unsigned length;
15 };
16 static struct playlist_info current_playlist;
17
18 /**
19 * Re-insert an audio file into the tree of admissible files.
20 *
21 * \param aft_row Determines the audio file.
22 *
23 * \return The return value of score_update().
24 */
25 int playlist_update_audio_file(struct osl_row *aft_row)
26 {
27 /* always re-insert to the top of the tree */
28 return score_update(aft_row, 0);
29 }
30
31 static int add_playlist_entry(char *path, void *data)
32 {
33 struct playlist_info *playlist = data;
34 struct osl_row *aft_row;
35 int ret = aft_get_row_of_path(path, &aft_row);
36
37 if (ret < 0) {
38 PARA_NOTICE_LOG("%s: %s\n", path, PARA_STRERROR(-ret));
39 return 1;
40 }
41 ret = score_add(aft_row, -playlist->length);
42 if (ret < 0) {
43 PARA_ERROR_LOG("failed to add %s: %d\n", path, ret);
44 return ret;
45 }
46 playlist->length++;
47 return 1;
48 }
49
50 static int get_playlist_data(struct osl_row *row, char **playlist_name,
51 struct osl_object *playlist_def)
52 {
53 struct osl_object obj;
54 int ret = osl_get_object(playlists_table, row, BLOBCOL_NAME, &obj);
55 if (ret < 0)
56 return ret;
57 *playlist_name = obj.data;
58 return osl_open_disk_object(playlists_table, row, BLOBCOL_DEF,
59 playlist_def);
60 }
61
62 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
63 static int load_playlist(struct osl_row *row, void *data)
64 {
65 struct playlist_info *playlist = data;
66 struct osl_object playlist_def;
67 char *playlist_name;
68 int ret;
69
70 ret = get_playlist_data(row, &playlist_name, &playlist_def);
71 if (ret < 0)
72 goto err;
73 playlist->length = 0;
74 ret = for_each_line_ro(playlist_def.data, playlist_def.size,
75 add_playlist_entry, playlist);
76 osl_close_disk_object(&playlist_def);
77 if (ret < 0)
78 goto err;
79 ret = -E_PLAYLIST_EMPTY;
80 if (!playlist->length)
81 goto err;
82 playlist->name = para_strdup(playlist_name);
83 PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
84 playlist->length);
85 return -E_PLAYLIST_LOADED;
86 err:
87 if (ret != -E_DUMMY_ROW)
88 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
89 PARA_STRERROR(-ret));
90 return 1;
91 }
92
93 static int load_first_available_playlist(struct playlist_info *playlist)
94 {
95 int ret = osl_rbtree_loop(playlists_table, BLOBCOL_NAME, playlist,
96 load_playlist);
97 if (ret == -E_PLAYLIST_LOADED) /* success */
98 return 1;
99 if (ret < 0)
100 return ret; /* error */
101 PARA_NOTICE_LOG("no valid playlist found\n");
102 return -E_NO_PLAYLIST;
103 }
104
105 static int check_playlist_path(char *path, void *data)
106 {
107 struct para_buffer *pb = data;
108 struct osl_row *aft_row;
109 int ret = aft_get_row_of_path(path, &aft_row);
110
111 if (ret < 0)
112 para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
113 return 1;
114 }
115
116 static int check_playlist(struct osl_row *row, void *data)
117 {
118 struct para_buffer *pb = data;
119 struct osl_object playlist_def;
120 char *playlist_name;
121 int ret = get_playlist_data(row, &playlist_name, &playlist_def);
122
123 if (ret < 0) {
124 para_printf(pb, "failed to get playlist data: %s\n",
125 PARA_STRERROR(-ret));
126 return 1;
127 }
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);
132 }
133 osl_close_disk_object(&playlist_def);
134 return 1;
135 }
136
137 int playlist_check_callback(__a_unused const struct osl_object *query,
138 struct osl_object *result)
139 {
140 struct para_buffer pb = {.buf = NULL};
141
142 para_printf(&pb, "checking playlists...\n");
143 osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
144 check_playlist);
145 result->data = pb.buf;
146 result->size = pb.size;
147 return 1;
148 }
149
150 /**
151 * Close the current playlist.
152 *
153 * \sa playlist_open().
154 */
155 void playlist_close(void)
156 {
157 free(current_playlist.name);
158 current_playlist.name = NULL;
159 }
160
161 /**
162 * Open the given playlist.
163 *
164 * \param name The name of the playlist to open.
165 *
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.
169 *
170 * \return Positive on success, negative on errors. Possible errors
171 * include: Given playlist not found, -E_NO_PLAYLIST (no playlist defined).
172 */
173 int playlist_open(char *name)
174 {
175 struct osl_object obj;
176 int ret;
177 struct osl_row *row;
178
179 if (!name)
180 return load_first_available_playlist(&current_playlist);
181 obj.data = name;
182 obj.size = strlen(obj.data);
183 ret = osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row);
184 if (ret < 0) {
185 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
186 return ret;
187 }
188 return load_playlist(row, &current_playlist);
189 }