barcode_app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "barcode_app.h"
  2. #include "barcode_app_icons.h"
  3. /**
  4. * Opens a file browser dialog and returns the filepath of the selected file
  5. *
  6. * @param folder the folder to view when the browser opens
  7. * @param file_path a string pointer for the file_path when a file is selected,
  8. * file_path will be the folder path is nothing is selected
  9. * @returns true if a file is selected
  10. */
  11. static bool select_file(const char* folder, FuriString* file_path) {
  12. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  13. DialogsFileBrowserOptions browser_options;
  14. dialog_file_browser_set_basic_options(&browser_options, "", &I_barcode_10);
  15. browser_options.base_path = DEFAULT_USER_BARCODES;
  16. furi_string_set(file_path, folder);
  17. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  18. furi_record_close(RECORD_DIALOGS);
  19. return res;
  20. }
  21. /**
  22. * Reads the data from a file and stores them in the FuriStrings raw_type and raw_data
  23. */
  24. ErrorCode read_raw_data(FuriString* file_path, FuriString* raw_type, FuriString* raw_data) {
  25. //Open Storage
  26. Storage* storage = furi_record_open(RECORD_STORAGE);
  27. FlipperFormat* ff = flipper_format_file_alloc(storage);
  28. ErrorCode reason = OKCode;
  29. if(!flipper_format_file_open_existing(ff, furi_string_get_cstr(file_path))) {
  30. FURI_LOG_E(TAG, "Could not open file %s", furi_string_get_cstr(file_path));
  31. reason = FileOpening;
  32. } else {
  33. if(!flipper_format_read_string(ff, "Type", raw_type)) {
  34. FURI_LOG_E(TAG, "Could not read \"Type\" string");
  35. reason = InvalidFileData;
  36. }
  37. if(!flipper_format_read_string(ff, "Data", raw_data)) {
  38. FURI_LOG_E(TAG, "Could not read \"Data\" string");
  39. reason = InvalidFileData;
  40. }
  41. }
  42. //Close Storage
  43. flipper_format_free(ff);
  44. furi_record_close(RECORD_STORAGE);
  45. return reason;
  46. }
  47. /**
  48. * Gets the file name from a file path
  49. * @param file_path the file path
  50. * @param file_name the FuriString to store the file name
  51. * @param remove_extension true if the extension should be removed, otherwise false
  52. */
  53. bool get_file_name_from_path(FuriString* file_path, FuriString* file_name, bool remove_extension) {
  54. if(file_path == NULL || file_name == NULL) {
  55. return false;
  56. }
  57. uint32_t slash_index = furi_string_search_rchar(file_path, '/', 0);
  58. if(slash_index == FURI_STRING_FAILURE || slash_index >= (furi_string_size(file_path) - 1)) {
  59. return false;
  60. }
  61. furi_string_set(file_name, file_path);
  62. furi_string_right(file_name, slash_index + 1);
  63. if(remove_extension) {
  64. uint32_t ext_index = furi_string_search_rchar(file_name, '.', 0);
  65. if(ext_index != FURI_STRING_FAILURE && ext_index < (furi_string_size(file_path))) {
  66. furi_string_left(file_name, ext_index);
  67. }
  68. }
  69. return true;
  70. }
  71. /**
  72. * Creates the barcode folder
  73. */
  74. void init_folder() {
  75. Storage* storage = furi_record_open(RECORD_STORAGE);
  76. FURI_LOG_I(TAG, "Creating barcodes folder");
  77. if(storage_simply_mkdir(storage, DEFAULT_USER_BARCODES)) {
  78. FURI_LOG_I(TAG, "Barcodes folder successfully created!");
  79. } else {
  80. FURI_LOG_I(TAG, "Barcodes folder already exists.");
  81. }
  82. furi_record_close(RECORD_STORAGE);
  83. }
  84. void select_barcode_item(BarcodeApp* app) {
  85. FuriString* file_path = furi_string_alloc();
  86. FuriString* raw_type = furi_string_alloc();
  87. FuriString* raw_data = furi_string_alloc();
  88. //this determines if the data was read correctly or if the
  89. bool loaded_success = true;
  90. ErrorCode reason = OKCode;
  91. bool file_selected = select_file(DEFAULT_USER_BARCODES, file_path);
  92. if(file_selected) {
  93. FURI_LOG_I(TAG, "The file selected is %s", furi_string_get_cstr(file_path));
  94. Barcode* barcode = app->barcode_view;
  95. reason = read_raw_data(file_path, raw_type, raw_data);
  96. if(reason != OKCode) {
  97. loaded_success = false;
  98. FURI_LOG_E(TAG, "Could not read data correctly");
  99. }
  100. //Free the data from the previous barcode
  101. barcode_free_model(barcode);
  102. with_view_model(
  103. barcode->view,
  104. BarcodeModel * model,
  105. {
  106. model->file_path = furi_string_alloc_set(file_path);
  107. model->data = malloc(sizeof(BarcodeData));
  108. model->data->valid = loaded_success;
  109. if(loaded_success) {
  110. model->data->raw_data = furi_string_alloc_set(raw_data);
  111. model->data->correct_data = furi_string_alloc();
  112. model->data->type_obj = get_type(raw_type);
  113. barcode_loader(model->data);
  114. } else {
  115. model->data->reason = reason;
  116. }
  117. },
  118. true);
  119. view_dispatcher_switch_to_view(app->view_dispatcher, BarcodeView);
  120. }
  121. furi_string_free(raw_type);
  122. furi_string_free(raw_data);
  123. furi_string_free(file_path);
  124. }
  125. void edit_barcode_item(BarcodeApp* app) {
  126. FuriString* file_path = furi_string_alloc();
  127. FuriString* file_name = furi_string_alloc();
  128. FuriString* raw_type = furi_string_alloc();
  129. FuriString* raw_data = furi_string_alloc();
  130. //this determines if the data was read correctly or if the
  131. ErrorCode reason = OKCode;
  132. bool file_selected = select_file(DEFAULT_USER_BARCODES, file_path);
  133. if(file_selected) {
  134. FURI_LOG_I(TAG, "The file selected is %s", furi_string_get_cstr(file_path));
  135. CreateView* create_view_object = app->create_view;
  136. reason = read_raw_data(file_path, raw_type, raw_data);
  137. if(reason != OKCode) {
  138. FURI_LOG_E(TAG, "Could not read data correctly");
  139. with_view_model(
  140. app->message_view->view,
  141. MessageViewModel * model,
  142. { model->message = get_error_code_message(reason); },
  143. true);
  144. view_dispatcher_switch_to_view(
  145. create_view_object->barcode_app->view_dispatcher, MessageErrorView);
  146. } else {
  147. BarcodeTypeObj* type_obj = get_type(raw_type);
  148. if(type_obj->type == UNKNOWN) {
  149. type_obj = barcode_type_objs[0];
  150. }
  151. get_file_name_from_path(file_path, file_name, true);
  152. create_view_free_model(create_view_object);
  153. with_view_model(
  154. create_view_object->view,
  155. CreateViewModel * model,
  156. {
  157. model->selected_menu_item = 0;
  158. model->barcode_type = type_obj;
  159. model->file_path = furi_string_alloc_set(file_path);
  160. model->file_name = furi_string_alloc_set(file_name);
  161. model->barcode_data = furi_string_alloc_set(raw_data);
  162. model->mode = EditMode;
  163. },
  164. true);
  165. view_dispatcher_switch_to_view(app->view_dispatcher, CreateBarcodeView);
  166. }
  167. }
  168. furi_string_free(raw_type);
  169. furi_string_free(raw_data);
  170. furi_string_free(file_name);
  171. furi_string_free(file_path);
  172. }
  173. void create_barcode_item(BarcodeApp* app) {
  174. CreateView* create_view_object = app->create_view;
  175. create_view_free_model(create_view_object);
  176. with_view_model(
  177. create_view_object->view,
  178. CreateViewModel * model,
  179. {
  180. model->selected_menu_item = 0;
  181. model->barcode_type = barcode_type_objs[0];
  182. model->file_path = furi_string_alloc();
  183. model->file_name = furi_string_alloc();
  184. model->barcode_data = furi_string_alloc();
  185. model->mode = NewMode;
  186. },
  187. true);
  188. view_dispatcher_switch_to_view(app->view_dispatcher, CreateBarcodeView);
  189. }
  190. void submenu_callback(void* context, uint32_t index) {
  191. furi_assert(context);
  192. BarcodeApp* app = context;
  193. if(index == SelectBarcodeItem) {
  194. select_barcode_item(app);
  195. } else if(index == EditBarcodeItem) {
  196. edit_barcode_item(app);
  197. } else if(index == CreateBarcodeItem) {
  198. create_barcode_item(app);
  199. }
  200. }
  201. uint32_t create_view_callback(void* context) {
  202. UNUSED(context);
  203. return CreateBarcodeView;
  204. }
  205. uint32_t main_menu_callback(void* context) {
  206. UNUSED(context);
  207. return MainMenuView;
  208. }
  209. uint32_t exit_callback(void* context) {
  210. UNUSED(context);
  211. return VIEW_NONE;
  212. }
  213. void free_app(BarcodeApp* app) {
  214. FURI_LOG_I(TAG, "Freeing Data");
  215. init_folder();
  216. free_types();
  217. view_dispatcher_remove_view(app->view_dispatcher, TextInputView);
  218. text_input_free(app->text_input);
  219. view_dispatcher_remove_view(app->view_dispatcher, MessageErrorView);
  220. message_view_free(app->message_view);
  221. view_dispatcher_remove_view(app->view_dispatcher, MainMenuView);
  222. submenu_free(app->main_menu);
  223. view_dispatcher_remove_view(app->view_dispatcher, CreateBarcodeView);
  224. create_view_free(app->create_view);
  225. view_dispatcher_remove_view(app->view_dispatcher, BarcodeView);
  226. barcode_free(app->barcode_view);
  227. //free the dispatcher
  228. view_dispatcher_free(app->view_dispatcher);
  229. furi_message_queue_free(app->event_queue);
  230. furi_record_close(RECORD_GUI);
  231. app->gui = NULL;
  232. free(app);
  233. }
  234. int32_t barcode_main(void* p) {
  235. UNUSED(p);
  236. BarcodeApp* app = malloc(sizeof(BarcodeApp));
  237. init_types();
  238. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  239. // Register view port in GUI
  240. app->gui = furi_record_open(RECORD_GUI);
  241. app->view_dispatcher = view_dispatcher_alloc();
  242. view_dispatcher_enable_queue(app->view_dispatcher);
  243. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  244. app->main_menu = submenu_alloc();
  245. submenu_add_item(app->main_menu, "Load Barcode", SelectBarcodeItem, submenu_callback, app);
  246. view_set_previous_callback(submenu_get_view(app->main_menu), exit_callback);
  247. view_dispatcher_add_view(app->view_dispatcher, MainMenuView, submenu_get_view(app->main_menu));
  248. submenu_add_item(app->main_menu, "Edit Barcode", EditBarcodeItem, submenu_callback, app);
  249. /*****************************
  250. * Creating Text Input View
  251. ******************************/
  252. app->text_input = text_input_alloc();
  253. view_set_previous_callback(text_input_get_view(app->text_input), create_view_callback);
  254. view_dispatcher_add_view(
  255. app->view_dispatcher, TextInputView, text_input_get_view(app->text_input));
  256. /*****************************
  257. * Creating Message View
  258. ******************************/
  259. app->message_view = message_view_allocate(app);
  260. view_dispatcher_add_view(
  261. app->view_dispatcher, MessageErrorView, message_get_view(app->message_view));
  262. /*****************************
  263. * Creating Create View
  264. ******************************/
  265. app->create_view = create_view_allocate(app);
  266. submenu_add_item(app->main_menu, "Create Barcode", CreateBarcodeItem, submenu_callback, app);
  267. view_set_previous_callback(create_get_view(app->create_view), main_menu_callback);
  268. view_dispatcher_add_view(
  269. app->view_dispatcher, CreateBarcodeView, create_get_view(app->create_view));
  270. /*****************************
  271. * Creating Barcode View
  272. ******************************/
  273. app->barcode_view = barcode_view_allocate(app);
  274. view_set_previous_callback(barcode_get_view(app->barcode_view), main_menu_callback);
  275. view_dispatcher_add_view(
  276. app->view_dispatcher, BarcodeView, barcode_get_view(app->barcode_view));
  277. //switch view to submenu and run dispatcher
  278. view_dispatcher_switch_to_view(app->view_dispatcher, MainMenuView);
  279. view_dispatcher_run(app->view_dispatcher);
  280. free_app(app);
  281. return 0;
  282. }