Add osl.h include to generated command C files.
[paraslash.git] / playlist.c
1 /*
2  * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 #include <osl.h>
8 #include "para.h"
9 #include "error.h"
10 #include "string.h"
11 #include "afh.h"
12 #include "afs.h"
13 #include "ipc.h"
14
15 /** \file playlist.c Functions for loading and saving playlists. */
16
17 /** Structure used for adding entries to a playlist. */
18 struct playlist_info {
19         /** The name of the playlist. */
20         char *name;
21         /** The number of entries currently in the playlist. */
22         unsigned length;
23 };
24 static struct playlist_info current_playlist;
25
26 /**
27  * Re-insert an audio file into the tree of admissible files.
28  *
29  * \param aft_row Determines the audio file.
30  *
31  * \return The return value of score_update().
32  */
33 static int playlist_update_audio_file(const struct osl_row *aft_row)
34 {
35         /* always re-insert to the top of the tree */
36         return score_update(aft_row, 0);
37 }
38
39 static int add_playlist_entry(char *path, void *data)
40 {
41         struct playlist_info *playlist = data;
42         struct osl_row *aft_row;
43         int ret = aft_get_row_of_path(path, &aft_row);
44
45         if (ret < 0) {
46                 PARA_NOTICE_LOG("%s: %s\n", path, para_strerror(-ret));
47                 return 1;
48         }
49         ret = score_add(aft_row, -playlist->length);
50         if (ret < 0) {
51                 PARA_ERROR_LOG("failed to add %s: %s\n", path, para_strerror(-ret));
52                 return ret;
53         }
54         playlist->length++;
55         return 1;
56 }
57
58 /* returns -E_PLAYLIST_LOADED on _success_ to terminate the loop */
59 static int load_playlist(struct osl_row *row, void *data)
60 {
61         struct playlist_info *playlist = data;
62         struct osl_object playlist_def;
63         char *playlist_name;
64         int ret;
65
66         ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
67         if (ret < 0)
68                 goto err;
69         playlist->length = 0;
70         ret = for_each_line_ro(playlist_def.data, playlist_def.size,
71                 add_playlist_entry, playlist);
72         osl_close_disk_object(&playlist_def);
73         if (ret < 0)
74                 goto err;
75         ret = -E_PLAYLIST_EMPTY;
76         if (!playlist->length)
77                 goto err;
78         playlist->name = para_strdup(playlist_name);
79         PARA_NOTICE_LOG("loaded playlist %s (%u files)\n", playlist->name,
80                 playlist->length);
81         return -E_PLAYLIST_LOADED;
82 err:
83         if (ret != -E_DUMMY_ROW)
84                 PARA_NOTICE_LOG("unable to load playlist (%s)\n",
85                         para_strerror(-ret));
86         return 1;
87 }
88
89 static int check_playlist_path(char *path, void *data)
90 {
91         struct para_buffer *pb = data;
92         struct osl_row *aft_row;
93         int ret = aft_get_row_of_path(path, &aft_row);
94
95         if (ret >= 0)
96                 return 1;
97         return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
98 }
99
100 static int check_playlist(struct osl_row *row, void *data)
101 {
102         struct para_buffer *pb = data;
103         struct osl_object playlist_def;
104         char *playlist_name;
105         int ret = pl_get_name_and_def_by_row(row, &playlist_name, &playlist_def);
106
107         if (ret < 0)
108                 return para_printf(pb, "failed to get playlist data: %s\n",
109                         para_strerror(-ret));
110         if (*playlist_name) { /* skip dummy row */
111                 ret = para_printf(pb, "checking playlist %s...\n", playlist_name);
112                 if (ret < 0)
113                         return ret;
114                 ret = for_each_line_ro(playlist_def.data, playlist_def.size,
115                         check_playlist_path, pb);
116         }
117         osl_close_disk_object(&playlist_def);
118         return ret;
119 }
120
121 /**
122  * Check the playlist table for inconsistencies.
123  *
124  * \param fd The afs socket.
125  * \param query Unused.
126  *
127  * \return The return value of the underlying call to osl_rbtree_loop().
128  */
129 void playlist_check_callback(int fd, __a_unused const struct osl_object *query)
130 {
131         struct para_buffer pb = {
132                 .max_size = SHMMAX,
133                 .private_data = &fd,
134                 .max_size_handler = pass_buffer_as_shm
135         };
136         int ret = para_printf(&pb, "checking playlists...\n");
137
138         if (ret < 0)
139                 return;
140         osl_rbtree_loop(playlists_table, BLOBCOL_ID, &pb,
141                 check_playlist);
142         if (pb.offset)
143                 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
144         free(pb.buf);
145 }
146
147 /**
148  * Close the current playlist.
149  *
150  * \sa playlist_open().
151  */
152 void playlist_close(void)
153 {
154         if (!current_playlist.name)
155                 return;
156         free(current_playlist.name);
157         current_playlist.name = NULL;
158 }
159
160 /**
161  * Open the given playlist.
162  *
163  * \param name The name of the playlist to open.
164  *
165  * Files which are listed in the playlist, but not contained in the database
166  * are ignored.  This is not considered an error.
167  *
168  * \return Standard.
169  */
170 int playlist_open(char *name)
171 {
172         struct osl_object obj;
173         int ret;
174         struct osl_row *row;
175
176         obj.data = name;
177         obj.size = strlen(obj.data);
178         ret = osl(osl_get_row(playlists_table, BLOBCOL_NAME, &obj, &row));
179         if (ret < 0) {
180                 PARA_NOTICE_LOG("failed to load playlist %s\n", name);
181                 return ret;
182         }
183         playlist_close();
184         ret = load_playlist(row, &current_playlist);
185         return (ret == -E_PLAYLIST_LOADED)? current_playlist.length : ret;
186 }
187
188 static int search_path(char *path, void *data)
189 {
190         if (strcmp(path, data))
191                 return 1;
192         return -E_PATH_FOUND;
193 }
194
195 static int handle_audio_file_event(enum afs_events event, void *data)
196 {
197         int ret, was_admissible = 0, is_admissible;
198         struct osl_object playlist_def;
199         char *new_path;
200         const struct osl_row *row = data;
201
202         if (!current_playlist.name)
203                 return 1;
204         if (event == AUDIO_FILE_RENAME) {
205                 ret = row_belongs_to_score_table(row, NULL);
206                 if (ret < 0)
207                         return ret;
208                 was_admissible = ret;
209         }
210         ret = get_audio_file_path_of_row(row, &new_path);
211         if (ret < 0)
212                 return ret;
213         ret = pl_get_def_by_name(current_playlist.name, &playlist_def);
214         if (ret < 0)
215                 return ret;
216         ret = for_each_line_ro(playlist_def.data, playlist_def.size,
217                 search_path, new_path);
218         osl_close_disk_object(&playlist_def);
219         is_admissible = (ret < 0);
220         if (was_admissible && is_admissible)
221                 return 1;
222         if (!was_admissible && !is_admissible)
223                 return 1;
224         if (was_admissible && !is_admissible) {
225                 current_playlist.length--;
226                 return score_delete(row);
227         }
228         /* !was_admissible && is_admissible */
229         current_playlist.length++;
230         return score_add(row, 0); /* play it immediately */
231 }
232
233 /**
234  * Handle afs events relevant to playlists.
235  *
236  * \param event The event type.
237  * \param pb Unused.
238  * \param data Depends on the event type.
239  *
240  * \return Standard.
241  */
242 int playlists_event_handler(enum afs_events event,
243         __a_unused struct para_buffer *pb, void *data)
244 {
245         int ret;
246         struct afsi_change_event_data *aced = data;
247
248         switch(event) {
249         case AFSI_CHANGE:
250                 return playlist_update_audio_file(aced->aft_row);
251         case AUDIO_FILE_RENAME:
252         case AUDIO_FILE_ADD:
253                 return handle_audio_file_event(event, data);
254         case AUDIO_FILE_REMOVE:
255                 ret = row_belongs_to_score_table(data, NULL);
256                 if (ret < 0)
257                         return ret;
258                 if (!ret)
259                         return 1;
260                 current_playlist.length--;
261                 return score_delete(data);
262         default:
263                 return 1;
264         }
265 }