file_select.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "file_select.h"
  2. #include <gui/elements.h>
  3. #include <m-string.h>
  4. #include <sys/param.h>
  5. #include <storage/storage.h>
  6. #define FILENAME_COUNT 4
  7. struct FileSelect {
  8. // public
  9. View* view;
  10. Storage* fs_api;
  11. const char* path;
  12. const char* extension;
  13. bool init_completed;
  14. FileSelectCallback callback;
  15. void* context;
  16. char* buffer;
  17. uint8_t buffer_size;
  18. };
  19. typedef struct {
  20. string_t filename[FILENAME_COUNT];
  21. uint8_t position;
  22. uint16_t first_file_index;
  23. uint16_t file_count;
  24. } FileSelectModel;
  25. bool file_select_fill_strings(FileSelect* file_select);
  26. bool file_select_fill_count(FileSelect* file_select);
  27. static bool file_select_init_inner(FileSelect* file_select);
  28. static void file_select_draw_callback(Canvas* canvas, void* _model) {
  29. FileSelectModel* model = _model;
  30. string_t string_buff;
  31. const uint8_t item_height = 16;
  32. const uint8_t item_width = 123;
  33. const uint8_t text_max_width = 115;
  34. canvas_clear(canvas);
  35. canvas_set_font(canvas, FontSecondary);
  36. if(model->file_count) {
  37. for(uint8_t i = 0; i < MIN(FILENAME_COUNT, model->file_count); i++) {
  38. if(i == model->position) {
  39. canvas_set_color(canvas, ColorBlack);
  40. canvas_draw_box(canvas, 0, (i * item_height) + 1, item_width, item_height - 2);
  41. canvas_set_color(canvas, ColorWhite);
  42. canvas_draw_dot(canvas, 0, (i * item_height) + 1);
  43. canvas_draw_dot(canvas, 0, (i * item_height) + item_height - 2);
  44. canvas_draw_dot(canvas, item_width - 1, (i * item_height) + 1);
  45. canvas_draw_dot(canvas, item_width - 1, (i * item_height) + item_height - 2);
  46. } else {
  47. canvas_set_color(canvas, ColorBlack);
  48. }
  49. string_init_set(string_buff, model->filename[i]);
  50. elements_string_fit_width(canvas, string_buff, text_max_width);
  51. canvas_draw_str(
  52. canvas, 6, (i * item_height) + item_height - 4, string_get_cstr(string_buff));
  53. string_clear(string_buff);
  54. }
  55. } else {
  56. canvas_draw_str(canvas, 6, item_height, "Empty folder");
  57. }
  58. elements_scrollbar(canvas, model->first_file_index + model->position, model->file_count);
  59. }
  60. static bool file_select_input_callback(InputEvent* event, void* context) {
  61. FileSelect* file_select = (FileSelect*)context;
  62. bool consumed = false;
  63. if(event->type == InputTypeShort) {
  64. if(!file_select->init_completed) {
  65. if(!file_select_init_inner(file_select)) {
  66. file_select->callback(false, file_select->context);
  67. }
  68. } else if(event->key == InputKeyUp) {
  69. with_view_model(
  70. file_select->view, (FileSelectModel * model) {
  71. if(model->position == 0) {
  72. if(model->first_file_index == 0) {
  73. // wrap
  74. int16_t max_first_file_index = model->file_count - FILENAME_COUNT;
  75. model->position = MIN(FILENAME_COUNT - 1, model->file_count - 1);
  76. model->first_file_index =
  77. max_first_file_index < 0 ? 0 : max_first_file_index;
  78. } else {
  79. model->first_file_index--;
  80. }
  81. } else if(model->position == 1) {
  82. if(model->first_file_index == 0) {
  83. model->position--;
  84. } else {
  85. model->first_file_index--;
  86. }
  87. } else {
  88. model->position--;
  89. }
  90. return true;
  91. });
  92. consumed = true;
  93. if(!file_select_fill_strings(file_select)) {
  94. file_select->callback(false, file_select->context);
  95. }
  96. } else if(event->key == InputKeyDown) {
  97. with_view_model(
  98. file_select->view, (FileSelectModel * model) {
  99. uint16_t max_first_file_index = model->file_count > FILENAME_COUNT ?
  100. model->file_count - FILENAME_COUNT :
  101. 0;
  102. if(model->position >= MIN(FILENAME_COUNT - 1, model->file_count - 1)) {
  103. if(model->first_file_index >= max_first_file_index) {
  104. // wrap
  105. model->position = 0;
  106. model->first_file_index = 0;
  107. } else {
  108. model->first_file_index++;
  109. }
  110. } else if(model->position >= (FILENAME_COUNT - 2)) {
  111. if(model->first_file_index >= max_first_file_index) {
  112. model->position++;
  113. } else {
  114. model->first_file_index++;
  115. }
  116. } else {
  117. model->position++;
  118. }
  119. return true;
  120. });
  121. consumed = true;
  122. if(!file_select_fill_strings(file_select)) {
  123. file_select->callback(false, file_select->context);
  124. }
  125. } else if(event->key == InputKeyOk) {
  126. if(file_select->callback != NULL) {
  127. if(file_select->buffer) {
  128. with_view_model(
  129. file_select->view, (FileSelectModel * model) {
  130. strlcpy(
  131. file_select->buffer,
  132. string_get_cstr(model->filename[model->position]),
  133. file_select->buffer_size);
  134. return false;
  135. });
  136. };
  137. file_select->callback(true, file_select->context);
  138. }
  139. consumed = true;
  140. }
  141. }
  142. return consumed;
  143. }
  144. static bool file_select_init_inner(FileSelect* file_select) {
  145. bool result = false;
  146. if(file_select->path && file_select->extension && file_select->fs_api) {
  147. if(file_select_fill_count(file_select)) {
  148. if(file_select_fill_strings(file_select)) {
  149. file_select->init_completed = true;
  150. result = true;
  151. }
  152. }
  153. }
  154. return result;
  155. }
  156. FileSelect* file_select_alloc() {
  157. FileSelect* file_select = furi_alloc(sizeof(FileSelect));
  158. file_select->view = view_alloc();
  159. file_select->fs_api = furi_record_open("storage");
  160. view_set_context(file_select->view, file_select);
  161. view_allocate_model(file_select->view, ViewModelTypeLockFree, sizeof(FileSelectModel));
  162. view_set_draw_callback(file_select->view, file_select_draw_callback);
  163. view_set_input_callback(file_select->view, file_select_input_callback);
  164. with_view_model(
  165. file_select->view, (FileSelectModel * model) {
  166. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  167. string_init(model->filename[i]);
  168. }
  169. model->first_file_index = 0;
  170. model->file_count = 0;
  171. return false;
  172. });
  173. return file_select;
  174. }
  175. void file_select_free(FileSelect* file_select) {
  176. furi_assert(file_select);
  177. with_view_model(
  178. file_select->view, (FileSelectModel * model) {
  179. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  180. string_clear(model->filename[i]);
  181. }
  182. return false;
  183. });
  184. view_free(file_select->view);
  185. free(file_select);
  186. furi_record_close("storage");
  187. }
  188. View* file_select_get_view(FileSelect* file_select) {
  189. furi_assert(file_select);
  190. return file_select->view;
  191. }
  192. void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context) {
  193. file_select->context = context;
  194. file_select->callback = callback;
  195. }
  196. void file_select_set_filter(FileSelect* file_select, const char* path, const char* extension) {
  197. furi_assert(file_select);
  198. file_select->path = path;
  199. file_select->extension = extension;
  200. }
  201. void file_select_set_result_buffer(FileSelect* file_select, char* buffer, uint8_t buffer_size) {
  202. file_select->buffer = buffer;
  203. file_select->buffer_size = buffer_size;
  204. if(file_select->buffer) {
  205. strlcpy(file_select->buffer, "", file_select->buffer_size);
  206. }
  207. }
  208. bool file_select_init(FileSelect* file_select) {
  209. if(!file_select_init_inner(file_select)) {
  210. file_select->callback(false, file_select->context);
  211. return false;
  212. } else {
  213. return true;
  214. }
  215. }
  216. static bool filter_file(FileSelect* file_select, FileInfo* file_info, char* name) {
  217. bool result = false;
  218. if(!(file_info->flags & FSF_DIRECTORY)) {
  219. if(strcmp(file_select->extension, "*") == 0) {
  220. result = true;
  221. } else if(strstr(name, file_select->extension) != NULL) {
  222. result = true;
  223. }
  224. }
  225. return result;
  226. }
  227. bool file_select_fill_strings(FileSelect* file_select) {
  228. furi_assert(file_select);
  229. furi_assert(file_select->fs_api);
  230. furi_assert(file_select->path);
  231. furi_assert(file_select->extension);
  232. FileInfo file_info;
  233. File* directory = storage_file_alloc(file_select->fs_api);
  234. uint8_t string_counter = 0;
  235. uint16_t file_counter = 0;
  236. const uint8_t name_length = 100;
  237. char* name = furi_alloc(name_length);
  238. uint16_t first_file_index = 0;
  239. with_view_model(
  240. file_select->view, (FileSelectModel * model) {
  241. first_file_index = model->first_file_index;
  242. return false;
  243. });
  244. if(!storage_dir_open(directory, file_select->path)) {
  245. storage_dir_close(directory);
  246. storage_file_free(directory);
  247. free(name);
  248. return true;
  249. }
  250. while(1) {
  251. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  252. break;
  253. }
  254. if(storage_file_get_error(directory) == FSE_OK) {
  255. if(filter_file(file_select, &file_info, name)) {
  256. if(file_counter >= first_file_index) {
  257. with_view_model(
  258. file_select->view, (FileSelectModel * model) {
  259. string_set_str(model->filename[string_counter], name);
  260. if(strcmp(file_select->extension, "*") != 0) {
  261. string_replace_all_str(
  262. model->filename[string_counter], file_select->extension, "");
  263. }
  264. return true;
  265. });
  266. string_counter++;
  267. if(string_counter >= FILENAME_COUNT) {
  268. break;
  269. }
  270. }
  271. file_counter++;
  272. }
  273. } else {
  274. storage_dir_close(directory);
  275. storage_file_free(directory);
  276. free(name);
  277. return false;
  278. }
  279. }
  280. storage_dir_close(directory);
  281. storage_file_free(directory);
  282. free(name);
  283. return true;
  284. }
  285. bool file_select_fill_count(FileSelect* file_select) {
  286. furi_assert(file_select);
  287. furi_assert(file_select->fs_api);
  288. furi_assert(file_select->path);
  289. furi_assert(file_select->extension);
  290. FileInfo file_info;
  291. File* directory = storage_file_alloc(file_select->fs_api);
  292. uint16_t file_counter = 0;
  293. const uint8_t name_length = 100;
  294. char* name = furi_alloc(name_length);
  295. if(!storage_dir_open(directory, file_select->path)) {
  296. storage_dir_close(directory);
  297. storage_file_free(directory);
  298. free(name);
  299. return true;
  300. }
  301. while(1) {
  302. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  303. break;
  304. }
  305. if(storage_file_get_error(directory) == FSE_OK) {
  306. if(filter_file(file_select, &file_info, name)) {
  307. file_counter++;
  308. }
  309. } else {
  310. storage_dir_close(directory);
  311. storage_file_free(directory);
  312. free(name);
  313. return false;
  314. }
  315. }
  316. with_view_model(
  317. file_select->view, (FileSelectModel * model) {
  318. model->file_count = file_counter;
  319. return false;
  320. });
  321. storage_dir_close(directory);
  322. storage_file_free(directory);
  323. free(name);
  324. return true;
  325. }
  326. void file_select_set_selected_file_internal(FileSelect* file_select, const char* filename) {
  327. furi_assert(file_select);
  328. furi_assert(filename);
  329. furi_assert(file_select->fs_api);
  330. furi_assert(file_select->path);
  331. furi_assert(file_select->extension);
  332. if(strlen(filename) == 0) return;
  333. FileInfo file_info;
  334. File* directory = storage_file_alloc(file_select->fs_api);
  335. const uint8_t name_length = 100;
  336. char* name = furi_alloc(name_length);
  337. uint16_t file_position = 0;
  338. bool file_found = false;
  339. string_t filename_str;
  340. string_init_set_str(filename_str, filename);
  341. if(strcmp(file_select->extension, "*") != 0) {
  342. string_cat_str(filename_str, file_select->extension);
  343. }
  344. if(!storage_dir_open(directory, file_select->path)) {
  345. string_clear(filename_str);
  346. storage_dir_close(directory);
  347. storage_file_free(directory);
  348. free(name);
  349. return;
  350. }
  351. while(1) {
  352. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  353. break;
  354. }
  355. if(storage_file_get_error(directory) == FSE_OK) {
  356. if(filter_file(file_select, &file_info, name)) {
  357. if(strcmp(string_get_cstr(filename_str), name) == 0) {
  358. file_found = true;
  359. break;
  360. }
  361. file_position++;
  362. }
  363. } else {
  364. string_clear(filename_str);
  365. storage_dir_close(directory);
  366. storage_file_free(directory);
  367. free(name);
  368. return;
  369. }
  370. }
  371. if(file_found) {
  372. with_view_model(
  373. file_select->view, (FileSelectModel * model) {
  374. uint16_t max_first_file_index =
  375. model->file_count > FILENAME_COUNT ? model->file_count - FILENAME_COUNT : 0;
  376. model->first_file_index = file_position;
  377. if(model->first_file_index > 0) {
  378. model->first_file_index -= 1;
  379. }
  380. if(model->first_file_index >= max_first_file_index) {
  381. model->first_file_index = max_first_file_index;
  382. }
  383. model->position = file_position - model->first_file_index;
  384. return true;
  385. });
  386. }
  387. string_clear(filename_str);
  388. storage_dir_close(directory);
  389. storage_file_free(directory);
  390. free(name);
  391. }
  392. void file_select_set_selected_file(FileSelect* file_select, const char* filename) {
  393. file_select_set_selected_file_internal(file_select, filename);
  394. if(!file_select_fill_strings(file_select)) {
  395. file_select->callback(false, file_select->context);
  396. }
  397. }