Add placeholder for the remote hotkey (aka repair WPS keymap table)
[kugel-rb.git] / apps / bookmark.c
blob1b82966be8a4ac911052fba893c4589ddaff3f4d
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 "file.h"
44 #include "filefuncs.h"
46 #define MAX_BOOKMARKS 10
47 #define MAX_BOOKMARK_SIZE 350
48 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
50 /* Used to buffer bookmarks while displaying the bookmark list. */
51 struct bookmark_list
53 const char* filename;
54 size_t buffer_size;
55 int start;
56 int count;
57 int total_count;
58 bool show_dont_resume;
59 bool reload;
60 bool show_playlist_name;
61 char* items[];
64 /* bookmark flags */
65 #define F_BMFILES 0x01
67 /* bookmark values */
68 static struct {
69 int resume_index;
70 unsigned long resume_offset;
71 int resume_seed;
72 long resume_time;
73 int repeat_mode;
74 bool shuffle;
75 } bm;
77 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
78 bool most_recent);
79 static char* create_bookmark(void);
80 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
81 static void say_bookmark(const char* bookmark,
82 int bookmark_id, bool show_playlist_name);
83 static bool play_bookmark(const char* bookmark);
84 static bool generate_bookmark_file_name(const char *in);
85 static bool parse_bookmark(const char *bookmark, const int flags);
86 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
87 static const char* get_bookmark_info(int list_index,
88 void* data,
89 char *buffer,
90 size_t buffer_len);
91 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
92 static bool system_check(void);
93 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
94 static int get_bookmark_count(const char* bookmark_file_name);
96 #define TEMP_BUF_SIZE (MAX_PATH + 1)
97 static char global_temp_buffer[TEMP_BUF_SIZE];
98 /* File name created by generate_bookmark_file_name */
99 static char global_bookmark_file_name[MAX_PATH];
100 static char global_read_buffer[MAX_BOOKMARK_SIZE];
101 /* Bookmark created by create_bookmark*/
102 static char global_bookmark[MAX_BOOKMARK_SIZE];
103 /* Filename from parsed bookmark (can be made local where needed) */
104 static char global_filename[MAX_PATH];
106 /* ----------------------------------------------------------------------- */
107 /* This is the interface function from the main menu. */
108 /* ----------------------------------------------------------------------- */
109 bool bookmark_create_menu(void)
111 write_bookmark(true, create_bookmark());
112 return false;
115 /* ----------------------------------------------------------------------- */
116 /* This function acts as the load interface from the main menu */
117 /* This function determines the bookmark file name and then loads that file*/
118 /* for the user. The user can then select a bookmark to load. */
119 /* If no file/directory is currently playing, the menu item does not work. */
120 /* ----------------------------------------------------------------------- */
121 bool bookmark_load_menu(void)
123 if (system_check())
125 char* name = playlist_get_name(NULL, global_temp_buffer,
126 sizeof(global_temp_buffer));
127 if (generate_bookmark_file_name(name))
129 char* bookmark = select_bookmark(global_bookmark_file_name, false);
131 if (bookmark != NULL)
133 return play_bookmark(bookmark);
138 return false;
141 /* ----------------------------------------------------------------------- */
142 /* Gives the user a list of the Most Recent Bookmarks. This is an */
143 /* interface function */
144 /* ----------------------------------------------------------------------- */
145 bool bookmark_mrb_load()
147 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
149 if (bookmark != NULL)
151 return play_bookmark(bookmark);
154 return false;
157 /* ----------------------------------------------------------------------- */
158 /* This function handles an autobookmark creation. This is an interface */
159 /* function. */
160 /* ----------------------------------------------------------------------- */
161 bool bookmark_autobookmark(bool prompt_ok)
163 char* bookmark;
164 if (!system_check())
165 return false;
167 audio_pause(); /* first pause playback */
168 bookmark = create_bookmark();
169 /* Workaround for inability to speak when paused: all callers will
170 just do audio_stop() when we return, so we can do it right
171 away. This makes it possible to speak the "Create a Bookmark?"
172 prompt and the "Bookmark Created" splash. */
173 audio_stop();
174 switch (global_settings.autocreatebookmark)
176 case BOOKMARK_YES:
177 return write_bookmark(true, bookmark);
179 case BOOKMARK_NO:
180 return false;
182 case BOOKMARK_RECENT_ONLY_YES:
183 return write_bookmark(false, bookmark);
185 #ifdef HAVE_LCD_BITMAP
186 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
187 const struct text_message message={lines, 1};
188 #else
189 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
190 str(LANG_CONFIRM_WITH_BUTTON)};
191 const struct text_message message={lines, 2};
192 #endif
194 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
196 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
197 return write_bookmark(false, bookmark);
198 else
199 return write_bookmark(true, bookmark);
201 return false;
204 /* ----------------------------------------------------------------------- */
205 /* This function takes the current current resume information and writes */
206 /* that to the beginning of the bookmark file. */
207 /* This file will contain N number of bookmarks in the following format: */
208 /* resume_index*resume_offset*resume_seed*resume_first_index* */
209 /* resume_file*milliseconds*MP3 Title* */
210 /* ------------------------------------------------------------------------*/
211 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
213 bool success=false;
214 if (!bookmark)
215 return false; /* something didn't happen correctly, do nothing */
217 if (global_settings.usemrb)
218 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
221 /* writing the bookmark */
222 if (create_bookmark_file)
224 char* name = playlist_get_name(NULL, global_temp_buffer,
225 sizeof(global_temp_buffer));
226 if (generate_bookmark_file_name(name))
228 success = add_bookmark(global_bookmark_file_name, bookmark, false);
232 splash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
233 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
235 return true;
238 /* ----------------------------------------------------------------------- */
239 /* This function adds a bookmark to a file. */
240 /* ------------------------------------------------------------------------*/
241 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
242 bool most_recent)
244 int temp_bookmark_file = 0;
245 int bookmark_file = 0;
246 int bookmark_count = 0;
247 char* playlist = NULL;
248 char* cp;
249 char* tmp;
250 int len = 0;
251 bool unique = false;
253 /* Opening up a temp bookmark file */
254 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
255 "%s.tmp", bookmark_file_name);
256 temp_bookmark_file = open(global_temp_buffer,
257 O_WRONLY | O_CREAT | O_TRUNC);
258 if (temp_bookmark_file < 0)
259 return false; /* can't open the temp file */
261 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
263 playlist = strchr(bookmark,'/');
264 cp = strrchr(bookmark,';');
265 len = cp - playlist;
266 unique = true;
269 /* Writing the new bookmark to the begining of the temp file */
270 write(temp_bookmark_file, bookmark, strlen(bookmark));
271 write(temp_bookmark_file, "\n", 1);
272 bookmark_count++;
274 /* Reading in the previous bookmarks and writing them to the temp file */
275 bookmark_file = open(bookmark_file_name, O_RDONLY);
276 if (bookmark_file >= 0)
278 while (read_line(bookmark_file, global_read_buffer,
279 sizeof(global_read_buffer)) > 0)
281 /* The MRB has a max of MAX_BOOKMARKS in it */
282 /* This keeps it from getting too large */
283 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
284 break;
286 cp = strchr(global_read_buffer,'/');
287 tmp = strrchr(global_read_buffer,';');
288 if (parse_bookmark(global_read_buffer, 0) &&
289 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
291 bookmark_count++;
292 write(temp_bookmark_file, global_read_buffer,
293 strlen(global_read_buffer));
294 write(temp_bookmark_file, "\n", 1);
297 close(bookmark_file);
299 close(temp_bookmark_file);
301 remove(bookmark_file_name);
302 rename(global_temp_buffer, bookmark_file_name);
304 return true;
308 /* ----------------------------------------------------------------------- */
309 /* This function takes the system resume data and formats it into a valid */
310 /* bookmark. */
311 /* ----------------------------------------------------------------------- */
312 static char* create_bookmark()
314 int resume_index = 0;
315 char *file;
317 if (!system_check())
318 return NULL; /* something didn't happen correctly, do nothing */
320 /* grab the currently playing track */
321 struct mp3entry *id3 = audio_current_track();
322 if(!id3)
323 return NULL;
325 /* Get some basic resume information */
326 /* queue_resume and queue_resume_index are not used and can be ignored.*/
327 playlist_get_resume_info(&resume_index);
329 /* Get the currently playing file minus the path */
330 /* This is used when displaying the available bookmarks */
331 file = strrchr(id3->path,'/');
332 if(NULL == file)
333 return NULL;
335 /* create the bookmark */
336 snprintf(global_bookmark, sizeof(global_bookmark),
337 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
338 resume_index,
339 id3->offset,
340 playlist_get_seed(NULL),
342 id3->elapsed,
343 global_settings.repeat_mode,
344 global_settings.playlist_shuffle,
345 playlist_get_name(NULL, global_temp_buffer,
346 sizeof(global_temp_buffer)),
347 file+1);
349 /* checking to see if the bookmark is valid */
350 if (parse_bookmark(global_bookmark, 0))
351 return global_bookmark;
352 else
353 return NULL;
356 /* ----------------------------------------------------------------------- */
357 /* This function will determine if an autoload is necessary. This is an */
358 /* interface function. */
359 /* ------------------------------------------------------------------------*/
360 bool bookmark_autoload(const char* file)
362 if(global_settings.autoloadbookmark == BOOKMARK_NO)
363 return false;
365 /*Checking to see if a bookmark file exists.*/
366 if(!generate_bookmark_file_name(file))
368 return false;
371 if(!file_exists(global_bookmark_file_name))
372 return false;
374 if(global_settings.autoloadbookmark == BOOKMARK_YES)
376 return bookmark_load(global_bookmark_file_name, true);
378 else
380 char* bookmark = select_bookmark(global_bookmark_file_name, true);
382 if (bookmark != NULL)
384 if (!play_bookmark(bookmark))
386 /* Selected bookmark not found. */
387 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
390 /* Act as if autoload was done even if it failed, since the
391 * user did make an active selection.
393 return true;
396 return false;
400 /* ----------------------------------------------------------------------- */
401 /* This function loads the bookmark information into the resume memory. */
402 /* This is an interface function. */
403 /* ------------------------------------------------------------------------*/
404 bool bookmark_load(const char* file, bool autoload)
406 int fd;
407 char* bookmark = NULL;
409 if(autoload)
411 fd = open(file, O_RDONLY);
412 if(fd >= 0)
414 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
415 bookmark=global_read_buffer;
416 close(fd);
419 else
421 /* This is not an auto-load, so list the bookmarks */
422 bookmark = select_bookmark(file, false);
425 if (bookmark != NULL)
427 if (!play_bookmark(bookmark))
429 /* Selected bookmark not found. */
430 if (!autoload)
432 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
435 return false;
439 return true;
443 static int get_bookmark_count(const char* bookmark_file_name)
445 int read_count = 0;
446 int file = open(bookmark_file_name, O_RDONLY);
448 if(file < 0)
449 return -1;
451 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
453 read_count++;
456 close(file);
457 return read_count;
460 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
462 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
463 int read_count = 0;
464 int file = open(bookmarks->filename, O_RDONLY);
466 if (file < 0)
468 return -1;
471 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
472 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
474 /* Entire file fits in buffer */
475 first_line = 0;
478 bookmarks->start = first_line;
479 bookmarks->count = 0;
480 bookmarks->reload = false;
482 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
484 read_count++;
486 if (read_count >= first_line)
488 dest -= strlen(global_read_buffer) + 1;
490 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
491 + (sizeof(char*) * (bookmarks->count + 1)))
493 break;
496 strcpy(dest, global_read_buffer);
497 bookmarks->items[bookmarks->count] = dest;
498 bookmarks->count++;
502 close(file);
503 return bookmarks->start + bookmarks->count;
506 static const char* get_bookmark_info(int list_index,
507 void* data,
508 char *buffer,
509 size_t buffer_len)
511 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
512 int index = list_index / 2;
514 if (bookmarks->show_dont_resume)
516 if (index == 0)
518 return list_index % 2 == 0
519 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
522 index--;
525 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
526 || (index < bookmarks->start))
528 int read_index = index;
530 /* Using count as a guide on how far to move could possibly fail
531 * sometimes. Use byte count if that is a problem?
534 if (read_index != 0)
536 /* Move count * 3 / 4 items in the direction the user is moving,
537 * but don't go too close to the end.
539 int offset = bookmarks->count;
540 int max = bookmarks->total_count - (bookmarks->count / 2);
542 if (read_index < bookmarks->start)
544 offset *= 3;
547 read_index = index - offset / 4;
549 if (read_index > max)
551 read_index = max;
554 if (read_index < 0)
556 read_index = 0;
560 if (buffer_bookmarks(bookmarks, read_index) <= index)
562 return "";
566 const int flags = F_BMFILES;
568 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], flags))
570 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
573 if (list_index % 2 == 0)
575 char *name;
576 char *format;
577 int len = strlen(global_temp_buffer);
579 if (bookmarks->show_playlist_name && len > 0)
581 name = global_temp_buffer;
582 len--;
584 if (name[len] != '/')
586 strrsplt(name, '.');
588 else if (len > 1)
590 name[len] = '\0';
593 if (len > 1)
595 name = strrsplt(name, '/');
598 format = "%s : %s";
600 else
602 name = global_filename;
603 format = "%s";
606 strrsplt(global_filename, '.');
607 snprintf(buffer, buffer_len, format, name, global_filename);
608 return buffer;
610 else
612 char time_buf[32];
614 format_time(time_buf, sizeof(time_buf), bm.resume_time);
615 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
616 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
617 return buffer;
621 static int bookmark_list_voice_cb(int list_index, void* data)
623 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
624 int index = list_index / 2;
626 if (bookmarks->show_dont_resume)
628 if (index == 0)
629 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
630 index--;
632 say_bookmark(bookmarks->items[index - bookmarks->start], index,
633 bookmarks->show_playlist_name);
634 return 0;
637 /* ----------------------------------------------------------------------- */
638 /* This displays a the bookmarks in a file and allows the user to */
639 /* select one to play. */
640 /* ------------------------------------------------------------------------*/
641 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
643 struct bookmark_list* bookmarks;
644 struct gui_synclist list;
645 int item = 0;
646 int action;
647 size_t size;
648 bool exit = false;
649 bool refresh = true;
651 bookmarks = plugin_get_buffer(&size);
652 bookmarks->buffer_size = size;
653 bookmarks->show_dont_resume = show_dont_resume;
654 bookmarks->filename = bookmark_file_name;
655 bookmarks->start = 0;
656 bookmarks->show_playlist_name
657 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
658 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
659 if(global_settings.talk_menu)
660 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
661 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
662 Icon_Bookmark);
664 while (!exit)
667 if (refresh)
669 int count = get_bookmark_count(bookmark_file_name);
670 bookmarks->total_count = count;
672 if (bookmarks->total_count < 1)
674 /* No more bookmarks, delete file and exit */
675 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
676 remove(bookmark_file_name);
677 return NULL;
680 if (bookmarks->show_dont_resume)
682 count++;
683 item++;
686 gui_synclist_set_nb_items(&list, count * 2);
688 if (item >= count)
690 /* Selected item has been deleted */
691 item = count - 1;
692 gui_synclist_select_item(&list, item * 2);
695 buffer_bookmarks(bookmarks, bookmarks->start);
696 gui_synclist_draw(&list);
697 cond_talk_ids_fq(VOICE_EXT_BMARK);
698 gui_synclist_speak_item(&list);
699 refresh = false;
702 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
703 &list, &action, LIST_WRAP_UNLESS_HELD);
704 item = gui_synclist_get_sel_pos(&list) / 2;
706 if (bookmarks->show_dont_resume)
708 item--;
711 if (action == ACTION_STD_CONTEXT)
713 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
714 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
715 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
716 static const int menu_actions[] =
718 ACTION_STD_OK, ACTION_BMS_DELETE
720 int selection = do_menu(&menu_items, NULL, NULL, false);
722 refresh = true;
724 if (selection >= 0 && selection <=
725 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
727 action = menu_actions[selection];
731 switch (action)
733 case ACTION_STD_OK:
734 if (item >= 0)
736 talk_shutup();
737 return bookmarks->items[item - bookmarks->start];
740 /* Else fall through */
742 case ACTION_TREE_WPS:
743 case ACTION_STD_CANCEL:
744 exit = true;
745 break;
747 case ACTION_BMS_DELETE:
748 if (item >= 0)
750 const char *lines[]={
751 ID2P(LANG_REALLY_DELETE)
753 const char *yes_lines[]={
754 ID2P(LANG_DELETING)
757 const struct text_message message={lines, 1};
758 const struct text_message yes_message={yes_lines, 1};
760 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
762 delete_bookmark(bookmark_file_name, item);
763 bookmarks->reload = true;
765 refresh = true;
767 break;
769 default:
770 if (default_event_handler(action) == SYS_USB_CONNECTED)
772 exit = true;
775 break;
779 talk_shutup();
780 return NULL;
783 /* ----------------------------------------------------------------------- */
784 /* This function takes a location in a bookmark file and deletes that */
785 /* bookmark. */
786 /* ------------------------------------------------------------------------*/
787 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
789 int temp_bookmark_file = 0;
790 int bookmark_file = 0;
791 int bookmark_count = 0;
793 /* Opening up a temp bookmark file */
794 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
795 "%s.tmp", bookmark_file_name);
796 temp_bookmark_file = open(global_temp_buffer,
797 O_WRONLY | O_CREAT | O_TRUNC);
799 if (temp_bookmark_file < 0)
800 return false; /* can't open the temp file */
802 /* Reading in the previous bookmarks and writing them to the temp file */
803 bookmark_file = open(bookmark_file_name, O_RDONLY);
804 if (bookmark_file >= 0)
806 while (read_line(bookmark_file, global_read_buffer,
807 sizeof(global_read_buffer)) > 0)
809 if (bookmark_id != bookmark_count)
811 write(temp_bookmark_file, global_read_buffer,
812 strlen(global_read_buffer));
813 write(temp_bookmark_file, "\n", 1);
815 bookmark_count++;
817 close(bookmark_file);
819 close(temp_bookmark_file);
821 remove(bookmark_file_name);
822 rename(global_temp_buffer, bookmark_file_name);
824 return true;
827 /* ----------------------------------------------------------------------- */
828 /* This function parses a bookmark, says the voice UI part of it. */
829 /* ------------------------------------------------------------------------*/
830 static void say_bookmark(const char* bookmark,
831 int bookmark_id, bool show_playlist_name)
833 bool is_dir;
835 const int flags = F_BMFILES;
837 if (!parse_bookmark(bookmark, flags))
839 talk_id(LANG_BOOKMARK_INVALID, false);
840 return;
843 talk_number(bookmark_id + 1, false);
845 is_dir = (global_temp_buffer[0]
846 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
847 #if CONFIG_CODEC == SWCODEC
848 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
849 together, because there is no guarantee that the same mp3
850 parameters are used. */
851 if(show_playlist_name)
852 { /* It's useful to know which playlist this is */
853 if(is_dir)
854 talk_dir_or_spell(global_temp_buffer,
855 TALK_IDARRAY(VOICE_DIR), true);
856 else talk_file_or_spell(NULL, global_temp_buffer,
857 TALK_IDARRAY(LANG_PLAYLIST), true);
859 #else
860 (void)show_playlist_name;
861 #endif
863 if(bm.shuffle)
864 talk_id(LANG_SHUFFLE, true);
866 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
867 talk_number(bm.resume_index + 1, true);
868 talk_id(LANG_TIME, true);
869 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
871 #if CONFIG_CODEC == SWCODEC
872 /* Track filename */
873 if(is_dir)
874 talk_file_or_spell(global_temp_buffer, global_filename,
875 TALK_IDARRAY(VOICE_FILE), true);
876 else
877 { /* Unfortunately if this is a playlist, we do not know in which
878 directory the file is and therefore cannot find the track's
879 .talk file. */
880 talk_id(VOICE_FILE, true);
881 talk_spell(global_filename, true);
883 #endif
886 /* ----------------------------------------------------------------------- */
887 /* This function parses a bookmark and then plays it. */
888 /* ------------------------------------------------------------------------*/
889 static bool play_bookmark(const char* bookmark)
891 const int flags = F_BMFILES;
893 if (parse_bookmark(bookmark, flags))
895 global_settings.repeat_mode = bm.repeat_mode;
896 global_settings.playlist_shuffle = bm.shuffle;
897 return bookmark_play(global_temp_buffer, bm.resume_index,
898 bm.resume_offset, bm.resume_seed, global_filename);
901 return false;
904 static const char* skip_token(const char* s)
906 while (*s && *s != ';')
908 s++;
911 if (*s)
913 s++;
916 return s;
919 static const char* long_token(const char* s, long* dest)
921 *dest = atoi(s); /* Should be atol, but we don't have it. */
922 return skip_token(s);
925 /* ----------------------------------------------------------------------- */
926 /* This function takes a bookmark and parses it. This function also */
927 /* validates the bookmark. Flags are set to indicate which bookmark */
928 /* tokens are to be processed. */
929 /* ----------------------------------------------------------------------- */
930 static bool parse_bookmark(const char *bookmark, const int flags)
932 const char* s = bookmark;
933 const char* end;
935 #define FLAG(a) (flags & a)
936 #define GET_INT_TOKEN(var) s = long_token(s, (long *)&var)
937 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
939 GET_INT_TOKEN(bm.resume_index);
940 GET_INT_TOKEN(bm.resume_offset);
941 GET_INT_TOKEN(bm.resume_seed);
942 /* skip deprecated token */
943 s = skip_token(s);
944 GET_INT_TOKEN(bm.resume_time);
945 GET_INT_TOKEN(bm.repeat_mode);
946 GET_BOOL_TOKEN(bm.shuffle);
948 if (*s == 0)
950 return false;
953 end = strchr(s, ';');
955 /* extract file names */
956 if (FLAG(F_BMFILES))
958 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
959 len = MIN(TEMP_BUF_SIZE - 1, len);
960 strlcpy(global_temp_buffer, s, len + 1);
962 if (end != NULL)
964 end++;
965 strlcpy(global_filename, end, MAX_PATH);
969 return true;
972 /* ----------------------------------------------------------------------- */
973 /* This function is used by multiple functions and is used to generate a */
974 /* bookmark named based off of the input. */
975 /* Changing this function could result in how the bookmarks are stored. */
976 /* it would be here that the centralized/decentralized bookmark code */
977 /* could be placed. */
978 /* ----------------------------------------------------------------------- */
979 static bool generate_bookmark_file_name(const char *in)
981 int len = strlen(in);
983 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
984 /* otherwise, name it based on the in variable */
985 if (!strcmp("/", in))
986 strcpy(global_bookmark_file_name, "/root_dir.bmark");
987 else
989 #ifdef HAVE_MULTIVOLUME
990 /* The "root" of an extra volume need special handling too. */
991 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
992 !strcmp("/", global_bookmark_file_name));
993 #endif
995 strcpy(global_bookmark_file_name, in);
996 if(global_bookmark_file_name[len-1] == '/')
997 len--;
998 #ifdef HAVE_MULTIVOLUME
999 if (volume_root)
1000 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1001 else
1002 #endif
1003 strcpy(&global_bookmark_file_name[len], ".bmark");
1006 return true;
1009 /* ----------------------------------------------------------------------- */
1010 /* Returns true if a bookmark file exists for the current playlist */
1011 /* ----------------------------------------------------------------------- */
1012 bool bookmark_exist(void)
1014 bool exist=false;
1016 if(system_check())
1018 char* name = playlist_get_name(NULL, global_temp_buffer,
1019 sizeof(global_temp_buffer));
1020 if (generate_bookmark_file_name(name))
1022 exist = file_exists(global_bookmark_file_name);
1026 return exist;
1029 /* ----------------------------------------------------------------------- */
1030 /* Checks the current state of the system and returns true if it is in a */
1031 /* bookmarkable state. */
1032 /* ----------------------------------------------------------------------- */
1033 static bool system_check(void)
1035 int resume_index = 0;
1037 if (!(audio_status() && audio_current_track()) ||
1038 /* no track playing */
1039 (playlist_get_resume_info(&resume_index) == -1) ||
1040 /* invalid queue info */
1041 (playlist_modified(NULL)))
1042 /* can't bookmark while in the queue */
1044 return false;
1047 return true;