notification.c 3.9 KB

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