Fix minor typos in description fields.
[kugel-rb.git] / apps / playlist_catalog.c
blobef44e36ba7426880c1fa6bd76002dd685408b23f
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-extra.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 "filefuncs.h"
36 #include "onplay.h"
37 #include "playlist.h"
38 #include "settings.h"
39 #include "splash.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)
82 const char *dir = get_user_file_path(PLAYLIST_CATALOG_DEFAULT_DIR,
83 FORCE_BUFFER_COPY|NEED_WRITE,
84 playlist_dir, sizeof(playlist_dir));
85 if (!dir_exists(dir))
86 mkdir(dir);
89 playlist_dir_length = strlen(playlist_dir);
91 if (dir_exists(playlist_dir))
93 playlist_dir_exists = true;
94 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
95 initialized = true;
99 if (!playlist_dir_exists)
101 if (mkdir(playlist_dir) < 0) {
102 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
103 return -1;
105 else {
106 playlist_dir_exists = true;
107 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
108 initialized = true;
112 return 0;
114 /* Use the filetree functions to retrieve the list of playlists in the
115 directory */
116 static int create_playlist_list(char** playlists, int num_items,
117 int* num_playlists)
119 int result = -1;
120 int num_files = 0;
121 int index = 0;
122 int i;
123 bool most_recent = false;
124 struct entry *files;
125 struct tree_context* tc = tree_get_context();
126 int dirfilter = *(tc->dirfilter);
128 *num_playlists = 0;
130 /* use the tree browser dircache to load only playlists */
131 *(tc->dirfilter) = SHOW_PLAYLIST;
133 if (ft_load(tc, playlist_dir) < 0)
135 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
136 goto exit;
139 files = (struct entry*) tc->dircache;
140 num_files = tc->filesindir;
142 /* we've overwritten the dircache so tree browser will need to be
143 reloaded */
144 reload_directory();
146 /* if it exists, most recent playlist will always be index 0 */
147 if (most_recent_playlist[0] != '\0')
149 index = 1;
150 most_recent = true;
153 for (i=0; i<num_files && index<num_items; i++)
155 if (files[i].attr & FILE_ATTR_M3U)
157 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
158 sizeof(most_recent_playlist)))
160 playlists[0] = files[i].name;
161 most_recent = false;
163 else
165 playlists[index] = files[i].name;
166 index++;
171 *num_playlists = index;
173 /* we couldn't find the most recent playlist, shift all playlists up */
174 if (most_recent)
176 for (i=0; i<index-1; i++)
177 playlists[i] = playlists[i+1];
179 (*num_playlists)--;
181 most_recent_playlist[0] = '\0';
184 result = 0;
186 exit:
187 *(tc->dirfilter) = dirfilter;
188 return result;
191 /* Callback for gui_synclist */
192 static const char* playlist_callback_name(int selected_item, void* data,
193 char* buffer, size_t buffer_len)
195 char** playlists = (char**) data;
197 strlcpy(buffer, playlists[selected_item], buffer_len);
199 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
200 || (global_settings.show_filename_ext == 3
201 && global_settings.dirfilter == 0)))
203 char* dot = strrchr(buffer, '.');
205 if (dot != NULL)
207 *dot = '\0';
211 return buffer;
214 static int playlist_callback_voice(int selected_item, void* data)
216 char** playlists = (char**) data;
217 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
218 return 0;
221 /* Display all playlists in catalog. Selected "playlist" is returned.
222 If "view" mode is set then we're not adding anything into playlist. */
223 static int display_playlists(char* playlist, bool view)
225 int result = -1;
226 int num_playlists = 0;
227 bool exit = false;
228 char temp_buf[MAX_PATH];
229 char* playlists[MAX_PLAYLISTS];
230 struct gui_synclist playlist_lists;
232 if (create_playlist_list(playlists, MAX_PLAYLISTS,
233 &num_playlists) != 0)
234 return -1;
236 if (num_playlists <= 0)
238 splash(HZ*2, ID2P(LANG_CATALOG_NO_PLAYLISTS));
239 return -1;
242 if (!playlist)
243 playlist = temp_buf;
245 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
246 false, 1, NULL);
247 if(global_settings.talk_menu)
248 gui_synclist_set_voice_callback(&playlist_lists,
249 playlist_callback_voice);
250 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
251 gui_synclist_draw(&playlist_lists);
252 gui_synclist_speak_item(&playlist_lists);
254 while (!exit)
256 int button;
257 char* sel_file;
258 list_do_action(CONTEXT_LIST,HZ/2,
259 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
260 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
262 switch (button)
264 case ACTION_STD_CANCEL:
265 exit = true;
266 break;
268 case ACTION_STD_OK:
269 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
271 if (view)
273 /* In view mode, selecting a playlist starts playback */
274 ft_play_playlist(playlist, playlist_dir, sel_file);
277 result = 0;
278 exit = true;
279 break;
281 case ACTION_STD_CONTEXT:
282 /* context menu only available in view mode */
283 if (view)
285 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
286 sel_file);
288 if (onplay(playlist, FILE_ATTR_M3U,
289 CONTEXT_TREE, false) != ONPLAY_OK)
291 result = 0;
292 exit = true;
294 else
296 gui_synclist_draw(&playlist_lists);
297 gui_synclist_speak_item(&playlist_lists);
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;
354 if (new_playlist)
355 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
356 else
357 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND, 0666);
359 if(fd < 0)
360 return result;
362 /* In case we're in the playlist directory */
363 reload_directory();
365 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
367 /* append the selected file */
368 if (fdprintf(fd, "%s\n", sel) > 0)
369 result = 0;
371 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
373 /* append playlist */
374 int f, fs, i;
375 char buf[1024];
377 if(strcasecmp(playlist, sel) == 0)
378 goto exit;
380 f = open_utf8(sel, O_RDONLY);
381 if (f < 0)
382 goto exit;
384 i = lseek(f, 0, SEEK_CUR);
385 fs = filesize(f);
386 while (i < fs)
388 int n;
390 n = read(f, buf, sizeof(buf));
391 if (n < 0)
392 break;
394 if (write(fd, buf, n) < 0)
395 break;
397 i += n;
400 if (i >= fs)
401 result = 0;
403 close(f);
405 else if (sel_attr & ATTR_DIRECTORY)
407 /* search directory for tracks and append to playlist */
408 bool recurse = false;
409 const char *lines[] = {
410 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
411 const struct text_message message={lines, 2};
412 struct add_track_context context;
414 if (global_settings.recursive_dir_insert != RECURSE_ASK)
415 recurse = (bool)global_settings.recursive_dir_insert;
416 else
418 /* Ask if user wants to recurse directory */
419 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
422 context.fd = fd;
423 context.count = 0;
425 display_insert_count(0);
427 result = playlist_directory_tracksearch(sel, recurse,
428 add_track_to_playlist, &context);
430 display_insert_count(context.count);
433 exit:
434 close(fd);
435 return result;
437 static bool in_cat_viewer = false;
438 bool catalog_view_playlists(void)
440 bool retval = true;
441 if (in_cat_viewer)
442 return false;
444 if (initialize_catalog() == -1)
445 return false;
446 in_cat_viewer = true;
447 retval = (display_playlists(NULL, true) != -1);
448 in_cat_viewer = false;
449 return retval;
452 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
453 bool new_playlist, char *m3u8name)
455 char playlist[MAX_PATH];
457 if (initialize_catalog() == -1)
458 return false;
460 if (new_playlist)
462 size_t len;
463 if (m3u8name == NULL)
465 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
466 if (kbd_input(playlist, MAX_PATH))
467 return false;
469 else
470 strcpy(playlist, m3u8name);
472 len = strlen(playlist);
474 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
475 strcat(playlist, "8");
476 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
477 strcat(playlist, ".m3u8");
479 else
481 if (display_playlists(playlist, false) == -1)
482 return false;
485 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
487 strlcpy(most_recent_playlist, playlist+playlist_dir_length+1,
488 sizeof(most_recent_playlist));
489 return true;
491 else
492 return false;