scene_action_settings.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_frame_data(&I_SubGHz_10px, 0), 32); // TODO: find the right size!
  70. } else if(!strcmp(ext, ".rfid")) {
  71. memcpy(*icon, icon_get_frame_data(&I_RFID_10px, 0), 32);
  72. } else if(!strcmp(ext, ".ir")) {
  73. memcpy(*icon, icon_get_frame_data(&I_IR_10px, 0), 32);
  74. } else if(!strcmp(ext, ".nfc")) {
  75. memcpy(*icon, icon_get_frame_data(&I_NFC_10px, 0), 32);
  76. } else if(!strcmp(ext, ".qpl")) {
  77. memcpy(*icon, icon_get_frame_data(&I_Playlist_10px, 0), 32);
  78. } else {
  79. return false;
  80. }
  81. return true;
  82. }
  83. // Ask user for file to import from elsewhere on the SD card
  84. FuriString* scene_action_get_file_to_import_alloc(App* app) {
  85. // Setup our file browser options
  86. DialogsFileBrowserOptions fb_options;
  87. dialog_file_browser_set_basic_options(&fb_options, "", NULL);
  88. fb_options.base_path = STORAGE_EXT_PATH_PREFIX;
  89. fb_options.skip_assets = true;
  90. furi_string_set_str(app->temp_str, fb_options.base_path);
  91. fb_options.item_loader_callback = scene_action_settings_import_file_browser_callback;
  92. fb_options.item_loader_context = app;
  93. FuriString* full_path = NULL;
  94. if(dialog_file_browser_show(app->dialog, app->temp_str, app->temp_str, &fb_options)) {
  95. full_path = furi_string_alloc_set(app->temp_str);
  96. }
  97. return full_path;
  98. }
  99. // Import a file from elsewhere on the SD card
  100. // Update items_view list before returning so that UI is updated and correct
  101. bool scene_action_settings_import(App* app) {
  102. bool success = false;
  103. FuriString* current_path = furi_string_alloc();
  104. if(app->selected_item != EMPTY_ACTION_INDEX) {
  105. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  106. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  107. } else {
  108. furi_string_set(current_path, app->items_view->path);
  109. }
  110. // Setup our file browser options
  111. DialogsFileBrowserOptions fb_options;
  112. dialog_file_browser_set_basic_options(&fb_options, "", NULL);
  113. fb_options.base_path = furi_string_get_cstr(current_path);
  114. fb_options.skip_assets = true;
  115. furi_string_set_str(app->temp_str, fb_options.base_path);
  116. fb_options.item_loader_callback = scene_action_settings_import_file_browser_callback;
  117. fb_options.item_loader_context = app;
  118. if(dialog_file_browser_show(app->dialog, app->temp_str, app->temp_str, &fb_options)) {
  119. // FURI_LOG_I(TAG, "Selected file is %s", furi_string_get_cstr(app->temp_str));
  120. // TODO: this should be a method
  121. FuriString* file_name = furi_string_alloc();
  122. path_extract_filename(app->temp_str, file_name, false);
  123. // FURI_LOG_I(TAG, "Importing file %s", furi_string_get_cstr(file_name));
  124. FuriString* full_path;
  125. full_path = furi_string_alloc_printf(
  126. "%s/%s", furi_string_get_cstr(current_path), furi_string_get_cstr(file_name));
  127. // FURI_LOG_I(TAG, "New path is %s", furi_string_get_cstr(full_path));
  128. FS_Error fs_result = storage_common_copy(
  129. app->storage, furi_string_get_cstr(app->temp_str), furi_string_get_cstr(full_path));
  130. if(fs_result == FSE_OK) {
  131. success = true;
  132. // FURI_LOG_I(TAG, "File copied / updating items view list");
  133. // ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  134. // item_items_view_free(app->items_view);
  135. // app->items_view = new_items;
  136. } else {
  137. FURI_LOG_E(TAG, "File copy failed! %s", filesystem_api_error_get_desc(fs_result));
  138. FuriString* error_msg = furi_string_alloc_printf(
  139. "File copy failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  140. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  141. furi_string_free(error_msg);
  142. }
  143. furi_string_free(file_name);
  144. furi_string_free(full_path);
  145. } else {
  146. // FURI_LOG_I(TAG, "User cancelled");
  147. }
  148. furi_string_free(current_path);
  149. return success;
  150. }
  151. // Prompt user for the name of the new Group
  152. // Update items_view list before returning so that UI is updated and correct
  153. bool scene_action_settings_create_group(App* app) {
  154. UNUSED(app);
  155. return false;
  156. }
  157. void scene_action_settings_callback(void* context, uint32_t index) {
  158. App* app = context;
  159. view_dispatcher_send_custom_event(app->view_dispatcher, index);
  160. }
  161. void scene_action_settings_on_enter(void* context) {
  162. App* app = context;
  163. Submenu* menu = app->sub_menu;
  164. submenu_reset(menu);
  165. if(app->selected_item >= 0) {
  166. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  167. submenu_set_header(menu, furi_string_get_cstr(item->name));
  168. submenu_add_item(
  169. menu, "Rename", ActionSettingsRename, scene_action_settings_callback, app);
  170. submenu_add_item(
  171. menu, "Delete", ActionSettingsDelete, scene_action_settings_callback, app);
  172. } else {
  173. submenu_set_header(menu, furi_string_get_cstr(app->items_view->name));
  174. }
  175. submenu_add_item(
  176. menu, "Import Here", ActionSettingsImport, scene_action_settings_callback, app);
  177. submenu_add_item(
  178. menu, "Create Group", ActionSettingsCreateGroup, scene_action_settings_callback, app);
  179. view_dispatcher_switch_to_view(app->view_dispatcher, QView_SubMenu);
  180. }
  181. bool scene_action_settings_on_event(void* context, SceneManagerEvent event) {
  182. App* app = context;
  183. bool consumed = false;
  184. if(event.type == SceneManagerEventTypeCustom) {
  185. switch(event.event) {
  186. case ActionSettingsRename:
  187. consumed = true;
  188. scene_manager_next_scene(app->scene_manager, QScene_ActionRename);
  189. break;
  190. case ActionSettingsDelete:
  191. consumed = true;
  192. if(scene_action_settings_delete(app)) {
  193. scene_manager_previous_scene(app->scene_manager);
  194. }
  195. break;
  196. case ActionSettingsImport:
  197. consumed = true;
  198. // get the filename to import
  199. FuriString* import_file = scene_action_get_file_to_import_alloc(app);
  200. if(import_file) {
  201. FURI_LOG_I(TAG, "Importing %s", furi_string_get_cstr(import_file));
  202. // if it's a .ir file, switch to a scene that lets user pick the command from the file
  203. // only if there's more than one command in the file. then copy that relevant chunk
  204. // to the local directory
  205. char ext[MAX_EXT_LEN] = {0};
  206. path_extract_extension(import_file, ext, MAX_EXT_LEN);
  207. if(!strcmp(ext, ".ir")) {
  208. FURI_LOG_I(TAG, "Loading ir file %s", furi_string_get_cstr(app->temp_str));
  209. // load scene that takes filename and lists all commands
  210. // the scene should write the new file, eh?
  211. scene_manager_next_scene(app->scene_manager, QScene_ActionIRList);
  212. } else {
  213. // just copy the file here
  214. FuriString* current_path = furi_string_alloc();
  215. if(app->selected_item != EMPTY_ACTION_INDEX) {
  216. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  217. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  218. } else {
  219. furi_string_set(current_path, app->items_view->path);
  220. }
  221. // TODO: this should be a method
  222. FuriString* file_name = furi_string_alloc();
  223. path_extract_filename(import_file, file_name, false);
  224. // FURI_LOG_I(TAG, "Importing file %s", furi_string_get_cstr(file_name));
  225. FuriString* full_path;
  226. full_path = furi_string_alloc_printf(
  227. "%s/%s",
  228. furi_string_get_cstr(current_path),
  229. furi_string_get_cstr(file_name));
  230. // FURI_LOG_I(TAG, "New path is %s", furi_string_get_cstr(full_path));
  231. FURI_LOG_I(
  232. TAG,
  233. "Copy: %s to %s",
  234. furi_string_get_cstr(import_file),
  235. furi_string_get_cstr(full_path));
  236. FS_Error fs_result = storage_common_copy(
  237. app->storage,
  238. furi_string_get_cstr(import_file),
  239. furi_string_get_cstr(full_path));
  240. if(fs_result != FSE_OK) {
  241. FURI_LOG_E(
  242. TAG, "Copy file failed! %s", filesystem_api_error_get_desc(fs_result));
  243. FuriString* error_msg = furi_string_alloc_printf(
  244. "Copy failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  245. dialog_message_show_storage_error(
  246. app->dialog, furi_string_get_cstr(error_msg));
  247. furi_string_free(error_msg);
  248. }
  249. furi_string_free(file_name);
  250. furi_string_free(full_path);
  251. }
  252. furi_string_free(import_file);
  253. } else {
  254. scene_manager_previous_scene(app->scene_manager);
  255. }
  256. // if(scene_action_settings_import(app)) {
  257. // scene_manager_previous_scene(app->scene_manager);
  258. // }
  259. break;
  260. case ActionSettingsCreateGroup:
  261. consumed = true;
  262. scene_manager_next_scene(app->scene_manager, QScene_ActionCreateGroup);
  263. break;
  264. }
  265. }
  266. return consumed;
  267. }
  268. void scene_action_settings_on_exit(void* context) {
  269. App* app = context;
  270. submenu_reset(app->sub_menu);
  271. // Rebuild our list on exit, to pick up any renames
  272. ItemsView* new_items = item_get_items_view_from_path(app, app->items_view->path);
  273. item_items_view_free(app->items_view);
  274. app->items_view = new_items;
  275. }