when changing settings from the Talk and Voice window also update the main widgets...
[Rockbox.git] / apps / onplay.c
blob439220c7115742e0385a54d6f0a58254085a9126
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"
68 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
69 #include "backdrop.h"
70 #endif
72 static int context;
73 static char* selected_file = NULL;
74 static int selected_file_attr = 0;
75 static int onplay_result = ONPLAY_OK;
76 static char clipboard_selection[MAX_PATH];
77 static int clipboard_selection_attr = 0;
78 static bool clipboard_is_copy = false;
80 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
81 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
82 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
83 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
84 static const struct menu_item_ex name = \
85 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
86 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
87 { (void*)name##_},{.callback_and_desc = & name##__}};
90 /* ----------------------------------------------------------------------- */
91 /* Displays the bookmark menu options for the user to decide. This is an */
92 /* interface function. */
93 /* ----------------------------------------------------------------------- */
95 static int bookmark_menu_callback(int action,
96 const struct menu_item_ex *this_item);
97 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
98 ID2P(LANG_BOOKMARK_MENU_CREATE),
99 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
100 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
101 ID2P(LANG_BOOKMARK_MENU_LIST),
102 bookmark_load_menu, NULL,
103 bookmark_menu_callback, Icon_Bookmark);
104 MAKE_MENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
105 Icon_Bookmark, &bookmark_create_menu_item, &bookmark_load_menu_item);
106 static int bookmark_menu_callback(int action,
107 const struct menu_item_ex *this_item)
109 (void)this_item;
110 switch (action)
112 case ACTION_REQUEST_MENUITEM:
113 if (this_item == &bookmark_load_menu_item)
115 if (bookmark_exist() == 0)
116 return ACTION_EXIT_MENUITEM;
118 /* hide the bookmark menu if there is no playback */
119 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
120 return ACTION_EXIT_MENUITEM;
121 break;
122 #ifdef HAVE_LCD_CHARCELLS
123 case ACTION_ENTER_MENUITEM:
124 status_set_param(true);
125 break;
126 #endif
127 case ACTION_EXIT_MENUITEM:
128 #ifdef HAVE_LCD_CHARCELLS
129 status_set_param(false);
130 #endif
131 settings_save();
132 break;
134 return action;
137 static bool list_viewers(void)
139 int ret = filetype_list_viewers(selected_file);
140 if (ret == PLUGIN_USB_CONNECTED)
141 onplay_result = ONPLAY_RELOAD_DIR;
142 return false;
145 static bool shuffle_playlist(void)
147 playlist_sort(NULL, true);
148 playlist_randomise(NULL, current_tick, true);
150 return false;
153 static bool save_playlist(void)
155 save_playlist_screen(NULL);
156 return false;
159 static bool add_to_playlist(int position, bool queue)
161 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
162 char *lines[] = {
163 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
164 selected_file
166 struct text_message message={lines, 2};
168 gui_syncsplash(0, ID2P(LANG_WAIT));
170 if (new_playlist)
171 playlist_create(NULL, NULL);
173 /* always set seed before inserting shuffled */
174 if (position == PLAYLIST_INSERT_SHUFFLED)
175 srand(current_tick);
177 #ifdef HAVE_TAGCACHE
178 if (context == CONTEXT_ID3DB)
180 tagtree_insert_selection_playlist(position, queue);
182 else
183 #endif
185 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
186 playlist_insert_track(NULL, selected_file, position, queue, true);
187 else if (selected_file_attr & ATTR_DIRECTORY)
189 bool recurse = false;
191 if (global_settings.recursive_dir_insert != RECURSE_ASK)
192 recurse = (bool)global_settings.recursive_dir_insert;
193 else
195 /* Ask if user wants to recurse directory */
196 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
199 playlist_insert_directory(NULL, selected_file, position, queue,
200 recurse);
202 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
203 playlist_insert_playlist(NULL, selected_file, position, queue);
206 if (new_playlist && (playlist_amount() > 0))
208 /* nothing is currently playing so begin playing what we just
209 inserted */
210 if (global_settings.playlist_shuffle)
211 playlist_shuffle(current_tick, -1);
212 playlist_start(0,0);
213 gui_syncstatusbar_draw(&statusbars, false);
214 onplay_result = ONPLAY_START_PLAY;
217 return false;
220 static bool view_playlist(void)
222 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
223 bool result;
225 result = playlist_viewer_ex(selected_file);
227 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
228 onplay_result == ONPLAY_OK)
229 /* playlist was started from viewer */
230 onplay_result = ONPLAY_START_PLAY;
232 return result;
235 static bool cat_add_to_a_playlist(void)
237 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
238 false);
241 static bool cat_add_to_a_new_playlist(void)
243 return catalog_add_to_a_playlist(selected_file, selected_file_attr, true);
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_MENU( cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
257 Icon_Playlist, &cat_view_lists,
258 &cat_add_to_list, &cat_add_to_new );
260 static int cat_playlist_callback(int action,
261 const struct menu_item_ex *this_item)
263 switch (action)
265 case ACTION_REQUEST_MENUITEM:
266 if (this_item == &cat_view_lists)
268 if (context == CONTEXT_WPS)
269 return action;
271 else if (selected_file && /* set before calling this menu,
272 so safe */
273 ((audio_status() & AUDIO_STATUS_PLAY &&
274 context == CONTEXT_WPS) ||
275 context == CONTEXT_TREE))
277 return action;
279 else
280 return ACTION_EXIT_MENUITEM;
281 break;
283 return action;
287 /* CONTEXT_WPS playlist options */
288 MENUITEM_FUNCTION(playlist_viewer_item, 0,
289 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
290 NULL, NULL, Icon_Playlist);
291 MENUITEM_FUNCTION(search_playlist_item, 0,
292 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
293 NULL, NULL, Icon_Playlist);
294 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
295 save_playlist, NULL, NULL, Icon_Playlist);
296 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
297 shuffle_playlist, NULL, NULL, Icon_Playlist);
298 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
299 NULL, Icon_Playlist,
300 &playlist_viewer_item, &search_playlist_item,
301 &playlist_save_item, &reshuffle_item
304 /* CONTEXT_[TREE|ID3DB] playlist options */
305 static int playlist_insert_func(void *param)
307 add_to_playlist((intptr_t)param, false);
308 return 0;
310 static int playlist_queue_func(void *param)
312 add_to_playlist((intptr_t)param, true);
313 return 0;
315 static int treeplaylist_wplayback_callback(int action,
316 const struct menu_item_ex*
317 this_item)
319 (void)this_item;
320 switch (action)
322 case ACTION_REQUEST_MENUITEM:
323 if (audio_status() & AUDIO_STATUS_PLAY)
324 return action;
325 else
326 return ACTION_EXIT_MENUITEM;
327 break;
329 return action;
332 static int treeplaylist_callback(int action,
333 const struct menu_item_ex *this_item);
335 /* insert items */
336 MENUITEM_FUNCTION(i_pl_item_no_play, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
337 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
338 treeplaylist_callback, Icon_Playlist);
339 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
340 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
341 treeplaylist_wplayback_callback, Icon_Playlist);
342 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
343 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
344 treeplaylist_wplayback_callback, Icon_Playlist);
345 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
346 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
347 treeplaylist_wplayback_callback, Icon_Playlist);
348 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
349 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
350 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
351 Icon_Playlist);
352 /* queue items */
353 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
354 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
355 treeplaylist_wplayback_callback, Icon_Playlist);
356 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
357 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
358 treeplaylist_wplayback_callback, Icon_Playlist);
359 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
360 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
361 treeplaylist_wplayback_callback, Icon_Playlist);
362 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
363 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
364 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
365 treeplaylist_wplayback_callback, Icon_Playlist);
366 /* replace playlist */
367 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
368 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
369 treeplaylist_wplayback_callback, Icon_Playlist);
370 /* others */
372 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
373 view_playlist, NULL,
374 treeplaylist_callback, Icon_Playlist);
376 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
377 treeplaylist_callback, Icon_Playlist,
379 /* view */
380 &view_playlist_item,
382 /* insert */
383 &i_pl_item_no_play, &i_pl_item, &i_first_pl_item,
384 &i_last_pl_item, &i_shuf_pl_item,
386 /* queue */
387 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
388 &q_shuf_pl_item,
390 /* replace */
391 &replace_pl_item
393 static int treeplaylist_callback(int action,
394 const struct menu_item_ex *this_item)
396 (void)this_item;
397 switch (action)
399 case ACTION_REQUEST_MENUITEM:
400 if (this_item == &tree_playlist_menu)
402 if (((selected_file_attr & FILE_ATTR_MASK) ==
403 FILE_ATTR_AUDIO) ||
404 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
405 (selected_file_attr & ATTR_DIRECTORY))
407 return action;
409 else
410 return ACTION_EXIT_MENUITEM;
412 else if (this_item == &view_playlist_item)
414 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
415 context == CONTEXT_TREE)
416 return action;
417 else
418 return ACTION_EXIT_MENUITEM;
420 else if (this_item == &i_pl_item_no_play)
422 if (!(audio_status() & AUDIO_STATUS_PLAY))
424 return action;
426 else
427 return ACTION_EXIT_MENUITEM;
429 else if (this_item == &i_shuf_pl_item)
432 if (audio_status() & AUDIO_STATUS_PLAY)
434 return action;
436 else if ((this_item == &i_shuf_pl_item) &&
437 ((selected_file_attr & ATTR_DIRECTORY) ||
438 ((selected_file_attr & FILE_ATTR_MASK) ==
439 FILE_ATTR_M3U)))
441 return action;
443 return ACTION_EXIT_MENUITEM;
445 break;
447 return action;
450 /* helper function to remove a non-empty directory */
451 static int remove_dir(char* dirname, int len)
453 int result = 0;
454 DIR* dir;
455 int dirlen = strlen(dirname);
456 int i;
458 dir = opendir(dirname);
459 if (!dir)
460 return -1; /* open error */
462 while(true)
464 struct dirent* entry;
465 /* walk through the directory content */
466 entry = readdir(dir);
467 if (!entry)
468 break;
470 dirname[dirlen] ='\0';
471 FOR_NB_SCREENS(i)
472 screens[i].puts(0,1,dirname);
474 /* append name to current directory */
475 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
476 if (entry->attribute & ATTR_DIRECTORY)
477 { /* remove a subdirectory */
478 if (!strcmp((char *)entry->d_name, ".") ||
479 !strcmp((char *)entry->d_name, ".."))
480 continue; /* skip these */
482 /* inform the user which dir we're deleting */
484 result = remove_dir(dirname, len); /* recursion */
485 if (result)
486 break; /* or better continue, delete what we can? */
488 else
489 { /* remove a file */
490 #ifdef HAVE_LCD_BITMAP
491 FOR_NB_SCREENS(i)
493 show_busy_slider(&screens[i], 2, 3*screens[i].char_height,
494 LCD_WIDTH-4, screens[i].char_height);
496 #endif
497 result = remove(dirname);
499 #ifdef HAVE_LCD_BITMAP
500 FOR_NB_SCREENS(i)
501 screens[i].update();
502 #endif
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 inline void draw_slider(void)
674 int i;
675 FOR_NB_SCREENS(i)
677 show_busy_slider(&screens[i], 2, LCD_HEIGHT/4,
678 LCD_WIDTH-4, screens[i].char_height);
679 screens[i].update();
682 #else
683 #define draw_slider()
684 #endif
686 /* Paste a file to a new directory. Will overwrite always. */
687 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
689 int src_fd, target_fd;
690 size_t buffersize;
691 ssize_t size, bytesread, byteswritten;
692 char *buffer;
693 bool result = false;
695 if (copy) {
696 /* See if we can get the plugin buffer for the file copy buffer */
697 buffer = (char *) plugin_get_buffer(&buffersize);
698 if (buffer == NULL || buffersize < 512) {
699 /* Not large enough, try for a disk sector worth of stack
700 instead */
701 buffersize = 512;
702 buffer = (char *) __builtin_alloca(buffersize);
705 if (buffer == NULL) {
706 return false;
709 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
710 size */
712 src_fd = open(src, O_RDONLY);
714 if (src_fd >= 0) {
715 target_fd = creat(target);
717 if (target_fd >= 0) {
718 result = true;
720 size = filesize(src_fd);
722 if (size == -1) {
723 result = false;
726 while(size > 0) {
727 bytesread = read(src_fd, buffer, buffersize);
729 if (bytesread == -1) {
730 result = false;
731 break;
734 size -= bytesread;
736 while(bytesread > 0) {
737 byteswritten = write(target_fd, buffer, bytesread);
739 if (byteswritten == -1) {
740 result = false;
741 size = 0;
742 break;
745 bytesread -= byteswritten;
746 draw_slider();
750 close(target_fd);
752 /* Copy failed. Cleanup. */
753 if (!result) {
754 remove(target);
758 close(src_fd);
760 } else {
761 result = rename(src, target) == 0;
762 #ifdef HAVE_MULTIVOLUME
763 if (!result) {
764 if (errno == EXDEV) {
765 /* Failed because cross volume rename doesn't work. Copy
766 instead */
767 result = clipboard_pastefile(src, target, true);
769 if (result) {
770 result = remove(src) == 0;
774 #endif
777 return result;
780 /* Paste a directory to a new location. Designed to be called by
781 clipboard_paste */
782 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
783 int targetlen, bool copy)
785 DIR *srcdir;
786 int srcdirlen = strlen(src);
787 int targetdirlen = strlen(target);
788 int fd;
789 bool result = true;
791 /* Check if the target exists */
792 fd = open(target, O_RDONLY);
793 close(fd);
795 if (fd < 0) {
796 if (!copy) {
797 /* Just move the directory */
798 result = rename(src, target) == 0;
800 #ifdef HAVE_MULTIVOLUME
801 if (!result && errno == EXDEV) {
802 /* Try a copy as we're going across devices */
803 result = clipboard_pastedirectory(src, srclen, target,
804 targetlen, true);
806 /* If it worked, remove the source directory */
807 if (result) {
808 remove_dir(src, srclen);
811 #endif
812 return result;
813 } else {
814 /* Make a directory to copy things to */
815 result = mkdir(target) == 0;
819 /* Check if something went wrong already */
820 if (!result) {
821 return result;
824 srcdir = opendir(src);
825 if (!srcdir) {
826 return false;
829 /* This loop will exit as soon as there's a problem */
830 while(result)
832 struct dirent* entry;
833 /* walk through the directory content */
834 entry = readdir(srcdir);
835 if (!entry)
836 break;
838 /* append name to current directory */
839 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
840 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
841 entry->d_name);
843 DEBUGF("Copy %s to %s\n", src, target);
845 if (entry->attribute & ATTR_DIRECTORY)
846 { /* copy/move a subdirectory */
847 if (!strcmp((char *)entry->d_name, ".") ||
848 !strcmp((char *)entry->d_name, ".."))
849 continue; /* skip these */
851 result = clipboard_pastedirectory(src, srclen, target, targetlen,
852 copy); /* recursion */
854 else
855 { /* copy/move a file */
856 result = clipboard_pastefile(src, target, copy);
860 closedir(srcdir);
862 if (result) {
863 src[srcdirlen] = '\0'; /* terminate to original length */
864 target[targetdirlen] = '\0'; /* terminate to original length */
867 return result;
870 /* Paste the clipboard to the current directory */
871 static bool clipboard_paste(void)
873 char target[MAX_PATH];
874 char *cwd, *nameptr;
875 bool success;
876 int target_fd;
878 unsigned char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
879 struct text_message message={(char **)lines, 1};
881 /* Get the name of the current directory */
882 cwd = getcwd(NULL, 0);
884 /* Figure out the name of the selection */
885 nameptr = strrchr(clipboard_selection, '/');
887 /* Final target is current directory plus name of selection */
888 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
890 /* Check if we're going to overwrite */
891 target_fd = open(target, O_RDONLY);
892 close(target_fd);
894 /* If the target existed but they choose not to overwite, exit */
895 if (target_fd >= 0 &&
896 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
897 return false;
900 if (clipboard_is_copy) {
901 gui_syncsplash(0, ID2P(LANG_COPYING));
903 else
905 gui_syncsplash(0, ID2P(LANG_MOVING));
908 /* Now figure out what we're doing */
909 cpu_boost(true);
910 if (clipboard_selection_attr & ATTR_DIRECTORY) {
911 /* Recursion. Set up external stack */
912 char srcpath[MAX_PATH];
913 char targetpath[MAX_PATH];
914 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
916 /* Do not allow the user to paste a directory into a dir they are
917 copying */
918 success = 0;
920 else
922 strncpy(srcpath, clipboard_selection, sizeof srcpath);
923 strncpy(targetpath, target, sizeof targetpath);
925 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
926 target, sizeof(targetpath), clipboard_is_copy);
928 } else {
929 success = clipboard_pastefile(clipboard_selection, target,
930 clipboard_is_copy);
932 cpu_boost(false);
934 /* Did it work? */
935 if (success) {
936 /* Reset everything */
937 clipboard_selection[0] = 0;
938 clipboard_selection_attr = 0;
939 clipboard_is_copy = false;
941 /* Force reload of the current directory */
942 onplay_result = ONPLAY_RELOAD_DIR;
943 } else {
944 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
945 gui_syncsplash(HZ, (unsigned char *)"%s %s",
946 str(LANG_PASTE), str(LANG_FAILED));
949 return true;
952 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
953 #ifdef HAVE_TAGCACHE
954 static int set_rating_inline(void)
956 struct mp3entry* id3 = audio_current_track();
957 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
959 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
960 NULL, 1, 0, 10, NULL, NULL);
961 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
963 else
964 gui_syncsplash(HZ*2, ID2P(LANG_ID3_NO_INFO));
965 return 0;
967 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
969 (void)this_item;
970 switch (action)
972 case ACTION_REQUEST_MENUITEM:
973 if (!selected_file || !global_settings.runtimedb)
974 return ACTION_EXIT_MENUITEM;
975 break;
977 return action;
979 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
980 set_rating_inline, NULL,
981 ratingitem_callback, Icon_Questionmark);
982 #endif
984 static bool view_cue(void)
986 struct mp3entry* id3 = audio_current_track();
987 if(id3 && cuesheet_is_enabled() && id3->cuesheet_type)
989 browse_cuesheet(curr_cue);
991 return false;
993 static int view_cue_item_callback(int action,
994 const struct menu_item_ex *this_item)
996 (void)this_item;
997 struct mp3entry* id3 = audio_current_track();
998 switch (action)
1000 case ACTION_REQUEST_MENUITEM:
1001 if (!selected_file || !cuesheet_is_enabled()
1002 || !id3 || !id3->cuesheet_type)
1003 return ACTION_EXIT_MENUITEM;
1004 break;
1006 return action;
1008 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
1009 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
1011 /* CONTEXT_WPS items */
1012 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
1013 browse_id3, NULL, NULL, Icon_NOICON);
1014 #ifdef HAVE_PITCHSCREEN
1015 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
1016 pitch_screen, NULL, NULL, Icon_Audio);
1017 #endif
1018 #if CONFIG_CODEC == SWCODEC
1019 MENUITEM_FUNCTION(eq_menu_graphical_item, 0, ID2P(LANG_EQUALIZER_GRAPHICAL),
1020 eq_menu_graphical, NULL, NULL, Icon_Audio);
1021 MENUITEM_FUNCTION(eq_browse_presets_item, 0, ID2P(LANG_EQUALIZER_BROWSE),
1022 eq_browse_presets, NULL, NULL, Icon_Audio);
1023 #endif
1025 /* CONTEXT_[TREE|ID3DB] items */
1026 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1027 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1028 rename_file, NULL, clipboard_callback, Icon_NOICON);
1029 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1030 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1031 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1032 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1033 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1034 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1035 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1036 delete_file, NULL, clipboard_callback, Icon_NOICON);
1037 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1038 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1039 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1040 properties, NULL, clipboard_callback, Icon_NOICON);
1041 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1042 create_dir, NULL, clipboard_callback, Icon_NOICON);
1043 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1044 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1045 #if LCD_DEPTH > 1
1046 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1047 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1048 #endif
1049 #ifdef HAVE_RECORDING
1050 static bool set_recdir(void)
1052 strncpy(global_settings.rec_directory,
1053 selected_file, MAX_FILENAME+1);
1054 settings_save();
1055 return false;
1057 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1058 set_recdir, NULL, clipboard_callback, Icon_Recording);
1059 #endif
1060 static bool add_to_faves(void)
1062 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1063 selected_file))
1064 onplay_result = ONPLAY_RELOAD_DIR;
1065 return false;
1067 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1068 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1071 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1073 switch (action)
1075 case ACTION_REQUEST_MENUITEM:
1076 if (context == CONTEXT_ID3DB)
1077 return ACTION_EXIT_MENUITEM;
1078 if (this_item == &clipboard_paste_item)
1079 { /* visible if there is something to paste */
1080 return (clipboard_selection[0] != 0) ?
1081 action : ACTION_EXIT_MENUITEM;
1083 else if ((this_item == &create_dir_item) ||
1084 (this_item == &properties_item) ||
1085 (this_item == &rename_file_item) ||
1086 (this_item == &clipboard_cut_item) ||
1087 (this_item == &clipboard_copy_item) ||
1088 (this_item == &add_to_faves_item)
1091 /* always visible */
1092 return action;
1094 #if LCD_DEPTH > 1
1095 else if (this_item == &set_backdrop_item)
1097 if (selected_file)
1099 char *suffix = strrchr(selected_file, '.');
1100 if (suffix)
1102 if (strcasecmp(suffix, ".bmp") == 0)
1104 return action;
1108 return ACTION_EXIT_MENUITEM;
1110 #endif
1111 else if ((selected_file_attr & ATTR_DIRECTORY))
1113 if ((this_item == &delete_dir_item)
1115 return action;
1116 #ifdef HAVE_RECORDING
1117 else if (this_item == &set_recdir_item)
1118 return action;
1119 #endif
1121 else if (selected_file
1122 #ifdef HAVE_MULTIVOLUME
1123 /* no rename+delete for volumes */
1124 && !(selected_file_attr & ATTR_VOLUME)
1125 #endif
1128 if ((this_item == &delete_file_item) ||
1129 (this_item == &list_viewers_item))
1131 return action;
1134 return ACTION_EXIT_MENUITEM;
1135 break;
1137 return action;
1139 /* used when onplay() is called in the CONTEXT_WPS context */
1142 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1143 onplaymenu_callback, Icon_Audio,
1144 &sound_settings, &wps_playlist_menu, &cat_playlist_menu,
1145 #ifdef HAVE_TAGCACHE
1146 &rating_item,
1147 #endif
1148 &bookmark_menu, &browse_id3_item, &delete_file_item, &view_cue_item,
1149 #ifdef HAVE_PITCHSCREEN
1150 &pitch_screen_item,
1151 #endif
1152 #if CONFIG_CODEC == SWCODEC
1153 &eq_menu_graphical_item, &eq_browse_presets_item,
1154 #endif
1156 /* used when onplay() is not called in the CONTEXT_WPS context */
1157 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1158 onplaymenu_callback, Icon_file_view_menu,
1159 &tree_playlist_menu, &cat_playlist_menu,
1160 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1161 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1162 #if LCD_DEPTH > 1
1163 &set_backdrop_item,
1164 #endif
1165 &list_viewers_item, &create_dir_item, &properties_item,
1166 #ifdef HAVE_RECORDING
1167 &set_recdir_item,
1168 #endif
1169 &add_to_faves_item,
1171 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1173 (void)this_item;
1174 switch (action)
1176 case ACTION_TREE_STOP:
1177 if (this_item == &wps_onplay_menu)
1179 list_stop_handler();
1180 return ACTION_STD_CANCEL;
1182 break;
1183 case ACTION_EXIT_MENUITEM:
1184 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1185 break;
1187 return action;
1189 int onplay(char* file, int attr, int from)
1191 const struct menu_item_ex *menu;
1192 onplay_result = ONPLAY_OK;
1193 context = from;
1194 selected_file = file;
1195 selected_file_attr = attr;
1196 if (context == CONTEXT_WPS)
1197 menu = &wps_onplay_menu;
1198 else
1199 menu = &tree_onplay_menu;
1200 switch (do_menu(menu, NULL))
1202 case GO_TO_WPS:
1203 return ONPLAY_START_PLAY;
1204 case GO_TO_ROOT:
1205 case GO_TO_MAINMENU:
1206 return ONPLAY_MAINMENU;
1207 default:
1208 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;