Fix strcpy from a user-supplied string to fixed size string.
[kugel-rb.git] / apps / onplay.c
blobf9c75ab253f855bc369d1e788fc138f02ea5986f
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 "settings.h"
44 #include "playlist_viewer.h"
45 #include "talk.h"
46 #include "onplay.h"
47 #include "filetypes.h"
48 #include "plugin.h"
49 #include "bookmark.h"
50 #include "action.h"
51 #include "splash.h"
52 #include "yesno.h"
53 #include "menus/exported_menus.h"
54 #ifdef HAVE_LCD_BITMAP
55 #include "icons.h"
56 #endif
57 #include "sound_menu.h"
58 #include "playlist_menu.h"
59 #include "playlist_catalog.h"
60 #ifdef HAVE_TAGCACHE
61 #include "tagtree.h"
62 #endif
63 #include "cuesheet.h"
64 #include "statusbar-skinned.h"
65 #include "pitchscreen.h"
66 #include "viewport.h"
68 static int context;
69 static char* selected_file = NULL;
70 static int selected_file_attr = 0;
71 static int onplay_result = ONPLAY_OK;
72 static char clipboard_selection[MAX_PATH];
73 static int clipboard_selection_attr = 0;
74 static bool clipboard_is_copy = false;
76 /* redefine MAKE_MENU so the MENU_EXITAFTERTHISMENU flag can be added easily */
77 #define MAKE_ONPLAYMENU( name, str, callback, icon, ... ) \
78 static const struct menu_item_ex *name##_[] = {__VA_ARGS__}; \
79 static const struct menu_callback_with_desc name##__ = {callback,str,icon};\
80 static const struct menu_item_ex name = \
81 {MT_MENU|MENU_HAS_DESC|MENU_EXITAFTERTHISMENU| \
82 MENU_ITEM_COUNT(sizeof( name##_)/sizeof(*name##_)), \
83 { (void*)name##_},{.callback_and_desc = & name##__}};
85 /* ----------------------------------------------------------------------- */
86 /* Displays the bookmark menu options for the user to decide. This is an */
87 /* interface function. */
88 /* ----------------------------------------------------------------------- */
90 static int bookmark_menu_callback(int action,
91 const struct menu_item_ex *this_item);
92 MENUITEM_FUNCTION(bookmark_create_menu_item, 0,
93 ID2P(LANG_BOOKMARK_MENU_CREATE),
94 bookmark_create_menu, NULL, NULL, Icon_Bookmark);
95 MENUITEM_FUNCTION(bookmark_load_menu_item, 0,
96 ID2P(LANG_BOOKMARK_MENU_LIST),
97 bookmark_load_menu, NULL,
98 bookmark_menu_callback, Icon_Bookmark);
99 MAKE_ONPLAYMENU(bookmark_menu, ID2P(LANG_BOOKMARK_MENU),
100 bookmark_menu_callback, Icon_Bookmark,
101 &bookmark_create_menu_item, &bookmark_load_menu_item);
102 static int bookmark_menu_callback(int action,
103 const struct menu_item_ex *this_item)
105 switch (action)
107 case ACTION_REQUEST_MENUITEM:
108 if (this_item == &bookmark_load_menu_item)
110 if (bookmark_exist() == 0)
111 return ACTION_EXIT_MENUITEM;
113 /* hide the bookmark menu if there is no playback */
114 else if ((audio_status() & AUDIO_STATUS_PLAY) == 0)
115 return ACTION_EXIT_MENUITEM;
116 break;
117 #ifdef HAVE_LCD_CHARCELLS
118 case ACTION_ENTER_MENUITEM:
119 status_set_param(true);
120 break;
121 #endif
122 case ACTION_EXIT_MENUITEM:
123 #ifdef HAVE_LCD_CHARCELLS
124 status_set_param(false);
125 #endif
126 settings_save();
127 break;
129 return action;
132 /* CONTEXT_WPS playlist options */
133 static bool shuffle_playlist(void)
135 playlist_sort(NULL, true);
136 playlist_randomise(NULL, current_tick, true);
138 return false;
140 static bool save_playlist(void)
142 save_playlist_screen(NULL);
143 return false;
146 extern struct menu_item_ex view_cur_playlist; /* from playlist_menu.c */
147 MENUITEM_FUNCTION(search_playlist_item, 0, ID2P(LANG_SEARCH_IN_PLAYLIST),
148 search_playlist, NULL, NULL, Icon_Playlist);
149 MENUITEM_FUNCTION(playlist_save_item, 0, ID2P(LANG_SAVE_DYNAMIC_PLAYLIST),
150 save_playlist, NULL, NULL, Icon_Playlist);
151 MENUITEM_FUNCTION(reshuffle_item, 0, ID2P(LANG_SHUFFLE_PLAYLIST),
152 shuffle_playlist, NULL, NULL, Icon_Playlist);
153 MAKE_ONPLAYMENU( wps_playlist_menu, ID2P(LANG_PLAYLIST),
154 NULL, Icon_Playlist,
155 &view_cur_playlist, &search_playlist_item,
156 &playlist_save_item, &reshuffle_item
159 /* CONTEXT_[TREE|ID3DB] playlist options */
160 static bool add_to_playlist(int position, bool queue)
162 bool new_playlist = !(audio_status() & AUDIO_STATUS_PLAY);
163 const char *lines[] = {
164 ID2P(LANG_RECURSE_DIRECTORY_QUESTION),
165 selected_file
167 const struct text_message message={lines, 2};
169 splash(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 position == PLAYLIST_INSERT_LAST_SHUFFLED)
178 srand(current_tick);
179 if (position == PLAYLIST_INSERT_LAST_SHUFFLED)
180 playlist_set_last_shuffled_start();
183 #ifdef HAVE_TAGCACHE
184 if (context == CONTEXT_ID3DB)
186 tagtree_insert_selection_playlist(position, queue);
188 else
189 #endif
191 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_AUDIO)
192 playlist_insert_track(NULL, selected_file, position, queue, true);
193 else if (selected_file_attr & ATTR_DIRECTORY)
195 bool recurse = false;
197 if (global_settings.recursive_dir_insert != RECURSE_ASK)
198 recurse = (bool)global_settings.recursive_dir_insert;
199 else
201 /* Ask if user wants to recurse directory */
202 recurse = (gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES);
205 playlist_insert_directory(NULL, selected_file, position, queue,
206 recurse);
208 else if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)
209 playlist_insert_playlist(NULL, selected_file, position, queue);
212 if (new_playlist && (playlist_amount() > 0))
214 /* nothing is currently playing so begin playing what we just
215 inserted */
216 if (global_settings.playlist_shuffle)
217 playlist_shuffle(current_tick, -1);
218 playlist_start(0,0);
219 onplay_result = ONPLAY_START_PLAY;
222 return false;
225 static bool view_playlist(void)
227 bool was_playing = audio_status() & AUDIO_STATUS_PLAY;
228 bool result;
230 result = playlist_viewer_ex(selected_file);
232 if (!was_playing && (audio_status() & AUDIO_STATUS_PLAY) &&
233 onplay_result == ONPLAY_OK)
234 /* playlist was started from viewer */
235 onplay_result = ONPLAY_START_PLAY;
237 return result;
240 static int playlist_insert_func(void *param)
242 if (((intptr_t)param == PLAYLIST_REPLACE) && !warn_on_pl_erase())
243 return 0;
244 add_to_playlist((intptr_t)param, false);
245 return 0;
248 static int playlist_queue_func(void *param)
250 add_to_playlist((intptr_t)param, true);
251 return 0;
254 static int treeplaylist_wplayback_callback(int action,
255 const struct menu_item_ex* this_item)
257 (void)this_item;
258 switch (action)
260 case ACTION_REQUEST_MENUITEM:
261 if (audio_status() & AUDIO_STATUS_PLAY)
262 return action;
263 else
264 return ACTION_EXIT_MENUITEM;
265 break;
267 return action;
270 static int treeplaylist_callback(int action,
271 const struct menu_item_ex *this_item);
273 /* insert items */
274 MENUITEM_FUNCTION(i_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT),
275 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT,
276 NULL, Icon_Playlist);
277 MENUITEM_FUNCTION(i_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_FIRST),
278 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
279 treeplaylist_wplayback_callback, Icon_Playlist);
280 MENUITEM_FUNCTION(i_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_INSERT_LAST),
281 playlist_insert_func, (intptr_t*)PLAYLIST_INSERT_LAST,
282 treeplaylist_wplayback_callback, Icon_Playlist);
283 MENUITEM_FUNCTION(i_shuf_pl_item, MENU_FUNC_USEPARAM,
284 ID2P(LANG_INSERT_SHUFFLED), playlist_insert_func,
285 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
286 treeplaylist_callback, Icon_Playlist);
287 MENUITEM_FUNCTION(i_last_shuf_pl_item, MENU_FUNC_USEPARAM,
288 ID2P(LANG_INSERT_LAST_SHUFFLED), playlist_insert_func,
289 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
290 treeplaylist_callback, Icon_Playlist);
291 /* queue items */
292 MENUITEM_FUNCTION(q_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE),
293 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT,
294 treeplaylist_wplayback_callback, Icon_Playlist);
295 MENUITEM_FUNCTION(q_first_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_FIRST),
296 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_FIRST,
297 treeplaylist_wplayback_callback, Icon_Playlist);
298 MENUITEM_FUNCTION(q_last_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_QUEUE_LAST),
299 playlist_queue_func, (intptr_t*)PLAYLIST_INSERT_LAST,
300 treeplaylist_wplayback_callback, Icon_Playlist);
301 MENUITEM_FUNCTION(q_shuf_pl_item, MENU_FUNC_USEPARAM,
302 ID2P(LANG_QUEUE_SHUFFLED), playlist_queue_func,
303 (intptr_t*)PLAYLIST_INSERT_SHUFFLED,
304 treeplaylist_wplayback_callback, Icon_Playlist);
305 MENUITEM_FUNCTION(q_last_shuf_pl_item, MENU_FUNC_USEPARAM,
306 ID2P(LANG_QUEUE_LAST_SHUFFLED), playlist_queue_func,
307 (intptr_t*)PLAYLIST_INSERT_LAST_SHUFFLED,
308 treeplaylist_callback, Icon_Playlist);
309 /* replace playlist */
310 MENUITEM_FUNCTION(replace_pl_item, MENU_FUNC_USEPARAM, ID2P(LANG_REPLACE),
311 playlist_insert_func, (intptr_t*)PLAYLIST_REPLACE,
312 treeplaylist_wplayback_callback, Icon_Playlist);
314 /* others */
315 MENUITEM_FUNCTION(view_playlist_item, 0, ID2P(LANG_VIEW),
316 view_playlist, NULL,
317 treeplaylist_callback, Icon_Playlist);
319 MAKE_ONPLAYMENU( tree_playlist_menu, ID2P(LANG_PLAYLIST),
320 treeplaylist_callback, Icon_Playlist,
322 /* view */
323 &view_playlist_item,
325 /* insert */
326 &i_pl_item, &i_first_pl_item, &i_last_pl_item,
327 &i_shuf_pl_item, &i_last_shuf_pl_item,
328 /* queue */
330 &q_pl_item, &q_first_pl_item, &q_last_pl_item,
331 &q_shuf_pl_item, &q_last_shuf_pl_item,
333 /* replace */
334 &replace_pl_item
336 static int treeplaylist_callback(int action,
337 const struct menu_item_ex *this_item)
339 switch (action)
341 case ACTION_REQUEST_MENUITEM:
342 if (this_item == &tree_playlist_menu)
344 if (((selected_file_attr & FILE_ATTR_MASK) ==
345 FILE_ATTR_AUDIO) ||
346 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)||
347 (selected_file_attr & ATTR_DIRECTORY))
349 return action;
352 else if (this_item == &view_playlist_item)
354 if ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U &&
355 context == CONTEXT_TREE)
357 return action;
360 else if (this_item == &i_shuf_pl_item)
362 if ((audio_status() & AUDIO_STATUS_PLAY) ||
363 (selected_file_attr & ATTR_DIRECTORY) ||
364 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U))
366 return action;
369 else if (this_item == &i_last_shuf_pl_item ||
370 this_item == &q_last_shuf_pl_item)
372 if ((playlist_amount() > 0) &&
373 (audio_status() & AUDIO_STATUS_PLAY) &&
374 ((selected_file_attr & ATTR_DIRECTORY) ||
375 ((selected_file_attr & FILE_ATTR_MASK) == FILE_ATTR_M3U)))
377 return action;
380 return ACTION_EXIT_MENUITEM;
381 break;
383 return action;
387 /* playlist catalog options */
388 static bool cat_add_to_a_playlist(void)
390 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
391 false, NULL);
394 static bool cat_add_to_a_new_playlist(void)
396 return catalog_add_to_a_playlist(selected_file, selected_file_attr,
397 true, NULL);
400 static int cat_playlist_callback(int action,
401 const struct menu_item_ex *this_item);
402 MENUITEM_FUNCTION(cat_view_lists, 0, ID2P(LANG_CATALOG_VIEW),
403 catalog_view_playlists, 0,
404 cat_playlist_callback, Icon_Playlist);
405 MENUITEM_FUNCTION(cat_add_to_list, 0, ID2P(LANG_CATALOG_ADD_TO),
406 cat_add_to_a_playlist, 0, NULL, Icon_Playlist);
407 MENUITEM_FUNCTION(cat_add_to_new, 0, ID2P(LANG_CATALOG_ADD_TO_NEW),
408 cat_add_to_a_new_playlist, 0, NULL, Icon_Playlist);
409 MAKE_ONPLAYMENU(cat_playlist_menu, ID2P(LANG_CATALOG),
410 cat_playlist_callback, Icon_Playlist,
411 &cat_view_lists, &cat_add_to_list, &cat_add_to_new);
413 static int cat_playlist_callback(int action,
414 const struct menu_item_ex *this_item)
416 if (!selected_file ||
417 (((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO) &&
418 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_M3U) &&
419 ((selected_file_attr & ATTR_DIRECTORY) == 0)))
421 return ACTION_EXIT_MENUITEM;
423 #ifdef HAVE_TAGCACHE
424 if (context == CONTEXT_ID3DB &&
425 ((selected_file_attr & FILE_ATTR_MASK) != FILE_ATTR_AUDIO))
427 return ACTION_EXIT_MENUITEM;
429 #endif
431 switch (action)
433 case ACTION_REQUEST_MENUITEM:
434 if (this_item == &cat_view_lists)
436 return action;
438 else if ((audio_status() & AUDIO_STATUS_PLAY) ||
439 context != CONTEXT_WPS)
441 return action;
443 else
444 return ACTION_EXIT_MENUITEM;
445 break;
447 return action;
450 #ifdef HAVE_LCD_BITMAP
451 static void draw_slider(void)
453 int i;
454 FOR_NB_SCREENS(i)
456 struct viewport vp;
457 int slider_height = 2*screens[i].getcharheight();
458 viewport_set_defaults(&vp, i);
459 screens[i].set_viewport(&vp);
460 show_busy_slider(&screens[i], 1, vp.height - slider_height,
461 vp.width-2, slider_height-1);
462 screens[i].update_viewport();
463 screens[i].set_viewport(NULL);
466 #else
467 #define draw_slider()
468 #endif
470 /* helper function to remove a non-empty directory */
471 static int remove_dir(char* dirname, int len)
473 int result = 0;
474 DIR* dir;
475 int dirlen = strlen(dirname);
477 dir = opendir(dirname);
478 if (!dir)
479 return -1; /* open error */
481 while(true)
483 struct dirent* entry;
484 /* walk through the directory content */
485 entry = readdir(dir);
486 if (!entry)
487 break;
489 dirname[dirlen] ='\0';
490 /* inform the user which dir we're deleting */
491 splash(0, dirname);
493 /* append name to current directory */
494 snprintf(dirname+dirlen, len-dirlen, "/%s", entry->d_name);
495 if (entry->attribute & ATTR_DIRECTORY)
496 { /* remove a subdirectory */
497 if (!strcmp((char *)entry->d_name, ".") ||
498 !strcmp((char *)entry->d_name, ".."))
499 continue; /* skip these */
501 result = remove_dir(dirname, len); /* recursion */
502 if (result)
503 break; /* or better continue, delete what we can? */
505 else
506 { /* remove a file */
507 draw_slider();
508 result = remove(dirname);
510 if(ACTION_STD_CANCEL == get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
512 splash(HZ, ID2P(LANG_CANCEL));
513 result = -1;
514 break;
517 closedir(dir);
519 if (!result)
520 { /* remove the now empty directory */
521 dirname[dirlen] = '\0'; /* terminate to original length */
523 result = rmdir(dirname);
526 return result;
530 /* share code for file and directory deletion, saves space */
531 static bool delete_handler(bool is_dir)
533 char file_to_delete[MAX_PATH];
534 strcpy(file_to_delete, selected_file);
536 const char *lines[]={
537 ID2P(LANG_REALLY_DELETE),
538 file_to_delete
540 const char *yes_lines[]={
541 ID2P(LANG_DELETING),
542 file_to_delete
545 const struct text_message message={lines, 2};
546 const struct text_message yes_message={yes_lines, 2};
548 if(gui_syncyesno_run(&message, &yes_message, NULL)!=YESNO_YES)
549 return false;
551 splash(0, str(LANG_DELETING));
553 int res;
554 if (is_dir)
556 char pathname[MAX_PATH]; /* space to go deep */
557 cpu_boost(true);
558 strlcpy(pathname, file_to_delete, sizeof(pathname));
559 res = remove_dir(pathname, sizeof(pathname));
560 cpu_boost(false);
562 else
563 res = remove(file_to_delete);
565 if (!res)
566 onplay_result = ONPLAY_RELOAD_DIR;
568 return (res == 0);
571 static bool delete_file(void)
573 return delete_handler(false);
576 static bool delete_dir(void)
578 return delete_handler(true);
581 static bool rename_file(void)
583 char newname[MAX_PATH];
584 char* ptr = strrchr(selected_file, '/') + 1;
585 int pathlen = (ptr - selected_file);
586 strlcpy(newname, selected_file, sizeof(newname));
587 if (!kbd_input(newname + pathlen, (sizeof newname)-pathlen)) {
588 if (!strlen(newname + pathlen) ||
589 (rename(selected_file, newname) < 0)) {
590 cond_talk_ids_fq(LANG_RENAME, LANG_FAILED);
591 splashf(HZ*2, "%s %s", str(LANG_RENAME), str(LANG_FAILED));
593 else
594 onplay_result = ONPLAY_RELOAD_DIR;
597 return false;
600 static bool create_dir(void)
602 char dirname[MAX_PATH];
603 char *cwd;
604 int rc;
605 int pathlen;
607 cwd = getcwd(NULL, 0);
608 memset(dirname, 0, sizeof dirname);
610 snprintf(dirname, sizeof dirname, "%s/", cwd[1] ? cwd : "");
612 pathlen = strlen(dirname);
613 rc = kbd_input(dirname + pathlen, (sizeof dirname)-pathlen);
614 if (rc < 0)
615 return false;
617 rc = mkdir(dirname);
618 if (rc < 0) {
619 cond_talk_ids_fq(LANG_CREATE_DIR, LANG_FAILED);
620 splashf(HZ, (unsigned char *)"%s %s", str(LANG_CREATE_DIR),
621 str(LANG_FAILED));
622 } else {
623 onplay_result = ONPLAY_RELOAD_DIR;
626 return true;
629 /* Store the current selection in the clipboard */
630 static bool clipboard_clip(bool copy)
632 clipboard_selection[0] = 0;
633 strlcpy(clipboard_selection, selected_file, sizeof(clipboard_selection));
634 clipboard_selection_attr = selected_file_attr;
635 clipboard_is_copy = copy;
637 return true;
640 static bool clipboard_cut(void)
642 return clipboard_clip(false);
645 static bool clipboard_copy(void)
647 return clipboard_clip(true);
650 /* Paste a file to a new directory. Will overwrite always. */
651 static bool clipboard_pastefile(const char *src, const char *target, bool copy)
653 int src_fd, target_fd;
654 size_t buffersize;
655 ssize_t size, bytesread, byteswritten;
656 char *buffer;
657 bool result = false;
659 if (copy) {
660 /* See if we can get the plugin buffer for the file copy buffer */
661 buffer = (char *) plugin_get_buffer(&buffersize);
662 if (buffer == NULL || buffersize < 512) {
663 /* Not large enough, try for a disk sector worth of stack
664 instead */
665 buffersize = 512;
666 buffer = (char *) __builtin_alloca(buffersize);
669 if (buffer == NULL) {
670 return false;
673 buffersize &= ~0x1ff; /* Round buffer size to multiple of sector
674 size */
676 src_fd = open(src, O_RDONLY);
678 if (src_fd >= 0) {
679 target_fd = creat(target);
681 if (target_fd >= 0) {
682 result = true;
684 size = filesize(src_fd);
686 if (size == -1) {
687 result = false;
690 while(size > 0) {
691 bytesread = read(src_fd, buffer, buffersize);
693 if (bytesread == -1) {
694 result = false;
695 break;
698 size -= bytesread;
700 while(bytesread > 0) {
701 byteswritten = write(target_fd, buffer, bytesread);
703 if (byteswritten < 0) {
704 result = false;
705 size = 0;
706 break;
709 bytesread -= byteswritten;
710 draw_slider();
714 close(target_fd);
716 /* Copy failed. Cleanup. */
717 if (!result) {
718 remove(target);
722 close(src_fd);
724 } else {
725 result = rename(src, target) == 0;
726 #ifdef HAVE_MULTIVOLUME
727 if (!result) {
728 if (errno == EXDEV) {
729 /* Failed because cross volume rename doesn't work. Copy
730 instead */
731 result = clipboard_pastefile(src, target, true);
733 if (result) {
734 result = remove(src) == 0;
738 #endif
741 return result;
744 /* Paste a directory to a new location. Designed to be called by
745 clipboard_paste */
746 static bool clipboard_pastedirectory(char *src, int srclen, char *target,
747 int targetlen, bool copy)
749 DIR *srcdir;
750 int srcdirlen = strlen(src);
751 int targetdirlen = strlen(target);
752 bool result = true;
754 if (!file_exists(target)) {
755 if (!copy) {
756 /* Just move the directory */
757 result = rename(src, target) == 0;
759 #ifdef HAVE_MULTIVOLUME
760 if (!result && errno == EXDEV) {
761 /* Try a copy as we're going across devices */
762 result = clipboard_pastedirectory(src, srclen, target,
763 targetlen, true);
765 /* If it worked, remove the source directory */
766 if (result) {
767 remove_dir(src, srclen);
770 #endif
771 return result;
772 } else {
773 /* Make a directory to copy things to */
774 result = mkdir(target) == 0;
778 /* Check if something went wrong already */
779 if (!result) {
780 return result;
783 srcdir = opendir(src);
784 if (!srcdir) {
785 return false;
788 /* This loop will exit as soon as there's a problem */
789 while(result)
791 struct dirent* entry;
792 /* walk through the directory content */
793 entry = readdir(srcdir);
794 if (!entry)
795 break;
797 /* append name to current directory */
798 snprintf(src+srcdirlen, srclen-srcdirlen, "/%s", entry->d_name);
799 snprintf(target+targetdirlen, targetlen-targetdirlen, "/%s",
800 entry->d_name);
802 DEBUGF("Copy %s to %s\n", src, target);
804 if (entry->attribute & ATTR_DIRECTORY)
805 { /* copy/move a subdirectory */
806 if (!strcmp((char *)entry->d_name, ".") ||
807 !strcmp((char *)entry->d_name, ".."))
808 continue; /* skip these */
810 result = clipboard_pastedirectory(src, srclen, target, targetlen,
811 copy); /* recursion */
813 else
814 { /* copy/move a file */
815 draw_slider();
816 result = clipboard_pastefile(src, target, copy);
820 closedir(srcdir);
822 if (result) {
823 src[srcdirlen] = '\0'; /* terminate to original length */
824 target[targetdirlen] = '\0'; /* terminate to original length */
827 return result;
830 /* Paste the clipboard to the current directory */
831 static bool clipboard_paste(void)
833 char target[MAX_PATH];
834 char *cwd, *nameptr;
835 bool success;
837 static const char *lines[]={ID2P(LANG_REALLY_OVERWRITE)};
838 static const struct text_message message={lines, 1};
840 /* Get the name of the current directory */
841 cwd = getcwd(NULL, 0);
843 /* Figure out the name of the selection */
844 nameptr = strrchr(clipboard_selection, '/');
846 /* Final target is current directory plus name of selection */
847 snprintf(target, sizeof(target), "%s%s", cwd[1] ? cwd : "", nameptr);
849 /* If the target existed but they choose not to overwite, exit */
850 if (file_exists(target) &&
851 (gui_syncyesno_run(&message, NULL, NULL) == YESNO_NO)) {
852 return false;
855 if (clipboard_is_copy) {
856 splash(0, ID2P(LANG_COPYING));
858 else
860 splash(0, ID2P(LANG_MOVING));
863 /* Now figure out what we're doing */
864 cpu_boost(true);
865 if (clipboard_selection_attr & ATTR_DIRECTORY) {
866 /* Recursion. Set up external stack */
867 char srcpath[MAX_PATH];
868 char targetpath[MAX_PATH];
869 if (!strncmp(clipboard_selection, target, strlen(clipboard_selection)))
871 /* Do not allow the user to paste a directory into a dir they are
872 copying */
873 success = 0;
875 else
877 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
878 strlcpy(targetpath, target, sizeof(targetpath));
880 success = clipboard_pastedirectory(srcpath, sizeof(srcpath),
881 target, sizeof(targetpath), clipboard_is_copy);
883 if (success && !clipboard_is_copy)
885 strlcpy(srcpath, clipboard_selection, sizeof(srcpath));
886 remove_dir(srcpath, sizeof(srcpath));
889 } else {
890 success = clipboard_pastefile(clipboard_selection, target,
891 clipboard_is_copy);
893 cpu_boost(false);
895 /* Did it work? */
896 if (success) {
897 /* Reset everything */
898 clipboard_selection[0] = 0;
899 clipboard_selection_attr = 0;
900 clipboard_is_copy = false;
902 /* Force reload of the current directory */
903 onplay_result = ONPLAY_RELOAD_DIR;
904 } else {
905 cond_talk_ids_fq(LANG_PASTE, LANG_FAILED);
906 splashf(HZ, (unsigned char *)"%s %s", str(LANG_PASTE),
907 str(LANG_FAILED));
910 return true;
913 #ifdef HAVE_TAGCACHE
914 static int set_rating_inline(void)
916 struct mp3entry* id3 = audio_current_track();
917 if (id3 && id3->tagcache_idx && global_settings.runtimedb)
919 set_int_ex(str(LANG_MENU_SET_RATING), "", UNIT_INT, (void*)(&id3->rating),
920 NULL, 1, 0, 10, NULL, NULL);
921 tagcache_update_numeric(id3->tagcache_idx-1, tag_rating, id3->rating);
923 else
924 splash(HZ*2, ID2P(LANG_ID3_NO_INFO));
925 return 0;
927 static int ratingitem_callback(int action,const struct menu_item_ex *this_item)
929 (void)this_item;
930 switch (action)
932 case ACTION_REQUEST_MENUITEM:
933 if (!selected_file || !global_settings.runtimedb ||
934 !tagcache_is_usable())
935 return ACTION_EXIT_MENUITEM;
936 break;
938 return action;
940 MENUITEM_FUNCTION(rating_item, 0, ID2P(LANG_MENU_SET_RATING),
941 set_rating_inline, NULL,
942 ratingitem_callback, Icon_Questionmark);
943 #endif
945 static bool view_cue(void)
947 struct mp3entry* id3 = audio_current_track();
948 if(id3 && id3->cuesheet)
950 browse_cuesheet(id3->cuesheet);
952 return false;
954 static int view_cue_item_callback(int action,
955 const struct menu_item_ex *this_item)
957 (void)this_item;
958 struct mp3entry* id3 = audio_current_track();
959 switch (action)
961 case ACTION_REQUEST_MENUITEM:
962 if (!selected_file
963 || !id3 || !id3->cuesheet)
964 return ACTION_EXIT_MENUITEM;
965 break;
967 return action;
969 MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET),
970 view_cue, NULL, view_cue_item_callback, Icon_NOICON);
972 /* CONTEXT_WPS items */
973 MENUITEM_FUNCTION(browse_id3_item, 0, ID2P(LANG_MENU_SHOW_ID3_INFO),
974 browse_id3, NULL, NULL, Icon_NOICON);
975 #ifdef HAVE_PITCHSCREEN
976 MENUITEM_FUNCTION(pitch_screen_item, 0, ID2P(LANG_PITCH),
977 gui_syncpitchscreen_run, NULL, NULL, Icon_Audio);
978 #endif
980 /* CONTEXT_[TREE|ID3DB] items */
981 static int clipboard_callback(int action,const struct menu_item_ex *this_item);
982 MENUITEM_FUNCTION(rename_file_item, 0, ID2P(LANG_RENAME),
983 rename_file, NULL, clipboard_callback, Icon_NOICON);
984 MENUITEM_FUNCTION(clipboard_cut_item, 0, ID2P(LANG_CUT),
985 clipboard_cut, NULL, clipboard_callback, Icon_NOICON);
986 MENUITEM_FUNCTION(clipboard_copy_item, 0, ID2P(LANG_COPY),
987 clipboard_copy, NULL, clipboard_callback, Icon_NOICON);
988 MENUITEM_FUNCTION(clipboard_paste_item, 0, ID2P(LANG_PASTE),
989 clipboard_paste, NULL, clipboard_callback, Icon_NOICON);
990 MENUITEM_FUNCTION(delete_file_item, 0, ID2P(LANG_DELETE),
991 delete_file, NULL, clipboard_callback, Icon_NOICON);
992 MENUITEM_FUNCTION(delete_dir_item, 0, ID2P(LANG_DELETE_DIR),
993 delete_dir, NULL, clipboard_callback, Icon_NOICON);
994 MENUITEM_FUNCTION(create_dir_item, 0, ID2P(LANG_CREATE_DIR),
995 create_dir, NULL, clipboard_callback, Icon_NOICON);
997 /* other items */
998 static bool list_viewers(void)
1000 int ret = filetype_list_viewers(selected_file);
1001 if (ret == PLUGIN_USB_CONNECTED)
1002 onplay_result = ONPLAY_RELOAD_DIR;
1003 return false;
1006 static bool onplay_load_plugin(void *param)
1008 int ret = filetype_load_plugin((const char*)param, selected_file);
1009 if (ret == PLUGIN_USB_CONNECTED)
1010 onplay_result = ONPLAY_RELOAD_DIR;
1011 return false;
1014 MENUITEM_FUNCTION(list_viewers_item, 0, ID2P(LANG_ONPLAY_OPEN_WITH),
1015 list_viewers, NULL, clipboard_callback, Icon_NOICON);
1016 MENUITEM_FUNCTION(properties_item, MENU_FUNC_USEPARAM, ID2P(LANG_PROPERTIES),
1017 onplay_load_plugin, (void *)"properties",
1018 clipboard_callback, Icon_NOICON);
1019 MENUITEM_FUNCTION(add_to_faves_item, MENU_FUNC_USEPARAM, ID2P(LANG_ADD_TO_FAVES),
1020 onplay_load_plugin, (void *)"shortcuts_append",
1021 clipboard_callback, Icon_NOICON);
1023 #if LCD_DEPTH > 1
1024 static bool set_backdrop(void)
1026 /* load the image */
1027 if(sb_set_backdrop(SCREEN_MAIN, selected_file)) {
1028 splash(HZ, str(LANG_BACKDROP_LOADED));
1029 set_file(selected_file, (char *)global_settings.backdrop_file,
1030 MAX_FILENAME);
1031 return true;
1032 } else {
1033 splash(HZ, str(LANG_BACKDROP_FAILED));
1034 return false;
1036 return true;
1038 MENUITEM_FUNCTION(set_backdrop_item, 0, ID2P(LANG_SET_AS_BACKDROP),
1039 set_backdrop, NULL, clipboard_callback, Icon_NOICON);
1040 #endif
1041 #ifdef HAVE_RECORDING
1042 static bool set_recdir(void)
1044 strlcpy(global_settings.rec_directory, selected_file, MAX_FILENAME+1);
1045 settings_save();
1046 return false;
1048 MENUITEM_FUNCTION(set_recdir_item, 0, ID2P(LANG_SET_AS_REC_DIR),
1049 set_recdir, NULL, clipboard_callback, Icon_Recording);
1050 #endif
1052 static int clipboard_callback(int action,const struct menu_item_ex *this_item)
1054 switch (action)
1056 case ACTION_REQUEST_MENUITEM:
1057 #ifdef HAVE_MULTIVOLUME
1058 if ((selected_file_attr & FAT_ATTR_VOLUME) &&
1059 (this_item == &rename_file_item ||
1060 this_item == &delete_dir_item ||
1061 this_item == &clipboard_cut_item) )
1062 return ACTION_EXIT_MENUITEM;
1063 /* no rename+delete for volumes */
1064 if ((selected_file_attr & ATTR_VOLUME) &&
1065 (this_item == &delete_file_item ||
1066 this_item == &list_viewers_item))
1067 return ACTION_EXIT_MENUITEM;
1068 #endif
1069 #ifdef HAVE_TAGCACHE
1070 if (context == CONTEXT_ID3DB)
1072 if (((selected_file_attr & FILE_ATTR_MASK) ==
1073 FILE_ATTR_AUDIO) &&
1074 this_item == &properties_item)
1075 return action;
1076 return ACTION_EXIT_MENUITEM;
1078 #endif
1079 if (this_item == &clipboard_paste_item)
1080 { /* visible if there is something to paste */
1081 return (clipboard_selection[0] != 0) ?
1082 action : ACTION_EXIT_MENUITEM;
1084 else if (this_item == &create_dir_item)
1086 /* always visible */
1087 return action;
1089 else if (selected_file)
1091 /* requires an actual file */
1092 if (this_item == &rename_file_item ||
1093 this_item == &clipboard_cut_item ||
1094 this_item == &clipboard_copy_item ||
1095 this_item == &properties_item ||
1096 this_item == &add_to_faves_item)
1098 return action;
1100 else if ((selected_file_attr & ATTR_DIRECTORY))
1102 /* only for directories */
1103 if (this_item == &delete_dir_item
1104 #ifdef HAVE_RECORDING
1105 || this_item == &set_recdir_item
1106 #endif
1108 return action;
1110 else if (this_item == &delete_file_item ||
1111 this_item == &list_viewers_item)
1113 /* only for files */
1114 return action;
1116 #if LCD_DEPTH > 1
1117 else if (this_item == &set_backdrop_item)
1119 char *suffix = strrchr(selected_file, '.');
1120 if (suffix)
1122 if (strcasecmp(suffix, ".bmp") == 0)
1124 return action;
1128 #endif
1130 return ACTION_EXIT_MENUITEM;
1131 break;
1133 return action;
1136 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item);
1137 /* used when onplay() is called in the CONTEXT_WPS context */
1138 MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1139 onplaymenu_callback, Icon_Audio,
1140 &wps_playlist_menu, &cat_playlist_menu,
1141 &sound_settings, &playback_settings,
1142 #ifdef HAVE_TAGCACHE
1143 &rating_item,
1144 #endif
1145 &bookmark_menu, &browse_id3_item, &list_viewers_item,
1146 &delete_file_item, &view_cue_item,
1147 #ifdef HAVE_PITCHSCREEN
1148 &pitch_screen_item,
1149 #endif
1151 /* used when onplay() is not called in the CONTEXT_WPS context */
1152 MAKE_ONPLAYMENU( tree_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE),
1153 onplaymenu_callback, Icon_file_view_menu,
1154 &tree_playlist_menu, &cat_playlist_menu,
1155 &rename_file_item, &clipboard_cut_item, &clipboard_copy_item,
1156 &clipboard_paste_item, &delete_file_item, &delete_dir_item,
1157 #if LCD_DEPTH > 1
1158 &set_backdrop_item,
1159 #endif
1160 &list_viewers_item, &create_dir_item, &properties_item,
1161 #ifdef HAVE_RECORDING
1162 &set_recdir_item,
1163 #endif
1164 &add_to_faves_item,
1166 static int onplaymenu_callback(int action,const struct menu_item_ex *this_item)
1168 switch (action)
1170 case ACTION_TREE_STOP:
1171 if (this_item == &wps_onplay_menu)
1173 list_stop_handler();
1174 return ACTION_STD_CANCEL;
1176 break;
1177 case ACTION_EXIT_MENUITEM:
1178 return ACTION_EXIT_AFTER_THIS_MENUITEM;
1179 break;
1181 return action;
1183 int onplay(char* file, int attr, int from)
1185 const struct menu_item_ex *menu;
1186 onplay_result = ONPLAY_OK;
1187 context = from;
1188 selected_file = file;
1189 selected_file_attr = attr;
1190 if (context == CONTEXT_WPS)
1191 menu = &wps_onplay_menu;
1192 else
1193 menu = &tree_onplay_menu;
1194 switch (do_menu(menu, NULL, NULL, false))
1196 case GO_TO_WPS:
1197 return ONPLAY_START_PLAY;
1198 case GO_TO_ROOT:
1199 case GO_TO_MAINMENU:
1200 return ONPLAY_MAINMENU;
1201 case GO_TO_PLAYLIST_VIEWER:
1202 return ONPLAY_PLAYLIST;
1203 default:
1204 return onplay_result;