Add a browse (remote) custom statusbar item in the theme settings.
[kugel-rb.git] / apps / onplay.c
blob28bf7de8ae434692458d590cca96d5e959169532
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
27 #include "debug.h"
28 #include "sprintf.h"
29 #include "lcd.h"
30 #include "dir.h"
31 #include "file.h"
32 #include "audio.h"
33 #include "menu.h"
34 #include "lang.h"
35 #include "playlist.h"
36 #include "button.h"
37 #include "kernel.h"
38 #include "keyboard.h"
39 #include "mp3data.h"
40 #include "metadata.h"
41 #include "screens.h"
42 #include "tree.h"
43 #include "buffer.h"
44 #include "settings.h"
45 #include "playlist_viewer.h"
46 #include "talk.h"
47 #include "onplay.h"
48 #include "filetypes.h"
49 #include "plugin.h"
50 #include "bookmark.h"
51 #include "action.h"
52 #include "splash.h"
53 #include "yesno.h"
54 #include "menus/exported_menus.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "icons.h"
57 #endif
58 #include "sound_menu.h"
59 #include "playlist_menu.h"
60 #include "playlist_catalog.h"
61 #ifdef HAVE_TAGCACHE
62 #include "tagtree.h"
63 #endif
64 #include "cuesheet.h"
65 #include "backdrop.h"
66 #include "pitchscreen.h"
67 #include "viewport.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_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU), bookmark_menu_callback,
106 Icon_Bookmark, &bookmark_create_menu_item,
107 &bookmark_load_menu_item);
108 static int bookmark_menu_callback(int action,
109 const struct menu_item_ex *this_item)
111 (void)this_item;
112 switch (action)
114 case ACTION_REQUEST_MENUITEM:
115 if (this_item == &bookmark_load_menu_item)
117 if (bookmark_exist() == 0)
118 return ACTION_EXIT_MENUITEM;
120 /* hide the bookmark menu if there is no playback */
121 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
122 return ACTION_EXIT_MENUITEM;
123 break;
124 #ifdef HAVE_LCD_CHARCELLS
125 case ACTION_ENTER_MENUITEM:
126 status_set_param(true);
127 break;
128 #endif
129 case ACTION_EXIT_MENUITEM:
130 #ifdef HAVE_LCD_CHARCELLS
131 status_set_param(false);
132 #endif
133 settings_save();
134 break;
136 return action;
139 static bool list_viewers(void)
141 int ret = filetype_list_viewers(selected_file);
142 if (ret == PLUGIN_USB_CONNECTED)
143 onplay_result = ONPLAY_RELOAD_DIR;
144 return false;
147 static bool shuffle_playlist(void)
149 playlist_sort(NULL, true);
150 playlist_randomise(NULL, current_tick, true);
152 return false;
155 static bool save_playlist(void)
157 save_playlist_screen(NULL);
158 return false;
161 static bool add_to_playlist(int position, bool queue)
163 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
164 const char *lines[] = {
165 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
166 selected_file
168 const struct text_message message={lines, 2};
170 splash(0, ID2P(LANG_WAIT));
172 if (new_playlist)
173 playlist_create(NULL, NULL);
175 /* always set seed before inserting shuffled */
176 if (position == PLAYLIST_INSERT_SHUFFLED)
177 srand(current_tick);
179 #ifdef HAVE_TAGCACHE
180 if (context == CONTEXT_ID3DB)
182 tagtree_insert_selection_playlist(position, queue);
184 else
185 #endif
187 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
188 playlist_insert_track(NULL, selected_file, position, queue, true);
189 else if (selected_file_attr & ATTR_DIRECTORY)
191 bool recurse = false;
193 if (global_settings.recursive_dir_insert != RECURSE_ASK)
194 recurse = (bool)global_settings.recursive_dir_insert;
195 else
197 /* Ask if user wants to recurse directory */
198 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
201 playlist_insert_directory(NULL, selected_file, position, queue,
202 recurse);
204 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
205 playlist_insert_playlist(NULL, selected_file, position, queue);
208 if (new_playlist && (playlist_amount() > 0))
210 /* nothing is currently playing so begin playing what we just
211 inserted */
212 if (global_settings.playlist_shuffle)
213 playlist_shuffle(current_tick, -1);
214 playlist_start(0,0);
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, NULL);
242 static bool cat_add_to_a_new_playlist(void)
244 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
245 true, NULL);
249 static int cat_playlist_callback(int action,
250 const struct menu_item_ex *this_item);
251 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
252 catalog_view_playlists, 0, cat_playlist_callback,
253 Icon_Playlist);
254 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
255 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
256 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
257 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
258 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG), cat_playlist_callback,
259 Icon_Playlist, &cat_view_lists, &cat_add_to_list,
260 &cat_add_to_new);
262 static int cat_playlist_callback(int action,
263 const struct menu_item_ex *this_item)
265 if (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
266 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
267 ((selected_file_attr & ATTR_DIRECTORY) == 0))
269 return ACTION_EXIT_MENUITEM;
272 switch (action)
274 case ACTION_REQUEST_MENUITEM:
275 if (this_item == &cat_view_lists)
277 if (context == CONTEXT_WPS)
278 return action;
280 else if (selected_file && /* set before calling this menu,
281 so safe */
282 ((audio_status() & AUDIO_STATUS_PLAY &&
283 context == CONTEXT_WPS) ||
284 context == CONTEXT_TREE))
286 return action;
288 else
289 return ACTION_EXIT_MENUITEM;
290 break;
292 return action;
296 /* CONTEXT_WPS playlist options */
297 MENUITEM_FUNCTION(playlist_viewer_item, 0,
298 ID2P(LANG_VIEW_DYNAMIC_PLAYLIST), playlist_viewer,
299 NULL, NULL, Icon_Playlist);
300 MENUITEM_FUNCTION(search_playlist_item, 0,
301 ID2P(LANG_SEARCH_IN_PLAYLIST), search_playlist,
302 NULL, NULL, Icon_Playlist);
303 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
304 save_playlist, NULL, NULL, Icon_Playlist);
305 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
306 shuffle_playlist, NULL, NULL, Icon_Playlist);
307 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
308 NULL, Icon_Playlist,
309 &playlist_viewer_item, &search_playlist_item,
310 &playlist_save_item, &reshuffle_item
313 /* CONTEXT_[TREE|ID3DB] playlist options */
314 static int playlist_insert_func(void *param)
316 add_to_playlist((intptr_t)param, false);
317 return 0;
319 static int playlist_queue_func(void *param)
321 add_to_playlist((intptr_t)param, true);
322 return 0;
324 static int treeplaylist_wplayback_callback(int action,
325 const struct menu_item_ex*
326 this_item)
328 (void)this_item;
329 switch (action)
331 case ACTION_REQUEST_MENUITEM:
332 if (audio_status() & AUDIO_STATUS_PLAY)
333 return action;
334 else
335 return ACTION_EXIT_MENUITEM;
336 break;
338 return action;
341 static int treeplaylist_callback(int action,
342 const struct menu_item_ex *this_item);
344 /* insert items */
345 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
346 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
347 treeplaylist_callback, Icon_Playlist);
348 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
349 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
350 treeplaylist_wplayback_callback, Icon_Playlist);
351 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
352 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
353 treeplaylist_wplayback_callback, Icon_Playlist);
354 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
355 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
356 (intptr_t*)PLAYLIST_INSERT_SHUFFLED, treeplaylist_callback,
357 Icon_Playlist);
358 /* queue items */
359 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
360 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
361 treeplaylist_wplayback_callback, Icon_Playlist);
362 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
363 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
364 treeplaylist_wplayback_callback, Icon_Playlist);
365 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
366 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
367 treeplaylist_wplayback_callback, Icon_Playlist);
368 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
369 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
370 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
371 treeplaylist_wplayback_callback, Icon_Playlist);
372 /* replace playlist */
373 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
374 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
375 treeplaylist_wplayback_callback, Icon_Playlist);
376 /* others */
378 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
379 view_playlist, NULL,
380 treeplaylist_callback, Icon_Playlist);
382 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
383 treeplaylist_callback, Icon_Playlist,
385 /* view */
386 &view_playlist_item,
388 /* insert */
389 &i_pl_item, &i_first_pl_item,
390 &i_last_pl_item, &i_shuf_pl_item,
392 /* queue */
393 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
394 &q_shuf_pl_item,
396 /* replace */
397 &replace_pl_item
399 static int treeplaylist_callback(int action,
400 const struct menu_item_ex *this_item)
402 (void)this_item;
403 switch (action)
405 case ACTION_REQUEST_MENUITEM:
406 if (this_item == &tree_playlist_menu)
408 if (((selected_file_attr & FILE_ATTR_MASK) ==
409 FILE_ATTR_AUDIO) ||
410 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
411 (selected_file_attr & ATTR_DIRECTORY))
413 return action;
415 else
416 return ACTION_EXIT_MENUITEM;
418 else if (this_item == &view_playlist_item)
420 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
421 context == CONTEXT_TREE)
422 return action;
423 else
424 return ACTION_EXIT_MENUITEM;
426 else if (this_item == &i_shuf_pl_item)
429 if (audio_status() & AUDIO_STATUS_PLAY)
431 return action;
433 else if ((this_item == &i_shuf_pl_item) &&
434 ((selected_file_attr & ATTR_DIRECTORY) ||
435 ((selected_file_attr & FILE_ATTR_MASK) ==
436 FILE_ATTR_M3U)))
438 return action;
440 return ACTION_EXIT_MENUITEM;
442 break;
444 return action;
447 /* helper function to remove a non-empty directory */
448 static int remove_dir(char* dirname, int len)
450 int result = 0;
451 DIR* dir;
452 int dirlen = strlen(dirname);
454 dir = opendir(dirname);
455 if (!dir)
456 return -1; /* open error */
458 while(true)
460 struct dirent* entry;
461 /* walk through the directory content */
462 entry = readdir(dir);
463 if (!entry)
464 break;
466 dirname[dirlen] ='\0';
467 splash(0, dirname);
469 /* append name to current directory */
470 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
471 if (entry->attribute & ATTR_DIRECTORY)
472 { /* remove a subdirectory */
473 if (!strcmp((char *)entry->d_name, ".") ||
474 !strcmp((char *)entry->d_name, ".."))
475 continue; /* skip these */
477 /* inform the user which dir we're deleting */
479 result = remove_dir(dirname, len); /* recursion */
480 if (result)
481 break; /* or better continue, delete what we can? */
483 else
484 { /* remove a file */
485 draw_slider();
486 result = remove(dirname);
488 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
490 splash(HZ, ID2P(LANG_CANCEL));
491 result = -1;
492 break;
495 closedir(dir);
497 if (!result)
498 { /* remove the now empty directory */
499 dirname[dirlen] = '\0'; /* terminate to original length */
501 result = rmdir(dirname);
504 return result;
508 /* share code for file and directory deletion, saves space */
509 static bool delete_handler(bool is_dir)
511 char file_to_delete[MAX_PATH];
512 strcpy(file_to_delete, selected_file);
514 const char *lines[]={
515 ID2P(LANG_REALLY_DELETE),
516 file_to_delete
518 const char *yes_lines[]={
519 ID2P(LANG_DELETING),
520 file_to_delete
523 const struct text_message message={lines, 2};
524 const struct text_message yes_message={yes_lines, 2};
526 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
527 return false;
529 splash(0, str(LANG_DELETING));
531 int res;
532 if (is_dir)
534 char pathname[MAX_PATH]; /* space to go deep */
535 cpu_boost(true);
536 strlcpy(pathname, file_to_delete, sizeof(pathname));
537 res = remove_dir(pathname, sizeof(pathname));
538 cpu_boost(false);
540 else
541 res = remove(file_to_delete);
543 if (!res)
544 onplay_result = ONPLAY_RELOAD_DIR;
546 return false;
550 static bool delete_file(void)
552 return delete_handler(false);
555 static bool delete_dir(void)
557 return delete_handler(true);
560 #if LCD_DEPTH > 1
561 static bool set_backdrop(void)
563 /* load the image */
564 if(backdrop_load(BACKDROP_MAIN, selected_file)) {
565 splash(HZ, str(LANG_BACKDROP_LOADED));
566 set_file(selected_file, (char *)global_settings.backdrop_file,
567 MAX_FILENAME);
568 backdrop_show(BACKDROP_MAIN);
569 return true;
570 } else {
571 splash(HZ, str(LANG_BACKDROP_FAILED));
572 return false;
575 #endif
577 static bool rename_file(void)
579 char newname[MAX_PATH];
580 char* ptr = strrchr(selected_file, '/') + 1;
581 int pathlen = (ptr - selected_file);
582 strlcpy(newname, selected_file, sizeof(newname));
583 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
584 if (!strlen(newname + pathlen) ||
585 (rename(selected_file, newname) < 0)) {
586 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
587 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
589 else
590 onplay_result = ONPLAY_RELOAD_DIR;
593 return false;
596 static bool create_dir(void)
598 char dirname[MAX_PATH];
599 char *cwd;
600 int rc;
601 int pathlen;
603 cwd = getcwd(NULL, 0);
604 memset(dirname, 0, sizeof dirname);
606 snprintf(dirname, sizeof dirname, "%s/",
607 cwd[1] ? cwd : "");
609 pathlen = strlen(dirname);
610 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
611 if (rc < 0)
612 return false;
614 rc = mkdir(dirname);
615 if (rc < 0) {
616 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
617 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
618 str(LANG_FAILED));
619 } else {
620 onplay_result = ONPLAY_RELOAD_DIR;
623 return true;
626 static bool properties(void)
628 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("properties",
629 selected_file))
630 onplay_result = ONPLAY_RELOAD_DIR;
631 return false;
634 /* Store the current selection in the clipboard */
635 static bool clipboard_clip(bool copy)
637 clipboard_selection[0] = 0;
638 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
639 clipboard_selection_attr = selected_file_attr;
640 clipboard_is_copy = copy;
642 return true;
645 static bool clipboard_cut(void)
647 return clipboard_clip(false);
650 static bool clipboard_copy(void)
652 return clipboard_clip(true);
655 #ifdef HAVE_LCD_BITMAP
656 static void draw_slider(void)
658 int i;
659 FOR_NB_SCREENS(i)
661 struct viewport *vp = &(viewport_get_current_vp())[i];
662 show_busy_slider(&screens[i], vp->x,
663 (vp->y+vp->height)-2*screens[i].getcharheight(),
664 vp->width, 2*screens[i].getcharheight()-1);
665 screens[i].update();
668 #endif
670 /* Paste a file to a new directory. Will overwrite always. */
671 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
673 int src_fd, target_fd;
674 size_t buffersize;
675 ssize_t size, bytesread, byteswritten;
676 char *buffer;
677 bool result = false;
679 if (copy) {
680 /* See if we can get the plugin buffer for the file copy buffer */
681 buffer = (char *) plugin_get_buffer(&buffersize);
682 if (buffer == NULL || buffersize < 512) {
683 /* Not large enough, try for a disk sector worth of stack
684 instead */
685 buffersize = 512;
686 buffer = (char *) __builtin_alloca(buffersize);
689 if (buffer == NULL) {
690 return false;
693 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
694 size */
696 src_fd = open(src, O_RDONLY);
698 if (src_fd >= 0) {
699 target_fd = creat(target);
701 if (target_fd >= 0) {
702 result = true;
704 size = filesize(src_fd);
706 if (size == -1) {
707 result = false;
710 while(size > 0) {
711 bytesread = read(src_fd, buffer, buffersize);
713 if (bytesread == -1) {
714 result = false;
715 break;
718 size -= bytesread;
720 while(bytesread > 0) {
721 byteswritten = write(target_fd, buffer, bytesread);
723 if (byteswritten < 0) {
724 result = false;
725 size = 0;
726 break;
729 bytesread -= byteswritten;
730 draw_slider();
734 close(target_fd);
736 /* Copy failed. Cleanup. */
737 if (!result) {
738 remove(target);
742 close(src_fd);
744 } else {
745 result = rename(src, target) == 0;
746 #ifdef HAVE_MULTIVOLUME
747 if (!result) {
748 if (errno == EXDEV) {
749 /* Failed because cross volume rename doesn't work. Copy
750 instead */
751 result = clipboard_pastefile(src, target, true);
753 if (result) {
754 result = remove(src) == 0;
758 #endif
761 return result;
764 /* Paste a directory to a new location. Designed to be called by
765 clipboard_paste */
766 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
767 int targetlen, bool copy)
769 DIR *srcdir;
770 int srcdirlen = strlen(src);
771 int targetdirlen = strlen(target);
772 bool result = true;
774 if (!file_exists(target)) {
775 if (!copy) {
776 /* Just move the directory */
777 result = rename(src, target) == 0;
779 #ifdef HAVE_MULTIVOLUME
780 if (!result && errno == EXDEV) {
781 /* Try a copy as we're going across devices */
782 result = clipboard_pastedirectory(src, srclen, target,
783 targetlen, true);
785 /* If it worked, remove the source directory */
786 if (result) {
787 remove_dir(src, srclen);
790 #endif
791 return result;
792 } else {
793 /* Make a directory to copy things to */
794 result = mkdir(target) == 0;
798 /* Check if something went wrong already */
799 if (!result) {
800 return result;
803 srcdir = opendir(src);
804 if (!srcdir) {
805 return false;
808 /* This loop will exit as soon as there's a problem */
809 while(result)
811 struct dirent* entry;
812 /* walk through the directory content */
813 entry = readdir(srcdir);
814 if (!entry)
815 break;
817 /* append name to current directory */
818 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
819 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
820 entry->d_name);
822 DEBUGF("Copy %s to %s\n", src, target);
824 if (entry->attribute & ATTR_DIRECTORY)
825 { /* copy/move a subdirectory */
826 if (!strcmp((char *)entry->d_name, ".") ||
827 !strcmp((char *)entry->d_name, ".."))
828 continue; /* skip these */
830 result = clipboard_pastedirectory(src, srclen, target, targetlen,
831 copy); /* recursion */
833 else
834 { /* copy/move a file */
835 result = clipboard_pastefile(src, target, copy);
839 closedir(srcdir);
841 if (result) {
842 src[srcdirlen] = '\0'; /* terminate to original length */
843 target[targetdirlen] = '\0'; /* terminate to original length */
846 return result;
849 /* Paste the clipboard to the current directory */
850 static bool clipboard_paste(void)
852 char target[MAX_PATH];
853 char *cwd, *nameptr;
854 bool success;
856 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
857 static const struct text_message message={lines, 1};
859 /* Get the name of the current directory */
860 cwd = getcwd(NULL, 0);
862 /* Figure out the name of the selection */
863 nameptr = strrchr(clipboard_selection, '/');
865 /* Final target is current directory plus name of selection */
866 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
868 /* If the target existed but they choose not to overwite, exit */
869 if (file_exists(target) &&
870 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
871 return false;
874 if (clipboard_is_copy) {
875 splash(0, ID2P(LANG_COPYING));
877 else
879 splash(0, ID2P(LANG_MOVING));
882 /* Now figure out what we're doing */
883 cpu_boost(true);
884 if (clipboard_selection_attr & ATTR_DIRECTORY) {
885 /* Recursion. Set up external stack */
886 char srcpath[MAX_PATH];
887 char targetpath[MAX_PATH];
888 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
890 /* Do not allow the user to paste a directory into a dir they are
891 copying */
892 success = 0;
894 else
896 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
897 strlcpy(targetpath, target, sizeof(targetpath));
899 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
900 target, sizeof(targetpath), clipboard_is_copy);
902 if (success && !clipboard_is_copy)
904 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
905 remove_dir(srcpath, sizeof(srcpath));
908 } else {
909 success = clipboard_pastefile(clipboard_selection, target,
910 clipboard_is_copy);
912 cpu_boost(false);
914 /* Did it work? */
915 if (success) {
916 /* Reset everything */
917 clipboard_selection[0] = 0;
918 clipboard_selection_attr = 0;
919 clipboard_is_copy = false;
921 /* Force reload of the current directory */
922 onplay_result = ONPLAY_RELOAD_DIR;
923 } else {
924 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
925 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
926 str(LANG_FAILED));
929 return true;
932 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
933 #ifdef HAVE_TAGCACHE
934 static int set_rating_inline(void)
936 struct mp3entry* id3 = audio_current_track();
937 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
939 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
940 NULL, 1, 0, 10, NULL, NULL);
941 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
943 else
944 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
945 return 0;
947 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
949 (void)this_item;
950 switch (action)
952 case ACTION_REQUEST_MENUITEM:
953 if (!selected_file || !global_settings.runtimedb ||
954 !tagcache_is_usable())
955 return ACTION_EXIT_MENUITEM;
956 break;
958 return action;
960 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
961 set_rating_inline, NULL,
962 ratingitem_callback, Icon_Questionmark);
963 #endif
965 static bool view_cue(void)
967 struct mp3entry* id3 = audio_current_track();
968 if(id3 && id3->cuesheet)
970 browse_cuesheet(id3->cuesheet);
972 return false;
974 static int view_cue_item_callback(int action,
975 const struct menu_item_ex *this_item)
977 (void)this_item;
978 struct mp3entry* id3 = audio_current_track();
979 switch (action)
981 case ACTION_REQUEST_MENUITEM:
982 if (!selected_file
983 || !id3 || !id3->cuesheet)
984 return ACTION_EXIT_MENUITEM;
985 break;
987 return action;
989 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
990 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
992 /* CONTEXT_WPS items */
993 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
994 browse_id3, NULL, NULL, Icon_NOICON);
995 #ifdef HAVE_PITCHSCREEN
996 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
997 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
998 #endif
1000 /* CONTEXT_[TREE|ID3DB] items */
1001 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
1002 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
1003 rename_file, NULL, clipboard_callback, Icon_NOICON);
1004 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
1005 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
1006 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
1007 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
1008 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
1009 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
1010 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
1011 delete_file, NULL, clipboard_callback, Icon_NOICON);
1012 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
1013 delete_dir, NULL, clipboard_callback, Icon_NOICON);
1014 MENUITEM_FUNCTION(properties_item, 0, ID2P(LANG_PROPERTIES),
1015 properties, NULL, clipboard_callback, Icon_NOICON);
1016 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
1017 create_dir, NULL, clipboard_callback, Icon_NOICON);
1018 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1019 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1020 #if LCD_DEPTH > 1
1021 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1022 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1023 #endif
1024 #ifdef HAVE_RECORDING
1025 static bool set_recdir(void)
1027 strlcpy(global_settings.rec_directory,
1028 selected_file, MAX_FILENAME+1);
1029 settings_save();
1030 return false;
1032 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1033 set_recdir, NULL, clipboard_callback, Icon_Recording);
1034 #endif
1035 static bool add_to_faves(void)
1037 if(PLUGIN_USB_CONNECTED == filetype_load_plugin("shortcuts_append",
1038 selected_file))
1039 onplay_result = ONPLAY_RELOAD_DIR;
1040 return false;
1042 MENUITEM_FUNCTION(add_to_faves_item, 0, ID2P(LANG_ADD_TO_FAVES),
1043 add_to_faves, NULL, clipboard_callback, Icon_NOICON);
1046 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1048 switch (action)
1050 case ACTION_REQUEST_MENUITEM:
1051 #ifdef HAVE_MULTIVOLUME
1052 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1053 (this_item == &rename_file_item ||
1054 this_item == &delete_dir_item ||
1055 this_item == &clipboard_cut_item) )
1056 return ACTION_EXIT_MENUITEM;
1057 #endif
1058 if (context == CONTEXT_ID3DB)
1059 return ACTION_EXIT_MENUITEM;
1060 if (this_item == &clipboard_paste_item)
1061 { /* visible if there is something to paste */
1062 return (clipboard_selection[0] != 0) ?
1063 action : ACTION_EXIT_MENUITEM;
1065 else if (this_item == &create_dir_item)
1067 /* always visible */
1068 return action;
1070 else if ((this_item == &properties_item) ||
1071 (this_item == &rename_file_item) ||
1072 (this_item == &clipboard_cut_item) ||
1073 (this_item == &clipboard_copy_item) ||
1074 (this_item == &add_to_faves_item)
1077 /* requires an actual file */
1078 return (selected_file) ? action : ACTION_EXIT_MENUITEM;
1080 #if LCD_DEPTH > 1
1081 else if (this_item == &set_backdrop_item)
1083 if (selected_file)
1085 char *suffix = strrchr(selected_file, '.');
1086 if (suffix)
1088 if (strcasecmp(suffix, ".bmp") == 0)
1090 return action;
1094 return ACTION_EXIT_MENUITEM;
1096 #endif
1097 else if ((selected_file_attr & ATTR_DIRECTORY))
1099 if ((this_item == &delete_dir_item)
1101 return action;
1102 #ifdef HAVE_RECORDING
1103 else if (this_item == &set_recdir_item)
1104 return action;
1105 #endif
1107 else if (selected_file
1108 #ifdef HAVE_MULTIVOLUME
1109 /* no rename+delete for volumes */
1110 && !(selected_file_attr & ATTR_VOLUME)
1111 #endif
1114 if ((this_item == &delete_file_item) ||
1115 (this_item == &list_viewers_item))
1117 return action;
1120 return ACTION_EXIT_MENUITEM;
1121 break;
1123 return action;
1125 /* used when onplay() is called in the CONTEXT_WPS context */
1128 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1129 onplaymenu_callback, Icon_Audio,
1130 &wps_playlist_menu, &cat_playlist_menu,
1131 &sound_settings, &playback_settings,
1132 #ifdef HAVE_TAGCACHE
1133 &rating_item,
1134 #endif
1135 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1136 &delete_file_item, &view_cue_item,
1137 #ifdef HAVE_PITCHSCREEN
1138 &pitch_screen_item,
1139 #endif
1141 /* used when onplay() is not called in the CONTEXT_WPS context */
1142 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1143 onplaymenu_callback, Icon_file_view_menu,
1144 &tree_playlist_menu, &cat_playlist_menu,
1145 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1146 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1147 #if LCD_DEPTH > 1
1148 &set_backdrop_item,
1149 #endif
1150 &list_viewers_item, &create_dir_item, &properties_item,
1151 #ifdef HAVE_RECORDING
1152 &set_recdir_item,
1153 #endif
1154 &add_to_faves_item,
1156 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1158 (void)this_item;
1159 switch (action)
1161 case ACTION_TREE_STOP:
1162 if (this_item == &wps_onplay_menu)
1164 list_stop_handler();
1165 return ACTION_STD_CANCEL;
1167 break;
1168 case ACTION_EXIT_MENUITEM:
1169 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1170 break;
1172 return action;
1174 int onplay(char* file, int attr, int from)
1176 const struct menu_item_ex *menu;
1177 onplay_result = ONPLAY_OK;
1178 context = from;
1179 selected_file = file;
1180 selected_file_attr = attr;
1181 if (context == CONTEXT_WPS)
1182 menu = &wps_onplay_menu;
1183 else
1184 menu = &tree_onplay_menu;
1185 switch (do_menu(menu, NULL, NULL, false))
1187 case GO_TO_WPS:
1188 return ONPLAY_START_PLAY;
1189 case GO_TO_ROOT:
1190 case GO_TO_MAINMENU:
1191 return ONPLAY_MAINMENU;
1192 default:
1193 return context == CONTEXT_WPS ? ONPLAY_OK : ONPLAY_RELOAD_DIR;