mfc_editor_app.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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->mf_classic_data = mf_classic_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. mf_classic_free(instance->mf_classic_data);
  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. NfcDevice* nfc_device = nfc_device_alloc();
  78. if(!nfc_device_load(nfc_device, furi_string_get_cstr(file_path))) {
  79. result = MfcEditorPromptResponseFailure;
  80. dialog_message_show_storage_error(instance->dialogs, "Cannot load\nkey file");
  81. } else {
  82. if(nfc_device_get_protocol(nfc_device) == NfcProtocolMfClassic) {
  83. const MfClassicData* mf_classic_data =
  84. nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  85. mf_classic_copy(instance->mf_classic_data, mf_classic_data);
  86. } else {
  87. result = MfcEditorPromptResponseNotMfClassic;
  88. }
  89. }
  90. nfc_device_free(nfc_device);
  91. return result;
  92. }
  93. static DialogMessageButton mfc_editor_prompt_should_load_shadow(MfcEditorApp* instance) {
  94. DialogMessage* message = dialog_message_alloc();
  95. dialog_message_set_header(message, "File has modifications", 63, 0, AlignCenter, AlignTop);
  96. dialog_message_set_text(
  97. message,
  98. "Would you like to load the\nmodified file (recommended)\nor the original file?",
  99. 63,
  100. 30,
  101. AlignCenter,
  102. AlignCenter);
  103. dialog_message_set_buttons(message, "Original", NULL, "Modified");
  104. DialogMessageButton message_button = dialog_message_show(instance->dialogs, message);
  105. dialog_message_free(message);
  106. return message_button;
  107. }
  108. static void mfc_editor_get_shadow_file_path(FuriString* file_path, FuriString* shadow_file_path) {
  109. furi_assert(file_path);
  110. furi_assert(shadow_file_path);
  111. // Remove NFC extension from end of string then append shadow extension
  112. furi_string_set_n(shadow_file_path, file_path, 0, furi_string_size(file_path) - 4);
  113. furi_string_cat_printf(shadow_file_path, "%s", NFC_APP_SHADOW_EXTENSION);
  114. }
  115. static bool mfc_editor_file_has_shadow_file(MfcEditorApp* instance, FuriString* file_path) {
  116. furi_assert(instance);
  117. furi_assert(file_path);
  118. FuriString* shadow_file_path = furi_string_alloc();
  119. mfc_editor_get_shadow_file_path(file_path, shadow_file_path);
  120. bool has_shadow_file =
  121. storage_common_exists(instance->storage, furi_string_get_cstr(shadow_file_path));
  122. furi_string_free(shadow_file_path);
  123. return has_shadow_file;
  124. }
  125. MfcEditorPromptResponse mfc_editor_prompt_load_file(MfcEditorApp* instance) {
  126. furi_assert(instance);
  127. DialogsFileBrowserOptions browser_options;
  128. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  129. browser_options.base_path = NFC_APP_FOLDER;
  130. browser_options.hide_dot_files = true;
  131. MfcEditorPromptResponse result = MfcEditorPromptResponseSuccess;
  132. if(!dialog_file_browser_show(
  133. instance->dialogs, instance->file_path, instance->file_path, &browser_options)) {
  134. result = MfcEditorPromptResponseExitedFile;
  135. } else {
  136. if(mfc_editor_file_has_shadow_file(instance, instance->file_path)) {
  137. DialogMessageButton message_button = mfc_editor_prompt_should_load_shadow(instance);
  138. if(message_button == DialogMessageButtonRight) {
  139. // User selected to use shadow file, so replace selected path with that path
  140. FuriString* shadow_file_path = furi_string_alloc();
  141. mfc_editor_get_shadow_file_path(instance->file_path, shadow_file_path);
  142. furi_string_set(instance->file_path, shadow_file_path);
  143. furi_string_free(shadow_file_path);
  144. } else if(message_button == DialogMessageButtonBack) {
  145. result = MfcEditorPromptResponseExitedShadow;
  146. }
  147. }
  148. // Don't load the file if user was prompted for shadow file use but went back
  149. if(result == MfcEditorPromptResponseSuccess) {
  150. result = mfc_editor_load_file(instance, instance->file_path);
  151. }
  152. }
  153. return result;
  154. }
  155. int32_t mfc_editor_app(void* p) {
  156. UNUSED(p);
  157. MfcEditorApp* instance = mfc_editor_app_alloc();
  158. scene_manager_next_scene(instance->scene_manager, MfcEditorSceneStart);
  159. view_dispatcher_run(instance->view_dispatcher);
  160. mfc_editor_app_free(instance);
  161. return 0;
  162. }