grr.. typo
[Rockbox.git] / apps / onplay.c
blob47b962bd44e00989206a71670388fe03be0d030c
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 #if CONFIG_CODEC == SWCODEC
59 #include "menus/eq_menu.h"
60 #endif
61 #include "playlist_menu.h"
62 #include "playlist_catalog.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagtree.h"
65 #endif
66 #include "cuesheet.h"
67 #include "backdrop.h"
69 static int context;
70 static char* selected_file = NULL;
71 static int selected_file_attr = 0;
72 static int onplay_result = ONPLAY_OK;
73 static char clipboard_selection[MAX_PATH];
74 static int clipboard_selection_attr = 0;
75 static bool clipboard_is_copy = false;
77 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
78 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
79 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
80 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
81 static const struct menu_item_ex name = \
82 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
83 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
84 { (void*)name##_},{.callback_and_desc = & name##__}};
86 #ifdef HAVE_LCD_BITMAP
87 static void draw_slider(void);
88 #else
89 #define draw_slider()
90 #endif
91 /* ----------------------------------------------------------------------- */
92 /* Displays the bookmark menu options for the user to decide. This is an */
93 /* interface function. */
94 /* ----------------------------------------------------------------------- */
96 static int bookmark_menu_callback(int action,
97 const struct menu_item_ex *this_item);
98 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
99 ID2P(LANG_BOOKMARK_MENU_CREATE),
100 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
101 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
102 ID2P(LANG_BOOKMARK_MENU_LIST),
103 bookmark_load_menu, NULL,
104 bookmark_menu_callback, Icon_Bookmark);
105 MAKE_MENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
106 Icon_Bookmark, &bookmark_create_menu_item, &bookmark_load_menu_item);
107 static int bookmark_menu_callback(int action,
108 const struct menu_item_ex *this_item)
110 (void)this_item;
111 switch (action)
113 case ACTION_REQUEST_MENUITEM:
114 if (this_item == &bookmark_load_menu_item)
116 if (bookmark_exist() == 0)
117 return ACTION_EXIT_MENUITEM;
119 /* hide the bookmark menu if there is no playback */
120 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
121 return ACTION_EXIT_MENUITEM;
122 break;
123 #ifdef HAVE_LCD_CHARCELLS
124 case ACTION_ENTER_MENUITEM:
125 status_set_param(true);
126 break;
127 #endif
128 case ACTION_EXIT_MENUITEM:
129 #ifdef HAVE_LCD_CHARCELLS
130 status_set_param(false);
131 #endif
132 settings_save();
133 break;
135 return action;
138 static bool list_viewers(void)
140 int ret = filetype_list_viewers(selected_file);
141 if (ret == PLUGIN_USB_CONNECTED)
142 onplay_result = ONPLAY_RELOAD_DIR;
143 return false;
146 static bool shuffle_playlist(void)
148 playlist_sort(NULL, true);
149 playlist_randomise(NULL, current_tick, true);
151 return false;
154 static bool save_playlist(void)
156 save_playlist_screen(NULL);
157 return false;
160 static bool add_to_playlist(int position, bool queue)
162 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
163 char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 struct text_message message={lines, 2};
169 gui_syncsplash(0, ID2P(LANG_WAIT));
171 if (new_playlist)
172 playlist_create(NULL, NULL);
174 /* always set seed before inserting shuffled */
175 if (position == PLAYLIST_INSERT_SHUFFLED)
176 srand(current_tick);
178 #ifdef HAVE_TAGCACHE
179 if (context == CONTEXT_ID3DB)
181 tagtree_insert_selection_playlist(position, queue);
183 else
184 #endif
186 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
187 playlist_insert_track(NULL, selected_file, position, queue, true);
188 else if (selected_file_attr & ATTR_DIRECTORY)
190 bool recurse = false;
192 if (global_settings.recursive_dir_insert != RECURSE_ASK)
193 recurse = (bool)global_settings.recursive_dir_insert;
194 else
196 /* Ask if user wants to recurse directory */
197 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
200 playlist_insert_directory(NULL, selected_file, position, queue,
201 recurse);
203 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
204 playlist_insert_playlist(NULL, selected_file, position, queue);
207 if (new_playlist && (playlist_amount() > 0))
209 /* nothing is currently playing so begin playing what we just
210 inserted */
211 if (global_settings.playlist_shuffle)
212 playlist_shuffle(current_tick, -1);
213 playlist_start(0,0);
214 gui_syncstatusbar_draw(&statusbars, false);
215 onplay_result = ONPLAY_START_PLAY;
218 return false;
221 static bool view_playlist(void)
223 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
224 bool result;
226 result = playlist_viewer_ex(selected_file);
228 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
229 onplay_result == ONPLAY_OK)
230 /* playlist was started from viewer */
231 onplay_result = ONPLAY_START_PLAY;
233 return result;
236 static bool cat_add_to_a_playlist(void)
238 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
239 false);
242 static bool cat_add_to_a_new_playlist(void)
244 return catalog_add_to_a_playlist(selected_file, selected_file_attr, true);
248 static int cat_playlist_callback(int action,
249 const struct menu_item_ex *this_item);
250 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
251 catalog_view_playlists, 0, cat_playlist_callback,
252 Icon_Playlist);
253 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
254 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
255 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
256 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
257 MAKE_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
258 Icon_Playlist, &cat_view_lists,
259 &cat_add_to_list, &cat_add_to_new );
261 static int cat_playlist_callback(int action,
262 const struct menu_item_ex *this_item)
264 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
265 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
266 ((selected_file_attr & ATTR_DIRECTORY) == 0))
268 return ACTION_EXIT_MENUITEM;
271 switch (action)
273 case ACTION_REQUEST_MENUITEM:
274 if (this_item == &cat_view_lists)
276 if (context == CONTEXT_WPS)
277 return action;
279 else if (selected_file && /* set before calling this menu,
280 so safe */
281 ((audio_status() & AUDIO_STATUS_PLAY &&
282 context == CONTEXT_WPS) ||
283 context == CONTEXT_TREE))
285 return action;
287 else
288 return ACTION_EXIT_MENUITEM;
289 break;
291 return action;
295 /* CONTEXT_WPS playlist options */
296 MENUITEM_FUNCTION(playlist_viewer_item, 0,
297 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
298 NULL, NULL, Icon_Playlist);
299 MENUITEM_FUNCTION(search_playlist_item, 0,
300 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
301 NULL, NULL, Icon_Playlist);
302 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
303 save_playlist, NULL, NULL, Icon_Playlist);
304 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
305 shuffle_playlist, NULL, NULL, Icon_Playlist);
306 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
307 NULL, Icon_Playlist,
308 &playlist_viewer_item, &search_playlist_item,
309 &playlist_save_item, &reshuffle_item
312 /* CONTEXT_[TREE|ID3DB] playlist options */
313 static int playlist_insert_func(void *param)
315 add_to_playlist((intptr_t)param, false);
316 return 0;
318 static int playlist_queue_func(void *param)
320 add_to_playlist((intptr_t)param, true);
321 return 0;
323 static int treeplaylist_wplayback_callback(int action,
324 const struct menu_item_ex*
325 this_item)
327 (void)this_item;
328 switch (action)
330 case ACTION_REQUEST_MENUITEM:
331 if (audio_status() & AUDIO_STATUS_PLAY)
332 return action;
333 else
334 return ACTION_EXIT_MENUITEM;
335 break;
337 return action;
340 static int treeplaylist_callback(int action,
341 const struct menu_item_ex *this_item);
343 /* insert items */
344 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
345 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
346 treeplaylist_callback, Icon_Playlist);
347 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
348 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
349 treeplaylist_wplayback_callback, Icon_Playlist);
350 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
351 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
352 treeplaylist_wplayback_callback, Icon_Playlist);
353 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
354 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
355 treeplaylist_wplayback_callback, Icon_Playlist);
356 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
357 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
358 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
359 Icon_Playlist);
360 /* queue items */
361 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
362 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
363 treeplaylist_wplayback_callback, Icon_Playlist);
364 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
365 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
366 treeplaylist_wplayback_callback, Icon_Playlist);
367 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
368 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
369 treeplaylist_wplayback_callback, Icon_Playlist);
370 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
371 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
372 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
373 treeplaylist_wplayback_callback, Icon_Playlist);
374 /* replace playlist */
375 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
376 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
377 treeplaylist_wplayback_callback, Icon_Playlist);
378 /* others */
380 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
381 view_playlist, NULL,
382 treeplaylist_callback, Icon_Playlist);
384 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
385 treeplaylist_callback, Icon_Playlist,
387 /* view */
388 &view_playlist_item,
390 /* insert */
391 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
392 &i_last_pl_item, &i_shuf_pl_item,
394 /* queue */
395 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
396 &q_shuf_pl_item,
398 /* replace */
399 &replace_pl_item
401 static int treeplaylist_callback(int action,
402 const struct menu_item_ex *this_item)
404 (void)this_item;
405 switch (action)
407 case ACTION_REQUEST_MENUITEM:
408 if (this_item == &tree_playlist_menu)
410 if (((selected_file_attr & FILE_ATTR_MASK) ==
411 FILE_ATTR_AUDIO) ||
412 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
413 (selected_file_attr & ATTR_DIRECTORY))
415 return action;
417 else
418 return ACTION_EXIT_MENUITEM;
420 else if (this_item == &view_playlist_item)
422 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
423 context == CONTEXT_TREE)
424 return action;
425 else
426 return ACTION_EXIT_MENUITEM;
428 else if (this_item == &i_pl_item_no_play)
430 if (!(audio_status() & AUDIO_STATUS_PLAY))
432 return action;
434 else
435 return ACTION_EXIT_MENUITEM;
437 else if (this_item == &i_shuf_pl_item)
440 if (audio_status() & AUDIO_STATUS_PLAY)
442 return action;
444 else if ((this_item == &i_shuf_pl_item) &&
445 ((selected_file_attr & ATTR_DIRECTORY) ||
446 ((selected_file_attr & FILE_ATTR_MASK) ==
447 FILE_ATTR_M3U)))
449 return action;
451 return ACTION_EXIT_MENUITEM;
453 break;
455 return action;
458 /* helper function to remove a non-empty directory */
459 static int remove_dir(char* dirname, int len)
461 int result = 0;
462 DIR* dir;
463 int dirlen = strlen(dirname);
464 int i;
466 dir = opendir(dirname);
467 if (!dir)
468 return -1; /* open error */
470 while(true)
472 struct dirent* entry;
473 /* walk through the directory content */
474 entry = readdir(dir);
475 if (!entry)
476 break;
478 dirname[dirlen] ='\0';
479 FOR_NB_SCREENS(i){
480 screens[i].puts(0,1,dirname);
481 screens[i].update();
484 /* append name to current directory */
485 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
486 if (entry->attribute & ATTR_DIRECTORY)
487 { /* remove a subdirectory */
488 if (!strcmp((char *)entry->d_name, ".") ||
489 !strcmp((char *)entry->d_name, ".."))
490 continue; /* skip these */
492 /* inform the user which dir we're deleting */
494 result = remove_dir(dirname, len); /* recursion */
495 if (result)
496 break; /* or better continue, delete what we can? */
498 else
499 { /* remove a file */
500 draw_slider();
501 result = remove(dirname);
503 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
505 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
506 result = -1;
507 break;
510 closedir(dir);
512 if (!result)
513 { /* remove the now empty directory */
514 dirname[dirlen] = '\0'; /* terminate to original length */
516 result = rmdir(dirname);
519 return result;
523 /* share code for file and directory deletion, saves space */
524 static bool delete_handler(bool is_dir)
526 char *lines[]={
527 ID2P(LANG_REALLY_DELETE),
528 selected_file
530 char *yes_lines[]={
531 ID2P(LANG_DELETED),
532 selected_file
535 struct text_message message={lines, 2};
536 struct text_message yes_message={yes_lines, 2};
538 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
539 return false;
541 gui_syncsplash(0, str(LANG_DELETING));
543 int res;
544 if (is_dir)
546 char pathname[MAX_PATH]; /* space to go deep */
547 cpu_boost(true);
548 strncpy(pathname, selected_file, sizeof pathname);
549 res = remove_dir(pathname, sizeof(pathname));
550 cpu_boost(false);
552 else
553 res = remove(selected_file);
555 if (!res) {
556 onplay_result = ONPLAY_RELOAD_DIR;
558 return false;
562 static bool delete_file(void)
564 return delete_handler(false);
567 static bool delete_dir(void)
569 return delete_handler(true);
572 #if LCD_DEPTH > 1
573 static bool set_backdrop(void)
575 /* load the image */
576 if(load_main_backdrop(selected_file)) {
577 gui_syncsplash(HZ, str(LANG_BACKDROP_LOADED));
578 set_file(selected_file, (char *)global_settings.backdrop_file,
579 MAX_FILENAME);
580 show_main_backdrop();
581 return true;
582 } else {
583 gui_syncsplash(HZ, str(LANG_BACKDROP_FAILED));
584 return false;
587 #endif
589 static bool rename_file(void)
591 char newname[MAX_PATH];
592 char* ptr = strrchr(selected_file, '/') + 1;
593 int pathlen = (ptr - selected_file);
594 strncpy(newname, selected_file, sizeof newname);
595 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
596 if (!strlen(newname + pathlen) ||
597 (rename(selected_file, newname) < 0)) {
598 lcd_clear_display();
599 lcd_puts(0,0,str(LANG_RENAME));
600 lcd_puts(0,1,str(LANG_FAILED));
601 lcd_update();
602 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
603 sleep(HZ*2);
605 else
606 onplay_result = ONPLAY_RELOAD_DIR;
609 return false;
612 static bool create_dir(void)
614 char dirname[MAX_PATH];
615 char *cwd;
616 int rc;
617 int pathlen;
619 cwd = getcwd(NULL, 0);
620 memset(dirname, 0, sizeof dirname);
622 snprintf(dirname, sizeof dirname, "%s/",
623 cwd[1] ? cwd : "");
625 pathlen = strlen(dirname);
626 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
627 if (rc < 0)
628 return false;
630 rc = mkdir(dirname);
631 if (rc < 0) {
632 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
633 gui_syncsplash(HZ, (unsigned char *)"%s %s",
634 str(LANG_CREATE_DIR), str(LANG_FAILED));
635 } else {
636 onplay_result = ONPLAY_RELOAD_DIR;
639 return true;
642 static bool properties(void)
644 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
645 selected_file))
646 onplay_result = ONPLAY_RELOAD_DIR;
647 return false;
650 /* Store the current selection in the clipboard */
651 static bool clipboard_clip(bool copy)
653 clipboard_selection[0] = 0;
654 strncpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
655 clipboard_selection_attr = selected_file_attr;
656 clipboard_is_copy = copy;
658 return true;
661 static bool clipboard_cut(void)
663 return clipboard_clip(false);
666 static bool clipboard_copy(void)
668 return clipboard_clip(true);
671 #ifdef HAVE_LCD_BITMAP
672 static void draw_slider(void)
674 int i;
675 FOR_NB_SCREENS(i)
677 show_busy_slider(&screens[i], 1, LCD_HEIGHT-2*screens[i].char_height,
678 LCD_WIDTH-2, 2*screens[i].char_height-1);
679 screens[i].update();
682 #endif
684 /* Paste a file to a new directory. Will overwrite always. */
685 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
687 int src_fd, target_fd;
688 size_t buffersize;
689 ssize_t size, bytesread, byteswritten;
690 char *buffer;
691 bool result = false;
693 if (copy) {
694 /* See if we can get the plugin buffer for the file copy buffer */
695 buffer = (char *) plugin_get_buffer(&buffersize);
696 if (buffer == NULL || buffersize < 512) {
697 /* Not large enough, try for a disk sector worth of stack
698 instead */
699 buffersize = 512;
700 buffer = (char *) __builtin_alloca(buffersize);
703 if (buffer == NULL) {
704 return false;
707 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
708 size */
710 src_fd = open(src, O_RDONLY);
712 if (src_fd >= 0) {
713 target_fd = creat(target);
715 if (target_fd >= 0) {
716 result = true;
718 size = filesize(src_fd);
720 if (size == -1) {
721 result = false;
724 while(size > 0) {
725 bytesread = read(src_fd, buffer, buffersize);
727 if (bytesread == -1) {
728 result = false;
729 break;
732 size -= bytesread;
734 while(bytesread > 0) {
735 byteswritten = write(target_fd, buffer, bytesread);
737 if (byteswritten == -1) {
738 result = false;
739 size = 0;
740 break;
743 bytesread -= byteswritten;
744 draw_slider();
748 close(target_fd);
750 /* Copy failed. Cleanup. */
751 if (!result) {
752 remove(target);
756 close(src_fd);
758 } else {
759 result = rename(src, target) == 0;
760 #ifdef HAVE_MULTIVOLUME
761 if (!result) {
762 if (errno == EXDEV) {
763 /* Failed because cross volume rename doesn't work. Copy
764 instead */
765 result = clipboard_pastefile(src, target, true);
767 if (result) {
768 result = remove(src) == 0;
772 #endif
775 return result;
778 /* Paste a directory to a new location. Designed to be called by
779 clipboard_paste */
780 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
781 int targetlen, bool copy)
783 DIR *srcdir;
784 int srcdirlen = strlen(src);
785 int targetdirlen = strlen(target);
786 int fd;
787 bool result = true;
789 /* Check if the target exists */
790 fd = open(target, O_RDONLY);
791 close(fd);
793 if (fd < 0) {
794 if (!copy) {
795 /* Just move the directory */
796 result = rename(src, target) == 0;
798 #ifdef HAVE_MULTIVOLUME
799 if (!result && errno == EXDEV) {
800 /* Try a copy as we're going across devices */
801 result = clipboard_pastedirectory(src, srclen, target,
802 targetlen, true);
804 /* If it worked, remove the source directory */
805 if (result) {
806 remove_dir(src, srclen);
809 #endif
810 return result;
811 } else {
812 /* Make a directory to copy things to */
813 result = mkdir(target) == 0;
817 /* Check if something went wrong already */
818 if (!result) {
819 return result;
822 srcdir = opendir(src);
823 if (!srcdir) {
824 return false;
827 /* This loop will exit as soon as there's a problem */
828 while(result)
830 struct dirent* entry;
831 /* walk through the directory content */
832 entry = readdir(srcdir);
833 if (!entry)
834 break;
836 /* append name to current directory */
837 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
838 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
839 entry->d_name);
841 DEBUGF("Copy %s to %s\n", src, target);
843 if (entry->attribute & ATTR_DIRECTORY)
844 { /* copy/move a subdirectory */
845 if (!strcmp((char *)entry->d_name, ".") ||
846 !strcmp((char *)entry->d_name, ".."))
847 continue; /* skip these */
849 result = clipboard_pastedirectory(src, srclen, target, targetlen,
850 copy); /* recursion */
852 else
853 { /* copy/move a file */
854 result = clipboard_pastefile(src, target, copy);
858 closedir(srcdir);
860 if (result) {
861 src[srcdirlen] = '\0'; /* terminate to original length */
862 target[targetdirlen] = '\0'; /* terminate to original length */
865 return result;
868 /* Paste the clipboard to the current directory */
869 static bool clipboard_paste(void)
871 char target[MAX_PATH];
872 char *cwd, *nameptr;
873 bool success;
874 int target_fd;
876 unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
877 struct text_message message={(char **)lines, 1};
879 /* Get the name of the current directory */
880 cwd = getcwd(NULL, 0);
882 /* Figure out the name of the selection */
883 nameptr = strrchr(clipboard_selection, '/');
885 /* Final target is current directory plus name of selection */
886 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
888 /* Check if we're going to overwrite */
889 target_fd = open(target, O_RDONLY);
890 close(target_fd);
892 /* If the target existed but they choose not to overwite, exit */
893 if (target_fd >= 0 &&
894 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
895 return false;
898 if (clipboard_is_copy) {
899 gui_syncsplash(0, ID2P(LANG_COPYING));
901 else
903 gui_syncsplash(0, ID2P(LANG_MOVING));
906 /* Now figure out what we're doing */
907 cpu_boost(true);
908 if (clipboard_selection_attr & ATTR_DIRECTORY) {
909 /* Recursion. Set up external stack */
910 char srcpath[MAX_PATH];
911 char targetpath[MAX_PATH];
912 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
914 /* Do not allow the user to paste a directory into a dir they are
915 copying */
916 success = 0;
918 else
920 strncpy(srcpath, clipboard_selection, sizeof srcpath);
921 strncpy(targetpath, target, sizeof targetpath);
923 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
924 target, sizeof(targetpath), clipboard_is_copy);
926 } else {
927 success = clipboard_pastefile(clipboard_selection, target,
928 clipboard_is_copy);
930 cpu_boost(false);
932 /* Did it work? */
933 if (success) {
934 /* Reset everything */
935 clipboard_selection[0] = 0;
936 clipboard_selection_attr = 0;
937 clipboard_is_copy = false;
939 /* Force reload of the current directory */
940 onplay_result = ONPLAY_RELOAD_DIR;
941 } else {
942 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
943 gui_syncsplash(HZ, (unsigned char *)"%s %s",
944 str(LANG_PASTE), str(LANG_FAILED));
947 return true;
950 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
951 #ifdef HAVE_TAGCACHE
952 static int set_rating_inline(void)
954 struct mp3entry* id3 = audio_current_track();
955 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
957 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
958 NULL, 1, 0, 10, NULL, NULL);
959 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
961 else
962 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
963 return 0;
965 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
967 (void)this_item;
968 switch (action)
970 case ACTION_REQUEST_MENUITEM:
971 if (!selected_file || !global_settings.runtimedb)
972 return ACTION_EXIT_MENUITEM;
973 break;
975 return action;
977 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
978 set_rating_inline, NULL,
979 ratingitem_callback, Icon_Questionmark);
980 #endif
982 static bool view_cue(void)
984 struct mp3entry* id3 = audio_current_track();
985 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
987 browse_cuesheet(curr_cue);
989 return false;
991 static int view_cue_item_callback(int action,
992 const struct menu_item_ex *this_item)
994 (void)this_item;
995 struct mp3entry* id3 = audio_current_track();
996 switch (action)
998 case ACTION_REQUEST_MENUITEM:
999 if (!selected_file || !cuesheet_is_enabled()
1000 || !id3 || !id3->cuesheet_type)
1001 return ACTION_EXIT_MENUITEM;
1002 break;
1004 return action;
1006 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1007 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1009 /* CONTEXT_WPS items */
1010 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1011 browse_id3, NULL, NULL, Icon_NOICON);
1012 #ifdef HAVE_PITCHSCREEN
1013 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1014 pitch_screen, NULL, NULL, Icon_Audio);
1015 #endif
1016 #if CONFIG_CODEC == SWCODEC
1017 MENUITEM_FUNCTION(eq_menu_graphical_item, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
1018 eq_menu_graphical, NULL, NULL, Icon_Audio);
1019 MENUITEM_FUNCTION(eq_browse_presets_item, 0, ID2P(LANG_EQUALIZER_BROWSE),
1020 eq_browse_presets, NULL, NULL, Icon_Audio);
1021 #endif
1023 /* CONTEXT_[TREE|ID3DB] items */
1024 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1025 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1026 rename_file, NULL, clipboard_callback, Icon_NOICON);
1027 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1028 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1029 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1030 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1031 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1032 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1033 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1034 delete_file, NULL, clipboard_callback, Icon_NOICON);
1035 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1036 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1037 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1038 properties, NULL, clipboard_callback, Icon_NOICON);
1039 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1040 create_dir, NULL, clipboard_callback, Icon_NOICON);
1041 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1042 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1043 #if LCD_DEPTH > 1
1044 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1045 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1046 #endif
1047 #ifdef HAVE_RECORDING
1048 static bool set_recdir(void)
1050 strncpy(global_settings.rec_directory,
1051 selected_file, MAX_FILENAME+1);
1052 settings_save();
1053 return false;
1055 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1056 set_recdir, NULL, clipboard_callback, Icon_Recording);
1057 #endif
1058 static bool add_to_faves(void)
1060 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1061 selected_file))
1062 onplay_result = ONPLAY_RELOAD_DIR;
1063 return false;
1065 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1066 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1069 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1071 switch (action)
1073 case ACTION_REQUEST_MENUITEM:
1074 if (context == CONTEXT_ID3DB)
1075 return ACTION_EXIT_MENUITEM;
1076 if (this_item == &clipboard_paste_item)
1077 { /* visible if there is something to paste */
1078 return (clipboard_selection[0] != 0) ?
1079 action : ACTION_EXIT_MENUITEM;
1081 else if (this_item == &create_dir_item)
1083 /* always visible */
1084 return action;
1086 else if ((this_item == &properties_item) ||
1087 (this_item == &rename_file_item) ||
1088 (this_item == &clipboard_cut_item) ||
1089 (this_item == &clipboard_copy_item) ||
1090 (this_item == &add_to_faves_item)
1093 /* requires an actual file */
1094 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1096 #if LCD_DEPTH > 1
1097 else if (this_item == &set_backdrop_item)
1099 if (selected_file)
1101 char *suffix = strrchr(selected_file, '.');
1102 if (suffix)
1104 if (strcasecmp(suffix, ".bmp") == 0)
1106 return action;
1110 return ACTION_EXIT_MENUITEM;
1112 #endif
1113 else if ((selected_file_attr & ATTR_DIRECTORY))
1115 if ((this_item == &delete_dir_item)
1117 return action;
1118 #ifdef HAVE_RECORDING
1119 else if (this_item == &set_recdir_item)
1120 return action;
1121 #endif
1123 else if (selected_file
1124 #ifdef HAVE_MULTIVOLUME
1125 /* no rename+delete for volumes */
1126 && !(selected_file_attr & ATTR_VOLUME)
1127 #endif
1130 if ((this_item == &delete_file_item) ||
1131 (this_item == &list_viewers_item))
1133 return action;
1136 return ACTION_EXIT_MENUITEM;
1137 break;
1139 return action;
1141 /* used when onplay() is called in the CONTEXT_WPS context */
1144 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1145 onplaymenu_callback, Icon_Audio,
1146 &sound_settings, &wps_playlist_menu, &cat_playlist_menu,
1147 #ifdef HAVE_TAGCACHE
1148 &rating_item,
1149 #endif
1150 &bookmark_menu, &browse_id3_item, &delete_file_item, &view_cue_item,
1151 #ifdef HAVE_PITCHSCREEN
1152 &pitch_screen_item,
1153 #endif
1154 #if CONFIG_CODEC == SWCODEC
1155 &eq_menu_graphical_item, &eq_browse_presets_item,
1156 #endif
1158 /* used when onplay() is not called in the CONTEXT_WPS context */
1159 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1160 onplaymenu_callback, Icon_file_view_menu,
1161 &tree_playlist_menu, &cat_playlist_menu,
1162 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1163 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1164 #if LCD_DEPTH > 1
1165 &set_backdrop_item,
1166 #endif
1167 &list_viewers_item, &create_dir_item, &properties_item,
1168 #ifdef HAVE_RECORDING
1169 &set_recdir_item,
1170 #endif
1171 &add_to_faves_item,
1173 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1175 (void)this_item;
1176 switch (action)
1178 case ACTION_TREE_STOP:
1179 if (this_item == &wps_onplay_menu)
1181 list_stop_handler();
1182 return ACTION_STD_CANCEL;
1184 break;
1185 case ACTION_EXIT_MENUITEM:
1186 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1187 break;
1189 return action;
1191 int onplay(char* file, int attr, int from)
1193 const struct menu_item_ex *menu;
1194 onplay_result = ONPLAY_OK;
1195 context = from;
1196 selected_file = file;
1197 selected_file_attr = attr;
1198 if (context == CONTEXT_WPS)
1199 menu = &wps_onplay_menu;
1200 else
1201 menu = &tree_onplay_menu;
1202 switch (do_menu(menu, NULL, NULL, false))
1204 case GO_TO_WPS:
1205 return ONPLAY_START_PLAY;
1206 case GO_TO_ROOT:
1207 case GO_TO_MAINMENU:
1208 return ONPLAY_MAINMENU;
1209 default:
1210 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;