simple fix for FS#7274 - selected item might not be shown when a list is drawn in...
[Rockbox.git] / apps / playlist_catalog.c
blob5ff2bddaabf962816e454079a8f703092fabca32
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"
42 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
43 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
44 #define MAX_PLAYLISTS 400
45 #define PLAYLIST_DISPLAY_COUNT 10
47 /* Use for recursive directory search */
48 struct add_track_context {
49 int fd;
50 int count;
53 /* keep track of most recently used playlist */
54 static char most_recent_playlist[MAX_PATH];
56 /* directory where our playlists our stored (configured in
57 PLAYLIST_CATALOG_CFG) */
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 int f;
70 DIR* dir;
71 bool default_dir = true;
73 f = open(PLAYLIST_CATALOG_CFG, O_RDONLY);
74 if (f >= 0)
76 char buf[MAX_PATH+5];
78 while (read_line(f, buf, sizeof(buf)))
80 char* name;
81 char* value;
83 /* directory config is of the format: "dir: /path/to/dir" */
84 if (settings_parseline(buf, &name, &value) &&
85 !strncasecmp(name, "dir:", strlen(name)) &&
86 strlen(value) > 0)
88 strncpy(playlist_dir, value, strlen(value));
89 default_dir = false;
93 close(f);
96 /* fall back to default directory if no or invalid config */
97 if (default_dir)
98 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
99 sizeof(playlist_dir));
101 playlist_dir_length = strlen(playlist_dir);
103 dir = opendir(playlist_dir);
104 if (dir)
106 playlist_dir_exists = true;
107 closedir(dir);
108 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
109 initialized = true;
113 if (!playlist_dir_exists)
115 if (mkdir(playlist_dir) < 0) {
116 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
117 playlist_dir);
118 return -1;
120 else {
121 playlist_dir_exists = true;
122 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
123 initialized = true;
127 return 0;
129 /* Use the filetree functions to retrieve the list of playlists in the
130 directory */
131 static int create_playlist_list(char** playlists, int num_items,
132 int* num_playlists)
134 int result = -1;
135 int num_files = 0;
136 int index = 0;
137 int i;
138 bool most_recent = false;
139 struct entry *files;
140 struct tree_context* tc = tree_get_context();
141 int dirfilter = *(tc->dirfilter);
143 *num_playlists = 0;
145 /* use the tree browser dircache to load only playlists */
146 *(tc->dirfilter) = SHOW_PLAYLIST;
148 if (ft_load(tc, playlist_dir) < 0)
150 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
151 playlist_dir);
152 goto exit;
155 files = (struct entry*) tc->dircache;
156 num_files = tc->filesindir;
158 /* we've overwritten the dircache so tree browser will need to be
159 reloaded */
160 reload_directory();
162 /* if it exists, most recent playlist will always be index 0 */
163 if (most_recent_playlist[0] != '\0')
165 index = 1;
166 most_recent = true;
169 for (i=0; i<num_files && index<num_items; i++)
171 if (files[i].attr & FILE_ATTR_M3U)
173 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
174 sizeof(most_recent_playlist)))
176 playlists[0] = files[i].name;
177 most_recent = false;
179 else
181 playlists[index] = files[i].name;
182 index++;
187 *num_playlists = index;
189 /* we couldn't find the most recent playlist, shift all playlists up */
190 if (most_recent)
192 for (i=0; i<index-1; i++)
193 playlists[i] = playlists[i+1];
195 (*num_playlists)--;
197 most_recent_playlist[0] = '\0';
200 result = 0;
202 exit:
203 *(tc->dirfilter) = dirfilter;
204 return result;
207 /* Callback for gui_synclist */
208 static char* playlist_callback_name(int selected_item, void* data,
209 char* buffer)
211 char** playlists = (char**) data;
213 strncpy(buffer, playlists[selected_item], MAX_PATH);
215 return buffer;
218 /* Display all playlists in catalog. Selected "playlist" is returned.
219 If "view" mode is set then we're not adding anything into playlist. */
220 static int display_playlists(char* playlist, bool view)
222 int result = -1;
223 int num_playlists = 0;
224 bool exit = false;
225 char temp_buf[MAX_PATH];
226 char* playlists[MAX_PLAYLISTS];
227 struct gui_synclist playlist_lists;
229 if (create_playlist_list(playlists, sizeof(playlists),
230 &num_playlists) != 0)
231 return -1;
233 if (num_playlists <= 0)
235 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
236 return -1;
239 if (!playlist)
240 playlist = temp_buf;
242 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
243 false, 1);
244 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
245 gui_synclist_draw(&playlist_lists);
247 while (!exit)
249 int button = get_action(CONTEXT_LIST,HZ/2);
250 char* sel_file;
252 gui_synclist_do_button(&playlist_lists, button,LIST_WRAP_UNLESS_HELD);
254 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
256 switch (button)
258 case ACTION_STD_CANCEL:
259 exit = true;
260 break;
262 case ACTION_STD_OK:
263 if (view)
265 /* In view mode, selecting a playlist starts playback */
266 if (playlist_create(playlist_dir, sel_file) != -1)
268 if (global_settings.playlist_shuffle)
269 playlist_shuffle(current_tick, -1);
270 playlist_start(0, 0);
273 else
275 /* we found the playlist we want to add to */
276 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
277 sel_file);
280 result = 0;
281 exit = true;
282 break;
284 case ACTION_STD_CONTEXT:
285 /* context menu only available in view mode */
286 if (view)
288 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
289 sel_file);
291 if (onplay(playlist, FILE_ATTR_M3U,
292 CONTEXT_TREE) != ONPLAY_OK)
294 result = 0;
295 exit = true;
297 else
298 gui_synclist_draw(&playlist_lists);
300 break;
302 case ACTION_NONE:
303 gui_syncstatusbar_draw(&statusbars, false);
304 break;
306 default:
307 if(default_event_handler(button) == SYS_USB_CONNECTED)
309 result = -1;
310 exit = true;
312 break;
315 action_signalscreenchange();
316 return result;
319 /* display number of tracks inserted into playlists. Used for directory
320 insert */
321 static void display_insert_count(int count)
323 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
324 #if CONFIG_KEYPAD == PLAYER_PAD
325 str(LANG_STOP_ABORT)
326 #else
327 str(LANG_OFF_ABORT)
328 #endif
332 /* Add specified track into playlist. Callback from directory insert */
333 static int add_track_to_playlist(char* filename, void* context)
335 struct add_track_context* c = (struct add_track_context*) context;
337 if (fdprintf(c->fd, "%s\n", filename) <= 0)
338 return -1;
340 (c->count)++;
342 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
343 display_insert_count(c->count);
345 return 0;
348 /* Add "sel" file into specified "playlist". How to insert depends on type
349 of file */
350 static int add_to_playlist(const char* playlist, char* sel, int sel_attr)
352 int fd;
353 int result = -1;
355 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
356 if(fd < 0)
357 return result;
359 /* In case we're in the playlist directory */
360 reload_directory();
362 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
364 /* append the selected file */
365 if (fdprintf(fd, "%s\n", sel) > 0)
366 result = 0;
368 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
370 /* append playlist */
371 int f, fs, i;
372 char buf[1024];
374 if(strcasecmp(playlist, sel) == 0)
375 goto exit;
377 f = open(sel, O_RDONLY);
378 if (f < 0)
379 goto exit;
381 fs = filesize(f);
383 for (i=0; i<fs;)
385 int n;
387 n = read(f, buf, sizeof(buf));
388 if (n < 0)
389 break;
391 if (write(fd, buf, n) < 0)
392 break;
394 i += n;
397 if (i >= fs)
398 result = 0;
400 close(f);
402 else if (sel_attr & ATTR_DIRECTORY)
404 /* search directory for tracks and append to playlist */
405 bool recurse = false;
406 char *lines[] = {
407 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION),
410 struct text_message message={lines, 2};
411 struct add_track_context context;
413 if (global_settings.recursive_dir_insert != RECURSE_ASK)
414 recurse = (bool)global_settings.recursive_dir_insert;
415 else
417 /* Ask if user wants to recurse directory */
418 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
421 context.fd = fd;
422 context.count = 0;
424 display_insert_count(0);
426 result = playlist_directory_tracksearch(sel, recurse,
427 add_track_to_playlist, &context);
429 display_insert_count(context.count);
432 exit:
433 close(fd);
434 return result;
437 bool catalog_view_playlists(void)
439 if (initialize_catalog() == -1)
440 return false;
442 if (display_playlists(NULL, true) == -1)
443 return false;
445 return true;
448 bool catalog_add_to_a_playlist(char* sel, int sel_attr, bool new_playlist)
450 char playlist[MAX_PATH];
452 if (initialize_catalog() == -1)
453 return false;
455 if (new_playlist)
457 size_t len;
458 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
459 if (kbd_input(playlist, MAX_PATH))
460 return false;
462 len = strlen(playlist);
464 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
465 strcat(playlist, "8");
466 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
467 strcat(playlist, ".m3u8");
469 else
471 if (display_playlists(playlist, false) == -1)
472 return false;
475 if (add_to_playlist(playlist, sel, sel_attr) == 0)
477 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
478 sizeof(most_recent_playlist));
479 return true;
481 else
482 return false;