don't save mountpoint using native separators in the configuration. Makes things...
[Rockbox.git] / apps / bookmark.c
blob2ce03a2d3dc04c044e6510b5d3c4317b7015df47
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C)2003 by Benjamin Metzler
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdbool.h>
24 #include "config.h"
25 #include "action.h"
26 #include "audio.h"
27 #include "playlist.h"
28 #include "settings.h"
29 #include "tree.h"
30 #include "bookmark.h"
31 #include "system.h"
32 #include "icons.h"
33 #include "menu.h"
34 #include "lang.h"
35 #include "talk.h"
36 #include "misc.h"
37 #include "splash.h"
38 #include "yesno.h"
39 #include "list.h"
40 #include "plugin.h"
41 #include "backdrop.h"
42 #include "file.h"
43 #include "statusbar.h"
44 #include "textarea.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 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
65 bool most_recent);
66 static bool check_bookmark(const char* bookmark);
67 static char* create_bookmark(void);
68 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
69 static void say_bookmark(const char* bookmark,
70 int bookmark_id);
71 static bool play_bookmark(const char* bookmark);
72 static bool generate_bookmark_file_name(const char *in);
73 static const char* skip_token(const char* s);
74 static const char* int_token(const char* s, int* dest);
75 static const char* long_token(const char* s, long* dest);
76 static const char* bool_token(const char* s, bool* dest);
77 static bool parse_bookmark(const char *bookmark,
78 int *resume_index,
79 int *resume_offset,
80 int *resume_seed,
81 int *resume_first_index,
82 char* resume_file,
83 unsigned int resume_file_size,
84 long* ms,
85 int * repeat_mode,
86 bool *shuffle,
87 char* file_name);
88 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
89 static char* get_bookmark_info(int list_index,
90 void* data,
91 char *buffer,
92 size_t buffer_len);
93 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
94 static bool system_check(void);
95 static bool write_bookmark(bool create_bookmark_file);
96 static int get_bookmark_count(const char* bookmark_file_name);
98 static char global_temp_buffer[MAX_PATH+1];
99 /* File name created by generate_bookmark_file_name */
100 static char global_bookmark_file_name[MAX_PATH];
101 static char global_read_buffer[MAX_BOOKMARK_SIZE];
102 /* Bookmark created by create_bookmark*/
103 static char global_bookmark[MAX_BOOKMARK_SIZE];
104 /* Filename from parsed bookmark (can be made local where needed) */
105 static char global_filename[MAX_PATH];
107 /* ----------------------------------------------------------------------- */
108 /* This is the interface function from the main menu. */
109 /* ----------------------------------------------------------------------- */
110 bool bookmark_create_menu(void)
112 write_bookmark(true);
113 return false;
116 /* ----------------------------------------------------------------------- */
117 /* This function acts as the load interface from the main menu */
118 /* This function determines the bookmark file name and then loads that file*/
119 /* for the user. The user can then select a bookmark to load. */
120 /* If no file/directory is currently playing, the menu item does not work. */
121 /* ----------------------------------------------------------------------- */
122 bool bookmark_load_menu(void)
124 if (system_check())
126 char* name = playlist_get_name(NULL, global_temp_buffer,
127 sizeof(global_temp_buffer));
128 if (generate_bookmark_file_name(name))
130 char* bookmark = select_bookmark(global_bookmark_file_name, false);
132 if (bookmark != NULL)
134 return play_bookmark(bookmark);
139 return false;
142 /* ----------------------------------------------------------------------- */
143 /* Gives the user a list of the Most Recent Bookmarks. This is an */
144 /* interface function */
145 /* ----------------------------------------------------------------------- */
146 bool bookmark_mrb_load()
148 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE, false);
150 if (bookmark != NULL)
152 return play_bookmark(bookmark);
155 return false;
158 /* ----------------------------------------------------------------------- */
159 /* This function handles an autobookmark creation. This is an interface */
160 /* function. */
161 /* ----------------------------------------------------------------------- */
162 bool bookmark_autobookmark(void)
164 if (!system_check())
165 return false;
167 audio_pause(); /* first pause playback */
168 switch (global_settings.autocreatebookmark)
170 case BOOKMARK_YES:
171 return write_bookmark(true);
173 case BOOKMARK_NO:
174 return false;
176 case BOOKMARK_RECENT_ONLY_YES:
177 return write_bookmark(false);
179 #ifdef HAVE_LCD_BITMAP
180 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
181 const struct text_message message={lines, 1};
182 #else
183 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
184 str(LANG_CONFIRM_WITH_BUTTON)};
185 const struct text_message message={lines, 2};
186 #endif
187 #if LCD_DEPTH > 1
188 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
189 #endif
190 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
191 show_remote_main_backdrop();
192 #endif
193 gui_syncstatusbar_draw(&statusbars, false);
194 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
196 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
197 return write_bookmark(false);
198 else
199 return write_bookmark(true);
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)
213 bool success=false;
214 char* bookmark;
216 if (!system_check())
217 return false; /* something didn't happen correctly, do nothing */
219 bookmark = create_bookmark();
220 if (!bookmark)
221 return false; /* something didn't happen correctly, do nothing */
223 if (global_settings.usemrb)
224 success = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
227 /* writing the bookmark */
228 if (create_bookmark_file)
230 char* name = playlist_get_name(NULL, global_temp_buffer,
231 sizeof(global_temp_buffer));
232 if (generate_bookmark_file_name(name))
234 success = add_bookmark(global_bookmark_file_name, bookmark, false);
238 gui_syncsplash(HZ, success ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
239 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
241 return true;
244 /* ----------------------------------------------------------------------- */
245 /* This function adds a bookmark to a file. */
246 /* ------------------------------------------------------------------------*/
247 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
248 bool most_recent)
250 int temp_bookmark_file = 0;
251 int bookmark_file = 0;
252 int bookmark_count = 0;
253 char* playlist = NULL;
254 char* cp;
255 char* tmp;
256 int len = 0;
257 bool unique = false;
259 /* Opening up a temp bookmark file */
260 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
261 "%s.tmp", bookmark_file_name);
262 temp_bookmark_file = open(global_temp_buffer,
263 O_WRONLY | O_CREAT | O_TRUNC);
264 if (temp_bookmark_file < 0)
265 return false; /* can't open the temp file */
267 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
269 playlist = strchr(bookmark,'/');
270 cp = strrchr(bookmark,';');
271 len = cp - playlist;
272 unique = true;
275 /* Writing the new bookmark to the begining of the temp file */
276 write(temp_bookmark_file, bookmark, strlen(bookmark));
277 write(temp_bookmark_file, "\n", 1);
278 bookmark_count++;
280 /* Reading in the previous bookmarks and writing them to the temp file */
281 bookmark_file = open(bookmark_file_name, O_RDONLY);
282 if (bookmark_file >= 0)
284 while (read_line(bookmark_file, global_read_buffer,
285 sizeof(global_read_buffer)) > 0)
287 /* The MRB has a max of MAX_BOOKMARKS in it */
288 /* This keeps it from getting too large */
289 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
290 break;
292 cp = strchr(global_read_buffer,'/');
293 tmp = strrchr(global_read_buffer,';');
294 if (check_bookmark(global_read_buffer) &&
295 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
297 bookmark_count++;
298 write(temp_bookmark_file, global_read_buffer,
299 strlen(global_read_buffer));
300 write(temp_bookmark_file, "\n", 1);
303 close(bookmark_file);
305 close(temp_bookmark_file);
307 remove(bookmark_file_name);
308 rename(global_temp_buffer, bookmark_file_name);
310 return true;
314 /* ----------------------------------------------------------------------- */
315 /* This function takes the system resume data and formats it into a valid */
316 /* bookmark. */
317 /* ----------------------------------------------------------------------- */
318 static char* create_bookmark()
320 int resume_index = 0;
321 char *file;
323 /* grab the currently playing track */
324 struct mp3entry *id3 = audio_current_track();
325 if(!id3)
326 return NULL;
328 /* Get some basic resume information */
329 /* queue_resume and queue_resume_index are not used and can be ignored.*/
330 playlist_get_resume_info(&resume_index);
332 /* Get the currently playing file minus the path */
333 /* This is used when displaying the available bookmarks */
334 file = strrchr(id3->path,'/');
335 if(NULL == file)
336 return NULL;
338 /* create the bookmark */
339 snprintf(global_bookmark, sizeof(global_bookmark),
340 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
341 resume_index,
342 id3->offset,
343 playlist_get_seed(NULL),
345 id3->elapsed,
346 global_settings.repeat_mode,
347 global_settings.playlist_shuffle,
348 playlist_get_name(NULL, global_temp_buffer,
349 sizeof(global_temp_buffer)),
350 file+1);
352 /* checking to see if the bookmark is valid */
353 if (check_bookmark(global_bookmark))
354 return global_bookmark;
355 else
356 return NULL;
359 static bool check_bookmark(const char* bookmark)
361 return parse_bookmark(bookmark,
362 NULL,NULL,NULL, NULL,
363 NULL,0,NULL,NULL,
364 NULL, NULL);
367 /* ----------------------------------------------------------------------- */
368 /* This function will determine if an autoload is necessary. This is an */
369 /* interface function. */
370 /* ------------------------------------------------------------------------*/
371 bool bookmark_autoload(const char* file)
373 if(global_settings.autoloadbookmark == BOOKMARK_NO)
374 return false;
376 /*Checking to see if a bookmark file exists.*/
377 if(!generate_bookmark_file_name(file))
379 return false;
382 if(!file_exists(global_bookmark_file_name))
383 return false;
385 if(global_settings.autoloadbookmark == BOOKMARK_YES)
387 return bookmark_load(global_bookmark_file_name, true);
389 else
391 char* bookmark = select_bookmark(global_bookmark_file_name, true);
393 if (bookmark != NULL)
395 if (!play_bookmark(bookmark))
397 /* Selected bookmark not found. */
398 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
401 /* Act as if autoload was done even if it failed, since the
402 * user did make an active selection.
404 return true;
407 return false;
411 /* ----------------------------------------------------------------------- */
412 /* This function loads the bookmark information into the resume memory. */
413 /* This is an interface function. */
414 /* ------------------------------------------------------------------------*/
415 bool bookmark_load(const char* file, bool autoload)
417 int fd;
418 char* bookmark = NULL;
420 if(autoload)
422 fd = open(file, O_RDONLY);
423 if(fd >= 0)
425 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
426 bookmark=global_read_buffer;
427 close(fd);
430 else
432 /* This is not an auto-load, so list the bookmarks */
433 bookmark = select_bookmark(file, false);
436 if (bookmark != NULL)
438 if (!play_bookmark(bookmark))
440 /* Selected bookmark not found. */
441 if (!autoload)
443 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
446 return false;
450 return true;
454 static int get_bookmark_count(const char* bookmark_file_name)
456 int read_count = 0;
457 int file = open(bookmark_file_name, O_RDONLY);
459 if(file < 0)
460 return -1;
462 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
464 read_count++;
467 close(file);
468 return read_count;
471 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
473 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
474 int read_count = 0;
475 int file = open(bookmarks->filename, O_RDONLY);
477 if (file < 0)
479 return -1;
482 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
483 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
485 /* Entire file fits in buffer */
486 first_line = 0;
489 bookmarks->start = first_line;
490 bookmarks->count = 0;
491 bookmarks->reload = false;
493 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
495 read_count++;
497 if (read_count >= first_line)
499 dest -= strlen(global_read_buffer) + 1;
501 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
502 + (sizeof(char*) * (bookmarks->count + 1)))
504 break;
507 strcpy(dest, global_read_buffer);
508 bookmarks->items[bookmarks->count] = dest;
509 bookmarks->count++;
513 close(file);
514 return bookmarks->start + bookmarks->count;
517 static char* get_bookmark_info(int list_index,
518 void* data,
519 char *buffer,
520 size_t buffer_len)
522 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
523 int index = list_index / 2;
524 int resume_index = 0;
525 long resume_time = 0;
526 bool shuffle = false;
528 if (bookmarks->show_dont_resume)
530 if (index == 0)
532 return list_index % 2 == 0
533 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
536 index--;
539 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
540 || (index < bookmarks->start))
542 int read_index = index;
544 /* Using count as a guide on how far to move could possibly fail
545 * sometimes. Use byte count if that is a problem?
548 if (read_index != 0)
550 /* Move count * 3 / 4 items in the direction the user is moving,
551 * but don't go too close to the end.
553 int offset = bookmarks->count;
554 int max = bookmarks->total_count - (bookmarks->count / 2);
556 if (read_index < bookmarks->start)
558 offset *= 3;
561 read_index = index - offset / 4;
563 if (read_index > max)
565 read_index = max;
568 if (read_index < 0)
570 read_index = 0;
574 if (buffer_bookmarks(bookmarks, read_index) <= index)
576 return "";
580 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
581 &resume_index, NULL, NULL, NULL, global_temp_buffer,
582 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
583 global_filename))
585 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
588 if (list_index % 2 == 0)
590 char *name;
591 char *format;
592 int len = strlen(global_temp_buffer);
594 if (bookmarks->show_playlist_name && len > 0)
596 name = global_temp_buffer;
597 len--;
599 if (name[len] != '/')
601 strrsplt(name, '.');
603 else if (len > 1)
605 name[len] = '\0';
608 if (len > 1)
610 name = strrsplt(name, '/');
613 format = "%s : %s";
615 else
617 name = global_filename;
618 format = "%s";
621 strrsplt(global_filename, '.');
622 snprintf(buffer, buffer_len, format, name, global_filename);
623 return buffer;
625 else
627 char time_buf[32];
629 format_time(time_buf, sizeof(time_buf), resume_time);
630 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
631 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
632 return buffer;
636 /* ----------------------------------------------------------------------- */
637 /* This displays a the bookmarks in a file and allows the user to */
638 /* select one to play. */
639 /* ------------------------------------------------------------------------*/
640 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
642 struct bookmark_list* bookmarks;
643 struct gui_synclist list;
644 int last_item = -2;
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 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
660 Icon_Bookmark);
661 gui_syncstatusbar_draw(&statusbars, true);
663 while (!exit)
665 gui_syncstatusbar_draw(&statusbars, false);
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 gui_syncsplash(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 refresh = false;
700 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
701 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
702 item = gui_synclist_get_sel_pos(&list) / 2;
704 if (bookmarks->show_dont_resume)
706 item--;
709 if (item != last_item && global_settings.talk_menu)
711 last_item = item;
713 if (item == -1)
715 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
717 else
719 say_bookmark(bookmarks->items[item - bookmarks->start], item);
723 if (action == ACTION_STD_CONTEXT)
725 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
726 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
727 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
728 static const int menu_actions[] =
730 ACTION_STD_OK, ACTION_BMS_DELETE
732 int selection = do_menu(&menu_items, NULL, NULL, false);
734 refresh = true;
736 if (selection >= 0 && selection <=
737 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
739 action = menu_actions[selection];
743 switch (action)
745 case ACTION_STD_OK:
746 if (item >= 0)
748 return bookmarks->items[item - bookmarks->start];
751 /* Else fall through */
753 case ACTION_TREE_WPS:
754 case ACTION_STD_CANCEL:
755 exit = true;
756 break;
758 case ACTION_BMS_DELETE:
759 if (item >= 0)
761 delete_bookmark(bookmark_file_name, item);
762 bookmarks->reload = true;
763 refresh = true;
764 last_item = -2;
766 break;
768 default:
769 if (default_event_handler(action) == SYS_USB_CONNECTED)
771 exit = true;
774 break;
778 return NULL;
781 /* ----------------------------------------------------------------------- */
782 /* This function takes a location in a bookmark file and deletes that */
783 /* bookmark. */
784 /* ------------------------------------------------------------------------*/
785 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
787 int temp_bookmark_file = 0;
788 int bookmark_file = 0;
789 int bookmark_count = 0;
791 /* Opening up a temp bookmark file */
792 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
793 "%s.tmp", bookmark_file_name);
794 temp_bookmark_file = open(global_temp_buffer,
795 O_WRONLY | O_CREAT | O_TRUNC);
797 if (temp_bookmark_file < 0)
798 return false; /* can't open the temp file */
800 /* Reading in the previous bookmarks and writing them to the temp file */
801 bookmark_file = open(bookmark_file_name, O_RDONLY);
802 if (bookmark_file >= 0)
804 while (read_line(bookmark_file, global_read_buffer,
805 sizeof(global_read_buffer)) > 0)
807 if (bookmark_id != bookmark_count)
809 write(temp_bookmark_file, global_read_buffer,
810 strlen(global_read_buffer));
811 write(temp_bookmark_file, "\n", 1);
813 bookmark_count++;
815 close(bookmark_file);
817 close(temp_bookmark_file);
819 remove(bookmark_file_name);
820 rename(global_temp_buffer, bookmark_file_name);
822 return true;
825 /* ----------------------------------------------------------------------- */
826 /* This function parses a bookmark, says the voice UI part of it. */
827 /* ------------------------------------------------------------------------*/
828 static void say_bookmark(const char* bookmark,
829 int bookmark_id)
831 int resume_index;
832 long ms;
833 bool enqueue = false; /* only the first voice is not queued */
835 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
836 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
838 talk_id(LANG_BOOKMARK_INVALID, true);
839 return;
842 /* disabled, because transition between talkbox and voice UI clip is not nice */
843 #if 0
844 if (global_settings.talk_dir >= 3)
845 { /* "talkbox" enabled */
846 char* last = strrchr(global_temp_buffer, '/');
847 if (last)
848 { /* compose filename for talkbox */
849 strncpy(last + 1, dir_thumbnail_name,
850 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
851 talk_file(global_temp_buffer, enqueue);
852 enqueue = true;
855 #endif
856 talk_id(VOICE_EXT_BMARK, enqueue);
857 talk_number(bookmark_id + 1, true);
858 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
859 talk_number(resume_index + 1, true);
860 talk_id(LANG_TIME, true);
861 if (ms / 60000)
862 talk_value(ms / 60000, UNIT_MIN, true);
863 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
866 /* ----------------------------------------------------------------------- */
867 /* This function parses a bookmark and then plays it. */
868 /* ------------------------------------------------------------------------*/
869 static bool play_bookmark(const char* bookmark)
871 int index;
872 int offset;
873 int seed;
875 if (parse_bookmark(bookmark,
876 &index,
877 &offset,
878 &seed,
879 NULL,
880 global_temp_buffer,
881 sizeof(global_temp_buffer),
882 NULL,
883 &global_settings.repeat_mode,
884 &global_settings.playlist_shuffle,
885 global_filename))
887 return bookmark_play(global_temp_buffer, index, offset, seed,
888 global_filename);
891 return false;
894 static const char* skip_token(const char* s)
896 while (*s && *s != ';')
898 s++;
901 if (*s)
903 s++;
906 return s;
909 static const char* int_token(const char* s, int* dest)
911 if (dest != NULL)
913 *dest = atoi(s);
916 return skip_token(s);
919 static const char* long_token(const char* s, long* dest)
921 if (dest != NULL)
923 *dest = atoi(s); /* Should be atol, but we don't have it. */
926 return skip_token(s);
929 static const char* bool_token(const char* s, bool* dest)
931 if (dest != NULL)
933 *dest = atoi(s) != 0;
936 return skip_token(s);
939 /* ----------------------------------------------------------------------- */
940 /* This function takes a bookmark and parses it. This function also */
941 /* validates the bookmark. Passing in NULL for an output variable */
942 /* indicates that value is not requested. */
943 /* ----------------------------------------------------------------------- */
944 static bool parse_bookmark(const char *bookmark,
945 int *resume_index,
946 int *resume_offset,
947 int *resume_seed,
948 int *resume_first_index,
949 char* resume_file,
950 unsigned int resume_file_size,
951 long* ms,
952 int * repeat_mode, bool *shuffle,
953 char* file_name)
955 const char* s = bookmark;
956 const char* end;
958 s = int_token(s, resume_index);
959 s = int_token(s, resume_offset);
960 s = int_token(s, resume_seed);
961 s = int_token(s, resume_first_index);
962 s = long_token(s, ms);
963 s = int_token(s, repeat_mode);
964 s = bool_token(s, shuffle);
966 if (*s == 0)
968 return false;
971 end = strchr(s, ';');
973 if (resume_file != NULL)
975 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
977 len = MIN(resume_file_size - 1, len);
978 strncpy(resume_file, s, len);
979 resume_file[len] = 0;
982 if (end != NULL && file_name != NULL)
984 end++;
985 strncpy(file_name, end, MAX_PATH - 1);
986 file_name[MAX_PATH - 1] = 0;
989 return true;
992 /* ----------------------------------------------------------------------- */
993 /* This function is used by multiple functions and is used to generate a */
994 /* bookmark named based off of the input. */
995 /* Changing this function could result in how the bookmarks are stored. */
996 /* it would be here that the centralized/decentralized bookmark code */
997 /* could be placed. */
998 /* ----------------------------------------------------------------------- */
999 static bool generate_bookmark_file_name(const char *in)
1001 int len = strlen(in);
1003 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1004 /* otherwise, name it based on the in variable */
1005 if (!strcmp("/", in))
1006 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1007 else
1009 strcpy(global_bookmark_file_name, in);
1010 if(global_bookmark_file_name[len-1] == '/')
1011 len--;
1012 strcpy(&global_bookmark_file_name[len], ".bmark");
1015 return true;
1018 /* ----------------------------------------------------------------------- */
1019 /* Returns true if a bookmark file exists for the current playlist */
1020 /* ----------------------------------------------------------------------- */
1021 bool bookmark_exist(void)
1023 bool exist=false;
1025 if(system_check())
1027 char* name = playlist_get_name(NULL, global_temp_buffer,
1028 sizeof(global_temp_buffer));
1029 if (generate_bookmark_file_name(name))
1031 exist = file_exists(global_bookmark_file_name);
1035 return exist;
1038 /* ----------------------------------------------------------------------- */
1039 /* Checks the current state of the system and returns if it is in a */
1040 /* bookmarkable state. */
1041 /* ----------------------------------------------------------------------- */
1042 /* Inputs: */
1043 /* ----------------------------------------------------------------------- */
1044 /* Outputs: */
1045 /* return bool: Indicates if the system was in a bookmarkable state */
1046 /* ----------------------------------------------------------------------- */
1047 static bool system_check(void)
1049 int resume_index = 0;
1051 if (!(audio_status() && audio_current_track()))
1053 /* no track playing */
1054 return false;
1057 /* Checking to see if playing a queued track */
1058 if (playlist_get_resume_info(&resume_index) == -1)
1060 /* something bad happened while getting the queue information */
1061 return false;
1063 else if (playlist_modified(NULL))
1065 /* can't bookmark while in the queue */
1066 return false;
1069 return true;