notification.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <flipper_application/flipper_application.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. #include "../../cli_shared_methods.h"
  7. #include "../../cli_plugin_interface.h"
  8. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "none"
  9. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "sound"
  10. #define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "vibro"
  11. static void
  12. totp_cli_command_notification_print_method(NotificationMethod method, const char* color) {
  13. bool has_previous_method = false;
  14. if(method & NotificationMethodSound) {
  15. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
  16. has_previous_method = true;
  17. }
  18. if(method & NotificationMethodVibro) {
  19. if(has_previous_method) {
  20. TOTP_CLI_PRINTF_COLORFUL(color, " and ");
  21. }
  22. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
  23. }
  24. if(method == NotificationMethodNone) {
  25. TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
  26. }
  27. }
  28. static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe) {
  29. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  30. return;
  31. }
  32. FuriString* temp_str = furi_string_alloc();
  33. bool new_method_provided = false;
  34. NotificationMethod new_method = NotificationMethodNone;
  35. bool args_valid = true;
  36. while(args_read_string_and_trim(args, temp_str)) {
  37. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE) == 0) {
  38. new_method_provided = true;
  39. new_method = NotificationMethodNone;
  40. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND) == 0) {
  41. new_method_provided = true;
  42. new_method |= NotificationMethodSound;
  43. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO) == 0) {
  44. new_method_provided = true;
  45. new_method |= NotificationMethodVibro;
  46. } else {
  47. args_valid = false;
  48. break;
  49. }
  50. }
  51. do {
  52. if(!args_valid) {
  53. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  54. break;
  55. }
  56. if(new_method_provided) {
  57. TOTP_CLI_LOCK_UI(plugin_state);
  58. plugin_state->notification_method = new_method;
  59. if(totp_config_file_update_notification_method(plugin_state)) {
  60. TOTP_CLI_PRINTF_SUCCESS("Notification method is set to ");
  61. totp_cli_command_notification_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
  62. printf("\r\n");
  63. } else {
  64. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  65. }
  66. TOTP_CLI_UNLOCK_UI(plugin_state);
  67. } else {
  68. TOTP_CLI_PRINTF_INFO("Current notification method is ");
  69. totp_cli_command_notification_print_method(
  70. plugin_state->notification_method, TOTP_CLI_COLOR_INFO);
  71. printf("\r\n");
  72. }
  73. } while(false);
  74. furi_string_free(temp_str);
  75. }
  76. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Notification", .handle = &handle};
  77. static const FlipperAppPluginDescriptor plugin_descriptor = {
  78. .appid = PLUGIN_APP_ID,
  79. .ep_api_version = PLUGIN_API_VERSION,
  80. .entry_point = &plugin,
  81. };
  82. const FlipperAppPluginDescriptor* totp_cli_notification_plugin_ep() {
  83. return &plugin_descriptor;
  84. }