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 MAX_PLAYLISTS 400
44 #define PLAYLIST_DISPLAY_COUNT 10
46 /* Use for recursive directory search */
47 struct add_track_context
{
52 /* keep track of most recently used playlist */
53 static char most_recent_playlist
[MAX_PATH
];
55 /* directory where our playlists our stored */
56 static char playlist_dir
[MAX_PATH
];
57 static int playlist_dir_length
;
58 static bool playlist_dir_exists
= false;
60 /* Retrieve playlist directory from config file and verify it exists */
61 static int initialize_catalog(void)
63 static bool initialized
= false;
68 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 strncpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
,
81 sizeof(playlist_dir
));
83 playlist_dir_length
= strlen(playlist_dir
);
85 dir
= opendir(playlist_dir
);
88 playlist_dir_exists
= true;
90 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
95 if (!playlist_dir_exists
)
97 if (mkdir(playlist_dir
) < 0) {
98 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_DIRECTORY
),
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 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_DIRECTORY
),
137 files
= (struct entry
*) tc
->dircache
;
138 num_files
= tc
->filesindir
;
140 /* we've overwritten the dircache so tree browser will need to be
144 /* if it exists, most recent playlist will always be index 0 */
145 if (most_recent_playlist
[0] != '\0')
151 for (i
=0; i
<num_files
&& index
<num_items
; i
++)
153 if (files
[i
].attr
& FILE_ATTR_M3U
)
155 if (most_recent
&& !strncmp(files
[i
].name
, most_recent_playlist
,
156 sizeof(most_recent_playlist
)))
158 playlists
[0] = files
[i
].name
;
163 playlists
[index
] = files
[i
].name
;
169 *num_playlists
= index
;
171 /* we couldn't find the most recent playlist, shift all playlists up */
174 for (i
=0; i
<index
-1; i
++)
175 playlists
[i
] = playlists
[i
+1];
179 most_recent_playlist
[0] = '\0';
185 *(tc
->dirfilter
) = dirfilter
;
189 /* Callback for gui_synclist */
190 static char* playlist_callback_name(int selected_item
, void* data
,
193 char** playlists
= (char**) data
;
195 strncpy(buffer
, playlists
[selected_item
], MAX_PATH
);
197 if (buffer
[0] != '.' && !(global_settings
.show_filename_ext
== 1
198 || (global_settings
.show_filename_ext
== 3
199 && global_settings
.dirfilter
== 0)))
201 char* dot
= strrchr(buffer
, '.');
212 /* Display all playlists in catalog. Selected "playlist" is returned.
213 If "view" mode is set then we're not adding anything into playlist. */
214 static int display_playlists(char* playlist
, bool view
)
217 int num_playlists
= 0;
219 char temp_buf
[MAX_PATH
];
220 char* playlists
[MAX_PLAYLISTS
];
221 struct gui_synclist playlist_lists
;
223 if (create_playlist_list(playlists
, sizeof(playlists
),
224 &num_playlists
) != 0)
227 if (num_playlists
<= 0)
229 gui_syncsplash(HZ
*2, str(LANG_CATALOG_NO_PLAYLISTS
));
236 gui_synclist_init(&playlist_lists
, playlist_callback_name
, playlists
,
238 gui_synclist_set_nb_items(&playlist_lists
, num_playlists
);
239 gui_synclist_draw(&playlist_lists
);
243 int button
= get_action(CONTEXT_LIST
,HZ
/2);
246 gui_synclist_do_button(&playlist_lists
, &button
,LIST_WRAP_UNLESS_HELD
);
248 sel_file
= playlists
[gui_synclist_get_sel_pos(&playlist_lists
)];
252 case ACTION_STD_CANCEL
:
257 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
, sel_file
);
261 /* In view mode, selecting a playlist starts playback */
262 ft_play_playlist(playlist
, playlist_dir
, sel_file
);
269 case ACTION_STD_CONTEXT
:
270 /* context menu only available in view mode */
273 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
276 if (onplay(playlist
, FILE_ATTR_M3U
,
277 CONTEXT_TREE
) != ONPLAY_OK
)
283 gui_synclist_draw(&playlist_lists
);
288 gui_syncstatusbar_draw(&statusbars
, false);
292 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
303 /* display number of tracks inserted into playlists. Used for directory
305 static void display_insert_count(int count
)
307 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
,
308 str(LANG_OFF_ABORT
));
311 /* Add specified track into playlist. Callback from directory insert */
312 static int add_track_to_playlist(char* filename
, void* context
)
314 struct add_track_context
* c
= (struct add_track_context
*) context
;
316 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
321 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
322 display_insert_count(c
->count
);
327 /* Add "sel" file into specified "playlist". How to insert depends on type
329 static int add_to_playlist(const char* playlist
, char* sel
, int sel_attr
)
334 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
);
338 /* In case we're in the playlist directory */
341 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
343 /* append the selected file */
344 if (fdprintf(fd
, "%s\n", sel
) > 0)
347 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
349 /* append playlist */
353 if(strcasecmp(playlist
, sel
) == 0)
356 f
= open(sel
, O_RDONLY
);
366 n
= read(f
, buf
, sizeof(buf
));
370 if (write(fd
, buf
, n
) < 0)
381 else if (sel_attr
& ATTR_DIRECTORY
)
383 /* search directory for tracks and append to playlist */
384 bool recurse
= false;
386 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION
),
389 struct text_message message
={lines
, 2};
390 struct add_track_context context
;
392 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
393 recurse
= (bool)global_settings
.recursive_dir_insert
;
396 /* Ask if user wants to recurse directory */
397 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
403 display_insert_count(0);
405 result
= playlist_directory_tracksearch(sel
, recurse
,
406 add_track_to_playlist
, &context
);
408 display_insert_count(context
.count
);
416 bool catalog_view_playlists(void)
418 if (initialize_catalog() == -1)
421 if (display_playlists(NULL
, true) == -1)
427 bool catalog_add_to_a_playlist(char* sel
, int sel_attr
, bool new_playlist
)
429 char playlist
[MAX_PATH
];
431 if (initialize_catalog() == -1)
437 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
438 if (kbd_input(playlist
, MAX_PATH
))
441 len
= strlen(playlist
);
443 if(len
> 4 && !strcasecmp(&playlist
[len
-4], ".m3u"))
444 strcat(playlist
, "8");
445 else if(len
<= 5 || strcasecmp(&playlist
[len
-5], ".m3u8"))
446 strcat(playlist
, ".m3u8");
450 if (display_playlists(playlist
, false) == -1)
454 if (add_to_playlist(playlist
, sel
, sel_attr
) == 0)
456 strncpy(most_recent_playlist
, playlist
+playlist_dir_length
+1,
457 sizeof(most_recent_playlist
));