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 ****************************************************************************/
41 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
42 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
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 (configured in
56 PLAYLIST_CATALOG_CFG) */
57 static char playlist_dir
[MAX_PATH
];
58 static int playlist_dir_length
;
59 static bool playlist_dir_exists
= false;
61 /* Retrieve playlist directory from config file and verify it exists */
62 static int initialize_catalog(void)
64 static bool initialized
= false;
70 bool default_dir
= true;
72 f
= open(PLAYLIST_CATALOG_CFG
, O_RDONLY
);
77 while (read_line(f
, buf
, sizeof(buf
)))
82 /* directory config is of the format: "dir: /path/to/dir" */
83 if (settings_parseline(buf
, &name
, &value
) &&
84 !strncasecmp(name
, "dir:", strlen(name
)) &&
87 strncpy(playlist_dir
, value
, strlen(value
));
95 /* fall back to default directory if no or invalid config */
97 strncpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
,
98 sizeof(playlist_dir
));
100 playlist_dir_length
= strlen(playlist_dir
);
102 dir
= opendir(playlist_dir
);
105 playlist_dir_exists
= true;
107 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
112 if (!playlist_dir_exists
)
114 gui_syncsplash(HZ
*2, true, str(LANG_CATALOG_NO_DIRECTORY
),
122 /* Use the filetree functions to retrieve the list of playlists in the
124 static int create_playlist_list(char** playlists
, int num_items
,
131 bool most_recent
= false;
133 struct tree_context
* tc
= tree_get_context();
134 int dirfilter
= *(tc
->dirfilter
);
138 /* use the tree browser dircache to load only playlists */
139 *(tc
->dirfilter
) = SHOW_PLAYLIST
;
141 if (ft_load(tc
, playlist_dir
) < 0)
143 gui_syncsplash(HZ
*2, true, str(LANG_CATALOG_NO_DIRECTORY
),
148 files
= (struct entry
*) tc
->dircache
;
149 num_files
= tc
->filesindir
;
151 /* we've overwritten the dircache so tree browser will need to be
155 /* if it exists, most recent playlist will always be index 0 */
156 if (most_recent_playlist
[0] != '\0')
162 for (i
=0; i
<num_files
&& index
<num_items
; i
++)
164 if (files
[i
].attr
& TREE_ATTR_M3U
)
166 if (most_recent
&& !strncmp(files
[i
].name
, most_recent_playlist
,
167 sizeof(most_recent_playlist
)))
169 playlists
[0] = files
[i
].name
;
174 playlists
[index
] = files
[i
].name
;
180 *num_playlists
= index
;
182 /* we couldn't find the most recent playlist, shift all playlists up */
185 for (i
=0; i
<index
-1; i
++)
186 playlists
[i
] = playlists
[i
+1];
190 most_recent_playlist
[0] = '\0';
196 *(tc
->dirfilter
) = dirfilter
;
200 /* Callback for gui_synclist */
201 static char* playlist_callback_name(int selected_item
, void* data
,
204 char** playlists
= (char**) data
;
206 strncpy(buffer
, playlists
[selected_item
], MAX_PATH
);
211 /* Display all playlists in catalog. Selected "playlist" is returned.
212 If "view" mode is set then we're not adding anything into playlist. */
213 static int display_playlists(char* playlist
, bool view
)
216 int num_playlists
= 0;
217 int lastbutton
= BUTTON_NONE
;
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, true, 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
= button_get_w_tmo(HZ
/2);
246 gui_synclist_do_button(&playlist_lists
, button
);
248 sel_file
= playlists
[gui_synclist_get_sel_pos(&playlist_lists
)];
264 case TREE_ENTER
| BUTTON_REPEAT
:
271 if (((button
== TREE_RUN
)
272 #ifdef TREE_RC_RUN_PRE
273 || (button
== TREE_RC_RUN
))
274 && ((lastbutton
!= TREE_RC_RUN_PRE
)
276 && (lastbutton
!= TREE_RUN_PRE
)))
282 /* In view mode, selecting a playlist starts playback */
283 if (playlist_create(playlist_dir
, sel_file
) != -1)
285 if (global_settings
.playlist_shuffle
)
286 playlist_shuffle(current_tick
, -1);
287 playlist_start(0, 0);
292 /* we found the playlist we want to add to */
293 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
305 #ifdef TREE_RC_CONTEXT
306 case TREE_RC_CONTEXT
:
308 /* context menu only available in view mode */
311 snprintf(playlist
, MAX_PATH
, "%s/%s", playlist_dir
,
314 if (onplay(playlist
, TREE_ATTR_M3U
,
315 CONTEXT_TREE
) != ONPLAY_OK
)
321 gui_synclist_draw(&playlist_lists
);
326 gui_syncstatusbar_draw(&statusbars
, false);
330 if(default_event_handler(button
) == SYS_USB_CONNECTED
)
344 /* display number of tracks inserted into playlists. Used for directory
346 static void display_insert_count(int count
)
348 gui_syncsplash(0, true, str(LANG_PLAYLIST_INSERT_COUNT
), count
,
349 #if CONFIG_KEYPAD == PLAYER_PAD
357 /* Add specified track into playlist. Callback from directory insert */
358 static int add_track_to_playlist(char* filename
, void* context
)
360 struct add_track_context
* c
= (struct add_track_context
*) context
;
362 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
367 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
368 display_insert_count(c
->count
);
373 /* Add "sel" file into specified "playlist". How to insert depends on type
375 static int add_to_playlist(const char* playlist
, char* sel
, int sel_attr
)
380 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
);
384 /* In case we're in the playlist directory */
387 if ((sel_attr
& TREE_ATTR_MASK
) == TREE_ATTR_MPA
)
389 /* append the selected file */
390 if (fdprintf(fd
, "%s\n", sel
) > 0)
393 else if ((sel_attr
& TREE_ATTR_MASK
) == TREE_ATTR_M3U
)
395 /* append playlist */
399 if(strcasecmp(playlist
, sel
) == 0)
402 f
= open(sel
, O_RDONLY
);
412 n
= read(f
, buf
, sizeof(buf
));
416 if (write(fd
, buf
, n
) < 0)
427 else if (sel_attr
& ATTR_DIRECTORY
)
429 /* search directory for tracks and append to playlist */
430 bool recurse
= false;
432 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION
),
435 struct text_message message
={lines
, 2};
436 struct add_track_context context
;
438 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
439 recurse
= (bool)global_settings
.recursive_dir_insert
;
442 /* Ask if user wants to recurse directory */
443 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
449 display_insert_count(0);
451 result
= playlist_directory_tracksearch(sel
, recurse
,
452 add_track_to_playlist
, &context
);
454 display_insert_count(context
.count
);
462 bool catalog_view_playlists(void)
464 if (initialize_catalog() == -1)
467 if (display_playlists(NULL
, true) == -1)
473 bool catalog_add_to_a_playlist(char* sel
, int sel_attr
, bool new_playlist
)
475 char playlist
[MAX_PATH
];
477 if (initialize_catalog() == -1)
482 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
483 if (kbd_input(playlist
, MAX_PATH
))
486 if(strlen(playlist
) <= 4 ||
487 strcasecmp(&playlist
[strlen(playlist
)-4], ".m3u"))
488 strcat(playlist
, ".m3u");
492 if (display_playlists(playlist
, false) == -1)
496 if (add_to_playlist(playlist
, sel
, sel_attr
) == 0)
498 strncpy(most_recent_playlist
, playlist
+playlist_dir_length
+1,
499 sizeof(most_recent_playlist
));