Explicitly set the dialog's result code for TTS / Encoder windows. Fixes an issue...
[Rockbox.git] / apps / onplay.c
blobddbe24d15269c129a905c16f5cbf9855ea6db58c
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_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
103 Icon_Bookmark, &bookmark_create_menu_item,
104 &bookmark_load_menu_item);
105 static int bookmark_menu_callback(int action,
106 const struct menu_item_ex *this_item)
108 (void)this_item;
109 switch (action)
111 case ACTION_REQUEST_MENUITEM:
112 if (this_item == &bookmark_load_menu_item)
114 if (bookmark_exist() == 0)
115 return ACTION_EXIT_MENUITEM;
117 /* hide the bookmark menu if there is no playback */
118 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
119 return ACTION_EXIT_MENUITEM;
120 break;
121 #ifdef HAVE_LCD_CHARCELLS
122 case ACTION_ENTER_MENUITEM:
123 status_set_param(true);
124 break;
125 #endif
126 case ACTION_EXIT_MENUITEM:
127 #ifdef HAVE_LCD_CHARCELLS
128 status_set_param(false);
129 #endif
130 settings_save();
131 break;
133 return action;
136 static bool list_viewers(void)
138 int ret = filetype_list_viewers(selected_file);
139 if (ret == PLUGIN_USB_CONNECTED)
140 onplay_result = ONPLAY_RELOAD_DIR;
141 return false;
144 static bool shuffle_playlist(void)
146 playlist_sort(NULL, true);
147 playlist_randomise(NULL, current_tick, true);
149 return false;
152 static bool save_playlist(void)
154 save_playlist_screen(NULL);
155 return false;
158 static bool add_to_playlist(int position, bool queue)
160 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
161 const char *lines[] = {
162 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
163 selected_file
165 const struct text_message message={lines, 2};
167 gui_syncsplash(0, ID2P(LANG_WAIT));
169 if (new_playlist)
170 playlist_create(NULL, NULL);
172 /* always set seed before inserting shuffled */
173 if (position == PLAYLIST_INSERT_SHUFFLED)
174 srand(current_tick);
176 #ifdef HAVE_TAGCACHE
177 if (context == CONTEXT_ID3DB)
179 tagtree_insert_selection_playlist(position, queue);
181 else
182 #endif
184 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
185 playlist_insert_track(NULL, selected_file, position, queue, true);
186 else if (selected_file_attr & ATTR_DIRECTORY)
188 bool recurse = false;
190 if (global_settings.recursive_dir_insert != RECURSE_ASK)
191 recurse = (bool)global_settings.recursive_dir_insert;
192 else
194 /* Ask if user wants to recurse directory */
195 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
198 playlist_insert_directory(NULL, selected_file, position, queue,
199 recurse);
201 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
202 playlist_insert_playlist(NULL, selected_file, position, queue);
205 if (new_playlist && (playlist_amount() > 0))
207 /* nothing is currently playing so begin playing what we just
208 inserted */
209 if (global_settings.playlist_shuffle)
210 playlist_shuffle(current_tick, -1);
211 playlist_start(0,0);
212 gui_syncstatusbar_draw(&statusbars, false);
213 onplay_result = ONPLAY_START_PLAY;
216 return false;
219 static bool view_playlist(void)
221 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
222 bool result;
224 result = playlist_viewer_ex(selected_file);
226 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
227 onplay_result == ONPLAY_OK)
228 /* playlist was started from viewer */
229 onplay_result = ONPLAY_START_PLAY;
231 return result;
234 static bool cat_add_to_a_playlist(void)
236 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
237 false, NULL);
240 static bool cat_add_to_a_new_playlist(void)
242 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
243 true, NULL);
247 static int cat_playlist_callback(int action,
248 const struct menu_item_ex *this_item);
249 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
250 catalog_view_playlists, 0, cat_playlist_callback,
251 Icon_Playlist);
252 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
253 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
254 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
255 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
256 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
257 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
258 &cat_add_to_new);
260 static int cat_playlist_callback(int action,
261 const struct menu_item_ex *this_item)
263 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
264 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
265 ((selected_file_attr & ATTR_DIRECTORY) == 0))
267 return ACTION_EXIT_MENUITEM;
270 switch (action)
272 case ACTION_REQUEST_MENUITEM:
273 if (this_item == &cat_view_lists)
275 if (context == CONTEXT_WPS)
276 return action;
278 else if (selected_file && /* set before calling this menu,
279 so safe */
280 ((audio_status() & AUDIO_STATUS_PLAY &&
281 context == CONTEXT_WPS) ||
282 context == CONTEXT_TREE))
284 return action;
286 else
287 return ACTION_EXIT_MENUITEM;
288 break;
290 return action;
294 /* CONTEXT_WPS playlist options */
295 MENUITEM_FUNCTION(playlist_viewer_item, 0,
296 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
297 NULL, NULL, Icon_Playlist);
298 MENUITEM_FUNCTION(search_playlist_item, 0,
299 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
300 NULL, NULL, Icon_Playlist);
301 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
302 save_playlist, NULL, NULL, Icon_Playlist);
303 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
304 shuffle_playlist, NULL, NULL, Icon_Playlist);
305 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
306 NULL, Icon_Playlist,
307 &playlist_viewer_item, &search_playlist_item,
308 &playlist_save_item, &reshuffle_item
311 /* CONTEXT_[TREE|ID3DB] playlist options */
312 static int playlist_insert_func(void *param)
314 add_to_playlist((intptr_t)param, false);
315 return 0;
317 static int playlist_queue_func(void *param)
319 add_to_playlist((intptr_t)param, true);
320 return 0;
322 static int treeplaylist_wplayback_callback(int action,
323 const struct menu_item_ex*
324 this_item)
326 (void)this_item;
327 switch (action)
329 case ACTION_REQUEST_MENUITEM:
330 if (audio_status() & AUDIO_STATUS_PLAY)
331 return action;
332 else
333 return ACTION_EXIT_MENUITEM;
334 break;
336 return action;
339 static int treeplaylist_callback(int action,
340 const struct menu_item_ex *this_item);
342 /* insert items */
343 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
344 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
345 treeplaylist_callback, Icon_Playlist);
346 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
347 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
348 treeplaylist_wplayback_callback, Icon_Playlist);
349 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
350 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
351 treeplaylist_wplayback_callback, Icon_Playlist);
352 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
353 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
354 treeplaylist_wplayback_callback, Icon_Playlist);
355 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
356 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
357 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
358 Icon_Playlist);
359 /* queue items */
360 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
361 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
362 treeplaylist_wplayback_callback, Icon_Playlist);
363 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
364 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
365 treeplaylist_wplayback_callback, Icon_Playlist);
366 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
367 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
368 treeplaylist_wplayback_callback, Icon_Playlist);
369 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
370 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
371 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
372 treeplaylist_wplayback_callback, Icon_Playlist);
373 /* replace playlist */
374 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
375 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
376 treeplaylist_wplayback_callback, Icon_Playlist);
377 /* others */
379 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
380 view_playlist, NULL,
381 treeplaylist_callback, Icon_Playlist);
383 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
384 treeplaylist_callback, Icon_Playlist,
386 /* view */
387 &view_playlist_item,
389 /* insert */
390 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
391 &i_last_pl_item, &i_shuf_pl_item,
393 /* queue */
394 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
395 &q_shuf_pl_item,
397 /* replace */
398 &replace_pl_item
400 static int treeplaylist_callback(int action,
401 const struct menu_item_ex *this_item)
403 (void)this_item;
404 switch (action)
406 case ACTION_REQUEST_MENUITEM:
407 if (this_item == &tree_playlist_menu)
409 if (((selected_file_attr & FILE_ATTR_MASK) ==
410 FILE_ATTR_AUDIO) ||
411 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
412 (selected_file_attr & ATTR_DIRECTORY))
414 return action;
416 else
417 return ACTION_EXIT_MENUITEM;
419 else if (this_item == &view_playlist_item)
421 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
422 context == CONTEXT_TREE)
423 return action;
424 else
425 return ACTION_EXIT_MENUITEM;
427 else if (this_item == &i_pl_item_no_play)
429 if (!(audio_status() & AUDIO_STATUS_PLAY))
431 return action;
433 else
434 return ACTION_EXIT_MENUITEM;
436 else if (this_item == &i_shuf_pl_item)
439 if (audio_status() & AUDIO_STATUS_PLAY)
441 return action;
443 else if ((this_item == &i_shuf_pl_item) &&
444 ((selected_file_attr & ATTR_DIRECTORY) ||
445 ((selected_file_attr & FILE_ATTR_MASK) ==
446 FILE_ATTR_M3U)))
448 return action;
450 return ACTION_EXIT_MENUITEM;
452 break;
454 return action;
457 /* helper function to remove a non-empty directory */
458 static int remove_dir(char* dirname, int len)
460 int result = 0;
461 DIR* dir;
462 int dirlen = strlen(dirname);
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 gui_syncsplash(0, dirname);
479 /* append name to current directory */
480 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
481 if (entry->attribute & ATTR_DIRECTORY)
482 { /* remove a subdirectory */
483 if (!strcmp((char *)entry->d_name, ".") ||
484 !strcmp((char *)entry->d_name, ".."))
485 continue; /* skip these */
487 /* inform the user which dir we're deleting */
489 result = remove_dir(dirname, len); /* recursion */
490 if (result)
491 break; /* or better continue, delete what we can? */
493 else
494 { /* remove a file */
495 draw_slider();
496 result = remove(dirname);
498 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
500 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
501 result = -1;
502 break;
505 closedir(dir);
507 if (!result)
508 { /* remove the now empty directory */
509 dirname[dirlen] = '\0'; /* terminate to original length */
511 result = rmdir(dirname);
514 return result;
518 /* share code for file and directory deletion, saves space */
519 static bool delete_handler(bool is_dir)
521 char file_to_delete[MAX_PATH];
522 strcpy(file_to_delete, selected_file);
524 const char *lines[]={
525 ID2P(LANG_REALLY_DELETE),
526 file_to_delete
528 const char *yes_lines[]={
529 ID2P(LANG_DELETED),
530 file_to_delete
533 struct text_message message={lines, 2};
534 struct text_message yes_message={yes_lines, 2};
536 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
537 return false;
539 gui_syncsplash(0, str(LANG_DELETING));
541 int res;
542 if (is_dir)
544 char pathname[MAX_PATH]; /* space to go deep */
545 cpu_boost(true);
546 strncpy(pathname, file_to_delete, sizeof pathname);
547 res = remove_dir(pathname, sizeof(pathname));
548 cpu_boost(false);
550 else
551 res = remove(file_to_delete);
553 if (!res)
554 onplay_result = ONPLAY_RELOAD_DIR;
556 return false;
560 static bool delete_file(void)
562 return delete_handler(false);
565 static bool delete_dir(void)
567 return delete_handler(true);
570 #if LCD_DEPTH > 1
571 static bool set_backdrop(void)
573 /* load the image */
574 if(load_main_backdrop(selected_file)) {
575 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
576 set_file(selected_file, (char *)global_settings.backdrop_file,
577 MAX_FILENAME);
578 show_main_backdrop();
579 return true;
580 } else {
581 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
582 return false;
585 #endif
587 static bool rename_file(void)
589 char newname[MAX_PATH];
590 char* ptr = strrchr(selected_file, '/') + 1;
591 int pathlen = (ptr - selected_file);
592 strncpy(newname, selected_file, sizeof newname);
593 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
594 if (!strlen(newname + pathlen) ||
595 (rename(selected_file, newname) < 0)) {
596 lcd_clear_display();
597 lcd_puts(0,0,str(LANG_RENAME));
598 lcd_puts(0,1,str(LANG_FAILED));
599 lcd_update();
600 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
601 sleep(HZ*2);
603 else
604 onplay_result = ONPLAY_RELOAD_DIR;
607 return false;
610 static bool create_dir(void)
612 char dirname[MAX_PATH];
613 char *cwd;
614 int rc;
615 int pathlen;
617 cwd = getcwd(NULL, 0);
618 memset(dirname, 0, sizeof dirname);
620 snprintf(dirname, sizeof dirname, "%s/",
621 cwd[1] ? cwd : "");
623 pathlen = strlen(dirname);
624 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
625 if (rc < 0)
626 return false;
628 rc = mkdir(dirname);
629 if (rc < 0) {
630 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
631 gui_syncsplash(HZ, (unsigned char *)"%s %s",
632 str(LANG_CREATE_DIR), str(LANG_FAILED));
633 } else {
634 onplay_result = ONPLAY_RELOAD_DIR;
637 return true;
640 static bool properties(void)
642 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
643 selected_file))
644 onplay_result = ONPLAY_RELOAD_DIR;
645 return false;
648 /* Store the current selection in the clipboard */
649 static bool clipboard_clip(bool copy)
651 clipboard_selection[0] = 0;
652 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
653 clipboard_selection_attr = selected_file_attr;
654 clipboard_is_copy = copy;
656 return true;
659 static bool clipboard_cut(void)
661 return clipboard_clip(false);
664 static bool clipboard_copy(void)
666 return clipboard_clip(true);
669 #ifdef HAVE_LCD_BITMAP
670 static void draw_slider(void)
672 int i;
673 FOR_NB_SCREENS(i)
675 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
676 LCD_WIDTH-2, 2*screens[i].char_height-1);
677 screens[i].update();
680 #endif
682 /* Paste a file to a new directory. Will overwrite always. */
683 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
685 int src_fd, target_fd;
686 size_t buffersize;
687 ssize_t size, bytesread, byteswritten;
688 char *buffer;
689 bool result = false;
691 if (copy) {
692 /* See if we can get the plugin buffer for the file copy buffer */
693 buffer = (char *) plugin_get_buffer(&buffersize);
694 if (buffer == NULL || buffersize < 512) {
695 /* Not large enough, try for a disk sector worth of stack
696 instead */
697 buffersize = 512;
698 buffer = (char *) __builtin_alloca(buffersize);
701 if (buffer == NULL) {
702 return false;
705 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
706 size */
708 src_fd = open(src, O_RDONLY);
710 if (src_fd >= 0) {
711 target_fd = creat(target);
713 if (target_fd >= 0) {
714 result = true;
716 size = filesize(src_fd);
718 if (size == -1) {
719 result = false;
722 while(size > 0) {
723 bytesread = read(src_fd, buffer, buffersize);
725 if (bytesread == -1) {
726 result = false;
727 break;
730 size -= bytesread;
732 while(bytesread > 0) {
733 byteswritten = write(target_fd, buffer, bytesread);
735 if (byteswritten == -1) {
736 result = false;
737 size = 0;
738 break;
741 bytesread -= byteswritten;
742 draw_slider();
746 close(target_fd);
748 /* Copy failed. Cleanup. */
749 if (!result) {
750 remove(target);
754 close(src_fd);
756 } else {
757 result = rename(src, target) == 0;
758 #ifdef HAVE_MULTIVOLUME
759 if (!result) {
760 if (errno == EXDEV) {
761 /* Failed because cross volume rename doesn't work. Copy
762 instead */
763 result = clipboard_pastefile(src, target, true);
765 if (result) {
766 result = remove(src) == 0;
770 #endif
773 return result;
776 /* Paste a directory to a new location. Designed to be called by
777 clipboard_paste */
778 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
779 int targetlen, bool copy)
781 DIR *srcdir;
782 int srcdirlen = strlen(src);
783 int targetdirlen = strlen(target);
784 bool result = true;
786 if (!file_exists(target)) {
787 if (!copy) {
788 /* Just move the directory */
789 result = rename(src, target) == 0;
791 #ifdef HAVE_MULTIVOLUME
792 if (!result && errno == EXDEV) {
793 /* Try a copy as we're going across devices */
794 result = clipboard_pastedirectory(src, srclen, target,
795 targetlen, true);
797 /* If it worked, remove the source directory */
798 if (result) {
799 remove_dir(src, srclen);
802 #endif
803 return result;
804 } else {
805 /* Make a directory to copy things to */
806 result = mkdir(target) == 0;
810 /* Check if something went wrong already */
811 if (!result) {
812 return result;
815 srcdir = opendir(src);
816 if (!srcdir) {
817 return false;
820 /* This loop will exit as soon as there's a problem */
821 while(result)
823 struct dirent* entry;
824 /* walk through the directory content */
825 entry = readdir(srcdir);
826 if (!entry)
827 break;
829 /* append name to current directory */
830 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
831 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
832 entry->d_name);
834 DEBUGF("Copy %s to %s\n", src, target);
836 if (entry->attribute & ATTR_DIRECTORY)
837 { /* copy/move a subdirectory */
838 if (!strcmp((char *)entry->d_name, ".") ||
839 !strcmp((char *)entry->d_name, ".."))
840 continue; /* skip these */
842 result = clipboard_pastedirectory(src, srclen, target, targetlen,
843 copy); /* recursion */
845 else
846 { /* copy/move a file */
847 result = clipboard_pastefile(src, target, copy);
851 closedir(srcdir);
853 if (result) {
854 src[srcdirlen] = '\0'; /* terminate to original length */
855 target[targetdirlen] = '\0'; /* terminate to original length */
858 return result;
861 /* Paste the clipboard to the current directory */
862 static bool clipboard_paste(void)
864 char target[MAX_PATH];
865 char *cwd, *nameptr;
866 bool success;
868 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
869 static const struct text_message message={lines, 1};
871 /* Get the name of the current directory */
872 cwd = getcwd(NULL, 0);
874 /* Figure out the name of the selection */
875 nameptr = strrchr(clipboard_selection, '/');
877 /* Final target is current directory plus name of selection */
878 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
880 /* If the target existed but they choose not to overwite, exit */
881 if (file_exists(target) &&
882 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
883 return false;
886 if (clipboard_is_copy) {
887 gui_syncsplash(0, ID2P(LANG_COPYING));
889 else
891 gui_syncsplash(0, ID2P(LANG_MOVING));
894 /* Now figure out what we're doing */
895 cpu_boost(true);
896 if (clipboard_selection_attr & ATTR_DIRECTORY) {
897 /* Recursion. Set up external stack */
898 char srcpath[MAX_PATH];
899 char targetpath[MAX_PATH];
900 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
902 /* Do not allow the user to paste a directory into a dir they are
903 copying */
904 success = 0;
906 else
908 strncpy(srcpath, clipboard_selection, sizeof srcpath);
909 strncpy(targetpath, target, sizeof targetpath);
911 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
912 target, sizeof(targetpath), clipboard_is_copy);
914 } else {
915 success = clipboard_pastefile(clipboard_selection, target,
916 clipboard_is_copy);
918 cpu_boost(false);
920 /* Did it work? */
921 if (success) {
922 /* Reset everything */
923 clipboard_selection[0] = 0;
924 clipboard_selection_attr = 0;
925 clipboard_is_copy = false;
927 /* Force reload of the current directory */
928 onplay_result = ONPLAY_RELOAD_DIR;
929 } else {
930 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
931 gui_syncsplash(HZ, (unsigned char *)"%s %s",
932 str(LANG_PASTE), str(LANG_FAILED));
935 return true;
938 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
939 #ifdef HAVE_TAGCACHE
940 static int set_rating_inline(void)
942 struct mp3entry* id3 = audio_current_track();
943 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
945 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
946 NULL, 1, 0, 10, NULL, NULL);
947 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
949 else
950 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
951 return 0;
953 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
955 (void)this_item;
956 switch (action)
958 case ACTION_REQUEST_MENUITEM:
959 if (!selected_file || !global_settings.runtimedb)
960 return ACTION_EXIT_MENUITEM;
961 break;
963 return action;
965 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
966 set_rating_inline, NULL,
967 ratingitem_callback, Icon_Questionmark);
968 #endif
970 static bool view_cue(void)
972 struct mp3entry* id3 = audio_current_track();
973 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
975 browse_cuesheet(curr_cue);
977 return false;
979 static int view_cue_item_callback(int action,
980 const struct menu_item_ex *this_item)
982 (void)this_item;
983 struct mp3entry* id3 = audio_current_track();
984 switch (action)
986 case ACTION_REQUEST_MENUITEM:
987 if (!selected_file || !cuesheet_is_enabled()
988 || !id3 || !id3->cuesheet_type)
989 return ACTION_EXIT_MENUITEM;
990 break;
992 return action;
994 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
995 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
997 /* CONTEXT_WPS items */
998 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
999 browse_id3, NULL, NULL, Icon_NOICON);
1000 #ifdef HAVE_PITCHSCREEN
1001 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1002 pitch_screen, NULL, NULL, Icon_Audio);
1003 #endif
1005 /* CONTEXT_[TREE|ID3DB] items */
1006 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1007 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1008 rename_file, NULL, clipboard_callback, Icon_NOICON);
1009 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1010 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1011 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1012 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1013 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1014 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1015 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1016 delete_file, NULL, clipboard_callback, Icon_NOICON);
1017 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1018 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1019 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1020 properties, NULL, clipboard_callback, Icon_NOICON);
1021 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1022 create_dir, NULL, clipboard_callback, Icon_NOICON);
1023 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1024 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1025 #if LCD_DEPTH > 1
1026 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1027 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1028 #endif
1029 #ifdef HAVE_RECORDING
1030 static bool set_recdir(void)
1032 strncpy(global_settings.rec_directory,
1033 selected_file, MAX_FILENAME+1);
1034 settings_save();
1035 return false;
1037 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1038 set_recdir, NULL, clipboard_callback, Icon_Recording);
1039 #endif
1040 static bool add_to_faves(void)
1042 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1043 selected_file))
1044 onplay_result = ONPLAY_RELOAD_DIR;
1045 return false;
1047 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1048 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1051 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1053 switch (action)
1055 case ACTION_REQUEST_MENUITEM:
1056 #ifdef HAVE_MULTIVOLUME
1057 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1058 (this_item == &rename_file_item ||
1059 this_item == &delete_dir_item ||
1060 this_item == &clipboard_cut_item) )
1061 return ACTION_EXIT_MENUITEM;
1062 #endif
1063 if (context == CONTEXT_ID3DB)
1064 return ACTION_EXIT_MENUITEM;
1065 if (this_item == &clipboard_paste_item)
1066 { /* visible if there is something to paste */
1067 return (clipboard_selection[0] != 0) ?
1068 action : ACTION_EXIT_MENUITEM;
1070 else if (this_item == &create_dir_item)
1072 /* always visible */
1073 return action;
1075 else if ((this_item == &properties_item) ||
1076 (this_item == &rename_file_item) ||
1077 (this_item == &clipboard_cut_item) ||
1078 (this_item == &clipboard_copy_item) ||
1079 (this_item == &add_to_faves_item)
1082 /* requires an actual file */
1083 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1085 #if LCD_DEPTH > 1
1086 else if (this_item == &set_backdrop_item)
1088 if (selected_file)
1090 char *suffix = strrchr(selected_file, '.');
1091 if (suffix)
1093 if (strcasecmp(suffix, ".bmp") == 0)
1095 return action;
1099 return ACTION_EXIT_MENUITEM;
1101 #endif
1102 else if ((selected_file_attr & ATTR_DIRECTORY))
1104 if ((this_item == &delete_dir_item)
1106 return action;
1107 #ifdef HAVE_RECORDING
1108 else if (this_item == &set_recdir_item)
1109 return action;
1110 #endif
1112 else if (selected_file
1113 #ifdef HAVE_MULTIVOLUME
1114 /* no rename+delete for volumes */
1115 && !(selected_file_attr & ATTR_VOLUME)
1116 #endif
1119 if ((this_item == &delete_file_item) ||
1120 (this_item == &list_viewers_item))
1122 return action;
1125 return ACTION_EXIT_MENUITEM;
1126 break;
1128 return action;
1130 /* used when onplay() is called in the CONTEXT_WPS context */
1133 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1134 onplaymenu_callback, Icon_Audio,
1135 &wps_playlist_menu, &cat_playlist_menu,
1136 &sound_settings, &playback_settings,
1137 #ifdef HAVE_TAGCACHE
1138 &rating_item,
1139 #endif
1140 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1141 &delete_file_item, &view_cue_item,
1142 #ifdef HAVE_PITCHSCREEN
1143 &pitch_screen_item,
1144 #endif
1146 /* used when onplay() is not called in the CONTEXT_WPS context */
1147 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1148 onplaymenu_callback, Icon_file_view_menu,
1149 &tree_playlist_menu, &cat_playlist_menu,
1150 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1151 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1152 #if LCD_DEPTH > 1
1153 &set_backdrop_item,
1154 #endif
1155 &list_viewers_item, &create_dir_item, &properties_item,
1156 #ifdef HAVE_RECORDING
1157 &set_recdir_item,
1158 #endif
1159 &add_to_faves_item,
1161 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1163 (void)this_item;
1164 switch (action)
1166 case ACTION_TREE_STOP:
1167 if (this_item == &wps_onplay_menu)
1169 list_stop_handler();
1170 return ACTION_STD_CANCEL;
1172 break;
1173 case ACTION_EXIT_MENUITEM:
1174 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1175 break;
1177 return action;
1179 int onplay(char* file, int attr, int from)
1181 const struct menu_item_ex *menu;
1182 onplay_result = ONPLAY_OK;
1183 context = from;
1184 selected_file = file;
1185 selected_file_attr = attr;
1186 if (context == CONTEXT_WPS)
1187 menu = &wps_onplay_menu;
1188 else
1189 menu = &tree_onplay_menu;
1190 switch (do_menu(menu, NULL, NULL, false))
1192 case GO_TO_WPS:
1193 return ONPLAY_START_PLAY;
1194 case GO_TO_ROOT:
1195 case GO_TO_MAINMENU:
1196 return ONPLAY_MAINMENU;
1197 default:
1198 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;