ibutton-scene-save-name.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  17. text_input_set_header_text(text_input, "Name the key");
  18. text_input_set_result_callback(
  19. text_input, callback, app, app->get_text_store(), app->get_text_store_size());
  20. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewTextInput);
  21. }
  22. bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
  23. bool consumed = false;
  24. if(event->type == iButtonEvent::Type::EventTypeTextEditResult) {
  25. iButtonKey* key = app->get_key();
  26. File key_file;
  27. string_t key_file_name;
  28. string_init_set_str(key_file_name, "ibutton/");
  29. string_cat_str(key_file_name, app->get_text_store());
  30. uint8_t key_data[IBUTTON_KEY_SIZE + 1];
  31. key_data[0] = static_cast<uint8_t>(key->get_key_type());
  32. memcpy(key_data + 1, key->get_data(), IBUTTON_KEY_SIZE);
  33. // Create ibutton directory if necessary
  34. app->get_fs_api()->common.mkdir("ibutton");
  35. bool res = app->get_fs_api()->file.open(
  36. &key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  37. // TODO process file system errors from file system service
  38. if(res) {
  39. res = app->get_fs_api()->file.write(&key_file, key_data, IBUTTON_KEY_SIZE + 1);
  40. res = app->get_fs_api()->file.close(&key_file);
  41. app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
  42. } else {
  43. app->get_sd_ex_api()->check_error(app->get_sd_ex_api()->context);
  44. app->search_and_switch_to_previous_scene(
  45. {iButtonApp::Scene::SceneReadedKeyMenu,
  46. iButtonApp::Scene::SceneSavedKeyMenu,
  47. iButtonApp::Scene::SceneAddType});
  48. }
  49. string_clear(key_file_name);
  50. consumed = true;
  51. }
  52. return consumed;
  53. }
  54. void iButtonSceneSaveName::on_exit(iButtonApp* app) {
  55. TextInput* text_input = app->get_view_manager()->get_text_input();
  56. text_input_set_header_text(text_input, "");
  57. text_input_set_result_callback(text_input, NULL, NULL, NULL, 0);
  58. }
  59. void iButtonSceneSaveName::text_input_callback(void* context, char* text) {
  60. iButtonApp* app = static_cast<iButtonApp*>(context);
  61. iButtonEvent event;
  62. event.type = iButtonEvent::Type::EventTypeTextEditResult;
  63. app->get_view_manager()->send_event(&event);
  64. }