scene_action_settings.c 13 KB

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