text_viewer_scene_show.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "../text_viewer.h"
  2. #define SHOW_MAX_FILE_SIZE 8000
  3. void text_viewer_scene_show_widget_callback(GuiButtonType result, InputType type, void* context) {
  4. furi_assert(context);
  5. TextViewer* app = (TextViewer*)context;
  6. if(type == InputTypeShort) {
  7. view_dispatcher_send_custom_event(app->view_dispatcher, result);
  8. }
  9. }
  10. static bool text_show_read_lines(File* file, FuriString* str_result) {
  11. //furi_string_reset(str_result);
  12. uint8_t buffer[SHOW_MAX_FILE_SIZE];
  13. uint16_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
  14. if(storage_file_get_error(file) != FSE_OK) {
  15. return false;
  16. }
  17. for(uint16_t i = 0; i < read_count; i++) {
  18. furi_string_push_back(str_result, buffer[i]);
  19. }
  20. return true;
  21. }
  22. void text_viewer_scene_show_on_enter(void* context) {
  23. furi_assert(context);
  24. TextViewer* app = context;
  25. FuriString* buffer;
  26. buffer = furi_string_alloc();
  27. Storage* storage = furi_record_open(RECORD_STORAGE);
  28. File* file = storage_file_alloc(storage);
  29. FileInfo fileinfo;
  30. FS_Error error = storage_common_stat(storage, furi_string_get_cstr(app->path), &fileinfo);
  31. if(error == FSE_OK) {
  32. if((fileinfo.size < SHOW_MAX_FILE_SIZE) && (fileinfo.size > 2)) {
  33. bool ok = storage_file_open(
  34. file, furi_string_get_cstr(app->path), FSAM_READ, FSOM_OPEN_EXISTING);
  35. if(ok) {
  36. if(!text_show_read_lines(file, buffer)) {
  37. goto text_file_read_err;
  38. }
  39. if(!furi_string_size(buffer)) {
  40. goto text_file_read_err;
  41. }
  42. storage_file_seek(file, 0, true);
  43. widget_add_text_scroll_element(
  44. app->widget, 0, 0, 128, 64, furi_string_get_cstr(buffer));
  45. } else {
  46. text_file_read_err:
  47. widget_add_text_box_element(
  48. app->widget,
  49. 0,
  50. 0,
  51. 128,
  52. 64,
  53. AlignLeft,
  54. AlignCenter,
  55. "\e#Error:\nStorage file open error\e#",
  56. false);
  57. }
  58. storage_file_close(file);
  59. } else if(fileinfo.size < 2) {
  60. widget_add_text_box_element(
  61. app->widget,
  62. 0,
  63. 0,
  64. 128,
  65. 64,
  66. AlignLeft,
  67. AlignCenter,
  68. "\e#Error:\nFile is too small\e#",
  69. false);
  70. } else {
  71. widget_add_text_box_element(
  72. app->widget,
  73. 0,
  74. 0,
  75. 128,
  76. 64,
  77. AlignLeft,
  78. AlignCenter,
  79. "\e#Error:\nFile is too large to show\e#",
  80. false);
  81. }
  82. } else {
  83. widget_add_text_box_element(
  84. app->widget,
  85. 0,
  86. 0,
  87. 128,
  88. 64,
  89. AlignLeft,
  90. AlignCenter,
  91. "\e#Error:\nFile system error\e#",
  92. false);
  93. }
  94. furi_string_free(buffer);
  95. storage_file_free(file);
  96. furi_record_close(RECORD_STORAGE);
  97. view_dispatcher_switch_to_view(app->view_dispatcher, TextViewerViewWidget);
  98. }
  99. bool text_viewer_scene_show_on_event(void* context, SceneManagerEvent event) {
  100. furi_assert(context);
  101. TextViewer* app = (TextViewer*)context;
  102. if(event.type == SceneManagerEventTypeCustom) {
  103. scene_manager_previous_scene(app->scene_manager);
  104. return true;
  105. }
  106. return false;
  107. }
  108. void text_viewer_scene_show_on_exit(void* context) {
  109. furi_assert(context);
  110. TextViewer* app = (TextViewer*)context;
  111. widget_reset(app->widget);
  112. }