item.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, FuriString* input_path) {
  10. App* app = context;
  11. // Handle the app start condition
  12. if(input_path == NULL) {
  13. input_path = furi_string_alloc_set_str(QUAC_DATA_PATH);
  14. }
  15. const char* cpath = furi_string_get_cstr(input_path);
  16. FURI_LOG_I(TAG, "Getting items from: %s", cpath);
  17. ItemsView* iview = malloc(sizeof(ItemsView));
  18. iview->path = furi_string_alloc_set(input_path);
  19. iview->name = furi_string_alloc();
  20. if(app->depth == 0) {
  21. FURI_LOG_I(TAG, "Depth is ZERO!");
  22. furi_string_set_str(iview->name, QUAC_NAME);
  23. } else {
  24. FURI_LOG_I(TAG, "Depth is %d", app->depth);
  25. path_extract_basename(cpath, iview->name);
  26. item_prettify_name(iview->name);
  27. }
  28. DirWalk* dir_walk = dir_walk_alloc(app->storage);
  29. dir_walk_set_recursive(dir_walk, false);
  30. FuriString* path = furi_string_alloc();
  31. FileArray_t flist;
  32. FileArray_init(flist);
  33. FuriString* filename_tmp;
  34. filename_tmp = furi_string_alloc();
  35. // FURI_LOG_I(TAG, "About to walk the dir");
  36. if(dir_walk_open(dir_walk, cpath)) {
  37. while(dir_walk_read(dir_walk, path, NULL) == DirWalkOK) {
  38. FURI_LOG_I(TAG, "> dir_walk: %s", furi_string_get_cstr(path));
  39. const char* cpath = furi_string_get_cstr(path);
  40. // Skip "hidden" files
  41. path_extract_filename(path, filename_tmp, false);
  42. char first_char = furi_string_get_char(filename_tmp, 0);
  43. if(first_char == '.') {
  44. FURI_LOG_I(TAG, ">> skipping hidden file: %s", furi_string_get_cstr(filename_tmp));
  45. continue;
  46. }
  47. // Insert the new file path in sorted order to flist
  48. uint32_t i = 0;
  49. FileArray_it_t it;
  50. for(FileArray_it(it, flist); !FileArray_end_p(it); FileArray_next(it), ++i) {
  51. if(strcmp(cpath, furi_string_get_cstr(*FileArray_ref(it))) > 0) {
  52. continue;
  53. }
  54. // FURI_LOG_I(TAG, ">> Inserting at %lu", i);
  55. FileArray_push_at(flist, i, path);
  56. break;
  57. }
  58. if(i == FileArray_size(flist)) {
  59. // FURI_LOG_I(TAG, "Couldn't insert, so adding at the end!");
  60. FileArray_push_back(flist, path);
  61. }
  62. }
  63. }
  64. furi_string_free(filename_tmp);
  65. furi_string_free(path);
  66. // DEBUG: Now print our array in original order
  67. FileArray_it_t iter;
  68. for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
  69. const char* f = furi_string_get_cstr(*FileArray_cref(iter));
  70. FURI_LOG_I(TAG, "Found: %s", f);
  71. }
  72. FURI_LOG_I(TAG, "Creating our ItemsArray");
  73. ItemArray_init(iview->items);
  74. for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
  75. path = *FileArray_ref(iter);
  76. const char* found_path = furi_string_get_cstr(path);
  77. Item* item = ItemArray_push_new(iview->items);
  78. // Action files have extensions, so item->ext starts with '.' - ehhhh
  79. item->ext[0] = 0;
  80. path_extract_extension(path, item->ext, MAX_EXT_LEN);
  81. item->type = (item->ext[0] == '.') ? Item_Action : Item_Group;
  82. item->name = furi_string_alloc();
  83. path_extract_filename_no_ext(found_path, item->name);
  84. FURI_LOG_I(TAG, "Basename: %s", furi_string_get_cstr(item->name));
  85. item_prettify_name(item->name);
  86. item->path = furi_string_alloc();
  87. furi_string_set(item->path, path);
  88. FURI_LOG_I(TAG, "Path: %s", furi_string_get_cstr(item->path));
  89. }
  90. FileArray_clear(flist);
  91. dir_walk_free(dir_walk);
  92. return iview;
  93. }
  94. void item_items_view_free(ItemsView* items_view) {
  95. FURI_LOG_I(TAG, "item_items_view_free - begin");
  96. furi_string_free(items_view->name);
  97. furi_string_free(items_view->path);
  98. ItemArray_it_t iter;
  99. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter); ItemArray_next(iter)) {
  100. furi_string_free(ItemArray_ref(iter)->name);
  101. furi_string_free(ItemArray_ref(iter)->path);
  102. }
  103. ItemArray_clear(items_view->items);
  104. free(items_view);
  105. FURI_LOG_I(TAG, "item_items_view_free - end");
  106. }
  107. void item_prettify_name(FuriString* name) {
  108. // FURI_LOG_I(TAG, "Converting %s to...", furi_string_get_cstr(name));
  109. if(furi_string_size(name) > 3) {
  110. char c = furi_string_get_char(name, 2);
  111. if(c == '_') {
  112. char a = furi_string_get_char(name, 0);
  113. char b = furi_string_get_char(name, 1);
  114. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  115. furi_string_right(name, 3);
  116. }
  117. }
  118. }
  119. furi_string_replace_str(name, "_", " ", 0);
  120. // FURI_LOG_I(TAG, "... %s", furi_string_get_cstr(name));
  121. }