ibutton-scene-save-name.cpp 3.0 KB

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