file_select.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "file_select.h"
  2. #include <gui/elements.h>
  3. #include <m-string.h>
  4. #include <sys/param.h>
  5. #define FILENAME_COUNT 4
  6. struct FileSelect {
  7. // public
  8. View* view;
  9. FS_Api* 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. view_set_context(file_select->view, file_select);
  159. view_allocate_model(file_select->view, ViewModelTypeLockFree, sizeof(FileSelectModel));
  160. view_set_draw_callback(file_select->view, file_select_draw_callback);
  161. view_set_input_callback(file_select->view, file_select_input_callback);
  162. with_view_model(
  163. file_select->view, (FileSelectModel * model) {
  164. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  165. string_init(model->filename[i]);
  166. }
  167. model->first_file_index = 0;
  168. model->file_count = 0;
  169. return false;
  170. });
  171. return file_select;
  172. }
  173. void file_select_free(FileSelect* file_select) {
  174. furi_assert(file_select);
  175. with_view_model(
  176. file_select->view, (FileSelectModel * model) {
  177. for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
  178. string_clear(model->filename[i]);
  179. }
  180. return false;
  181. });
  182. view_free(file_select->view);
  183. free(file_select);
  184. }
  185. View* file_select_get_view(FileSelect* file_select) {
  186. furi_assert(file_select);
  187. return file_select->view;
  188. }
  189. void file_select_set_api(FileSelect* file_select, FS_Api* fs_api) {
  190. furi_assert(file_select);
  191. file_select->fs_api = fs_api;
  192. }
  193. void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context) {
  194. file_select->context = context;
  195. file_select->callback = callback;
  196. }
  197. void file_select_set_filter(FileSelect* file_select, const char* path, const char* extension) {
  198. furi_assert(file_select);
  199. file_select->path = path;
  200. file_select->extension = extension;
  201. }
  202. void file_select_set_result_buffer(FileSelect* file_select, char* buffer, uint8_t buffer_size) {
  203. file_select->buffer = buffer;
  204. file_select->buffer_size = buffer_size;
  205. if(file_select->buffer) {
  206. strlcpy(file_select->buffer, "", file_select->buffer_size);
  207. }
  208. }
  209. bool file_select_init(FileSelect* file_select) {
  210. if(!file_select_init_inner(file_select)) {
  211. file_select->callback(false, file_select->context);
  212. return false;
  213. } else {
  214. return true;
  215. }
  216. }
  217. static bool filter_file(FileSelect* file_select, FileInfo* file_info, char* name) {
  218. bool result = false;
  219. if(!(file_info->flags & FSF_DIRECTORY)) {
  220. if(strcmp(file_select->extension, "*") == 0) {
  221. result = true;
  222. } else if(strstr(name, file_select->extension) != NULL) {
  223. result = true;
  224. }
  225. }
  226. return result;
  227. }
  228. bool file_select_fill_strings(FileSelect* file_select) {
  229. furi_assert(file_select);
  230. furi_assert(file_select->fs_api);
  231. furi_assert(file_select->path);
  232. furi_assert(file_select->extension);
  233. FileInfo file_info;
  234. File directory;
  235. bool result;
  236. FS_Dir_Api* dir_api = &file_select->fs_api->dir;
  237. uint8_t string_counter = 0;
  238. uint16_t file_counter = 0;
  239. const uint8_t name_length = 100;
  240. char* name = calloc(name_length, sizeof(char));
  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(name == NULL) {
  248. return false;
  249. }
  250. result = dir_api->open(&directory, file_select->path);
  251. if(!result) {
  252. dir_api->close(&directory);
  253. free(name);
  254. return false;
  255. }
  256. while(1) {
  257. result = dir_api->read(&directory, &file_info, name, name_length);
  258. if(directory.error_id == FSE_NOT_EXIST || name[0] == 0) {
  259. break;
  260. }
  261. if(result) {
  262. if(directory.error_id == FSE_OK) {
  263. if(filter_file(file_select, &file_info, name)) {
  264. if(file_counter >= first_file_index) {
  265. with_view_model(
  266. file_select->view, (FileSelectModel * model) {
  267. string_set_str(model->filename[string_counter], name);
  268. if(strcmp(file_select->extension, "*") != 0) {
  269. string_replace_all_str(
  270. model->filename[string_counter],
  271. file_select->extension,
  272. "");
  273. }
  274. return true;
  275. });
  276. string_counter++;
  277. if(string_counter >= FILENAME_COUNT) {
  278. break;
  279. }
  280. }
  281. file_counter++;
  282. }
  283. } else {
  284. dir_api->close(&directory);
  285. free(name);
  286. return false;
  287. }
  288. }
  289. }
  290. dir_api->close(&directory);
  291. free(name);
  292. return true;
  293. }
  294. bool file_select_fill_count(FileSelect* file_select) {
  295. furi_assert(file_select);
  296. furi_assert(file_select->fs_api);
  297. furi_assert(file_select->path);
  298. furi_assert(file_select->extension);
  299. FileInfo file_info;
  300. File directory;
  301. bool result;
  302. FS_Dir_Api* dir_api = &file_select->fs_api->dir;
  303. uint16_t file_counter = 0;
  304. const uint8_t name_length = 100;
  305. char* name = calloc(name_length, sizeof(char));
  306. if(name == NULL) {
  307. return false;
  308. }
  309. result = dir_api->open(&directory, file_select->path);
  310. if(!result) {
  311. dir_api->close(&directory);
  312. free(name);
  313. return false;
  314. }
  315. while(1) {
  316. result = dir_api->read(&directory, &file_info, name, name_length);
  317. if(directory.error_id == FSE_NOT_EXIST || name[0] == 0) {
  318. break;
  319. }
  320. if(result) {
  321. if(directory.error_id == FSE_OK) {
  322. if(filter_file(file_select, &file_info, name)) {
  323. file_counter++;
  324. }
  325. } else {
  326. dir_api->close(&directory);
  327. free(name);
  328. return false;
  329. }
  330. }
  331. }
  332. with_view_model(
  333. file_select->view, (FileSelectModel * model) {
  334. model->file_count = file_counter;
  335. return false;
  336. });
  337. dir_api->close(&directory);
  338. free(name);
  339. return true;
  340. }
  341. void file_select_set_selected_file_internal(FileSelect* file_select, const char* filename) {
  342. furi_assert(file_select);
  343. furi_assert(filename);
  344. furi_assert(file_select->fs_api);
  345. furi_assert(file_select->path);
  346. furi_assert(file_select->extension);
  347. if(strlen(filename) == 0) return;
  348. FileInfo file_info;
  349. File directory;
  350. bool result;
  351. FS_Dir_Api* dir_api = &file_select->fs_api->dir;
  352. const uint8_t name_length = 100;
  353. char* name = calloc(name_length, sizeof(char));
  354. if(name == NULL) {
  355. return;
  356. }
  357. uint16_t file_position = 0;
  358. bool file_found = false;
  359. string_t filename_str;
  360. string_init_set_str(filename_str, filename);
  361. if(strcmp(file_select->extension, "*") != 0) {
  362. string_cat_str(filename_str, file_select->extension);
  363. }
  364. result = dir_api->open(&directory, file_select->path);
  365. if(!result) {
  366. string_clear(filename_str);
  367. dir_api->close(&directory);
  368. free(name);
  369. return;
  370. }
  371. while(1) {
  372. result = dir_api->read(&directory, &file_info, name, name_length);
  373. if(directory.error_id == FSE_NOT_EXIST || name[0] == 0) {
  374. break;
  375. }
  376. if(result) {
  377. if(directory.error_id == FSE_OK) {
  378. if(filter_file(file_select, &file_info, name)) {
  379. if(strcmp(string_get_cstr(filename_str), name) == 0) {
  380. file_found = true;
  381. break;
  382. }
  383. file_position++;
  384. }
  385. } else {
  386. string_clear(filename_str);
  387. dir_api->close(&directory);
  388. free(name);
  389. return;
  390. }
  391. }
  392. }
  393. if(file_found) {
  394. with_view_model(
  395. file_select->view, (FileSelectModel * model) {
  396. uint16_t max_first_file_index =
  397. model->file_count > FILENAME_COUNT ? model->file_count - FILENAME_COUNT : 0;
  398. model->first_file_index = file_position;
  399. if(model->first_file_index > 0) {
  400. model->first_file_index -= 1;
  401. }
  402. if(model->first_file_index >= max_first_file_index) {
  403. model->first_file_index = max_first_file_index;
  404. }
  405. model->position = file_position - model->first_file_index;
  406. return true;
  407. });
  408. }
  409. string_clear(filename_str);
  410. dir_api->close(&directory);
  411. free(name);
  412. }
  413. void file_select_set_selected_file(FileSelect* file_select, const char* filename) {
  414. file_select_set_selected_file_internal(file_select, filename);
  415. if(!file_select_fill_strings(file_select)) {
  416. file_select->callback(false, file_select->context);
  417. }
  418. }