redraw the siplelist more often
[kugel-rb.git] / apps / playlist_catalog.c
blobf5c5bcb35ba1f6fa9c95466ae02999f455bd8d00
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"
40 #include "filetypes.h"
41 #include "debug.h"
43 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
44 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
45 #define MAX_PLAYLISTS 400
46 #define PLAYLIST_DISPLAY_COUNT 10
48 /* Use for recursive directory search */
49 struct add_track_context {
50 int fd;
51 int count;
54 /* keep track of most recently used playlist */
55 static char most_recent_playlist[MAX_PATH];
57 /* directory where our playlists our stored (configured in
58 PLAYLIST_CATALOG_CFG) */
59 static char playlist_dir[MAX_PATH];
60 static int playlist_dir_length;
61 static bool playlist_dir_exists = false;
63 /* Retrieve playlist directory from config file and verify it exists */
64 static int initialize_catalog(void)
66 static bool initialized = false;
68 if (!initialized)
70 int f;
71 DIR* dir;
72 bool default_dir = true;
74 f = open(PLAYLIST_CATALOG_CFG, O_RDONLY);
75 if (f >= 0)
77 char buf[MAX_PATH+5];
79 while (read_line(f, buf, sizeof(buf)))
81 char* name;
82 char* value;
84 /* directory config is of the format: "dir: /path/to/dir" */
85 if (settings_parseline(buf, &name, &value) &&
86 !strncasecmp(name, "dir:", strlen(name)) &&
87 strlen(value) > 0)
89 strncpy(playlist_dir, value, strlen(value));
90 default_dir = false;
94 close(f);
97 /* fall back to default directory if no or invalid config */
98 if (default_dir)
99 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
100 sizeof(playlist_dir));
102 playlist_dir_length = strlen(playlist_dir);
104 dir = opendir(playlist_dir);
105 if (dir)
107 playlist_dir_exists = true;
108 closedir(dir);
109 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
110 initialized = true;
114 if (!playlist_dir_exists)
116 if (mkdir(playlist_dir) < 0) {
117 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
118 playlist_dir);
119 return -1;
121 else {
122 playlist_dir_exists = true;
123 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
124 initialized = true;
128 return 0;
130 /* Use the filetree functions to retrieve the list of playlists in the
131 directory */
132 static int create_playlist_list(char** playlists, int num_items,
133 int* num_playlists)
135 int result = -1;
136 int num_files = 0;
137 int index = 0;
138 int i;
139 bool most_recent = false;
140 struct entry *files;
141 struct tree_context* tc = tree_get_context();
142 int dirfilter = *(tc->dirfilter);
144 *num_playlists = 0;
146 /* use the tree browser dircache to load only playlists */
147 *(tc->dirfilter) = SHOW_PLAYLIST;
149 if (ft_load(tc, playlist_dir) < 0)
151 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
152 playlist_dir);
153 goto exit;
156 files = (struct entry*) tc->dircache;
157 num_files = tc->filesindir;
159 /* we've overwritten the dircache so tree browser will need to be
160 reloaded */
161 reload_directory();
163 /* if it exists, most recent playlist will always be index 0 */
164 if (most_recent_playlist[0] != '\0')
166 index = 1;
167 most_recent = true;
170 for (i=0; i<num_files && index<num_items; i++)
172 if (files[i].attr & FILE_ATTR_M3U)
174 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
175 sizeof(most_recent_playlist)))
177 playlists[0] = files[i].name;
178 most_recent = false;
180 else
182 playlists[index] = files[i].name;
183 index++;
188 *num_playlists = index;
190 /* we couldn't find the most recent playlist, shift all playlists up */
191 if (most_recent)
193 for (i=0; i<index-1; i++)
194 playlists[i] = playlists[i+1];
196 (*num_playlists)--;
198 most_recent_playlist[0] = '\0';
201 result = 0;
203 exit:
204 *(tc->dirfilter) = dirfilter;
205 return result;
208 /* Callback for gui_synclist */
209 static char* playlist_callback_name(int selected_item, void* data,
210 char* buffer)
212 char** playlists = (char**) data;
214 strncpy(buffer, playlists[selected_item], MAX_PATH);
216 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
217 || (global_settings.show_filename_ext == 3
218 && global_settings.dirfilter == 0)))
220 char* dot = strrchr(buffer, '.');
222 if (dot != NULL)
224 *dot = '\0';
228 return buffer;
231 /* Display all playlists in catalog. Selected "playlist" is returned.
232 If "view" mode is set then we're not adding anything into playlist. */
233 static int display_playlists(char* playlist, bool view)
235 int result = -1;
236 int num_playlists = 0;
237 bool exit = false;
238 char temp_buf[MAX_PATH];
239 char* playlists[MAX_PLAYLISTS];
240 struct gui_synclist playlist_lists;
242 if (create_playlist_list(playlists, sizeof(playlists),
243 &num_playlists) != 0)
244 return -1;
246 if (num_playlists <= 0)
248 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
249 return -1;
252 if (!playlist)
253 playlist = temp_buf;
255 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
256 false, 1);
257 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
258 gui_synclist_draw(&playlist_lists);
260 while (!exit)
262 int button = get_action(CONTEXT_LIST,HZ/2);
263 char* sel_file;
265 gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
267 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
269 switch (button)
271 case ACTION_STD_CANCEL:
272 exit = true;
273 break;
275 case ACTION_STD_OK:
276 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
278 if (view)
280 /* In view mode, selecting a playlist starts playback */
281 ft_play_playlist(playlist, playlist_dir, sel_file);
284 result = 0;
285 exit = true;
286 break;
288 case ACTION_STD_CONTEXT:
289 /* context menu only available in view mode */
290 if (view)
292 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
293 sel_file);
295 if (onplay(playlist, FILE_ATTR_M3U,
296 CONTEXT_TREE) != ONPLAY_OK)
298 result = 0;
299 exit = true;
301 else
302 gui_synclist_draw(&playlist_lists);
304 break;
306 case ACTION_NONE:
307 gui_syncstatusbar_draw(&statusbars, false);
308 break;
310 default:
311 if(default_event_handler(button) == SYS_USB_CONNECTED)
313 result = -1;
314 exit = true;
316 break;
319 return result;
322 /* display number of tracks inserted into playlists. Used for directory
323 insert */
324 static void display_insert_count(int count)
326 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
327 str(LANG_OFF_ABORT));
330 /* Add specified track into playlist. Callback from directory insert */
331 static int add_track_to_playlist(char* filename, void* context)
333 struct add_track_context* c = (struct add_track_context*) context;
335 if (fdprintf(c->fd, "%s\n", filename) <= 0)
336 return -1;
338 (c->count)++;
340 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
341 display_insert_count(c->count);
343 return 0;
346 /* Add "sel" file into specified "playlist". How to insert depends on type
347 of file */
348 static int add_to_playlist(const char* playlist, char* sel, int sel_attr)
350 int fd;
351 int result = -1;
353 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
354 if(fd < 0)
355 return result;
357 /* In case we're in the playlist directory */
358 reload_directory();
360 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
362 /* append the selected file */
363 if (fdprintf(fd, "%s\n", sel) > 0)
364 result = 0;
366 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
368 /* append playlist */
369 int f, fs, i;
370 char buf[1024];
372 if(strcasecmp(playlist, sel) == 0)
373 goto exit;
375 f = open(sel, O_RDONLY);
376 if (f < 0)
377 goto exit;
379 fs = filesize(f);
381 for (i=0; i<fs;)
383 int n;
385 n = read(f, buf, sizeof(buf));
386 if (n < 0)
387 break;
389 if (write(fd, buf, n) < 0)
390 break;
392 i += n;
395 if (i >= fs)
396 result = 0;
398 close(f);
400 else if (sel_attr & ATTR_DIRECTORY)
402 /* search directory for tracks and append to playlist */
403 bool recurse = false;
404 char *lines[] = {
405 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION),
408 struct text_message message={lines, 2};
409 struct add_track_context context;
411 if (global_settings.recursive_dir_insert != RECURSE_ASK)
412 recurse = (bool)global_settings.recursive_dir_insert;
413 else
415 /* Ask if user wants to recurse directory */
416 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
419 context.fd = fd;
420 context.count = 0;
422 display_insert_count(0);
424 result = playlist_directory_tracksearch(sel, recurse,
425 add_track_to_playlist, &context);
427 display_insert_count(context.count);
430 exit:
431 close(fd);
432 return result;
435 bool catalog_view_playlists(void)
437 if (initialize_catalog() == -1)
438 return false;
440 if (display_playlists(NULL, true) == -1)
441 return false;
443 return true;
446 bool catalog_add_to_a_playlist(char* sel, int sel_attr, bool new_playlist)
448 char playlist[MAX_PATH];
450 if (initialize_catalog() == -1)
451 return false;
453 if (new_playlist)
455 size_t len;
456 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
457 if (kbd_input(playlist, MAX_PATH))
458 return false;
460 len = strlen(playlist);
462 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
463 strcat(playlist, "8");
464 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
465 strcat(playlist, ".m3u8");
467 else
469 if (display_playlists(playlist, false) == -1)
470 return false;
473 if (add_to_playlist(playlist, sel, sel_attr) == 0)
475 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
476 sizeof(most_recent_playlist));
477 return true;
479 else
480 return false;