Fix red introduced with r20610.
[kugel-rb/myfork.git] / apps / bookmark.c
blob9f015f1156d1da3d7453ca8ae448cd4e2098ca63
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 "statusbar.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 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;
169 audio_pause(); /* first pause playback */
170 bookmark = create_bookmark();
171 /* Workaround for inability to speak when paused: all callers will
172 just do audio_stop() when we return, so we can do it right
173 away. This makes it possible to speak the "Create a Bookmark?"
174 prompt and the "Bookmark Created" splash. */
175 audio_stop();
176 switch (global_settings.autocreatebookmark)
178 case BOOKMARK_YES:
179 return write_bookmark(true, bookmark);
181 case BOOKMARK_NO:
182 return false;
184 case BOOKMARK_RECENT_ONLY_YES:
185 return write_bookmark(false, bookmark);
187 #ifdef HAVE_LCD_BITMAP
188 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
189 const struct text_message message={lines, 1};
190 #else
191 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
192 str(LANG_CONFIRM_WITH_BUTTON)};
193 const struct text_message message={lines, 2};
194 #endif
195 #if LCD_DEPTH > 1
196 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
197 #endif
198 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
199 show_remote_main_backdrop();
200 #endif
201 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
203 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
204 return write_bookmark(false, bookmark);
205 else
206 return write_bookmark(true, bookmark);
208 return false;
211 /* ----------------------------------------------------------------------- */
212 /* This function takes the current current resume information and writes */
213 /* that to the beginning of the bookmark file. */
214 /* This file will contain N number of bookmarks in the following format: */
215 /* resume_index*resume_offset*resume_seed*resume_first_index* */
216 /* resume_file*milliseconds*MP3 Title* */
217 /* ------------------------------------------------------------------------*/
218 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
220 bool success=false;
221 if (!bookmark)
222 return false; /* something didn't happen correctly, do nothing */
224 if (global_settings.usemrb)
225 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
228 /* writing the bookmark */
229 if (create_bookmark_file)
231 char* name = playlist_get_name(NULL, global_temp_buffer,
232 sizeof(global_temp_buffer));
233 if (generate_bookmark_file_name(name))
235 success = add_bookmark(global_bookmark_file_name, bookmark, false);
239 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
240 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
242 return true;
245 /* ----------------------------------------------------------------------- */
246 /* This function adds a bookmark to a file. */
247 /* ------------------------------------------------------------------------*/
248 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
249 bool most_recent)
251 int temp_bookmark_file = 0;
252 int bookmark_file = 0;
253 int bookmark_count = 0;
254 char* playlist = NULL;
255 char* cp;
256 char* tmp;
257 int len = 0;
258 bool unique = false;
260 /* Opening up a temp bookmark file */
261 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
262 "%s.tmp", bookmark_file_name);
263 temp_bookmark_file = open(global_temp_buffer,
264 O_WRONLY | O_CREAT | O_TRUNC);
265 if (temp_bookmark_file < 0)
266 return false; /* can't open the temp file */
268 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
270 playlist = strchr(bookmark,'/');
271 cp = strrchr(bookmark,';');
272 len = cp - playlist;
273 unique = true;
276 /* Writing the new bookmark to the begining of the temp file */
277 write(temp_bookmark_file, bookmark, strlen(bookmark));
278 write(temp_bookmark_file, "\n", 1);
279 bookmark_count++;
281 /* Reading in the previous bookmarks and writing them to the temp file */
282 bookmark_file = open(bookmark_file_name, O_RDONLY);
283 if (bookmark_file >= 0)
285 while (read_line(bookmark_file, global_read_buffer,
286 sizeof(global_read_buffer)) > 0)
288 /* The MRB has a max of MAX_BOOKMARKS in it */
289 /* This keeps it from getting too large */
290 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
291 break;
293 cp = strchr(global_read_buffer,'/');
294 tmp = strrchr(global_read_buffer,';');
295 if (check_bookmark(global_read_buffer) &&
296 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
298 bookmark_count++;
299 write(temp_bookmark_file, global_read_buffer,
300 strlen(global_read_buffer));
301 write(temp_bookmark_file, "\n", 1);
304 close(bookmark_file);
306 close(temp_bookmark_file);
308 remove(bookmark_file_name);
309 rename(global_temp_buffer, bookmark_file_name);
311 return true;
315 /* ----------------------------------------------------------------------- */
316 /* This function takes the system resume data and formats it into a valid */
317 /* bookmark. */
318 /* ----------------------------------------------------------------------- */
319 static char* create_bookmark()
321 int resume_index = 0;
322 char *file;
324 if (!system_check())
325 return NULL; /* something didn't happen correctly, do nothing */
327 /* grab the currently playing track */
328 struct mp3entry *id3 = audio_current_track();
329 if(!id3)
330 return NULL;
332 /* Get some basic resume information */
333 /* queue_resume and queue_resume_index are not used and can be ignored.*/
334 playlist_get_resume_info(&resume_index);
336 /* Get the currently playing file minus the path */
337 /* This is used when displaying the available bookmarks */
338 file = strrchr(id3->path,'/');
339 if(NULL == file)
340 return NULL;
342 /* create the bookmark */
343 snprintf(global_bookmark, sizeof(global_bookmark),
344 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
345 resume_index,
346 id3->offset,
347 playlist_get_seed(NULL),
349 id3->elapsed,
350 global_settings.repeat_mode,
351 global_settings.playlist_shuffle,
352 playlist_get_name(NULL, global_temp_buffer,
353 sizeof(global_temp_buffer)),
354 file+1);
356 /* checking to see if the bookmark is valid */
357 if (check_bookmark(global_bookmark))
358 return global_bookmark;
359 else
360 return NULL;
363 static bool check_bookmark(const char* bookmark)
365 return parse_bookmark(bookmark,
366 NULL,NULL,NULL, NULL,
367 NULL,0,NULL,NULL,
368 NULL, NULL);
371 /* ----------------------------------------------------------------------- */
372 /* This function will determine if an autoload is necessary. This is an */
373 /* interface function. */
374 /* ------------------------------------------------------------------------*/
375 bool bookmark_autoload(const char* file)
377 if(global_settings.autoloadbookmark == BOOKMARK_NO)
378 return false;
380 /*Checking to see if a bookmark file exists.*/
381 if(!generate_bookmark_file_name(file))
383 return false;
386 if(!file_exists(global_bookmark_file_name))
387 return false;
389 if(global_settings.autoloadbookmark == BOOKMARK_YES)
391 return bookmark_load(global_bookmark_file_name, true);
393 else
395 char* bookmark = select_bookmark(global_bookmark_file_name, true);
397 if (bookmark != NULL)
399 if (!play_bookmark(bookmark))
401 /* Selected bookmark not found. */
402 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
405 /* Act as if autoload was done even if it failed, since the
406 * user did make an active selection.
408 return true;
411 return false;
415 /* ----------------------------------------------------------------------- */
416 /* This function loads the bookmark information into the resume memory. */
417 /* This is an interface function. */
418 /* ------------------------------------------------------------------------*/
419 bool bookmark_load(const char* file, bool autoload)
421 int fd;
422 char* bookmark = NULL;
424 if(autoload)
426 fd = open(file, O_RDONLY);
427 if(fd >= 0)
429 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
430 bookmark=global_read_buffer;
431 close(fd);
434 else
436 /* This is not an auto-load, so list the bookmarks */
437 bookmark = select_bookmark(file, false);
440 if (bookmark != NULL)
442 if (!play_bookmark(bookmark))
444 /* Selected bookmark not found. */
445 if (!autoload)
447 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
450 return false;
454 return true;
458 static int get_bookmark_count(const char* bookmark_file_name)
460 int read_count = 0;
461 int file = open(bookmark_file_name, O_RDONLY);
463 if(file < 0)
464 return -1;
466 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
468 read_count++;
471 close(file);
472 return read_count;
475 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
477 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
478 int read_count = 0;
479 int file = open(bookmarks->filename, O_RDONLY);
481 if (file < 0)
483 return -1;
486 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
487 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
489 /* Entire file fits in buffer */
490 first_line = 0;
493 bookmarks->start = first_line;
494 bookmarks->count = 0;
495 bookmarks->reload = false;
497 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
499 read_count++;
501 if (read_count >= first_line)
503 dest -= strlen(global_read_buffer) + 1;
505 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
506 + (sizeof(char*) * (bookmarks->count + 1)))
508 break;
511 strcpy(dest, global_read_buffer);
512 bookmarks->items[bookmarks->count] = dest;
513 bookmarks->count++;
517 close(file);
518 return bookmarks->start + bookmarks->count;
521 static char* get_bookmark_info(int list_index,
522 void* data,
523 char *buffer,
524 size_t buffer_len)
526 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
527 int index = list_index / 2;
528 int resume_index = 0;
529 long resume_time = 0;
530 bool shuffle = false;
532 if (bookmarks->show_dont_resume)
534 if (index == 0)
536 return list_index % 2 == 0
537 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
540 index--;
543 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
544 || (index < bookmarks->start))
546 int read_index = index;
548 /* Using count as a guide on how far to move could possibly fail
549 * sometimes. Use byte count if that is a problem?
552 if (read_index != 0)
554 /* Move count * 3 / 4 items in the direction the user is moving,
555 * but don't go too close to the end.
557 int offset = bookmarks->count;
558 int max = bookmarks->total_count - (bookmarks->count / 2);
560 if (read_index < bookmarks->start)
562 offset *= 3;
565 read_index = index - offset / 4;
567 if (read_index > max)
569 read_index = max;
572 if (read_index < 0)
574 read_index = 0;
578 if (buffer_bookmarks(bookmarks, read_index) <= index)
580 return "";
584 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
585 &resume_index, NULL, NULL, NULL, global_temp_buffer,
586 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
587 global_filename))
589 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
592 if (list_index % 2 == 0)
594 char *name;
595 char *format;
596 int len = strlen(global_temp_buffer);
598 if (bookmarks->show_playlist_name && len > 0)
600 name = global_temp_buffer;
601 len--;
603 if (name[len] != '/')
605 strrsplt(name, '.');
607 else if (len > 1)
609 name[len] = '\0';
612 if (len > 1)
614 name = strrsplt(name, '/');
617 format = "%s : %s";
619 else
621 name = global_filename;
622 format = "%s";
625 strrsplt(global_filename, '.');
626 snprintf(buffer, buffer_len, format, name, global_filename);
627 return buffer;
629 else
631 char time_buf[32];
633 format_time(time_buf, sizeof(time_buf), resume_time);
634 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
635 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
636 return buffer;
640 static int bookmark_list_voice_cb(int list_index, void* data)
642 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
643 int index = list_index / 2;
645 if (bookmarks->show_dont_resume)
647 if (index == 0)
648 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
649 index--;
651 say_bookmark(bookmarks->items[index - bookmarks->start], index,
652 bookmarks->show_playlist_name);
653 return 0;
656 /* ----------------------------------------------------------------------- */
657 /* This displays a the bookmarks in a file and allows the user to */
658 /* select one to play. */
659 /* ------------------------------------------------------------------------*/
660 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
662 struct bookmark_list* bookmarks;
663 struct gui_synclist list;
664 int item = 0;
665 int action;
666 size_t size;
667 bool exit = false;
668 bool refresh = true;
670 bookmarks = plugin_get_buffer(&size);
671 bookmarks->buffer_size = size;
672 bookmarks->show_dont_resume = show_dont_resume;
673 bookmarks->filename = bookmark_file_name;
674 bookmarks->start = 0;
675 bookmarks->show_playlist_name
676 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
677 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
678 if(global_settings.talk_menu)
679 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
680 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
681 Icon_Bookmark);
683 while (!exit)
686 if (refresh)
688 int count = get_bookmark_count(bookmark_file_name);
689 bookmarks->total_count = count;
691 if (bookmarks->total_count < 1)
693 /* No more bookmarks, delete file and exit */
694 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
695 remove(bookmark_file_name);
696 return NULL;
699 if (bookmarks->show_dont_resume)
701 count++;
702 item++;
705 gui_synclist_set_nb_items(&list, count * 2);
707 if (item >= count)
709 /* Selected item has been deleted */
710 item = count - 1;
711 gui_synclist_select_item(&list, item * 2);
714 buffer_bookmarks(bookmarks, bookmarks->start);
715 gui_synclist_draw(&list);
716 cond_talk_ids_fq(VOICE_EXT_BMARK);
717 gui_synclist_speak_item(&list);
718 refresh = false;
721 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
722 &list, &action, LIST_WRAP_UNLESS_HELD);
723 item = gui_synclist_get_sel_pos(&list) / 2;
725 if (bookmarks->show_dont_resume)
727 item--;
730 if (action == ACTION_STD_CONTEXT)
732 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
733 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
734 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
735 static const int menu_actions[] =
737 ACTION_STD_OK, ACTION_BMS_DELETE
739 int selection = do_menu(&menu_items, NULL, NULL, false);
741 refresh = true;
743 if (selection >= 0 && selection <=
744 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
746 action = menu_actions[selection];
750 switch (action)
752 case ACTION_STD_OK:
753 if (item >= 0)
755 talk_shutup();
756 return bookmarks->items[item - bookmarks->start];
759 /* Else fall through */
761 case ACTION_TREE_WPS:
762 case ACTION_STD_CANCEL:
763 exit = true;
764 break;
766 case ACTION_BMS_DELETE:
767 if (item >= 0)
769 delete_bookmark(bookmark_file_name, item);
770 bookmarks->reload = true;
771 refresh = true;
773 break;
775 default:
776 if (default_event_handler(action) == SYS_USB_CONNECTED)
778 exit = true;
781 break;
785 talk_shutup();
786 return NULL;
789 /* ----------------------------------------------------------------------- */
790 /* This function takes a location in a bookmark file and deletes that */
791 /* bookmark. */
792 /* ------------------------------------------------------------------------*/
793 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
795 int temp_bookmark_file = 0;
796 int bookmark_file = 0;
797 int bookmark_count = 0;
799 /* Opening up a temp bookmark file */
800 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
801 "%s.tmp", bookmark_file_name);
802 temp_bookmark_file = open(global_temp_buffer,
803 O_WRONLY | O_CREAT | O_TRUNC);
805 if (temp_bookmark_file < 0)
806 return false; /* can't open the temp file */
808 /* Reading in the previous bookmarks and writing them to the temp file */
809 bookmark_file = open(bookmark_file_name, O_RDONLY);
810 if (bookmark_file >= 0)
812 while (read_line(bookmark_file, global_read_buffer,
813 sizeof(global_read_buffer)) > 0)
815 if (bookmark_id != bookmark_count)
817 write(temp_bookmark_file, global_read_buffer,
818 strlen(global_read_buffer));
819 write(temp_bookmark_file, "\n", 1);
821 bookmark_count++;
823 close(bookmark_file);
825 close(temp_bookmark_file);
827 remove(bookmark_file_name);
828 rename(global_temp_buffer, bookmark_file_name);
830 return true;
833 /* ----------------------------------------------------------------------- */
834 /* This function parses a bookmark, says the voice UI part of it. */
835 /* ------------------------------------------------------------------------*/
836 static void say_bookmark(const char* bookmark,
837 int bookmark_id, bool show_playlist_name)
839 int resume_index;
840 long ms;
841 bool playlist_shuffle = false;
842 bool is_dir;
844 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
845 global_temp_buffer,sizeof(global_temp_buffer),
846 &ms, NULL, &playlist_shuffle,
847 global_filename))
849 talk_id(LANG_BOOKMARK_INVALID, false);
850 return;
853 talk_number(bookmark_id + 1, false);
855 is_dir = (global_temp_buffer[0]
856 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
857 #if CONFIG_CODEC == SWCODEC
858 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
859 together, because there is no guarantee that the same mp3
860 parameters are used. */
861 if(show_playlist_name)
862 { /* It's useful to know which playlist this is */
863 if(is_dir)
864 talk_dir_or_spell(global_temp_buffer,
865 TALK_IDARRAY(VOICE_DIR), true);
866 else talk_file_or_spell(NULL, global_temp_buffer,
867 TALK_IDARRAY(LANG_PLAYLIST), true);
869 #else
870 (void)show_playlist_name;
871 #endif
873 if(playlist_shuffle)
874 talk_id(LANG_SHUFFLE, true);
876 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
877 talk_number(resume_index + 1, true);
878 talk_id(LANG_TIME, true);
879 talk_value(ms / 1000, UNIT_TIME, true);
881 #if CONFIG_CODEC == SWCODEC
882 /* Track filename */
883 if(is_dir)
884 talk_file_or_spell(global_temp_buffer, global_filename,
885 TALK_IDARRAY(VOICE_FILE), true);
886 else
887 { /* Unfortunately if this is a playlist, we do not know in which
888 directory the file is and therefore cannot find the track's
889 .talk file. */
890 talk_id(VOICE_FILE, true);
891 talk_spell(global_filename, true);
893 #endif
896 /* ----------------------------------------------------------------------- */
897 /* This function parses a bookmark and then plays it. */
898 /* ------------------------------------------------------------------------*/
899 static bool play_bookmark(const char* bookmark)
901 int index;
902 int offset;
903 int seed;
905 if (parse_bookmark(bookmark,
906 &index,
907 &offset,
908 &seed,
909 NULL,
910 global_temp_buffer,
911 sizeof(global_temp_buffer),
912 NULL,
913 &global_settings.repeat_mode,
914 &global_settings.playlist_shuffle,
915 global_filename))
917 return bookmark_play(global_temp_buffer, index, offset, seed,
918 global_filename);
921 return false;
924 static const char* skip_token(const char* s)
926 while (*s && *s != ';')
928 s++;
931 if (*s)
933 s++;
936 return s;
939 static const char* int_token(const char* s, int* dest)
941 if (dest != NULL)
943 *dest = atoi(s);
946 return skip_token(s);
949 static const char* long_token(const char* s, long* dest)
951 if (dest != NULL)
953 *dest = atoi(s); /* Should be atol, but we don't have it. */
956 return skip_token(s);
959 static const char* bool_token(const char* s, bool* dest)
961 if (dest != NULL)
963 *dest = atoi(s) != 0;
966 return skip_token(s);
969 /* ----------------------------------------------------------------------- */
970 /* This function takes a bookmark and parses it. This function also */
971 /* validates the bookmark. Passing in NULL for an output variable */
972 /* indicates that value is not requested. */
973 /* ----------------------------------------------------------------------- */
974 static bool parse_bookmark(const char *bookmark,
975 int *resume_index,
976 int *resume_offset,
977 int *resume_seed,
978 int *resume_first_index,
979 char* resume_file,
980 unsigned int resume_file_size,
981 long* ms,
982 int * repeat_mode, bool *shuffle,
983 char* file_name)
985 const char* s = bookmark;
986 const char* end;
988 s = int_token(s, resume_index);
989 s = int_token(s, resume_offset);
990 s = int_token(s, resume_seed);
991 s = int_token(s, resume_first_index);
992 s = long_token(s, ms);
993 s = int_token(s, repeat_mode);
994 s = bool_token(s, shuffle);
996 if (*s == 0)
998 return false;
1001 end = strchr(s, ';');
1003 if (resume_file != NULL)
1005 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1007 len = MIN(resume_file_size - 1, len);
1008 strncpy(resume_file, s, len);
1009 resume_file[len] = 0;
1012 if (end != NULL && file_name != NULL)
1014 end++;
1015 strncpy(file_name, end, MAX_PATH - 1);
1016 file_name[MAX_PATH - 1] = 0;
1019 return true;
1022 /* ----------------------------------------------------------------------- */
1023 /* This function is used by multiple functions and is used to generate a */
1024 /* bookmark named based off of the input. */
1025 /* Changing this function could result in how the bookmarks are stored. */
1026 /* it would be here that the centralized/decentralized bookmark code */
1027 /* could be placed. */
1028 /* ----------------------------------------------------------------------- */
1029 static bool generate_bookmark_file_name(const char *in)
1031 int len = strlen(in);
1033 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1034 /* otherwise, name it based on the in variable */
1035 if (!strcmp("/", in))
1036 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1037 else
1039 #ifdef HAVE_MULTIVOLUME
1040 /* The "root" of an extra volume need special handling too. */
1041 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1042 !strcmp("/", global_bookmark_file_name));
1043 #endif
1045 strcpy(global_bookmark_file_name, in);
1046 if(global_bookmark_file_name[len-1] == '/')
1047 len--;
1048 #ifdef HAVE_MULTIVOLUME
1049 if (volume_root)
1050 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1051 else
1052 #endif
1053 strcpy(&global_bookmark_file_name[len], ".bmark");
1056 return true;
1059 /* ----------------------------------------------------------------------- */
1060 /* Returns true if a bookmark file exists for the current playlist */
1061 /* ----------------------------------------------------------------------- */
1062 bool bookmark_exist(void)
1064 bool exist=false;
1066 if(system_check())
1068 char* name = playlist_get_name(NULL, global_temp_buffer,
1069 sizeof(global_temp_buffer));
1070 if (generate_bookmark_file_name(name))
1072 exist = file_exists(global_bookmark_file_name);
1076 return exist;
1079 /* ----------------------------------------------------------------------- */
1080 /* Checks the current state of the system and returns if it is in a */
1081 /* bookmarkable state. */
1082 /* ----------------------------------------------------------------------- */
1083 /* Inputs: */
1084 /* ----------------------------------------------------------------------- */
1085 /* Outputs: */
1086 /* return bool: Indicates if the system was in a bookmarkable state */
1087 /* ----------------------------------------------------------------------- */
1088 static bool system_check(void)
1090 int resume_index = 0;
1092 if (!(audio_status() && audio_current_track()))
1094 /* no track playing */
1095 return false;
1098 /* Checking to see if playing a queued track */
1099 if (playlist_get_resume_info(&resume_index) == -1)
1101 /* something bad happened while getting the queue information */
1102 return false;
1104 else if (playlist_modified(NULL))
1106 /* can't bookmark while in the queue */
1107 return false;
1110 return true;