item.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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_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. // FURI_LOG_I(TAG, "About to walk the dir");
  35. if(dir_walk_open(dir_walk, cpath)) {
  36. while(dir_walk_read(dir_walk, path, NULL) == DirWalkOK) {
  37. // FURI_LOG_I(TAG, "> dir_walk: %s", furi_string_get_cstr(path));
  38. const char* cpath = furi_string_get_cstr(path);
  39. // Insert the new file path in sorted order to flist
  40. uint32_t i = 0;
  41. FileArray_it_t it;
  42. for(FileArray_it(it, flist); !FileArray_end_p(it); FileArray_next(it), ++i) {
  43. if(strcmp(cpath, furi_string_get_cstr(*FileArray_ref(it))) > 0) {
  44. continue;
  45. }
  46. // FURI_LOG_I(TAG, ">> Inserting at %lu", i);
  47. FileArray_push_at(flist, i, path);
  48. break;
  49. }
  50. if(i == FileArray_size(flist)) {
  51. // FURI_LOG_I(TAG, "Couldn't insert, so adding at the end!");
  52. FileArray_push_back(flist, path);
  53. }
  54. }
  55. }
  56. furi_string_free(path);
  57. // DEBUG: Now print our array in original order
  58. FileArray_it_t iter;
  59. for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
  60. const char* f = furi_string_get_cstr(*FileArray_cref(iter));
  61. FURI_LOG_I(TAG, "Found: %s", f);
  62. }
  63. FURI_LOG_I(TAG, "Creating our ItemsArray");
  64. ItemArray_init(iview->items);
  65. for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
  66. path = *FileArray_ref(iter);
  67. const char* found_path = furi_string_get_cstr(path);
  68. Item* item = ItemArray_push_new(iview->items);
  69. // Action files have extensions, so item->ext starts with '.' - ehhhh
  70. item->ext[0] = 0;
  71. path_extract_extension(path, item->ext, MAX_EXT_LEN);
  72. item->type = (item->ext[0] == '.') ? Item_Action : Item_Group;
  73. item->name = furi_string_alloc();
  74. path_extract_filename_no_ext(found_path, item->name);
  75. FURI_LOG_I(TAG, "Basename: %s", furi_string_get_cstr(item->name));
  76. item_prettify_name(item->name);
  77. item->path = furi_string_alloc();
  78. furi_string_set(item->path, path);
  79. FURI_LOG_I(TAG, "Path: %s", furi_string_get_cstr(item->path));
  80. }
  81. FileArray_clear(flist);
  82. dir_walk_free(dir_walk);
  83. return iview;
  84. }
  85. void item_items_view_free(ItemsView* items_view) {
  86. FURI_LOG_I(TAG, "item_items_view_free - begin");
  87. furi_string_free(items_view->name);
  88. furi_string_free(items_view->path);
  89. ItemArray_it_t iter;
  90. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter); ItemArray_next(iter)) {
  91. furi_string_free(ItemArray_ref(iter)->name);
  92. furi_string_free(ItemArray_ref(iter)->path);
  93. }
  94. ItemArray_clear(items_view->items);
  95. free(items_view);
  96. FURI_LOG_I(TAG, "item_items_view_free - end");
  97. }
  98. void item_prettify_name(FuriString* name) {
  99. // FURI_LOG_I(TAG, "Converting %s to...", furi_string_get_cstr(name));
  100. if(furi_string_size(name) > 3) {
  101. char c = furi_string_get_char(name, 2);
  102. if(c == '_') {
  103. char a = furi_string_get_char(name, 0);
  104. char b = furi_string_get_char(name, 1);
  105. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  106. furi_string_right(name, 3);
  107. }
  108. }
  109. }
  110. furi_string_replace_str(name, "_", " ", 0);
  111. // FURI_LOG_I(TAG, "... %s", furi_string_get_cstr(name));
  112. }