infrared_app_remote_manager.cpp 6.5 KB

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