Add note to Bookmarking sections that bookmarking only works from the file browser...
[kugel-rb.git] / apps / bookmark.c
blobd4bca415b441b7b3ee1f34a3bf71aadc38d3ea9c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "config.h"
27 #include "action.h"
28 #include "audio.h"
29 #include "playlist.h"
30 #include "settings.h"
31 #include "tree.h"
32 #include "bookmark.h"
33 #include "system.h"
34 #include "icons.h"
35 #include "menu.h"
36 #include "lang.h"
37 #include "talk.h"
38 #include "misc.h"
39 #include "splash.h"
40 #include "yesno.h"
41 #include "list.h"
42 #include "plugin.h"
43 #include "backdrop.h"
44 #include "file.h"
45 #include "filefuncs.h"
47 #define MAX_BOOKMARKS 10
48 #define MAX_BOOKMARK_SIZE 350
49 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
51 /* Used to buffer bookmarks while displaying the bookmark list. */
52 struct bookmark_list
54 const char* filename;
55 size_t buffer_size;
56 int start;
57 int count;
58 int total_count;
59 bool show_dont_resume;
60 bool reload;
61 bool show_playlist_name;
62 char* items[];
65 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
66 bool most_recent);
67 static bool check_bookmark(const char* bookmark);
68 static char* create_bookmark(void);
69 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
70 static void say_bookmark(const char* bookmark,
71 int bookmark_id, bool show_playlist_name);
72 static bool play_bookmark(const char* bookmark);
73 static bool generate_bookmark_file_name(const char *in);
74 static const char* skip_token(const char* s);
75 static const char* int_token(const char* s, int* dest);
76 static const char* long_token(const char* s, long* dest);
77 static const char* bool_token(const char* s, bool* dest);
78 static bool parse_bookmark(const char *bookmark,
79 int *resume_index,
80 int *resume_offset,
81 int *resume_seed,
82 int *resume_first_index,
83 char* resume_file,
84 unsigned int resume_file_size,
85 long* ms,
86 int * repeat_mode,
87 bool *shuffle,
88 char* file_name);
89 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
90 static const char* get_bookmark_info(int list_index,
91 void* data,
92 char *buffer,
93 size_t buffer_len);
94 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
95 static bool system_check(void);
96 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
97 static int get_bookmark_count(const char* bookmark_file_name);
99 static char global_temp_buffer[MAX_PATH+1];
100 /* File name created by generate_bookmark_file_name */
101 static char global_bookmark_file_name[MAX_PATH];
102 static char global_read_buffer[MAX_BOOKMARK_SIZE];
103 /* Bookmark created by create_bookmark*/
104 static char global_bookmark[MAX_BOOKMARK_SIZE];
105 /* Filename from parsed bookmark (can be made local where needed) */
106 static char global_filename[MAX_PATH];
108 /* ----------------------------------------------------------------------- */
109 /* This is the interface function from the main menu. */
110 /* ----------------------------------------------------------------------- */
111 bool bookmark_create_menu(void)
113 write_bookmark(true, create_bookmark());
114 return false;
117 /* ----------------------------------------------------------------------- */
118 /* This function acts as the load interface from the main menu */
119 /* This function determines the bookmark file name and then loads that file*/
120 /* for the user. The user can then select a bookmark to load. */
121 /* If no file/directory is currently playing, the menu item does not work. */
122 /* ----------------------------------------------------------------------- */
123 bool bookmark_load_menu(void)
125 if (system_check())
127 char* name = playlist_get_name(NULL, global_temp_buffer,
128 sizeof(global_temp_buffer));
129 if (generate_bookmark_file_name(name))
131 char* bookmark = select_bookmark(global_bookmark_file_name, false);
133 if (bookmark != NULL)
135 return play_bookmark(bookmark);
140 return false;
143 /* ----------------------------------------------------------------------- */
144 /* Gives the user a list of the Most Recent Bookmarks. This is an */
145 /* interface function */
146 /* ----------------------------------------------------------------------- */
147 bool bookmark_mrb_load()
149 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
151 if (bookmark != NULL)
153 return play_bookmark(bookmark);
156 return false;
159 /* ----------------------------------------------------------------------- */
160 /* This function handles an autobookmark creation. This is an interface */
161 /* function. */
162 /* ----------------------------------------------------------------------- */
163 bool bookmark_autobookmark(void)
165 char* bookmark;
166 if (!system_check())
167 return false;
168 int i;
170 audio_pause(); /* first pause playback */
171 bookmark = create_bookmark();
172 /* Workaround for inability to speak when paused: all callers will
173 just do audio_stop() when we return, so we can do it right
174 away. This makes it possible to speak the "Create a Bookmark?"
175 prompt and the "Bookmark Created" splash. */
176 audio_stop();
177 switch (global_settings.autocreatebookmark)
179 case BOOKMARK_YES:
180 return write_bookmark(true, bookmark);
182 case BOOKMARK_NO:
183 return false;
185 case BOOKMARK_RECENT_ONLY_YES:
186 return write_bookmark(false, bookmark);
188 #ifdef HAVE_LCD_BITMAP
189 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
190 const struct text_message message={lines, 1};
191 #else
192 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
193 str(LANG_CONFIRM_WITH_BUTTON)};
194 const struct text_message message={lines, 2};
195 #endif
196 FOR_NB_SCREENS(i)
197 screens[i].backdrop_show(BACKDROP_MAIN);
199 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
201 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
202 return write_bookmark(false, bookmark);
203 else
204 return write_bookmark(true, bookmark);
206 return false;
209 /* ----------------------------------------------------------------------- */
210 /* This function takes the current current resume information and writes */
211 /* that to the beginning of the bookmark file. */
212 /* This file will contain N number of bookmarks in the following format: */
213 /* resume_index*resume_offset*resume_seed*resume_first_index* */
214 /* resume_file*milliseconds*MP3 Title* */
215 /* ------------------------------------------------------------------------*/
216 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
218 bool success=false;
219 if (!bookmark)
220 return false; /* something didn't happen correctly, do nothing */
222 if (global_settings.usemrb)
223 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
226 /* writing the bookmark */
227 if (create_bookmark_file)
229 char* name = playlist_get_name(NULL, global_temp_buffer,
230 sizeof(global_temp_buffer));
231 if (generate_bookmark_file_name(name))
233 success = add_bookmark(global_bookmark_file_name, bookmark, false);
237 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
238 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
240 return true;
243 /* ----------------------------------------------------------------------- */
244 /* This function adds a bookmark to a file. */
245 /* ------------------------------------------------------------------------*/
246 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
247 bool most_recent)
249 int temp_bookmark_file = 0;
250 int bookmark_file = 0;
251 int bookmark_count = 0;
252 char* playlist = NULL;
253 char* cp;
254 char* tmp;
255 int len = 0;
256 bool unique = false;
258 /* Opening up a temp bookmark file */
259 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
260 "%s.tmp", bookmark_file_name);
261 temp_bookmark_file = open(global_temp_buffer,
262 O_WRONLY | O_CREAT | O_TRUNC);
263 if (temp_bookmark_file < 0)
264 return false; /* can't open the temp file */
266 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
268 playlist = strchr(bookmark,'/');
269 cp = strrchr(bookmark,';');
270 len = cp - playlist;
271 unique = true;
274 /* Writing the new bookmark to the begining of the temp file */
275 write(temp_bookmark_file, bookmark, strlen(bookmark));
276 write(temp_bookmark_file, "\n", 1);
277 bookmark_count++;
279 /* Reading in the previous bookmarks and writing them to the temp file */
280 bookmark_file = open(bookmark_file_name, O_RDONLY);
281 if (bookmark_file >= 0)
283 while (read_line(bookmark_file, global_read_buffer,
284 sizeof(global_read_buffer)) > 0)
286 /* The MRB has a max of MAX_BOOKMARKS in it */
287 /* This keeps it from getting too large */
288 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
289 break;
291 cp = strchr(global_read_buffer,'/');
292 tmp = strrchr(global_read_buffer,';');
293 if (check_bookmark(global_read_buffer) &&
294 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
296 bookmark_count++;
297 write(temp_bookmark_file, global_read_buffer,
298 strlen(global_read_buffer));
299 write(temp_bookmark_file, "\n", 1);
302 close(bookmark_file);
304 close(temp_bookmark_file);
306 remove(bookmark_file_name);
307 rename(global_temp_buffer, bookmark_file_name);
309 return true;
313 /* ----------------------------------------------------------------------- */
314 /* This function takes the system resume data and formats it into a valid */
315 /* bookmark. */
316 /* ----------------------------------------------------------------------- */
317 static char* create_bookmark()
319 int resume_index = 0;
320 char *file;
322 if (!system_check())
323 return NULL; /* something didn't happen correctly, do nothing */
325 /* grab the currently playing track */
326 struct mp3entry *id3 = audio_current_track();
327 if(!id3)
328 return NULL;
330 /* Get some basic resume information */
331 /* queue_resume and queue_resume_index are not used and can be ignored.*/
332 playlist_get_resume_info(&resume_index);
334 /* Get the currently playing file minus the path */
335 /* This is used when displaying the available bookmarks */
336 file = strrchr(id3->path,'/');
337 if(NULL == file)
338 return NULL;
340 /* create the bookmark */
341 snprintf(global_bookmark, sizeof(global_bookmark),
342 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
343 resume_index,
344 id3->offset,
345 playlist_get_seed(NULL),
347 id3->elapsed,
348 global_settings.repeat_mode,
349 global_settings.playlist_shuffle,
350 playlist_get_name(NULL, global_temp_buffer,
351 sizeof(global_temp_buffer)),
352 file+1);
354 /* checking to see if the bookmark is valid */
355 if (check_bookmark(global_bookmark))
356 return global_bookmark;
357 else
358 return NULL;
361 static bool check_bookmark(const char* bookmark)
363 return parse_bookmark(bookmark,
364 NULL,NULL,NULL, NULL,
365 NULL,0,NULL,NULL,
366 NULL, NULL);
369 /* ----------------------------------------------------------------------- */
370 /* This function will determine if an autoload is necessary. This is an */
371 /* interface function. */
372 /* ------------------------------------------------------------------------*/
373 bool bookmark_autoload(const char* file)
375 if(global_settings.autoloadbookmark == BOOKMARK_NO)
376 return false;
378 /*Checking to see if a bookmark file exists.*/
379 if(!generate_bookmark_file_name(file))
381 return false;
384 if(!file_exists(global_bookmark_file_name))
385 return false;
387 if(global_settings.autoloadbookmark == BOOKMARK_YES)
389 return bookmark_load(global_bookmark_file_name, true);
391 else
393 char* bookmark = select_bookmark(global_bookmark_file_name, true);
395 if (bookmark != NULL)
397 if (!play_bookmark(bookmark))
399 /* Selected bookmark not found. */
400 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
403 /* Act as if autoload was done even if it failed, since the
404 * user did make an active selection.
406 return true;
409 return false;
413 /* ----------------------------------------------------------------------- */
414 /* This function loads the bookmark information into the resume memory. */
415 /* This is an interface function. */
416 /* ------------------------------------------------------------------------*/
417 bool bookmark_load(const char* file, bool autoload)
419 int fd;
420 char* bookmark = NULL;
422 if(autoload)
424 fd = open(file, O_RDONLY);
425 if(fd >= 0)
427 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
428 bookmark=global_read_buffer;
429 close(fd);
432 else
434 /* This is not an auto-load, so list the bookmarks */
435 bookmark = select_bookmark(file, false);
438 if (bookmark != NULL)
440 if (!play_bookmark(bookmark))
442 /* Selected bookmark not found. */
443 if (!autoload)
445 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
448 return false;
452 return true;
456 static int get_bookmark_count(const char* bookmark_file_name)
458 int read_count = 0;
459 int file = open(bookmark_file_name, O_RDONLY);
461 if(file < 0)
462 return -1;
464 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
466 read_count++;
469 close(file);
470 return read_count;
473 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
475 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
476 int read_count = 0;
477 int file = open(bookmarks->filename, O_RDONLY);
479 if (file < 0)
481 return -1;
484 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
485 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
487 /* Entire file fits in buffer */
488 first_line = 0;
491 bookmarks->start = first_line;
492 bookmarks->count = 0;
493 bookmarks->reload = false;
495 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
497 read_count++;
499 if (read_count >= first_line)
501 dest -= strlen(global_read_buffer) + 1;
503 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
504 + (sizeof(char*) * (bookmarks->count + 1)))
506 break;
509 strcpy(dest, global_read_buffer);
510 bookmarks->items[bookmarks->count] = dest;
511 bookmarks->count++;
515 close(file);
516 return bookmarks->start + bookmarks->count;
519 static const char* get_bookmark_info(int list_index,
520 void* data,
521 char *buffer,
522 size_t buffer_len)
524 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
525 int index = list_index / 2;
526 int resume_index = 0;
527 long resume_time = 0;
528 bool shuffle = false;
530 if (bookmarks->show_dont_resume)
532 if (index == 0)
534 return list_index % 2 == 0
535 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
538 index--;
541 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
542 || (index < bookmarks->start))
544 int read_index = index;
546 /* Using count as a guide on how far to move could possibly fail
547 * sometimes. Use byte count if that is a problem?
550 if (read_index != 0)
552 /* Move count * 3 / 4 items in the direction the user is moving,
553 * but don't go too close to the end.
555 int offset = bookmarks->count;
556 int max = bookmarks->total_count - (bookmarks->count / 2);
558 if (read_index < bookmarks->start)
560 offset *= 3;
563 read_index = index - offset / 4;
565 if (read_index > max)
567 read_index = max;
570 if (read_index < 0)
572 read_index = 0;
576 if (buffer_bookmarks(bookmarks, read_index) <= index)
578 return "";
582 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
583 &resume_index, NULL, NULL, NULL, global_temp_buffer,
584 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
585 global_filename))
587 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
590 if (list_index % 2 == 0)
592 char *name;
593 char *format;
594 int len = strlen(global_temp_buffer);
596 if (bookmarks->show_playlist_name && len > 0)
598 name = global_temp_buffer;
599 len--;
601 if (name[len] != '/')
603 strrsplt(name, '.');
605 else if (len > 1)
607 name[len] = '\0';
610 if (len > 1)
612 name = strrsplt(name, '/');
615 format = "%s : %s";
617 else
619 name = global_filename;
620 format = "%s";
623 strrsplt(global_filename, '.');
624 snprintf(buffer, buffer_len, format, name, global_filename);
625 return buffer;
627 else
629 char time_buf[32];
631 format_time(time_buf, sizeof(time_buf), resume_time);
632 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
633 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
634 return buffer;
638 static int bookmark_list_voice_cb(int list_index, void* data)
640 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
641 int index = list_index / 2;
643 if (bookmarks->show_dont_resume)
645 if (index == 0)
646 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
647 index--;
649 say_bookmark(bookmarks->items[index - bookmarks->start], index,
650 bookmarks->show_playlist_name);
651 return 0;
654 /* ----------------------------------------------------------------------- */
655 /* This displays a the bookmarks in a file and allows the user to */
656 /* select one to play. */
657 /* ------------------------------------------------------------------------*/
658 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
660 struct bookmark_list* bookmarks;
661 struct gui_synclist list;
662 int item = 0;
663 int action;
664 size_t size;
665 bool exit = false;
666 bool refresh = true;
668 bookmarks = plugin_get_buffer(&size);
669 bookmarks->buffer_size = size;
670 bookmarks->show_dont_resume = show_dont_resume;
671 bookmarks->filename = bookmark_file_name;
672 bookmarks->start = 0;
673 bookmarks->show_playlist_name
674 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
675 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
676 if(global_settings.talk_menu)
677 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
678 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
679 Icon_Bookmark);
681 while (!exit)
684 if (refresh)
686 int count = get_bookmark_count(bookmark_file_name);
687 bookmarks->total_count = count;
689 if (bookmarks->total_count < 1)
691 /* No more bookmarks, delete file and exit */
692 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
693 remove(bookmark_file_name);
694 return NULL;
697 if (bookmarks->show_dont_resume)
699 count++;
700 item++;
703 gui_synclist_set_nb_items(&list, count * 2);
705 if (item >= count)
707 /* Selected item has been deleted */
708 item = count - 1;
709 gui_synclist_select_item(&list, item * 2);
712 buffer_bookmarks(bookmarks, bookmarks->start);
713 gui_synclist_draw(&list);
714 cond_talk_ids_fq(VOICE_EXT_BMARK);
715 gui_synclist_speak_item(&list);
716 refresh = false;
719 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
720 &list, &action, LIST_WRAP_UNLESS_HELD);
721 item = gui_synclist_get_sel_pos(&list) / 2;
723 if (bookmarks->show_dont_resume)
725 item--;
728 if (action == ACTION_STD_CONTEXT)
730 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
731 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
732 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
733 static const int menu_actions[] =
735 ACTION_STD_OK, ACTION_BMS_DELETE
737 int selection = do_menu(&menu_items, NULL, NULL, false);
739 refresh = true;
741 if (selection >= 0 && selection <=
742 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
744 action = menu_actions[selection];
748 switch (action)
750 case ACTION_STD_OK:
751 if (item >= 0)
753 talk_shutup();
754 return bookmarks->items[item - bookmarks->start];
757 /* Else fall through */
759 case ACTION_TREE_WPS:
760 case ACTION_STD_CANCEL:
761 exit = true;
762 break;
764 case ACTION_BMS_DELETE:
765 if (item >= 0)
767 const char *lines[]={
768 ID2P(LANG_REALLY_DELETE)
770 const char *yes_lines[]={
771 ID2P(LANG_DELETING)
774 const struct text_message message={lines, 1};
775 const struct text_message yes_message={yes_lines, 1};
777 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
779 splash(0, str(LANG_DELETING));
780 delete_bookmark(bookmark_file_name, item);
781 bookmarks->reload = true;
783 refresh = true;
785 break;
787 default:
788 if (default_event_handler(action) == SYS_USB_CONNECTED)
790 exit = true;
793 break;
797 talk_shutup();
798 return NULL;
801 /* ----------------------------------------------------------------------- */
802 /* This function takes a location in a bookmark file and deletes that */
803 /* bookmark. */
804 /* ------------------------------------------------------------------------*/
805 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
807 int temp_bookmark_file = 0;
808 int bookmark_file = 0;
809 int bookmark_count = 0;
811 /* Opening up a temp bookmark file */
812 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
813 "%s.tmp", bookmark_file_name);
814 temp_bookmark_file = open(global_temp_buffer,
815 O_WRONLY | O_CREAT | O_TRUNC);
817 if (temp_bookmark_file < 0)
818 return false; /* can't open the temp file */
820 /* Reading in the previous bookmarks and writing them to the temp file */
821 bookmark_file = open(bookmark_file_name, O_RDONLY);
822 if (bookmark_file >= 0)
824 while (read_line(bookmark_file, global_read_buffer,
825 sizeof(global_read_buffer)) > 0)
827 if (bookmark_id != bookmark_count)
829 write(temp_bookmark_file, global_read_buffer,
830 strlen(global_read_buffer));
831 write(temp_bookmark_file, "\n", 1);
833 bookmark_count++;
835 close(bookmark_file);
837 close(temp_bookmark_file);
839 remove(bookmark_file_name);
840 rename(global_temp_buffer, bookmark_file_name);
842 return true;
845 /* ----------------------------------------------------------------------- */
846 /* This function parses a bookmark, says the voice UI part of it. */
847 /* ------------------------------------------------------------------------*/
848 static void say_bookmark(const char* bookmark,
849 int bookmark_id, bool show_playlist_name)
851 int resume_index;
852 long ms;
853 bool playlist_shuffle = false;
854 bool is_dir;
856 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
857 global_temp_buffer,sizeof(global_temp_buffer),
858 &ms, NULL, &playlist_shuffle,
859 global_filename))
861 talk_id(LANG_BOOKMARK_INVALID, false);
862 return;
865 talk_number(bookmark_id + 1, false);
867 is_dir = (global_temp_buffer[0]
868 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
869 #if CONFIG_CODEC == SWCODEC
870 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
871 together, because there is no guarantee that the same mp3
872 parameters are used. */
873 if(show_playlist_name)
874 { /* It's useful to know which playlist this is */
875 if(is_dir)
876 talk_dir_or_spell(global_temp_buffer,
877 TALK_IDARRAY(VOICE_DIR), true);
878 else talk_file_or_spell(NULL, global_temp_buffer,
879 TALK_IDARRAY(LANG_PLAYLIST), true);
881 #else
882 (void)show_playlist_name;
883 #endif
885 if(playlist_shuffle)
886 talk_id(LANG_SHUFFLE, true);
888 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
889 talk_number(resume_index + 1, true);
890 talk_id(LANG_TIME, true);
891 talk_value(ms / 1000, UNIT_TIME, true);
893 #if CONFIG_CODEC == SWCODEC
894 /* Track filename */
895 if(is_dir)
896 talk_file_or_spell(global_temp_buffer, global_filename,
897 TALK_IDARRAY(VOICE_FILE), true);
898 else
899 { /* Unfortunately if this is a playlist, we do not know in which
900 directory the file is and therefore cannot find the track's
901 .talk file. */
902 talk_id(VOICE_FILE, true);
903 talk_spell(global_filename, true);
905 #endif
908 /* ----------------------------------------------------------------------- */
909 /* This function parses a bookmark and then plays it. */
910 /* ------------------------------------------------------------------------*/
911 static bool play_bookmark(const char* bookmark)
913 int index;
914 int offset;
915 int seed;
917 if (parse_bookmark(bookmark,
918 &index,
919 &offset,
920 &seed,
921 NULL,
922 global_temp_buffer,
923 sizeof(global_temp_buffer),
924 NULL,
925 &global_settings.repeat_mode,
926 &global_settings.playlist_shuffle,
927 global_filename))
929 return bookmark_play(global_temp_buffer, index, offset, seed,
930 global_filename);
933 return false;
936 static const char* skip_token(const char* s)
938 while (*s && *s != ';')
940 s++;
943 if (*s)
945 s++;
948 return s;
951 static const char* int_token(const char* s, int* dest)
953 if (dest != NULL)
955 *dest = atoi(s);
958 return skip_token(s);
961 static const char* long_token(const char* s, long* dest)
963 if (dest != NULL)
965 *dest = atoi(s); /* Should be atol, but we don't have it. */
968 return skip_token(s);
971 static const char* bool_token(const char* s, bool* dest)
973 if (dest != NULL)
975 *dest = atoi(s) != 0;
978 return skip_token(s);
981 /* ----------------------------------------------------------------------- */
982 /* This function takes a bookmark and parses it. This function also */
983 /* validates the bookmark. Passing in NULL for an output variable */
984 /* indicates that value is not requested. */
985 /* ----------------------------------------------------------------------- */
986 static bool parse_bookmark(const char *bookmark,
987 int *resume_index,
988 int *resume_offset,
989 int *resume_seed,
990 int *resume_first_index,
991 char* resume_file,
992 unsigned int resume_file_size,
993 long* ms,
994 int * repeat_mode, bool *shuffle,
995 char* file_name)
997 const char* s = bookmark;
998 const char* end;
1000 s = int_token(s, resume_index);
1001 s = int_token(s, resume_offset);
1002 s = int_token(s, resume_seed);
1003 s = int_token(s, resume_first_index);
1004 s = long_token(s, ms);
1005 s = int_token(s, repeat_mode);
1006 s = bool_token(s, shuffle);
1008 if (*s == 0)
1010 return false;
1013 end = strchr(s, ';');
1015 if (resume_file != NULL)
1017 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1018 len = MIN(resume_file_size - 1, len);
1019 strlcpy(resume_file, s, len + 1);
1022 if (end != NULL && file_name != NULL)
1024 end++;
1025 strlcpy(file_name, end, MAX_PATH);
1028 return true;
1031 /* ----------------------------------------------------------------------- */
1032 /* This function is used by multiple functions and is used to generate a */
1033 /* bookmark named based off of the input. */
1034 /* Changing this function could result in how the bookmarks are stored. */
1035 /* it would be here that the centralized/decentralized bookmark code */
1036 /* could be placed. */
1037 /* ----------------------------------------------------------------------- */
1038 static bool generate_bookmark_file_name(const char *in)
1040 int len = strlen(in);
1042 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1043 /* otherwise, name it based on the in variable */
1044 if (!strcmp("/", in))
1045 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1046 else
1048 #ifdef HAVE_MULTIVOLUME
1049 /* The "root" of an extra volume need special handling too. */
1050 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1051 !strcmp("/", global_bookmark_file_name));
1052 #endif
1054 strcpy(global_bookmark_file_name, in);
1055 if(global_bookmark_file_name[len-1] == '/')
1056 len--;
1057 #ifdef HAVE_MULTIVOLUME
1058 if (volume_root)
1059 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1060 else
1061 #endif
1062 strcpy(&global_bookmark_file_name[len], ".bmark");
1065 return true;
1068 /* ----------------------------------------------------------------------- */
1069 /* Returns true if a bookmark file exists for the current playlist */
1070 /* ----------------------------------------------------------------------- */
1071 bool bookmark_exist(void)
1073 bool exist=false;
1075 if(system_check())
1077 char* name = playlist_get_name(NULL, global_temp_buffer,
1078 sizeof(global_temp_buffer));
1079 if (generate_bookmark_file_name(name))
1081 exist = file_exists(global_bookmark_file_name);
1085 return exist;
1088 /* ----------------------------------------------------------------------- */
1089 /* Checks the current state of the system and returns if it is in a */
1090 /* bookmarkable state. */
1091 /* ----------------------------------------------------------------------- */
1092 /* Inputs: */
1093 /* ----------------------------------------------------------------------- */
1094 /* Outputs: */
1095 /* return bool: Indicates if the system was in a bookmarkable state */
1096 /* ----------------------------------------------------------------------- */
1097 static bool system_check(void)
1099 int resume_index = 0;
1101 if (!(audio_status() && audio_current_track()))
1103 /* no track playing */
1104 return false;
1107 /* Checking to see if playing a queued track */
1108 if (playlist_get_resume_info(&resume_index) == -1)
1110 /* something bad happened while getting the queue information */
1111 return false;
1113 else if (playlist_modified(NULL))
1115 /* can't bookmark while in the queue */
1116 return false;
1119 return true;