infrared_app_remote_manager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <file_worker_cpp.h>
  2. #include <flipper_format/flipper_format.h>
  3. #include "infrared_app_remote_manager.h"
  4. #include "infrared/helpers/infrared_parser.h"
  5. #include "infrared/infrared_app_signal.h"
  6. #include <utility>
  7. #include <infrared.h>
  8. #include <cstdio>
  9. #include <furi.h>
  10. #include <gui/modules/button_menu.h>
  11. #include <storage/storage.h>
  12. #include "infrared_app.h"
  13. static const std::string default_remote_name = "remote";
  14. std::string InfraredAppRemoteManager::make_full_name(const std::string& remote_name) const {
  15. return std::string("") + InfraredApp::infrared_directory + "/" + remote_name +
  16. InfraredApp::infrared_extension;
  17. }
  18. std::string InfraredAppRemoteManager::find_vacant_remote_name(const std::string& name) {
  19. bool exist = true;
  20. FileWorkerCpp file_worker;
  21. if(!file_worker.is_file_exist(make_full_name(name).c_str(), &exist)) {
  22. return std::string();
  23. } else if(!exist) {
  24. return name;
  25. }
  26. /* if suggested name is occupied, try another one (name2, name3, etc) */
  27. uint32_t i = 1;
  28. bool file_worker_result = false;
  29. std::string new_name;
  30. do {
  31. new_name = make_full_name(name + std::to_string(++i));
  32. file_worker_result = file_worker.is_file_exist(new_name.c_str(), &exist);
  33. } while(file_worker_result && exist);
  34. return !exist ? name + std::to_string(i) : std::string();
  35. }
  36. bool InfraredAppRemoteManager::add_button(const char* button_name, const InfraredAppSignal& signal) {
  37. remote->buttons.emplace_back(button_name, signal);
  38. return store();
  39. }
  40. bool InfraredAppRemoteManager::add_remote_with_button(
  41. const char* button_name,
  42. const InfraredAppSignal& signal) {
  43. furi_check(button_name != nullptr);
  44. auto new_name = find_vacant_remote_name(default_remote_name);
  45. if(new_name.empty()) {
  46. return false;
  47. }
  48. remote = std::make_unique<InfraredAppRemote>(new_name);
  49. return add_button(button_name, signal);
  50. }
  51. std::vector<std::string> InfraredAppRemoteManager::get_button_list(void) const {
  52. std::vector<std::string> name_vector;
  53. name_vector.reserve(remote->buttons.size());
  54. for(const auto& it : remote->buttons) {
  55. name_vector.emplace_back(it.name);
  56. }
  57. // copy elision
  58. return name_vector;
  59. }
  60. const InfraredAppSignal& InfraredAppRemoteManager::get_button_data(size_t index) const {
  61. furi_check(remote.get() != nullptr);
  62. auto& buttons = remote->buttons;
  63. furi_check(index < buttons.size());
  64. return buttons.at(index).signal;
  65. }
  66. bool InfraredAppRemoteManager::delete_remote() {
  67. bool result;
  68. FileWorkerCpp file_worker;
  69. result = file_worker.remove(make_full_name(remote->name).c_str());
  70. reset_remote();
  71. return result;
  72. }
  73. void InfraredAppRemoteManager::reset_remote() {
  74. remote.reset();
  75. }
  76. bool InfraredAppRemoteManager::delete_button(uint32_t index) {
  77. furi_check(remote.get() != nullptr);
  78. auto& buttons = remote->buttons;
  79. furi_check(index < buttons.size());
  80. buttons.erase(buttons.begin() + index);
  81. return store();
  82. }
  83. std::string InfraredAppRemoteManager::get_button_name(uint32_t index) {
  84. furi_check(remote.get() != nullptr);
  85. auto& buttons = remote->buttons;
  86. furi_check(index < buttons.size());
  87. return buttons[index].name.c_str();
  88. }
  89. std::string InfraredAppRemoteManager::get_remote_name() {
  90. return remote.get() ? remote->name : std::string();
  91. }
  92. bool InfraredAppRemoteManager::rename_remote(const char* str) {
  93. furi_check(str != nullptr);
  94. furi_check(remote.get() != nullptr);
  95. if(!remote->name.compare(str)) {
  96. return true;
  97. }
  98. auto new_name = find_vacant_remote_name(str);
  99. if(new_name.empty()) {
  100. return false;
  101. }
  102. FileWorkerCpp file_worker;
  103. std::string old_filename = make_full_name(remote->name);
  104. std::string new_filename = make_full_name(new_name);
  105. bool result = file_worker.rename(old_filename.c_str(), new_filename.c_str());
  106. remote->name = new_name;
  107. return result;
  108. }
  109. bool InfraredAppRemoteManager::rename_button(uint32_t index, const char* str) {
  110. furi_check(remote.get() != nullptr);
  111. auto& buttons = remote->buttons;
  112. furi_check(index < buttons.size());
  113. buttons[index].name = str;
  114. return store();
  115. }
  116. size_t InfraredAppRemoteManager::get_number_of_buttons() {
  117. furi_check(remote.get() != nullptr);
  118. return remote->buttons.size();
  119. }
  120. bool InfraredAppRemoteManager::store(void) {
  121. bool result = false;
  122. FileWorkerCpp file_worker;
  123. if(!file_worker.mkdir(InfraredApp::infrared_directory)) return false;
  124. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  125. FlipperFormat* ff = flipper_format_file_alloc(storage);
  126. FURI_LOG_I("RemoteManager", "store file: \'%s\'", make_full_name(remote->name).c_str());
  127. result = flipper_format_file_open_always(ff, make_full_name(remote->name).c_str());
  128. if(result) {
  129. result = flipper_format_write_header_cstr(ff, "IR signals file", 1);
  130. }
  131. if(result) {
  132. for(const auto& button : remote->buttons) {
  133. result = infrared_parser_save_signal(ff, button.signal, button.name.c_str());
  134. if(!result) {
  135. break;
  136. }
  137. }
  138. }
  139. flipper_format_free(ff);
  140. furi_record_close("storage");
  141. return result;
  142. }
  143. bool InfraredAppRemoteManager::load(const std::string& remote_name) {
  144. bool result = false;
  145. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  146. FlipperFormat* ff = flipper_format_file_alloc(storage);
  147. FURI_LOG_I("RemoteManager", "load file: \'%s\'", make_full_name(remote_name).c_str());
  148. result = flipper_format_file_open_existing(ff, make_full_name(remote_name).c_str());
  149. if(result) {
  150. string_t header;
  151. string_init(header);
  152. uint32_t version;
  153. result = flipper_format_read_header(ff, header, &version);
  154. if(result) {
  155. result = !string_cmp_str(header, "IR signals file") && (version == 1);
  156. }
  157. string_clear(header);
  158. }
  159. if(result) {
  160. remote = std::make_unique<InfraredAppRemote>(remote_name);
  161. InfraredAppSignal signal;
  162. std::string signal_name;
  163. while(infrared_parser_read_signal(ff, signal, signal_name)) {
  164. remote->buttons.emplace_back(signal_name.c_str(), std::move(signal));
  165. }
  166. }
  167. flipper_format_free(ff);
  168. furi_record_close("storage");
  169. return result;
  170. }