bump version to m1.0.5 release
[Rockbox.git] / apps / playlist_catalog.c
blob9ac9e32e001ad55d74b999775624f26310d1603d
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 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 */
56 static char playlist_dir[MAX_PATH];
57 static int playlist_dir_length;
58 static bool playlist_dir_exists = false;
60 /* Retrieve playlist directory from config file and verify it exists */
61 static int initialize_catalog(void)
63 static bool initialized = false;
65 if (!initialized)
67 bool default_dir = true;
69 /* directory config is of the format: "dir: /path/to/dir" */
70 if (global_settings.playlist_catalog_dir[0])
72 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
73 default_dir = false;
76 /* fall back to default directory if no or invalid config */
77 if (default_dir)
78 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
79 sizeof(playlist_dir));
81 playlist_dir_length = strlen(playlist_dir);
83 if (dir_exists(playlist_dir))
85 playlist_dir_exists = true;
86 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
87 initialized = true;
91 if (!playlist_dir_exists)
93 if (mkdir(playlist_dir) < 0) {
94 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
95 playlist_dir);
96 return -1;
98 else {
99 playlist_dir_exists = true;
100 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
101 initialized = true;
105 return 0;
107 /* Use the filetree functions to retrieve the list of playlists in the
108 directory */
109 static int create_playlist_list(char** playlists, int num_items,
110 int* num_playlists)
112 int result = -1;
113 int num_files = 0;
114 int index = 0;
115 int i;
116 bool most_recent = false;
117 struct entry *files;
118 struct tree_context* tc = tree_get_context();
119 int dirfilter = *(tc->dirfilter);
121 *num_playlists = 0;
123 /* use the tree browser dircache to load only playlists */
124 *(tc->dirfilter) = SHOW_PLAYLIST;
126 if (ft_load(tc, playlist_dir) < 0)
128 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
129 playlist_dir);
130 goto exit;
133 files = (struct entry*) tc->dircache;
134 num_files = tc->filesindir;
136 /* we've overwritten the dircache so tree browser will need to be
137 reloaded */
138 reload_directory();
140 /* if it exists, most recent playlist will always be index 0 */
141 if (most_recent_playlist[0] != '\0')
143 index = 1;
144 most_recent = true;
147 for (i=0; i<num_files && index<num_items; i++)
149 if (files[i].attr & FILE_ATTR_M3U)
151 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
152 sizeof(most_recent_playlist)))
154 playlists[0] = files[i].name;
155 most_recent = false;
157 else
159 playlists[index] = files[i].name;
160 index++;
165 *num_playlists = index;
167 /* we couldn't find the most recent playlist, shift all playlists up */
168 if (most_recent)
170 for (i=0; i<index-1; i++)
171 playlists[i] = playlists[i+1];
173 (*num_playlists)--;
175 most_recent_playlist[0] = '\0';
178 result = 0;
180 exit:
181 *(tc->dirfilter) = dirfilter;
182 return result;
185 /* Callback for gui_synclist */
186 static char* playlist_callback_name(int selected_item, void* data,
187 char* buffer, size_t buffer_len)
189 char** playlists = (char**) data;
191 strncpy(buffer, playlists[selected_item], buffer_len);
193 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
194 || (global_settings.show_filename_ext == 3
195 && global_settings.dirfilter == 0)))
197 char* dot = strrchr(buffer, '.');
199 if (dot != NULL)
201 *dot = '\0';
205 return buffer;
208 /* Display all playlists in catalog. Selected "playlist" is returned.
209 If "view" mode is set then we're not adding anything into playlist. */
210 static int display_playlists(char* playlist, bool view)
212 int result = -1;
213 int num_playlists = 0;
214 bool exit = false;
215 char temp_buf[MAX_PATH];
216 char* playlists[MAX_PLAYLISTS];
217 struct gui_synclist playlist_lists;
219 if (create_playlist_list(playlists, sizeof(playlists),
220 &num_playlists) != 0)
221 return -1;
223 if (num_playlists <= 0)
225 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
226 return -1;
229 if (!playlist)
230 playlist = temp_buf;
232 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
233 false, 1, NULL);
234 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
235 gui_synclist_draw(&playlist_lists);
237 while (!exit)
239 int button = get_action(CONTEXT_LIST,HZ/2);
240 char* sel_file;
242 gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
244 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
246 switch (button)
248 case ACTION_STD_CANCEL:
249 exit = true;
250 break;
252 case ACTION_STD_OK:
253 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
255 if (view)
257 /* In view mode, selecting a playlist starts playback */
258 ft_play_playlist(playlist, playlist_dir, sel_file);
261 result = 0;
262 exit = true;
263 break;
265 case ACTION_STD_CONTEXT:
266 /* context menu only available in view mode */
267 if (view)
269 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
270 sel_file);
272 if (onplay(playlist, FILE_ATTR_M3U,
273 CONTEXT_TREE) != ONPLAY_OK)
275 result = 0;
276 exit = true;
278 else
279 gui_synclist_draw(&playlist_lists);
281 break;
283 case ACTION_NONE:
284 gui_syncstatusbar_draw(&statusbars, false);
285 break;
287 default:
288 if(default_event_handler(button) == SYS_USB_CONNECTED)
290 result = -1;
291 exit = true;
293 break;
296 return result;
299 /* display number of tracks inserted into playlists. Used for directory
300 insert */
301 static void display_insert_count(int count)
303 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
304 str(LANG_OFF_ABORT));
307 /* Add specified track into playlist. Callback from directory insert */
308 static int add_track_to_playlist(char* filename, void* context)
310 struct add_track_context* c = (struct add_track_context*) context;
312 if (fdprintf(c->fd, "%s\n", filename) <= 0)
313 return -1;
315 (c->count)++;
317 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
318 display_insert_count(c->count);
320 return 0;
323 /* Add "sel" file into specified "playlist". How to insert depends on type
324 of file */
325 static int add_to_playlist(const char* playlist, char* sel, int sel_attr)
327 int fd;
328 int result = -1;
330 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
331 if(fd < 0)
332 return result;
334 /* In case we're in the playlist directory */
335 reload_directory();
337 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
339 /* append the selected file */
340 if (fdprintf(fd, "%s\n", sel) > 0)
341 result = 0;
343 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
345 /* append playlist */
346 int f, fs, i;
347 char buf[1024];
349 if(strcasecmp(playlist, sel) == 0)
350 goto exit;
352 f = open(sel, O_RDONLY);
353 if (f < 0)
354 goto exit;
356 fs = filesize(f);
358 for (i=0; i<fs;)
360 int n;
362 n = read(f, buf, sizeof(buf));
363 if (n < 0)
364 break;
366 if (write(fd, buf, n) < 0)
367 break;
369 i += n;
372 if (i >= fs)
373 result = 0;
375 close(f);
377 else if (sel_attr & ATTR_DIRECTORY)
379 /* search directory for tracks and append to playlist */
380 bool recurse = false;
381 char *lines[] = {
382 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION),
385 struct text_message message={lines, 2};
386 struct add_track_context context;
388 if (global_settings.recursive_dir_insert != RECURSE_ASK)
389 recurse = (bool)global_settings.recursive_dir_insert;
390 else
392 /* Ask if user wants to recurse directory */
393 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
396 context.fd = fd;
397 context.count = 0;
399 display_insert_count(0);
401 result = playlist_directory_tracksearch(sel, recurse,
402 add_track_to_playlist, &context);
404 display_insert_count(context.count);
407 exit:
408 close(fd);
409 return result;
412 bool catalog_view_playlists(void)
414 if (initialize_catalog() == -1)
415 return false;
417 if (display_playlists(NULL, true) == -1)
418 return false;
420 return true;
423 bool catalog_add_to_a_playlist(char* sel, int sel_attr, bool new_playlist)
425 char playlist[MAX_PATH];
427 if (initialize_catalog() == -1)
428 return false;
430 if (new_playlist)
432 size_t len;
433 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
434 if (kbd_input(playlist, MAX_PATH))
435 return false;
437 len = strlen(playlist);
439 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
440 strcat(playlist, "8");
441 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
442 strcat(playlist, ".m3u8");
444 else
446 if (display_playlists(playlist, false) == -1)
447 return false;
450 if (add_to_playlist(playlist, sel, sel_attr) == 0)
452 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
453 sizeof(most_recent_playlist));
454 return true;
456 else
457 return false;