infrared_app_remote_manager.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "m-string.h"
  2. #include "storage/filesystem_api_defines.h"
  3. #include <flipper_format/flipper_format.h>
  4. #include "infrared_app_remote_manager.h"
  5. #include "infrared/helpers/infrared_parser.h"
  6. #include "infrared/infrared_app_signal.h"
  7. #include <utility>
  8. #include <infrared.h>
  9. #include <cstdio>
  10. #include <furi.h>
  11. #include <gui/modules/button_menu.h>
  12. #include <storage/storage.h>
  13. #include "infrared_app.h"
  14. #include <toolbox/path.h>
  15. static const char* default_remote_name = "remote";
  16. void InfraredAppRemoteManager::find_vacant_remote_name(string_t name, string_t path) {
  17. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  18. string_t base_path;
  19. string_init_set(base_path, path);
  20. if(string_end_with_str_p(base_path, InfraredApp::infrared_extension)) {
  21. size_t filename_start = string_search_rchar(base_path, '/');
  22. string_left(base_path, filename_start);
  23. }
  24. string_printf(
  25. base_path,
  26. "%s/%s%s",
  27. string_get_cstr(path),
  28. string_get_cstr(name),
  29. InfraredApp::infrared_extension);
  30. FS_Error error = storage_common_stat(storage, string_get_cstr(base_path), NULL);
  31. if(error == FSE_OK) {
  32. /* if suggested name is occupied, try another one (name2, name3, etc) */
  33. size_t dot = string_search_rchar(base_path, '.');
  34. string_left(base_path, dot);
  35. string_t path_temp;
  36. string_init(path_temp);
  37. uint32_t i = 1;
  38. do {
  39. string_printf(
  40. path_temp,
  41. "%s%u%s",
  42. string_get_cstr(base_path),
  43. ++i,
  44. InfraredApp::infrared_extension);
  45. error = storage_common_stat(storage, string_get_cstr(path_temp), NULL);
  46. } while(error == FSE_OK);
  47. string_clear(path_temp);
  48. if(error == FSE_NOT_EXIST) {
  49. string_cat_printf(name, "%u", i);
  50. }
  51. }
  52. string_clear(base_path);
  53. furi_record_close("storage");
  54. }
  55. bool InfraredAppRemoteManager::add_button(const char* button_name, const InfraredAppSignal& signal) {
  56. remote->buttons.emplace_back(button_name, signal);
  57. return store();
  58. }
  59. bool InfraredAppRemoteManager::add_remote_with_button(
  60. const char* button_name,
  61. const InfraredAppSignal& signal) {
  62. furi_check(button_name != nullptr);
  63. string_t new_name;
  64. string_init_set_str(new_name, default_remote_name);
  65. string_t new_path;
  66. string_init_set_str(new_path, InfraredApp::infrared_directory);
  67. find_vacant_remote_name(new_name, new_path);
  68. string_cat_printf(
  69. new_path, "/%s%s", string_get_cstr(new_name), InfraredApp::infrared_extension);
  70. remote = std::make_unique<InfraredAppRemote>(new_path);
  71. remote->name = std::string(string_get_cstr(new_name));
  72. string_clear(new_path);
  73. string_clear(new_name);
  74. return add_button(button_name, signal);
  75. }
  76. std::vector<std::string> InfraredAppRemoteManager::get_button_list(void) const {
  77. std::vector<std::string> name_vector;
  78. name_vector.reserve(remote->buttons.size());
  79. for(const auto& it : remote->buttons) {
  80. name_vector.emplace_back(it.name);
  81. }
  82. // copy elision
  83. return name_vector;
  84. }
  85. const InfraredAppSignal& InfraredAppRemoteManager::get_button_data(size_t index) const {
  86. furi_check(remote.get() != nullptr);
  87. auto& buttons = remote->buttons;
  88. furi_check(index < buttons.size());
  89. return buttons.at(index).signal;
  90. }
  91. bool InfraredAppRemoteManager::delete_remote() {
  92. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  93. FS_Error error = storage_common_remove(storage, string_get_cstr(remote->path));
  94. reset_remote();
  95. furi_record_close("storage");
  96. return (error == FSE_OK || error == FSE_NOT_EXIST);
  97. }
  98. void InfraredAppRemoteManager::reset_remote() {
  99. remote.reset();
  100. }
  101. bool InfraredAppRemoteManager::delete_button(uint32_t index) {
  102. furi_check(remote.get() != nullptr);
  103. auto& buttons = remote->buttons;
  104. furi_check(index < buttons.size());
  105. buttons.erase(buttons.begin() + index);
  106. return store();
  107. }
  108. std::string InfraredAppRemoteManager::get_button_name(uint32_t index) {
  109. furi_check(remote.get() != nullptr);
  110. auto& buttons = remote->buttons;
  111. furi_check(index < buttons.size());
  112. return buttons[index].name.c_str();
  113. }
  114. std::string InfraredAppRemoteManager::get_remote_name() {
  115. return remote.get() ? remote->name : std::string();
  116. }
  117. bool InfraredAppRemoteManager::rename_remote(const char* str) {
  118. furi_check(str != nullptr);
  119. furi_check(remote.get() != nullptr);
  120. furi_check(!string_empty_p(remote->path));
  121. if(!remote->name.compare(str)) {
  122. return true;
  123. }
  124. string_t new_name;
  125. string_init_set_str(new_name, str);
  126. find_vacant_remote_name(new_name, remote->path);
  127. string_t new_path;
  128. string_init_set(new_path, remote->path);
  129. if(string_end_with_str_p(new_path, InfraredApp::infrared_extension)) {
  130. size_t filename_start = string_search_rchar(new_path, '/');
  131. string_left(new_path, filename_start);
  132. }
  133. string_cat_printf(
  134. new_path, "/%s%s", string_get_cstr(new_name), InfraredApp::infrared_extension);
  135. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  136. FS_Error error =
  137. storage_common_rename(storage, string_get_cstr(remote->path), string_get_cstr(new_path));
  138. remote->name = std::string(string_get_cstr(new_name));
  139. string_clear(new_name);
  140. string_clear(new_path);
  141. furi_record_close("storage");
  142. return (error == FSE_OK || error == FSE_EXIST);
  143. }
  144. bool InfraredAppRemoteManager::rename_button(uint32_t index, const char* str) {
  145. furi_check(remote.get() != nullptr);
  146. auto& buttons = remote->buttons;
  147. furi_check(index < buttons.size());
  148. buttons[index].name = str;
  149. return store();
  150. }
  151. size_t InfraredAppRemoteManager::get_number_of_buttons() {
  152. furi_check(remote.get() != nullptr);
  153. return remote->buttons.size();
  154. }
  155. bool InfraredAppRemoteManager::store(void) {
  156. bool result = false;
  157. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  158. if(!storage_simply_mkdir(storage, InfraredApp::infrared_directory)) return false;
  159. FlipperFormat* ff = flipper_format_file_alloc(storage);
  160. FURI_LOG_I("RemoteManager", "store file: \'%s\'", string_get_cstr(remote->path));
  161. result = flipper_format_file_open_always(ff, string_get_cstr(remote->path));
  162. if(result) {
  163. result = flipper_format_write_header_cstr(ff, "IR signals file", 1);
  164. }
  165. if(result) {
  166. for(const auto& button : remote->buttons) {
  167. result = infrared_parser_save_signal(ff, button.signal, button.name.c_str());
  168. if(!result) {
  169. break;
  170. }
  171. }
  172. }
  173. flipper_format_free(ff);
  174. furi_record_close("storage");
  175. return result;
  176. }
  177. bool InfraredAppRemoteManager::load(string_t path) {
  178. bool result = false;
  179. Storage* storage = static_cast<Storage*>(furi_record_open("storage"));
  180. FlipperFormat* ff = flipper_format_file_alloc(storage);
  181. FURI_LOG_I("RemoteManager", "load file: \'%s\'", string_get_cstr(path));
  182. result = flipper_format_file_open_existing(ff, string_get_cstr(path));
  183. if(result) {
  184. string_t header;
  185. string_init(header);
  186. uint32_t version;
  187. result = flipper_format_read_header(ff, header, &version);
  188. if(result) {
  189. result = !string_cmp_str(header, "IR signals file") && (version == 1);
  190. }
  191. string_clear(header);
  192. }
  193. if(result) {
  194. string_t new_name;
  195. string_init(new_name);
  196. remote = std::make_unique<InfraredAppRemote>(path);
  197. path_extract_filename(path, new_name, true);
  198. remote->name = std::string(string_get_cstr(new_name));
  199. string_clear(new_name);
  200. InfraredAppSignal signal;
  201. std::string signal_name;
  202. while(infrared_parser_read_signal(ff, signal, signal_name)) {
  203. remote->buttons.emplace_back(signal_name.c_str(), std::move(signal));
  204. }
  205. }
  206. flipper_format_free(ff);
  207. furi_record_close("storage");
  208. return result;
  209. }