mfc_editor_app.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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->data_view_header = furi_string_alloc();
  37. instance->data_view_text = furi_string_alloc();
  38. instance->submenu = submenu_alloc();
  39. view_dispatcher_add_view(
  40. instance->view_dispatcher, MfcEditorAppViewSubmenu, submenu_get_view(instance->submenu));
  41. instance->popup = popup_alloc();
  42. view_dispatcher_add_view(
  43. instance->view_dispatcher, MfcEditorAppViewPopup, popup_get_view(instance->popup));
  44. instance->dialog_ex = dialog_ex_alloc();
  45. view_dispatcher_add_view(
  46. instance->view_dispatcher,
  47. MfcEditorAppViewDialogEx,
  48. dialog_ex_get_view(instance->dialog_ex));
  49. return instance;
  50. }
  51. void mfc_editor_app_free(MfcEditorApp* instance) {
  52. furi_assert(instance);
  53. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewSubmenu);
  54. submenu_free(instance->submenu);
  55. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewPopup);
  56. popup_free(instance->popup);
  57. view_dispatcher_remove_view(instance->view_dispatcher, MfcEditorAppViewDialogEx);
  58. dialog_ex_free(instance->dialog_ex);
  59. view_dispatcher_free(instance->view_dispatcher);
  60. scene_manager_free(instance->scene_manager);
  61. furi_record_close(RECORD_GUI);
  62. instance->gui = NULL;
  63. furi_record_close(RECORD_STORAGE);
  64. instance->storage = NULL;
  65. furi_record_close(RECORD_DIALOGS);
  66. instance->dialogs = NULL;
  67. nfc_device_free(instance->nfc_device);
  68. furi_string_free(instance->file_path);
  69. furi_string_free(instance->data_view_header);
  70. furi_string_free(instance->data_view_text);
  71. free(instance);
  72. }
  73. MfcEditorPromptResponse mfc_editor_load_file(MfcEditorApp* instance, FuriString* file_path) {
  74. furi_assert(instance);
  75. furi_assert(file_path);
  76. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  77. if(!nfc_device_load(instance->nfc_device, furi_string_get_cstr(file_path))) {
  78. result = MfcEditorPromptResponseFailure;
  79. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  80. } else {
  81. if(nfc_device_get_protocol(instance->nfc_device) == NfcProtocolMfClassic) {
  82. instance->mf_classic_data =
  83. nfc_device_get_data(instance->nfc_device, NfcProtocolMfClassic);
  84. } else {
  85. result = MfcEditorPromptResponseNotMfClassic;
  86. }
  87. }
  88. return result;
  89. }
  90. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  91. DialogMessage* message = dialog_message_alloc();
  92. dialog_message_set_header(message, "File has modifications", 63, 0, AlignCenter, AlignTop);
  93. dialog_message_set_text(
  94. message,
  95. "Would you like to load the\nmodified file (recommended)\nor the original file?",
  96. 63,
  97. 30,
  98. AlignCenter,
  99. AlignCenter);
  100. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  101. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  102. dialog_message_free(message);
  103. return message_button;
  104. }
  105. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  106. furi_assert(file_path);
  107. furi_assert(shadow_file_path);
  108. // Remove NFC extension from end of string then append shadow extension
  109. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  110. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  111. }
  112. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  113. furi_assert(instance);
  114. furi_assert(file_path);
  115. FuriString* shadow_file_path = furi_string_alloc();
  116. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  117. bool has_shadow_file =
  118. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  119. furi_string_free(shadow_file_path);
  120. return has_shadow_file;
  121. }
  122. MfcEditorPromptResponse mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  123. furi_assert(instance);
  124. DialogsFileBrowserOptions browser_options;
  125. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  126. browser_options.base_path = NFC_APP_FOLDER;
  127. browser_options.hide_dot_files = true;
  128. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  129. if(!dialog_file_browser_show(
  130. instance->dialogs, instance->file_path, instance->file_path, &browser_options)) {
  131. result = MfcEditorPromptResponseExitedFile;
  132. } else {
  133. if(mfc_editor_file_has_shadow_file(instance, instance->file_path)) {
  134. DialogMessageButton message_button = mfc_editor_prompt_should_load_shadow(instance);
  135. if(message_button == DialogMessageButtonRight) {
  136. // User selected to use shadow file, so replace selected path with that path
  137. FuriString* shadow_file_path = furi_string_alloc();
  138. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  139. furi_string_set(instance->file_path, shadow_file_path);
  140. furi_string_free(shadow_file_path);
  141. } else if(message_button == DialogMessageButtonBack) {
  142. result = MfcEditorPromptResponseExitedShadow;
  143. }
  144. }
  145. // Don't load the file if user was prompted for shadow file use but went back
  146. if(result == MfcEditorPromptResponseSuccess) {
  147. result = mfc_editor_load_file(instance, instance->file_path);
  148. }
  149. }
  150. return result;
  151. }
  152. int32_t mfc_editor_app(void* p) {
  153. UNUSED(p);
  154. MfcEditorApp* instance = mfc_editor_app_alloc();
  155. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneStart);
  156. view_dispatcher_run(instance->view_dispatcher);
  157. mfc_editor_app_free(instance);
  158. return 0;
  159. }