RGB <-> HSV colorspace conversion lib
[maemo-rb.git] / apps / playlist_catalog.c
bloba1a9bd8ea1c4c188fbe129e4c493298416ea3856
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"
41 #define PLAYLIST_CATALOG_CFG ROCKBOX_DIR "/playlist_catalog.config"
42 #define PLAYLIST_CATALOG_DEFAULT_DIR "/Playlists"
43 #define MAX_PLAYLISTS 400
44 #define PLAYLIST_DISPLAY_COUNT 10
46 /* Use for recursive directory search */
47 struct add_track_context {
48 int fd;
49 int count;
52 /* keep track of most recently used playlist */
53 static char most_recent_playlist[MAX_PATH];
55 /* directory where our playlists our stored (configured in
56 PLAYLIST_CATALOG_CFG) */
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 int f;
69 DIR* dir;
70 bool default_dir = true;
72 f = open(PLAYLIST_CATALOG_CFG, O_RDONLY);
73 if (f >= 0)
75 char buf[MAX_PATH+5];
77 while (read_line(f, buf, sizeof(buf)))
79 char* name;
80 char* value;
82 /* directory config is of the format: "dir: /path/to/dir" */
83 if (settings_parseline(buf, &name, &value) &&
84 !strncasecmp(name, "dir:", strlen(name)) &&
85 strlen(value) > 0)
87 strncpy(playlist_dir, value, strlen(value));
88 default_dir = false;
92 close(f);
95 /* fall back to default directory if no or invalid config */
96 if (default_dir)
97 strncpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
98 sizeof(playlist_dir));
100 playlist_dir_length = strlen(playlist_dir);
102 dir = opendir(playlist_dir);
103 if (dir)
105 playlist_dir_exists = true;
106 closedir(dir);
107 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
108 initialized = true;
112 if (!playlist_dir_exists)
114 gui_syncsplash(HZ*2, true, str(LANG_CATALOG_NO_DIRECTORY),
115 playlist_dir);
116 return -1;
119 return 0;
122 /* Use the filetree functions to retrieve the list of playlists in the
123 directory */
124 static int create_playlist_list(char** playlists, int num_items,
125 int* num_playlists)
127 int result = -1;
128 int num_files = 0;
129 int index = 0;
130 int i;
131 bool most_recent = false;
132 struct entry *files;
133 struct tree_context* tc = tree_get_context();
134 int dirfilter = *(tc->dirfilter);
136 *num_playlists = 0;
138 /* use the tree browser dircache to load only playlists */
139 *(tc->dirfilter) = SHOW_PLAYLIST;
141 if (ft_load(tc, playlist_dir) < 0)
143 gui_syncsplash(HZ*2, true, str(LANG_CATALOG_NO_DIRECTORY),
144 playlist_dir);
145 goto exit;
148 files = (struct entry*) tc->dircache;
149 num_files = tc->filesindir;
151 /* we've overwritten the dircache so tree browser will need to be
152 reloaded */
153 reload_directory();
155 /* if it exists, most recent playlist will always be index 0 */
156 if (most_recent_playlist[0] != '\0')
158 index = 1;
159 most_recent = true;
162 for (i=0; i<num_files && index<num_items; i++)
164 if (files[i].attr & TREE_ATTR_M3U)
166 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
167 sizeof(most_recent_playlist)))
169 playlists[0] = files[i].name;
170 most_recent = false;
172 else
174 playlists[index] = files[i].name;
175 index++;
180 *num_playlists = index;
182 /* we couldn't find the most recent playlist, shift all playlists up */
183 if (most_recent)
185 for (i=0; i<index-1; i++)
186 playlists[i] = playlists[i+1];
188 (*num_playlists)--;
190 most_recent_playlist[0] = '\0';
193 result = 0;
195 exit:
196 *(tc->dirfilter) = dirfilter;
197 return result;
200 /* Callback for gui_synclist */
201 static char* playlist_callback_name(int selected_item, void* data,
202 char* buffer)
204 char** playlists = (char**) data;
206 strncpy(buffer, playlists[selected_item], MAX_PATH);
208 return buffer;
211 /* Display all playlists in catalog. Selected "playlist" is returned.
212 If "view" mode is set then we're not adding anything into playlist. */
213 static int display_playlists(char* playlist, bool view)
215 int result = -1;
216 int num_playlists = 0;
217 int lastbutton = BUTTON_NONE;
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, true, 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);
238 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
239 gui_synclist_draw(&playlist_lists);
241 while (!exit)
243 int button = button_get_w_tmo(HZ/2);
244 char* sel_file;
246 gui_synclist_do_button(&playlist_lists, button);
248 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
250 switch (button)
252 case TREE_EXIT:
253 #ifdef TREE_RC_EXIT
254 case TREE_RC_EXIT:
255 #endif
256 #ifdef TREE_OFF
257 case TREE_OFF:
258 #endif
259 exit = true;
260 break;
262 #ifdef TREE_ENTER
263 case TREE_ENTER:
264 case TREE_ENTER | BUTTON_REPEAT:
265 #endif
266 #ifdef TREE_RC_RUN
267 case TREE_RC_RUN:
268 #endif
269 case TREE_RUN:
270 #ifdef TREE_RUN_PRE
271 if (((button == TREE_RUN)
272 #ifdef TREE_RC_RUN_PRE
273 || (button == TREE_RC_RUN))
274 && ((lastbutton != TREE_RC_RUN_PRE)
275 #endif
276 && (lastbutton != TREE_RUN_PRE)))
277 break;
278 #endif
280 if (view)
282 /* In view mode, selecting a playlist starts playback */
283 if (playlist_create(playlist_dir, sel_file) != -1)
285 if (global_settings.playlist_shuffle)
286 playlist_shuffle(current_tick, -1);
287 playlist_start(0, 0);
290 else
292 /* we found the playlist we want to add to */
293 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
294 sel_file);
297 result = 0;
298 exit = true;
299 break;
301 case TREE_CONTEXT:
302 #ifdef TREE_CONTEXT2
303 case TREE_CONTEXT2:
304 #endif
305 #ifdef TREE_RC_CONTEXT
306 case TREE_RC_CONTEXT:
307 #endif
308 /* context menu only available in view mode */
309 if (view)
311 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
312 sel_file);
314 if (onplay(playlist, TREE_ATTR_M3U,
315 CONTEXT_TREE) != ONPLAY_OK)
317 result = 0;
318 exit = true;
320 else
321 gui_synclist_draw(&playlist_lists);
323 break;
325 case BUTTON_NONE:
326 gui_syncstatusbar_draw(&statusbars, false);
327 break;
329 default:
330 if(default_event_handler(button) == SYS_USB_CONNECTED)
332 result = -1;
333 exit = true;
335 break;
338 lastbutton = button;
341 return result;
344 /* display number of tracks inserted into playlists. Used for directory
345 insert */
346 static void display_insert_count(int count)
348 gui_syncsplash(0, true, str(LANG_PLAYLIST_INSERT_COUNT), count,
349 #if CONFIG_KEYPAD == PLAYER_PAD
350 str(LANG_STOP_ABORT)
351 #else
352 str(LANG_OFF_ABORT)
353 #endif
357 /* Add specified track into playlist. Callback from directory insert */
358 static int add_track_to_playlist(char* filename, void* context)
360 struct add_track_context* c = (struct add_track_context*) context;
362 if (fdprintf(c->fd, "%s\n", filename) <= 0)
363 return -1;
365 (c->count)++;
367 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
368 display_insert_count(c->count);
370 return 0;
373 /* Add "sel" file into specified "playlist". How to insert depends on type
374 of file */
375 static int add_to_playlist(const char* playlist, char* sel, int sel_attr)
377 int fd;
378 int result = -1;
380 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND);
381 if(fd < 0)
382 return result;
384 /* In case we're in the playlist directory */
385 reload_directory();
387 if ((sel_attr & TREE_ATTR_MASK) == TREE_ATTR_MPA)
389 /* append the selected file */
390 if (fdprintf(fd, "%s\n", sel) > 0)
391 result = 0;
393 else if ((sel_attr & TREE_ATTR_MASK) == TREE_ATTR_M3U)
395 /* append playlist */
396 int f, fs, i;
397 char buf[1024];
399 if(strcasecmp(playlist, sel) == 0)
400 goto exit;
402 f = open(sel, O_RDONLY);
403 if (f < 0)
404 goto exit;
406 fs = filesize(f);
408 for (i=0; i<fs;)
410 int n;
412 n = read(f, buf, sizeof(buf));
413 if (n < 0)
414 break;
416 if (write(fd, buf, n) < 0)
417 break;
419 i += n;
422 if (i >= fs)
423 result = 0;
425 close(f);
427 else if (sel_attr & ATTR_DIRECTORY)
429 /* search directory for tracks and append to playlist */
430 bool recurse = false;
431 char *lines[] = {
432 (char *)str(LANG_RECURSE_DIRECTORY_QUESTION),
435 struct text_message message={lines, 2};
436 struct add_track_context context;
438 if (global_settings.recursive_dir_insert != RECURSE_ASK)
439 recurse = (bool)global_settings.recursive_dir_insert;
440 else
442 /* Ask if user wants to recurse directory */
443 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
446 context.fd = fd;
447 context.count = 0;
449 display_insert_count(0);
451 result = playlist_directory_tracksearch(sel, recurse,
452 add_track_to_playlist, &context);
454 display_insert_count(context.count);
457 exit:
458 close(fd);
459 return result;
462 bool catalog_view_playlists(void)
464 if (initialize_catalog() == -1)
465 return false;
467 if (display_playlists(NULL, true) == -1)
468 return false;
470 return true;
473 bool catalog_add_to_a_playlist(char* sel, int sel_attr, bool new_playlist)
475 char playlist[MAX_PATH];
477 if (initialize_catalog() == -1)
478 return false;
480 if (new_playlist)
482 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
483 if (kbd_input(playlist, MAX_PATH))
484 return false;
486 if(strlen(playlist) <= 4 ||
487 strcasecmp(&playlist[strlen(playlist)-4], ".m3u"))
488 strcat(playlist, ".m3u");
490 else
492 if (display_playlists(playlist, false) == -1)
493 return false;
496 if (add_to_playlist(playlist, sel, sel_attr) == 0)
498 strncpy(most_recent_playlist, playlist+playlist_dir_length+1,
499 sizeof(most_recent_playlist));
500 return true;
502 else
503 return false;