Fix FS#12606 - next track can cause the screen to be cleared
[maemo-rb.git] / apps / bookmark.c
blobe0325b38d436bbce10c9a36371ab6b87b2a0b2e6
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"
45 #include "dsp.h"
47 #define MAX_BOOKMARKS 10
48 #define MAX_BOOKMARK_SIZE 350
49 #define RECENT_BOOKMARK_FILE ROCKBOX_DIR "/most-recent.bmark"
51 /* Used to buffer bookmarks while displaying the bookmark list. */
52 struct bookmark_list
54 const char* filename;
55 size_t buffer_size;
56 int start;
57 int count;
58 int total_count;
59 bool show_dont_resume;
60 bool reload;
61 bool show_playlist_name;
62 char* items[];
65 /* flags for optional bookmark tokens */
66 #define BM_PITCH 0x01
67 #define BM_SPEED 0x02
69 /* bookmark values */
70 static struct {
71 int resume_index;
72 unsigned long resume_offset;
73 int resume_seed;
74 long resume_time;
75 int repeat_mode;
76 bool shuffle;
77 /* optional values */
78 int pitch;
79 int speed;
80 } bm;
82 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
83 bool most_recent);
84 static char* create_bookmark(void);
85 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id);
86 static void say_bookmark(const char* bookmark,
87 int bookmark_id, bool show_playlist_name);
88 static bool play_bookmark(const char* bookmark);
89 static bool generate_bookmark_file_name(const char *in);
90 static bool parse_bookmark(const char *bookmark, const bool get_filenames);
91 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line);
92 static const char* get_bookmark_info(int list_index,
93 void* data,
94 char *buffer,
95 size_t buffer_len);
96 static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark);
97 static bool write_bookmark(bool create_bookmark_file, const char *bookmark);
98 static int get_bookmark_count(const char* bookmark_file_name);
100 #define TEMP_BUF_SIZE (MAX_PATH + 1)
101 static char global_temp_buffer[TEMP_BUF_SIZE];
102 /* File name created by generate_bookmark_file_name */
103 static char global_bookmark_file_name[MAX_PATH];
104 static char global_read_buffer[MAX_BOOKMARK_SIZE];
105 /* Bookmark created by create_bookmark*/
106 static char global_bookmark[MAX_BOOKMARK_SIZE];
107 /* Filename from parsed bookmark (can be made local where needed) */
108 static char global_filename[MAX_PATH];
110 /* ----------------------------------------------------------------------- */
111 /* This is an interface function from the context menu. */
112 /* Returns true on successful bookmark creation. */
113 /* ----------------------------------------------------------------------- */
114 bool bookmark_create_menu(void)
116 return write_bookmark(true, create_bookmark());
119 /* ----------------------------------------------------------------------- */
120 /* This function acts as the load interface from the context menu. */
121 /* This function determines the bookmark file name and then loads that file*/
122 /* for the user. The user can then select or delete previous bookmarks. */
123 /* This function returns BOOKMARK_SUCCESS on the selection of a track to */
124 /* resume, BOOKMARK_FAIL if the menu is exited without a selection and */
125 /* BOOKMARK_USB_CONNECTED if the menu is forced to exit due to a USB */
126 /* connection. */
127 /* ----------------------------------------------------------------------- */
128 int bookmark_load_menu(void)
130 char* bookmark;
131 int ret = BOOKMARK_FAIL;
133 push_current_activity(ACTIVITY_BOOKMARKSLIST);
135 char* name = playlist_get_name(NULL, global_temp_buffer,
136 sizeof(global_temp_buffer));
137 if (generate_bookmark_file_name(name))
139 ret = select_bookmark(global_bookmark_file_name, false, &bookmark);
140 if (bookmark != NULL)
142 ret = play_bookmark(bookmark) ? BOOKMARK_SUCCESS : BOOKMARK_FAIL;
146 pop_current_activity();
147 return ret;
150 /* ----------------------------------------------------------------------- */
151 /* Gives the user a list of the Most Recent Bookmarks. This is an */
152 /* interface function */
153 /* Returns true on the successful selection of a recent bookmark. */
154 /* ----------------------------------------------------------------------- */
155 bool bookmark_mrb_load()
157 char* bookmark;
158 bool ret = false;
160 push_current_activity(ACTIVITY_BOOKMARKSLIST);
161 select_bookmark(RECENT_BOOKMARK_FILE, false, &bookmark);
162 if (bookmark != NULL)
164 ret = play_bookmark(bookmark);
167 pop_current_activity();
168 return ret;
171 /* ----------------------------------------------------------------------- */
172 /* This function handles an autobookmark creation. This is an interface */
173 /* function. */
174 /* Returns true on successful bookmark creation. */
175 /* ----------------------------------------------------------------------- */
176 bool bookmark_autobookmark(bool prompt_ok)
178 char* bookmark;
179 bool update;
181 if (!bookmark_is_bookmarkable_state())
182 return false;
184 audio_pause(); /* first pause playback */
185 update = (global_settings.autoupdatebookmark && bookmark_exists());
186 bookmark = create_bookmark();
187 #if CONFIG_CODEC != SWCODEC
188 /* Workaround for inability to speak when paused: all callers will
189 just do audio_stop() when we return, so we can do it right
190 away. This makes it possible to speak the "Create a Bookmark?"
191 prompt and the "Bookmark Created" splash. */
192 audio_stop();
193 #endif
195 if (update)
196 return write_bookmark(true, bookmark);
198 switch (global_settings.autocreatebookmark)
200 case BOOKMARK_YES:
201 return write_bookmark(true, bookmark);
203 case BOOKMARK_NO:
204 return false;
206 case BOOKMARK_RECENT_ONLY_YES:
207 return write_bookmark(false, bookmark);
209 #ifdef HAVE_LCD_BITMAP
210 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY)};
211 const struct text_message message={lines, 1};
212 #else
213 const char *lines[]={ID2P(LANG_AUTO_BOOKMARK_QUERY),
214 str(LANG_CONFIRM_WITH_BUTTON)};
215 const struct text_message message={lines, 2};
216 #endif
218 if(prompt_ok && gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
220 if (global_settings.autocreatebookmark == BOOKMARK_RECENT_ONLY_ASK)
221 return write_bookmark(false, bookmark);
222 else
223 return write_bookmark(true, bookmark);
225 return false;
228 /* ----------------------------------------------------------------------- */
229 /* This function takes the current current resume information and writes */
230 /* that to the beginning of the bookmark file. */
231 /* This file will contain N number of bookmarks in the following format: */
232 /* resume_index*resume_offset*resume_seed*resume_first_index* */
233 /* resume_file*milliseconds*MP3 Title* */
234 /* Returns true on successful bookmark write. */
235 /* Returns false if any part of the bookmarking process fails. It is */
236 /* possible that a bookmark is successfully added to the most recent */
237 /* bookmark list but fails to be added to the bookmark file or vice versa. */
238 /* ------------------------------------------------------------------------*/
239 static bool write_bookmark(bool create_bookmark_file, const char *bookmark)
241 bool ret=true;
243 if (!bookmark)
245 ret = false; /* something didn't happen correctly, do nothing */
247 else
249 if (global_settings.usemrb)
250 ret = add_bookmark(RECENT_BOOKMARK_FILE, bookmark, true);
253 /* writing the bookmark */
254 if (create_bookmark_file)
256 char* name = playlist_get_name(NULL, global_temp_buffer,
257 sizeof(global_temp_buffer));
258 if (generate_bookmark_file_name(name))
260 ret = ret & add_bookmark(global_bookmark_file_name, bookmark, false);
262 else
264 ret = false; /* generating bookmark file failed */
269 splash(HZ, ret ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS)
270 : ID2P(LANG_BOOKMARK_CREATE_FAILURE));
272 return ret;
275 /* ----------------------------------------------------------------------- */
276 /* This function adds a bookmark to a file. */
277 /* Returns true on successful bookmark add. */
278 /* ------------------------------------------------------------------------*/
279 static bool add_bookmark(const char* bookmark_file_name, const char* bookmark,
280 bool most_recent)
282 int temp_bookmark_file = 0;
283 int bookmark_file = 0;
284 int bookmark_count = 0;
285 char* playlist = NULL;
286 char* cp;
287 char* tmp;
288 int len = 0;
289 bool unique = false;
291 /* Opening up a temp bookmark file */
292 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
293 "%s.tmp", bookmark_file_name);
294 temp_bookmark_file = open(global_temp_buffer,
295 O_WRONLY | O_CREAT | O_TRUNC, 0666);
296 if (temp_bookmark_file < 0)
297 return false; /* can't open the temp file */
299 if (most_recent && (global_settings.usemrb == BOOKMARK_UNIQUE_ONLY))
301 playlist = strchr(bookmark,'/');
302 cp = strrchr(bookmark,';');
303 len = cp - playlist;
304 unique = true;
307 /* Writing the new bookmark to the begining of the temp file */
308 write(temp_bookmark_file, bookmark, strlen(bookmark));
309 write(temp_bookmark_file, "\n", 1);
310 bookmark_count++;
312 /* Reading in the previous bookmarks and writing them to the temp file */
313 bookmark_file = open(bookmark_file_name, O_RDONLY);
314 if (bookmark_file >= 0)
316 while (read_line(bookmark_file, global_read_buffer,
317 sizeof(global_read_buffer)) > 0)
319 /* The MRB has a max of MAX_BOOKMARKS in it */
320 /* This keeps it from getting too large */
321 if (most_recent && (bookmark_count >= MAX_BOOKMARKS))
322 break;
324 cp = strchr(global_read_buffer,'/');
325 tmp = strrchr(global_read_buffer,';');
326 if (parse_bookmark(global_read_buffer, false) &&
327 (!unique || len != tmp -cp || strncmp(playlist,cp,len)))
329 bookmark_count++;
330 write(temp_bookmark_file, global_read_buffer,
331 strlen(global_read_buffer));
332 write(temp_bookmark_file, "\n", 1);
335 close(bookmark_file);
337 close(temp_bookmark_file);
339 remove(bookmark_file_name);
340 rename(global_temp_buffer, bookmark_file_name);
342 return true;
346 /* ----------------------------------------------------------------------- */
347 /* This function takes the system resume data and formats it into a valid */
348 /* bookmark. */
349 /* Returns not NULL on successful bookmark format. */
350 /* ----------------------------------------------------------------------- */
351 static char* create_bookmark()
353 int resume_index = 0;
354 char *file;
356 if (!bookmark_is_bookmarkable_state())
357 return NULL; /* something didn't happen correctly, do nothing */
359 /* grab the currently playing track */
360 struct mp3entry *id3 = audio_current_track();
361 if(!id3)
362 return NULL;
364 /* Get some basic resume information */
365 /* queue_resume and queue_resume_index are not used and can be ignored.*/
366 playlist_get_resume_info(&resume_index);
368 /* Get the currently playing file minus the path */
369 /* This is used when displaying the available bookmarks */
370 file = strrchr(id3->path,'/');
371 if(NULL == file)
372 return NULL;
374 /* create the bookmark */
375 snprintf(global_bookmark, sizeof(global_bookmark),
376 /* new optional bookmark token descriptors should be inserted
377 just before the "%s;%s" in this line... */
378 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
379 ">%d;%d;%ld;%d;%ld;%d;%d;%ld;%ld;%s;%s",
380 #else
381 ">%d;%d;%ld;%d;%ld;%d;%d;%s;%s",
382 #endif
383 /* ... their flags should go here ... */
384 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
385 BM_PITCH | BM_SPEED,
386 #else
388 #endif
389 resume_index,
390 id3->offset,
391 playlist_get_seed(NULL),
392 id3->elapsed,
393 global_settings.repeat_mode,
394 global_settings.playlist_shuffle,
395 /* ...and their values should go here */
396 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
397 (long)sound_get_pitch(),
398 (long)dsp_get_timestretch(),
399 #endif
400 /* more mandatory tokens */
401 playlist_get_name(NULL, global_temp_buffer,
402 sizeof(global_temp_buffer)),
403 file+1);
405 /* checking to see if the bookmark is valid */
406 if (parse_bookmark(global_bookmark, false))
407 return global_bookmark;
408 else
409 return NULL;
412 /* ----------------------------------------------------------------------- */
413 /* This function will determine if an autoload is necessary. This is an */
414 /* interface function. */
415 /* Returns true on bookmark load or bookmark selection. */
416 /* ------------------------------------------------------------------------*/
417 bool bookmark_autoload(const char* file)
419 char* bookmark;
421 if(global_settings.autoloadbookmark == BOOKMARK_NO)
422 return false;
424 /*Checking to see if a bookmark file exists.*/
425 if(!generate_bookmark_file_name(file))
427 return false;
430 if(!file_exists(global_bookmark_file_name))
431 return false;
433 if(global_settings.autoloadbookmark == BOOKMARK_YES)
435 return bookmark_load(global_bookmark_file_name, true);
437 else
439 select_bookmark(global_bookmark_file_name, true, &bookmark);
441 if (bookmark != NULL)
443 if (!play_bookmark(bookmark))
445 /* Selected bookmark not found. */
446 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
449 /* Act as if autoload was done even if it failed, since the
450 * user did make an active selection.
452 return true;
455 return false;
459 /* ----------------------------------------------------------------------- */
460 /* This function loads the bookmark information into the resume memory. */
461 /* This is an interface function. */
462 /* Returns true on successful bookmark load. */
463 /* ------------------------------------------------------------------------*/
464 bool bookmark_load(const char* file, bool autoload)
466 int fd;
467 char* bookmark = NULL;
469 if(autoload)
471 fd = open(file, O_RDONLY);
472 if(fd >= 0)
474 if(read_line(fd, global_read_buffer, sizeof(global_read_buffer)) > 0)
475 bookmark=global_read_buffer;
476 close(fd);
479 else
481 /* This is not an auto-load, so list the bookmarks */
482 select_bookmark(file, false, &bookmark);
485 if (bookmark != NULL)
487 if (!play_bookmark(bookmark))
489 /* Selected bookmark not found. */
490 if (!autoload)
492 splash(HZ*2, ID2P(LANG_NOTHING_TO_RESUME));
495 return false;
499 return true;
503 static int get_bookmark_count(const char* bookmark_file_name)
505 int read_count = 0;
506 int file = open(bookmark_file_name, O_RDONLY);
508 if(file < 0)
509 return -1;
511 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
513 read_count++;
516 close(file);
517 return read_count;
520 static int buffer_bookmarks(struct bookmark_list* bookmarks, int first_line)
522 char* dest = ((char*) bookmarks) + bookmarks->buffer_size - 1;
523 int read_count = 0;
524 int file = open(bookmarks->filename, O_RDONLY);
526 if (file < 0)
528 return -1;
531 if ((first_line != 0) && ((size_t) filesize(file) < bookmarks->buffer_size
532 - sizeof(*bookmarks) - (sizeof(char*) * bookmarks->total_count)))
534 /* Entire file fits in buffer */
535 first_line = 0;
538 bookmarks->start = first_line;
539 bookmarks->count = 0;
540 bookmarks->reload = false;
542 while(read_line(file, global_read_buffer, sizeof(global_read_buffer)) > 0)
544 read_count++;
546 if (read_count >= first_line)
548 dest -= strlen(global_read_buffer) + 1;
550 if (dest < ((char*) bookmarks) + sizeof(*bookmarks)
551 + (sizeof(char*) * (bookmarks->count + 1)))
553 break;
556 strcpy(dest, global_read_buffer);
557 bookmarks->items[bookmarks->count] = dest;
558 bookmarks->count++;
562 close(file);
563 return bookmarks->start + bookmarks->count;
566 static const char* get_bookmark_info(int list_index,
567 void* data,
568 char *buffer,
569 size_t buffer_len)
571 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
572 int index = list_index / 2;
574 if (bookmarks->show_dont_resume)
576 if (index == 0)
578 return list_index % 2 == 0
579 ? (char*) str(LANG_BOOKMARK_DONT_RESUME) : " ";
582 index--;
585 if (bookmarks->reload || (index >= bookmarks->start + bookmarks->count)
586 || (index < bookmarks->start))
588 int read_index = index;
590 /* Using count as a guide on how far to move could possibly fail
591 * sometimes. Use byte count if that is a problem?
594 if (read_index != 0)
596 /* Move count * 3 / 4 items in the direction the user is moving,
597 * but don't go too close to the end.
599 int offset = bookmarks->count;
600 int max = bookmarks->total_count - (bookmarks->count / 2);
602 if (read_index < bookmarks->start)
604 offset *= 3;
607 read_index = index - offset / 4;
609 if (read_index > max)
611 read_index = max;
614 if (read_index < 0)
616 read_index = 0;
620 if (buffer_bookmarks(bookmarks, read_index) <= index)
622 return "";
626 if (!parse_bookmark(bookmarks->items[index - bookmarks->start], true))
628 return list_index % 2 == 0 ? (char*) str(LANG_BOOKMARK_INVALID) : " ";
631 if (list_index % 2 == 0)
633 char *name;
634 char *format;
635 int len = strlen(global_temp_buffer);
637 if (bookmarks->show_playlist_name && len > 0)
639 name = global_temp_buffer;
640 len--;
642 if (name[len] != '/')
644 strrsplt(name, '.');
646 else if (len > 1)
648 name[len] = '\0';
651 if (len > 1)
653 name = strrsplt(name, '/');
656 format = "%s : %s";
658 else
660 name = global_filename;
661 format = "%s";
664 strrsplt(global_filename, '.');
665 snprintf(buffer, buffer_len, format, name, global_filename);
666 return buffer;
668 else
670 char time_buf[32];
672 format_time(time_buf, sizeof(time_buf), bm.resume_time);
673 snprintf(buffer, buffer_len, "%s, %d%s", time_buf, bm.resume_index + 1,
674 bm.shuffle ? (char*) str(LANG_BOOKMARK_SHUFFLE) : "");
675 return buffer;
679 static int bookmark_list_voice_cb(int list_index, void* data)
681 struct bookmark_list* bookmarks = (struct bookmark_list*) data;
682 int index = list_index / 2;
684 if (bookmarks->show_dont_resume)
686 if (index == 0)
687 return talk_id(LANG_BOOKMARK_DONT_RESUME, false);
688 index--;
690 say_bookmark(bookmarks->items[index - bookmarks->start], index,
691 bookmarks->show_playlist_name);
692 return 0;
695 /* ----------------------------------------------------------------------- */
696 /* This displays the bookmarks in a file and allows the user to */
697 /* select one to play. */
698 /* *selected_bookmark contains a non NULL value on successful bookmark */
699 /* selection. */
700 /* Returns BOOKMARK_SUCCESS on successful bookmark selection, BOOKMARK_FAIL*/
701 /* if no selection was made and BOOKMARK_USB_CONNECTED if the selection */
702 /* menu is forced to exit due to a USB connection. */
703 /* ------------------------------------------------------------------------*/
704 static int select_bookmark(const char* bookmark_file_name, bool show_dont_resume, char** selected_bookmark)
706 struct bookmark_list* bookmarks;
707 struct gui_synclist list;
708 int item = 0;
709 int action;
710 size_t size;
711 bool exit = false;
712 bool refresh = true;
713 int ret = BOOKMARK_FAIL;
715 bookmarks = plugin_get_buffer(&size);
716 bookmarks->buffer_size = size;
717 bookmarks->show_dont_resume = show_dont_resume;
718 bookmarks->filename = bookmark_file_name;
719 bookmarks->start = 0;
720 bookmarks->show_playlist_name
721 = strcmp(bookmark_file_name, RECENT_BOOKMARK_FILE) == 0;
722 gui_synclist_init(&list, &get_bookmark_info, (void*) bookmarks, false, 2, NULL);
723 if(global_settings.talk_menu)
724 gui_synclist_set_voice_callback(&list, bookmark_list_voice_cb);
725 gui_synclist_set_title(&list, str(LANG_BOOKMARK_SELECT_BOOKMARK),
726 Icon_Bookmark);
728 while (!exit)
731 if (refresh)
733 int count = get_bookmark_count(bookmark_file_name);
734 bookmarks->total_count = count;
736 if (bookmarks->total_count < 1)
738 /* No more bookmarks, delete file and exit */
739 splash(HZ, ID2P(LANG_BOOKMARK_LOAD_EMPTY));
740 remove(bookmark_file_name);
741 *selected_bookmark = NULL;
742 return BOOKMARK_FAIL;
745 if (bookmarks->show_dont_resume)
747 count++;
748 item++;
751 gui_synclist_set_nb_items(&list, count * 2);
753 if (item >= count)
755 /* Selected item has been deleted */
756 item = count - 1;
757 gui_synclist_select_item(&list, item * 2);
760 buffer_bookmarks(bookmarks, bookmarks->start);
761 gui_synclist_draw(&list);
762 cond_talk_ids_fq(VOICE_EXT_BMARK);
763 gui_synclist_speak_item(&list);
764 refresh = false;
767 list_do_action(CONTEXT_BOOKMARKSCREEN, HZ / 2,
768 &list, &action, LIST_WRAP_UNLESS_HELD);
769 item = gui_synclist_get_sel_pos(&list) / 2;
771 if (bookmarks->show_dont_resume)
773 item--;
776 if (action == ACTION_STD_CONTEXT)
778 MENUITEM_STRINGLIST(menu_items, ID2P(LANG_BOOKMARK_CONTEXT_MENU),
779 NULL, ID2P(LANG_BOOKMARK_CONTEXT_RESUME),
780 ID2P(LANG_BOOKMARK_CONTEXT_DELETE));
781 static const int menu_actions[] =
783 ACTION_STD_OK, ACTION_BMS_DELETE
785 int selection = do_menu(&menu_items, NULL, NULL, false);
787 refresh = true;
789 if (selection >= 0 && selection <=
790 (int) (sizeof(menu_actions) / sizeof(menu_actions[0])))
792 action = menu_actions[selection];
796 switch (action)
798 case ACTION_STD_OK:
799 if (item >= 0)
801 talk_shutup();
802 *selected_bookmark = bookmarks->items[item - bookmarks->start];
803 return BOOKMARK_SUCCESS;
806 /* Else fall through */
808 case ACTION_TREE_WPS:
809 case ACTION_STD_CANCEL:
810 exit = true;
811 break;
813 case ACTION_BMS_DELETE:
814 if (item >= 0)
816 const char *lines[]={
817 ID2P(LANG_REALLY_DELETE)
819 const char *yes_lines[]={
820 ID2P(LANG_DELETING)
823 const struct text_message message={lines, 1};
824 const struct text_message yes_message={yes_lines, 1};
826 if(gui_syncyesno_run(&message, &yes_message, NULL)==YESNO_YES)
828 delete_bookmark(bookmark_file_name, item);
829 bookmarks->reload = true;
831 refresh = true;
833 break;
835 default:
836 if (default_event_handler(action) == SYS_USB_CONNECTED)
838 ret = BOOKMARK_USB_CONNECTED;
839 exit = true;
842 break;
846 talk_shutup();
847 *selected_bookmark = NULL;
848 return ret;
851 /* ----------------------------------------------------------------------- */
852 /* This function takes a location in a bookmark file and deletes that */
853 /* bookmark. */
854 /* Returns true on successful bookmark deletion. */
855 /* ------------------------------------------------------------------------*/
856 static bool delete_bookmark(const char* bookmark_file_name, int bookmark_id)
858 int temp_bookmark_file = 0;
859 int bookmark_file = 0;
860 int bookmark_count = 0;
862 /* Opening up a temp bookmark file */
863 snprintf(global_temp_buffer, sizeof(global_temp_buffer),
864 "%s.tmp", bookmark_file_name);
865 temp_bookmark_file = open(global_temp_buffer,
866 O_WRONLY | O_CREAT | O_TRUNC, 0666);
868 if (temp_bookmark_file < 0)
869 return false; /* can't open the temp file */
871 /* Reading in the previous bookmarks and writing them to the temp file */
872 bookmark_file = open(bookmark_file_name, O_RDONLY);
873 if (bookmark_file >= 0)
875 while (read_line(bookmark_file, global_read_buffer,
876 sizeof(global_read_buffer)) > 0)
878 if (bookmark_id != bookmark_count)
880 write(temp_bookmark_file, global_read_buffer,
881 strlen(global_read_buffer));
882 write(temp_bookmark_file, "\n", 1);
884 bookmark_count++;
886 close(bookmark_file);
888 close(temp_bookmark_file);
890 remove(bookmark_file_name);
891 rename(global_temp_buffer, bookmark_file_name);
893 return true;
896 /* ----------------------------------------------------------------------- */
897 /* This function parses a bookmark, says the voice UI part of it. */
898 /* ------------------------------------------------------------------------*/
899 static void say_bookmark(const char* bookmark,
900 int bookmark_id, bool show_playlist_name)
902 if (!parse_bookmark(bookmark, true))
904 talk_id(LANG_BOOKMARK_INVALID, false);
905 return;
908 talk_number(bookmark_id + 1, false);
910 #if CONFIG_CODEC == SWCODEC
911 bool is_dir = (global_temp_buffer[0]
912 && global_temp_buffer[strlen(global_temp_buffer)-1] == '/');
914 /* HWCODEC cannot enqueue voice file entries and .talk thumbnails
915 together, because there is no guarantee that the same mp3
916 parameters are used. */
917 if(show_playlist_name)
918 { /* It's useful to know which playlist this is */
919 if(is_dir)
920 talk_dir_or_spell(global_temp_buffer,
921 TALK_IDARRAY(VOICE_DIR), true);
922 else talk_file_or_spell(NULL, global_temp_buffer,
923 TALK_IDARRAY(LANG_PLAYLIST), true);
925 #else
926 (void)show_playlist_name;
927 #endif
929 if(bm.shuffle)
930 talk_id(LANG_SHUFFLE, true);
932 talk_id(VOICE_BOOKMARK_SELECT_INDEX_TEXT, true);
933 talk_number(bm.resume_index + 1, true);
934 talk_id(LANG_TIME, true);
935 talk_value(bm.resume_time / 1000, UNIT_TIME, true);
937 #if CONFIG_CODEC == SWCODEC
938 /* Track filename */
939 if(is_dir)
940 talk_file_or_spell(global_temp_buffer, global_filename,
941 TALK_IDARRAY(VOICE_FILE), true);
942 else
943 { /* Unfortunately if this is a playlist, we do not know in which
944 directory the file is and therefore cannot find the track's
945 .talk file. */
946 talk_id(VOICE_FILE, true);
947 talk_spell(global_filename, true);
949 #endif
952 /* ----------------------------------------------------------------------- */
953 /* This function parses a bookmark and then plays it. */
954 /* Returns true on successful bookmark play. */
955 /* ------------------------------------------------------------------------*/
956 static bool play_bookmark(const char* bookmark)
958 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
959 /* preset pitch and speed to 100% in case bookmark doesn't have info */
960 bm.pitch = sound_get_pitch();
961 bm.speed = dsp_get_timestretch();
962 #endif
964 if (parse_bookmark(bookmark, true))
966 global_settings.repeat_mode = bm.repeat_mode;
967 global_settings.playlist_shuffle = bm.shuffle;
968 #if CONFIG_CODEC == SWCODEC && defined(HAVE_PITCHSCREEN)
969 sound_set_pitch(bm.pitch);
970 dsp_set_timestretch(bm.speed);
971 #endif
972 if (!warn_on_pl_erase())
973 return false;
974 return bookmark_play(global_temp_buffer, bm.resume_index,
975 bm.resume_offset, bm.resume_seed, global_filename);
978 return false;
981 static const char* skip_token(const char* s)
983 while (*s && *s != ';')
985 s++;
988 if (*s)
990 s++;
993 return s;
996 static const char* int_token(const char* s, int* dest)
998 *dest = atoi(s);
999 return skip_token(s);
1002 static const char* long_token(const char* s, long* dest)
1004 *dest = atoi(s); /* Should be atol, but we don't have it. */
1005 return skip_token(s);
1008 /* ----------------------------------------------------------------------- */
1009 /* This function takes a bookmark and parses it. This function also */
1010 /* validates the bookmark. The parse_filenames flag indicates whether */
1011 /* the filename tokens are to be extracted. */
1012 /* Returns true on successful bookmark parse. */
1013 /* ----------------------------------------------------------------------- */
1014 static bool parse_bookmark(const char *bookmark, const bool parse_filenames)
1016 const char* s = bookmark;
1017 const char* end;
1019 #define GET_INT_TOKEN(var) s = int_token(s, &var)
1020 #define GET_LONG_TOKEN(var) s = long_token(s, &var)
1021 #define GET_BOOL_TOKEN(var) var = (atoi(s)!=0); s = skip_token(s)
1023 /* if new format bookmark, extract the optional content flags,
1024 otherwise treat as an original format bookmark */
1025 int opt_flags = 0;
1026 bool new_format = (strchr(s, '>') == s);
1027 if (new_format)
1029 s++;
1030 GET_INT_TOKEN(opt_flags);
1033 /* extract all original bookmark tokens */
1034 GET_INT_TOKEN(bm.resume_index);
1035 GET_LONG_TOKEN(bm.resume_offset);
1036 GET_INT_TOKEN(bm.resume_seed);
1037 if (!new_format) /* skip deprecated token */
1038 s = skip_token(s);
1039 GET_LONG_TOKEN(bm.resume_time);
1040 GET_INT_TOKEN(bm.repeat_mode);
1041 GET_BOOL_TOKEN(bm.shuffle);
1043 /* extract all optional bookmark tokens */
1044 if (opt_flags & BM_PITCH)
1045 GET_INT_TOKEN(bm.pitch);
1046 if (opt_flags & BM_SPEED)
1047 GET_INT_TOKEN(bm.speed);
1049 if (*s == 0)
1051 return false;
1054 end = strchr(s, ';');
1056 /* extract file names */
1057 if (parse_filenames)
1059 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1060 len = MIN(TEMP_BUF_SIZE - 1, len);
1061 strlcpy(global_temp_buffer, s, len + 1);
1063 if (end != NULL)
1065 end++;
1066 strlcpy(global_filename, end, MAX_PATH);
1070 return true;
1073 /* ----------------------------------------------------------------------- */
1074 /* This function is used by multiple functions and is used to generate a */
1075 /* bookmark named based off of the input. */
1076 /* Changing this function could result in how the bookmarks are stored. */
1077 /* it would be here that the centralized/decentralized bookmark code */
1078 /* could be placed. */
1079 /* Always returns true */
1080 /* ----------------------------------------------------------------------- */
1081 static bool generate_bookmark_file_name(const char *in)
1083 int len = strlen(in);
1085 /* if this is a root dir MP3, rename the bookmark file root_dir.bmark */
1086 /* otherwise, name it based on the in variable */
1087 if (!strcmp("/", in))
1088 strcpy(global_bookmark_file_name, "/root_dir.bmark");
1089 else
1091 #ifdef HAVE_MULTIVOLUME
1092 /* The "root" of an extra volume need special handling too. */
1093 bool volume_root = (strip_volume(in, global_bookmark_file_name) &&
1094 !strcmp("/", global_bookmark_file_name));
1095 #endif
1097 strcpy(global_bookmark_file_name, in);
1098 if(global_bookmark_file_name[len-1] == '/')
1099 len--;
1100 #ifdef HAVE_MULTIVOLUME
1101 if (volume_root)
1102 strcpy(&global_bookmark_file_name[len], "/volume_dir.bmark");
1103 else
1104 #endif
1105 strcpy(&global_bookmark_file_name[len], ".bmark");
1108 return true;
1111 /* ----------------------------------------------------------------------- */
1112 /* Returns true if a bookmark file exists for the current playlist. */
1113 /* This is an interface function. */
1114 /* ----------------------------------------------------------------------- */
1115 bool bookmark_exists(void)
1117 bool exist=false;
1119 char* name = playlist_get_name(NULL, global_temp_buffer,
1120 sizeof(global_temp_buffer));
1121 if (generate_bookmark_file_name(name))
1123 exist = file_exists(global_bookmark_file_name);
1125 return exist;
1128 /* ----------------------------------------------------------------------- */
1129 /* Checks the current state of the system and returns true if it is in a */
1130 /* bookmarkable state. */
1131 /* This is an interface funtion. */
1132 /* ----------------------------------------------------------------------- */
1133 bool bookmark_is_bookmarkable_state(void)
1135 int resume_index = 0;
1137 if (!(audio_status() && audio_current_track()) ||
1138 /* no track playing */
1139 (playlist_get_resume_info(&resume_index) == -1) ||
1140 /* invalid queue info */
1141 (playlist_modified(NULL)))
1142 /* can't bookmark while in the queue */
1144 return false;
1147 return true;