Fixed m200v4 red build.
[kugel-rb.git] / apps / playlist_catalog.c
blob7daae155521c00db79ad2028202e8895f7996351
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
50 /* Use for recursive directory search */
51 struct add_track_context {
52 int fd;
53 int count;
56 /* keep track of most recently used playlist */
57 static char most_recent_playlist[MAX_PATH];
59 /* directory where our playlists our stored */
60 static char playlist_dir[MAX_PATH];
61 static int playlist_dir_length;
62 static bool playlist_dir_exists = false;
64 /* Retrieve playlist directory from config file and verify it exists */
65 static int initialize_catalog(void)
67 static bool initialized = false;
69 if (!initialized)
71 bool default_dir = true;
73 /* directory config is of the format: "dir: /path/to/dir" */
74 if (global_settings.playlist_catalog_dir[0])
76 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
77 default_dir = false;
80 /* fall back to default directory if no or invalid config */
81 if (default_dir)
82 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
83 sizeof(playlist_dir));
85 playlist_dir_length = strlen(playlist_dir);
87 if (dir_exists(playlist_dir))
89 playlist_dir_exists = true;
90 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
91 initialized = true;
95 if (!playlist_dir_exists)
97 if (mkdir(playlist_dir) < 0) {
98 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
99 return -1;
101 else {
102 playlist_dir_exists = true;
103 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
104 initialized = true;
108 return 0;
110 /* Use the filetree functions to retrieve the list of playlists in the
111 directory */
112 static int create_playlist_list(char** playlists, int num_items,
113 int* num_playlists)
115 int result = -1;
116 int num_files = 0;
117 int index = 0;
118 int i;
119 bool most_recent = false;
120 struct entry *files;
121 struct tree_context* tc = tree_get_context();
122 int dirfilter = *(tc->dirfilter);
124 *num_playlists = 0;
126 /* use the tree browser dircache to load only playlists */
127 *(tc->dirfilter) = SHOW_PLAYLIST;
129 if (ft_load(tc, playlist_dir) < 0)
131 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
132 goto exit;
135 files = (struct entry*) tc->dircache;
136 num_files = tc->filesindir;
138 /* we've overwritten the dircache so tree browser will need to be
139 reloaded */
140 reload_directory();
142 /* if it exists, most recent playlist will always be index 0 */
143 if (most_recent_playlist[0] != '\0')
145 index = 1;
146 most_recent = true;
149 for (i=0; i<num_files && index<num_items; i++)
151 if (files[i].attr & FILE_ATTR_M3U)
153 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
154 sizeof(most_recent_playlist)))
156 playlists[0] = files[i].name;
157 most_recent = false;
159 else
161 playlists[index] = files[i].name;
162 index++;
167 *num_playlists = index;
169 /* we couldn't find the most recent playlist, shift all playlists up */
170 if (most_recent)
172 for (i=0; i<index-1; i++)
173 playlists[i] = playlists[i+1];
175 (*num_playlists)--;
177 most_recent_playlist[0] = '\0';
180 result = 0;
182 exit:
183 *(tc->dirfilter) = dirfilter;
184 return result;
187 /* Callback for gui_synclist */
188 static char* playlist_callback_name(int selected_item, void* data,
189 char* buffer, size_t buffer_len)
191 char** playlists = (char**) data;
193 strncpy(buffer, playlists[selected_item], buffer_len);
195 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
196 || (global_settings.show_filename_ext == 3
197 && global_settings.dirfilter == 0)))
199 char* dot = strrchr(buffer, '.');
201 if (dot != NULL)
203 *dot = '\0';
207 return buffer;
210 static int playlist_callback_voice(int selected_item, void* data)
212 char** playlists = (char**) data;
213 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
214 return 0;
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 splash(HZ*2, ID2P(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, NULL);
243 if(global_settings.talk_menu)
244 gui_synclist_set_voice_callback(&playlist_lists,
245 playlist_callback_voice);
246 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
247 gui_synclist_draw(&playlist_lists);
248 gui_synclist_speak_item(&playlist_lists);
250 while (!exit)
252 int button;
253 char* sel_file;
254 list_do_action(CONTEXT_LIST,HZ/2,
255 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
256 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
258 switch (button)
260 case ACTION_STD_CANCEL:
261 exit = true;
262 break;
264 case ACTION_STD_OK:
265 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
267 if (view)
269 /* In view mode, selecting a playlist starts playback */
270 ft_play_playlist(playlist, playlist_dir, sel_file);
273 result = 0;
274 exit = true;
275 break;
277 case ACTION_STD_CONTEXT:
278 /* context menu only available in view mode */
279 if (view)
281 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
282 sel_file);
284 if (onplay(playlist, FILE_ATTR_M3U,
285 CONTEXT_TREE) != ONPLAY_OK)
287 result = 0;
288 exit = true;
290 else
292 gui_synclist_draw(&playlist_lists);
293 gui_synclist_speak_item(&playlist_lists);
296 break;
298 case ACTION_NONE:
299 gui_syncstatusbar_draw(&statusbars, false);
300 break;
302 default:
303 if(default_event_handler(button) == SYS_USB_CONNECTED)
305 result = -1;
306 exit = true;
308 break;
311 return result;
314 /* display number of tracks inserted into playlists. Used for directory
315 insert */
316 static void display_insert_count(int count)
318 static long talked_tick = 0;
319 if(global_settings.talk_menu && count &&
320 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
322 talked_tick = current_tick;
323 talk_number(count, false);
324 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
327 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, 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, bool new_playlist,
349 const char* sel, int sel_attr)
351 int fd;
352 int result = -1;
353 int flags = O_CREAT|O_WRONLY;
355 if (new_playlist)
356 flags |= O_TRUNC;
357 else
358 flags |= O_APPEND;
360 fd = open(playlist, flags);
361 if(fd < 0)
362 return result;
364 /* In case we're in the playlist directory */
365 reload_directory();
367 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
369 /* append the selected file */
370 if (fdprintf(fd, "%s\n", sel) > 0)
371 result = 0;
373 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
375 /* append playlist */
376 int f, fs, i;
377 char buf[1024];
379 if(strcasecmp(playlist, sel) == 0)
380 goto exit;
382 f = open(sel, O_RDONLY);
383 if (f < 0)
384 goto exit;
386 fs = filesize(f);
388 for (i=0; i<fs;)
390 int n;
392 n = read(f, buf, sizeof(buf));
393 if (n < 0)
394 break;
396 if (write(fd, buf, n) < 0)
397 break;
399 i += n;
402 if (i >= fs)
403 result = 0;
405 close(f);
407 else if (sel_attr & ATTR_DIRECTORY)
409 /* search directory for tracks and append to playlist */
410 bool recurse = false;
411 const char *lines[] = {
412 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
413 const struct text_message message={lines, 2};
414 struct add_track_context context;
416 if (global_settings.recursive_dir_insert != RECURSE_ASK)
417 recurse = (bool)global_settings.recursive_dir_insert;
418 else
420 /* Ask if user wants to recurse directory */
421 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
424 context.fd = fd;
425 context.count = 0;
427 display_insert_count(0);
429 result = playlist_directory_tracksearch(sel, recurse,
430 add_track_to_playlist, &context);
432 display_insert_count(context.count);
435 exit:
436 close(fd);
437 return result;
440 bool catalog_view_playlists(void)
442 if (initialize_catalog() == -1)
443 return false;
445 if (display_playlists(NULL, true) == -1)
446 return false;
448 return true;
451 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
452 bool new_playlist, char *m3u8name)
454 char playlist[MAX_PATH];
456 if (initialize_catalog() == -1)
457 return false;
459 if (new_playlist)
461 size_t len;
462 if (m3u8name == NULL)
464 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
465 if (kbd_input(playlist, MAX_PATH))
466 return false;
468 else
469 strcpy(playlist, m3u8name);
471 len = strlen(playlist);
473 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
474 strcat(playlist, "8");
475 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
476 strcat(playlist, ".m3u8");
478 else
480 if (display_playlists(playlist, false) == -1)
481 return false;
484 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
486 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
487 sizeof(most_recent_playlist));
488 return true;
490 else
491 return false;