Let qmake generate an install Makefile target to install the binary. Doesn't handle...
[Rockbox.git] / apps / playlist_catalog.c
blobac86f481095c7596f745556f6085f90a0402ccef
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"
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 */
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;
66 if (!initialized)
68 bool default_dir = true;
70 /* directory config is of the format: "dir: /path/to/dir" */
71 if (global_settings.playlist_catalog_dir[0])
73 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
74 default_dir = false;
77 /* fall back to default directory if no or invalid config */
78 if (default_dir)
79 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
80 sizeof(playlist_dir));
82 playlist_dir_length = strlen(playlist_dir);
84 if (dir_exists(playlist_dir))
86 playlist_dir_exists = true;
87 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
88 initialized = true;
92 if (!playlist_dir_exists)
94 if (mkdir(playlist_dir) < 0) {
95 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
96 playlist_dir);
97 return -1;
99 else {
100 playlist_dir_exists = true;
101 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
102 initialized = true;
106 return 0;
108 /* Use the filetree functions to retrieve the list of playlists in the
109 directory */
110 static int create_playlist_list(char** playlists, int num_items,
111 int* num_playlists)
113 int result = -1;
114 int num_files = 0;
115 int index = 0;
116 int i;
117 bool most_recent = false;
118 struct entry *files;
119 struct tree_context* tc = tree_get_context();
120 int dirfilter = *(tc->dirfilter);
122 *num_playlists = 0;
124 /* use the tree browser dircache to load only playlists */
125 *(tc->dirfilter) = SHOW_PLAYLIST;
127 if (ft_load(tc, playlist_dir) < 0)
129 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_DIRECTORY),
130 playlist_dir);
131 goto exit;
134 files = (struct entry*) tc->dircache;
135 num_files = tc->filesindir;
137 /* we've overwritten the dircache so tree browser will need to be
138 reloaded */
139 reload_directory();
141 /* if it exists, most recent playlist will always be index 0 */
142 if (most_recent_playlist[0] != '\0')
144 index = 1;
145 most_recent = true;
148 for (i=0; i<num_files && index<num_items; i++)
150 if (files[i].attr & FILE_ATTR_M3U)
152 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
153 sizeof(most_recent_playlist)))
155 playlists[0] = files[i].name;
156 most_recent = false;
158 else
160 playlists[index] = files[i].name;
161 index++;
166 *num_playlists = index;
168 /* we couldn't find the most recent playlist, shift all playlists up */
169 if (most_recent)
171 for (i=0; i<index-1; i++)
172 playlists[i] = playlists[i+1];
174 (*num_playlists)--;
176 most_recent_playlist[0] = '\0';
179 result = 0;
181 exit:
182 *(tc->dirfilter) = dirfilter;
183 return result;
186 /* Callback for gui_synclist */
187 static char* playlist_callback_name(int selected_item, void* data,
188 char* buffer, size_t buffer_len)
190 char** playlists = (char**) data;
192 strncpy(buffer, playlists[selected_item], buffer_len);
194 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
195 || (global_settings.show_filename_ext == 3
196 && global_settings.dirfilter == 0)))
198 char* dot = strrchr(buffer, '.');
200 if (dot != NULL)
202 *dot = '\0';
206 return buffer;
209 /* Display all playlists in catalog. Selected "playlist" is returned.
210 If "view" mode is set then we're not adding anything into playlist. */
211 static int display_playlists(char* playlist, bool view)
213 int result = -1;
214 int num_playlists = 0;
215 bool exit = false;
216 char temp_buf[MAX_PATH];
217 char* playlists[MAX_PLAYLISTS];
218 struct gui_synclist playlist_lists;
220 if (create_playlist_list(playlists, sizeof(playlists),
221 &num_playlists) != 0)
222 return -1;
224 if (num_playlists <= 0)
226 gui_syncsplash(HZ*2, str(LANG_CATALOG_NO_PLAYLISTS));
227 return -1;
230 if (!playlist)
231 playlist = temp_buf;
233 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
234 false, 1, NULL);
235 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
236 gui_synclist_draw(&playlist_lists);
238 while (!exit)
240 int button = get_action(CONTEXT_LIST,HZ/2);
241 char* sel_file;
243 gui_synclist_do_button(&playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
245 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
247 switch (button)
249 case ACTION_STD_CANCEL:
250 exit = true;
251 break;
253 case ACTION_STD_OK:
254 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
256 if (view)
258 /* In view mode, selecting a playlist starts playback */
259 ft_play_playlist(playlist, playlist_dir, sel_file);
262 result = 0;
263 exit = true;
264 break;
266 case ACTION_STD_CONTEXT:
267 /* context menu only available in view mode */
268 if (view)
270 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
271 sel_file);
273 if (onplay(playlist, FILE_ATTR_M3U,
274 CONTEXT_TREE) != ONPLAY_OK)
276 result = 0;
277 exit = true;
279 else
280 gui_synclist_draw(&playlist_lists);
282 break;
284 case ACTION_NONE:
285 gui_syncstatusbar_draw(&statusbars, false);
286 break;
288 default:
289 if(default_event_handler(button) == SYS_USB_CONNECTED)
291 result = -1;
292 exit = true;
294 break;
297 return result;
300 /* display number of tracks inserted into playlists. Used for directory
301 insert */
302 static void display_insert_count(int count)
304 gui_syncsplash(0, str(LANG_PLAYLIST_INSERT_COUNT), count,
305 str(LANG_OFF_ABORT));
308 /* Add specified track into playlist. Callback from directory insert */
309 static int add_track_to_playlist(char* filename, void* context)
311 struct add_track_context* c = (struct add_track_context*) context;
313 if (fdprintf(c->fd, "%s\n", filename) <= 0)
314 return -1;
316 (c->count)++;
318 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
319 display_insert_count(c->count);
321 return 0;
324 /* Add "sel" file into specified "playlist". How to insert depends on type
325 of file */
326 static int add_to_playlist(const char* playlist, const char* sel, int sel_attr)
328 int fd;
329 int result = -1;
331 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
332 if(fd < 0)
333 return result;
335 /* In case we're in the playlist directory */
336 reload_directory();
338 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
340 /* append the selected file */
341 if (fdprintf(fd, "%s\n", sel) > 0)
342 result = 0;
344 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
346 /* append playlist */
347 int f, fs, i;
348 char buf[1024];
350 if(strcasecmp(playlist, sel) == 0)
351 goto exit;
353 f = open(sel, O_RDONLY);
354 if (f < 0)
355 goto exit;
357 fs = filesize(f);
359 for (i=0; i<fs;)
361 int n;
363 n = read(f, buf, sizeof(buf));
364 if (n < 0)
365 break;
367 if (write(fd, buf, n) < 0)
368 break;
370 i += n;
373 if (i >= fs)
374 result = 0;
376 close(f);
378 else if (sel_attr & ATTR_DIRECTORY)
380 /* search directory for tracks and append to playlist */
381 bool recurse = false;
382 const char *lines[] = {
383 str(LANG_RECURSE_DIRECTORY_QUESTION), sel};
384 const struct text_message message={lines, 2};
385 struct add_track_context context;
387 if (global_settings.recursive_dir_insert != RECURSE_ASK)
388 recurse = (bool)global_settings.recursive_dir_insert;
389 else
391 /* Ask if user wants to recurse directory */
392 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
395 context.fd = fd;
396 context.count = 0;
398 display_insert_count(0);
400 result = playlist_directory_tracksearch(sel, recurse,
401 add_track_to_playlist, &context);
403 display_insert_count(context.count);
406 exit:
407 close(fd);
408 return result;
411 bool catalog_view_playlists(void)
413 if (initialize_catalog() == -1)
414 return false;
416 if (display_playlists(NULL, true) == -1)
417 return false;
419 return true;
422 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
423 bool new_playlist, char *m3u8name)
425 char playlist[MAX_PATH];
427 if (initialize_catalog() == -1)
428 return false;
430 if (new_playlist)
432 size_t len;
433 if (m3u8name == NULL)
435 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
436 if (kbd_input(playlist, MAX_PATH))
437 return false;
439 else
440 strcpy(playlist, m3u8name);
442 len = strlen(playlist);
444 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
445 strcat(playlist, "8");
446 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
447 strcat(playlist, ".m3u8");
449 else
451 if (display_playlists(playlist, false) == -1)
452 return false;
455 if (add_to_playlist(playlist, sel, sel_attr) == 0)
457 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
458 sizeof(most_recent_playlist));
459 return true;
461 else
462 return false;