Make some global function/variables local (by making them static)
[maemo-rb.git] / apps / playlist_catalog.c
blobf9a43da411012c59b6b4319432eb7e2bf39140c4
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 "onplay.h"
36 #include "playlist.h"
37 #include "settings.h"
38 #include "splash.h"
39 #include "tree.h"
40 #include "yesno.h"
41 #include "filetypes.h"
42 #include "debug.h"
43 #include "playlist_catalog.h"
44 #include "talk.h"
46 #define MAX_PLAYLISTS 400
48 /* Use for recursive directory search */
49 struct add_track_context {
50 int fd;
51 int count;
54 /* keep track of most recently used playlist */
55 static char most_recent_playlist[MAX_PATH];
57 /* directory where our playlists our stored */
58 static char playlist_dir[MAX_PATH];
59 static int playlist_dir_length;
60 static bool playlist_dir_exists = false;
62 /* Retrieve playlist directory from config file and verify it exists */
63 static int initialize_catalog(void)
65 static bool initialized = false;
67 if (!initialized)
69 bool default_dir = true;
71 /* directory config is of the format: "dir: /path/to/dir" */
72 if (global_settings.playlist_catalog_dir[0])
74 strcpy(playlist_dir, global_settings.playlist_catalog_dir);
75 default_dir = false;
78 /* fall back to default directory if no or invalid config */
79 if (default_dir)
80 strlcpy(playlist_dir, PLAYLIST_CATALOG_DEFAULT_DIR,
81 sizeof(playlist_dir));
83 playlist_dir_length = strlen(playlist_dir);
85 if (dir_exists(playlist_dir))
87 playlist_dir_exists = true;
88 memset(most_recent_playlist, 0, sizeof(most_recent_playlist));
89 initialized = true;
93 if (!playlist_dir_exists)
95 if (mkdir(playlist_dir) < 0) {
96 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), 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 splashf(HZ*2, ID2P(LANG_CATALOG_NO_DIRECTORY), playlist_dir);
130 goto exit;
133 files = (struct entry*) tc->dircache;
134 num_files = tc->filesindir;
136 /* we've overwritten the dircache so tree browser will need to be
137 reloaded */
138 reload_directory();
140 /* if it exists, most recent playlist will always be index 0 */
141 if (most_recent_playlist[0] != '\0')
143 index = 1;
144 most_recent = true;
147 for (i=0; i<num_files && index<num_items; i++)
149 if (files[i].attr & FILE_ATTR_M3U)
151 if (most_recent && !strncmp(files[i].name, most_recent_playlist,
152 sizeof(most_recent_playlist)))
154 playlists[0] = files[i].name;
155 most_recent = false;
157 else
159 playlists[index] = files[i].name;
160 index++;
165 *num_playlists = index;
167 /* we couldn't find the most recent playlist, shift all playlists up */
168 if (most_recent)
170 for (i=0; i<index-1; i++)
171 playlists[i] = playlists[i+1];
173 (*num_playlists)--;
175 most_recent_playlist[0] = '\0';
178 result = 0;
180 exit:
181 *(tc->dirfilter) = dirfilter;
182 return result;
185 /* Callback for gui_synclist */
186 static const char* playlist_callback_name(int selected_item, void* data,
187 char* buffer, size_t buffer_len)
189 char** playlists = (char**) data;
191 strlcpy(buffer, playlists[selected_item], buffer_len);
193 if (buffer[0] != '.' && !(global_settings.show_filename_ext == 1
194 || (global_settings.show_filename_ext == 3
195 && global_settings.dirfilter == 0)))
197 char* dot = strrchr(buffer, '.');
199 if (dot != NULL)
201 *dot = '\0';
205 return buffer;
208 static int playlist_callback_voice(int selected_item, void* data)
210 char** playlists = (char**) data;
211 talk_file_or_spell(playlist_dir, playlists[selected_item], NULL, false);
212 return 0;
215 /* Display all playlists in catalog. Selected "playlist" is returned.
216 If "view" mode is set then we're not adding anything into playlist. */
217 static int display_playlists(char* playlist, bool view)
219 int result = -1;
220 int num_playlists = 0;
221 bool exit = false;
222 char temp_buf[MAX_PATH];
223 char* playlists[MAX_PLAYLISTS];
224 struct gui_synclist playlist_lists;
226 if (create_playlist_list(playlists, MAX_PLAYLISTS,
227 &num_playlists) != 0)
228 return -1;
230 if (num_playlists <= 0)
232 splash(HZ*2, ID2P(LANG_CATALOG_NO_PLAYLISTS));
233 return -1;
236 if (!playlist)
237 playlist = temp_buf;
239 gui_synclist_init(&playlist_lists, playlist_callback_name, playlists,
240 false, 1, NULL);
241 if(global_settings.talk_menu)
242 gui_synclist_set_voice_callback(&playlist_lists,
243 playlist_callback_voice);
244 gui_synclist_set_nb_items(&playlist_lists, num_playlists);
245 gui_synclist_draw(&playlist_lists);
246 gui_synclist_speak_item(&playlist_lists);
248 while (!exit)
250 int button;
251 char* sel_file;
252 list_do_action(CONTEXT_LIST,HZ/2,
253 &playlist_lists, &button,LIST_WRAP_UNLESS_HELD);
254 sel_file = playlists[gui_synclist_get_sel_pos(&playlist_lists)];
256 switch (button)
258 case ACTION_STD_CANCEL:
259 exit = true;
260 break;
262 case ACTION_STD_OK:
263 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir, sel_file);
265 if (view)
267 /* In view mode, selecting a playlist starts playback */
268 ft_play_playlist(playlist, playlist_dir, sel_file);
271 result = 0;
272 exit = true;
273 break;
275 case ACTION_STD_CONTEXT:
276 /* context menu only available in view mode */
277 if (view)
279 snprintf(playlist, MAX_PATH, "%s/%s", playlist_dir,
280 sel_file);
282 if (onplay(playlist, FILE_ATTR_M3U,
283 CONTEXT_TREE, false) != ONPLAY_OK)
285 result = 0;
286 exit = true;
288 else
290 gui_synclist_draw(&playlist_lists);
291 gui_synclist_speak_item(&playlist_lists);
294 break;
296 default:
297 if(default_event_handler(button) == SYS_USB_CONNECTED)
299 result = -1;
300 exit = true;
302 break;
305 return result;
308 /* display number of tracks inserted into playlists. Used for directory
309 insert */
310 static void display_insert_count(int count)
312 static long talked_tick = 0;
313 if(global_settings.talk_menu && count &&
314 (talked_tick == 0 || TIME_AFTER(current_tick, talked_tick+5*HZ)))
316 talked_tick = current_tick;
317 talk_number(count, false);
318 talk_id(LANG_PLAYLIST_INSERT_COUNT, true);
321 splashf(0, str(LANG_PLAYLIST_INSERT_COUNT), count, str(LANG_OFF_ABORT));
324 /* Add specified track into playlist. Callback from directory insert */
325 static int add_track_to_playlist(char* filename, void* context)
327 struct add_track_context* c = (struct add_track_context*) context;
329 if (fdprintf(c->fd, "%s\n", filename) <= 0)
330 return -1;
332 (c->count)++;
334 if (((c->count)%PLAYLIST_DISPLAY_COUNT) == 0)
335 display_insert_count(c->count);
337 return 0;
340 /* Add "sel" file into specified "playlist". How to insert depends on type
341 of file */
342 static int add_to_playlist(const char* playlist, bool new_playlist,
343 const char* sel, int sel_attr)
345 int fd;
346 int result = -1;
348 if (new_playlist)
349 fd = open_utf8(playlist, O_CREAT|O_WRONLY|O_TRUNC);
350 else
351 fd = open(playlist, O_CREAT|O_WRONLY|O_APPEND, 0666);
353 if(fd < 0)
354 return result;
356 /* In case we're in the playlist directory */
357 reload_directory();
359 if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
361 /* append the selected file */
362 if (fdprintf(fd, "%s\n", sel) > 0)
363 result = 0;
365 else if ((sel_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
367 /* append playlist */
368 int f, fs, i;
369 char buf[1024];
371 if(strcasecmp(playlist, sel) == 0)
372 goto exit;
374 f = open_utf8(sel, O_RDONLY);
375 if (f < 0)
376 goto exit;
378 fs = filesize(f);
380 for (i=0; i<fs;)
382 int n;
384 n = read(f, buf, sizeof(buf));
385 if (n < 0)
386 break;
388 if (write(fd, buf, n) < 0)
389 break;
391 i += n;
394 if (i >= fs)
395 result = 0;
397 close(f);
399 else if (sel_attr & ATTR_DIRECTORY)
401 /* search directory for tracks and append to playlist */
402 bool recurse = false;
403 const char *lines[] = {
404 ID2P(LANG_RECURSE_DIRECTORY_QUESTION), sel};
405 const struct text_message message={lines, 2};
406 struct add_track_context context;
408 if (global_settings.recursive_dir_insert != RECURSE_ASK)
409 recurse = (bool)global_settings.recursive_dir_insert;
410 else
412 /* Ask if user wants to recurse directory */
413 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
416 context.fd = fd;
417 context.count = 0;
419 display_insert_count(0);
421 result = playlist_directory_tracksearch(sel, recurse,
422 add_track_to_playlist, &context);
424 display_insert_count(context.count);
427 exit:
428 close(fd);
429 return result;
431 static bool in_cat_viewer = false;
432 bool catalog_view_playlists(void)
434 bool retval = true;
435 if (in_cat_viewer)
436 return false;
438 if (initialize_catalog() == -1)
439 return false;
440 in_cat_viewer = true;
441 retval = (display_playlists(NULL, true) != -1);
442 in_cat_viewer = false;
443 return retval;
446 bool catalog_add_to_a_playlist(const char* sel, int sel_attr,
447 bool new_playlist, char *m3u8name)
449 char playlist[MAX_PATH];
451 if (initialize_catalog() == -1)
452 return false;
454 if (new_playlist)
456 size_t len;
457 if (m3u8name == NULL)
459 snprintf(playlist, MAX_PATH, "%s/", playlist_dir);
460 if (kbd_input(playlist, MAX_PATH))
461 return false;
463 else
464 strcpy(playlist, m3u8name);
466 len = strlen(playlist);
468 if(len > 4 && !strcasecmp(&playlist[len-4], ".m3u"))
469 strcat(playlist, "8");
470 else if(len <= 5 || strcasecmp(&playlist[len-5], ".m3u8"))
471 strcat(playlist, ".m3u8");
473 else
475 if (display_playlists(playlist, false) == -1)
476 return false;
479 if (add_to_playlist(playlist, new_playlist, sel, sel_attr) == 0)
481 strlcpy(most_recent_playlist, playlist+playlist_dir_length+1,
482 sizeof(most_recent_playlist));
483 return true;
485 else
486 return false;