Merge tag 'v3.13-final' into maemo-port
[maemo-rb.git] / apps / bookmark.c
blob12ec1edd0f642b37922273253752c5cfe7b39e61
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 /* flags for optional bookmark tokens */
65 #define BM_PITCH 0x01
66 #define BM_SPEED 0x02
68 /* bookmark values */
69 static struct {
70 int resume_index;
71 unsigned long resume_offset;
72 int resume_seed;
73 long resume_time;
74 int repeat_mode;
75 bool shuffle;
76 /* optional values */
77 int pitch;
78 int speed;
79 } bm;
81 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
82 bool most_recent);
83 static char* create_bookmark(void);
84 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
85 static void say_bookmark(const char* bookmark,
86 int bookmark_id, bool show_playlist_name);
87 static bool play_bookmark(const char* bookmark);
88 static bool generate_bookmark_file_name(const char *in);
89 static bool parse_bookmark(const char *bookmark, const bool get_filenames);
90 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
91 static const char* get_bookmark_info(int list_index,
92 void* data,
93 char *buffer,
94 size_t buffer_len);
95 static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark);
96 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
97 static int get_bookmark_count(const char* bookmark_file_name);
99 #define TEMP_BUF_SIZE (MAX_PATH + 1)
100 static char global_temp_buffer[TEMP_BUF_SIZE];
101 /* File name created by generate_bookmark_file_name */
102 static char global_bookmark_file_name[MAX_PATH];
103 static char global_read_buffer[MAX_BOOKMARK_SIZE];
104 /* Bookmark created by create_bookmark*/
105 static char global_bookmark[MAX_BOOKMARK_SIZE];
106 /* Filename from parsed bookmark (can be made local where needed) */
107 static char global_filename[MAX_PATH];
109 /* ----------------------------------------------------------------------- */
110 /* This is an interface function from the context menu. */
111 /* Returns true on successful bookmark creation. */
112 /* ----------------------------------------------------------------------- */
113 bool bookmark_create_menu(void)
115 return write_bookmark(true, create_bookmark());
118 /* ----------------------------------------------------------------------- */
119 /* This function acts as the load interface from the context menu. */
120 /* This function determines the bookmark file name and then loads that file*/
121 /* for the user. The user can then select or delete previous bookmarks. */
122 /* This function returns BOOKMARK_SUCCESS on the selection of a track to */
123 /* resume, BOOKMARK_FAIL if the menu is exited without a selection and */
124 /* BOOKMARK_USB_CONNECTED if the menu is forced to exit due to a USB */
125 /* connection. */
126 /* ----------------------------------------------------------------------- */
127 int bookmark_load_menu(void)
129 char* bookmark;
130 int ret = BOOKMARK_FAIL;
132 push_current_activity(ACTIVITY_BOOKMARKSLIST);
134 char* name = playlist_get_name(NULL, global_temp_buffer,
135 sizeof(global_temp_buffer));
136 if (generate_bookmark_file_name(name))
138 ret = select_bookmark(global_bookmark_file_name, false, &bookmark);
139 if (bookmark != NULL)
141 ret = play_bookmark(bookmark) ? BOOKMARK_SUCCESS : BOOKMARK_FAIL;
145 pop_current_activity();
146 return ret;
149 /* ----------------------------------------------------------------------- */
150 /* Gives the user a list of the Most Recent Bookmarks. This is an */
151 /* interface function */
152 /* Returns true on the successful selection of a recent bookmark. */
153 /* ----------------------------------------------------------------------- */
154 bool bookmark_mrb_load()
156 char* bookmark;
157 bool ret = false;
159 push_current_activity(ACTIVITY_BOOKMARKSLIST);
160 select_bookmark(RECENT_BOOKMARK_FILE, false, &bookmark);
161 if (bookmark != NULL)
163 ret = play_bookmark(bookmark);
166 pop_current_activity();
167 return ret;
170 /* ----------------------------------------------------------------------- */
171 /* This function handles an autobookmark creation. This is an interface */
172 /* function. */
173 /* Returns true on successful bookmark creation. */
174 /* ----------------------------------------------------------------------- */
175 bool bookmark_autobookmark(bool prompt_ok)
177 char* bookmark;
178 bool update;
180 if (!bookmark_is_bookmarkable_state())
181 return false;
183 audio_pause(); /* first pause playback */
184 update = (global_settings.autoupdatebookmark && bookmark_exists());
185 bookmark = create_bookmark();
186 #if CONFIG_CODEC != SWCODEC
187 /* Workaround for inability to speak when paused: all callers will
188 just do audio_stop() when we return, so we can do it right
189 away. This makes it possible to speak the "Create a Bookmark?"
190 prompt and the "Bookmark Created" splash. */
191 audio_stop();
192 #endif
194 if (update)
195 return write_bookmark(true, bookmark);
197 switch (global_settings.autocreatebookmark)
199 case BOOKMARK_YES:
200 return write_bookmark(true, bookmark);
202 case BOOKMARK_NO:
203 return false;
205 case BOOKMARK_RECENT_ONLY_YES:
206 return write_bookmark(false, bookmark);
208 #ifdef HAVE_LCD_BITMAP
209 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
210 const struct text_message message={lines, 1};
211 #else
212 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
213 str(LANG_CONFIRM_WITH_BUTTON)};
214 const struct text_message message={lines, 2};
215 #endif
217 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
219 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
220 return write_bookmark(false, bookmark);
221 else
222 return write_bookmark(true, bookmark);
224 return false;
227 /* ----------------------------------------------------------------------- */
228 /* This function takes the current current resume information and writes */
229 /* that to the beginning of the bookmark file. */
230 /* This file will contain N number of bookmarks in the following format: */
231 /* resume_index*resume_offset*resume_seed*resume_first_index* */
232 /* resume_file*milliseconds*MP3 Title* */
233 /* Returns true on successful bookmark write. */
234 /* Returns false if any part of the bookmarking process fails. It is */
235 /* possible that a bookmark is successfully added to the most recent */
236 /* bookmark list but fails to be added to the bookmark file or vice versa. */
237 /* ------------------------------------------------------------------------*/
238 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
240 bool ret=true;
242 if (!bookmark)
244 ret = false; /* something didn't happen correctly, do nothing */
246 else
248 if (global_settings.usemrb)
249 ret = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
252 /* writing the bookmark */
253 if (create_bookmark_file)
255 char* name = playlist_get_name(NULL, global_temp_buffer,
256 sizeof(global_temp_buffer));
257 if (generate_bookmark_file_name(name))
259 ret = ret & add_bookmark(global_bookmark_file_name, bookmark, false);
261 else
263 ret = false; /* generating bookmark file failed */
268 splash(HZ, ret ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
269 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
271 return ret;
274 /* ----------------------------------------------------------------------- */
275 /* This function adds a bookmark to a file. */
276 /* Returns true on successful bookmark add. */
277 /* ------------------------------------------------------------------------*/
278 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
279 bool most_recent)
281 int temp_bookmark_file = 0;
282 int bookmark_file = 0;
283 int bookmark_count = 0;
284 char* playlist = NULL;
285 char* cp;
286 char* tmp;
287 int len = 0;
288 bool unique = false;
290 /* Opening up a temp bookmark file */
291 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
292 "%s.tmp", bookmark_file_name);
293 temp_bookmark_file = open(global_temp_buffer,
294 O_WRONLY | O_CREAT | O_TRUNC, 0666);
295 if (temp_bookmark_file < 0)
296 return false; /* can't open the temp file */
298 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
300 playlist = strchr(bookmark,'/');
301 cp = strrchr(bookmark,';');
302 len = cp - playlist;
303 unique = true;
306 /* Writing the new bookmark to the begining of the temp file */
307 write(temp_bookmark_file, bookmark, strlen(bookmark));
308 write(temp_bookmark_file, "\n", 1);
309 bookmark_count++;
311 /* Reading in the previous bookmarks and writing them to the temp file */
312 bookmark_file = open(bookmark_file_name, O_RDONLY);
313 if (bookmark_file >= 0)
315 while (read_line(bookmark_file, global_read_buffer,
316 sizeof(global_read_buffer)) > 0)
318 /* The MRB has a max of MAX_BOOKMARKS in it */
319 /* This keeps it from getting too large */
320 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
321 break;
323 cp = strchr(global_read_buffer,'/');
324 tmp = strrchr(global_read_buffer,';');
325 if (parse_bookmark(global_read_buffer, false) &&
326 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
328 bookmark_count++;
329 write(temp_bookmark_file, global_read_buffer,
330 strlen(global_read_buffer));
331 write(temp_bookmark_file, "\n", 1);
334 close(bookmark_file);
336 close(temp_bookmark_file);
338 remove(bookmark_file_name);
339 rename(global_temp_buffer, bookmark_file_name);
341 return true;
345 /* ----------------------------------------------------------------------- */
346 /* This function takes the system resume data and formats it into a valid */
347 /* bookmark. */
348 /* Returns not NULL on successful bookmark format. */
349 /* ----------------------------------------------------------------------- */
350 static char* create_bookmark()
352 int resume_index = 0;
353 char *file;
355 if (!bookmark_is_bookmarkable_state())
356 return NULL; /* something didn't happen correctly, do nothing */
358 /* grab the currently playing track */
359 struct mp3entry *id3 = audio_current_track();
360 if(!id3)
361 return NULL;
363 /* Get some basic resume information */
364 /* queue_resume and queue_resume_index are not used and can be ignored.*/
365 playlist_get_resume_info(&resume_index);
367 /* Get the currently playing file minus the path */
368 /* This is used when displaying the available bookmarks */
369 file = strrchr(id3->path,'/');
370 if(NULL == file)
371 return NULL;
373 /* create the bookmark */
374 snprintf(global_bookmark, sizeof(global_bookmark),
375 /* new optional bookmark token descriptors should be inserted
376 just before the "%s;%s" in this line... */
377 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
378 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
379 #else
380 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
381 #endif
382 /* ... their flags should go here ... */
383 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
384 BM_PITCH | BM_SPEED,
385 #else
387 #endif
388 resume_index,
389 id3->offset,
390 playlist_get_seed(NULL),
391 id3->elapsed,
392 global_settings.repeat_mode,
393 global_settings.playlist_shuffle,
394 /* ...and their values should go here */
395 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
396 (long)sound_get_pitch(),
397 (long)dsp_get_timestretch(),
398 #endif
399 /* more mandatory tokens */
400 playlist_get_name(NULL, global_temp_buffer,
401 sizeof(global_temp_buffer)),
402 file+1);
404 /* checking to see if the bookmark is valid */
405 if (parse_bookmark(global_bookmark, false))
406 return global_bookmark;
407 else
408 return NULL;
411 /* ----------------------------------------------------------------------- */
412 /* This function will determine if an autoload is necessary. This is an */
413 /* interface function. */
414 /* Returns true on bookmark load or bookmark selection. */
415 /* ------------------------------------------------------------------------*/
416 bool bookmark_autoload(const char* file)
418 char* bookmark;
420 if(global_settings.autoloadbookmark == BOOKMARK_NO)
421 return false;
423 /*Checking to see if a bookmark file exists.*/
424 if(!generate_bookmark_file_name(file))
426 return false;
429 if(!file_exists(global_bookmark_file_name))
430 return false;
432 if(global_settings.autoloadbookmark == BOOKMARK_YES)
434 return bookmark_load(global_bookmark_file_name, true);
436 else
438 select_bookmark(global_bookmark_file_name, true, &bookmark);
440 if (bookmark != NULL)
442 if (!play_bookmark(bookmark))
444 /* Selected bookmark not found. */
445 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
448 /* Act as if autoload was done even if it failed, since the
449 * user did make an active selection.
451 return true;
454 return false;
458 /* ----------------------------------------------------------------------- */
459 /* This function loads the bookmark information into the resume memory. */
460 /* This is an interface function. */
461 /* Returns true on successful bookmark load. */
462 /* ------------------------------------------------------------------------*/
463 bool bookmark_load(const char* file, bool autoload)
465 int fd;
466 char* bookmark = NULL;
468 if(autoload)
470 fd = open(file, O_RDONLY);
471 if(fd >= 0)
473 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
474 bookmark=global_read_buffer;
475 close(fd);
478 else
480 /* This is not an auto-load, so list the bookmarks */
481 select_bookmark(file, false, &bookmark);
484 if (bookmark != NULL)
486 if (!play_bookmark(bookmark))
488 /* Selected bookmark not found. */
489 if (!autoload)
491 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
494 return false;
498 return true;
502 static int get_bookmark_count(const char* bookmark_file_name)
504 int read_count = 0;
505 int file = open(bookmark_file_name, O_RDONLY);
507 if(file < 0)
508 return -1;
510 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
512 read_count++;
515 close(file);
516 return read_count;
519 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
521 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
522 int read_count = 0;
523 int file = open(bookmarks->filename, O_RDONLY);
525 if (file < 0)
527 return -1;
530 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
531 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
533 /* Entire file fits in buffer */
534 first_line = 0;
537 bookmarks->start = first_line;
538 bookmarks->count = 0;
539 bookmarks->reload = false;
541 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
543 read_count++;
545 if (read_count >= first_line)
547 dest -= strlen(global_read_buffer) + 1;
549 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
550 + (sizeof(char*) * (bookmarks->count + 1)))
552 break;
555 strcpy(dest, global_read_buffer);
556 bookmarks->items[bookmarks->count] = dest;
557 bookmarks->count++;
561 close(file);
562 return bookmarks->start + bookmarks->count;
565 static const char* get_bookmark_info(int list_index,
566 void* data,
567 char *buffer,
568 size_t buffer_len)
570 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
571 int index = list_index / 2;
573 if (bookmarks->show_dont_resume)
575 if (index == 0)
577 return list_index % 2 == 0
578 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
581 index--;
584 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
585 || (index < bookmarks->start))
587 int read_index = index;
589 /* Using count as a guide on how far to move could possibly fail
590 * sometimes. Use byte count if that is a problem?
593 if (read_index != 0)
595 /* Move count * 3 / 4 items in the direction the user is moving,
596 * but don't go too close to the end.
598 int offset = bookmarks->count;
599 int max = bookmarks->total_count - (bookmarks->count / 2);
601 if (read_index < bookmarks->start)
603 offset *= 3;
606 read_index = index - offset / 4;
608 if (read_index > max)
610 read_index = max;
613 if (read_index < 0)
615 read_index = 0;
619 if (buffer_bookmarks(bookmarks, read_index) <= index)
621 return "";
625 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
627 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
630 if (list_index % 2 == 0)
632 char *name;
633 char *format;
634 int len = strlen(global_temp_buffer);
636 if (bookmarks->show_playlist_name && len > 0)
638 name = global_temp_buffer;
639 len--;
641 if (name[len] != '/')
643 strrsplt(name, '.');
645 else if (len > 1)
647 name[len] = '\0';
650 if (len > 1)
652 name = strrsplt(name, '/');
655 format = "%s : %s";
657 else
659 name = global_filename;
660 format = "%s";
663 strrsplt(global_filename, '.');
664 snprintf(buffer, buffer_len, format, name, global_filename);
665 return buffer;
667 else
669 char time_buf[32];
671 format_time(time_buf, sizeof(time_buf), bm.resume_time);
672 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
673 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
674 return buffer;
678 static int bookmark_list_voice_cb(int list_index, void* data)
680 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
681 int index = list_index / 2;
683 if (bookmarks->show_dont_resume)
685 if (index == 0)
686 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
687 index--;
689 say_bookmark(bookmarks->items[index - bookmarks->start], index,
690 bookmarks->show_playlist_name);
691 return 0;
694 /* ----------------------------------------------------------------------- */
695 /* This displays the bookmarks in a file and allows the user to */
696 /* select one to play. */
697 /* *selected_bookmark contains a non NULL value on successful bookmark */
698 /* selection. */
699 /* Returns BOOKMARK_SUCCESS on successful bookmark selection, BOOKMARK_FAIL*/
700 /* if no selection was made and BOOKMARK_USB_CONNECTED if the selection */
701 /* menu is forced to exit due to a USB connection. */
702 /* ------------------------------------------------------------------------*/
703 static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark)
705 struct bookmark_list* bookmarks;
706 struct gui_synclist list;
707 int item = 0;
708 int action;
709 size_t size;
710 bool exit = false;
711 bool refresh = true;
712 int ret = BOOKMARK_FAIL;
714 bookmarks = plugin_get_buffer(&size);
715 bookmarks->buffer_size = size;
716 bookmarks->show_dont_resume = show_dont_resume;
717 bookmarks->filename = bookmark_file_name;
718 bookmarks->start = 0;
719 bookmarks->show_playlist_name
720 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
721 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
722 if(global_settings.talk_menu)
723 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
724 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
725 Icon_Bookmark);
727 while (!exit)
730 if (refresh)
732 int count = get_bookmark_count(bookmark_file_name);
733 bookmarks->total_count = count;
735 if (bookmarks->total_count < 1)
737 /* No more bookmarks, delete file and exit */
738 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
739 remove(bookmark_file_name);
740 *selected_bookmark = NULL;
741 return BOOKMARK_FAIL;
744 if (bookmarks->show_dont_resume)
746 count++;
747 item++;
750 gui_synclist_set_nb_items(&list, count * 2);
752 if (item >= count)
754 /* Selected item has been deleted */
755 item = count - 1;
756 gui_synclist_select_item(&list, item * 2);
759 buffer_bookmarks(bookmarks, bookmarks->start);
760 gui_synclist_draw(&list);
761 cond_talk_ids_fq(VOICE_EXT_BMARK);
762 gui_synclist_speak_item(&list);
763 refresh = false;
766 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
767 &list, &action, LIST_WRAP_UNLESS_HELD);
768 item = gui_synclist_get_sel_pos(&list) / 2;
770 if (bookmarks->show_dont_resume)
772 item--;
775 if (action == ACTION_STD_CONTEXT)
777 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
778 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
779 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
780 static const int menu_actions[] =
782 ACTION_STD_OK, ACTION_BMS_DELETE
784 int selection = do_menu(&menu_items, NULL, NULL, false);
786 refresh = true;
788 if (selection >= 0 && selection <=
789 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
791 action = menu_actions[selection];
795 switch (action)
797 case ACTION_STD_OK:
798 if (item >= 0)
800 talk_shutup();
801 *selected_bookmark = bookmarks->items[item - bookmarks->start];
802 return BOOKMARK_SUCCESS;
805 /* Else fall through */
807 case ACTION_TREE_WPS:
808 case ACTION_STD_CANCEL:
809 exit = true;
810 break;
812 case ACTION_BMS_DELETE:
813 if (item >= 0)
815 const char *lines[]={
816 ID2P(LANG_REALLY_DELETE)
818 const char *yes_lines[]={
819 ID2P(LANG_DELETING)
822 const struct text_message message={lines, 1};
823 const struct text_message yes_message={yes_lines, 1};
825 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
827 delete_bookmark(bookmark_file_name, item);
828 bookmarks->reload = true;
830 refresh = true;
832 break;
834 default:
835 if (default_event_handler(action) == SYS_USB_CONNECTED)
837 ret = BOOKMARK_USB_CONNECTED;
838 exit = true;
841 break;
845 talk_shutup();
846 *selected_bookmark = NULL;
847 return ret;
850 /* ----------------------------------------------------------------------- */
851 /* This function takes a location in a bookmark file and deletes that */
852 /* bookmark. */
853 /* Returns true on successful bookmark deletion. */
854 /* ------------------------------------------------------------------------*/
855 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
857 int temp_bookmark_file = 0;
858 int bookmark_file = 0;
859 int bookmark_count = 0;
861 /* Opening up a temp bookmark file */
862 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
863 "%s.tmp", bookmark_file_name);
864 temp_bookmark_file = open(global_temp_buffer,
865 O_WRONLY | O_CREAT | O_TRUNC, 0666);
867 if (temp_bookmark_file < 0)
868 return false; /* can't open the temp file */
870 /* Reading in the previous bookmarks and writing them to the temp file */
871 bookmark_file = open(bookmark_file_name, O_RDONLY);
872 if (bookmark_file >= 0)
874 while (read_line(bookmark_file, global_read_buffer,
875 sizeof(global_read_buffer)) > 0)
877 if (bookmark_id != bookmark_count)
879 write(temp_bookmark_file, global_read_buffer,
880 strlen(global_read_buffer));
881 write(temp_bookmark_file, "\n", 1);
883 bookmark_count++;
885 close(bookmark_file);
887 close(temp_bookmark_file);
889 remove(bookmark_file_name);
890 rename(global_temp_buffer, bookmark_file_name);
892 return true;
895 /* ----------------------------------------------------------------------- */
896 /* This function parses a bookmark, says the voice UI part of it. */
897 /* ------------------------------------------------------------------------*/
898 static void say_bookmark(const char* bookmark,
899 int bookmark_id, bool show_playlist_name)
901 if (!parse_bookmark(bookmark, true))
903 talk_id(LANG_BOOKMARK_INVALID, false);
904 return;
907 talk_number(bookmark_id + 1, false);
909 #if CONFIG_CODEC == SWCODEC
910 bool is_dir = (global_temp_buffer[0]
911 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
913 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
914 together, because there is no guarantee that the same mp3
915 parameters are used. */
916 if(show_playlist_name)
917 { /* It's useful to know which playlist this is */
918 if(is_dir)
919 talk_dir_or_spell(global_temp_buffer,
920 TALK_IDARRAY(VOICE_DIR), true);
921 else talk_file_or_spell(NULL, global_temp_buffer,
922 TALK_IDARRAY(LANG_PLAYLIST), true);
924 #else
925 (void)show_playlist_name;
926 #endif
928 if(bm.shuffle)
929 talk_id(LANG_SHUFFLE, true);
931 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
932 talk_number(bm.resume_index + 1, true);
933 talk_id(LANG_TIME, true);
934 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
936 #if CONFIG_CODEC == SWCODEC
937 /* Track filename */
938 if(is_dir)
939 talk_file_or_spell(global_temp_buffer, global_filename,
940 TALK_IDARRAY(VOICE_FILE), true);
941 else
942 { /* Unfortunately if this is a playlist, we do not know in which
943 directory the file is and therefore cannot find the track's
944 .talk file. */
945 talk_id(VOICE_FILE, true);
946 talk_spell(global_filename, true);
948 #endif
951 /* ----------------------------------------------------------------------- */
952 /* This function parses a bookmark and then plays it. */
953 /* Returns true on successful bookmark play. */
954 /* ------------------------------------------------------------------------*/
955 static bool play_bookmark(const char* bookmark)
957 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
958 /* preset pitch and speed to 100% in case bookmark doesn't have info */
959 bm.pitch = sound_get_pitch();
960 bm.speed = dsp_get_timestretch();
961 #endif
963 if (parse_bookmark(bookmark, true))
965 global_settings.repeat_mode = bm.repeat_mode;
966 global_settings.playlist_shuffle = bm.shuffle;
967 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHCONTROL)
968 sound_set_pitch(bm.pitch);
969 dsp_set_timestretch(bm.speed);
970 #endif
971 if (!warn_on_pl_erase())
972 return false;
973 return bookmark_play(global_temp_buffer, bm.resume_index,
974 bm.resume_offset, bm.resume_seed, global_filename);
977 return false;
980 static const char* skip_token(const char* s)
982 while (*s && *s != ';')
984 s++;
987 if (*s)
989 s++;
992 return s;
995 static const char* int_token(const char* s, int* dest)
997 *dest = atoi(s);
998 return skip_token(s);
1001 static const char* long_token(const char* s, long* dest)
1003 *dest = atoi(s); /* Should be atol, but we don't have it. */
1004 return skip_token(s);
1007 /* ----------------------------------------------------------------------- */
1008 /* This function takes a bookmark and parses it. This function also */
1009 /* validates the bookmark. The parse_filenames flag indicates whether */
1010 /* the filename tokens are to be extracted. */
1011 /* Returns true on successful bookmark parse. */
1012 /* ----------------------------------------------------------------------- */
1013 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
1015 const char* s = bookmark;
1016 const char* end;
1018 #define GET_INT_TOKEN(var) s = int_token(s, &var)
1019 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
1020 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
1022 /* if new format bookmark, extract the optional content flags,
1023 otherwise treat as an original format bookmark */
1024 int opt_flags = 0;
1025 bool new_format = (strchr(s, '>') == s);
1026 if (new_format)
1028 s++;
1029 GET_INT_TOKEN(opt_flags);
1032 /* extract all original bookmark tokens */
1033 GET_INT_TOKEN(bm.resume_index);
1034 GET_LONG_TOKEN(bm.resume_offset);
1035 GET_INT_TOKEN(bm.resume_seed);
1036 if (!new_format) /* skip deprecated token */
1037 s = skip_token(s);
1038 GET_LONG_TOKEN(bm.resume_time);
1039 GET_INT_TOKEN(bm.repeat_mode);
1040 GET_BOOL_TOKEN(bm.shuffle);
1042 /* extract all optional bookmark tokens */
1043 if (opt_flags & BM_PITCH)
1044 GET_INT_TOKEN(bm.pitch);
1045 if (opt_flags & BM_SPEED)
1046 GET_INT_TOKEN(bm.speed);
1048 if (*s == 0)
1050 return false;
1053 end = strchr(s, ';');
1055 /* extract file names */
1056 if (parse_filenames)
1058 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1059 len = MIN(TEMP_BUF_SIZE - 1, len);
1060 strlcpy(global_temp_buffer, s, len + 1);
1062 if (end != NULL)
1064 end++;
1065 strlcpy(global_filename, end, MAX_PATH);
1069 return true;
1072 /* ----------------------------------------------------------------------- */
1073 /* This function is used by multiple functions and is used to generate a */
1074 /* bookmark named based off of the input. */
1075 /* Changing this function could result in how the bookmarks are stored. */
1076 /* it would be here that the centralized/decentralized bookmark code */
1077 /* could be placed. */
1078 /* Always returns true */
1079 /* ----------------------------------------------------------------------- */
1080 static bool generate_bookmark_file_name(const char *in)
1082 int len = strlen(in);
1084 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1085 /* otherwise, name it based on the in variable */
1086 if (!strcmp("/", in))
1087 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1088 else
1090 #ifdef HAVE_MULTIVOLUME
1091 /* The "root" of an extra volume need special handling too. */
1092 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1093 !strcmp("/", global_bookmark_file_name));
1094 #endif
1096 strcpy(global_bookmark_file_name, in);
1097 if(global_bookmark_file_name[len-1] == '/')
1098 len--;
1099 #ifdef HAVE_MULTIVOLUME
1100 if (volume_root)
1101 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1102 else
1103 #endif
1104 strcpy(&global_bookmark_file_name[len], ".bmark");
1107 return true;
1110 /* ----------------------------------------------------------------------- */
1111 /* Returns true if a bookmark file exists for the current playlist. */
1112 /* This is an interface function. */
1113 /* ----------------------------------------------------------------------- */
1114 bool bookmark_exists(void)
1116 bool exist=false;
1118 char* name = playlist_get_name(NULL, global_temp_buffer,
1119 sizeof(global_temp_buffer));
1120 if (generate_bookmark_file_name(name))
1122 exist = file_exists(global_bookmark_file_name);
1124 return exist;
1127 /* ----------------------------------------------------------------------- */
1128 /* Checks the current state of the system and returns true if it is in a */
1129 /* bookmarkable state. */
1130 /* This is an interface funtion. */
1131 /* ----------------------------------------------------------------------- */
1132 bool bookmark_is_bookmarkable_state(void)
1134 int resume_index = 0;
1136 if (!(audio_status() && audio_current_track()) ||
1137 /* no track playing */
1138 (playlist_get_resume_info(&resume_index) == -1) ||
1139 /* invalid queue info */
1140 (playlist_modified(NULL)))
1141 /* can't bookmark while in the queue */
1143 return false;
1146 return true;