Add a slot to set the progress bar value and a member to hide it.
[Rockbox.git] / apps / onplay.c
blob7ab459192ef11869d7037666f3b2046686b391d4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
25 #include "debug.h"
26 #include "sprintf.h"
27 #include "lcd.h"
28 #include "dir.h"
29 #include "file.h"
30 #include "audio.h"
31 #include "menu.h"
32 #include "lang.h"
33 #include "playlist.h"
34 #include "button.h"
35 #include "kernel.h"
36 #include "keyboard.h"
37 #include "mp3data.h"
38 #include "id3.h"
39 #include "screens.h"
40 #include "tree.h"
41 #include "buffer.h"
42 #include "settings.h"
43 #include "statusbar.h"
44 #include "playlist_viewer.h"
45 #include "talk.h"
46 #include "onplay.h"
47 #include "filetypes.h"
48 #include "plugin.h"
49 #include "bookmark.h"
50 #include "action.h"
51 #include "splash.h"
52 #include "yesno.h"
53 #include "menus/exported_menus.h"
54 #ifdef HAVE_LCD_BITMAP
55 #include "icons.h"
56 #endif
57 #include "sound_menu.h"
58 #include "playlist_menu.h"
59 #include "playlist_catalog.h"
60 #ifdef HAVE_TAGCACHE
61 #include "tagtree.h"
62 #endif
63 #include "cuesheet.h"
64 #include "backdrop.h"
66 static int context;
67 static char* selected_file = NULL;
68 static int selected_file_attr = 0;
69 static int onplay_result = ONPLAY_OK;
70 static char clipboard_selection[MAX_PATH];
71 static int clipboard_selection_attr = 0;
72 static bool clipboard_is_copy = false;
74 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
75 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
76 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
77 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
78 static const struct menu_item_ex name = \
79 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
80 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
81 { (void*)name##_},{.callback_and_desc = & name##__}};
83 #ifdef HAVE_LCD_BITMAP
84 static void draw_slider(void);
85 #else
86 #define draw_slider()
87 #endif
88 /* ----------------------------------------------------------------------- */
89 /* Displays the bookmark menu options for the user to decide. This is an */
90 /* interface function. */
91 /* ----------------------------------------------------------------------- */
93 static int bookmark_menu_callback(int action,
94 const struct menu_item_ex *this_item);
95 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
96 ID2P(LANG_BOOKMARK_MENU_CREATE),
97 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
98 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
99 ID2P(LANG_BOOKMARK_MENU_LIST),
100 bookmark_load_menu, NULL,
101 bookmark_menu_callback, Icon_Bookmark);
102 MAKE_MENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
103 Icon_Bookmark, &bookmark_create_menu_item, &bookmark_load_menu_item);
104 static int bookmark_menu_callback(int action,
105 const struct menu_item_ex *this_item)
107 (void)this_item;
108 switch (action)
110 case ACTION_REQUEST_MENUITEM:
111 if (this_item == &bookmark_load_menu_item)
113 if (bookmark_exist() == 0)
114 return ACTION_EXIT_MENUITEM;
116 /* hide the bookmark menu if there is no playback */
117 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
118 return ACTION_EXIT_MENUITEM;
119 break;
120 #ifdef HAVE_LCD_CHARCELLS
121 case ACTION_ENTER_MENUITEM:
122 status_set_param(true);
123 break;
124 #endif
125 case ACTION_EXIT_MENUITEM:
126 #ifdef HAVE_LCD_CHARCELLS
127 status_set_param(false);
128 #endif
129 settings_save();
130 break;
132 return action;
135 static bool list_viewers(void)
137 int ret = filetype_list_viewers(selected_file);
138 if (ret == PLUGIN_USB_CONNECTED)
139 onplay_result = ONPLAY_RELOAD_DIR;
140 return false;
143 static bool shuffle_playlist(void)
145 playlist_sort(NULL, true);
146 playlist_randomise(NULL, current_tick, true);
148 return false;
151 static bool save_playlist(void)
153 save_playlist_screen(NULL);
154 return false;
157 static bool add_to_playlist(int position, bool queue)
159 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
160 const char *lines[] = {
161 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
162 selected_file
164 const struct text_message message={lines, 2};
166 gui_syncsplash(0, ID2P(LANG_WAIT));
168 if (new_playlist)
169 playlist_create(NULL, NULL);
171 /* always set seed before inserting shuffled */
172 if (position == PLAYLIST_INSERT_SHUFFLED)
173 srand(current_tick);
175 #ifdef HAVE_TAGCACHE
176 if (context == CONTEXT_ID3DB)
178 tagtree_insert_selection_playlist(position, queue);
180 else
181 #endif
183 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
184 playlist_insert_track(NULL, selected_file, position, queue, true);
185 else if (selected_file_attr & ATTR_DIRECTORY)
187 bool recurse = false;
189 if (global_settings.recursive_dir_insert != RECURSE_ASK)
190 recurse = (bool)global_settings.recursive_dir_insert;
191 else
193 /* Ask if user wants to recurse directory */
194 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
197 playlist_insert_directory(NULL, selected_file, position, queue,
198 recurse);
200 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
201 playlist_insert_playlist(NULL, selected_file, position, queue);
204 if (new_playlist && (playlist_amount() > 0))
206 /* nothing is currently playing so begin playing what we just
207 inserted */
208 if (global_settings.playlist_shuffle)
209 playlist_shuffle(current_tick, -1);
210 playlist_start(0,0);
211 gui_syncstatusbar_draw(&statusbars, false);
212 onplay_result = ONPLAY_START_PLAY;
215 return false;
218 static bool view_playlist(void)
220 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
221 bool result;
223 result = playlist_viewer_ex(selected_file);
225 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
226 onplay_result == ONPLAY_OK)
227 /* playlist was started from viewer */
228 onplay_result = ONPLAY_START_PLAY;
230 return result;
233 static bool cat_add_to_a_playlist(void)
235 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
236 false, NULL);
239 static bool cat_add_to_a_new_playlist(void)
241 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
242 true, NULL);
246 static int cat_playlist_callback(int action,
247 const struct menu_item_ex *this_item);
248 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
249 catalog_view_playlists, 0, cat_playlist_callback,
250 Icon_Playlist);
251 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
252 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
253 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
254 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
255 MAKE_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
256 Icon_Playlist, &cat_view_lists,
257 &cat_add_to_list, &cat_add_to_new );
259 static int cat_playlist_callback(int action,
260 const struct menu_item_ex *this_item)
262 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
263 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
264 ((selected_file_attr & ATTR_DIRECTORY) == 0))
266 return ACTION_EXIT_MENUITEM;
269 switch (action)
271 case ACTION_REQUEST_MENUITEM:
272 if (this_item == &cat_view_lists)
274 if (context == CONTEXT_WPS)
275 return action;
277 else if (selected_file && /* set before calling this menu,
278 so safe */
279 ((audio_status() & AUDIO_STATUS_PLAY &&
280 context == CONTEXT_WPS) ||
281 context == CONTEXT_TREE))
283 return action;
285 else
286 return ACTION_EXIT_MENUITEM;
287 break;
289 return action;
293 /* CONTEXT_WPS playlist options */
294 MENUITEM_FUNCTION(playlist_viewer_item, 0,
295 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
296 NULL, NULL, Icon_Playlist);
297 MENUITEM_FUNCTION(search_playlist_item, 0,
298 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
299 NULL, NULL, Icon_Playlist);
300 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
301 save_playlist, NULL, NULL, Icon_Playlist);
302 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
303 shuffle_playlist, NULL, NULL, Icon_Playlist);
304 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
305 NULL, Icon_Playlist,
306 &playlist_viewer_item, &search_playlist_item,
307 &playlist_save_item, &reshuffle_item
310 /* CONTEXT_[TREE|ID3DB] playlist options */
311 static int playlist_insert_func(void *param)
313 add_to_playlist((intptr_t)param, false);
314 return 0;
316 static int playlist_queue_func(void *param)
318 add_to_playlist((intptr_t)param, true);
319 return 0;
321 static int treeplaylist_wplayback_callback(int action,
322 const struct menu_item_ex*
323 this_item)
325 (void)this_item;
326 switch (action)
328 case ACTION_REQUEST_MENUITEM:
329 if (audio_status() & AUDIO_STATUS_PLAY)
330 return action;
331 else
332 return ACTION_EXIT_MENUITEM;
333 break;
335 return action;
338 static int treeplaylist_callback(int action,
339 const struct menu_item_ex *this_item);
341 /* insert items */
342 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
343 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
344 treeplaylist_callback, Icon_Playlist);
345 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
346 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
347 treeplaylist_wplayback_callback, Icon_Playlist);
348 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
349 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
350 treeplaylist_wplayback_callback, Icon_Playlist);
351 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
352 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
353 treeplaylist_wplayback_callback, Icon_Playlist);
354 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
355 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
356 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
357 Icon_Playlist);
358 /* queue items */
359 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
360 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
361 treeplaylist_wplayback_callback, Icon_Playlist);
362 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
363 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
364 treeplaylist_wplayback_callback, Icon_Playlist);
365 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
366 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
367 treeplaylist_wplayback_callback, Icon_Playlist);
368 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
369 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
370 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
371 treeplaylist_wplayback_callback, Icon_Playlist);
372 /* replace playlist */
373 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
374 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
375 treeplaylist_wplayback_callback, Icon_Playlist);
376 /* others */
378 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
379 view_playlist, NULL,
380 treeplaylist_callback, Icon_Playlist);
382 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
383 treeplaylist_callback, Icon_Playlist,
385 /* view */
386 &view_playlist_item,
388 /* insert */
389 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
390 &i_last_pl_item, &i_shuf_pl_item,
392 /* queue */
393 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
394 &q_shuf_pl_item,
396 /* replace */
397 &replace_pl_item
399 static int treeplaylist_callback(int action,
400 const struct menu_item_ex *this_item)
402 (void)this_item;
403 switch (action)
405 case ACTION_REQUEST_MENUITEM:
406 if (this_item == &tree_playlist_menu)
408 if (((selected_file_attr & FILE_ATTR_MASK) ==
409 FILE_ATTR_AUDIO) ||
410 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
411 (selected_file_attr & ATTR_DIRECTORY))
413 return action;
415 else
416 return ACTION_EXIT_MENUITEM;
418 else if (this_item == &view_playlist_item)
420 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
421 context == CONTEXT_TREE)
422 return action;
423 else
424 return ACTION_EXIT_MENUITEM;
426 else if (this_item == &i_pl_item_no_play)
428 if (!(audio_status() & AUDIO_STATUS_PLAY))
430 return action;
432 else
433 return ACTION_EXIT_MENUITEM;
435 else if (this_item == &i_shuf_pl_item)
438 if (audio_status() & AUDIO_STATUS_PLAY)
440 return action;
442 else if ((this_item == &i_shuf_pl_item) &&
443 ((selected_file_attr & ATTR_DIRECTORY) ||
444 ((selected_file_attr & FILE_ATTR_MASK) ==
445 FILE_ATTR_M3U)))
447 return action;
449 return ACTION_EXIT_MENUITEM;
451 break;
453 return action;
456 /* helper function to remove a non-empty directory */
457 static int remove_dir(char* dirname, int len)
459 int result = 0;
460 DIR* dir;
461 int dirlen = strlen(dirname);
462 int i;
464 dir = opendir(dirname);
465 if (!dir)
466 return -1; /* open error */
468 while(true)
470 struct dirent* entry;
471 /* walk through the directory content */
472 entry = readdir(dir);
473 if (!entry)
474 break;
476 dirname[dirlen] ='\0';
477 FOR_NB_SCREENS(i){
478 screens[i].puts(0,1,dirname);
479 screens[i].update();
482 /* append name to current directory */
483 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
484 if (entry->attribute & ATTR_DIRECTORY)
485 { /* remove a subdirectory */
486 if (!strcmp((char *)entry->d_name, ".") ||
487 !strcmp((char *)entry->d_name, ".."))
488 continue; /* skip these */
490 /* inform the user which dir we're deleting */
492 result = remove_dir(dirname, len); /* recursion */
493 if (result)
494 break; /* or better continue, delete what we can? */
496 else
497 { /* remove a file */
498 draw_slider();
499 result = remove(dirname);
501 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
503 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
504 result = -1;
505 break;
508 closedir(dir);
510 if (!result)
511 { /* remove the now empty directory */
512 dirname[dirlen] = '\0'; /* terminate to original length */
514 result = rmdir(dirname);
517 return result;
521 /* share code for file and directory deletion, saves space */
522 static bool delete_handler(bool is_dir)
524 char file_to_delete[MAX_PATH];
525 strcpy(file_to_delete, selected_file);
527 const char *lines[]={
528 ID2P(LANG_REALLY_DELETE),
529 file_to_delete
531 const char *yes_lines[]={
532 ID2P(LANG_DELETED),
533 file_to_delete
536 struct text_message message={lines, 2};
537 struct text_message yes_message={yes_lines, 2};
539 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
540 return false;
542 gui_syncsplash(0, str(LANG_DELETING));
544 int res;
545 if (is_dir)
547 char pathname[MAX_PATH]; /* space to go deep */
548 cpu_boost(true);
549 strncpy(pathname, file_to_delete, sizeof pathname);
550 res = remove_dir(pathname, sizeof(pathname));
551 cpu_boost(false);
553 else
554 res = remove(file_to_delete);
556 if (!res)
557 onplay_result = ONPLAY_RELOAD_DIR;
559 return false;
563 static bool delete_file(void)
565 return delete_handler(false);
568 static bool delete_dir(void)
570 return delete_handler(true);
573 #if LCD_DEPTH > 1
574 static bool set_backdrop(void)
576 /* load the image */
577 if(load_main_backdrop(selected_file)) {
578 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
579 set_file(selected_file, (char *)global_settings.backdrop_file,
580 MAX_FILENAME);
581 show_main_backdrop();
582 return true;
583 } else {
584 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
585 return false;
588 #endif
590 static bool rename_file(void)
592 char newname[MAX_PATH];
593 char* ptr = strrchr(selected_file, '/') + 1;
594 int pathlen = (ptr - selected_file);
595 strncpy(newname, selected_file, sizeof newname);
596 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
597 if (!strlen(newname + pathlen) ||
598 (rename(selected_file, newname) < 0)) {
599 lcd_clear_display();
600 lcd_puts(0,0,str(LANG_RENAME));
601 lcd_puts(0,1,str(LANG_FAILED));
602 lcd_update();
603 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
604 sleep(HZ*2);
606 else
607 onplay_result = ONPLAY_RELOAD_DIR;
610 return false;
613 static bool create_dir(void)
615 char dirname[MAX_PATH];
616 char *cwd;
617 int rc;
618 int pathlen;
620 cwd = getcwd(NULL, 0);
621 memset(dirname, 0, sizeof dirname);
623 snprintf(dirname, sizeof dirname, "%s/",
624 cwd[1] ? cwd : "");
626 pathlen = strlen(dirname);
627 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
628 if (rc < 0)
629 return false;
631 rc = mkdir(dirname);
632 if (rc < 0) {
633 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
634 gui_syncsplash(HZ, (unsigned char *)"%s %s",
635 str(LANG_CREATE_DIR), str(LANG_FAILED));
636 } else {
637 onplay_result = ONPLAY_RELOAD_DIR;
640 return true;
643 static bool properties(void)
645 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
646 selected_file))
647 onplay_result = ONPLAY_RELOAD_DIR;
648 return false;
651 /* Store the current selection in the clipboard */
652 static bool clipboard_clip(bool copy)
654 clipboard_selection[0] = 0;
655 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
656 clipboard_selection_attr = selected_file_attr;
657 clipboard_is_copy = copy;
659 return true;
662 static bool clipboard_cut(void)
664 return clipboard_clip(false);
667 static bool clipboard_copy(void)
669 return clipboard_clip(true);
672 #ifdef HAVE_LCD_BITMAP
673 static void draw_slider(void)
675 int i;
676 FOR_NB_SCREENS(i)
678 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
679 LCD_WIDTH-2, 2*screens[i].char_height-1);
680 screens[i].update();
683 #endif
685 /* Paste a file to a new directory. Will overwrite always. */
686 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
688 int src_fd, target_fd;
689 size_t buffersize;
690 ssize_t size, bytesread, byteswritten;
691 char *buffer;
692 bool result = false;
694 if (copy) {
695 /* See if we can get the plugin buffer for the file copy buffer */
696 buffer = (char *) plugin_get_buffer(&buffersize);
697 if (buffer == NULL || buffersize < 512) {
698 /* Not large enough, try for a disk sector worth of stack
699 instead */
700 buffersize = 512;
701 buffer = (char *) __builtin_alloca(buffersize);
704 if (buffer == NULL) {
705 return false;
708 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
709 size */
711 src_fd = open(src, O_RDONLY);
713 if (src_fd >= 0) {
714 target_fd = creat(target);
716 if (target_fd >= 0) {
717 result = true;
719 size = filesize(src_fd);
721 if (size == -1) {
722 result = false;
725 while(size > 0) {
726 bytesread = read(src_fd, buffer, buffersize);
728 if (bytesread == -1) {
729 result = false;
730 break;
733 size -= bytesread;
735 while(bytesread > 0) {
736 byteswritten = write(target_fd, buffer, bytesread);
738 if (byteswritten == -1) {
739 result = false;
740 size = 0;
741 break;
744 bytesread -= byteswritten;
745 draw_slider();
749 close(target_fd);
751 /* Copy failed. Cleanup. */
752 if (!result) {
753 remove(target);
757 close(src_fd);
759 } else {
760 result = rename(src, target) == 0;
761 #ifdef HAVE_MULTIVOLUME
762 if (!result) {
763 if (errno == EXDEV) {
764 /* Failed because cross volume rename doesn't work. Copy
765 instead */
766 result = clipboard_pastefile(src, target, true);
768 if (result) {
769 result = remove(src) == 0;
773 #endif
776 return result;
779 /* Paste a directory to a new location. Designed to be called by
780 clipboard_paste */
781 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
782 int targetlen, bool copy)
784 DIR *srcdir;
785 int srcdirlen = strlen(src);
786 int targetdirlen = strlen(target);
787 bool result = true;
789 if (!file_exists(target)) {
790 if (!copy) {
791 /* Just move the directory */
792 result = rename(src, target) == 0;
794 #ifdef HAVE_MULTIVOLUME
795 if (!result && errno == EXDEV) {
796 /* Try a copy as we're going across devices */
797 result = clipboard_pastedirectory(src, srclen, target,
798 targetlen, true);
800 /* If it worked, remove the source directory */
801 if (result) {
802 remove_dir(src, srclen);
805 #endif
806 return result;
807 } else {
808 /* Make a directory to copy things to */
809 result = mkdir(target) == 0;
813 /* Check if something went wrong already */
814 if (!result) {
815 return result;
818 srcdir = opendir(src);
819 if (!srcdir) {
820 return false;
823 /* This loop will exit as soon as there's a problem */
824 while(result)
826 struct dirent* entry;
827 /* walk through the directory content */
828 entry = readdir(srcdir);
829 if (!entry)
830 break;
832 /* append name to current directory */
833 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
834 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
835 entry->d_name);
837 DEBUGF("Copy %s to %s\n", src, target);
839 if (entry->attribute & ATTR_DIRECTORY)
840 { /* copy/move a subdirectory */
841 if (!strcmp((char *)entry->d_name, ".") ||
842 !strcmp((char *)entry->d_name, ".."))
843 continue; /* skip these */
845 result = clipboard_pastedirectory(src, srclen, target, targetlen,
846 copy); /* recursion */
848 else
849 { /* copy/move a file */
850 result = clipboard_pastefile(src, target, copy);
854 closedir(srcdir);
856 if (result) {
857 src[srcdirlen] = '\0'; /* terminate to original length */
858 target[targetdirlen] = '\0'; /* terminate to original length */
861 return result;
864 /* Paste the clipboard to the current directory */
865 static bool clipboard_paste(void)
867 char target[MAX_PATH];
868 char *cwd, *nameptr;
869 bool success;
871 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
872 static const struct text_message message={lines, 1};
874 /* Get the name of the current directory */
875 cwd = getcwd(NULL, 0);
877 /* Figure out the name of the selection */
878 nameptr = strrchr(clipboard_selection, '/');
880 /* Final target is current directory plus name of selection */
881 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
883 /* If the target existed but they choose not to overwite, exit */
884 if (file_exists(target) &&
885 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
886 return false;
889 if (clipboard_is_copy) {
890 gui_syncsplash(0, ID2P(LANG_COPYING));
892 else
894 gui_syncsplash(0, ID2P(LANG_MOVING));
897 /* Now figure out what we're doing */
898 cpu_boost(true);
899 if (clipboard_selection_attr & ATTR_DIRECTORY) {
900 /* Recursion. Set up external stack */
901 char srcpath[MAX_PATH];
902 char targetpath[MAX_PATH];
903 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
905 /* Do not allow the user to paste a directory into a dir they are
906 copying */
907 success = 0;
909 else
911 strncpy(srcpath, clipboard_selection, sizeof srcpath);
912 strncpy(targetpath, target, sizeof targetpath);
914 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
915 target, sizeof(targetpath), clipboard_is_copy);
917 } else {
918 success = clipboard_pastefile(clipboard_selection, target,
919 clipboard_is_copy);
921 cpu_boost(false);
923 /* Did it work? */
924 if (success) {
925 /* Reset everything */
926 clipboard_selection[0] = 0;
927 clipboard_selection_attr = 0;
928 clipboard_is_copy = false;
930 /* Force reload of the current directory */
931 onplay_result = ONPLAY_RELOAD_DIR;
932 } else {
933 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
934 gui_syncsplash(HZ, (unsigned char *)"%s %s",
935 str(LANG_PASTE), str(LANG_FAILED));
938 return true;
941 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
942 #ifdef HAVE_TAGCACHE
943 static int set_rating_inline(void)
945 struct mp3entry* id3 = audio_current_track();
946 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
948 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
949 NULL, 1, 0, 10, NULL, NULL);
950 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
952 else
953 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
954 return 0;
956 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
958 (void)this_item;
959 switch (action)
961 case ACTION_REQUEST_MENUITEM:
962 if (!selected_file || !global_settings.runtimedb)
963 return ACTION_EXIT_MENUITEM;
964 break;
966 return action;
968 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
969 set_rating_inline, NULL,
970 ratingitem_callback, Icon_Questionmark);
971 #endif
973 static bool view_cue(void)
975 struct mp3entry* id3 = audio_current_track();
976 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
978 browse_cuesheet(curr_cue);
980 return false;
982 static int view_cue_item_callback(int action,
983 const struct menu_item_ex *this_item)
985 (void)this_item;
986 struct mp3entry* id3 = audio_current_track();
987 switch (action)
989 case ACTION_REQUEST_MENUITEM:
990 if (!selected_file || !cuesheet_is_enabled()
991 || !id3 || !id3->cuesheet_type)
992 return ACTION_EXIT_MENUITEM;
993 break;
995 return action;
997 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
998 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1000 /* CONTEXT_WPS items */
1001 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1002 browse_id3, NULL, NULL, Icon_NOICON);
1003 #ifdef HAVE_PITCHSCREEN
1004 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1005 pitch_screen, NULL, NULL, Icon_Audio);
1006 #endif
1008 /* CONTEXT_[TREE|ID3DB] items */
1009 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1010 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1011 rename_file, NULL, clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1013 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1014 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1015 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1016 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1017 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1018 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1019 delete_file, NULL, clipboard_callback, Icon_NOICON);
1020 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1021 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1022 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1023 properties, NULL, clipboard_callback, Icon_NOICON);
1024 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1025 create_dir, NULL, clipboard_callback, Icon_NOICON);
1026 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1027 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1028 #if LCD_DEPTH > 1
1029 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1030 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1031 #endif
1032 #ifdef HAVE_RECORDING
1033 static bool set_recdir(void)
1035 strncpy(global_settings.rec_directory,
1036 selected_file, MAX_FILENAME+1);
1037 settings_save();
1038 return false;
1040 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1041 set_recdir, NULL, clipboard_callback, Icon_Recording);
1042 #endif
1043 static bool add_to_faves(void)
1045 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1046 selected_file))
1047 onplay_result = ONPLAY_RELOAD_DIR;
1048 return false;
1050 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1051 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1054 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1056 switch (action)
1058 case ACTION_REQUEST_MENUITEM:
1059 #ifdef HAVE_MULTIVOLUME
1060 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1061 (this_item == &rename_file_item ||
1062 this_item == &delete_dir_item ||
1063 this_item == &clipboard_cut_item) )
1064 return ACTION_EXIT_MENUITEM;
1065 #endif
1066 if (context == CONTEXT_ID3DB)
1067 return ACTION_EXIT_MENUITEM;
1068 if (this_item == &clipboard_paste_item)
1069 { /* visible if there is something to paste */
1070 return (clipboard_selection[0] != 0) ?
1071 action : ACTION_EXIT_MENUITEM;
1073 else if (this_item == &create_dir_item)
1075 /* always visible */
1076 return action;
1078 else if ((this_item == &properties_item) ||
1079 (this_item == &rename_file_item) ||
1080 (this_item == &clipboard_cut_item) ||
1081 (this_item == &clipboard_copy_item) ||
1082 (this_item == &add_to_faves_item)
1085 /* requires an actual file */
1086 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1088 #if LCD_DEPTH > 1
1089 else if (this_item == &set_backdrop_item)
1091 if (selected_file)
1093 char *suffix = strrchr(selected_file, '.');
1094 if (suffix)
1096 if (strcasecmp(suffix, ".bmp") == 0)
1098 return action;
1102 return ACTION_EXIT_MENUITEM;
1104 #endif
1105 else if ((selected_file_attr & ATTR_DIRECTORY))
1107 if ((this_item == &delete_dir_item)
1109 return action;
1110 #ifdef HAVE_RECORDING
1111 else if (this_item == &set_recdir_item)
1112 return action;
1113 #endif
1115 else if (selected_file
1116 #ifdef HAVE_MULTIVOLUME
1117 /* no rename+delete for volumes */
1118 && !(selected_file_attr & ATTR_VOLUME)
1119 #endif
1122 if ((this_item == &delete_file_item) ||
1123 (this_item == &list_viewers_item))
1125 return action;
1128 return ACTION_EXIT_MENUITEM;
1129 break;
1131 return action;
1133 /* used when onplay() is called in the CONTEXT_WPS context */
1136 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1137 onplaymenu_callback, Icon_Audio,
1138 &wps_playlist_menu, &cat_playlist_menu,
1139 &sound_settings, &playback_settings,
1140 #ifdef HAVE_TAGCACHE
1141 &rating_item,
1142 #endif
1143 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1144 &delete_file_item, &view_cue_item,
1145 #ifdef HAVE_PITCHSCREEN
1146 &pitch_screen_item,
1147 #endif
1149 /* used when onplay() is not called in the CONTEXT_WPS context */
1150 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1151 onplaymenu_callback, Icon_file_view_menu,
1152 &tree_playlist_menu, &cat_playlist_menu,
1153 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1154 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1155 #if LCD_DEPTH > 1
1156 &set_backdrop_item,
1157 #endif
1158 &list_viewers_item, &create_dir_item, &properties_item,
1159 #ifdef HAVE_RECORDING
1160 &set_recdir_item,
1161 #endif
1162 &add_to_faves_item,
1164 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1166 (void)this_item;
1167 switch (action)
1169 case ACTION_TREE_STOP:
1170 if (this_item == &wps_onplay_menu)
1172 list_stop_handler();
1173 return ACTION_STD_CANCEL;
1175 break;
1176 case ACTION_EXIT_MENUITEM:
1177 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1178 break;
1180 return action;
1182 int onplay(char* file, int attr, int from)
1184 const struct menu_item_ex *menu;
1185 onplay_result = ONPLAY_OK;
1186 context = from;
1187 selected_file = file;
1188 selected_file_attr = attr;
1189 if (context == CONTEXT_WPS)
1190 menu = &wps_onplay_menu;
1191 else
1192 menu = &tree_onplay_menu;
1193 switch (do_menu(menu, NULL, NULL, false))
1195 case GO_TO_WPS:
1196 return ONPLAY_START_PLAY;
1197 case GO_TO_ROOT:
1198 case GO_TO_MAINMENU:
1199 return ONPLAY_MAINMENU;
1200 default:
1201 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;