tabs -> spaces in the new Yesno java files and remove unused imports.
[kugel-rb.git] / apps / playlist_catalog.c
blob5f9f46728ac619d160ee3b8d4c4a6440264f250f
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] &&
74 strcmp(global_settings.playlist_catalog_dir,
75 PLAYLIST_CATALOG_DEFAULT_DIR))
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)
84 const char *dir = get_user_file_path(PLAYLIST_CATALOG_DEFAULT_DIR,
85 FORCE_BUFFER_COPY|NEED_WRITE,
86 playlist_dir, sizeof(playlist_dir));
87 if (!dir_exists(dir))
88 mkdir(dir);
91 playlist_dir_length = strlen(playlist_dir);
93 if (dir_exists(playlist_dir))
95 playlist_dir_exists = true;
96 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
97 initialized = true;
101 if (!playlist_dir_exists)
103 if (mkdir(playlist_dir) < 0) {
104 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
105 return -1;
107 else {
108 playlist_dir_exists = true;
109 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
110 initialized = true;
114 return 0;
116 /* Use the filetree functions to retrieve the list of playlists in the
117 directory */
118 static int create_playlist_list(char** playlists, int num_items,
119 int* num_playlists)
121 int result = -1;
122 int num_files = 0;
123 int index = 0;
124 int i;
125 bool most_recent = false;
126 struct entry *files;
127 struct tree_context* tc = tree_get_context();
128 int dirfilter = *(tc->dirfilter);
130 *num_playlists = 0;
132 /* use the tree browser dircache to load only playlists */
133 *(tc->dirfilter) = SHOW_PLAYLIST;
135 if (ft_load(tc, playlist_dir) < 0)
137 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
138 goto exit;
141 files = (struct entry*) tc->dircache;
142 num_files = tc->filesindir;
144 /* we've overwritten the dircache so tree browser will need to be
145 reloaded */
146 reload_directory();
148 /* if it exists, most recent playlist will always be index 0 */
149 if (most_recent_playlist[0] != '\0')
151 index = 1;
152 most_recent = true;
155 for (i=0; i<num_files && index<num_items; i++)
157 if (files[i].attr & FILE_ATTR_M3U)
159 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
160 sizeof(most_recent_playlist)))
162 playlists[0] = files[i].name;
163 most_recent = false;
165 else
167 playlists[index] = files[i].name;
168 index++;
173 *num_playlists = index;
175 /* we couldn't find the most recent playlist, shift all playlists up */
176 if (most_recent)
178 for (i=0; i<index-1; i++)
179 playlists[i] = playlists[i+1];
181 (*num_playlists)--;
183 most_recent_playlist[0] = '\0';
186 result = 0;
188 exit:
189 *(tc->dirfilter) = dirfilter;
190 return result;
193 /* Callback for gui_synclist */
194 static const char* playlist_callback_name(int selected_item, void* data,
195 char* buffer, size_t buffer_len)
197 char** playlists = (char**) data;
199 strlcpy(buffer, playlists[selected_item], buffer_len);
201 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
202 || (global_settings.show_filename_ext == 3
203 && global_settings.dirfilter == 0)))
205 char* dot = strrchr(buffer, '.');
207 if (dot != NULL)
209 *dot = '\0';
213 return buffer;
216 static int playlist_callback_voice(int selected_item, void* data)
218 char** playlists = (char**) data;
219 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
220 return 0;
223 /* Display all playlists in catalog. Selected "playlist" is returned.
224 If "view" mode is set then we're not adding anything into playlist. */
225 static int display_playlists(char* playlist, bool view)
227 int result = -1;
228 int num_playlists = 0;
229 bool exit = false;
230 char temp_buf[MAX_PATH];
231 char* playlists[MAX_PLAYLISTS];
232 struct gui_synclist playlist_lists;
234 if (create_playlist_list(playlists, MAX_PLAYLISTS,
235 &num_playlists) != 0)
236 return -1;
238 if (num_playlists <= 0)
240 splash(HZ*2, ID2P(LANG_CATALOG_NO_PLAYLISTS));
241 return -1;
244 if (!playlist)
245 playlist = temp_buf;
247 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
248 false, 1, NULL);
249 if(global_settings.talk_menu)
250 gui_synclist_set_voice_callback(&playlist_lists,
251 playlist_callback_voice);
252 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
253 gui_synclist_draw(&playlist_lists);
254 gui_synclist_speak_item(&playlist_lists);
256 while (!exit)
258 int button;
259 char* sel_file;
260 list_do_action(CONTEXT_LIST,HZ/2,
261 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
262 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
264 switch (button)
266 case ACTION_STD_CANCEL:
267 exit = true;
268 break;
270 case ACTION_STD_OK:
271 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
273 if (view)
275 /* In view mode, selecting a playlist starts playback */
276 ft_play_playlist(playlist, playlist_dir, sel_file);
279 result = 0;
280 exit = true;
281 break;
283 case ACTION_STD_CONTEXT:
284 /* context menu only available in view mode */
285 if (view)
287 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
288 sel_file);
290 if (onplay(playlist, FILE_ATTR_M3U,
291 CONTEXT_TREE, false) != ONPLAY_OK)
293 result = 0;
294 exit = true;
296 else
298 gui_synclist_draw(&playlist_lists);
299 gui_synclist_speak_item(&playlist_lists);
302 break;
304 default:
305 if(default_event_handler(button) == SYS_USB_CONNECTED)
307 result = -1;
308 exit = true;
310 break;
313 return result;
316 /* display number of tracks inserted into playlists. Used for directory
317 insert */
318 static void display_insert_count(int count)
320 static long talked_tick = 0;
321 if(global_settings.talk_menu && count &&
322 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
324 talked_tick = current_tick;
325 talk_number(count, false);
326 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
329 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, str(LANG_OFF_ABORT));
332 /* Add specified track into playlist. Callback from directory insert */
333 static int add_track_to_playlist(char* filename, void* context)
335 struct add_track_context* c = (struct add_track_context*) context;
337 if (fdprintf(c->fd, "%s\n", filename) <= 0)
338 return -1;
340 (c->count)++;
342 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
343 display_insert_count(c->count);
345 return 0;
348 /* Add "sel" file into specified "playlist". How to insert depends on type
349 of file */
350 static int add_to_playlist(const char* playlist, bool new_playlist,
351 const char* sel, int sel_attr)
353 int fd;
354 int result = -1;
356 if (new_playlist)
357 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
358 else
359 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND, 0666);
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_utf8(sel, O_RDONLY);
383 if (f < 0)
384 goto exit;
386 i = lseek(f, 0, SEEK_CUR);
387 fs = filesize(f);
388 while (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;
439 static bool in_cat_viewer = false;
440 bool catalog_view_playlists(void)
442 bool retval = true;
443 if (in_cat_viewer)
444 return false;
446 if (initialize_catalog() == -1)
447 return false;
448 in_cat_viewer = true;
449 retval = (display_playlists(NULL, true) != -1);
450 in_cat_viewer = false;
451 return retval;
454 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
455 bool new_playlist, char *m3u8name)
457 char playlist[MAX_PATH];
459 if (initialize_catalog() == -1)
460 return false;
462 if (new_playlist)
464 size_t len;
465 if (m3u8name == NULL)
467 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
468 if (kbd_input(playlist, MAX_PATH))
469 return false;
471 else
472 strcpy(playlist, m3u8name);
474 len = strlen(playlist);
476 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
477 strcat(playlist, "8");
478 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
479 strcat(playlist, ".m3u8");
481 else
483 if (display_playlists(playlist, false) == -1)
484 return false;
487 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
489 strlcpy(most_recent_playlist, playlist+playlist_dir_length+1,
490 sizeof(most_recent_playlist));
491 return true;
493 else
494 return false;