timezone.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "timezone.h"
  2. #include <lib/toolbox/args.h>
  3. #include "../../../services/config/config.h"
  4. #include "../../../ui/scene_director.h"
  5. #include "../../cli_helpers.h"
  6. #define TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE "timezone"
  7. void totp_cli_command_timezone_docopt_commands() {
  8. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE ", " TOTP_CLI_COMMAND_TIMEZONE_ALT
  9. " Get or set current timezone\r\n");
  10. }
  11. void totp_cli_command_timezone_docopt_usage() {
  12. TOTP_CLI_PRINTF(
  13. " " TOTP_CLI_COMMAND_NAME
  14. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_TIMEZONE " | " TOTP_CLI_COMMAND_TIMEZONE_ALT) " " DOCOPT_OPTIONAL(
  15. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE)) "\r\n");
  16. }
  17. void totp_cli_command_timezone_docopt_arguments() {
  18. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
  19. " Timezone offset in hours to be set\r\n");
  20. }
  21. void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  22. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  23. return;
  24. }
  25. FuriString* temp_str = furi_string_alloc();
  26. if(args_read_string_and_trim(args, temp_str)) {
  27. float tz = strtof(furi_string_get_cstr(temp_str), NULL);
  28. if(tz >= -12.75f && tz <= 12.75f) {
  29. plugin_state->timezone_offset = tz;
  30. if(totp_config_file_update_timezone_offset(tz) == TotpConfigFileUpdateSuccess) {
  31. TOTP_CLI_PRINTF("Timezone is set to %f\r\n", tz);
  32. } else {
  33. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  34. }
  35. if(plugin_state->current_scene == TotpSceneGenerateToken) {
  36. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  37. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  38. } else if(plugin_state->current_scene == TotpSceneAppSettings) {
  39. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  40. totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings, NULL);
  41. }
  42. } else {
  43. TOTP_CLI_PRINTF("Invalid timezone offset\r\n");
  44. }
  45. } else {
  46. TOTP_CLI_PRINTF("Current timezone offset is %f\r\n", plugin_state->timezone_offset);
  47. }
  48. furi_string_free(temp_str);
  49. }