item.c 4.6 KB

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