file_select.c 15 KB

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