notification.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "notification.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_NOTIFICATION_ARG_METHOD "notification"
  7. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "none"
  8. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "sound"
  9. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "vibro"
  10. void totp_cli_command_notification_docopt_commands() {
  11. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NOTIFICATION
  12. " Get or set notification method\r\n");
  13. }
  14. void totp_cli_command_notification_docopt_usage() {
  15. TOTP_CLI_PRINTF(
  16. " " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_NOTIFICATION " " DOCOPT_OPTIONAL(
  17. DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD))) "\r\n");
  18. }
  19. void totp_cli_command_notification_docopt_arguments() {
  20. TOTP_CLI_PRINTF(
  21. " " TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD
  22. " Notification method to be set. Must be one of: " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE
  23. ", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND
  24. ", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\r\n");
  25. }
  26. static void totp_cli_command_notification_print_method(NotificationMethod method, char* color) {
  27. bool has_previous_method = false;
  28. if(method & NotificationMethodSound) {
  29. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
  30. has_previous_method = true;
  31. }
  32. if(method & NotificationMethodVibro) {
  33. if(has_previous_method) {
  34. TOTP_CLI_PRINTF_COLORFUL(color, " and ");
  35. }
  36. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
  37. }
  38. if(method == NotificationMethodNone) {
  39. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
  40. }
  41. }
  42. void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  43. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  44. return;
  45. }
  46. FuriString* temp_str = furi_string_alloc();
  47. bool new_method_provided = false;
  48. NotificationMethod new_method = NotificationMethodNone;
  49. bool args_valid = true;
  50. while(args_read_string_and_trim(args, temp_str)) {
  51. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE) == 0) {
  52. new_method_provided = true;
  53. new_method = NotificationMethodNone;
  54. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND) == 0) {
  55. new_method_provided = true;
  56. new_method |= NotificationMethodSound;
  57. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO) == 0) {
  58. new_method_provided = true;
  59. new_method |= NotificationMethodVibro;
  60. } else {
  61. args_valid = false;
  62. break;
  63. }
  64. }
  65. do {
  66. if(!args_valid) {
  67. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  68. break;
  69. }
  70. if(new_method_provided) {
  71. Scene previous_scene = TotpSceneNone;
  72. if(plugin_state->current_scene == TotpSceneGenerateToken ||
  73. plugin_state->current_scene == TotpSceneAppSettings) {
  74. previous_scene = plugin_state->current_scene;
  75. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  76. }
  77. plugin_state->notification_method = new_method;
  78. if(totp_config_file_update_notification_method(new_method) ==
  79. TotpConfigFileUpdateSuccess) {
  80. TOTP_CLI_PRINTF_SUCCESS("Notification method is set to ");
  81. totp_cli_command_notification_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
  82. cli_nl();
  83. } else {
  84. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  85. }
  86. if(previous_scene != TotpSceneNone) {
  87. totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
  88. }
  89. } else {
  90. TOTP_CLI_PRINTF_INFO("Current notification method is ");
  91. totp_cli_command_notification_print_method(
  92. plugin_state->notification_method, TOTP_CLI_COLOR_INFO);
  93. cli_nl();
  94. }
  95. } while(false);
  96. furi_string_free(temp_str);
  97. }