mfc_editor_app.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "mfc_editor_app_i.h"
  2. bool mfc_editor_app_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. MfcEditorApp* instance = context;
  5. return scene_manager_handle_custom_event(instance->scene_manager, event);
  6. }
  7. bool mfc_editor_app_back_event_callback(void* context) {
  8. furi_assert(context);
  9. MfcEditorApp* instance = context;
  10. return scene_manager_handle_back_event(instance->scene_manager);
  11. }
  12. void mfc_editor_app_tick_event_callback(void* context) {
  13. furi_assert(context);
  14. MfcEditorApp* instance = context;
  15. scene_manager_handle_tick_event(instance->scene_manager);
  16. }
  17. MfcEditorApp* mfc_editor_app_alloc() {
  18. MfcEditorApp* instance = malloc(sizeof(MfcEditorApp));
  19. instance->view_dispatcher = view_dispatcher_alloc();
  20. instance->scene_manager = scene_manager_alloc(&mfc_editor_scene_handlers, instance);
  21. view_dispatcher_enable_queue(instance->view_dispatcher);
  22. view_dispatcher_set_event_callback_context(instance->view_dispatcher, instance);
  23. view_dispatcher_set_custom_event_callback(
  24. instance->view_dispatcher, mfc_editor_app_custom_event_callback);
  25. view_dispatcher_set_navigation_event_callback(
  26. instance->view_dispatcher, mfc_editor_app_back_event_callback);
  27. view_dispatcher_set_tick_event_callback(
  28. instance->view_dispatcher, mfc_editor_app_tick_event_callback, 100);
  29. instance->gui = furi_record_open(RECORD_GUI);
  30. view_dispatcher_attach_to_gui(
  31. instance->view_dispatcher, instance->gui, ViewDispatcherTypeFullscreen);
  32. instance->storage = furi_record_open(RECORD_STORAGE);
  33. instance->dialogs = furi_record_open(RECORD_DIALOGS);
  34. instance->nfc_device = nfc_device_alloc();
  35. instance->file_path = furi_string_alloc_set(NFC_APP_FOLDER);
  36. instance->submenu = submenu_alloc();
  37. view_dispatcher_add_view(
  38. instance->view_dispatcher, MfcEditorAppViewSubmenu, submenu_get_view(instance->submenu));
  39. return instance;
  40. }
  41. void mfc_editor_app_free(MfcEditorApp* instance) {
  42. furi_assert(instance);
  43. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewSubmenu);
  44. submenu_free(instance->submenu);
  45. view_dispatcher_free(instance->view_dispatcher);
  46. scene_manager_free(instance->scene_manager);
  47. furi_record_close(RECORD_GUI);
  48. instance->gui = NULL;
  49. furi_record_close(RECORD_STORAGE);
  50. instance->storage = NULL;
  51. furi_record_close(RECORD_DIALOGS);
  52. instance->dialogs = NULL;
  53. nfc_device_free(instance->nfc_device);
  54. furi_string_free(instance->file_path);
  55. free(instance);
  56. }
  57. bool mfc_editor_load_file(MfcEditorApp* instance, FuriString* file_path, bool show_dialog) {
  58. furi_assert(instance);
  59. furi_assert(file_path);
  60. bool result = false;
  61. result = nfc_device_load(instance->nfc_device, furi_string_get_cstr(file_path));
  62. if(!result && show_dialog) {
  63. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  64. }
  65. return result;
  66. }
  67. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  68. DialogMessage* message = dialog_message_alloc();
  69. dialog_message_set_header(message, "File has modifications", 63, 0, AlignCenter, AlignTop);
  70. dialog_message_set_text(
  71. message,
  72. "Would you like to load the\nmodified file (recommended)\nor the original file?",
  73. 63,
  74. 30,
  75. AlignCenter,
  76. AlignCenter);
  77. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  78. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  79. dialog_message_free(message);
  80. return message_button;
  81. }
  82. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  83. furi_assert(file_path);
  84. furi_assert(shadow_file_path);
  85. // Remove NFC extension from end of string then append shadow extension
  86. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  87. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  88. }
  89. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  90. furi_assert(instance);
  91. furi_assert(file_path);
  92. FuriString* shadow_file_path = furi_string_alloc();
  93. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  94. bool has_shadow_file =
  95. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  96. furi_string_free(shadow_file_path);
  97. return has_shadow_file;
  98. }
  99. bool mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  100. furi_assert(instance);
  101. DialogsFileBrowserOptions browser_options;
  102. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  103. browser_options.base_path = NFC_APP_FOLDER;
  104. browser_options.hide_dot_files = true;
  105. bool result = dialog_file_browser_show(
  106. instance->dialogs, instance->file_path, instance->file_path, &browser_options);
  107. if(result) {
  108. if(mfc_editor_file_has_shadow_file(instance, instance->file_path) &&
  109. mfc_editor_prompt_should_load_shadow(instance) == DialogMessageButtonRight) {
  110. FuriString* shadow_file_path = furi_string_alloc();
  111. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  112. furi_string_set(instance->file_path, shadow_file_path);
  113. furi_string_free(shadow_file_path);
  114. }
  115. result = mfc_editor_load_file(instance, instance->file_path, true);
  116. }
  117. return result;
  118. }
  119. int32_t mfc_editor_app(void* p) {
  120. UNUSED(p);
  121. MfcEditorApp* instance = mfc_editor_app_alloc();
  122. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneStart);
  123. view_dispatcher_run(instance->view_dispatcher);
  124. mfc_editor_app_free(instance);
  125. return 0;
  126. }