scene_action_settings.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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, ".qpl")) {
  75. memcpy(*icon, icon_get_data(&I_Playlist_10px), 32);
  76. }
  77. return true;
  78. }
  79. // Import a file from elsewhere on the SD card
  80. // Update items_view list before returning so that UI is updated and correct
  81. bool scene_action_settings_import(App* app) {
  82. bool success = false;
  83. FuriString* current_path = furi_string_alloc();
  84. if(app->selected_item != EMPTY_ACTION_INDEX) {
  85. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  86. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  87. } else {
  88. furi_string_set(current_path, app->items_view->path);
  89. }
  90. // Setup our file browser options
  91. DialogsFileBrowserOptions fb_options;
  92. dialog_file_browser_set_basic_options(&fb_options, "", NULL);
  93. fb_options.base_path = furi_string_get_cstr(current_path);
  94. fb_options.skip_assets = true;
  95. furi_string_set_str(app->temp_str, fb_options.base_path);
  96. fb_options.item_loader_callback = scene_action_settings_import_file_browser_callback;
  97. fb_options.item_loader_context = app;
  98. if(dialog_file_browser_show(app->dialog, app->temp_str, app->temp_str, &fb_options)) {
  99. // FURI_LOG_I(TAG, "Selected file is %s", furi_string_get_cstr(app->temp_str));
  100. FuriString* file_name = furi_string_alloc();
  101. path_extract_filename(app->temp_str, file_name, false);
  102. // FURI_LOG_I(TAG, "Importing file %s", furi_string_get_cstr(file_name));
  103. FuriString* full_path;
  104. full_path = furi_string_alloc_printf(
  105. "%s/%s", furi_string_get_cstr(current_path), furi_string_get_cstr(file_name));
  106. // FURI_LOG_I(TAG, "New path is %s", furi_string_get_cstr(full_path));
  107. FS_Error fs_result = storage_common_copy(
  108. app->storage, furi_string_get_cstr(app->temp_str), furi_string_get_cstr(full_path));
  109. if(fs_result == FSE_OK) {
  110. success = true;
  111. // FURI_LOG_I(TAG, "File copied / updating items view list");
  112. // ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  113. // item_items_view_free(app->items_view);
  114. // app->items_view = new_items;
  115. } else {
  116. FURI_LOG_E(TAG, "File copy failed! %s", filesystem_api_error_get_desc(fs_result));
  117. FuriString* error_msg = furi_string_alloc_printf(
  118. "File copy failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  119. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  120. furi_string_free(error_msg);
  121. }
  122. furi_string_free(file_name);
  123. furi_string_free(full_path);
  124. } else {
  125. // FURI_LOG_I(TAG, "User cancelled");
  126. }
  127. furi_string_free(current_path);
  128. return success;
  129. }
  130. // Prompt user for the name of the new Group
  131. // Update items_view list before returning so that UI is updated and correct
  132. bool scene_action_settings_create_group(App* app) {
  133. UNUSED(app);
  134. return false;
  135. }
  136. void scene_action_settings_callback(void* context, uint32_t index) {
  137. App* app = context;
  138. view_dispatcher_send_custom_event(app->view_dispatcher, index);
  139. }
  140. void scene_action_settings_on_enter(void* context) {
  141. App* app = context;
  142. Submenu* menu = app->sub_menu;
  143. submenu_reset(menu);
  144. if(app->selected_item >= 0) {
  145. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  146. submenu_set_header(menu, furi_string_get_cstr(item->name));
  147. submenu_add_item(
  148. menu, "Rename", ActionSettingsRename, scene_action_settings_callback, app);
  149. submenu_add_item(
  150. menu, "Delete", ActionSettingsDelete, scene_action_settings_callback, app);
  151. } else {
  152. submenu_set_header(menu, furi_string_get_cstr(app->items_view->name));
  153. }
  154. submenu_add_item(
  155. menu, "Import Here", ActionSettingsImport, scene_action_settings_callback, app);
  156. submenu_add_item(
  157. menu, "Create Group", ActionSettingsCreateGroup, scene_action_settings_callback, app);
  158. view_dispatcher_switch_to_view(app->view_dispatcher, QView_ActionSettings);
  159. }
  160. bool scene_action_settings_on_event(void* context, SceneManagerEvent event) {
  161. App* app = context;
  162. bool consumed = false;
  163. if(event.type == SceneManagerEventTypeCustom) {
  164. switch(event.event) {
  165. case ActionSettingsRename:
  166. consumed = true;
  167. scene_manager_next_scene(app->scene_manager, QScene_ActionRename);
  168. break;
  169. case ActionSettingsDelete:
  170. consumed = true;
  171. if(scene_action_settings_delete(app)) {
  172. scene_manager_previous_scene(app->scene_manager);
  173. }
  174. break;
  175. case ActionSettingsImport:
  176. consumed = true;
  177. if(scene_action_settings_import(app)) {
  178. scene_manager_previous_scene(app->scene_manager);
  179. }
  180. break;
  181. case ActionSettingsCreateGroup:
  182. consumed = true;
  183. scene_manager_next_scene(app->scene_manager, QScene_ActionCreateGroup);
  184. break;
  185. }
  186. }
  187. return consumed;
  188. }
  189. void scene_action_settings_on_exit(void* context) {
  190. App* app = context;
  191. submenu_reset(app->sub_menu);
  192. // Rebuild our list on exit, to pick up any renames
  193. ItemsView* new_items = item_get_items_view_from_path(app, app->items_view->path);
  194. item_items_view_free(app->items_view);
  195. app->items_view = new_items;
  196. }