mfc_editor_app.c 6.6 KB

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