Move declaration of button_int and clickwheel_int to the proper header file instead...
[Rockbox.git] / apps / playlist_catalog.c
blobf23036ddeae9cf9daf2624a169c70eabb8c065c8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "action.h"
27 #include "dir.h"
28 #include "file.h"
29 #include "filetree.h"
30 #include "kernel.h"
31 #include "keyboard.h"
32 #include "lang.h"
33 #include "list.h"
34 #include "misc.h"
35 #include "onplay.h"
36 #include "playlist.h"
37 #include "settings.h"
38 #include "splash.h"
39 #include "sprintf.h"
40 #include "tree.h"
41 #include "yesno.h"
42 #include "filetypes.h"
43 #include "debug.h"
44 #include "playlist_catalog.h"
45 #include "statusbar.h"
47 #define MAX_PLAYLISTS 400
48 #define PLAYLIST_DISPLAY_COUNT 10
50 /* Use for recursive directory search */
51 struct add_track_context {
52 int fd;
53 int count;
56 /* keep track of most recently used playlist */
57 static char most_recent_playlist[MAX_PATH];
59 /* directory where our playlists our stored */
60 static char playlist_dir[MAX_PATH];
61 static int playlist_dir_length;
62 static bool playlist_dir_exists = false;
64 /* Retrieve playlist directory from config file and verify it exists */
65 static int initialize_catalog(void)
67 static bool initialized = false;
69 if (!initialized)
71 bool default_dir = true;
73 /* directory config is of the format: "dir: /path/to/dir" */
74 if (global_settings.playlist_catalog_dir[0])
76 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
77 default_dir = false;
80 /* fall back to default directory if no or invalid config */
81 if (default_dir)
82 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
83 sizeof(playlist_dir));
85 playlist_dir_length = strlen(playlist_dir);
87 if (dir_exists(playlist_dir))
89 playlist_dir_exists = true;
90 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
91 initialized = true;
95 if (!playlist_dir_exists)
97 if (mkdir(playlist_dir) < 0) {
98 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
99 playlist_dir);
100 return -1;
102 else {
103 playlist_dir_exists = true;
104 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
105 initialized = true;
109 return 0;
111 /* Use the filetree functions to retrieve the list of playlists in the
112 directory */
113 static int create_playlist_list(char** playlists, int num_items,
114 int* num_playlists)
116 int result = -1;
117 int num_files = 0;
118 int index = 0;
119 int i;
120 bool most_recent = false;
121 struct entry *files;
122 struct tree_context* tc = tree_get_context();
123 int dirfilter = *(tc->dirfilter);
125 *num_playlists = 0;
127 /* use the tree browser dircache to load only playlists */
128 *(tc->dirfilter) = SHOW_PLAYLIST;
130 if (ft_load(tc, playlist_dir) < 0)
132 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
133 playlist_dir);
134 goto exit;
137 files = (struct entry*) tc->dircache;
138 num_files = tc->filesindir;
140 /* we've overwritten the dircache so tree browser will need to be
141 reloaded */
142 reload_directory();
144 /* if it exists, most recent playlist will always be index 0 */
145 if (most_recent_playlist[0] != '\0')
147 index = 1;
148 most_recent = true;
151 for (i=0; i<num_files && index<num_items; i++)
153 if (files[i].attr & FILE_ATTR_M3U)
155 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
156 sizeof(most_recent_playlist)))
158 playlists[0] = files[i].name;
159 most_recent = false;
161 else
163 playlists[index] = files[i].name;
164 index++;
169 *num_playlists = index;
171 /* we couldn't find the most recent playlist, shift all playlists up */
172 if (most_recent)
174 for (i=0; i<index-1; i++)
175 playlists[i] = playlists[i+1];
177 (*num_playlists)--;
179 most_recent_playlist[0] = '\0';
182 result = 0;
184 exit:
185 *(tc->dirfilter) = dirfilter;
186 return result;
189 /* Callback for gui_synclist */
190 static char* playlist_callback_name(int selected_item, void* data,
191 char* buffer, size_t buffer_len)
193 char** playlists = (char**) data;
195 strncpy(buffer, playlists[selected_item], buffer_len);
197 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
198 || (global_settings.show_filename_ext == 3
199 && global_settings.dirfilter == 0)))
201 char* dot = strrchr(buffer, '.');
203 if (dot != NULL)
205 *dot = '\0';
209 return buffer;
212 /* Display all playlists in catalog. Selected "playlist" is returned.
213 If "view" mode is set then we're not adding anything into playlist. */
214 static int display_playlists(char* playlist, bool view)
216 int result = -1;
217 int num_playlists = 0;
218 bool exit = false;
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)
225 return -1;
227 if (num_playlists <= 0)
229 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
230 return -1;
233 if (!playlist)
234 playlist = temp_buf;
236 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
237 false, 1, NULL);
238 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
239 gui_synclist_draw(&playlist_lists);
241 while (!exit)
243 int button = get_action(CONTEXT_LIST,HZ/2);
244 char* sel_file;
246 gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
248 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
250 switch (button)
252 case ACTION_STD_CANCEL:
253 exit = true;
254 break;
256 case ACTION_STD_OK:
257 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
259 if (view)
261 /* In view mode, selecting a playlist starts playback */
262 ft_play_playlist(playlist, playlist_dir, sel_file);
265 result = 0;
266 exit = true;
267 break;
269 case ACTION_STD_CONTEXT:
270 /* context menu only available in view mode */
271 if (view)
273 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
274 sel_file);
276 if (onplay(playlist, FILE_ATTR_M3U,
277 CONTEXT_TREE) != ONPLAY_OK)
279 result = 0;
280 exit = true;
282 else
283 gui_synclist_draw(&playlist_lists);
285 break;
287 case ACTION_NONE:
288 gui_syncstatusbar_draw(&statusbars, false);
289 break;
291 default:
292 if(default_event_handler(button) == SYS_USB_CONNECTED)
294 result = -1;
295 exit = true;
297 break;
300 return result;
303 /* display number of tracks inserted into playlists. Used for directory
304 insert */
305 static void display_insert_count(int count)
307 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
308 str(LANG_OFF_ABORT));
311 /* Add specified track into playlist. Callback from directory insert */
312 static int add_track_to_playlist(char* filename, void* context)
314 struct add_track_context* c = (struct add_track_context*) context;
316 if (fdprintf(c->fd, "%s\n", filename) <= 0)
317 return -1;
319 (c->count)++;
321 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
322 display_insert_count(c->count);
324 return 0;
327 /* Add "sel" file into specified "playlist". How to insert depends on type
328 of file */
329 static int add_to_playlist(const char* playlist, bool new_playlist,
330 const char* sel, int sel_attr)
332 int fd;
333 int result = -1;
334 int flags = O_CREAT|O_WRONLY;
336 if (new_playlist)
337 flags |= O_TRUNC;
338 else
339 flags |= O_APPEND;
341 fd = open(playlist, flags);
342 if(fd < 0)
343 return result;
345 /* In case we're in the playlist directory */
346 reload_directory();
348 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
350 /* append the selected file */
351 if (fdprintf(fd, "%s\n", sel) > 0)
352 result = 0;
354 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
356 /* append playlist */
357 int f, fs, i;
358 char buf[1024];
360 if(strcasecmp(playlist, sel) == 0)
361 goto exit;
363 f = open(sel, O_RDONLY);
364 if (f < 0)
365 goto exit;
367 fs = filesize(f);
369 for (i=0; i<fs;)
371 int n;
373 n = read(f, buf, sizeof(buf));
374 if (n < 0)
375 break;
377 if (write(fd, buf, n) < 0)
378 break;
380 i += n;
383 if (i >= fs)
384 result = 0;
386 close(f);
388 else if (sel_attr & ATTR_DIRECTORY)
390 /* search directory for tracks and append to playlist */
391 bool recurse = false;
392 const char *lines[] = {
393 str(LANG_RECURSE_DIRECTORY_QUESTION), sel};
394 const struct text_message message={lines, 2};
395 struct add_track_context context;
397 if (global_settings.recursive_dir_insert != RECURSE_ASK)
398 recurse = (bool)global_settings.recursive_dir_insert;
399 else
401 /* Ask if user wants to recurse directory */
402 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
405 context.fd = fd;
406 context.count = 0;
408 display_insert_count(0);
410 result = playlist_directory_tracksearch(sel, recurse,
411 add_track_to_playlist, &context);
413 display_insert_count(context.count);
416 exit:
417 close(fd);
418 return result;
421 bool catalog_view_playlists(void)
423 if (initialize_catalog() == -1)
424 return false;
426 if (display_playlists(NULL, true) == -1)
427 return false;
429 return true;
432 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
433 bool new_playlist, char *m3u8name)
435 char playlist[MAX_PATH];
437 if (initialize_catalog() == -1)
438 return false;
440 if (new_playlist)
442 size_t len;
443 if (m3u8name == NULL)
445 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
446 if (kbd_input(playlist, MAX_PATH))
447 return false;
449 else
450 strcpy(playlist, m3u8name);
452 len = strlen(playlist);
454 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
455 strcat(playlist, "8");
456 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
457 strcat(playlist, ".m3u8");
459 else
461 if (display_playlists(playlist, false) == -1)
462 return false;
465 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
467 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
468 sizeof(most_recent_playlist));
469 return true;
471 else
472 return false;