Fix sansa c200 battery type comment.
[kugel-rb.git] / apps / playlist_catalog.c
blobcadad9f46421c737dad8f8fd560e945e2a9d9030
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Sebastian Henriksen, Hardeep Sidhu
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "action.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "filetree.h"
30 #include "kernel.h"
31 #include "keyboard.h"
32 #include "lang.h"
33 #include "list.h"
34 #include "misc.h"
35 #include "onplay.h"
36 #include "playlist.h"
37 #include "settings.h"
38 #include "splash.h"
39 #include "sprintf.h"
40 #include "tree.h"
41 #include "yesno.h"
42 #include "filetypes.h"
43 #include "debug.h"
44 #include "playlist_catalog.h"
45 #include "statusbar.h"
46 #include "talk.h"
48 #define MAX_PLAYLISTS 400
49 #define PLAYLIST_DISPLAY_COUNT 10
51 /* Use for recursive directory search */
52 struct add_track_context {
53 int fd;
54 int count;
57 /* keep track of most recently used playlist */
58 static char most_recent_playlist[MAX_PATH];
60 /* directory where our playlists our stored */
61 static char playlist_dir[MAX_PATH];
62 static int playlist_dir_length;
63 static bool playlist_dir_exists = false;
65 /* Retrieve playlist directory from config file and verify it exists */
66 static int initialize_catalog(void)
68 static bool initialized = false;
70 if (!initialized)
72 bool default_dir = true;
74 /* directory config is of the format: "dir: /path/to/dir" */
75 if (global_settings.playlist_catalog_dir[0])
77 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
78 default_dir = false;
81 /* fall back to default directory if no or invalid config */
82 if (default_dir)
83 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
84 sizeof(playlist_dir));
86 playlist_dir_length = strlen(playlist_dir);
88 if (dir_exists(playlist_dir))
90 playlist_dir_exists = true;
91 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
92 initialized = true;
96 if (!playlist_dir_exists)
98 if (mkdir(playlist_dir) < 0) {
99 gui_syncsplash(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY),
100 playlist_dir);
101 return -1;
103 else {
104 playlist_dir_exists = true;
105 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
106 initialized = true;
110 return 0;
112 /* Use the filetree functions to retrieve the list of playlists in the
113 directory */
114 static int create_playlist_list(char** playlists, int num_items,
115 int* num_playlists)
117 int result = -1;
118 int num_files = 0;
119 int index = 0;
120 int i;
121 bool most_recent = false;
122 struct entry *files;
123 struct tree_context* tc = tree_get_context();
124 int dirfilter = *(tc->dirfilter);
126 *num_playlists = 0;
128 /* use the tree browser dircache to load only playlists */
129 *(tc->dirfilter) = SHOW_PLAYLIST;
131 if (ft_load(tc, playlist_dir) < 0)
133 gui_syncsplash(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY),
134 playlist_dir);
135 goto exit;
138 files = (struct entry*) tc->dircache;
139 num_files = tc->filesindir;
141 /* we've overwritten the dircache so tree browser will need to be
142 reloaded */
143 reload_directory();
145 /* if it exists, most recent playlist will always be index 0 */
146 if (most_recent_playlist[0] != '\0')
148 index = 1;
149 most_recent = true;
152 for (i=0; i<num_files && index<num_items; i++)
154 if (files[i].attr & FILE_ATTR_M3U)
156 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
157 sizeof(most_recent_playlist)))
159 playlists[0] = files[i].name;
160 most_recent = false;
162 else
164 playlists[index] = files[i].name;
165 index++;
170 *num_playlists = index;
172 /* we couldn't find the most recent playlist, shift all playlists up */
173 if (most_recent)
175 for (i=0; i<index-1; i++)
176 playlists[i] = playlists[i+1];
178 (*num_playlists)--;
180 most_recent_playlist[0] = '\0';
183 result = 0;
185 exit:
186 *(tc->dirfilter) = dirfilter;
187 return result;
190 /* Callback for gui_synclist */
191 static char* playlist_callback_name(int selected_item, void* data,
192 char* buffer, size_t buffer_len)
194 char** playlists = (char**) data;
196 strncpy(buffer, playlists[selected_item], buffer_len);
198 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
199 || (global_settings.show_filename_ext == 3
200 && global_settings.dirfilter == 0)))
202 char* dot = strrchr(buffer, '.');
204 if (dot != NULL)
206 *dot = '\0';
210 return buffer;
213 static int playlist_callback_voice(int selected_item, void* data)
215 char** playlists = (char**) data;
216 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
217 return 0;
220 /* Display all playlists in catalog. Selected "playlist" is returned.
221 If "view" mode is set then we're not adding anything into playlist. */
222 static int display_playlists(char* playlist, bool view)
224 int result = -1;
225 int num_playlists = 0;
226 bool exit = false;
227 char temp_buf[MAX_PATH];
228 char* playlists[MAX_PLAYLISTS];
229 struct gui_synclist playlist_lists;
231 if (create_playlist_list(playlists, sizeof(playlists),
232 &num_playlists) != 0)
233 return -1;
235 if (num_playlists <= 0)
237 gui_syncsplash(HZ*2, ID2P(LANG_CATALOG_NO_PLAYLISTS));
238 return -1;
241 if (!playlist)
242 playlist = temp_buf;
244 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
245 false, 1, NULL);
246 if(global_settings.talk_menu)
247 gui_synclist_set_voice_callback(&playlist_lists,
248 playlist_callback_voice);
249 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
250 gui_synclist_draw(&playlist_lists);
251 gui_synclist_speak_item(&playlist_lists);
253 while (!exit)
255 int button;
256 char* sel_file;
257 list_do_action(CONTEXT_LIST,HZ/2,
258 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
259 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
261 switch (button)
263 case ACTION_STD_CANCEL:
264 exit = true;
265 break;
267 case ACTION_STD_OK:
268 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
270 if (view)
272 /* In view mode, selecting a playlist starts playback */
273 ft_play_playlist(playlist, playlist_dir, sel_file);
276 result = 0;
277 exit = true;
278 break;
280 case ACTION_STD_CONTEXT:
281 /* context menu only available in view mode */
282 if (view)
284 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
285 sel_file);
287 if (onplay(playlist, FILE_ATTR_M3U,
288 CONTEXT_TREE) != ONPLAY_OK)
290 result = 0;
291 exit = true;
293 else
295 gui_synclist_draw(&playlist_lists);
296 gui_synclist_speak_item(&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 return result;
317 /* display number of tracks inserted into playlists. Used for directory
318 insert */
319 static void display_insert_count(int count)
321 static long talked_tick = 0;
322 if(global_settings.talk_menu && count &&
323 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
325 talked_tick = current_tick;
326 talk_number(count, false);
327 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
330 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
331 str(LANG_OFF_ABORT));
334 /* Add specified track into playlist. Callback from directory insert */
335 static int add_track_to_playlist(char* filename, void* context)
337 struct add_track_context* c = (struct add_track_context*) context;
339 if (fdprintf(c->fd, "%s\n", filename) <= 0)
340 return -1;
342 (c->count)++;
344 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
345 display_insert_count(c->count);
347 return 0;
350 /* Add "sel" file into specified "playlist". How to insert depends on type
351 of file */
352 static int add_to_playlist(const char* playlist, bool new_playlist,
353 const char* sel, int sel_attr)
355 int fd;
356 int result = -1;
357 int flags = O_CREAT|O_WRONLY;
359 if (new_playlist)
360 flags |= O_TRUNC;
361 else
362 flags |= O_APPEND;
364 fd = open(playlist, flags);
365 if(fd < 0)
366 return result;
368 /* In case we're in the playlist directory */
369 reload_directory();
371 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
373 /* append the selected file */
374 if (fdprintf(fd, "%s\n", sel) > 0)
375 result = 0;
377 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
379 /* append playlist */
380 int f, fs, i;
381 char buf[1024];
383 if(strcasecmp(playlist, sel) == 0)
384 goto exit;
386 f = open(sel, O_RDONLY);
387 if (f < 0)
388 goto exit;
390 fs = filesize(f);
392 for (i=0; i<fs;)
394 int n;
396 n = read(f, buf, sizeof(buf));
397 if (n < 0)
398 break;
400 if (write(fd, buf, n) < 0)
401 break;
403 i += n;
406 if (i >= fs)
407 result = 0;
409 close(f);
411 else if (sel_attr & ATTR_DIRECTORY)
413 /* search directory for tracks and append to playlist */
414 bool recurse = false;
415 const char *lines[] = {
416 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
417 const struct text_message message={lines, 2};
418 struct add_track_context context;
420 if (global_settings.recursive_dir_insert != RECURSE_ASK)
421 recurse = (bool)global_settings.recursive_dir_insert;
422 else
424 /* Ask if user wants to recurse directory */
425 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
428 context.fd = fd;
429 context.count = 0;
431 display_insert_count(0);
433 result = playlist_directory_tracksearch(sel, recurse,
434 add_track_to_playlist, &context);
436 display_insert_count(context.count);
439 exit:
440 close(fd);
441 return result;
444 bool catalog_view_playlists(void)
446 if (initialize_catalog() == -1)
447 return false;
449 if (display_playlists(NULL, true) == -1)
450 return false;
452 return true;
455 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
456 bool new_playlist, char *m3u8name)
458 char playlist[MAX_PATH];
460 if (initialize_catalog() == -1)
461 return false;
463 if (new_playlist)
465 size_t len;
466 if (m3u8name == NULL)
468 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
469 if (kbd_input(playlist, MAX_PATH))
470 return false;
472 else
473 strcpy(playlist, m3u8name);
475 len = strlen(playlist);
477 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
478 strcat(playlist, "8");
479 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
480 strcat(playlist, ".m3u8");
482 else
484 if (display_playlists(playlist, false) == -1)
485 return false;
488 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
490 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
491 sizeof(most_recent_playlist));
492 return true;
494 else
495 return false;