Skip to content

Commit

Permalink
Clang format ran
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Oct 18, 2022
1 parent 0483d8f commit 526213c
Show file tree
Hide file tree
Showing 48 changed files with 2,121 additions and 1,846 deletions.
4 changes: 2 additions & 2 deletions scenes/add_new_token/totp_input_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void view_unlock_model(View* view) {
}

static void commit_text_input_callback(void* context) {
InputTextSceneState* text_input_state = (InputTextSceneState *)context;
if (text_input_state->callback != 0) {
InputTextSceneState* text_input_state = (InputTextSceneState*)context;
if(text_input_state->callback != 0) {
InputTextSceneCallbackResult* result = malloc(sizeof(InputTextSceneCallbackResult));
result->user_input_length = strlen(text_input_state->text_input_buffer);
result->user_input = malloc(result->user_input_length + 1);
Expand Down
242 changes: 140 additions & 102 deletions scenes/add_new_token/totp_scene_add_new_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "../generate_token/totp_scene_generate_token.h"

#define TOKEN_ALGO_LIST_LENGTH 3
char* TOKEN_ALGO_LIST[] = { "SHA1", "SHA256", "SHA512" };
char* TOKEN_ALGO_LIST[] = {"SHA1", "SHA256", "SHA512"};
#define TOKEN_DIGITS_LIST_LENGTH 2
char* TOKEN_DIGITS_LIST[] = { "6 digits", "8 digits" };
char* TOKEN_DIGITS_LIST[] = {"6 digits", "8 digits"};

typedef enum {
TokenNameTextBox,
Expand Down Expand Up @@ -62,7 +62,9 @@ static void on_token_secret_user_comitted(InputTextSceneCallbackResult* result)
free(result);
}

void totp_scene_add_new_token_activate(PluginState* plugin_state, const TokenAddEditSceneContext* context) {
void totp_scene_add_new_token_activate(
PluginState* plugin_state,
const TokenAddEditSceneContext* context) {
SceneState* scene_state = malloc(sizeof(SceneState));
plugin_state->current_scene_state = scene_state;
scene_state->token_name = "Name";
Expand All @@ -84,25 +86,52 @@ void totp_scene_add_new_token_activate(PluginState* plugin_state, const TokenAdd

scene_state->input_state = NULL;

if (context == NULL) {
if(context == NULL) {
scene_state->current_token_index = -1;
} else {
scene_state->current_token_index = context->current_token_index;
}
}

void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_state) {
SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
if (scene_state->input_started_at > 0) {
SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
if(scene_state->input_started_at > 0) {
totp_input_text_render(canvas, scene_state->input_state);
return;
}

ui_control_text_box_render(canvas, 10 - scene_state->screen_y_offset, scene_state->token_name, scene_state->selected_control == TokenNameTextBox);
ui_control_text_box_render(canvas, 27 - scene_state->screen_y_offset, scene_state->token_secret, scene_state->selected_control == TokenSecretTextBox);
ui_control_select_render(canvas, 0, 44 - scene_state->screen_y_offset, SCREEN_WIDTH, TOKEN_ALGO_LIST[scene_state->algo], scene_state->selected_control == TokenAlgoSelect);
ui_control_select_render(canvas, 0, 63 - scene_state->screen_y_offset, SCREEN_WIDTH, TOKEN_DIGITS_LIST[scene_state->digits_count], scene_state->selected_control == TokenLengthSelect);
ui_control_button_render(canvas, SCREEN_WIDTH_CENTER - 24, 85 - scene_state->screen_y_offset, 48, 13, "Confirm", scene_state->selected_control == ConfirmButton);
ui_control_text_box_render(
canvas,
10 - scene_state->screen_y_offset,
scene_state->token_name,
scene_state->selected_control == TokenNameTextBox);
ui_control_text_box_render(
canvas,
27 - scene_state->screen_y_offset,
scene_state->token_secret,
scene_state->selected_control == TokenSecretTextBox);
ui_control_select_render(
canvas,
0,
44 - scene_state->screen_y_offset,
SCREEN_WIDTH,
TOKEN_ALGO_LIST[scene_state->algo],
scene_state->selected_control == TokenAlgoSelect);
ui_control_select_render(
canvas,
0,
63 - scene_state->screen_y_offset,
SCREEN_WIDTH,
TOKEN_DIGITS_LIST[scene_state->digits_count],
scene_state->selected_control == TokenLengthSelect);
ui_control_button_render(
canvas,
SCREEN_WIDTH_CENTER - 24,
85 - scene_state->screen_y_offset,
48,
13,
"Confirm",
scene_state->selected_control == ConfirmButton);

canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, 0, 0, SCREEN_WIDTH, 10);
Expand All @@ -113,7 +142,7 @@ void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_s
}

void update_screen_y_offset(SceneState* scene_state) {
if (scene_state->selected_control > TokenAlgoSelect) {
if(scene_state->selected_control > TokenAlgoSelect) {
scene_state->screen_y_offset = 35;
} else {
scene_state->screen_y_offset = 0;
Expand All @@ -122,121 +151,130 @@ void update_screen_y_offset(SceneState* scene_state) {

bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState* plugin_state) {
if(event->type == EventTypeKey) {
SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
if (scene_state->input_started_at > 0 && furi_get_tick() - scene_state->input_started_at > 300) {
SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
if(scene_state->input_started_at > 0 &&
furi_get_tick() - scene_state->input_started_at > 300) {
return totp_input_text_handle_event(event, scene_state->input_state);
}

if (event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
return false;
} else if(event->input.type == InputTypePress) {
switch(event->input.key) {
case InputKeyUp:
if (scene_state->selected_control > TokenNameTextBox) {
scene_state->selected_control--;
update_screen_y_offset(scene_state);
case InputKeyUp:
if(scene_state->selected_control > TokenNameTextBox) {
scene_state->selected_control--;
update_screen_y_offset(scene_state);
}
break;
case InputKeyDown:
if(scene_state->selected_control < ConfirmButton) {
scene_state->selected_control++;
update_screen_y_offset(scene_state);
}
break;
case InputKeyRight:
if(scene_state->selected_control == TokenAlgoSelect) {
if(scene_state->algo < SHA512) {
scene_state->algo++;
} else {
scene_state->algo = SHA1;
}
break;
case InputKeyDown:
if (scene_state->selected_control < ConfirmButton) {
scene_state->selected_control++;
update_screen_y_offset(scene_state);
} else if(scene_state->selected_control == TokenLengthSelect) {
if(scene_state->digits_count < TOTP_8_DIGITS) {
scene_state->digits_count++;
} else {
scene_state->digits_count = TOTP_6_DIGITS;
}
break;
case InputKeyRight:
if (scene_state->selected_control == TokenAlgoSelect) {
if (scene_state->algo < SHA512) {
scene_state->algo++;
} else {
scene_state->algo = SHA1;
}
}
break;
case InputKeyLeft:
if(scene_state->selected_control == TokenAlgoSelect) {
if(scene_state->algo > SHA1) {
scene_state->algo--;
} else {
scene_state->algo = SHA512;
}
} else if(scene_state->selected_control == TokenLengthSelect) {
if(scene_state->digits_count > TOTP_6_DIGITS) {
scene_state->digits_count--;
} else {
scene_state->digits_count = TOTP_8_DIGITS;
}
else if (scene_state->selected_control == TokenLengthSelect) {
if (scene_state->digits_count < TOTP_8_DIGITS) {
scene_state->digits_count++;
} else {
scene_state->digits_count = TOTP_6_DIGITS;
}
}
break;
case InputKeyOk:
switch(scene_state->selected_control) {
case TokenNameTextBox:
if(scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}
scene_state->input_state =
totp_input_text_activate(scene_state->token_name_input_context);
scene_state->input_started_at = furi_get_tick();
break;
case InputKeyLeft:
if (scene_state->selected_control == TokenAlgoSelect) {
if (scene_state->algo > SHA1) {
scene_state->algo--;
} else {
scene_state->algo = SHA512;
}
}
else if (scene_state->selected_control == TokenLengthSelect) {
if (scene_state->digits_count > TOTP_6_DIGITS) {
scene_state->digits_count--;
} else {
scene_state->digits_count = TOTP_8_DIGITS;
}
case TokenSecretTextBox:
if(scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}
scene_state->input_state =
totp_input_text_activate(scene_state->token_secret_input_context);
scene_state->input_started_at = furi_get_tick();
break;
case InputKeyOk:
switch (scene_state->selected_control) {
case TokenNameTextBox:
if (scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}
scene_state->input_state = totp_input_text_activate(scene_state->token_name_input_context);
scene_state->input_started_at = furi_get_tick();
break;
case TokenSecretTextBox:
if (scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}
scene_state->input_state = totp_input_text_activate(scene_state->token_secret_input_context);
scene_state->input_started_at = furi_get_tick();
break;
case TokenAlgoSelect:
break;
case TokenLengthSelect:
break;
case ConfirmButton: {
TokenInfo* tokenInfo = token_info_alloc();
tokenInfo->name = malloc(scene_state->token_name_length + 1);
strcpy(tokenInfo->name, scene_state->token_name);

token_info_set_secret(tokenInfo, scene_state->token_secret, scene_state->token_secret_length, &plugin_state->iv[0]);
case TokenAlgoSelect:
break;
case TokenLengthSelect:
break;
case ConfirmButton: {
TokenInfo* tokenInfo = token_info_alloc();
tokenInfo->name = malloc(scene_state->token_name_length + 1);
strcpy(tokenInfo->name, scene_state->token_name);

tokenInfo->algo = scene_state->algo;
tokenInfo->digits = scene_state->digits_count;

if (plugin_state->tokens_list == NULL) {
plugin_state->tokens_list = list_init_head(tokenInfo);
} else {
list_add(plugin_state->tokens_list, tokenInfo);
}
plugin_state->tokens_count++;
token_info_set_secret(
tokenInfo,
scene_state->token_secret,
scene_state->token_secret_length,
&plugin_state->iv[0]);

totp_config_file_save_new_token(tokenInfo);
tokenInfo->algo = scene_state->algo;
tokenInfo->digits = scene_state->digits_count;

GenerateTokenSceneContext generate_scene_context = { .current_token_index = plugin_state->tokens_count - 1 };
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, &generate_scene_context);
break;
}
}
break;
case InputKeyBack:
if (scene_state->current_token_index >= 0) {
GenerateTokenSceneContext generate_scene_context = { .current_token_index = scene_state->current_token_index };
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, &generate_scene_context);
if(plugin_state->tokens_list == NULL) {
plugin_state->tokens_list = list_init_head(tokenInfo);
} else {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
list_add(plugin_state->tokens_list, tokenInfo);
}
plugin_state->tokens_count++;

totp_config_file_save_new_token(tokenInfo);

GenerateTokenSceneContext generate_scene_context = {
.current_token_index = plugin_state->tokens_count - 1};
totp_scene_director_activate_scene(
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
break;
}
}
break;
case InputKeyBack:
if(scene_state->current_token_index >= 0) {
GenerateTokenSceneContext generate_scene_context = {
.current_token_index = scene_state->current_token_index};
totp_scene_director_activate_scene(
plugin_state, TotpSceneGenerateToken, &generate_scene_context);
} else {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}
break;
}
}
}
return true;
}

void totp_scene_add_new_token_deactivate(PluginState* plugin_state) {
if (plugin_state->current_scene_state == NULL) return;
SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
if(plugin_state->current_scene_state == NULL) return;
SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
free(scene_state->token_name);
free(scene_state->token_secret);

Expand All @@ -246,7 +284,7 @@ void totp_scene_add_new_token_deactivate(PluginState* plugin_state) {
free(scene_state->token_secret_input_context->header_text);
free(scene_state->token_secret_input_context);

if (scene_state->input_state != NULL) {
if(scene_state->input_state != NULL) {
totp_input_text_free(scene_state->input_state);
}

Expand Down
4 changes: 3 additions & 1 deletion scenes/add_new_token/totp_scene_add_new_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ typedef struct {
} TokenAddEditSceneContext;

void totp_scene_add_new_token_init(PluginState* plugin_state);
void totp_scene_add_new_token_activate(PluginState* plugin_state, const TokenAddEditSceneContext* context);
void totp_scene_add_new_token_activate(
PluginState* plugin_state,
const TokenAddEditSceneContext* context);
void totp_scene_add_new_token_render(Canvas* const canvas, PluginState* plugin_state);
bool totp_scene_add_new_token_handle_event(PluginEvent* const event, PluginState* plugin_state);
void totp_scene_add_new_token_deactivate(PluginState* plugin_state);
Expand Down
Loading

0 comments on commit 526213c

Please sign in to comment.