item.c 5.7 KB

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