web_crawler_callback.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. // web_crawler_callback.h
  2. static bool sent_http_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_headers_clicked(void *context, uint32_t index);
  9. static void web_crawler_setting_item_payload_clicked(void *context, uint32_t index);
  10. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index);
  11. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index);
  12. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index);
  13. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index);
  14. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index);
  15. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index);
  16. static void web_crawler_http_method_change(VariableItem *item)
  17. {
  18. uint8_t index = variable_item_get_current_value_index(item);
  19. variable_item_set_current_value_text(item, http_method_names[index]);
  20. // save the http method
  21. if (app_instance)
  22. {
  23. strncpy(app_instance->http_method, http_method_names[index], strlen(http_method_names[index]) + 1);
  24. // save the settings
  25. save_settings(
  26. app_instance->path,
  27. app_instance->ssid,
  28. app_instance->password,
  29. app_instance->file_rename,
  30. app_instance->file_type,
  31. app_instance->http_method,
  32. app_instance->headers,
  33. app_instance->payload);
  34. }
  35. }
  36. static void web_crawler_view_draw_callback(Canvas *canvas, void *context)
  37. {
  38. UNUSED(context);
  39. if (!app_instance)
  40. {
  41. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  42. return;
  43. }
  44. if (!canvas)
  45. {
  46. FURI_LOG_E(TAG, "Canvas is NULL");
  47. return;
  48. }
  49. canvas_clear(canvas);
  50. canvas_set_font(canvas, FontSecondary);
  51. if (fhttp.state == INACTIVE)
  52. {
  53. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  54. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  55. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  56. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  57. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  58. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  59. return;
  60. }
  61. if (app_instance->path)
  62. {
  63. if (!sent_http_request)
  64. {
  65. if (strstr(app_instance->http_method, "GET") != NULL)
  66. {
  67. canvas_draw_str(canvas, 0, 10, "Sending GET request...");
  68. // Perform GET request and handle the response
  69. if (app_instance->headers == NULL || app_instance->headers[0] == '\0' || strstr(app_instance->headers, " ") == NULL)
  70. {
  71. get_success = flipper_http_get_request(app_instance->path);
  72. }
  73. else
  74. {
  75. get_success = flipper_http_get_request_with_headers(app_instance->path, app_instance->headers);
  76. }
  77. }
  78. else if (strstr(app_instance->http_method, "POST") != NULL)
  79. {
  80. canvas_draw_str(canvas, 0, 10, "Sending POST request...");
  81. // Perform POST request and handle the response
  82. get_success = flipper_http_post_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  83. }
  84. else if (strstr(app_instance->http_method, "PUT") != NULL)
  85. {
  86. canvas_draw_str(canvas, 0, 10, "Sending PUT request...");
  87. // Perform PUT request and handle the response
  88. get_success = flipper_http_put_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  89. }
  90. else
  91. {
  92. canvas_draw_str(canvas, 0, 10, "Sending DELETE request...");
  93. // Perform DELETE request and handle the response
  94. get_success = flipper_http_delete_request_with_headers(app_instance->path, app_instance->headers, app_instance->payload);
  95. }
  96. canvas_draw_str(canvas, 0, 20, "Sent!");
  97. if (get_success)
  98. {
  99. canvas_draw_str(canvas, 0, 30, "Receiving data...");
  100. // Set the state to RECEIVING to ensure we continue to see the receiving message
  101. fhttp.state = RECEIVING;
  102. }
  103. else
  104. {
  105. canvas_draw_str(canvas, 0, 30, "Failed.");
  106. }
  107. sent_http_request = true;
  108. }
  109. else
  110. {
  111. // print state
  112. if (get_success && fhttp.state == RECEIVING)
  113. {
  114. canvas_draw_str(canvas, 0, 10, "Receiving and parsing data...");
  115. }
  116. else if (get_success && fhttp.state == IDLE)
  117. {
  118. canvas_draw_str(canvas, 0, 10, "Data saved to file.");
  119. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  120. }
  121. else
  122. {
  123. if (fhttp.state == ISSUE)
  124. {
  125. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  126. {
  127. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  128. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  129. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  130. }
  131. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  132. {
  133. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  134. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  135. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  136. }
  137. else
  138. {
  139. canvas_draw_str(canvas, 0, 10, "[ERROR] Failed to sync data.");
  140. canvas_draw_str(canvas, 0, 30, "If this is your third attempt,");
  141. canvas_draw_str(canvas, 0, 40, "it's likely your URL is not");
  142. canvas_draw_str(canvas, 0, 50, "compabilbe or correct.");
  143. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  144. }
  145. }
  146. else
  147. {
  148. canvas_draw_str(canvas, 0, 10, "HTTP request failed.");
  149. canvas_draw_str(canvas, 0, 20, "Press BACK to return.");
  150. }
  151. get_success = false;
  152. }
  153. }
  154. }
  155. else
  156. {
  157. canvas_draw_str(canvas, 0, 10, "URL not set.");
  158. }
  159. }
  160. /**
  161. * @brief Navigation callback to handle exiting from other views to the submenu.
  162. * @param context The context - WebCrawlerApp object.
  163. * @return WebCrawlerViewSubmenu
  164. */
  165. static uint32_t web_crawler_back_to_configure_callback(void *context)
  166. {
  167. UNUSED(context);
  168. // free file read widget if it exists
  169. if (app_instance->widget_file_read)
  170. {
  171. widget_reset(app_instance->widget_file_read);
  172. }
  173. return WebCrawlerViewSubmenuConfig; // Return to the configure screen
  174. }
  175. /**
  176. * @brief Navigation callback to handle returning to the Wifi Settings screen.
  177. * @param context The context - WebCrawlerApp object.
  178. * @return WebCrawlerViewSubmenu
  179. */
  180. static uint32_t web_crawler_back_to_main_callback(void *context)
  181. {
  182. UNUSED(context);
  183. // reset GET request flags
  184. sent_http_request = false;
  185. get_success = false;
  186. already_success = false;
  187. // free file read widget if it exists
  188. if (app_instance->widget_file_read)
  189. {
  190. widget_reset(app_instance->widget_file_read);
  191. }
  192. return WebCrawlerViewSubmenuMain; // Return to the main submenu
  193. }
  194. static uint32_t web_crawler_back_to_file_callback(void *context)
  195. {
  196. UNUSED(context);
  197. return WebCrawlerViewVariableItemListFile; // Return to the file submenu
  198. }
  199. static uint32_t web_crawler_back_to_wifi_callback(void *context)
  200. {
  201. UNUSED(context);
  202. return WebCrawlerViewVariableItemListWifi; // Return to the wifi submenu
  203. }
  204. static uint32_t web_crawler_back_to_request_callback(void *context)
  205. {
  206. UNUSED(context);
  207. return WebCrawlerViewVariableItemListRequest; // Return to the request submenu
  208. }
  209. /**
  210. * @brief Navigation callback to handle exiting the app from the main submenu.
  211. * @param context The context - unused
  212. * @return VIEW_NONE to exit the app
  213. */
  214. static uint32_t web_crawler_exit_app_callback(void *context)
  215. {
  216. UNUSED(context);
  217. return VIEW_NONE;
  218. }
  219. /**
  220. * @brief Handle submenu item selection.
  221. * @param context The context - WebCrawlerApp object.
  222. * @param index The WebCrawlerSubmenuIndex item that was clicked.
  223. */
  224. static void web_crawler_submenu_callback(void *context, uint32_t index)
  225. {
  226. WebCrawlerApp *app = (WebCrawlerApp *)context;
  227. if (app->view_dispatcher)
  228. {
  229. switch (index)
  230. {
  231. case WebCrawlerSubmenuIndexRun:
  232. sent_http_request = false; // Reset the flag
  233. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewRun);
  234. break;
  235. case WebCrawlerSubmenuIndexAbout:
  236. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewAbout);
  237. break;
  238. case WebCrawlerSubmenuIndexConfig:
  239. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewSubmenuConfig);
  240. break;
  241. case WebCrawlerSubmenuIndexWifi:
  242. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  243. break;
  244. case WebCrawlerSubmenuIndexRequest:
  245. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  246. break;
  247. case WebCrawlerSubmenuIndexFile:
  248. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  249. break;
  250. default:
  251. FURI_LOG_E(TAG, "Unknown submenu index");
  252. break;
  253. }
  254. }
  255. }
  256. /**
  257. * @brief Configuration enter callback to handle different items.
  258. * @param context The context - WebCrawlerApp object.
  259. * @param index The index of the item that was clicked.
  260. */
  261. static void web_crawler_wifi_enter_callback(void *context, uint32_t index)
  262. {
  263. switch (index)
  264. {
  265. case 0: // SSID
  266. web_crawler_setting_item_ssid_clicked(context, index);
  267. break;
  268. case 1: // Password
  269. web_crawler_setting_item_password_clicked(context, index);
  270. break;
  271. default:
  272. FURI_LOG_E(TAG, "Unknown configuration item index");
  273. break;
  274. }
  275. }
  276. /**
  277. * @brief Configuration enter callback to handle different items.
  278. * @param context The context - WebCrawlerApp object.
  279. * @param index The index of the item that was clicked.
  280. */
  281. static void web_crawler_file_enter_callback(void *context, uint32_t index)
  282. {
  283. switch (index)
  284. {
  285. case 0: // File Read
  286. web_crawler_setting_item_file_read_clicked(context, index);
  287. break;
  288. case 1: // FIle Type
  289. web_crawler_setting_item_file_type_clicked(context, index);
  290. break;
  291. case 2: // File Rename
  292. web_crawler_setting_item_file_rename_clicked(context, index);
  293. break;
  294. case 3: // File Delete
  295. web_crawler_setting_item_file_delete_clicked(context, index);
  296. break;
  297. default:
  298. FURI_LOG_E(TAG, "Unknown configuration item index");
  299. break;
  300. }
  301. }
  302. /**
  303. * @brief Configuration enter callback to handle different items.
  304. * @param context The context - WebCrawlerApp object.
  305. * @param index The index of the item that was clicked.
  306. */
  307. static void web_crawler_request_enter_callback(void *context, uint32_t index)
  308. {
  309. switch (index)
  310. {
  311. case 0: // URL
  312. web_crawler_setting_item_path_clicked(context, index);
  313. break;
  314. case 1:
  315. // HTTP Method
  316. break;
  317. case 2:
  318. // Headers
  319. web_crawler_setting_item_headers_clicked(context, index);
  320. break;
  321. case 3:
  322. // Payload
  323. web_crawler_setting_item_payload_clicked(context, index);
  324. break;
  325. default:
  326. FURI_LOG_E(TAG, "Unknown configuration item index");
  327. break;
  328. }
  329. }
  330. /**
  331. * @brief Callback for when the user finishes entering the URL.
  332. * @param context The context - WebCrawlerApp object.
  333. */
  334. static void web_crawler_set_path_updated(void *context)
  335. {
  336. WebCrawlerApp *app = (WebCrawlerApp *)context;
  337. if (!app)
  338. {
  339. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  340. return;
  341. }
  342. if (!app->path || !app->temp_buffer_path || !app->temp_buffer_size_path || !app->path_item)
  343. {
  344. FURI_LOG_E(TAG, "Invalid path buffer");
  345. return;
  346. }
  347. // Store the entered URL from temp_buffer_path to path
  348. strncpy(app->path, app->temp_buffer_path, app->temp_buffer_size_path - 1);
  349. if (app->path_item)
  350. {
  351. variable_item_set_current_value_text(app->path_item, app->path);
  352. // Save the URL to the settings
  353. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  354. }
  355. // Return to the Configure view
  356. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  357. }
  358. /**
  359. * @brief Callback for when the user finishes entering the headers
  360. * @param context The context - WebCrawlerApp object.
  361. */
  362. static void web_crawler_set_headers_updated(void *context)
  363. {
  364. WebCrawlerApp *app = (WebCrawlerApp *)context;
  365. if (!app)
  366. {
  367. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  368. return;
  369. }
  370. if (!app->temp_buffer_headers || !app->temp_buffer_size_headers || !app->headers_item)
  371. {
  372. FURI_LOG_E(TAG, "Invalid headers buffer");
  373. return;
  374. }
  375. // Store the entered headers from temp_buffer_headers to headers
  376. strncpy(app->headers, app->temp_buffer_headers, app->temp_buffer_size_headers - 1);
  377. if (app->headers_item)
  378. {
  379. variable_item_set_current_value_text(app->headers_item, app->headers);
  380. // Save the headers to the settings
  381. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  382. }
  383. // Return to the Configure view
  384. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  385. }
  386. /**
  387. * @brief Callback for when the user finishes entering the payload.
  388. * @param context The context - WebCrawlerApp object.
  389. */
  390. static void web_crawler_set_payload_updated(void *context)
  391. {
  392. WebCrawlerApp *app = (WebCrawlerApp *)context;
  393. if (!app)
  394. {
  395. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  396. return;
  397. }
  398. if (!app->temp_buffer_payload || !app->temp_buffer_size_payload || !app->payload_item)
  399. {
  400. FURI_LOG_E(TAG, "Invalid payload buffer");
  401. return;
  402. }
  403. // Store the entered payload from temp_buffer_payload to payload
  404. strncpy(app->payload, app->temp_buffer_payload, app->temp_buffer_size_payload - 1);
  405. if (app->payload_item)
  406. {
  407. variable_item_set_current_value_text(app->payload_item, app->payload);
  408. // Save the payload to the settings
  409. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  410. }
  411. // Return to the Configure view
  412. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListRequest);
  413. }
  414. /**
  415. * @brief Callback for when the user finishes entering the SSID.
  416. * @param context The context - WebCrawlerApp object.
  417. */
  418. static void web_crawler_set_ssid_updated(void *context)
  419. {
  420. WebCrawlerApp *app = (WebCrawlerApp *)context;
  421. if (!app)
  422. {
  423. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  424. return;
  425. }
  426. if (!app->temp_buffer_ssid || !app->temp_buffer_size_ssid || !app->ssid || !app->ssid_item)
  427. {
  428. FURI_LOG_E(TAG, "Invalid SSID buffer");
  429. return;
  430. }
  431. // Store the entered SSID from temp_buffer_ssid to ssid
  432. strncpy(app->ssid, app->temp_buffer_ssid, app->temp_buffer_size_ssid - 1);
  433. if (app->ssid_item)
  434. {
  435. variable_item_set_current_value_text(app->ssid_item, app->ssid);
  436. // Save the SSID to the settings
  437. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  438. // send to UART
  439. if (!flipper_http_save_wifi(app->ssid, app->password))
  440. {
  441. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  442. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  443. }
  444. }
  445. // Return to the Configure view
  446. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  447. }
  448. /**
  449. * @brief Callback for when the user finishes entering the Password.
  450. * @param context The context - WebCrawlerApp object.
  451. */
  452. static void web_crawler_set_password_update(void *context)
  453. {
  454. WebCrawlerApp *app = (WebCrawlerApp *)context;
  455. if (!app)
  456. {
  457. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  458. return;
  459. }
  460. if (!app->temp_buffer_password || !app->temp_buffer_size_password || !app->password || !app->password_item)
  461. {
  462. FURI_LOG_E(TAG, "Invalid password buffer");
  463. return;
  464. }
  465. // Store the entered Password from temp_buffer_password to password
  466. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  467. if (app->password_item)
  468. {
  469. variable_item_set_current_value_text(app->password_item, app->password);
  470. // Save the Password to the settings
  471. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  472. // send to UART
  473. if (!flipper_http_save_wifi(app->ssid, app->password))
  474. {
  475. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  476. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  477. }
  478. }
  479. // Return to the Configure view
  480. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  481. }
  482. /**
  483. * @brief Callback for when the user finishes entering the File Type.
  484. * @param context The context - WebCrawlerApp object.
  485. */
  486. static void web_crawler_set_file_type_update(void *context)
  487. {
  488. WebCrawlerApp *app = (WebCrawlerApp *)context;
  489. if (!app)
  490. {
  491. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  492. return;
  493. }
  494. if (!app->temp_buffer_file_type || !app->temp_buffer_size_file_type || !app->file_type || !app->file_type_item)
  495. {
  496. FURI_LOG_E(TAG, "Invalid file type buffer");
  497. return;
  498. }
  499. // Temporary buffer to store the old name
  500. char old_file_type[256];
  501. strncpy(old_file_type, app->file_type, sizeof(old_file_type) - 1);
  502. old_file_type[sizeof(old_file_type) - 1] = '\0'; // Null-terminate
  503. strncpy(app->file_type, app->temp_buffer_file_type, app->temp_buffer_size_file_type - 1);
  504. if (app->file_type_item)
  505. {
  506. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  507. // Save the File Type to the settings
  508. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  509. }
  510. rename_received_data(app->file_rename, app->file_rename, app->file_type, old_file_type);
  511. // Return to the Configure view
  512. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  513. }
  514. /**
  515. * @brief Callback for when the user finishes entering the File Rename.
  516. * @param context The context - WebCrawlerApp object.
  517. */
  518. static void web_crawler_set_file_rename_update(void *context)
  519. {
  520. WebCrawlerApp *app = (WebCrawlerApp *)context;
  521. if (!app)
  522. {
  523. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  524. return;
  525. }
  526. if (!app->temp_buffer_file_rename || !app->temp_buffer_size_file_rename || !app->file_rename || !app->file_rename_item)
  527. {
  528. FURI_LOG_E(TAG, "Invalid file rename buffer");
  529. return;
  530. }
  531. // Temporary buffer to store the old name
  532. char old_name[256];
  533. // Ensure that app->file_rename is null-terminated
  534. strncpy(old_name, app->file_rename, sizeof(old_name) - 1);
  535. old_name[sizeof(old_name) - 1] = '\0'; // Null-terminate
  536. // Store the entered File Rename from temp_buffer_file_rename to file_rename
  537. strncpy(app->file_rename, app->temp_buffer_file_rename, app->temp_buffer_size_file_rename - 1);
  538. if (app->file_rename_item)
  539. {
  540. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  541. // Save the File Rename to the settings
  542. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  543. }
  544. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  545. // Return to the Configure view
  546. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  547. }
  548. /**
  549. * @brief Handler for Path configuration item click.
  550. * @param context The context - WebCrawlerApp object.
  551. * @param index The index of the item that was clicked.
  552. */
  553. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  554. {
  555. WebCrawlerApp *app = (WebCrawlerApp *)context;
  556. if (!app)
  557. {
  558. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  559. return;
  560. }
  561. if (!app->text_input_path)
  562. {
  563. FURI_LOG_E(TAG, "Text input is NULL");
  564. return;
  565. }
  566. UNUSED(index);
  567. // Initialize temp_buffer with existing path
  568. if (app->path && strlen(app->path) > 0)
  569. {
  570. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  571. }
  572. else
  573. {
  574. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  575. }
  576. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  577. // Configure the text input
  578. bool clear_previous_text = false;
  579. uart_text_input_set_result_callback(
  580. app->text_input_path,
  581. web_crawler_set_path_updated,
  582. app,
  583. app->temp_buffer_path,
  584. app->temp_buffer_size_path,
  585. clear_previous_text);
  586. // Set the previous callback to return to Configure screen
  587. view_set_previous_callback(
  588. uart_text_input_get_view(app->text_input_path),
  589. web_crawler_back_to_request_callback);
  590. // Show text input dialog
  591. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  592. }
  593. /**
  594. * @brief Handler for headers configuration item click.
  595. * @param context The context - WebCrawlerApp object.
  596. * @param index The index of the item that was clicked.
  597. */
  598. static void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  599. {
  600. WebCrawlerApp *app = (WebCrawlerApp *)context;
  601. if (!app)
  602. {
  603. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  604. return;
  605. }
  606. UNUSED(index);
  607. if (!app->text_input_headers)
  608. {
  609. FURI_LOG_E(TAG, "Text input is NULL");
  610. return;
  611. }
  612. if (!app->headers)
  613. {
  614. FURI_LOG_E(TAG, "Headers is NULL");
  615. return;
  616. }
  617. if (!app->temp_buffer_headers)
  618. {
  619. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  620. return;
  621. }
  622. // Initialize temp_buffer with existing headers
  623. if (app->headers && strlen(app->headers) > 0)
  624. {
  625. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  626. }
  627. else
  628. {
  629. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  630. }
  631. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  632. // Configure the text input
  633. bool clear_previous_text = false;
  634. uart_text_input_set_result_callback(
  635. app->text_input_headers,
  636. web_crawler_set_headers_updated,
  637. app,
  638. app->temp_buffer_headers,
  639. app->temp_buffer_size_headers,
  640. clear_previous_text);
  641. // Set the previous callback to return to Configure screen
  642. view_set_previous_callback(
  643. uart_text_input_get_view(app->text_input_headers),
  644. web_crawler_back_to_request_callback);
  645. // Show text input dialog
  646. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputHeaders);
  647. }
  648. /**
  649. * @brief Handler for payload configuration item click.
  650. * @param context The context - WebCrawlerApp object.
  651. * @param index The index of the item that was clicked.
  652. */
  653. static void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  654. {
  655. WebCrawlerApp *app = (WebCrawlerApp *)context;
  656. if (!app)
  657. {
  658. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  659. return;
  660. }
  661. UNUSED(index);
  662. if (!app->text_input_payload)
  663. {
  664. FURI_LOG_E(TAG, "Text input is NULL");
  665. return;
  666. }
  667. // Initialize temp_buffer with existing payload
  668. if (app->payload && strlen(app->payload) > 0)
  669. {
  670. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  671. }
  672. else
  673. {
  674. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  675. }
  676. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  677. // Configure the text input
  678. bool clear_previous_text = false;
  679. uart_text_input_set_result_callback(
  680. app->text_input_payload,
  681. web_crawler_set_payload_updated,
  682. app,
  683. app->temp_buffer_payload,
  684. app->temp_buffer_size_payload,
  685. clear_previous_text);
  686. // Set the previous callback to return to Configure screen
  687. view_set_previous_callback(
  688. uart_text_input_get_view(app->text_input_payload),
  689. web_crawler_back_to_request_callback);
  690. // Show text input dialog
  691. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPayload);
  692. }
  693. /**
  694. * @brief Handler for SSID configuration item click.
  695. * @param context The context - WebCrawlerApp object.
  696. * @param index The index of the item that was clicked.
  697. */
  698. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  699. {
  700. WebCrawlerApp *app = (WebCrawlerApp *)context;
  701. if (!app)
  702. {
  703. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  704. return;
  705. }
  706. UNUSED(index);
  707. if (!app->text_input_ssid)
  708. {
  709. FURI_LOG_E(TAG, "Text input is NULL");
  710. return;
  711. }
  712. // Initialize temp_buffer with existing SSID
  713. if (app->ssid && strlen(app->ssid) > 0)
  714. {
  715. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  716. }
  717. else
  718. {
  719. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  720. }
  721. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  722. // Configure the text input
  723. bool clear_previous_text = false;
  724. uart_text_input_set_result_callback(
  725. app->text_input_ssid,
  726. web_crawler_set_ssid_updated,
  727. app,
  728. app->temp_buffer_ssid,
  729. app->temp_buffer_size_ssid,
  730. clear_previous_text);
  731. // Set the previous callback to return to Configure screen
  732. view_set_previous_callback(
  733. uart_text_input_get_view(app->text_input_ssid),
  734. web_crawler_back_to_wifi_callback);
  735. // Show text input dialog
  736. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  737. }
  738. /**
  739. * @brief Handler for Password configuration item click.
  740. * @param context The context - WebCrawlerApp object.
  741. * @param index The index of the item that was clicked.
  742. */
  743. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  744. {
  745. WebCrawlerApp *app = (WebCrawlerApp *)context;
  746. if (!app)
  747. {
  748. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  749. return;
  750. }
  751. UNUSED(index);
  752. if (!app->text_input_password)
  753. {
  754. FURI_LOG_E(TAG, "Text input is NULL");
  755. return;
  756. }
  757. // Initialize temp_buffer with existing password
  758. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  759. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  760. // Configure the text input
  761. bool clear_previous_text = false;
  762. uart_text_input_set_result_callback(
  763. app->text_input_password,
  764. web_crawler_set_password_update,
  765. app,
  766. app->temp_buffer_password,
  767. app->temp_buffer_size_password,
  768. clear_previous_text);
  769. // Set the previous callback to return to Configure screen
  770. view_set_previous_callback(
  771. uart_text_input_get_view(app->text_input_password),
  772. web_crawler_back_to_wifi_callback);
  773. // Show text input dialog
  774. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  775. }
  776. /**
  777. * @brief Handler for File Type configuration item click.
  778. * @param context The context - WebCrawlerApp object.
  779. * @param index The index of the item that was clicked.
  780. */
  781. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  782. {
  783. WebCrawlerApp *app = (WebCrawlerApp *)context;
  784. if (!app)
  785. {
  786. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  787. return;
  788. }
  789. UNUSED(index);
  790. if (!app->text_input_file_type)
  791. {
  792. FURI_LOG_E(TAG, "Text input is NULL");
  793. return;
  794. }
  795. // Initialize temp_buffer with existing file_type
  796. if (app->file_type && strlen(app->file_type) > 0)
  797. {
  798. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  799. }
  800. else
  801. {
  802. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  803. }
  804. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  805. // Configure the text input
  806. bool clear_previous_text = false;
  807. uart_text_input_set_result_callback(
  808. app->text_input_file_type,
  809. web_crawler_set_file_type_update,
  810. app,
  811. app->temp_buffer_file_type,
  812. app->temp_buffer_size_file_type,
  813. clear_previous_text);
  814. // Set the previous callback to return to Configure screen
  815. view_set_previous_callback(
  816. uart_text_input_get_view(app->text_input_file_type),
  817. web_crawler_back_to_file_callback);
  818. // Show text input dialog
  819. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileType);
  820. }
  821. /**
  822. * @brief Handler for File Rename configuration item click.
  823. * @param context The context - WebCrawlerApp object.
  824. * @param index The index of the item that was clicked.
  825. */
  826. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  827. {
  828. WebCrawlerApp *app = (WebCrawlerApp *)context;
  829. if (!app)
  830. {
  831. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  832. return;
  833. }
  834. UNUSED(index);
  835. if (!app->text_input_file_rename)
  836. {
  837. FURI_LOG_E(TAG, "Text input is NULL");
  838. return;
  839. }
  840. // Initialize temp_buffer with existing file_rename
  841. if (app->file_rename && strlen(app->file_rename) > 0)
  842. {
  843. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  844. }
  845. else
  846. {
  847. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  848. }
  849. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  850. // Configure the text input
  851. bool clear_previous_text = false;
  852. uart_text_input_set_result_callback(
  853. app->text_input_file_rename,
  854. web_crawler_set_file_rename_update,
  855. app,
  856. app->temp_buffer_file_rename,
  857. app->temp_buffer_size_file_rename,
  858. clear_previous_text);
  859. // Set the previous callback to return to Configure screen
  860. view_set_previous_callback(
  861. uart_text_input_get_view(app->text_input_file_rename),
  862. web_crawler_back_to_file_callback);
  863. // Show text input dialog
  864. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileRename);
  865. }
  866. /**
  867. * @brief Handler for File Delete configuration item click.
  868. * @param context The context - WebCrawlerApp object.
  869. * @param index The index of the item that was clicked.
  870. */
  871. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  872. {
  873. WebCrawlerApp *app = (WebCrawlerApp *)context;
  874. if (!app)
  875. {
  876. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  877. return;
  878. }
  879. UNUSED(index);
  880. if (!delete_received_data(app))
  881. {
  882. FURI_LOG_E(TAG, "Failed to delete file");
  883. }
  884. // Set the previous callback to return to Configure screen
  885. view_set_previous_callback(
  886. widget_get_view(app->widget_file_delete),
  887. web_crawler_back_to_file_callback);
  888. // Show text input dialog
  889. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileDelete);
  890. }
  891. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  892. {
  893. WebCrawlerApp *app = (WebCrawlerApp *)context;
  894. if (!app)
  895. {
  896. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  897. return;
  898. }
  899. UNUSED(index);
  900. if (!load_received_data(app))
  901. {
  902. if (app->widget_file_read)
  903. {
  904. widget_reset(app->widget_file_read);
  905. widget_add_text_scroll_element(
  906. app->widget_file_read,
  907. 0,
  908. 0,
  909. 128,
  910. 64, "File is empty.");
  911. }
  912. }
  913. // Set the previous callback to return to Configure screen
  914. view_set_previous_callback(
  915. widget_get_view(app->widget_file_read),
  916. web_crawler_back_to_file_callback);
  917. // Show text input dialog
  918. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  919. }