subrem_scene_open_sub_file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "../subghz_remote_app_i.h"
  2. void subrem_scene_open_sub_file_error_popup_callback(void* context) {
  3. SubGhzRemoteApp* app = context;
  4. view_dispatcher_send_custom_event(
  5. app->view_dispatcher, SubRemCustomEventSceneEditOpenSubErrorPopup);
  6. }
  7. SubRemLoadSubState subrem_scene_open_sub_file_dialog(SubGhzRemoteApp* app) {
  8. furi_assert(app);
  9. SubRemSubFilePreset* sub = app->map_preset->subs_preset[app->chosen_sub];
  10. FuriString* temp_file_path = furi_string_alloc();
  11. if(furi_string_empty(sub->file_path)) {
  12. furi_string_set(temp_file_path, SUBGHZ_RAW_FOLDER);
  13. } else {
  14. furi_string_set(temp_file_path, sub->file_path);
  15. }
  16. SubRemLoadSubState ret = SubRemLoadSubStateNotSet;
  17. DialogsFileBrowserOptions browser_options;
  18. dialog_file_browser_set_basic_options(
  19. &browser_options, SUBGHZ_APP_FILENAME_EXTENSION, &I_sub1_10px);
  20. browser_options.base_path = SUBGHZ_RAW_FOLDER;
  21. // Input events and views are managed by file_select
  22. if(!dialog_file_browser_show(app->dialogs, temp_file_path, temp_file_path, &browser_options)) {
  23. } else {
  24. // Check sub file
  25. SubRemSubFilePreset* sub_candidate = subrem_sub_file_preset_alloc();
  26. furi_string_set(sub_candidate->label, sub->label);
  27. furi_string_set(sub_candidate->file_path, temp_file_path);
  28. Storage* storage = furi_record_open(RECORD_STORAGE);
  29. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  30. if(flipper_format_file_open_existing(
  31. fff_file, furi_string_get_cstr(sub_candidate->file_path))) {
  32. ret = subrem_sub_preset_load(sub_candidate, app->txrx, fff_file);
  33. }
  34. flipper_format_file_close(fff_file);
  35. flipper_format_free(fff_file);
  36. furi_record_close(RECORD_STORAGE);
  37. if(ret == SubRemLoadSubStateOK) {
  38. subrem_sub_file_preset_free(app->map_preset->subs_preset[app->chosen_sub]);
  39. app->map_preset->subs_preset[app->chosen_sub] = sub_candidate;
  40. app->map_not_saved = true;
  41. } else {
  42. subrem_sub_file_preset_free(sub_candidate);
  43. }
  44. }
  45. furi_string_free(temp_file_path);
  46. return ret;
  47. }
  48. void subrem_scene_open_sub_file_on_enter(void* context) {
  49. furi_assert(context);
  50. SubGhzRemoteApp* app = context;
  51. SubRemLoadSubState load_state = subrem_scene_open_sub_file_dialog(app);
  52. Popup* popup = app->popup;
  53. // popup_set_icon();
  54. popup_set_header(popup, "ERROR", 63, 16, AlignCenter, AlignBottom);
  55. popup_set_timeout(popup, 1500);
  56. popup_set_context(popup, app);
  57. popup_set_callback(popup, subrem_scene_open_sub_file_error_popup_callback);
  58. popup_enable_timeout(popup);
  59. if(load_state == SubRemLoadSubStateOK) {
  60. scene_manager_previous_scene(app->scene_manager);
  61. } else if(load_state == SubRemLoadSubStateNotSet) {
  62. scene_manager_previous_scene(app->scene_manager);
  63. } else {
  64. switch(load_state) {
  65. case SubRemLoadSubStateErrorFreq:
  66. popup_set_text(popup, "Bad frequency", 63, 30, AlignCenter, AlignBottom);
  67. break;
  68. case SubRemLoadSubStateErrorMod:
  69. popup_set_text(popup, "Bad modulation", 63, 30, AlignCenter, AlignBottom);
  70. break;
  71. case SubRemLoadSubStateErrorProtocol:
  72. popup_set_text(popup, "Unsupported protocol", 63, 30, AlignCenter, AlignBottom);
  73. break;
  74. default:
  75. break;
  76. }
  77. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDPopup);
  78. }
  79. }
  80. bool subrem_scene_open_sub_file_on_event(void* context, SceneManagerEvent event) {
  81. SubGhzRemoteApp* app = context;
  82. if(event.type == SceneManagerEventTypeCustom &&
  83. event.event == SubRemCustomEventSceneEditOpenSubErrorPopup) {
  84. scene_manager_previous_scene(app->scene_manager);
  85. return true;
  86. }
  87. return false;
  88. }
  89. void subrem_scene_open_sub_file_on_exit(void* context) {
  90. SubGhzRemoteApp* app = context;
  91. popup_reset(app->popup);
  92. }