scene_action_ir_list.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_ir_list.h"
  9. #include "../actions/action.h"
  10. #include "../actions/action_ir_utils.h"
  11. #include "quac_icons.h"
  12. #include <flipper_format/flipper_format.h>
  13. void scene_action_ir_list_callback(void* context, uint32_t index) {
  14. App* app = context;
  15. view_dispatcher_send_custom_event(app->view_dispatcher, index);
  16. }
  17. void scene_action_ir_list_on_enter(void* context) {
  18. App* app = context;
  19. Submenu* menu = app->sub_menu;
  20. submenu_reset(menu);
  21. // Our selected IR File is app->temp_str
  22. submenu_set_header(menu, "Select IR Command");
  23. // read the IR file and load the names of all of the commands
  24. FuriString* name = furi_string_alloc();
  25. uint32_t index = 0;
  26. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  27. if(flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(app->temp_str))) {
  28. while(flipper_format_read_string(fff_data_file, "name", name)) {
  29. submenu_add_item(
  30. menu, furi_string_get_cstr(name), index, scene_action_ir_list_callback, app);
  31. index++;
  32. }
  33. }
  34. if(index == 0) {
  35. FURI_LOG_E(TAG, "Failed to get commands from %s", furi_string_get_cstr(app->temp_str));
  36. }
  37. flipper_format_file_close(fff_data_file);
  38. flipper_format_free(fff_data_file);
  39. furi_string_free(name);
  40. view_dispatcher_switch_to_view(app->view_dispatcher, QView_SubMenu);
  41. }
  42. bool scene_action_ir_list_on_event(void* context, SceneManagerEvent event) {
  43. App* app = context;
  44. bool consumed = false;
  45. if(event.type == SceneManagerEventTypeCustom) {
  46. consumed = true;
  47. uint32_t index = event.event;
  48. InfraredSignal* signal = infrared_utils_signal_alloc();
  49. // extract that item as it's own file and place it "here", as defined by
  50. // the currently selected_item
  51. FuriString* name = furi_string_alloc(); // IR command name
  52. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  53. FuriString* file_name = furi_string_alloc(); // new IR file name
  54. do {
  55. if(!flipper_format_file_open_existing(
  56. fff_data_file, furi_string_get_cstr(app->temp_str))) {
  57. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(app->temp_str));
  58. break;
  59. }
  60. if(!infrared_utils_read_signal_at_index(fff_data_file, index, signal, name)) {
  61. FURI_LOG_E(TAG, "Failed to read signal at %lu", index);
  62. break;
  63. }
  64. FURI_LOG_I(TAG, "Read IR signal: %s", furi_string_get_cstr(name));
  65. flipper_format_file_close(fff_data_file);
  66. // generate the new path, based on current item's dir and new command name
  67. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  68. path_extract_dirname(furi_string_get_cstr(item->path), file_name);
  69. furi_string_cat_printf(file_name, "/%s.ir", furi_string_get_cstr(name));
  70. FURI_LOG_I(TAG, "Writing new IR file: %s", furi_string_get_cstr(file_name));
  71. if(!flipper_format_file_open_new(fff_data_file, furi_string_get_cstr(file_name))) {
  72. FURI_LOG_E(TAG, "Error creating new file: %s", furi_string_get_cstr(file_name));
  73. break;
  74. }
  75. if(!infrared_utils_write_signal(fff_data_file, signal, name)) {
  76. FURI_LOG_E(TAG, "Failed to write signal!");
  77. break;
  78. }
  79. // Import successful
  80. // TODO: give user an OK prompt and leave them on this scene
  81. // in case they want to import more from this IR file
  82. } while(false);
  83. // cleanup
  84. flipper_format_file_close(fff_data_file);
  85. flipper_format_free(fff_data_file);
  86. furi_string_free(name);
  87. furi_string_free(file_name);
  88. infrared_utils_signal_free(signal);
  89. }
  90. return consumed;
  91. }
  92. void scene_action_ir_list_on_exit(void* context) {
  93. App* app = context;
  94. submenu_reset(app->sub_menu);
  95. }