item.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <toolbox/dir_walk.h>
  4. #include <toolbox/path.h>
  5. #include "quac.h"
  6. #include "item.h"
  7. #include <m-array.h>
  8. ARRAY_DEF(FileArray, FuriString*, FURI_STRING_OPLIST);
  9. ItemsView* item_get_items_view_from_path(void* context, const FuriString* input_path) {
  10. App* app = context;
  11. // Handle the app start condition
  12. FuriString* in_path;
  13. if(input_path == NULL) {
  14. in_path = furi_string_alloc_set_str(QUAC_DATA_PATH);
  15. } else {
  16. in_path = furi_string_alloc_set(input_path);
  17. }
  18. const char* cpath = furi_string_get_cstr(in_path);
  19. FURI_LOG_I(TAG, "Reading items from path: %s", cpath);
  20. ItemsView* iview = malloc(sizeof(ItemsView));
  21. iview->path = furi_string_alloc_set(in_path);
  22. iview->name = furi_string_alloc();
  23. if(app->depth == 0) {
  24. furi_string_set_str(iview->name, QUAC_NAME);
  25. } else {
  26. path_extract_basename(cpath, iview->name);
  27. item_prettify_name(iview->name);
  28. }
  29. DirWalk* dir_walk = dir_walk_alloc(app->storage);
  30. dir_walk_set_recursive(dir_walk, false);
  31. FuriString* path = furi_string_alloc();
  32. FileArray_t flist;
  33. FileArray_init(flist);
  34. FuriString* filename_tmp;
  35. filename_tmp = furi_string_alloc();
  36. // Walk the directory and store all file names in sorted order
  37. if(dir_walk_open(dir_walk, cpath)) {
  38. while(dir_walk_read(dir_walk, path, NULL) == DirWalkOK) {
  39. // FURI_LOG_I(TAG, "> dir_walk: %s", furi_string_get_cstr(path));
  40. const char* cpath = furi_string_get_cstr(path);
  41. // Skip "hidden" files
  42. path_extract_filename(path, filename_tmp, false);
  43. char first_char = furi_string_get_char(filename_tmp, 0);
  44. if(first_char == '.') {
  45. // FURI_LOG_I(TAG, ">> skipping hidden file: %s", furi_string_get_cstr(filename_tmp));
  46. continue;
  47. }
  48. // Insert the new file path in sorted order to flist
  49. uint32_t i = 0;
  50. FileArray_it_t it;
  51. for(FileArray_it(it, flist); !FileArray_end_p(it); FileArray_next(it), ++i) {
  52. if(strcmp(cpath, furi_string_get_cstr(*FileArray_ref(it))) > 0) {
  53. continue;
  54. }
  55. // FURI_LOG_I(TAG, ">> Inserting at %lu", i);
  56. FileArray_push_at(flist, i, path);
  57. break;
  58. }
  59. if(i == FileArray_size(flist)) {
  60. // FURI_LOG_I(TAG, "Couldn't insert, so adding at the end!");
  61. FileArray_push_back(flist, path);
  62. }
  63. }
  64. }
  65. furi_string_free(filename_tmp);
  66. furi_string_free(path);
  67. // Generate our Item list
  68. FileArray_it_t iter;
  69. ItemArray_init(iview->items);
  70. for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
  71. path = *FileArray_ref(iter);
  72. const char* found_path = furi_string_get_cstr(path);
  73. Item* item = ItemArray_push_new(iview->items);
  74. // Action files have extensions, so item->ext starts with '.'
  75. item->ext[0] = 0;
  76. path_extract_extension(path, item->ext, MAX_EXT_LEN);
  77. item->type = item_get_item_type_from_extension(item->ext);
  78. item->name = furi_string_alloc();
  79. path_extract_filename_no_ext(found_path, item->name);
  80. // FURI_LOG_I(TAG, "Basename: %s", furi_string_get_cstr(item->name));
  81. item_prettify_name(item->name);
  82. item->path = furi_string_alloc();
  83. furi_string_set(item->path, path);
  84. // FURI_LOG_I(TAG, "Path: %s", furi_string_get_cstr(item->path));
  85. }
  86. furi_string_free(in_path);
  87. FileArray_clear(flist);
  88. dir_walk_free(dir_walk);
  89. return iview;
  90. }
  91. void item_items_view_free(ItemsView* items_view) {
  92. furi_string_free(items_view->name);
  93. furi_string_free(items_view->path);
  94. ItemArray_it_t iter;
  95. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter); ItemArray_next(iter)) {
  96. furi_string_free(ItemArray_ref(iter)->name);
  97. furi_string_free(ItemArray_ref(iter)->path);
  98. }
  99. ItemArray_clear(items_view->items);
  100. free(items_view);
  101. }
  102. void item_prettify_name(FuriString* name) {
  103. // FURI_LOG_I(TAG, "Converting %s to...", furi_string_get_cstr(name));
  104. if(furi_string_size(name) > 3) {
  105. char c = furi_string_get_char(name, 2);
  106. if(c == '_') {
  107. char a = furi_string_get_char(name, 0);
  108. char b = furi_string_get_char(name, 1);
  109. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  110. furi_string_right(name, 3);
  111. }
  112. }
  113. }
  114. furi_string_replace_str(name, "_", " ", 0);
  115. // FURI_LOG_I(TAG, "... %s", furi_string_get_cstr(name));
  116. }
  117. ItemType item_get_item_type_from_extension(const char* ext) {
  118. ItemType type = Item_Group;
  119. if(!strcmp(ext, ".sub")) {
  120. type = Item_SubGhz;
  121. } else if(!strcmp(ext, ".rfid")) {
  122. type = Item_RFID;
  123. } else if(!strcmp(ext, ".ir")) {
  124. type = Item_IR;
  125. } else if(!strcmp(ext, ".qpl")) {
  126. type = Item_Playlist;
  127. }
  128. return type;
  129. }