scene_action_settings.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <furi.h>
  2. #include <gui/view_dispatcher.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/modules/submenu.h>
  5. #include <lib/toolbox/path.h>
  6. #include "quac.h"
  7. #include "scenes.h"
  8. #include "scene_action_settings.h"
  9. #include "../actions/action.h"
  10. #include "quac_icons.h"
  11. // Define different settings per Action
  12. typedef enum {
  13. ActionSettingsRename, // Rename file or folder
  14. ActionSettingsDelete, // Delete file or folder on SDcard
  15. ActionSettingsImport, // Copy a remote file into "current" folder
  16. ActionSettingsCreateGroup, // Create new empty folder in "current" folder
  17. ActionSettingsCreatePlaylist, // Turn this folder into a playlist
  18. ActionSettingsAddToPlaylist, // Append a remote file to this playlist
  19. } ActionSettingsIndex;
  20. // Delete the file of the currently selected item
  21. // Update items_view list before returning so that UI is updated and correct
  22. bool scene_action_settings_delete(App* app) {
  23. bool success = false;
  24. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  25. DialogMessage* dialog = dialog_message_alloc();
  26. dialog_message_set_header(dialog, "Delete?", 64, 0, AlignCenter, AlignTop);
  27. FuriString* text = furi_string_alloc();
  28. furi_string_printf(text, "%s\n\n%s", furi_string_get_cstr(item->name), "Are you sure?");
  29. dialog_message_set_text(dialog, furi_string_get_cstr(text), 64, 18, AlignCenter, AlignTop);
  30. dialog_message_set_buttons(dialog, "Cancel", NULL, "OK");
  31. DialogMessageButton button = dialog_message_show(app->dialog, dialog);
  32. if(button == DialogMessageButtonRight) {
  33. FuriString* current_path = furi_string_alloc();
  34. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  35. FS_Error fs_result = storage_common_remove(app->storage, furi_string_get_cstr(item->path));
  36. if(fs_result == FSE_OK) {
  37. success = true;
  38. FURI_LOG_I(TAG, "Deleted file: %s", furi_string_get_cstr(item->path));
  39. // ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  40. // item_items_view_free(app->items_view);
  41. // app->items_view = new_items;
  42. } else {
  43. FURI_LOG_E(
  44. TAG, "Error deleting file! Error=%s", filesystem_api_error_get_desc(fs_result));
  45. FuriString* error_msg = furi_string_alloc();
  46. furi_string_printf(
  47. error_msg, "Delete failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  48. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  49. furi_string_free(error_msg);
  50. }
  51. furi_string_free(current_path);
  52. } else {
  53. // FURI_LOG_I(TAG, "Used cancelled Delete");
  54. }
  55. furi_string_free(text);
  56. dialog_message_free(dialog);
  57. return success;
  58. }
  59. static bool scene_action_settings_import_file_browser_callback(
  60. FuriString* path,
  61. void* context,
  62. uint8_t** icon,
  63. FuriString* item_name) {
  64. UNUSED(context);
  65. UNUSED(item_name);
  66. char ext[MAX_EXT_LEN];
  67. path_extract_extension(path, ext, MAX_EXT_LEN);
  68. if(!strcmp(ext, ".sub")) {
  69. memcpy(*icon, icon_get_data(&I_SubGHz_10px), 32); // TODO: find the right size!
  70. } else if(!strcmp(ext, ".rfid")) {
  71. memcpy(*icon, icon_get_data(&I_RFID_10px), 32);
  72. } else if(!strcmp(ext, ".ir")) {
  73. memcpy(*icon, icon_get_data(&I_IR_10px), 32);
  74. } else if(!strcmp(ext, ".nfc")) {
  75. memcpy(*icon, icon_get_data(&I_NFC_10px), 32);
  76. } else if(!strcmp(ext, ".qpl")) {
  77. memcpy(*icon, icon_get_data(&I_Playlist_10px), 32);
  78. } else {
  79. return false;
  80. }
  81. return true;
  82. }
  83. // Import a file from elsewhere on the SD card
  84. // Update items_view list before returning so that UI is updated and correct
  85. bool scene_action_settings_import(App* app) {
  86. bool success = false;
  87. FuriString* current_path = furi_string_alloc();
  88. if(app->selected_item != EMPTY_ACTION_INDEX) {
  89. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  90. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  91. } else {
  92. furi_string_set(current_path, app->items_view->path);
  93. }
  94. // Setup our file browser options
  95. DialogsFileBrowserOptions fb_options;
  96. dialog_file_browser_set_basic_options(&fb_options, "", NULL);
  97. fb_options.base_path = furi_string_get_cstr(current_path);
  98. fb_options.skip_assets = true;
  99. furi_string_set_str(app->temp_str, fb_options.base_path);
  100. fb_options.item_loader_callback = scene_action_settings_import_file_browser_callback;
  101. fb_options.item_loader_context = app;
  102. if(dialog_file_browser_show(app->dialog, app->temp_str, app->temp_str, &fb_options)) {
  103. // FURI_LOG_I(TAG, "Selected file is %s", furi_string_get_cstr(app->temp_str));
  104. FuriString* file_name = furi_string_alloc();
  105. path_extract_filename(app->temp_str, file_name, false);
  106. // FURI_LOG_I(TAG, "Importing file %s", furi_string_get_cstr(file_name));
  107. FuriString* full_path;
  108. full_path = furi_string_alloc_printf(
  109. "%s/%s", furi_string_get_cstr(current_path), furi_string_get_cstr(file_name));
  110. // FURI_LOG_I(TAG, "New path is %s", furi_string_get_cstr(full_path));
  111. FS_Error fs_result = storage_common_copy(
  112. app->storage, furi_string_get_cstr(app->temp_str), furi_string_get_cstr(full_path));
  113. if(fs_result == FSE_OK) {
  114. success = true;
  115. // FURI_LOG_I(TAG, "File copied / updating items view list");
  116. // ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  117. // item_items_view_free(app->items_view);
  118. // app->items_view = new_items;
  119. } else {
  120. FURI_LOG_E(TAG, "File copy failed! %s", filesystem_api_error_get_desc(fs_result));
  121. FuriString* error_msg = furi_string_alloc_printf(
  122. "File copy failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  123. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  124. furi_string_free(error_msg);
  125. }
  126. furi_string_free(file_name);
  127. furi_string_free(full_path);
  128. } else {
  129. // FURI_LOG_I(TAG, "User cancelled");
  130. }
  131. furi_string_free(current_path);
  132. return success;
  133. }
  134. // Prompt user for the name of the new Group
  135. // Update items_view list before returning so that UI is updated and correct
  136. bool scene_action_settings_create_group(App* app) {
  137. UNUSED(app);
  138. return false;
  139. }
  140. void scene_action_settings_callback(void* context, uint32_t index) {
  141. App* app = context;
  142. view_dispatcher_send_custom_event(app->view_dispatcher, index);
  143. }
  144. void scene_action_settings_on_enter(void* context) {
  145. App* app = context;
  146. Submenu* menu = app->sub_menu;
  147. submenu_reset(menu);
  148. if(app->selected_item >= 0) {
  149. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  150. submenu_set_header(menu, furi_string_get_cstr(item->name));
  151. submenu_add_item(
  152. menu, "Rename", ActionSettingsRename, scene_action_settings_callback, app);
  153. submenu_add_item(
  154. menu, "Delete", ActionSettingsDelete, scene_action_settings_callback, app);
  155. } else {
  156. submenu_set_header(menu, furi_string_get_cstr(app->items_view->name));
  157. }
  158. submenu_add_item(
  159. menu, "Import Here", ActionSettingsImport, scene_action_settings_callback, app);
  160. submenu_add_item(
  161. menu, "Create Group", ActionSettingsCreateGroup, scene_action_settings_callback, app);
  162. view_dispatcher_switch_to_view(app->view_dispatcher, QView_ActionSettings);
  163. }
  164. bool scene_action_settings_on_event(void* context, SceneManagerEvent event) {
  165. App* app = context;
  166. bool consumed = false;
  167. if(event.type == SceneManagerEventTypeCustom) {
  168. switch(event.event) {
  169. case ActionSettingsRename:
  170. consumed = true;
  171. scene_manager_next_scene(app->scene_manager, QScene_ActionRename);
  172. break;
  173. case ActionSettingsDelete:
  174. consumed = true;
  175. if(scene_action_settings_delete(app)) {
  176. scene_manager_previous_scene(app->scene_manager);
  177. }
  178. break;
  179. case ActionSettingsImport:
  180. consumed = true;
  181. if(scene_action_settings_import(app)) {
  182. scene_manager_previous_scene(app->scene_manager);
  183. }
  184. break;
  185. case ActionSettingsCreateGroup:
  186. consumed = true;
  187. scene_manager_next_scene(app->scene_manager, QScene_ActionCreateGroup);
  188. break;
  189. }
  190. }
  191. return consumed;
  192. }
  193. void scene_action_settings_on_exit(void* context) {
  194. App* app = context;
  195. submenu_reset(app->sub_menu);
  196. // Rebuild our list on exit, to pick up any renames
  197. ItemsView* new_items = item_get_items_view_from_path(app, app->items_view->path);
  198. item_items_view_free(app->items_view);
  199. app->items_view = new_items;
  200. }