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