file_select.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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) {
  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. if(file_select->buffer) {
  127. with_view_model(
  128. file_select->view, (FileSelectModel * model) {
  129. strlcpy(
  130. file_select->buffer,
  131. string_get_cstr(model->filename[model->position]),
  132. file_select->buffer_size);
  133. return false;
  134. });
  135. };
  136. file_select->callback(true, file_select->context);
  137. }
  138. consumed = true;
  139. }
  140. }
  141. return consumed;
  142. }
  143. static bool file_select_init_inner(FileSelect* file_select) {
  144. bool result = false;
  145. if(file_select->path && file_select->extension && file_select->fs_api) {
  146. if(file_select_fill_count(file_select)) {
  147. if(file_select_fill_strings(file_select)) {
  148. file_select->init_completed = true;
  149. result = true;
  150. }
  151. }
  152. }
  153. return result;
  154. }
  155. FileSelect* file_select_alloc() {
  156. FileSelect* file_select = furi_alloc(sizeof(FileSelect));
  157. file_select->view = view_alloc();
  158. file_select->fs_api = furi_record_open("storage");
  159. view_set_context(file_select->view, file_select);
  160. view_allocate_model(file_select->view, ViewModelTypeLockFree, sizeof(FileSelectModel));
  161. view_set_draw_callback(file_select->view, file_select_draw_callback);
  162. view_set_input_callback(file_select->view, file_select_input_callback);
  163. with_view_model(
  164. file_select->view, (FileSelectModel * model) {
  165. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  166. string_init(model->filename[i]);
  167. }
  168. model->first_file_index = 0;
  169. model->file_count = 0;
  170. return false;
  171. });
  172. return file_select;
  173. }
  174. void file_select_free(FileSelect* file_select) {
  175. furi_assert(file_select);
  176. with_view_model(
  177. file_select->view, (FileSelectModel * model) {
  178. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  179. string_clear(model->filename[i]);
  180. }
  181. return false;
  182. });
  183. view_free(file_select->view);
  184. free(file_select);
  185. furi_record_close("storage");
  186. }
  187. View* file_select_get_view(FileSelect* file_select) {
  188. furi_assert(file_select);
  189. return file_select->view;
  190. }
  191. void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context) {
  192. file_select->context = context;
  193. file_select->callback = callback;
  194. }
  195. void file_select_set_filter(FileSelect* file_select, const char* path, const char* extension) {
  196. furi_assert(file_select);
  197. file_select->path = path;
  198. file_select->extension = extension;
  199. }
  200. void file_select_set_result_buffer(FileSelect* file_select, char* buffer, uint8_t buffer_size) {
  201. file_select->buffer = buffer;
  202. file_select->buffer_size = buffer_size;
  203. if(file_select->buffer) {
  204. strlcpy(file_select->buffer, "", file_select->buffer_size);
  205. }
  206. }
  207. bool file_select_init(FileSelect* file_select) {
  208. if(!file_select_init_inner(file_select)) {
  209. file_select->callback(false, file_select->context);
  210. return false;
  211. } else {
  212. return true;
  213. }
  214. }
  215. static bool filter_file(FileSelect* file_select, FileInfo* file_info, char* name) {
  216. bool result = false;
  217. if(!(file_info->flags & FSF_DIRECTORY)) {
  218. if(strcmp(file_select->extension, "*") == 0) {
  219. result = true;
  220. } else if(strstr(name, file_select->extension) != NULL) {
  221. result = true;
  222. }
  223. }
  224. return result;
  225. }
  226. bool file_select_fill_strings(FileSelect* file_select) {
  227. furi_assert(file_select);
  228. furi_assert(file_select->fs_api);
  229. furi_assert(file_select->path);
  230. furi_assert(file_select->extension);
  231. FileInfo file_info;
  232. File* directory = storage_file_alloc(file_select->fs_api);
  233. uint8_t string_counter = 0;
  234. uint16_t file_counter = 0;
  235. const uint8_t name_length = 100;
  236. char* name = furi_alloc(name_length);
  237. uint16_t first_file_index = 0;
  238. with_view_model(
  239. file_select->view, (FileSelectModel * model) {
  240. first_file_index = model->first_file_index;
  241. return false;
  242. });
  243. if(!storage_dir_open(directory, file_select->path)) {
  244. storage_dir_close(directory);
  245. storage_file_free(directory);
  246. free(name);
  247. return true;
  248. }
  249. while(1) {
  250. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  251. break;
  252. }
  253. if(storage_file_get_error(directory) == FSE_OK) {
  254. if(filter_file(file_select, &file_info, name)) {
  255. if(file_counter >= first_file_index) {
  256. with_view_model(
  257. file_select->view, (FileSelectModel * model) {
  258. string_set_str(model->filename[string_counter], name);
  259. if(strcmp(file_select->extension, "*") != 0) {
  260. string_replace_all_str(
  261. model->filename[string_counter], file_select->extension, "");
  262. }
  263. return true;
  264. });
  265. string_counter++;
  266. if(string_counter >= FILENAME_COUNT) {
  267. break;
  268. }
  269. }
  270. file_counter++;
  271. }
  272. } else {
  273. storage_dir_close(directory);
  274. storage_file_free(directory);
  275. free(name);
  276. return false;
  277. }
  278. }
  279. storage_dir_close(directory);
  280. storage_file_free(directory);
  281. free(name);
  282. return true;
  283. }
  284. bool file_select_fill_count(FileSelect* file_select) {
  285. furi_assert(file_select);
  286. furi_assert(file_select->fs_api);
  287. furi_assert(file_select->path);
  288. furi_assert(file_select->extension);
  289. FileInfo file_info;
  290. File* directory = storage_file_alloc(file_select->fs_api);
  291. uint16_t file_counter = 0;
  292. const uint8_t name_length = 100;
  293. char* name = furi_alloc(name_length);
  294. if(!storage_dir_open(directory, file_select->path)) {
  295. storage_dir_close(directory);
  296. storage_file_free(directory);
  297. free(name);
  298. return true;
  299. }
  300. while(1) {
  301. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  302. break;
  303. }
  304. if(storage_file_get_error(directory) == FSE_OK) {
  305. if(filter_file(file_select, &file_info, name)) {
  306. file_counter++;
  307. }
  308. } else {
  309. storage_dir_close(directory);
  310. storage_file_free(directory);
  311. free(name);
  312. return false;
  313. }
  314. }
  315. with_view_model(
  316. file_select->view, (FileSelectModel * model) {
  317. model->file_count = file_counter;
  318. return false;
  319. });
  320. storage_dir_close(directory);
  321. storage_file_free(directory);
  322. free(name);
  323. return true;
  324. }
  325. void file_select_set_selected_file_internal(FileSelect* file_select, const char* filename) {
  326. furi_assert(file_select);
  327. furi_assert(filename);
  328. furi_assert(file_select->fs_api);
  329. furi_assert(file_select->path);
  330. furi_assert(file_select->extension);
  331. if(strlen(filename) == 0) return;
  332. FileInfo file_info;
  333. File* directory = storage_file_alloc(file_select->fs_api);
  334. const uint8_t name_length = 100;
  335. char* name = furi_alloc(name_length);
  336. uint16_t file_position = 0;
  337. bool file_found = false;
  338. string_t filename_str;
  339. string_init_set_str(filename_str, filename);
  340. if(strcmp(file_select->extension, "*") != 0) {
  341. string_cat_str(filename_str, file_select->extension);
  342. }
  343. if(!storage_dir_open(directory, file_select->path)) {
  344. string_clear(filename_str);
  345. storage_dir_close(directory);
  346. storage_file_free(directory);
  347. free(name);
  348. return;
  349. }
  350. while(1) {
  351. if(!storage_dir_read(directory, &file_info, name, name_length)) {
  352. break;
  353. }
  354. if(storage_file_get_error(directory) == FSE_OK) {
  355. if(filter_file(file_select, &file_info, name)) {
  356. if(strcmp(string_get_cstr(filename_str), name) == 0) {
  357. file_found = true;
  358. break;
  359. }
  360. file_position++;
  361. }
  362. } else {
  363. string_clear(filename_str);
  364. storage_dir_close(directory);
  365. storage_file_free(directory);
  366. free(name);
  367. return;
  368. }
  369. }
  370. if(file_found) {
  371. with_view_model(
  372. file_select->view, (FileSelectModel * model) {
  373. uint16_t max_first_file_index =
  374. model->file_count > FILENAME_COUNT ? model->file_count - FILENAME_COUNT : 0;
  375. model->first_file_index = file_position;
  376. if(model->first_file_index > 0) {
  377. model->first_file_index -= 1;
  378. }
  379. if(model->first_file_index >= max_first_file_index) {
  380. model->first_file_index = max_first_file_index;
  381. }
  382. model->position = file_position - model->first_file_index;
  383. return true;
  384. });
  385. }
  386. string_clear(filename_str);
  387. storage_dir_close(directory);
  388. storage_file_free(directory);
  389. free(name);
  390. }
  391. void file_select_set_selected_file(FileSelect* file_select, const char* filename) {
  392. file_select_set_selected_file_internal(file_select, filename);
  393. if(!file_select_fill_strings(file_select)) {
  394. file_select->callback(false, file_select->context);
  395. }
  396. }