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"
35 #include "filefuncs.h"
42 #include "filetypes.h"
44 #include "playlist_catalog.h"
47 /* Use for recursive directory search */
48 struct add_track_context
{
53 /* keep track of most recently used playlist */
54 static char most_recent_playlist
[MAX_PATH
];
56 /* directory where our playlists our stored */
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;
68 bool default_dir
= true;
70 /* directory config is of the format: "dir: /path/to/dir" */
71 if (global_settings
.playlist_catalog_dir
[0] &&
72 strcmp(global_settings
.playlist_catalog_dir
,
73 PLAYLIST_CATALOG_DEFAULT_DIR
))
75 strcpy(playlist_dir
, global_settings
.playlist_catalog_dir
);
79 /* fall back to default directory if no or invalid config */
82 strcpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
);
83 if (!dir_exists(playlist_dir
))
87 playlist_dir_length
= strlen(playlist_dir
);
89 if (dir_exists(playlist_dir
))
91 playlist_dir_exists
= true;
92 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
97 if (!playlist_dir_exists
)
99 if (mkdir(playlist_dir
) < 0) {
100 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
104 playlist_dir_exists
= true;
105 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
113 /* Display all playlists in catalog. Selected "playlist" is returned.
114 If "view" mode is set then we're not adding anything into playlist. */
115 static int display_playlists(char* playlist
, bool view
)
117 struct browse_context browse
;
118 char selected_playlist
[MAX_PATH
];
121 browse_context_init(&browse
, SHOW_M3U
,
122 BROWSE_SELECTONLY
|(view
? 0: BROWSE_NO_CONTEXT_MENU
),
123 str(LANG_CATALOG
), NOICON
,
124 playlist_dir
, most_recent_playlist
);
126 browse
.buf
= selected_playlist
;
127 browse
.bufsize
= sizeof(selected_playlist
);
129 rockbox_browse(&browse
);
131 if (browse
.flags
& BROWSE_SELECTED
)
133 strlcpy(most_recent_playlist
, selected_playlist
+playlist_dir_length
+1,
134 sizeof(most_recent_playlist
));
138 char *filename
= strrchr(selected_playlist
, '/')+1;
139 /* In view mode, selecting a playlist starts playback */
140 ft_play_playlist(selected_playlist
, playlist_dir
, filename
);
146 strlcpy(playlist
, selected_playlist
, MAX_PATH
);
153 /* display number of tracks inserted into playlists. Used for directory
155 static void display_insert_count(int count
)
157 static long talked_tick
= 0;
158 if(global_settings
.talk_menu
&& count
&&
159 (talked_tick
== 0 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
)))
161 talked_tick
= current_tick
;
162 talk_number(count
, false);
163 talk_id(LANG_PLAYLIST_INSERT_COUNT
, true);
166 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
, str(LANG_OFF_ABORT
));
169 /* Add specified track into playlist. Callback from directory insert */
170 static int add_track_to_playlist(char* filename
, void* context
)
172 struct add_track_context
* c
= (struct add_track_context
*) context
;
174 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
179 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
180 display_insert_count(c
->count
);
185 /* Add "sel" file into specified "playlist". How to insert depends on type
187 static int add_to_playlist(const char* playlist
, bool new_playlist
,
188 const char* sel
, int sel_attr
)
194 fd
= open_utf8(playlist
, O_CREAT
|O_WRONLY
|O_TRUNC
);
196 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
, 0666);
201 /* In case we're in the playlist directory */
204 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
206 /* append the selected file */
207 if (fdprintf(fd
, "%s\n", sel
) > 0)
210 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
212 /* append playlist */
216 if(strcasecmp(playlist
, sel
) == 0)
219 f
= open_utf8(sel
, O_RDONLY
);
223 i
= lseek(f
, 0, SEEK_CUR
);
229 n
= read(f
, buf
, sizeof(buf
));
233 if (write(fd
, buf
, n
) < 0)
244 else if (sel_attr
& ATTR_DIRECTORY
)
246 /* search directory for tracks and append to playlist */
247 bool recurse
= false;
248 const char *lines
[] = {
249 ID2P(LANG_RECURSE_DIRECTORY_QUESTION
), sel
};
250 const struct text_message message
={lines
, 2};
251 struct add_track_context context
;
253 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
254 recurse
= (bool)global_settings
.recursive_dir_insert
;
257 /* Ask if user wants to recurse directory */
258 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
264 display_insert_count(0);
266 result
= playlist_directory_tracksearch(sel
, recurse
,
267 add_track_to_playlist
, &context
);
269 display_insert_count(context
.count
);
276 static bool in_cat_viewer
= false;
277 bool catalog_view_playlists(void)
283 if (initialize_catalog() == -1)
286 in_cat_viewer
= true;
287 retval
= (display_playlists(NULL
, true) != -1);
288 in_cat_viewer
= false;
292 static bool in_add_to_playlist
= false;
293 bool catalog_add_to_a_playlist(const char* sel
, int sel_attr
,
294 bool new_playlist
, char *m3u8name
)
297 char playlist
[MAX_PATH
];
298 if (in_add_to_playlist
)
301 if (initialize_catalog() == -1)
307 if (m3u8name
== NULL
)
309 snprintf(playlist
, MAX_PATH
, "%s/", playlist_dir
);
310 if (kbd_input(playlist
, MAX_PATH
))
314 strcpy(playlist
, m3u8name
);
316 len
= strlen(playlist
);
318 if(len
> 4 && !strcasecmp(&playlist
[len
-4], ".m3u"))
319 strcat(playlist
, "8");
320 else if(len
<= 5 || strcasecmp(&playlist
[len
-5], ".m3u8"))
321 strcat(playlist
, ".m3u8");
325 in_add_to_playlist
= true;
326 result
= display_playlists(playlist
, false);
327 in_add_to_playlist
= false;
333 if (add_to_playlist(playlist
, new_playlist
, sel
, sel_attr
) == 0)