irda_app_remote_manager.cpp 6.4 KB

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