Fix strcpy from a user-supplied string to fixed size string.
[kugel-rb.git] / apps / playlist_catalog.c
blobc5d4df0684b0f7f20762025660fafccc7bf2dd39
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 "talk.h"
47 #define MAX_PLAYLISTS 400
49 /* Use for recursive directory search */
50 struct add_track_context {
51 int fd;
52 int count;
55 /* keep track of most recently used playlist */
56 static char most_recent_playlist[MAX_PATH];
58 /* directory where our playlists our stored */
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 bool default_dir = true;
72 /* directory config is of the format: "dir: /path/to/dir" */
73 if (global_settings.playlist_catalog_dir[0])
75 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
76 default_dir = false;
79 /* fall back to default directory if no or invalid config */
80 if (default_dir)
81 strlcpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
82 sizeof(playlist_dir));
84 playlist_dir_length = strlen(playlist_dir);
86 if (dir_exists(playlist_dir))
88 playlist_dir_exists = true;
89 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
90 initialized = true;
94 if (!playlist_dir_exists)
96 if (mkdir(playlist_dir) < 0) {
97 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
98 return -1;
100 else {
101 playlist_dir_exists = true;
102 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
103 initialized = true;
107 return 0;
109 /* Use the filetree functions to retrieve the list of playlists in the
110 directory */
111 static int create_playlist_list(char** playlists, int num_items,
112 int* num_playlists)
114 int result = -1;
115 int num_files = 0;
116 int index = 0;
117 int i;
118 bool most_recent = false;
119 struct entry *files;
120 struct tree_context* tc = tree_get_context();
121 int dirfilter = *(tc->dirfilter);
123 *num_playlists = 0;
125 /* use the tree browser dircache to load only playlists */
126 *(tc->dirfilter) = SHOW_PLAYLIST;
128 if (ft_load(tc, playlist_dir) < 0)
130 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
131 goto exit;
134 files = (struct entry*) tc->dircache;
135 num_files = tc->filesindir;
137 /* we've overwritten the dircache so tree browser will need to be
138 reloaded */
139 reload_directory();
141 /* if it exists, most recent playlist will always be index 0 */
142 if (most_recent_playlist[0] != '\0')
144 index = 1;
145 most_recent = true;
148 for (i=0; i<num_files && index<num_items; i++)
150 if (files[i].attr & FILE_ATTR_M3U)
152 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
153 sizeof(most_recent_playlist)))
155 playlists[0] = files[i].name;
156 most_recent = false;
158 else
160 playlists[index] = files[i].name;
161 index++;
166 *num_playlists = index;
168 /* we couldn't find the most recent playlist, shift all playlists up */
169 if (most_recent)
171 for (i=0; i<index-1; i++)
172 playlists[i] = playlists[i+1];
174 (*num_playlists)--;
176 most_recent_playlist[0] = '\0';
179 result = 0;
181 exit:
182 *(tc->dirfilter) = dirfilter;
183 return result;
186 /* Callback for gui_synclist */
187 static const char* playlist_callback_name(int selected_item, void* data,
188 char* buffer, size_t buffer_len)
190 char** playlists = (char**) data;
192 strlcpy(buffer, playlists[selected_item], buffer_len);
194 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
195 || (global_settings.show_filename_ext == 3
196 && global_settings.dirfilter == 0)))
198 char* dot = strrchr(buffer, '.');
200 if (dot != NULL)
202 *dot = '\0';
206 return buffer;
209 static int playlist_callback_voice(int selected_item, void* data)
211 char** playlists = (char**) data;
212 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
213 return 0;
216 /* Display all playlists in catalog. Selected "playlist" is returned.
217 If "view" mode is set then we're not adding anything into playlist. */
218 static int display_playlists(char* playlist, bool view)
220 int result = -1;
221 int num_playlists = 0;
222 bool exit = false;
223 char temp_buf[MAX_PATH];
224 char* playlists[MAX_PLAYLISTS];
225 struct gui_synclist playlist_lists;
227 if (create_playlist_list(playlists, MAX_PLAYLISTS,
228 &num_playlists) != 0)
229 return -1;
231 if (num_playlists <= 0)
233 splash(HZ*2, ID2P(LANG_CATALOG_NO_PLAYLISTS));
234 return -1;
237 if (!playlist)
238 playlist = temp_buf;
240 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
241 false, 1, NULL);
242 if(global_settings.talk_menu)
243 gui_synclist_set_voice_callback(&playlist_lists,
244 playlist_callback_voice);
245 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
246 gui_synclist_draw(&playlist_lists);
247 gui_synclist_speak_item(&playlist_lists);
249 while (!exit)
251 int button;
252 char* sel_file;
253 list_do_action(CONTEXT_LIST,HZ/2,
254 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
255 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
257 switch (button)
259 case ACTION_STD_CANCEL:
260 exit = true;
261 break;
263 case ACTION_STD_OK:
264 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
266 if (view)
268 /* In view mode, selecting a playlist starts playback */
269 ft_play_playlist(playlist, playlist_dir, sel_file);
272 result = 0;
273 exit = true;
274 break;
276 case ACTION_STD_CONTEXT:
277 /* context menu only available in view mode */
278 if (view)
280 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
281 sel_file);
283 if (onplay(playlist, FILE_ATTR_M3U,
284 CONTEXT_TREE) != ONPLAY_OK)
286 result = 0;
287 exit = true;
289 else
291 gui_synclist_draw(&playlist_lists);
292 gui_synclist_speak_item(&playlist_lists);
295 break;
297 default:
298 if(default_event_handler(button) == SYS_USB_CONNECTED)
300 result = -1;
301 exit = true;
303 break;
306 return result;
309 /* display number of tracks inserted into playlists. Used for directory
310 insert */
311 static void display_insert_count(int count)
313 static long talked_tick = 0;
314 if(global_settings.talk_menu && count &&
315 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
317 talked_tick = current_tick;
318 talk_number(count, false);
319 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
322 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, str(LANG_OFF_ABORT));
325 /* Add specified track into playlist. Callback from directory insert */
326 static int add_track_to_playlist(char* filename, void* context)
328 struct add_track_context* c = (struct add_track_context*) context;
330 if (fdprintf(c->fd, "%s\n", filename) <= 0)
331 return -1;
333 (c->count)++;
335 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
336 display_insert_count(c->count);
338 return 0;
341 /* Add "sel" file into specified "playlist". How to insert depends on type
342 of file */
343 static int add_to_playlist(const char* playlist, bool new_playlist,
344 const char* sel, int sel_attr)
346 int fd;
347 int result = -1;
349 if (new_playlist)
350 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
351 else
352 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_utf8(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 const char *lines[] = {
405 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
406 const struct text_message message={lines, 2};
407 struct add_track_context context;
409 if (global_settings.recursive_dir_insert != RECURSE_ASK)
410 recurse = (bool)global_settings.recursive_dir_insert;
411 else
413 /* Ask if user wants to recurse directory */
414 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
417 context.fd = fd;
418 context.count = 0;
420 display_insert_count(0);
422 result = playlist_directory_tracksearch(sel, recurse,
423 add_track_to_playlist, &context);
425 display_insert_count(context.count);
428 exit:
429 close(fd);
430 return result;
432 static bool in_cat_viewer = false;
433 bool catalog_view_playlists(void)
435 bool retval = true;
436 if (in_cat_viewer)
437 return false;
439 if (initialize_catalog() == -1)
440 return false;
441 in_cat_viewer = true;
442 retval = (display_playlists(NULL, true) != -1);
443 in_cat_viewer = false;
444 return retval;
447 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
448 bool new_playlist, char *m3u8name)
450 char playlist[MAX_PATH];
452 if (initialize_catalog() == -1)
453 return false;
455 if (new_playlist)
457 size_t len;
458 if (m3u8name == NULL)
460 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
461 if (kbd_input(playlist, MAX_PATH))
462 return false;
464 else
465 strcpy(playlist, m3u8name);
467 len = strlen(playlist);
469 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
470 strcat(playlist, "8");
471 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
472 strcat(playlist, ".m3u8");
474 else
476 if (display_playlists(playlist, false) == -1)
477 return false;
480 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
482 strlcpy(most_recent_playlist, playlist+playlist_dir_length+1,
483 sizeof(most_recent_playlist));
484 return true;
486 else
487 return false;