web_crawler_alloc.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <alloc/web_crawler_alloc.h>
  2. /**
  3. * @brief Function to allocate resources for the WebCrawlerApp.
  4. * @return Pointer to the initialized WebCrawlerApp, or NULL on failure.
  5. */
  6. WebCrawlerApp *web_crawler_app_alloc()
  7. {
  8. // Initialize the entire structure to zero to prevent undefined behavior
  9. WebCrawlerApp *app = (WebCrawlerApp *)malloc(sizeof(WebCrawlerApp));
  10. // Open GUI
  11. Gui *gui = furi_record_open(RECORD_GUI);
  12. // Allocate ViewDispatcher
  13. if (!easy_flipper_set_view_dispatcher(&app->view_dispatcher, gui, app))
  14. {
  15. return NULL;
  16. }
  17. view_dispatcher_set_custom_event_callback(app->view_dispatcher, web_crawler_custom_event_callback);
  18. // Allocate and initialize temp_buffer and path
  19. app->temp_buffer_size_http_method = 16;
  20. if (!easy_flipper_set_buffer(&app->temp_buffer_http_method, app->temp_buffer_size_http_method))
  21. {
  22. return NULL;
  23. }
  24. if (!easy_flipper_set_buffer(&app->http_method, app->temp_buffer_size_http_method))
  25. {
  26. return NULL;
  27. }
  28. // Allocate Submenu views
  29. if (!easy_flipper_set_submenu(&app->submenu_main, WebCrawlerViewSubmenuMain, VERSION_TAG, web_crawler_exit_app_callback, &app->view_dispatcher))
  30. {
  31. return NULL;
  32. }
  33. // Add Submenu items
  34. submenu_add_item(app->submenu_main, "Run", WebCrawlerSubmenuIndexRun, web_crawler_submenu_callback, app);
  35. submenu_add_item(app->submenu_main, "About", WebCrawlerSubmenuIndexAbout, web_crawler_submenu_callback, app);
  36. submenu_add_item(app->submenu_main, "Settings", WebCrawlerSubmenuIndexConfig, web_crawler_submenu_callback, app);
  37. // Main view
  38. if (!easy_flipper_set_view(&app->view_loader, WebCrawlerViewLoader, web_crawler_loader_draw_callback, NULL, web_crawler_back_to_main_callback, &app->view_dispatcher, app))
  39. {
  40. return NULL;
  41. }
  42. web_crawler_loader_init(app->view_loader);
  43. if (!easy_flipper_set_widget(&app->widget_result, WebCrawlerViewWidgetResult, "Error, try again.", web_crawler_back_to_main_callback, &app->view_dispatcher))
  44. {
  45. return NULL;
  46. }
  47. // Start with the Submenu view
  48. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuMain);
  49. return app;
  50. }