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 ****************************************************************************/
25 #include "string-extra.h"
41 #include "filetypes.h"
43 #include "playlist_catalog.h"
46 #define MAX_PLAYLISTS 400
48 /* Use for recursive directory search */
49 struct add_track_context
{
54 /* keep track of most recently used playlist */
55 static char most_recent_playlist
[MAX_PATH
];
57 /* directory where our playlists our stored */
58 static char playlist_dir
[MAX_PATH
];
59 static int playlist_dir_length
;
60 static bool playlist_dir_exists
= false;
62 /* Retrieve playlist directory from config file and verify it exists */
63 static int initialize_catalog(void)
65 static bool initialized
= false;
69 bool default_dir
= true;
71 /* directory config is of the format: "dir: /path/to/dir" */
72 if (global_settings
.playlist_catalog_dir
[0])
74 strcpy(playlist_dir
, global_settings
.playlist_catalog_dir
);
78 /* fall back to default directory if no or invalid config */
80 strlcpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
,
81 sizeof(playlist_dir
));
83 playlist_dir_length
= strlen(playlist_dir
);
85 if (dir_exists(playlist_dir
))
87 playlist_dir_exists
= true;
88 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
93 if (!playlist_dir_exists
)
95 if (mkdir(playlist_dir
) < 0) {
96 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
100 playlist_dir_exists
= true;
101 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
108 /* Use the filetree functions to retrieve the list of playlists in the
110 static int create_playlist_list(char** playlists
, int num_items
,
117 bool most_recent
= false;
119 struct tree_context
* tc
= tree_get_context();
120 int dirfilter
= *(tc
->dirfilter
);
124 /* use the tree browser dircache to load only playlists */
125 *(tc
->dirfilter
) = SHOW_PLAYLIST
;
127 if (ft_load(tc
, playlist_dir
) < 0)
129 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
133 files
= (struct entry
*) tc
->dircache
;
134 num_files
= tc
->filesindir
;
136 /* we've overwritten the dircache so tree browser will need to be
140 /* if it exists, most recent playlist will always be index 0 */
141 if (most_recent_playlist
[0] != '\0')
147 for (i
=0; i
<num_files
&& index
<num_items
; i
++)
149 if (files
[i
].attr
& FILE_ATTR_M3U
)
151 if (most_recent
&& !strncmp(files
[i
].name
, most_recent_playlist
,
152 sizeof(most_recent_playlist
)))
154 playlists
[0] = files
[i
].name
;
159 playlists
[index
] = files
[i
].name
;
165 *num_playlists
= index
;
167 /* we couldn't find the most recent playlist, shift all playlists up */
170 for (i
=0; i
<index
-1; i
++)
171 playlists
[i
] = playlists
[i
+1];
175 most_recent_playlist
[0] = '\0';
181 *(tc
->dirfilter
) = dirfilter
;
185 /* Callback for gui_synclist */
186 static const char* playlist_callback_name(int selected_item
, void* data
,
187 char* buffer
, size_t buffer_len
)
189 char** playlists
= (char**) data
;
191 strlcpy(buffer
, playlists
[selected_item
], buffer_len
);
193 if (buffer
[0] != '.' && !(global_settings
.show_filename_ext
== 1
194 || (global_settings
.show_filename_ext
== 3
195 && global_settings
.dirfilter
== 0)))
197 char* dot
= strrchr(buffer
, '.');
208 static int playlist_callback_voice(int selected_item
, void* data
)
210 char** playlists
= (char**) data
;
211 talk_file_or_spell(playlist_dir
, playlists
[selected_item
], NULL
, false);
215 /* Display all playlists in catalog. Selected "playlist" is returned.
216 If "view" mode is set then we're not adding anything into playlist. */
217 static int display_playlists(char* playlist
, bool view
)
220 int num_playlists
= 0;
222 char temp_buf
[MAX_PATH
];
223 char* playlists
[MAX_PLAYLISTS
];
224 struct gui_synclist playlist_lists
;
226 if (create_playlist_list(playlists
, MAX_PLAYLISTS
,
227 &num_playlists
) != 0)
230 if (num_playlists
<= 0)
232 splash(HZ
*2, ID2P(LANG_CATALOG_NO_PLAYLISTS
));
239 gui_synclist_init(&playlist_lists
, playlist_callback_name
, playlists
,
241 if(global_settings
.talk_menu
)
242 gui_synclist_set_voice_callback(&playlist_lists
,
243 playlist_callback_voice
);
244 gui_synclist_set_nb_items(&playlist_lists
, num_playlists
);
245 gui_synclist_draw(&playlist_lists
);
246 gui_synclist_speak_item(&playlist_lists
);
252 list_do_action(CONTEXT_LIST
,HZ
/2,
253 &playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
);
254 sel_file
= playlists
[gui_synclist_get_sel_pos(&playlist_lists
)];
258 case ACTION_STD_CANCEL
:
263 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
, sel_file
);
267 /* In view mode, selecting a playlist starts playback */
268 ft_play_playlist(playlist
, playlist_dir
, sel_file
);
275 case ACTION_STD_CONTEXT
:
276 /* context menu only available in view mode */
279 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
282 if (onplay(playlist
, FILE_ATTR_M3U
,
283 CONTEXT_TREE
, false) != ONPLAY_OK
)
290 gui_synclist_draw(&playlist_lists
);
291 gui_synclist_speak_item(&playlist_lists
);
297 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
308 /* display number of tracks inserted into playlists. Used for directory
310 static void display_insert_count(int count
)
312 static long talked_tick
= 0;
313 if(global_settings
.talk_menu
&& count
&&
314 (talked_tick
== 0 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
)))
316 talked_tick
= current_tick
;
317 talk_number(count
, false);
318 talk_id(LANG_PLAYLIST_INSERT_COUNT
, true);
321 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
, str(LANG_OFF_ABORT
));
324 /* Add specified track into playlist. Callback from directory insert */
325 static int add_track_to_playlist(char* filename
, void* context
)
327 struct add_track_context
* c
= (struct add_track_context
*) context
;
329 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
334 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
335 display_insert_count(c
->count
);
340 /* Add "sel" file into specified "playlist". How to insert depends on type
342 static int add_to_playlist(const char* playlist
, bool new_playlist
,
343 const char* sel
, int sel_attr
)
349 fd
= open_utf8(playlist
, O_CREAT
|O_WRONLY
|O_TRUNC
);
351 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
, 0666);
356 /* In case we're in the playlist directory */
359 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
361 /* append the selected file */
362 if (fdprintf(fd
, "%s\n", sel
) > 0)
365 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
367 /* append playlist */
371 if(strcasecmp(playlist
, sel
) == 0)
374 f
= open_utf8(sel
, O_RDONLY
);
384 n
= read(f
, buf
, sizeof(buf
));
388 if (write(fd
, buf
, n
) < 0)
399 else if (sel_attr
& ATTR_DIRECTORY
)
401 /* search directory for tracks and append to playlist */
402 bool recurse
= false;
403 const char *lines
[] = {
404 ID2P(LANG_RECURSE_DIRECTORY_QUESTION
), sel
};
405 const struct text_message message
={lines
, 2};
406 struct add_track_context context
;
408 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
409 recurse
= (bool)global_settings
.recursive_dir_insert
;
412 /* Ask if user wants to recurse directory */
413 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
419 display_insert_count(0);
421 result
= playlist_directory_tracksearch(sel
, recurse
,
422 add_track_to_playlist
, &context
);
424 display_insert_count(context
.count
);
431 static bool in_cat_viewer
= false;
432 bool catalog_view_playlists(void)
438 if (initialize_catalog() == -1)
440 in_cat_viewer
= true;
441 retval
= (display_playlists(NULL
, true) != -1);
442 in_cat_viewer
= false;
446 bool catalog_add_to_a_playlist(const char* sel
, int sel_attr
,
447 bool new_playlist
, char *m3u8name
)
449 char playlist
[MAX_PATH
];
451 if (initialize_catalog() == -1)
457 if (m3u8name
== NULL
)
459 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
460 if (kbd_input(playlist
, MAX_PATH
))
464 strcpy(playlist
, m3u8name
);
466 len
= strlen(playlist
);
468 if(len
> 4 && !strcasecmp(&playlist
[len
-4], ".m3u"))
469 strcat(playlist
, "8");
470 else if(len
<= 5 || strcasecmp(&playlist
[len
-5], ".m3u8"))
471 strcat(playlist
, ".m3u8");
475 if (display_playlists(playlist
, false) == -1)
479 if (add_to_playlist(playlist
, new_playlist
, sel
, sel_attr
) == 0)
481 strlcpy(most_recent_playlist
, playlist
+playlist_dir_length
+1,
482 sizeof(most_recent_playlist
));