1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Sebastian Henriksen, Hardeep Sidhu
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
40 #include "filetypes.h"
43 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
44 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
45 #define MAX_PLAYLISTS 400
46 #define PLAYLIST_DISPLAY_COUNT 10
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 (configured in
58 PLAYLIST_CATALOG_CFG) */
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;
72 bool default_dir
= true;
74 f
= open(PLAYLIST_CATALOG_CFG
, O_RDONLY
);
79 while (read_line(f
, buf
, sizeof(buf
)))
84 /* directory config is of the format: "dir: /path/to/dir" */
85 if (settings_parseline(buf
, &name
, &value
) &&
86 !strncasecmp(name
, "dir:", strlen(name
)) &&
89 strncpy(playlist_dir
, value
, strlen(value
));
97 /* fall back to default directory if no or invalid config */
99 strncpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
,
100 sizeof(playlist_dir
));
102 playlist_dir_length
= strlen(playlist_dir
);
104 dir
= opendir(playlist_dir
);
107 playlist_dir_exists
= true;
109 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
114 if (!playlist_dir_exists
)
116 if (mkdir(playlist_dir
) < 0) {
117 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_DIRECTORY
),
122 playlist_dir_exists
= true;
123 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
130 /* Use the filetree functions to retrieve the list of playlists in the
132 static int create_playlist_list(char** playlists
, int num_items
,
139 bool most_recent
= false;
141 struct tree_context
* tc
= tree_get_context();
142 int dirfilter
= *(tc
->dirfilter
);
146 /* use the tree browser dircache to load only playlists */
147 *(tc
->dirfilter
) = SHOW_PLAYLIST
;
149 if (ft_load(tc
, playlist_dir
) < 0)
151 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_DIRECTORY
),
156 files
= (struct entry
*) tc
->dircache
;
157 num_files
= tc
->filesindir
;
159 /* we've overwritten the dircache so tree browser will need to be
163 /* if it exists, most recent playlist will always be index 0 */
164 if (most_recent_playlist
[0] != '\0')
170 for (i
=0; i
<num_files
&& index
<num_items
; i
++)
172 if (files
[i
].attr
& FILE_ATTR_M3U
)
174 if (most_recent
&& !strncmp(files
[i
].name
, most_recent_playlist
,
175 sizeof(most_recent_playlist
)))
177 playlists
[0] = files
[i
].name
;
182 playlists
[index
] = files
[i
].name
;
188 *num_playlists
= index
;
190 /* we couldn't find the most recent playlist, shift all playlists up */
193 for (i
=0; i
<index
-1; i
++)
194 playlists
[i
] = playlists
[i
+1];
198 most_recent_playlist
[0] = '\0';
204 *(tc
->dirfilter
) = dirfilter
;
208 /* Callback for gui_synclist */
209 static char* playlist_callback_name(int selected_item
, void* data
,
212 char** playlists
= (char**) data
;
214 strncpy(buffer
, playlists
[selected_item
], MAX_PATH
);
216 if (buffer
[0] != '.' && !(global_settings
.show_filename_ext
== 1
217 || (global_settings
.show_filename_ext
== 3
218 && global_settings
.dirfilter
== 0)))
220 char* dot
= strrchr(buffer
, '.');
231 /* Display all playlists in catalog. Selected "playlist" is returned.
232 If "view" mode is set then we're not adding anything into playlist. */
233 static int display_playlists(char* playlist
, bool view
)
236 int num_playlists
= 0;
238 char temp_buf
[MAX_PATH
];
239 char* playlists
[MAX_PLAYLISTS
];
240 struct gui_synclist playlist_lists
;
242 if (create_playlist_list(playlists
, sizeof(playlists
),
243 &num_playlists
) != 0)
246 if (num_playlists
<= 0)
248 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_PLAYLISTS
));
255 gui_synclist_init(&playlist_lists
, playlist_callback_name
, playlists
,
257 gui_synclist_set_nb_items(&playlist_lists
, num_playlists
);
258 gui_synclist_draw(&playlist_lists
);
262 int button
= get_action(CONTEXT_LIST
,HZ
/2);
265 gui_synclist_do_button(&playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
);
267 sel_file
= playlists
[gui_synclist_get_sel_pos(&playlist_lists
)];
271 case ACTION_STD_CANCEL
:
276 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
, sel_file
);
280 /* In view mode, selecting a playlist starts playback */
281 ft_play_playlist(playlist
, playlist_dir
, sel_file
);
288 case ACTION_STD_CONTEXT
:
289 /* context menu only available in view mode */
292 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
295 if (onplay(playlist
, FILE_ATTR_M3U
,
296 CONTEXT_TREE
) != ONPLAY_OK
)
302 gui_synclist_draw(&playlist_lists
);
307 gui_syncstatusbar_draw(&statusbars
, false);
311 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
322 /* display number of tracks inserted into playlists. Used for directory
324 static void display_insert_count(int count
)
326 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
,
327 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)
340 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
341 display_insert_count(c
->count
);
346 /* Add "sel" file into specified "playlist". How to insert depends on type
348 static int add_to_playlist(const char* playlist
, char* sel
, int sel_attr
)
353 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
);
357 /* In case we're in the playlist directory */
360 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
362 /* append the selected file */
363 if (fdprintf(fd
, "%s\n", sel
) > 0)
366 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
368 /* append playlist */
372 if(strcasecmp(playlist
, sel
) == 0)
375 f
= open(sel
, O_RDONLY
);
385 n
= read(f
, buf
, sizeof(buf
));
389 if (write(fd
, buf
, n
) < 0)
400 else if (sel_attr
& ATTR_DIRECTORY
)
402 /* search directory for tracks and append to playlist */
403 bool recurse
= false;
405 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION
),
408 struct text_message message
={lines
, 2};
409 struct add_track_context context
;
411 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
412 recurse
= (bool)global_settings
.recursive_dir_insert
;
415 /* Ask if user wants to recurse directory */
416 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
422 display_insert_count(0);
424 result
= playlist_directory_tracksearch(sel
, recurse
,
425 add_track_to_playlist
, &context
);
427 display_insert_count(context
.count
);
435 bool catalog_view_playlists(void)
437 if (initialize_catalog() == -1)
440 if (display_playlists(NULL
, true) == -1)
446 bool catalog_add_to_a_playlist(char* sel
, int sel_attr
, bool new_playlist
)
448 char playlist
[MAX_PATH
];
450 if (initialize_catalog() == -1)
456 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
457 if (kbd_input(playlist
, MAX_PATH
))
460 len
= strlen(playlist
);
462 if(len
> 4 && !strcasecmp(&playlist
[len
-4], ".m3u"))
463 strcat(playlist
, "8");
464 else if(len
<= 5 || strcasecmp(&playlist
[len
-5], ".m3u8"))
465 strcat(playlist
, ".m3u8");
469 if (display_playlists(playlist
, false) == -1)
473 if (add_to_playlist(playlist
, sel
, sel_attr
) == 0)
475 strncpy(most_recent_playlist
, playlist
+playlist_dir_length
+1,
476 sizeof(most_recent_playlist
));