Explicitly set the dialog's result code for TTS / Encoder windows. Fixes an issue...
[Rockbox.git] / apps / playlist_catalog.c
blobdfcfd0c36363b6f205aec30985cc6e37c300c702
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "action.h"
25 #include "dir.h"
26 #include "file.h"
27 #include "filetree.h"
28 #include "kernel.h"
29 #include "keyboard.h"
30 #include "lang.h"
31 #include "list.h"
32 #include "misc.h"
33 #include "onplay.h"
34 #include "playlist.h"
35 #include "settings.h"
36 #include "splash.h"
37 #include "sprintf.h"
38 #include "tree.h"
39 #include "yesno.h"
40 #include "filetypes.h"
41 #include "debug.h"
42 #include "playlist_catalog.h"
43 #include "statusbar.h"
45 #define MAX_PLAYLISTS 400
46 #define PLAYLIST_DISPLAY_COUNT 10
48 /* Use for recursive directory search */
49 struct add_track_context {
50 int fd;
51 int count;
54 /* keep track of most recently used playlist */
55 static char most_recent_playlist[MAX_PATH];
57 /* directory where our playlists our stored */
58 static char playlist_dir[MAX_PATH];
59 static int playlist_dir_length;
60 static bool playlist_dir_exists = false;
62 /* Retrieve playlist directory from config file and verify it exists */
63 static int initialize_catalog(void)
65 static bool initialized = false;
67 if (!initialized)
69 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);
75 default_dir = false;
78 /* fall back to default directory if no or invalid config */
79 if (default_dir)
80 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
81 sizeof(playlist_dir));
83 playlist_dir_length = strlen(playlist_dir);
85 if (dir_exists(playlist_dir))
87 playlist_dir_exists = true;
88 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
89 initialized = true;
93 if (!playlist_dir_exists)
95 if (mkdir(playlist_dir) < 0) {
96 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
97 playlist_dir);
98 return -1;
100 else {
101 playlist_dir_exists = true;
102 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
103 initialized = true;
107 return 0;
109 /* Use the filetree functions to retrieve the list of playlists in the
110 directory */
111 static int create_playlist_list(char** playlists, int num_items,
112 int* num_playlists)
114 int result = -1;
115 int num_files = 0;
116 int index = 0;
117 int i;
118 bool most_recent = false;
119 struct entry *files;
120 struct tree_context* tc = tree_get_context();
121 int dirfilter = *(tc->dirfilter);
123 *num_playlists = 0;
125 /* use the tree browser dircache to load only playlists */
126 *(tc->dirfilter) = SHOW_PLAYLIST;
128 if (ft_load(tc, playlist_dir) < 0)
130 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
131 playlist_dir);
132 goto exit;
135 files = (struct entry*) tc->dircache;
136 num_files = tc->filesindir;
138 /* we've overwritten the dircache so tree browser will need to be
139 reloaded */
140 reload_directory();
142 /* if it exists, most recent playlist will always be index 0 */
143 if (most_recent_playlist[0] != '\0')
145 index = 1;
146 most_recent = true;
149 for (i=0; i<num_files && index<num_items; i++)
151 if (files[i].attr & FILE_ATTR_M3U)
153 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
154 sizeof(most_recent_playlist)))
156 playlists[0] = files[i].name;
157 most_recent = false;
159 else
161 playlists[index] = files[i].name;
162 index++;
167 *num_playlists = index;
169 /* we couldn't find the most recent playlist, shift all playlists up */
170 if (most_recent)
172 for (i=0; i<index-1; i++)
173 playlists[i] = playlists[i+1];
175 (*num_playlists)--;
177 most_recent_playlist[0] = '\0';
180 result = 0;
182 exit:
183 *(tc->dirfilter) = dirfilter;
184 return result;
187 /* Callback for gui_synclist */
188 static char* playlist_callback_name(int selected_item, void* data,
189 char* buffer, size_t buffer_len)
191 char** playlists = (char**) data;
193 strncpy(buffer, playlists[selected_item], buffer_len);
195 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
196 || (global_settings.show_filename_ext == 3
197 && global_settings.dirfilter == 0)))
199 char* dot = strrchr(buffer, '.');
201 if (dot != NULL)
203 *dot = '\0';
207 return buffer;
210 /* Display all playlists in catalog. Selected "playlist" is returned.
211 If "view" mode is set then we're not adding anything into playlist. */
212 static int display_playlists(char* playlist, bool view)
214 int result = -1;
215 int num_playlists = 0;
216 bool exit = false;
217 char temp_buf[MAX_PATH];
218 char* playlists[MAX_PLAYLISTS];
219 struct gui_synclist playlist_lists;
221 if (create_playlist_list(playlists, sizeof(playlists),
222 &num_playlists) != 0)
223 return -1;
225 if (num_playlists <= 0)
227 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
228 return -1;
231 if (!playlist)
232 playlist = temp_buf;
234 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
235 false, 1, NULL);
236 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
237 gui_synclist_draw(&playlist_lists);
239 while (!exit)
241 int button = get_action(CONTEXT_LIST,HZ/2);
242 char* sel_file;
244 gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
246 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
248 switch (button)
250 case ACTION_STD_CANCEL:
251 exit = true;
252 break;
254 case ACTION_STD_OK:
255 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
257 if (view)
259 /* In view mode, selecting a playlist starts playback */
260 ft_play_playlist(playlist, playlist_dir, sel_file);
263 result = 0;
264 exit = true;
265 break;
267 case ACTION_STD_CONTEXT:
268 /* context menu only available in view mode */
269 if (view)
271 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
272 sel_file);
274 if (onplay(playlist, FILE_ATTR_M3U,
275 CONTEXT_TREE) != ONPLAY_OK)
277 result = 0;
278 exit = true;
280 else
281 gui_synclist_draw(&playlist_lists);
283 break;
285 case ACTION_NONE:
286 gui_syncstatusbar_draw(&statusbars, false);
287 break;
289 default:
290 if(default_event_handler(button) == SYS_USB_CONNECTED)
292 result = -1;
293 exit = true;
295 break;
298 return result;
301 /* display number of tracks inserted into playlists. Used for directory
302 insert */
303 static void display_insert_count(int count)
305 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
306 str(LANG_OFF_ABORT));
309 /* Add specified track into playlist. Callback from directory insert */
310 static int add_track_to_playlist(char* filename, void* context)
312 struct add_track_context* c = (struct add_track_context*) context;
314 if (fdprintf(c->fd, "%s\n", filename) <= 0)
315 return -1;
317 (c->count)++;
319 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
320 display_insert_count(c->count);
322 return 0;
325 /* Add "sel" file into specified "playlist". How to insert depends on type
326 of file */
327 static int add_to_playlist(const char* playlist, bool new_playlist,
328 const char* sel, int sel_attr)
330 int fd;
331 int result = -1;
332 int flags = O_CREAT|O_WRONLY;
334 if (new_playlist)
335 flags |= O_TRUNC;
336 else
337 flags |= O_APPEND;
339 fd = open(playlist, flags);
340 if(fd < 0)
341 return result;
343 /* In case we're in the playlist directory */
344 reload_directory();
346 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
348 /* append the selected file */
349 if (fdprintf(fd, "%s\n", sel) > 0)
350 result = 0;
352 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
354 /* append playlist */
355 int f, fs, i;
356 char buf[1024];
358 if(strcasecmp(playlist, sel) == 0)
359 goto exit;
361 f = open(sel, O_RDONLY);
362 if (f < 0)
363 goto exit;
365 fs = filesize(f);
367 for (i=0; i<fs;)
369 int n;
371 n = read(f, buf, sizeof(buf));
372 if (n < 0)
373 break;
375 if (write(fd, buf, n) < 0)
376 break;
378 i += n;
381 if (i >= fs)
382 result = 0;
384 close(f);
386 else if (sel_attr & ATTR_DIRECTORY)
388 /* search directory for tracks and append to playlist */
389 bool recurse = false;
390 const char *lines[] = {
391 str(LANG_RECURSE_DIRECTORY_QUESTION), sel};
392 const struct text_message message={lines, 2};
393 struct add_track_context context;
395 if (global_settings.recursive_dir_insert != RECURSE_ASK)
396 recurse = (bool)global_settings.recursive_dir_insert;
397 else
399 /* Ask if user wants to recurse directory */
400 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
403 context.fd = fd;
404 context.count = 0;
406 display_insert_count(0);
408 result = playlist_directory_tracksearch(sel, recurse,
409 add_track_to_playlist, &context);
411 display_insert_count(context.count);
414 exit:
415 close(fd);
416 return result;
419 bool catalog_view_playlists(void)
421 if (initialize_catalog() == -1)
422 return false;
424 if (display_playlists(NULL, true) == -1)
425 return false;
427 return true;
430 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
431 bool new_playlist, char *m3u8name)
433 char playlist[MAX_PATH];
435 if (initialize_catalog() == -1)
436 return false;
438 if (new_playlist)
440 size_t len;
441 if (m3u8name == NULL)
443 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
444 if (kbd_input(playlist, MAX_PATH))
445 return false;
447 else
448 strcpy(playlist, m3u8name);
450 len = strlen(playlist);
452 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
453 strcat(playlist, "8");
454 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
455 strcat(playlist, ".m3u8");
457 else
459 if (display_playlists(playlist, false) == -1)
460 return false;
463 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
465 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
466 sizeof(most_recent_playlist));
467 return true;
469 else
470 return false;