timezone.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. char* strtof_endptr;
  28. float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr);
  29. if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) {
  30. plugin_state->timezone_offset = tz;
  31. if(totp_config_file_update_timezone_offset(tz) == TotpConfigFileUpdateSuccess) {
  32. TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", (double)tz);
  33. } else {
  34. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  35. }
  36. if(plugin_state->current_scene == TotpSceneGenerateToken) {
  37. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  38. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  39. } else if(plugin_state->current_scene == TotpSceneAppSettings) {
  40. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  41. totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings, NULL);
  42. }
  43. } else {
  44. TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n");
  45. }
  46. } else {
  47. TOTP_CLI_PRINTF_INFO(
  48. "Current timezone offset is %f\r\n", (double)plugin_state->timezone_offset);
  49. }
  50. furi_string_free(temp_str);
  51. }