1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
42 #include "filetypes.h"
44 #include "playlist_catalog.h"
45 #include "statusbar.h"
48 #define MAX_PLAYLISTS 400
49 #define PLAYLIST_DISPLAY_COUNT 10
51 /* Use for recursive directory search */
52 struct add_track_context
{
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;
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
);
81 /* fall back to default directory if no or invalid config */
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
));
96 if (!playlist_dir_exists
)
98 if (mkdir(playlist_dir
) < 0) {
99 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
103 playlist_dir_exists
= true;
104 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
111 /* Use the filetree functions to retrieve the list of playlists in the
113 static int create_playlist_list(char** playlists
, int num_items
,
120 bool most_recent
= false;
122 struct tree_context
* tc
= tree_get_context();
123 int dirfilter
= *(tc
->dirfilter
);
127 /* use the tree browser dircache to load only playlists */
128 *(tc
->dirfilter
) = SHOW_PLAYLIST
;
130 if (ft_load(tc
, playlist_dir
) < 0)
132 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
136 files
= (struct entry
*) tc
->dircache
;
137 num_files
= tc
->filesindir
;
139 /* we've overwritten the dircache so tree browser will need to be
143 /* if it exists, most recent playlist will always be index 0 */
144 if (most_recent_playlist
[0] != '\0')
150 for (i
=0; i
<num_files
&& index
<num_items
; i
++)
152 if (files
[i
].attr
& FILE_ATTR_M3U
)
154 if (most_recent
&& !strncmp(files
[i
].name
, most_recent_playlist
,
155 sizeof(most_recent_playlist
)))
157 playlists
[0] = files
[i
].name
;
162 playlists
[index
] = files
[i
].name
;
168 *num_playlists
= index
;
170 /* we couldn't find the most recent playlist, shift all playlists up */
173 for (i
=0; i
<index
-1; i
++)
174 playlists
[i
] = playlists
[i
+1];
178 most_recent_playlist
[0] = '\0';
184 *(tc
->dirfilter
) = dirfilter
;
188 /* Callback for gui_synclist */
189 static char* playlist_callback_name(int selected_item
, void* data
,
190 char* buffer
, size_t buffer_len
)
192 char** playlists
= (char**) data
;
194 strncpy(buffer
, playlists
[selected_item
], buffer_len
);
196 if (buffer
[0] != '.' && !(global_settings
.show_filename_ext
== 1
197 || (global_settings
.show_filename_ext
== 3
198 && global_settings
.dirfilter
== 0)))
200 char* dot
= strrchr(buffer
, '.');
211 static int playlist_callback_voice(int selected_item
, void* data
)
213 char** playlists
= (char**) data
;
214 talk_file_or_spell(playlist_dir
, playlists
[selected_item
], NULL
, false);
218 /* Display all playlists in catalog. Selected "playlist" is returned.
219 If "view" mode is set then we're not adding anything into playlist. */
220 static int display_playlists(char* playlist
, bool view
)
223 int num_playlists
= 0;
225 char temp_buf
[MAX_PATH
];
226 char* playlists
[MAX_PLAYLISTS
];
227 struct gui_synclist playlist_lists
;
229 if (create_playlist_list(playlists
, sizeof(playlists
),
230 &num_playlists
) != 0)
233 if (num_playlists
<= 0)
235 splash(HZ
*2, ID2P(LANG_CATALOG_NO_PLAYLISTS
));
242 gui_synclist_init(&playlist_lists
, playlist_callback_name
, playlists
,
244 if(global_settings
.talk_menu
)
245 gui_synclist_set_voice_callback(&playlist_lists
,
246 playlist_callback_voice
);
247 gui_synclist_set_nb_items(&playlist_lists
, num_playlists
);
248 gui_synclist_draw(&playlist_lists
);
249 gui_synclist_speak_item(&playlist_lists
);
255 list_do_action(CONTEXT_LIST
,HZ
/2,
256 &playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
);
257 sel_file
= playlists
[gui_synclist_get_sel_pos(&playlist_lists
)];
261 case ACTION_STD_CANCEL
:
266 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
, sel_file
);
270 /* In view mode, selecting a playlist starts playback */
271 ft_play_playlist(playlist
, playlist_dir
, sel_file
);
278 case ACTION_STD_CONTEXT
:
279 /* context menu only available in view mode */
282 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
285 if (onplay(playlist
, FILE_ATTR_M3U
,
286 CONTEXT_TREE
) != ONPLAY_OK
)
293 gui_synclist_draw(&playlist_lists
);
294 gui_synclist_speak_item(&playlist_lists
);
300 gui_syncstatusbar_draw(&statusbars
, false);
304 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
315 /* display number of tracks inserted into playlists. Used for directory
317 static void display_insert_count(int count
)
319 static long talked_tick
= 0;
320 if(global_settings
.talk_menu
&& count
&&
321 (talked_tick
== 0 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
)))
323 talked_tick
= current_tick
;
324 talk_number(count
, false);
325 talk_id(LANG_PLAYLIST_INSERT_COUNT
, true);
328 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
, str(LANG_OFF_ABORT
));
331 /* Add specified track into playlist. Callback from directory insert */
332 static int add_track_to_playlist(char* filename
, void* context
)
334 struct add_track_context
* c
= (struct add_track_context
*) context
;
336 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
341 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
342 display_insert_count(c
->count
);
347 /* Add "sel" file into specified "playlist". How to insert depends on type
349 static int add_to_playlist(const char* playlist
, bool new_playlist
,
350 const char* sel
, int sel_attr
)
354 int flags
= O_CREAT
|O_WRONLY
;
361 fd
= open(playlist
, flags
);
365 /* In case we're in the playlist directory */
368 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
370 /* append the selected file */
371 if (fdprintf(fd
, "%s\n", sel
) > 0)
374 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
376 /* append playlist */
380 if(strcasecmp(playlist
, sel
) == 0)
383 f
= open(sel
, O_RDONLY
);
393 n
= read(f
, buf
, sizeof(buf
));
397 if (write(fd
, buf
, n
) < 0)
408 else if (sel_attr
& ATTR_DIRECTORY
)
410 /* search directory for tracks and append to playlist */
411 bool recurse
= false;
412 const char *lines
[] = {
413 ID2P(LANG_RECURSE_DIRECTORY_QUESTION
), sel
};
414 const struct text_message message
={lines
, 2};
415 struct add_track_context context
;
417 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
418 recurse
= (bool)global_settings
.recursive_dir_insert
;
421 /* Ask if user wants to recurse directory */
422 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
428 display_insert_count(0);
430 result
= playlist_directory_tracksearch(sel
, recurse
,
431 add_track_to_playlist
, &context
);
433 display_insert_count(context
.count
);
441 bool catalog_view_playlists(void)
443 if (initialize_catalog() == -1)
446 if (display_playlists(NULL
, true) == -1)
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)
463 if (m3u8name
== NULL
)
465 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
466 if (kbd_input(playlist
, MAX_PATH
))
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");
481 if (display_playlists(playlist
, false) == -1)
485 if (add_to_playlist(playlist
, new_playlist
, sel
, sel_attr
) == 0)
487 strncpy(most_recent_playlist
, playlist
+playlist_dir_length
+1,
488 sizeof(most_recent_playlist
));