web_crawler_callback.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // web_crawler_callback.h
  2. static bool sent_get_request = false;
  3. static bool get_success = false;
  4. static bool already_success = false;
  5. static WebCrawlerApp *app_instance = NULL;
  6. // Forward declaration of callback functions
  7. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index);
  8. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index);
  9. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index);
  10. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index);
  11. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index);
  12. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index);
  13. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index);
  14. static void web_crawler_view_draw_callback(Canvas *canvas, void *context)
  15. {
  16. UNUSED(context);
  17. if (!app_instance)
  18. {
  19. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  20. return;
  21. }
  22. if (!canvas)
  23. {
  24. FURI_LOG_E(TAG, "Canvas is NULL");
  25. return;
  26. }
  27. canvas_clear(canvas);
  28. canvas_set_font(canvas, FontSecondary);
  29. if (fhttp.state == INACTIVE)
  30. {
  31. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  32. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  33. canvas_draw_str(canvas, 0, 32, "If you board is connected,");
  34. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  35. canvas_draw_str(canvas, 0, 52, "your Dev Board with the");
  36. canvas_draw_str(canvas, 0, 62, "FlipperHTTP firmware.");
  37. return;
  38. }
  39. if (app_instance->path)
  40. {
  41. if (!sent_get_request)
  42. {
  43. canvas_draw_str(canvas, 0, 10, "Sending GET request...");
  44. // Perform GET request and handle the response
  45. get_success = flipper_http_get_request(app_instance->path);
  46. canvas_draw_str(canvas, 0, 20, "Sent!");
  47. if (get_success)
  48. {
  49. canvas_draw_str(canvas, 0, 30, "Receiving data...");
  50. // Set the state to RECEIVING to ensure we continue to see the receiving message
  51. fhttp.state = RECEIVING;
  52. }
  53. else
  54. {
  55. canvas_draw_str(canvas, 0, 30, "Failed.");
  56. }
  57. sent_get_request = true;
  58. }
  59. else
  60. {
  61. // print state
  62. if (get_success && fhttp.state == RECEIVING)
  63. {
  64. canvas_draw_str(canvas, 0, 10, "Receiving and parsing data...");
  65. }
  66. else if (get_success && fhttp.state == IDLE)
  67. {
  68. canvas_draw_str(canvas, 0, 10, "Data saved to file.");
  69. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  70. }
  71. else
  72. {
  73. if (fhttp.state == ISSUE)
  74. {
  75. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  76. {
  77. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  78. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  79. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  80. }
  81. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  82. {
  83. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  84. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  85. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  86. }
  87. else
  88. {
  89. canvas_draw_str(canvas, 0, 10, "[ERROR] Failed to sync data.");
  90. canvas_draw_str(canvas, 0, 30, "If this is your third attempt,");
  91. canvas_draw_str(canvas, 0, 40, "it's likely your URL is not");
  92. canvas_draw_str(canvas, 0, 50, "compabilbe or correct.");
  93. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  94. }
  95. }
  96. else
  97. {
  98. canvas_draw_str(canvas, 0, 10, "GET request failed.");
  99. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  100. }
  101. get_success = false;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. canvas_draw_str(canvas, 0, 10, "URL not set.");
  108. }
  109. }
  110. /**
  111. * @brief Navigation callback to handle exiting from other views to the submenu.
  112. * @param context The context - WebCrawlerApp object.
  113. * @return WebCrawlerViewSubmenu
  114. */
  115. static uint32_t web_crawler_back_to_configure_callback(void *context)
  116. {
  117. UNUSED(context);
  118. // free file read widget if it exists
  119. if (app_instance->widget_file_read)
  120. {
  121. widget_reset(app_instance->widget_file_read);
  122. }
  123. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  124. }
  125. /**
  126. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  127. * @param context The context - WebCrawlerApp object.
  128. * @return WebCrawlerViewSubmenu
  129. */
  130. static uint32_t web_crawler_back_to_main_callback(void *context)
  131. {
  132. UNUSED(context);
  133. // reset GET request flags
  134. sent_get_request = false;
  135. get_success = false;
  136. already_success = false;
  137. // free file read widget if it exists
  138. if (app_instance->widget_file_read)
  139. {
  140. widget_reset(app_instance->widget_file_read);
  141. }
  142. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  143. }
  144. static uint32_t web_crawler_back_to_file_callback(void *context)
  145. {
  146. UNUSED(context);
  147. return WebCrawlerViewVariableItemListFile; // Return to the file submenu
  148. }
  149. static uint32_t web_crawler_back_to_wifi_callback(void *context)
  150. {
  151. UNUSED(context);
  152. return WebCrawlerViewVariableItemListWifi; // Return to the wifi submenu
  153. }
  154. static uint32_t web_crawler_back_to_request_callback(void *context)
  155. {
  156. UNUSED(context);
  157. return WebCrawlerViewVariableItemListRequest; // Return to the request submenu
  158. }
  159. /**
  160. * @brief Navigation callback to handle exiting the app from the main submenu.
  161. * @param context The context - unused
  162. * @return VIEW_NONE to exit the app
  163. */
  164. static uint32_t web_crawler_exit_app_callback(void *context)
  165. {
  166. UNUSED(context);
  167. return VIEW_NONE;
  168. }
  169. /**
  170. * @brief Handle submenu item selection.
  171. * @param context The context - WebCrawlerApp object.
  172. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  173. */
  174. static void web_crawler_submenu_callback(void *context, uint32_t index)
  175. {
  176. WebCrawlerApp *app = (WebCrawlerApp *)context;
  177. if (app->view_dispatcher)
  178. {
  179. switch (index)
  180. {
  181. case WebCrawlerSubmenuIndexRun:
  182. sent_get_request = false; // Reset the flag
  183. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewRun);
  184. break;
  185. case WebCrawlerSubmenuIndexAbout:
  186. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewAbout);
  187. break;
  188. case WebCrawlerSubmenuIndexConfig:
  189. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  190. break;
  191. case WebCrawlerSubmenuIndexWifi:
  192. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  193. break;
  194. case WebCrawlerSubmenuIndexRequest:
  195. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  196. break;
  197. case WebCrawlerSubmenuIndexFile:
  198. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  199. break;
  200. default:
  201. FURI_LOG_E(TAG, "Unknown submenu index");
  202. break;
  203. }
  204. }
  205. }
  206. /**
  207. * @brief Configuration enter callback to handle different items.
  208. * @param context The context - WebCrawlerApp object.
  209. * @param index The index of the item that was clicked.
  210. */
  211. static void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  212. {
  213. switch (index)
  214. {
  215. case 0: // SSID
  216. web_crawler_setting_item_ssid_clicked(context, index);
  217. break;
  218. case 1: // Password
  219. web_crawler_setting_item_password_clicked(context, index);
  220. break;
  221. default:
  222. FURI_LOG_E(TAG, "Unknown configuration item index");
  223. break;
  224. }
  225. }
  226. /**
  227. * @brief Configuration enter callback to handle different items.
  228. * @param context The context - WebCrawlerApp object.
  229. * @param index The index of the item that was clicked.
  230. */
  231. static void web_crawler_file_enter_callback(void *context, uint32_t index)
  232. {
  233. switch (index)
  234. {
  235. case 0: // File Read
  236. web_crawler_setting_item_file_read_clicked(context, index);
  237. break;
  238. case 1: // FIle Type
  239. web_crawler_setting_item_file_type_clicked(context, index);
  240. break;
  241. case 2: // File Rename
  242. web_crawler_setting_item_file_rename_clicked(context, index);
  243. break;
  244. case 3: // File Delete
  245. web_crawler_setting_item_file_delete_clicked(context, index);
  246. break;
  247. default:
  248. FURI_LOG_E(TAG, "Unknown configuration item index");
  249. break;
  250. }
  251. }
  252. /**
  253. * @brief Configuration enter callback to handle different items.
  254. * @param context The context - WebCrawlerApp object.
  255. * @param index The index of the item that was clicked.
  256. */
  257. static void web_crawler_request_enter_callback(void *context, uint32_t index)
  258. {
  259. switch (index)
  260. {
  261. case 0: // URL
  262. web_crawler_setting_item_path_clicked(context, index);
  263. break;
  264. default:
  265. FURI_LOG_E(TAG, "Unknown configuration item index");
  266. break;
  267. }
  268. }
  269. /**
  270. * @brief Callback for when the user finishes entering the URL.
  271. * @param context The context - WebCrawlerApp object.
  272. */
  273. static void web_crawler_set_path_updated(void *context)
  274. {
  275. WebCrawlerApp *app = (WebCrawlerApp *)context;
  276. if (!app)
  277. {
  278. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  279. return;
  280. }
  281. if (!app->path || !app->temp_buffer_path || !app->temp_buffer_size_path || !app->path_item)
  282. {
  283. FURI_LOG_E(TAG, "Invalid path buffer");
  284. return;
  285. }
  286. // Store the entered URL from temp_buffer_path to path
  287. strncpy(app->path, app->temp_buffer_path, app->temp_buffer_size_path - 1);
  288. if (app->path_item)
  289. {
  290. variable_item_set_current_value_text(app->path_item, app->path);
  291. // Save the URL to the settings
  292. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type);
  293. // send to UART
  294. if (!flipper_http_save_wifi(app->ssid, app->password))
  295. {
  296. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  297. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  298. }
  299. FURI_LOG_D(TAG, "URL saved: %s", app->path);
  300. }
  301. // Return to the Configure view
  302. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  303. }
  304. /**
  305. * @brief Callback for when the user finishes entering the SSID.
  306. * @param context The context - WebCrawlerApp object.
  307. */
  308. static void web_crawler_set_ssid_updated(void *context)
  309. {
  310. WebCrawlerApp *app = (WebCrawlerApp *)context;
  311. if (!app)
  312. {
  313. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  314. return;
  315. }
  316. if (!app->temp_buffer_ssid || !app->temp_buffer_size_ssid || !app->ssid || !app->ssid_item)
  317. {
  318. FURI_LOG_E(TAG, "Invalid SSID buffer");
  319. return;
  320. }
  321. // Store the entered SSID from temp_buffer_ssid to ssid
  322. strncpy(app->ssid, app->temp_buffer_ssid, app->temp_buffer_size_ssid - 1);
  323. if (app->ssid_item)
  324. {
  325. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  326. // Save the SSID to the settings
  327. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type);
  328. // send to UART
  329. if (!flipper_http_save_wifi(app->ssid, app->password))
  330. {
  331. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  332. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  333. }
  334. FURI_LOG_D(TAG, "SSID saved: %s", app->ssid);
  335. }
  336. // Return to the Configure view
  337. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  338. }
  339. /**
  340. * @brief Callback for when the user finishes entering the Password.
  341. * @param context The context - WebCrawlerApp object.
  342. */
  343. static void web_crawler_set_password_update(void *context)
  344. {
  345. WebCrawlerApp *app = (WebCrawlerApp *)context;
  346. if (!app)
  347. {
  348. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  349. return;
  350. }
  351. if (!app->temp_buffer_password || !app->temp_buffer_size_password || !app->password || !app->password_item)
  352. {
  353. FURI_LOG_E(TAG, "Invalid password buffer");
  354. return;
  355. }
  356. // Store the entered Password from temp_buffer_password to password
  357. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  358. if (app->password_item)
  359. {
  360. variable_item_set_current_value_text(app->password_item, app->password);
  361. // Save the Password to the settings
  362. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type);
  363. // send to UART
  364. if (!flipper_http_save_wifi(app->ssid, app->password))
  365. {
  366. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  367. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  368. }
  369. FURI_LOG_D(TAG, "Password saved: %s", app->password);
  370. }
  371. // Return to the Configure view
  372. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  373. }
  374. /**
  375. * @brief Callback for when the user finishes entering the File Type.
  376. * @param context The context - WebCrawlerApp object.
  377. */
  378. static void web_crawler_set_file_type_update(void *context)
  379. {
  380. WebCrawlerApp *app = (WebCrawlerApp *)context;
  381. if (!app)
  382. {
  383. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  384. return;
  385. }
  386. if (!app->temp_buffer_file_type || !app->temp_buffer_size_file_type || !app->file_type || !app->file_type_item)
  387. {
  388. FURI_LOG_E(TAG, "Invalid file type buffer");
  389. return;
  390. }
  391. // Temporary buffer to store the old name
  392. char old_file_type[256];
  393. strncpy(old_file_type, app->file_type, sizeof(old_file_type) - 1);
  394. old_file_type[sizeof(old_file_type) - 1] = '\0'; // Null-terminate
  395. strncpy(app->file_type, app->temp_buffer_file_type, app->temp_buffer_size_file_type - 1);
  396. if (app->file_type_item)
  397. {
  398. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  399. }
  400. rename_received_data(app->file_rename, app->file_rename, app->file_type, old_file_type);
  401. // Return to the Configure view
  402. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  403. }
  404. /**
  405. * @brief Callback for when the user finishes entering the File Rename.
  406. * @param context The context - WebCrawlerApp object.
  407. */
  408. static void web_crawler_set_file_rename_update(void *context)
  409. {
  410. WebCrawlerApp *app = (WebCrawlerApp *)context;
  411. if (!app)
  412. {
  413. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  414. return;
  415. }
  416. if (!app->temp_buffer_file_rename || !app->temp_buffer_size_file_rename || !app->file_rename || !app->file_rename_item)
  417. {
  418. FURI_LOG_E(TAG, "Invalid file rename buffer");
  419. return;
  420. }
  421. // Temporary buffer to store the old name
  422. char old_name[256];
  423. // Ensure that app->file_rename is null-terminated
  424. strncpy(old_name, app->file_rename, sizeof(old_name) - 1);
  425. old_name[sizeof(old_name) - 1] = '\0'; // Null-terminate
  426. // Store the entered File Rename from temp_buffer_file_rename to file_rename
  427. strncpy(app->file_rename, app->temp_buffer_file_rename, app->temp_buffer_size_file_rename - 1);
  428. if (app->file_rename_item)
  429. {
  430. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  431. }
  432. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  433. // Return to the Configure view
  434. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  435. }
  436. /**
  437. * @brief Handler for Path configuration item click.
  438. * @param context The context - WebCrawlerApp object.
  439. * @param index The index of the item that was clicked.
  440. */
  441. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  442. {
  443. WebCrawlerApp *app = (WebCrawlerApp *)context;
  444. if (!app)
  445. {
  446. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  447. return;
  448. }
  449. if (!app->text_input_path)
  450. {
  451. FURI_LOG_E(TAG, "Text input is NULL");
  452. return;
  453. }
  454. UNUSED(index);
  455. // Set up the text input
  456. text_input_set_header_text(app->text_input_path, "Enter URL");
  457. // Initialize temp_buffer with existing path
  458. if (app->path && strlen(app->path) > 0)
  459. {
  460. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  461. }
  462. else
  463. {
  464. strncpy(app->temp_buffer_path, "https://www.google.com/", app->temp_buffer_size_path - 1);
  465. }
  466. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  467. // Configure the text input
  468. bool clear_previous_text = false;
  469. text_input_set_result_callback(
  470. app->text_input_path,
  471. web_crawler_set_path_updated,
  472. app,
  473. app->temp_buffer_path,
  474. app->temp_buffer_size_path,
  475. clear_previous_text);
  476. // Set the previous callback to return to Configure screen
  477. view_set_previous_callback(
  478. text_input_get_view(app->text_input_path),
  479. web_crawler_back_to_request_callback);
  480. // Show text input dialog
  481. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  482. }
  483. /**
  484. * @brief Handler for SSID configuration item click.
  485. * @param context The context - WebCrawlerApp object.
  486. * @param index The index of the item that was clicked.
  487. */
  488. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  489. {
  490. WebCrawlerApp *app = (WebCrawlerApp *)context;
  491. if (!app)
  492. {
  493. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  494. return;
  495. }
  496. UNUSED(index);
  497. if (!app->text_input_ssid)
  498. {
  499. FURI_LOG_E(TAG, "Text input is NULL");
  500. return;
  501. }
  502. // Set up the text input
  503. text_input_set_header_text(app->text_input_ssid, "Enter SSID");
  504. // Initialize temp_buffer with existing SSID
  505. if (app->ssid && strlen(app->ssid) > 0)
  506. {
  507. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  508. }
  509. else
  510. {
  511. strncpy(app->temp_buffer_ssid, "SSID-2G-", app->temp_buffer_size_ssid - 1);
  512. }
  513. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  514. // Configure the text input
  515. bool clear_previous_text = false;
  516. text_input_set_result_callback(
  517. app->text_input_ssid,
  518. web_crawler_set_ssid_updated,
  519. app,
  520. app->temp_buffer_ssid,
  521. app->temp_buffer_size_ssid,
  522. clear_previous_text);
  523. // Set the previous callback to return to Configure screen
  524. view_set_previous_callback(
  525. text_input_get_view(app->text_input_ssid),
  526. web_crawler_back_to_wifi_callback);
  527. // Show text input dialog
  528. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  529. }
  530. /**
  531. * @brief Handler for Password configuration item click.
  532. * @param context The context - WebCrawlerApp object.
  533. * @param index The index of the item that was clicked.
  534. */
  535. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  536. {
  537. WebCrawlerApp *app = (WebCrawlerApp *)context;
  538. if (!app)
  539. {
  540. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  541. return;
  542. }
  543. UNUSED(index);
  544. if (!app->text_input_password)
  545. {
  546. FURI_LOG_E(TAG, "Text input is NULL");
  547. return;
  548. }
  549. // Set up the text input
  550. text_input_set_header_text(app->text_input_password, "Enter Password");
  551. // Initialize temp_buffer with existing password
  552. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  553. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  554. // Configure the text input
  555. bool clear_previous_text = false;
  556. text_input_set_result_callback(
  557. app->text_input_password,
  558. web_crawler_set_password_update,
  559. app,
  560. app->temp_buffer_password,
  561. app->temp_buffer_size_password,
  562. clear_previous_text);
  563. // Set the previous callback to return to Configure screen
  564. view_set_previous_callback(
  565. text_input_get_view(app->text_input_password),
  566. web_crawler_back_to_wifi_callback);
  567. // Show text input dialog
  568. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  569. }
  570. /**
  571. * @brief Handler for File Type configuration item click.
  572. * @param context The context - WebCrawlerApp object.
  573. * @param index The index of the item that was clicked.
  574. */
  575. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  576. {
  577. WebCrawlerApp *app = (WebCrawlerApp *)context;
  578. if (!app)
  579. {
  580. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  581. return;
  582. }
  583. UNUSED(index);
  584. if (!app->text_input_file_type)
  585. {
  586. FURI_LOG_E(TAG, "Text input is NULL");
  587. return;
  588. }
  589. // Set up the text input
  590. text_input_set_header_text(app->text_input_file_type, "Enter File Type");
  591. // Initialize temp_buffer with existing file_type
  592. if (app->file_type && strlen(app->file_type) > 0)
  593. {
  594. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  595. }
  596. else
  597. {
  598. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  599. }
  600. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  601. // Configure the text input
  602. bool clear_previous_text = false;
  603. text_input_set_result_callback(
  604. app->text_input_file_type,
  605. web_crawler_set_file_type_update,
  606. app,
  607. app->temp_buffer_file_type,
  608. app->temp_buffer_size_file_type,
  609. clear_previous_text);
  610. // Set the previous callback to return to Configure screen
  611. view_set_previous_callback(
  612. text_input_get_view(app->text_input_file_type),
  613. web_crawler_back_to_file_callback);
  614. // Show text input dialog
  615. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileType);
  616. }
  617. /**
  618. * @brief Handler for File Rename configuration item click.
  619. * @param context The context - WebCrawlerApp object.
  620. * @param index The index of the item that was clicked.
  621. */
  622. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  623. {
  624. WebCrawlerApp *app = (WebCrawlerApp *)context;
  625. if (!app)
  626. {
  627. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  628. return;
  629. }
  630. UNUSED(index);
  631. if (!app->text_input_file_rename)
  632. {
  633. FURI_LOG_E(TAG, "Text input is NULL");
  634. return;
  635. }
  636. // Set up the text input
  637. text_input_set_header_text(app->text_input_file_rename, "Enter File Rename");
  638. // Initialize temp_buffer with existing file_rename
  639. if (app->file_rename && strlen(app->file_rename) > 0)
  640. {
  641. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  642. }
  643. else
  644. {
  645. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  646. }
  647. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  648. // Configure the text input
  649. bool clear_previous_text = false;
  650. text_input_set_result_callback(
  651. app->text_input_file_rename,
  652. web_crawler_set_file_rename_update,
  653. app,
  654. app->temp_buffer_file_rename,
  655. app->temp_buffer_size_file_rename,
  656. clear_previous_text);
  657. // Set the previous callback to return to Configure screen
  658. view_set_previous_callback(
  659. text_input_get_view(app->text_input_file_rename),
  660. web_crawler_back_to_file_callback);
  661. // Show text input dialog
  662. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileRename);
  663. }
  664. /**
  665. * @brief Handler for File Delete configuration item click.
  666. * @param context The context - WebCrawlerApp object.
  667. * @param index The index of the item that was clicked.
  668. */
  669. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  670. {
  671. WebCrawlerApp *app = (WebCrawlerApp *)context;
  672. if (!app)
  673. {
  674. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  675. return;
  676. }
  677. UNUSED(index);
  678. if (!delete_received_data(app))
  679. {
  680. FURI_LOG_E(TAG, "Failed to delete file");
  681. }
  682. // Set the previous callback to return to Configure screen
  683. view_set_previous_callback(
  684. widget_get_view(app->widget_file_delete),
  685. web_crawler_back_to_file_callback);
  686. // Show text input dialog
  687. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileDelete);
  688. }
  689. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  690. {
  691. WebCrawlerApp *app = (WebCrawlerApp *)context;
  692. if (!app)
  693. {
  694. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  695. return;
  696. }
  697. UNUSED(index);
  698. if (!load_received_data(app))
  699. {
  700. if (app->widget_file_read)
  701. {
  702. widget_reset(app->widget_file_read);
  703. widget_add_text_scroll_element(
  704. app->widget_file_read,
  705. 0,
  706. 0,
  707. 128,
  708. 64, "File is empty.");
  709. }
  710. }
  711. // Set the previous callback to return to Configure screen
  712. view_set_previous_callback(
  713. widget_get_view(app->widget_file_read),
  714. web_crawler_back_to_file_callback);
  715. // Show text input dialog
  716. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  717. }