ibutton-scene-save-name.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "ibutton-scene-save-name.h"
  2. #include "../ibutton-app.h"
  3. #include "../ibutton-view-manager.h"
  4. #include "../ibutton-event.h"
  5. #include "../ibutton-key.h"
  6. #include <callback-connector.h>
  7. #include <filesystem-api.h>
  8. void iButtonSceneSaveName::on_enter(iButtonApp* app) {
  9. iButtonAppViewManager* view_manager = app->get_view_manager();
  10. TextInput* text_input = view_manager->get_text_input();
  11. auto callback = cbc::obtain_connector(this, &iButtonSceneSaveName::text_input_callback);
  12. iButtonKey* key = app->get_key();
  13. const char* key_name = key->get_name();
  14. if(strcmp(key_name, "") == 0) {
  15. app->generate_random_name(app->get_text_store(), app->get_text_store_size());
  16. } else {
  17. app->set_text_store("%s", key_name);
  18. }
  19. text_input_set_header_text(text_input, "Name the key");
  20. text_input_set_result_callback(
  21. text_input, callback, app, app->get_text_store(), app->get_text_store_size());
  22. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewTextInput);
  23. }
  24. bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
  25. bool consumed = false;
  26. if(event->type == iButtonEvent::Type::EventTypeTextEditResult) {
  27. iButtonKey* key = app->get_key();
  28. File key_file;
  29. string_t key_file_name;
  30. // Create ibutton directory if necessary
  31. app->get_fs_api()->common.mkdir("ibutton");
  32. // First remove key if it was saved
  33. string_init_set_str(key_file_name, "ibutton/");
  34. string_cat_str(key_file_name, key->get_name());
  35. app->get_fs_api()->common.remove(string_get_cstr(key_file_name));
  36. // Save the key
  37. key->set_name(app->get_text_store());
  38. string_set_str(key_file_name, "ibutton/");
  39. string_cat_str(key_file_name, app->get_text_store());
  40. uint8_t key_data[IBUTTON_KEY_DATA_SIZE + 1];
  41. key_data[0] = static_cast<uint8_t>(key->get_key_type());
  42. memcpy(key_data + 1, key->get_data(), IBUTTON_KEY_DATA_SIZE);
  43. bool res = app->get_fs_api()->file.open(
  44. &key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  45. // TODO process file system errors from file system service
  46. if(res) {
  47. res = app->get_fs_api()->file.write(&key_file, key_data, IBUTTON_KEY_DATA_SIZE + 1);
  48. res = app->get_fs_api()->file.close(&key_file);
  49. app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
  50. } else {
  51. app->get_sd_ex_api()->check_error(app->get_sd_ex_api()->context);
  52. app->search_and_switch_to_previous_scene(
  53. {iButtonApp::Scene::SceneReadedKeyMenu,
  54. iButtonApp::Scene::SceneSavedKeyMenu,
  55. iButtonApp::Scene::SceneAddType});
  56. }
  57. string_clear(key_file_name);
  58. consumed = true;
  59. }
  60. return consumed;
  61. }
  62. void iButtonSceneSaveName::on_exit(iButtonApp* app) {
  63. TextInput* text_input = app->get_view_manager()->get_text_input();
  64. text_input_set_header_text(text_input, "");
  65. text_input_set_result_callback(text_input, NULL, NULL, NULL, 0);
  66. }
  67. void iButtonSceneSaveName::text_input_callback(void* context, char* text) {
  68. iButtonApp* app = static_cast<iButtonApp*>(context);
  69. iButtonEvent event;
  70. event.type = iButtonEvent::Type::EventTypeTextEditResult;
  71. app->get_view_manager()->send_event(&event);
  72. }