Bump version numbers for 3.13
[maemo-rb.git] / apps / playlist_catalog.c
blob3687681b6682aca5615f9e64e26c9331ac29edf3
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"
46 #include "playlist_viewer.h"
47 #include "bookmark.h"
49 /* Use for recursive directory search */
50 struct add_track_context {
51 int fd;
52 int count;
55 /* keep track of most recently used playlist */
56 static char most_recent_playlist[MAX_PATH];
58 /* directory where our playlists our stored */
59 static char playlist_dir[MAX_PATH];
60 static int playlist_dir_length;
61 static bool playlist_dir_exists = false;
63 /* Retrieve playlist directory from config file and verify it exists */
64 static bool initialized = false;
65 static int initialize_catalog(void)
68 if (!initialized)
70 bool default_dir = true;
72 /* directory config is of the format: "dir: /path/to/dir" */
73 if (global_settings.playlist_catalog_dir[0] &&
74 strcmp(global_settings.playlist_catalog_dir,
75 PLAYLIST_CATALOG_DEFAULT_DIR))
77 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
78 default_dir = false;
81 /* fall back to default directory if no or invalid config */
82 if (default_dir)
84 strcpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR);
85 if (!dir_exists(playlist_dir))
86 mkdir(playlist_dir);
89 playlist_dir_length = strlen(playlist_dir);
91 if (dir_exists(playlist_dir))
93 playlist_dir_exists = true;
94 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
95 initialized = true;
99 if (!playlist_dir_exists)
101 if (mkdir(playlist_dir) < 0) {
102 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
103 return -1;
105 else {
106 playlist_dir_exists = true;
107 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
108 initialized = true;
112 return 0;
115 void catalog_set_directory(const char* directory)
117 if (directory == NULL)
119 global_settings.playlist_catalog_dir[0] = '\0';
121 else
123 strcpy(global_settings.playlist_catalog_dir, directory);
125 initialized = false;
126 initialize_catalog();
129 const char* catalog_get_directory(void)
131 if (initialize_catalog() == -1)
132 return "";
133 return playlist_dir;
136 /* Display all playlists in catalog. Selected "playlist" is returned.
137 If "view" mode is set then we're not adding anything into playlist. */
138 static int display_playlists(char* playlist, bool view)
140 struct browse_context browse;
141 char selected_playlist[MAX_PATH];
142 int result = -1;
144 browse_context_init(&browse, SHOW_M3U,
145 BROWSE_SELECTONLY|(view? 0: BROWSE_NO_CONTEXT_MENU),
146 str(LANG_CATALOG), NOICON,
147 playlist_dir, most_recent_playlist);
149 browse.buf = selected_playlist;
150 browse.bufsize = sizeof(selected_playlist);
152 restart:
153 browse.flags &= ~BROWSE_SELECTED;
154 rockbox_browse(&browse);
156 if (browse.flags & BROWSE_SELECTED)
158 strlcpy(most_recent_playlist, selected_playlist+playlist_dir_length+1,
159 sizeof(most_recent_playlist));
161 if (view)
164 if (!bookmark_autoload(selected_playlist))
166 if (playlist_viewer_ex(selected_playlist) == PLAYLIST_VIEWER_CANCEL)
167 goto restart;
169 result = 0;
171 else
173 result = 0;
174 strlcpy(playlist, selected_playlist, MAX_PATH);
178 return result;
181 /* display number of tracks inserted into playlists. Used for directory
182 insert */
183 static void display_insert_count(int count)
185 static long talked_tick = 0;
186 if(global_settings.talk_menu && count &&
187 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
189 talked_tick = current_tick;
190 talk_number(count, false);
191 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
194 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, str(LANG_OFF_ABORT));
197 /* Add specified track into playlist. Callback from directory insert */
198 static int add_track_to_playlist(char* filename, void* context)
200 struct add_track_context* c = (struct add_track_context*) context;
202 if (fdprintf(c->fd, "%s\n", filename) <= 0)
203 return -1;
205 (c->count)++;
207 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
208 display_insert_count(c->count);
210 return 0;
213 /* Add "sel" file into specified "playlist". How to insert depends on type
214 of file */
215 static int add_to_playlist(const char* playlist, bool new_playlist,
216 const char* sel, int sel_attr)
218 int fd;
219 int result = -1;
221 if (new_playlist)
222 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
223 else
224 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND, 0666);
226 if(fd < 0)
227 return result;
229 /* In case we're in the playlist directory */
230 reload_directory();
232 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
234 /* append the selected file */
235 if (fdprintf(fd, "%s\n", sel) > 0)
236 result = 0;
238 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
240 /* append playlist */
241 int f, fs, i;
242 char buf[1024];
244 if(strcasecmp(playlist, sel) == 0)
245 goto exit;
247 f = open_utf8(sel, O_RDONLY);
248 if (f < 0)
249 goto exit;
251 i = lseek(f, 0, SEEK_CUR);
252 fs = filesize(f);
253 while (i < fs)
255 int n;
257 n = read(f, buf, sizeof(buf));
258 if (n < 0)
259 break;
261 if (write(fd, buf, n) < 0)
262 break;
264 i += n;
267 if (i >= fs)
268 result = 0;
270 close(f);
272 else if (sel_attr & ATTR_DIRECTORY)
274 /* search directory for tracks and append to playlist */
275 bool recurse = false;
276 const char *lines[] = {
277 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
278 const struct text_message message={lines, 2};
279 struct add_track_context context;
281 if (global_settings.recursive_dir_insert != RECURSE_ASK)
282 recurse = (bool)global_settings.recursive_dir_insert;
283 else
285 /* Ask if user wants to recurse directory */
286 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
289 context.fd = fd;
290 context.count = 0;
292 display_insert_count(0);
294 result = playlist_directory_tracksearch(sel, recurse,
295 add_track_to_playlist, &context);
297 display_insert_count(context.count);
300 exit:
301 close(fd);
302 return result;
304 static bool in_cat_viewer = false;
305 bool catalog_view_playlists(void)
307 bool retval = true;
308 if (in_cat_viewer)
309 return false;
311 if (initialize_catalog() == -1)
312 return false;
314 in_cat_viewer = true;
315 retval = (display_playlists(NULL, true) != -1);
316 in_cat_viewer = false;
317 return retval;
320 static bool in_add_to_playlist = false;
321 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
322 bool new_playlist, char *m3u8name)
324 int result;
325 char playlist[MAX_PATH];
326 if (in_add_to_playlist)
327 return false;
329 if (initialize_catalog() == -1)
330 return false;
332 if (new_playlist)
334 size_t len;
335 if (m3u8name == NULL)
337 /*If sel is a folder, we prefill the text field with its name*/
338 const char *name = strrchr(sel, '/');
339 snprintf(playlist, MAX_PATH, "%s/%s.m3u8",
340 playlist_dir,
341 (name!=NULL && (sel_attr & ATTR_DIRECTORY))?name+1:"");
343 else
344 strcpy(playlist, m3u8name);
346 len = strlen(playlist);
348 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
349 strcat(playlist, "8");
350 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
351 strcat(playlist, ".m3u8");
353 if (kbd_input(playlist, MAX_PATH))
354 return false;
356 else
358 in_add_to_playlist = true;
359 result = display_playlists(playlist, false);
360 in_add_to_playlist = false;
362 if (result == -1)
363 return false;
366 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
367 return true;
368 else
369 return false;