Fix the wavplay icon
[Rockbox.git] / apps / playlist_catalog.c
blob1e3d523181ca1a4ef0416ea226e8dd1a7f4bee17
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Sebastian Henriksen, Hardeep Sidhu
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "action.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "filetree.h"
28 #include "kernel.h"
29 #include "keyboard.h"
30 #include "lang.h"
31 #include "list.h"
32 #include "misc.h"
33 #include "onplay.h"
34 #include "playlist.h"
35 #include "settings.h"
36 #include "splash.h"
37 #include "sprintf.h"
38 #include "tree.h"
39 #include "yesno.h"
41 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
42 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
43 #define MAX_PLAYLISTS 400
44 #define PLAYLIST_DISPLAY_COUNT 10
46 /* Use for recursive directory search */
47 struct add_track_context {
48 int fd;
49 int count;
52 /* keep track of most recently used playlist */
53 static char most_recent_playlist[MAX_PATH];
55 /* directory where our playlists our stored (configured in
56 PLAYLIST_CATALOG_CFG) */
57 static char playlist_dir[MAX_PATH];
58 static int playlist_dir_length;
59 static bool playlist_dir_exists = false;
61 /* Retrieve playlist directory from config file and verify it exists */
62 static int initialize_catalog(void)
64 static bool initialized = false;
66 if (!initialized)
68 int f;
69 DIR* dir;
70 bool default_dir = true;
72 f = open(PLAYLIST_CATALOG_CFG, O_RDONLY);
73 if (f >= 0)
75 char buf[MAX_PATH+5];
77 while (read_line(f, buf, sizeof(buf)))
79 char* name;
80 char* value;
82 /* directory config is of the format: "dir: /path/to/dir" */
83 if (settings_parseline(buf, &name, &value) &&
84 !strncasecmp(name, "dir:", strlen(name)) &&
85 strlen(value) > 0)
87 strncpy(playlist_dir, value, strlen(value));
88 default_dir = false;
92 close(f);
95 /* fall back to default directory if no or invalid config */
96 if (default_dir)
97 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
98 sizeof(playlist_dir));
100 playlist_dir_length = strlen(playlist_dir);
102 dir = opendir(playlist_dir);
103 if (dir)
105 playlist_dir_exists = true;
106 closedir(dir);
107 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
108 initialized = true;
112 if (!playlist_dir_exists)
114 if (mkdir(playlist_dir) < 0) {
115 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
116 playlist_dir);
117 return -1;
119 else {
120 playlist_dir_exists = true;
121 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
122 initialized = true;
126 return 0;
128 /* Use the filetree functions to retrieve the list of playlists in the
129 directory */
130 static int create_playlist_list(char** playlists, int num_items,
131 int* num_playlists)
133 int result = -1;
134 int num_files = 0;
135 int index = 0;
136 int i;
137 bool most_recent = false;
138 struct entry *files;
139 struct tree_context* tc = tree_get_context();
140 int dirfilter = *(tc->dirfilter);
142 *num_playlists = 0;
144 /* use the tree browser dircache to load only playlists */
145 *(tc->dirfilter) = SHOW_PLAYLIST;
147 if (ft_load(tc, playlist_dir) < 0)
149 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
150 playlist_dir);
151 goto exit;
154 files = (struct entry*) tc->dircache;
155 num_files = tc->filesindir;
157 /* we've overwritten the dircache so tree browser will need to be
158 reloaded */
159 reload_directory();
161 /* if it exists, most recent playlist will always be index 0 */
162 if (most_recent_playlist[0] != '\0')
164 index = 1;
165 most_recent = true;
168 for (i=0; i<num_files && index<num_items; i++)
170 if (files[i].attr & TREE_ATTR_M3U)
172 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
173 sizeof(most_recent_playlist)))
175 playlists[0] = files[i].name;
176 most_recent = false;
178 else
180 playlists[index] = files[i].name;
181 index++;
186 *num_playlists = index;
188 /* we couldn't find the most recent playlist, shift all playlists up */
189 if (most_recent)
191 for (i=0; i<index-1; i++)
192 playlists[i] = playlists[i+1];
194 (*num_playlists)--;
196 most_recent_playlist[0] = '\0';
199 result = 0;
201 exit:
202 *(tc->dirfilter) = dirfilter;
203 return result;
206 /* Callback for gui_synclist */
207 static char* playlist_callback_name(int selected_item, void* data,
208 char* buffer)
210 char** playlists = (char**) data;
212 strncpy(buffer, playlists[selected_item], MAX_PATH);
214 return buffer;
217 /* Display all playlists in catalog. Selected "playlist" is returned.
218 If "view" mode is set then we're not adding anything into playlist. */
219 static int display_playlists(char* playlist, bool view)
221 int result = -1;
222 int num_playlists = 0;
223 bool exit = false;
224 char temp_buf[MAX_PATH];
225 char* playlists[MAX_PLAYLISTS];
226 struct gui_synclist playlist_lists;
228 if (create_playlist_list(playlists, sizeof(playlists),
229 &num_playlists) != 0)
230 return -1;
232 if (num_playlists <= 0)
234 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
235 return -1;
238 if (!playlist)
239 playlist = temp_buf;
241 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
242 false, 1);
243 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
244 gui_synclist_draw(&playlist_lists);
246 while (!exit)
248 int button = get_action(CONTEXT_LIST,HZ/2);
249 char* sel_file;
251 gui_synclist_do_button(&playlist_lists, button,LIST_WRAP_UNLESS_HELD);
253 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
255 switch (button)
257 case ACTION_STD_CANCEL:
258 exit = true;
259 break;
261 case ACTION_STD_OK:
262 if (view)
264 /* In view mode, selecting a playlist starts playback */
265 if (playlist_create(playlist_dir, sel_file) != -1)
267 if (global_settings.playlist_shuffle)
268 playlist_shuffle(current_tick, -1);
269 playlist_start(0, 0);
272 else
274 /* we found the playlist we want to add to */
275 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
276 sel_file);
279 result = 0;
280 exit = true;
281 break;
283 case ACTION_STD_CONTEXT:
284 /* context menu only available in view mode */
285 if (view)
287 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
288 sel_file);
290 if (onplay(playlist, TREE_ATTR_M3U,
291 CONTEXT_TREE) != ONPLAY_OK)
293 result = 0;
294 exit = true;
296 else
297 gui_synclist_draw(&playlist_lists);
299 break;
301 case ACTION_NONE:
302 gui_syncstatusbar_draw(&statusbars, false);
303 break;
305 default:
306 if(default_event_handler(button) == SYS_USB_CONNECTED)
308 result = -1;
309 exit = true;
311 break;
314 action_signalscreenchange();
315 return result;
318 /* display number of tracks inserted into playlists. Used for directory
319 insert */
320 static void display_insert_count(int count)
322 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
323 #if CONFIG_KEYPAD == PLAYER_PAD
324 str(LANG_STOP_ABORT)
325 #else
326 str(LANG_OFF_ABORT)
327 #endif
331 /* Add specified track into playlist. Callback from directory insert */
332 static int add_track_to_playlist(char* filename, void* context)
334 struct add_track_context* c = (struct add_track_context*) context;
336 if (fdprintf(c->fd, "%s\n", filename) <= 0)
337 return -1;
339 (c->count)++;
341 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
342 display_insert_count(c->count);
344 return 0;
347 /* Add "sel" file into specified "playlist". How to insert depends on type
348 of file */
349 static int add_to_playlist(const char* playlist, char* sel, int sel_attr)
351 int fd;
352 int result = -1;
354 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
355 if(fd < 0)
356 return result;
358 /* In case we're in the playlist directory */
359 reload_directory();
361 if ((sel_attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
363 /* append the selected file */
364 if (fdprintf(fd, "%s\n", sel) > 0)
365 result = 0;
367 else if ((sel_attr & TREE_ATTR_MASK) == TREE_ATTR_M3U)
369 /* append playlist */
370 int f, fs, i;
371 char buf[1024];
373 if(strcasecmp(playlist, sel) == 0)
374 goto exit;
376 f = open(sel, O_RDONLY);
377 if (f < 0)
378 goto exit;
380 fs = filesize(f);
382 for (i=0; i<fs;)
384 int n;
386 n = read(f, buf, sizeof(buf));
387 if (n < 0)
388 break;
390 if (write(fd, buf, n) < 0)
391 break;
393 i += n;
396 if (i >= fs)
397 result = 0;
399 close(f);
401 else if (sel_attr & ATTR_DIRECTORY)
403 /* search directory for tracks and append to playlist */
404 bool recurse = false;
405 char *lines[] = {
406 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION),
409 struct text_message message={lines, 2};
410 struct add_track_context context;
412 if (global_settings.recursive_dir_insert != RECURSE_ASK)
413 recurse = (bool)global_settings.recursive_dir_insert;
414 else
416 /* Ask if user wants to recurse directory */
417 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
420 context.fd = fd;
421 context.count = 0;
423 display_insert_count(0);
425 result = playlist_directory_tracksearch(sel, recurse,
426 add_track_to_playlist, &context);
428 display_insert_count(context.count);
431 exit:
432 close(fd);
433 return result;
436 bool catalog_view_playlists(void)
438 if (initialize_catalog() == -1)
439 return false;
441 if (display_playlists(NULL, true) == -1)
442 return false;
444 return true;
447 bool catalog_add_to_a_playlist(char* sel, int sel_attr, bool new_playlist)
449 char playlist[MAX_PATH];
451 if (initialize_catalog() == -1)
452 return false;
454 if (new_playlist)
456 size_t len;
457 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
458 if (kbd_input(playlist, MAX_PATH))
459 return false;
461 len = strlen(playlist);
463 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
464 strcat(playlist, "8");
465 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
466 strcat(playlist, ".m3u8");
468 else
470 if (display_playlists(playlist, false) == -1)
471 return false;
474 if (add_to_playlist(playlist, sel, sel_attr) == 0)
476 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
477 sizeof(most_recent_playlist));
478 return true;
480 else
481 return false;