-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscene_save_code.c
112 lines (82 loc) · 3.7 KB
/
scene_save_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Takes the code in result_code and opens a file name KB
Saves the code into that file and then goes to the send code
dialog and clears app state
TODO:
Codes need @ and ^ to denote checksum and xor characters
Codes should save using the special characters listed on
https://humulos.com/digimon/0NL1NE/converter/
In order to do this I should save a code-type
When loading a code type, I can assume saving response codes as the same type.
If there is no code type on load, and there are no special chars, I can have the user
specify the code type.
Code type should be specified on save as well for manual add
*/
#include "flipper.h"
#include "app_state.h"
#include "scenes.h"
#include "scene_save_code.h"
void save_text_input_callback(void* context) {
App* app = context;
FURI_LOG_I(TAG, "save_text_input_callback %s", app->state->file_name_tmp);
FlipperFormat* fff_file = flipper_format_file_alloc(app->storage);
FuriString* path = furi_string_alloc();
// Add the .digirom extension so it will show up in the file picker
furi_string_printf(path, "%s/%s.digirom",APP_DIRECTORY_PATH, app->state->file_name_tmp);
const char* path_cstr = furi_string_get_cstr(path);
if(storage_file_exists(app->storage, path_cstr)) {
storage_simply_remove(app->storage, path_cstr);
}
// Open File, create if not exists
if(!storage_common_stat(app->storage, path_cstr, NULL) == FSE_OK) {
FURI_LOG_D(TAG, "Config file %s is not found. Will create new.", path_cstr);
if(storage_common_stat(app->storage, APP_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
FURI_LOG_D(
TAG,
"Directory %s doesn't exist. Will create new.",
APP_DIRECTORY_PATH);
if(!storage_simply_mkdir(app->storage, APP_DIRECTORY_PATH)) {
FURI_LOG_E(TAG, "Error creating directory %s", APP_DIRECTORY_PATH);
}
}
}
do {
if(!flipper_format_file_open_new(fff_file, path_cstr)) break;
if(!flipper_format_write_header_cstr(fff_file, "F-Com Code File", 0)) break;
if(!flipper_format_write_comment_cstr(fff_file, "Digimon DMCommm Code")) break;
if(!flipper_format_write_string_cstr(fff_file, "Code", app->state->result_code)) break;
// signal that the file was written successfully
} while(0);
flipper_format_file_close(fff_file);
flipper_format_free(fff_file);
// After we save, switch to main menu
scene_manager_search_and_switch_to_previous_scene(app->scene_manager, app->state->save_code_return_scene);
}
void fcom_save_code_scene_on_enter(void* context) {
FURI_LOG_I(TAG, "fcom_save_code_scene_on_enter");
App* app = context;
text_input_reset(app->text_input);
text_input_set_header_text(app->text_input, "Set filename");
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
"apppath",
".txt",
"code.txt");
text_input_set_validator(app->text_input, validator_is_file_callback, validator_is_file);
text_input_set_result_callback(app->text_input,
save_text_input_callback,
app,
app->state->file_name_tmp,
MAX_FILENAME_LEN,
true);
view_dispatcher_switch_to_view(app->view_dispatcher, FcomKeyboardView);
}
bool fcom_save_code_scene_on_event(void* context, SceneManagerEvent event) {
FURI_LOG_I(TAG, "fcom_save_code_scene_on_event");
UNUSED(context);
UNUSED(event);
return false; //consumed event
}
void fcom_save_code_scene_on_exit(void* context) {
FURI_LOG_I(TAG, "fcom_save_code_scene_on_exit");
UNUSED(context);
}