total removal of gui_textarea. The only thing using the text_message struct is the...
[maemo-rb.git] / apps / bookmark.c
blobc9a285975d3feae07a56d7b4648fafbfe649bbba
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"
45 #define MAX_BOOKMARKS 10
46 #define MAX_BOOKMARK_SIZE 350
47 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
49 /* Used to buffer bookmarks while displaying the bookmark list. */
50 struct bookmark_list
52 const char* filename;
53 size_t buffer_size;
54 int start;
55 int count;
56 int total_count;
57 bool show_dont_resume;
58 bool reload;
59 bool show_playlist_name;
60 char* items[];
63 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
64 bool most_recent);
65 static bool check_bookmark(const char* bookmark);
66 static char* create_bookmark(void);
67 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
68 static void say_bookmark(const char* bookmark,
69 int bookmark_id);
70 static bool play_bookmark(const char* bookmark);
71 static bool generate_bookmark_file_name(const char *in);
72 static const char* skip_token(const char* s);
73 static const char* int_token(const char* s, int* dest);
74 static const char* long_token(const char* s, long* dest);
75 static const char* bool_token(const char* s, bool* dest);
76 static bool parse_bookmark(const char *bookmark,
77 int *resume_index,
78 int *resume_offset,
79 int *resume_seed,
80 int *resume_first_index,
81 char* resume_file,
82 unsigned int resume_file_size,
83 long* ms,
84 int * repeat_mode,
85 bool *shuffle,
86 char* file_name);
87 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
88 static char* get_bookmark_info(int list_index,
89 void* data,
90 char *buffer,
91 size_t buffer_len);
92 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume);
93 static bool system_check(void);
94 static bool write_bookmark(bool create_bookmark_file);
95 static int get_bookmark_count(const char* bookmark_file_name);
97 static char global_temp_buffer[MAX_PATH+1];
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);
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(void)
163 if (!system_check())
164 return false;
166 audio_pause(); /* first pause playback */
167 switch (global_settings.autocreatebookmark)
169 case BOOKMARK_YES:
170 return write_bookmark(true);
172 case BOOKMARK_NO:
173 return false;
175 case BOOKMARK_RECENT_ONLY_YES:
176 return write_bookmark(false);
178 #ifdef HAVE_LCD_BITMAP
179 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
180 const struct text_message message={lines, 1};
181 #else
182 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
183 str(LANG_CONFIRM_WITH_BUTTON)};
184 const struct text_message message={lines, 2};
185 #endif
186 #if LCD_DEPTH > 1
187 show_main_backdrop(); /* switch to main backdrop as we may come from wps */
188 #endif
189 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
190 show_remote_main_backdrop();
191 #endif
192 gui_syncstatusbar_draw(&statusbars, false);
193 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
195 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
196 return write_bookmark(false);
197 else
198 return write_bookmark(true);
200 return false;
203 /* ----------------------------------------------------------------------- */
204 /* This function takes the current current resume information and writes */
205 /* that to the beginning of the bookmark file. */
206 /* This file will contain N number of bookmarks in the following format: */
207 /* resume_index*resume_offset*resume_seed*resume_first_index* */
208 /* resume_file*milliseconds*MP3 Title* */
209 /* ------------------------------------------------------------------------*/
210 static bool write_bookmark(bool create_bookmark_file)
212 bool success=false;
213 char* bookmark;
215 if (!system_check())
216 return false; /* something didn't happen correctly, do nothing */
218 bookmark = create_bookmark();
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 gui_syncsplash(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 /* grab the currently playing track */
323 struct mp3entry *id3 = audio_current_track();
324 if(!id3)
325 return NULL;
327 /* Get some basic resume information */
328 /* queue_resume and queue_resume_index are not used and can be ignored.*/
329 playlist_get_resume_info(&resume_index);
331 /* Get the currently playing file minus the path */
332 /* This is used when displaying the available bookmarks */
333 file = strrchr(id3->path,'/');
334 if(NULL == file)
335 return NULL;
337 /* create the bookmark */
338 snprintf(global_bookmark, sizeof(global_bookmark),
339 "%d;%ld;%d;%d;%ld;%d;%d;%s;%s",
340 resume_index,
341 id3->offset,
342 playlist_get_seed(NULL),
344 id3->elapsed,
345 global_settings.repeat_mode,
346 global_settings.playlist_shuffle,
347 playlist_get_name(NULL, global_temp_buffer,
348 sizeof(global_temp_buffer)),
349 file+1);
351 /* checking to see if the bookmark is valid */
352 if (check_bookmark(global_bookmark))
353 return global_bookmark;
354 else
355 return NULL;
358 static bool check_bookmark(const char* bookmark)
360 return parse_bookmark(bookmark,
361 NULL,NULL,NULL, NULL,
362 NULL,0,NULL,NULL,
363 NULL, NULL);
366 /* ----------------------------------------------------------------------- */
367 /* This function will determine if an autoload is necessary. This is an */
368 /* interface function. */
369 /* ------------------------------------------------------------------------*/
370 bool bookmark_autoload(const char* file)
372 if(global_settings.autoloadbookmark == BOOKMARK_NO)
373 return false;
375 /*Checking to see if a bookmark file exists.*/
376 if(!generate_bookmark_file_name(file))
378 return false;
381 if(!file_exists(global_bookmark_file_name))
382 return false;
384 if(global_settings.autoloadbookmark == BOOKMARK_YES)
386 return bookmark_load(global_bookmark_file_name, true);
388 else
390 char* bookmark = select_bookmark(global_bookmark_file_name, true);
392 if (bookmark != NULL)
394 if (!play_bookmark(bookmark))
396 /* Selected bookmark not found. */
397 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
400 /* Act as if autoload was done even if it failed, since the
401 * user did make an active selection.
403 return true;
406 return false;
410 /* ----------------------------------------------------------------------- */
411 /* This function loads the bookmark information into the resume memory. */
412 /* This is an interface function. */
413 /* ------------------------------------------------------------------------*/
414 bool bookmark_load(const char* file, bool autoload)
416 int fd;
417 char* bookmark = NULL;
419 if(autoload)
421 fd = open(file, O_RDONLY);
422 if(fd >= 0)
424 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
425 bookmark=global_read_buffer;
426 close(fd);
429 else
431 /* This is not an auto-load, so list the bookmarks */
432 bookmark = select_bookmark(file, false);
435 if (bookmark != NULL)
437 if (!play_bookmark(bookmark))
439 /* Selected bookmark not found. */
440 if (!autoload)
442 gui_syncsplash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
445 return false;
449 return true;
453 static int get_bookmark_count(const char* bookmark_file_name)
455 int read_count = 0;
456 int file = open(bookmark_file_name, O_RDONLY);
458 if(file < 0)
459 return -1;
461 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
463 read_count++;
466 close(file);
467 return read_count;
470 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
472 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
473 int read_count = 0;
474 int file = open(bookmarks->filename, O_RDONLY);
476 if (file < 0)
478 return -1;
481 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
482 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
484 /* Entire file fits in buffer */
485 first_line = 0;
488 bookmarks->start = first_line;
489 bookmarks->count = 0;
490 bookmarks->reload = false;
492 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
494 read_count++;
496 if (read_count >= first_line)
498 dest -= strlen(global_read_buffer) + 1;
500 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
501 + (sizeof(char*) * (bookmarks->count + 1)))
503 break;
506 strcpy(dest, global_read_buffer);
507 bookmarks->items[bookmarks->count] = dest;
508 bookmarks->count++;
512 close(file);
513 return bookmarks->start + bookmarks->count;
516 static char* get_bookmark_info(int list_index,
517 void* data,
518 char *buffer,
519 size_t buffer_len)
521 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
522 int index = list_index / 2;
523 int resume_index = 0;
524 long resume_time = 0;
525 bool shuffle = false;
527 if (bookmarks->show_dont_resume)
529 if (index == 0)
531 return list_index % 2 == 0
532 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
535 index--;
538 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
539 || (index < bookmarks->start))
541 int read_index = index;
543 /* Using count as a guide on how far to move could possibly fail
544 * sometimes. Use byte count if that is a problem?
547 if (read_index != 0)
549 /* Move count * 3 / 4 items in the direction the user is moving,
550 * but don't go too close to the end.
552 int offset = bookmarks->count;
553 int max = bookmarks->total_count - (bookmarks->count / 2);
555 if (read_index < bookmarks->start)
557 offset *= 3;
560 read_index = index - offset / 4;
562 if (read_index > max)
564 read_index = max;
567 if (read_index < 0)
569 read_index = 0;
573 if (buffer_bookmarks(bookmarks, read_index) <= index)
575 return "";
579 if (!parse_bookmark(bookmarks->items[index - bookmarks->start],
580 &resume_index, NULL, NULL, NULL, global_temp_buffer,
581 sizeof(global_temp_buffer), &resume_time, NULL, &shuffle,
582 global_filename))
584 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
587 if (list_index % 2 == 0)
589 char *name;
590 char *format;
591 int len = strlen(global_temp_buffer);
593 if (bookmarks->show_playlist_name && len > 0)
595 name = global_temp_buffer;
596 len--;
598 if (name[len] != '/')
600 strrsplt(name, '.');
602 else if (len > 1)
604 name[len] = '\0';
607 if (len > 1)
609 name = strrsplt(name, '/');
612 format = "%s : %s";
614 else
616 name = global_filename;
617 format = "%s";
620 strrsplt(global_filename, '.');
621 snprintf(buffer, buffer_len, format, name, global_filename);
622 return buffer;
624 else
626 char time_buf[32];
628 format_time(time_buf, sizeof(time_buf), resume_time);
629 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, resume_index + 1,
630 shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
631 return buffer;
635 /* ----------------------------------------------------------------------- */
636 /* This displays a the bookmarks in a file and allows the user to */
637 /* select one to play. */
638 /* ------------------------------------------------------------------------*/
639 static char* select_bookmark(const char* bookmark_file_name, bool show_dont_resume)
641 struct bookmark_list* bookmarks;
642 struct gui_synclist list;
643 int last_item = -2;
644 int item = 0;
645 int action;
646 size_t size;
647 bool exit = false;
648 bool refresh = true;
650 bookmarks = plugin_get_buffer(&size);
651 bookmarks->buffer_size = size;
652 bookmarks->show_dont_resume = show_dont_resume;
653 bookmarks->filename = bookmark_file_name;
654 bookmarks->start = 0;
655 bookmarks->show_playlist_name
656 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
657 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
658 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
659 Icon_Bookmark);
660 gui_syncstatusbar_draw(&statusbars, true);
662 while (!exit)
664 gui_syncstatusbar_draw(&statusbars, false);
666 if (refresh)
668 int count = get_bookmark_count(bookmark_file_name);
669 bookmarks->total_count = count;
671 if (bookmarks->total_count < 1)
673 /* No more bookmarks, delete file and exit */
674 gui_syncsplash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
675 remove(bookmark_file_name);
676 return NULL;
679 if (bookmarks->show_dont_resume)
681 count++;
682 item++;
685 gui_synclist_set_nb_items(&list, count * 2);
687 if (item >= count)
689 /* Selected item has been deleted */
690 item = count - 1;
691 gui_synclist_select_item(&list, item * 2);
694 buffer_bookmarks(bookmarks, bookmarks->start);
695 gui_synclist_draw(&list);
696 refresh = false;
699 action = get_action(CONTEXT_BOOKMARKSCREEN, HZ / 2);
700 gui_synclist_do_button(&list, &action, LIST_WRAP_UNLESS_HELD);
701 item = gui_synclist_get_sel_pos(&list) / 2;
703 if (bookmarks->show_dont_resume)
705 item--;
708 if (item != last_item && global_settings.talk_menu)
710 last_item = item;
712 if (item == -1)
714 talk_id(LANG_BOOKMARK_DONT_RESUME, true);
716 else
718 say_bookmark(bookmarks->items[item - bookmarks->start], item);
722 if (action == ACTION_STD_CONTEXT)
724 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
725 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
726 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
727 static const int menu_actions[] =
729 ACTION_STD_OK, ACTION_BMS_DELETE
731 int selection = do_menu(&menu_items, NULL, NULL, false);
733 refresh = true;
735 if (selection >= 0 && selection <=
736 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
738 action = menu_actions[selection];
742 switch (action)
744 case ACTION_STD_OK:
745 if (item >= 0)
747 return bookmarks->items[item - bookmarks->start];
750 /* Else fall through */
752 case ACTION_TREE_WPS:
753 case ACTION_STD_CANCEL:
754 exit = true;
755 break;
757 case ACTION_BMS_DELETE:
758 if (item >= 0)
760 delete_bookmark(bookmark_file_name, item);
761 bookmarks->reload = true;
762 refresh = true;
763 last_item = -2;
765 break;
767 default:
768 if (default_event_handler(action) == SYS_USB_CONNECTED)
770 exit = true;
773 break;
777 return NULL;
780 /* ----------------------------------------------------------------------- */
781 /* This function takes a location in a bookmark file and deletes that */
782 /* bookmark. */
783 /* ------------------------------------------------------------------------*/
784 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
786 int temp_bookmark_file = 0;
787 int bookmark_file = 0;
788 int bookmark_count = 0;
790 /* Opening up a temp bookmark file */
791 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
792 "%s.tmp", bookmark_file_name);
793 temp_bookmark_file = open(global_temp_buffer,
794 O_WRONLY | O_CREAT | O_TRUNC);
796 if (temp_bookmark_file < 0)
797 return false; /* can't open the temp file */
799 /* Reading in the previous bookmarks and writing them to the temp file */
800 bookmark_file = open(bookmark_file_name, O_RDONLY);
801 if (bookmark_file >= 0)
803 while (read_line(bookmark_file, global_read_buffer,
804 sizeof(global_read_buffer)) > 0)
806 if (bookmark_id != bookmark_count)
808 write(temp_bookmark_file, global_read_buffer,
809 strlen(global_read_buffer));
810 write(temp_bookmark_file, "\n", 1);
812 bookmark_count++;
814 close(bookmark_file);
816 close(temp_bookmark_file);
818 remove(bookmark_file_name);
819 rename(global_temp_buffer, bookmark_file_name);
821 return true;
824 /* ----------------------------------------------------------------------- */
825 /* This function parses a bookmark, says the voice UI part of it. */
826 /* ------------------------------------------------------------------------*/
827 static void say_bookmark(const char* bookmark,
828 int bookmark_id)
830 int resume_index;
831 long ms;
832 bool enqueue = false; /* only the first voice is not queued */
834 if (!parse_bookmark(bookmark, &resume_index, NULL, NULL, NULL,
835 global_temp_buffer, sizeof(global_temp_buffer), &ms, NULL, NULL, NULL))
837 talk_id(LANG_BOOKMARK_INVALID, true);
838 return;
841 /* disabled, because transition between talkbox and voice UI clip is not nice */
842 #if 0
843 if (global_settings.talk_dir >= 3)
844 { /* "talkbox" enabled */
845 char* last = strrchr(global_temp_buffer, '/');
846 if (last)
847 { /* compose filename for talkbox */
848 strncpy(last + 1, dir_thumbnail_name,
849 sizeof(global_temp_buffer) - (last - global_temp_buffer) - 1);
850 talk_file(global_temp_buffer, enqueue);
851 enqueue = true;
854 #endif
855 talk_id(VOICE_EXT_BMARK, enqueue);
856 talk_number(bookmark_id + 1, true);
857 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
858 talk_number(resume_index + 1, true);
859 talk_id(LANG_TIME, true);
860 if (ms / 60000)
861 talk_value(ms / 60000, UNIT_MIN, true);
862 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
865 /* ----------------------------------------------------------------------- */
866 /* This function parses a bookmark and then plays it. */
867 /* ------------------------------------------------------------------------*/
868 static bool play_bookmark(const char* bookmark)
870 int index;
871 int offset;
872 int seed;
874 if (parse_bookmark(bookmark,
875 &index,
876 &offset,
877 &seed,
878 NULL,
879 global_temp_buffer,
880 sizeof(global_temp_buffer),
881 NULL,
882 &global_settings.repeat_mode,
883 &global_settings.playlist_shuffle,
884 global_filename))
886 return bookmark_play(global_temp_buffer, index, offset, seed,
887 global_filename);
890 return false;
893 static const char* skip_token(const char* s)
895 while (*s && *s != ';')
897 s++;
900 if (*s)
902 s++;
905 return s;
908 static const char* int_token(const char* s, int* dest)
910 if (dest != NULL)
912 *dest = atoi(s);
915 return skip_token(s);
918 static const char* long_token(const char* s, long* dest)
920 if (dest != NULL)
922 *dest = atoi(s); /* Should be atol, but we don't have it. */
925 return skip_token(s);
928 static const char* bool_token(const char* s, bool* dest)
930 if (dest != NULL)
932 *dest = atoi(s) != 0;
935 return skip_token(s);
938 /* ----------------------------------------------------------------------- */
939 /* This function takes a bookmark and parses it. This function also */
940 /* validates the bookmark. Passing in NULL for an output variable */
941 /* indicates that value is not requested. */
942 /* ----------------------------------------------------------------------- */
943 static bool parse_bookmark(const char *bookmark,
944 int *resume_index,
945 int *resume_offset,
946 int *resume_seed,
947 int *resume_first_index,
948 char* resume_file,
949 unsigned int resume_file_size,
950 long* ms,
951 int * repeat_mode, bool *shuffle,
952 char* file_name)
954 const char* s = bookmark;
955 const char* end;
957 s = int_token(s, resume_index);
958 s = int_token(s, resume_offset);
959 s = int_token(s, resume_seed);
960 s = int_token(s, resume_first_index);
961 s = long_token(s, ms);
962 s = int_token(s, repeat_mode);
963 s = bool_token(s, shuffle);
965 if (*s == 0)
967 return false;
970 end = strchr(s, ';');
972 if (resume_file != NULL)
974 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
976 len = MIN(resume_file_size - 1, len);
977 strncpy(resume_file, s, len);
978 resume_file[len] = 0;
981 if (end != NULL && file_name != NULL)
983 end++;
984 strncpy(file_name, end, MAX_PATH - 1);
985 file_name[MAX_PATH - 1] = 0;
988 return true;
991 /* ----------------------------------------------------------------------- */
992 /* This function is used by multiple functions and is used to generate a */
993 /* bookmark named based off of the input. */
994 /* Changing this function could result in how the bookmarks are stored. */
995 /* it would be here that the centralized/decentralized bookmark code */
996 /* could be placed. */
997 /* ----------------------------------------------------------------------- */
998 static bool generate_bookmark_file_name(const char *in)
1000 int len = strlen(in);
1002 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1003 /* otherwise, name it based on the in variable */
1004 if (!strcmp("/", in))
1005 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1006 else
1008 strcpy(global_bookmark_file_name, in);
1009 if(global_bookmark_file_name[len-1] == '/')
1010 len--;
1011 strcpy(&global_bookmark_file_name[len], ".bmark");
1014 return true;
1017 /* ----------------------------------------------------------------------- */
1018 /* Returns true if a bookmark file exists for the current playlist */
1019 /* ----------------------------------------------------------------------- */
1020 bool bookmark_exist(void)
1022 bool exist=false;
1024 if(system_check())
1026 char* name = playlist_get_name(NULL, global_temp_buffer,
1027 sizeof(global_temp_buffer));
1028 if (generate_bookmark_file_name(name))
1030 exist = file_exists(global_bookmark_file_name);
1034 return exist;
1037 /* ----------------------------------------------------------------------- */
1038 /* Checks the current state of the system and returns if it is in a */
1039 /* bookmarkable state. */
1040 /* ----------------------------------------------------------------------- */
1041 /* Inputs: */
1042 /* ----------------------------------------------------------------------- */
1043 /* Outputs: */
1044 /* return bool: Indicates if the system was in a bookmarkable state */
1045 /* ----------------------------------------------------------------------- */
1046 static bool system_check(void)
1048 int resume_index = 0;
1050 if (!(audio_status() && audio_current_track()))
1052 /* no track playing */
1053 return false;
1056 /* Checking to see if playing a queued track */
1057 if (playlist_get_resume_info(&resume_index) == -1)
1059 /* something bad happened while getting the queue information */
1060 return false;
1062 else if (playlist_modified(NULL))
1064 /* can't bookmark while in the queue */
1065 return false;
1068 return true;