Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / playlist_catalog.c
blobdffc1671daec1a72cfefbb4bbd21d29a77760569
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-extra.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 "filefuncs.h"
36 #include "onplay.h"
37 #include "playlist.h"
38 #include "settings.h"
39 #include "splash.h"
40 #include "tree.h"
41 #include "yesno.h"
42 #include "filetypes.h"
43 #include "debug.h"
44 #include "playlist_catalog.h"
45 #include "talk.h"
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] &&
72 strcmp(global_settings.playlist_catalog_dir,
73 PLAYLIST_CATALOG_DEFAULT_DIR))
75 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
76 default_dir = false;
79 /* fall back to default directory if no or invalid config */
80 if (default_dir)
82 strcpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR);
83 if (!dir_exists(playlist_dir))
84 mkdir(playlist_dir);
87 playlist_dir_length = strlen(playlist_dir);
89 if (dir_exists(playlist_dir))
91 playlist_dir_exists = true;
92 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
93 initialized = true;
97 if (!playlist_dir_exists)
99 if (mkdir(playlist_dir) < 0) {
100 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
101 return -1;
103 else {
104 playlist_dir_exists = true;
105 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
106 initialized = true;
110 return 0;
113 /* Display all playlists in catalog. Selected "playlist" is returned.
114 If "view" mode is set then we're not adding anything into playlist. */
115 static int display_playlists(char* playlist, bool view)
117 struct browse_context browse;
118 char selected_playlist[MAX_PATH];
119 int result = -1;
121 browse_context_init(&browse, SHOW_M3U,
122 BROWSE_SELECTONLY|(view? 0: BROWSE_NO_CONTEXT_MENU),
123 str(LANG_CATALOG), NOICON,
124 playlist_dir, most_recent_playlist);
126 browse.buf = selected_playlist;
127 browse.bufsize = sizeof(selected_playlist);
129 rockbox_browse(&browse);
131 if (browse.flags & BROWSE_SELECTED)
133 strlcpy(most_recent_playlist, selected_playlist+playlist_dir_length+1,
134 sizeof(most_recent_playlist));
136 if (view)
138 char *filename = strrchr(selected_playlist, '/')+1;
139 /* In view mode, selecting a playlist starts playback */
140 ft_play_playlist(selected_playlist, playlist_dir, filename);
141 result = 0;
143 else
145 result = 0;
146 strlcpy(playlist, selected_playlist, MAX_PATH);
150 return result;
153 /* display number of tracks inserted into playlists. Used for directory
154 insert */
155 static void display_insert_count(int count)
157 static long talked_tick = 0;
158 if(global_settings.talk_menu && count &&
159 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
161 talked_tick = current_tick;
162 talk_number(count, false);
163 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
166 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, str(LANG_OFF_ABORT));
169 /* Add specified track into playlist. Callback from directory insert */
170 static int add_track_to_playlist(char* filename, void* context)
172 struct add_track_context* c = (struct add_track_context*) context;
174 if (fdprintf(c->fd, "%s\n", filename) <= 0)
175 return -1;
177 (c->count)++;
179 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
180 display_insert_count(c->count);
182 return 0;
185 /* Add "sel" file into specified "playlist". How to insert depends on type
186 of file */
187 static int add_to_playlist(const char* playlist, bool new_playlist,
188 const char* sel, int sel_attr)
190 int fd;
191 int result = -1;
193 if (new_playlist)
194 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
195 else
196 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND, 0666);
198 if(fd < 0)
199 return result;
201 /* In case we're in the playlist directory */
202 reload_directory();
204 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
206 /* append the selected file */
207 if (fdprintf(fd, "%s\n", sel) > 0)
208 result = 0;
210 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
212 /* append playlist */
213 int f, fs, i;
214 char buf[1024];
216 if(strcasecmp(playlist, sel) == 0)
217 goto exit;
219 f = open_utf8(sel, O_RDONLY);
220 if (f < 0)
221 goto exit;
223 i = lseek(f, 0, SEEK_CUR);
224 fs = filesize(f);
225 while (i < fs)
227 int n;
229 n = read(f, buf, sizeof(buf));
230 if (n < 0)
231 break;
233 if (write(fd, buf, n) < 0)
234 break;
236 i += n;
239 if (i >= fs)
240 result = 0;
242 close(f);
244 else if (sel_attr & ATTR_DIRECTORY)
246 /* search directory for tracks and append to playlist */
247 bool recurse = false;
248 const char *lines[] = {
249 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
250 const struct text_message message={lines, 2};
251 struct add_track_context context;
253 if (global_settings.recursive_dir_insert != RECURSE_ASK)
254 recurse = (bool)global_settings.recursive_dir_insert;
255 else
257 /* Ask if user wants to recurse directory */
258 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
261 context.fd = fd;
262 context.count = 0;
264 display_insert_count(0);
266 result = playlist_directory_tracksearch(sel, recurse,
267 add_track_to_playlist, &context);
269 display_insert_count(context.count);
272 exit:
273 close(fd);
274 return result;
276 static bool in_cat_viewer = false;
277 bool catalog_view_playlists(void)
279 bool retval = true;
280 if (in_cat_viewer)
281 return false;
283 if (initialize_catalog() == -1)
284 return false;
286 in_cat_viewer = true;
287 retval = (display_playlists(NULL, true) != -1);
288 in_cat_viewer = false;
289 return retval;
292 static bool in_add_to_playlist = false;
293 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
294 bool new_playlist, char *m3u8name)
296 int result;
297 char playlist[MAX_PATH];
298 if (in_add_to_playlist)
299 return false;
301 if (initialize_catalog() == -1)
302 return false;
304 if (new_playlist)
306 size_t len;
307 if (m3u8name == NULL)
309 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
310 if (kbd_input(playlist, MAX_PATH))
311 return false;
313 else
314 strcpy(playlist, m3u8name);
316 len = strlen(playlist);
318 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
319 strcat(playlist, "8");
320 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
321 strcat(playlist, ".m3u8");
323 else
325 in_add_to_playlist = true;
326 result = display_playlists(playlist, false);
327 in_add_to_playlist = false;
329 if (result == -1)
330 return false;
333 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
334 return true;
335 else
336 return false;