web_crawler_callback.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 you board is connected,");
  56. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  57. canvas_draw_str(canvas, 0, 52, "your Dev Board with the");
  58. canvas_draw_str(canvas, 0, 62, "FlipperHTTP firmware.");
  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. FURI_LOG_D(TAG, "SSID saved: %s", app->ssid);
  445. }
  446. // Return to the Configure view
  447. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  448. }
  449. /**
  450. * @brief Callback for when the user finishes entering the Password.
  451. * @param context The context - WebCrawlerApp object.
  452. */
  453. static void web_crawler_set_password_update(void *context)
  454. {
  455. WebCrawlerApp *app = (WebCrawlerApp *)context;
  456. if (!app)
  457. {
  458. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  459. return;
  460. }
  461. if (!app->temp_buffer_password || !app->temp_buffer_size_password || !app->password || !app->password_item)
  462. {
  463. FURI_LOG_E(TAG, "Invalid password buffer");
  464. return;
  465. }
  466. // Store the entered Password from temp_buffer_password to password
  467. strncpy(app->password, app->temp_buffer_password, app->temp_buffer_size_password - 1);
  468. if (app->password_item)
  469. {
  470. variable_item_set_current_value_text(app->password_item, app->password);
  471. // Save the Password to the settings
  472. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  473. // send to UART
  474. if (!flipper_http_save_wifi(app->ssid, app->password))
  475. {
  476. FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
  477. FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
  478. }
  479. FURI_LOG_D(TAG, "Password saved: %s", app->password);
  480. }
  481. // Return to the Configure view
  482. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListWifi);
  483. }
  484. /**
  485. * @brief Callback for when the user finishes entering the File Type.
  486. * @param context The context - WebCrawlerApp object.
  487. */
  488. static void web_crawler_set_file_type_update(void *context)
  489. {
  490. WebCrawlerApp *app = (WebCrawlerApp *)context;
  491. if (!app)
  492. {
  493. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  494. return;
  495. }
  496. if (!app->temp_buffer_file_type || !app->temp_buffer_size_file_type || !app->file_type || !app->file_type_item)
  497. {
  498. FURI_LOG_E(TAG, "Invalid file type buffer");
  499. return;
  500. }
  501. // Temporary buffer to store the old name
  502. char old_file_type[256];
  503. strncpy(old_file_type, app->file_type, sizeof(old_file_type) - 1);
  504. old_file_type[sizeof(old_file_type) - 1] = '\0'; // Null-terminate
  505. strncpy(app->file_type, app->temp_buffer_file_type, app->temp_buffer_size_file_type - 1);
  506. if (app->file_type_item)
  507. {
  508. variable_item_set_current_value_text(app->file_type_item, app->file_type);
  509. // Save the File Type to the settings
  510. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  511. }
  512. rename_received_data(app->file_rename, app->file_rename, app->file_type, old_file_type);
  513. // Return to the Configure view
  514. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  515. }
  516. /**
  517. * @brief Callback for when the user finishes entering the File Rename.
  518. * @param context The context - WebCrawlerApp object.
  519. */
  520. static void web_crawler_set_file_rename_update(void *context)
  521. {
  522. WebCrawlerApp *app = (WebCrawlerApp *)context;
  523. if (!app)
  524. {
  525. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  526. return;
  527. }
  528. if (!app->temp_buffer_file_rename || !app->temp_buffer_size_file_rename || !app->file_rename || !app->file_rename_item)
  529. {
  530. FURI_LOG_E(TAG, "Invalid file rename buffer");
  531. return;
  532. }
  533. // Temporary buffer to store the old name
  534. char old_name[256];
  535. // Ensure that app->file_rename is null-terminated
  536. strncpy(old_name, app->file_rename, sizeof(old_name) - 1);
  537. old_name[sizeof(old_name) - 1] = '\0'; // Null-terminate
  538. // Store the entered File Rename from temp_buffer_file_rename to file_rename
  539. strncpy(app->file_rename, app->temp_buffer_file_rename, app->temp_buffer_size_file_rename - 1);
  540. if (app->file_rename_item)
  541. {
  542. variable_item_set_current_value_text(app->file_rename_item, app->file_rename);
  543. // Save the File Rename to the settings
  544. save_settings(app->path, app->ssid, app->password, app->file_rename, app->file_type, app->http_method, app->headers, app->payload);
  545. }
  546. rename_received_data(old_name, app->file_rename, app->file_type, app->file_type);
  547. // Return to the Configure view
  548. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewVariableItemListFile);
  549. }
  550. /**
  551. * @brief Handler for Path configuration item click.
  552. * @param context The context - WebCrawlerApp object.
  553. * @param index The index of the item that was clicked.
  554. */
  555. static void web_crawler_setting_item_path_clicked(void *context, uint32_t index)
  556. {
  557. WebCrawlerApp *app = (WebCrawlerApp *)context;
  558. if (!app)
  559. {
  560. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  561. return;
  562. }
  563. if (!app->text_input_path)
  564. {
  565. FURI_LOG_E(TAG, "Text input is NULL");
  566. return;
  567. }
  568. UNUSED(index);
  569. // Set up the text input
  570. uart_text_input_set_header_text(app->text_input_path, "Enter URL");
  571. // Initialize temp_buffer with existing path
  572. if (app->path && strlen(app->path) > 0)
  573. {
  574. strncpy(app->temp_buffer_path, app->path, app->temp_buffer_size_path - 1);
  575. }
  576. else
  577. {
  578. strncpy(app->temp_buffer_path, "https://httpbin.org/get", app->temp_buffer_size_path - 1);
  579. }
  580. app->temp_buffer_path[app->temp_buffer_size_path - 1] = '\0';
  581. // Configure the text input
  582. bool clear_previous_text = false;
  583. uart_text_input_set_result_callback(
  584. app->text_input_path,
  585. web_crawler_set_path_updated,
  586. app,
  587. app->temp_buffer_path,
  588. app->temp_buffer_size_path,
  589. clear_previous_text);
  590. // Set the previous callback to return to Configure screen
  591. view_set_previous_callback(
  592. uart_text_input_get_view(app->text_input_path),
  593. web_crawler_back_to_request_callback);
  594. // Show text input dialog
  595. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInput);
  596. }
  597. /**
  598. * @brief Handler for headers configuration item click.
  599. * @param context The context - WebCrawlerApp object.
  600. * @param index The index of the item that was clicked.
  601. */
  602. static void web_crawler_setting_item_headers_clicked(void *context, uint32_t index)
  603. {
  604. WebCrawlerApp *app = (WebCrawlerApp *)context;
  605. if (!app)
  606. {
  607. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  608. return;
  609. }
  610. UNUSED(index);
  611. if (!app->text_input_headers)
  612. {
  613. FURI_LOG_E(TAG, "Text input is NULL");
  614. return;
  615. }
  616. if (!app->headers)
  617. {
  618. FURI_LOG_E(TAG, "Headers is NULL");
  619. return;
  620. }
  621. if (!app->temp_buffer_headers)
  622. {
  623. FURI_LOG_E(TAG, "Temp buffer headers is NULL");
  624. return;
  625. }
  626. // Set up the text input
  627. uart_text_input_set_header_text(app->text_input_headers, "Enter Headers");
  628. // Initialize temp_buffer with existing headers
  629. if (app->headers && strlen(app->headers) > 0)
  630. {
  631. strncpy(app->temp_buffer_headers, app->headers, app->temp_buffer_size_headers - 1);
  632. }
  633. else
  634. {
  635. strncpy(app->temp_buffer_headers, "{\"Content-Type\":\"application/json\",\"key\":\"value\"}", app->temp_buffer_size_headers - 1);
  636. }
  637. app->temp_buffer_headers[app->temp_buffer_size_headers - 1] = '\0';
  638. // Configure the text input
  639. bool clear_previous_text = false;
  640. uart_text_input_set_result_callback(
  641. app->text_input_headers,
  642. web_crawler_set_headers_updated,
  643. app,
  644. app->temp_buffer_headers,
  645. app->temp_buffer_size_headers,
  646. clear_previous_text);
  647. // Set the previous callback to return to Configure screen
  648. view_set_previous_callback(
  649. uart_text_input_get_view(app->text_input_headers),
  650. web_crawler_back_to_request_callback);
  651. // Show text input dialog
  652. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputHeaders);
  653. }
  654. /**
  655. * @brief Handler for payload configuration item click.
  656. * @param context The context - WebCrawlerApp object.
  657. * @param index The index of the item that was clicked.
  658. */
  659. static void web_crawler_setting_item_payload_clicked(void *context, uint32_t index)
  660. {
  661. WebCrawlerApp *app = (WebCrawlerApp *)context;
  662. if (!app)
  663. {
  664. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  665. return;
  666. }
  667. UNUSED(index);
  668. if (!app->text_input_payload)
  669. {
  670. FURI_LOG_E(TAG, "Text input is NULL");
  671. return;
  672. }
  673. // Set up the text input
  674. uart_text_input_set_header_text(app->text_input_payload, "Enter Payload");
  675. // Initialize temp_buffer with existing payload
  676. if (app->payload && strlen(app->payload) > 0)
  677. {
  678. strncpy(app->temp_buffer_payload, app->payload, app->temp_buffer_size_payload - 1);
  679. }
  680. else
  681. {
  682. strncpy(app->temp_buffer_payload, "{\"key\":\"value\"}", app->temp_buffer_size_payload - 1);
  683. }
  684. app->temp_buffer_payload[app->temp_buffer_size_payload - 1] = '\0';
  685. // Configure the text input
  686. bool clear_previous_text = false;
  687. uart_text_input_set_result_callback(
  688. app->text_input_payload,
  689. web_crawler_set_payload_updated,
  690. app,
  691. app->temp_buffer_payload,
  692. app->temp_buffer_size_payload,
  693. clear_previous_text);
  694. // Set the previous callback to return to Configure screen
  695. view_set_previous_callback(
  696. uart_text_input_get_view(app->text_input_payload),
  697. web_crawler_back_to_request_callback);
  698. // Show text input dialog
  699. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPayload);
  700. }
  701. /**
  702. * @brief Handler for SSID configuration item click.
  703. * @param context The context - WebCrawlerApp object.
  704. * @param index The index of the item that was clicked.
  705. */
  706. static void web_crawler_setting_item_ssid_clicked(void *context, uint32_t index)
  707. {
  708. WebCrawlerApp *app = (WebCrawlerApp *)context;
  709. if (!app)
  710. {
  711. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  712. return;
  713. }
  714. UNUSED(index);
  715. if (!app->text_input_ssid)
  716. {
  717. FURI_LOG_E(TAG, "Text input is NULL");
  718. return;
  719. }
  720. // Set up the text input
  721. uart_text_input_set_header_text(app->text_input_ssid, "Enter SSID");
  722. // Initialize temp_buffer with existing SSID
  723. if (app->ssid && strlen(app->ssid) > 0)
  724. {
  725. strncpy(app->temp_buffer_ssid, app->ssid, app->temp_buffer_size_ssid - 1);
  726. }
  727. else
  728. {
  729. strncpy(app->temp_buffer_ssid, "", app->temp_buffer_size_ssid - 1);
  730. }
  731. app->temp_buffer_ssid[app->temp_buffer_size_ssid - 1] = '\0';
  732. // Configure the text input
  733. bool clear_previous_text = false;
  734. uart_text_input_set_result_callback(
  735. app->text_input_ssid,
  736. web_crawler_set_ssid_updated,
  737. app,
  738. app->temp_buffer_ssid,
  739. app->temp_buffer_size_ssid,
  740. clear_previous_text);
  741. // Set the previous callback to return to Configure screen
  742. view_set_previous_callback(
  743. uart_text_input_get_view(app->text_input_ssid),
  744. web_crawler_back_to_wifi_callback);
  745. // Show text input dialog
  746. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputSSID);
  747. }
  748. /**
  749. * @brief Handler for Password configuration item click.
  750. * @param context The context - WebCrawlerApp object.
  751. * @param index The index of the item that was clicked.
  752. */
  753. static void web_crawler_setting_item_password_clicked(void *context, uint32_t index)
  754. {
  755. WebCrawlerApp *app = (WebCrawlerApp *)context;
  756. if (!app)
  757. {
  758. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  759. return;
  760. }
  761. UNUSED(index);
  762. if (!app->text_input_password)
  763. {
  764. FURI_LOG_E(TAG, "Text input is NULL");
  765. return;
  766. }
  767. // Set up the text input
  768. uart_text_input_set_header_text(app->text_input_password, "Enter Password");
  769. // Initialize temp_buffer with existing password
  770. strncpy(app->temp_buffer_password, app->password, app->temp_buffer_size_password - 1);
  771. app->temp_buffer_password[app->temp_buffer_size_password - 1] = '\0';
  772. // Configure the text input
  773. bool clear_previous_text = false;
  774. uart_text_input_set_result_callback(
  775. app->text_input_password,
  776. web_crawler_set_password_update,
  777. app,
  778. app->temp_buffer_password,
  779. app->temp_buffer_size_password,
  780. clear_previous_text);
  781. // Set the previous callback to return to Configure screen
  782. view_set_previous_callback(
  783. uart_text_input_get_view(app->text_input_password),
  784. web_crawler_back_to_wifi_callback);
  785. // Show text input dialog
  786. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputPassword);
  787. }
  788. /**
  789. * @brief Handler for File Type configuration item click.
  790. * @param context The context - WebCrawlerApp object.
  791. * @param index The index of the item that was clicked.
  792. */
  793. static void web_crawler_setting_item_file_type_clicked(void *context, uint32_t index)
  794. {
  795. WebCrawlerApp *app = (WebCrawlerApp *)context;
  796. if (!app)
  797. {
  798. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  799. return;
  800. }
  801. UNUSED(index);
  802. if (!app->text_input_file_type)
  803. {
  804. FURI_LOG_E(TAG, "Text input is NULL");
  805. return;
  806. }
  807. // Set up the text input
  808. uart_text_input_set_header_text(app->text_input_file_type, "Enter File Type");
  809. // Initialize temp_buffer with existing file_type
  810. if (app->file_type && strlen(app->file_type) > 0)
  811. {
  812. strncpy(app->temp_buffer_file_type, app->file_type, app->temp_buffer_size_file_type - 1);
  813. }
  814. else
  815. {
  816. strncpy(app->temp_buffer_file_type, ".txt", app->temp_buffer_size_file_type - 1);
  817. }
  818. app->temp_buffer_file_type[app->temp_buffer_size_file_type - 1] = '\0';
  819. // Configure the text input
  820. bool clear_previous_text = false;
  821. uart_text_input_set_result_callback(
  822. app->text_input_file_type,
  823. web_crawler_set_file_type_update,
  824. app,
  825. app->temp_buffer_file_type,
  826. app->temp_buffer_size_file_type,
  827. clear_previous_text);
  828. // Set the previous callback to return to Configure screen
  829. view_set_previous_callback(
  830. uart_text_input_get_view(app->text_input_file_type),
  831. web_crawler_back_to_file_callback);
  832. // Show text input dialog
  833. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileType);
  834. }
  835. /**
  836. * @brief Handler for File Rename configuration item click.
  837. * @param context The context - WebCrawlerApp object.
  838. * @param index The index of the item that was clicked.
  839. */
  840. static void web_crawler_setting_item_file_rename_clicked(void *context, uint32_t index)
  841. {
  842. WebCrawlerApp *app = (WebCrawlerApp *)context;
  843. if (!app)
  844. {
  845. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  846. return;
  847. }
  848. UNUSED(index);
  849. if (!app->text_input_file_rename)
  850. {
  851. FURI_LOG_E(TAG, "Text input is NULL");
  852. return;
  853. }
  854. // Set up the text input
  855. uart_text_input_set_header_text(app->text_input_file_rename, "Enter File Rename");
  856. // Initialize temp_buffer with existing file_rename
  857. if (app->file_rename && strlen(app->file_rename) > 0)
  858. {
  859. strncpy(app->temp_buffer_file_rename, app->file_rename, app->temp_buffer_size_file_rename - 1);
  860. }
  861. else
  862. {
  863. strncpy(app->temp_buffer_file_rename, "received_data", app->temp_buffer_size_file_rename - 1);
  864. }
  865. app->temp_buffer_file_rename[app->temp_buffer_size_file_rename - 1] = '\0';
  866. // Configure the text input
  867. bool clear_previous_text = false;
  868. uart_text_input_set_result_callback(
  869. app->text_input_file_rename,
  870. web_crawler_set_file_rename_update,
  871. app,
  872. app->temp_buffer_file_rename,
  873. app->temp_buffer_size_file_rename,
  874. clear_previous_text);
  875. // Set the previous callback to return to Configure screen
  876. view_set_previous_callback(
  877. uart_text_input_get_view(app->text_input_file_rename),
  878. web_crawler_back_to_file_callback);
  879. // Show text input dialog
  880. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewTextInputFileRename);
  881. }
  882. /**
  883. * @brief Handler for File Delete configuration item click.
  884. * @param context The context - WebCrawlerApp object.
  885. * @param index The index of the item that was clicked.
  886. */
  887. static void web_crawler_setting_item_file_delete_clicked(void *context, uint32_t index)
  888. {
  889. WebCrawlerApp *app = (WebCrawlerApp *)context;
  890. if (!app)
  891. {
  892. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  893. return;
  894. }
  895. UNUSED(index);
  896. if (!delete_received_data(app))
  897. {
  898. FURI_LOG_E(TAG, "Failed to delete file");
  899. }
  900. // Set the previous callback to return to Configure screen
  901. view_set_previous_callback(
  902. widget_get_view(app->widget_file_delete),
  903. web_crawler_back_to_file_callback);
  904. // Show text input dialog
  905. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileDelete);
  906. }
  907. static void web_crawler_setting_item_file_read_clicked(void *context, uint32_t index)
  908. {
  909. WebCrawlerApp *app = (WebCrawlerApp *)context;
  910. if (!app)
  911. {
  912. FURI_LOG_E(TAG, "WebCrawlerApp is NULL");
  913. return;
  914. }
  915. UNUSED(index);
  916. if (!load_received_data(app))
  917. {
  918. if (app->widget_file_read)
  919. {
  920. widget_reset(app->widget_file_read);
  921. widget_add_text_scroll_element(
  922. app->widget_file_read,
  923. 0,
  924. 0,
  925. 128,
  926. 64, "File is empty.");
  927. }
  928. }
  929. // Set the previous callback to return to Configure screen
  930. view_set_previous_callback(
  931. widget_get_view(app->widget_file_read),
  932. web_crawler_back_to_file_callback);
  933. // Show text input dialog
  934. view_dispatcher_switch_to_view(app->view_dispatcher, WebCrawlerViewFileRead);
  935. }