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"
46 #include "playlist_viewer.h"
49 /* Use for recursive directory search */
50 struct add_track_context
{
55 /* keep track of most recently used playlist */
56 static char most_recent_playlist
[MAX_PATH
];
58 /* directory where our playlists our stored */
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 bool initialized
= false;
65 static int initialize_catalog(void)
70 bool default_dir
= true;
72 /* directory config is of the format: "dir: /path/to/dir" */
73 if (global_settings
.playlist_catalog_dir
[0] &&
74 strcmp(global_settings
.playlist_catalog_dir
,
75 PLAYLIST_CATALOG_DEFAULT_DIR
))
77 strcpy(playlist_dir
, global_settings
.playlist_catalog_dir
);
81 /* fall back to default directory if no or invalid config */
84 strcpy(playlist_dir
, PLAYLIST_CATALOG_DEFAULT_DIR
);
85 if (!dir_exists(playlist_dir
))
89 playlist_dir_length
= strlen(playlist_dir
);
91 if (dir_exists(playlist_dir
))
93 playlist_dir_exists
= true;
94 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
99 if (!playlist_dir_exists
)
101 if (mkdir(playlist_dir
) < 0) {
102 splashf(HZ
*2, ID2P(LANG_CATALOG_NO_DIRECTORY
), playlist_dir
);
106 playlist_dir_exists
= true;
107 memset(most_recent_playlist
, 0, sizeof(most_recent_playlist
));
115 void catalog_set_directory(const char* directory
)
117 if (directory
== NULL
)
119 global_settings
.playlist_catalog_dir
[0] = '\0';
123 strcpy(global_settings
.playlist_catalog_dir
, directory
);
126 initialize_catalog();
129 const char* catalog_get_directory(void)
131 if (initialize_catalog() == -1)
136 /* Display all playlists in catalog. Selected "playlist" is returned.
137 If "view" mode is set then we're not adding anything into playlist. */
138 static int display_playlists(char* playlist
, bool view
)
140 struct browse_context browse
;
141 char selected_playlist
[MAX_PATH
];
144 browse_context_init(&browse
, SHOW_M3U
,
145 BROWSE_SELECTONLY
|(view
? 0: BROWSE_NO_CONTEXT_MENU
),
146 str(LANG_CATALOG
), NOICON
,
147 playlist_dir
, most_recent_playlist
);
149 browse
.buf
= selected_playlist
;
150 browse
.bufsize
= sizeof(selected_playlist
);
153 browse
.flags
&= ~BROWSE_SELECTED
;
154 rockbox_browse(&browse
);
156 if (browse
.flags
& BROWSE_SELECTED
)
158 strlcpy(most_recent_playlist
, selected_playlist
+playlist_dir_length
+1,
159 sizeof(most_recent_playlist
));
164 if (!bookmark_autoload(selected_playlist
))
166 if (playlist_viewer_ex(selected_playlist
) == PLAYLIST_VIEWER_CANCEL
)
174 strlcpy(playlist
, selected_playlist
, MAX_PATH
);
181 /* display number of tracks inserted into playlists. Used for directory
183 static void display_insert_count(int count
)
185 static long talked_tick
= 0;
186 if(global_settings
.talk_menu
&& count
&&
187 (talked_tick
== 0 || TIME_AFTER(current_tick
, talked_tick
+5*HZ
)))
189 talked_tick
= current_tick
;
190 talk_number(count
, false);
191 talk_id(LANG_PLAYLIST_INSERT_COUNT
, true);
194 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT
), count
, str(LANG_OFF_ABORT
));
197 /* Add specified track into playlist. Callback from directory insert */
198 static int add_track_to_playlist(char* filename
, void* context
)
200 struct add_track_context
* c
= (struct add_track_context
*) context
;
202 if (fdprintf(c
->fd
, "%s\n", filename
) <= 0)
207 if (((c
->count
)%PLAYLIST_DISPLAY_COUNT
) == 0)
208 display_insert_count(c
->count
);
213 /* Add "sel" file into specified "playlist". How to insert depends on type
215 static int add_to_playlist(const char* playlist
, bool new_playlist
,
216 const char* sel
, int sel_attr
)
222 fd
= open_utf8(playlist
, O_CREAT
|O_WRONLY
|O_TRUNC
);
224 fd
= open(playlist
, O_CREAT
|O_WRONLY
|O_APPEND
, 0666);
229 /* In case we're in the playlist directory */
232 if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_AUDIO
)
234 /* append the selected file */
235 if (fdprintf(fd
, "%s\n", sel
) > 0)
238 else if ((sel_attr
& FILE_ATTR_MASK
) == FILE_ATTR_M3U
)
240 /* append playlist */
244 if(strcasecmp(playlist
, sel
) == 0)
247 f
= open_utf8(sel
, O_RDONLY
);
251 i
= lseek(f
, 0, SEEK_CUR
);
257 n
= read(f
, buf
, sizeof(buf
));
261 if (write(fd
, buf
, n
) < 0)
272 else if (sel_attr
& ATTR_DIRECTORY
)
274 /* search directory for tracks and append to playlist */
275 bool recurse
= false;
276 const char *lines
[] = {
277 ID2P(LANG_RECURSE_DIRECTORY_QUESTION
), sel
};
278 const struct text_message message
={lines
, 2};
279 struct add_track_context context
;
281 if (global_settings
.recursive_dir_insert
!= RECURSE_ASK
)
282 recurse
= (bool)global_settings
.recursive_dir_insert
;
285 /* Ask if user wants to recurse directory */
286 recurse
= (gui_syncyesno_run(&message
, NULL
, NULL
)==YESNO_YES
);
292 display_insert_count(0);
294 result
= playlist_directory_tracksearch(sel
, recurse
,
295 add_track_to_playlist
, &context
);
297 display_insert_count(context
.count
);
304 static bool in_cat_viewer
= false;
305 bool catalog_view_playlists(void)
311 if (initialize_catalog() == -1)
314 in_cat_viewer
= true;
315 retval
= (display_playlists(NULL
, true) != -1);
316 in_cat_viewer
= false;
320 static bool in_add_to_playlist
= false;
321 bool catalog_add_to_a_playlist(const char* sel
, int sel_attr
,
322 bool new_playlist
, char *m3u8name
)
325 char playlist
[MAX_PATH
];
326 if (in_add_to_playlist
)
329 if (initialize_catalog() == -1)
335 if (m3u8name
== NULL
)
337 /*If sel is a folder, we prefill the text field with its name*/
338 const char *name
= strrchr(sel
, '/');
339 snprintf(playlist
, MAX_PATH
, "%s/%s.m3u8",
341 (name
!=NULL
&& (sel_attr
& ATTR_DIRECTORY
))?name
+1:"");
344 strcpy(playlist
, m3u8name
);
346 len
= strlen(playlist
);
348 if(len
> 4 && !strcasecmp(&playlist
[len
-4], ".m3u"))
349 strcat(playlist
, "8");
350 else if(len
<= 5 || strcasecmp(&playlist
[len
-5], ".m3u8"))
351 strcat(playlist
, ".m3u8");
353 if (kbd_input(playlist
, MAX_PATH
))
358 in_add_to_playlist
= true;
359 result
= display_playlists(playlist
, false);
360 in_add_to_playlist
= false;
366 if (add_to_playlist(playlist
, new_playlist
, sel
, sel_attr
) == 0)