archive_favorites.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "archive_favorites.h"
  2. #include "archive_files.h"
  3. #include "archive_apps.h"
  4. #include "archive_browser.h"
  5. #define ARCHIVE_FAV_FILE_BUF_LEN 32
  6. static bool archive_favorites_read_line(File* file, FuriString* str_result) {
  7. furi_string_reset(str_result);
  8. uint8_t buffer[ARCHIVE_FAV_FILE_BUF_LEN];
  9. bool result = false;
  10. do {
  11. uint16_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
  12. if(storage_file_get_error(file) != FSE_OK) {
  13. return false;
  14. }
  15. for(uint16_t i = 0; i < read_count; i++) {
  16. if(buffer[i] == '\n') {
  17. uint32_t position = storage_file_tell(file);
  18. if(storage_file_get_error(file) != FSE_OK) {
  19. return false;
  20. }
  21. position = position - read_count + i + 1;
  22. storage_file_seek(file, position, true);
  23. if(storage_file_get_error(file) != FSE_OK) {
  24. return false;
  25. }
  26. result = true;
  27. break;
  28. } else {
  29. furi_string_push_back(str_result, buffer[i]);
  30. }
  31. }
  32. if(result || read_count == 0) {
  33. break;
  34. }
  35. } while(true);
  36. return result;
  37. }
  38. uint16_t archive_favorites_count(void* context) {
  39. furi_assert(context);
  40. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  41. File* file = storage_file_alloc(fs_api);
  42. FuriString* buffer;
  43. buffer = furi_string_alloc();
  44. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  45. uint16_t lines = 0;
  46. if(result) {
  47. while(1) {
  48. if(!archive_favorites_read_line(file, buffer)) {
  49. break;
  50. }
  51. if(!furi_string_size(buffer)) {
  52. continue; // Skip empty lines
  53. }
  54. ++lines;
  55. }
  56. }
  57. storage_file_close(file);
  58. furi_string_free(buffer);
  59. storage_file_free(file);
  60. furi_record_close(RECORD_STORAGE);
  61. return lines;
  62. }
  63. static bool archive_favourites_rescan() {
  64. FuriString* buffer;
  65. buffer = furi_string_alloc();
  66. Storage* storage = furi_record_open(RECORD_STORAGE);
  67. File* file = storage_file_alloc(storage);
  68. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  69. if(result) {
  70. while(1) {
  71. if(!archive_favorites_read_line(file, buffer)) {
  72. break;
  73. }
  74. if(!furi_string_size(buffer)) {
  75. continue;
  76. }
  77. if(furi_string_search(buffer, "/app:") == 0) {
  78. if(archive_app_is_available(NULL, furi_string_get_cstr(buffer))) {
  79. archive_file_append(
  80. ARCHIVE_FAV_TEMP_PATH, "%s\n", furi_string_get_cstr(buffer));
  81. }
  82. } else {
  83. if(storage_file_exists(storage, furi_string_get_cstr(buffer))) {
  84. archive_file_append(
  85. ARCHIVE_FAV_TEMP_PATH, "%s\n", furi_string_get_cstr(buffer));
  86. }
  87. }
  88. }
  89. }
  90. furi_string_free(buffer);
  91. storage_file_close(file);
  92. storage_common_remove(storage, ARCHIVE_FAV_PATH);
  93. storage_common_rename(storage, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
  94. storage_common_remove(storage, ARCHIVE_FAV_TEMP_PATH);
  95. storage_file_free(file);
  96. furi_record_close(RECORD_STORAGE);
  97. return result;
  98. }
  99. bool archive_favorites_read(void* context) {
  100. furi_assert(context);
  101. ArchiveBrowserView* browser = context;
  102. Storage* storage = furi_record_open(RECORD_STORAGE);
  103. File* file = storage_file_alloc(storage);
  104. FuriString* buffer;
  105. FileInfo file_info;
  106. buffer = furi_string_alloc();
  107. bool need_refresh = false;
  108. uint16_t file_count = 0;
  109. archive_file_array_rm_all(browser);
  110. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  111. if(result) {
  112. while(1) {
  113. if(!archive_favorites_read_line(file, buffer)) {
  114. break;
  115. }
  116. if(!furi_string_size(buffer)) {
  117. continue;
  118. }
  119. if(furi_string_search(buffer, "/app:") == 0) {
  120. if(archive_app_is_available(browser, furi_string_get_cstr(buffer))) {
  121. archive_add_app_item(browser, furi_string_get_cstr(buffer));
  122. file_count++;
  123. } else {
  124. need_refresh = true;
  125. }
  126. } else {
  127. if(storage_file_exists(storage, furi_string_get_cstr(buffer))) {
  128. storage_common_stat(storage, furi_string_get_cstr(buffer), &file_info);
  129. archive_add_file_item(
  130. browser, (file_info.flags & FSF_DIRECTORY), furi_string_get_cstr(buffer));
  131. file_count++;
  132. } else {
  133. need_refresh = true;
  134. }
  135. }
  136. furi_string_reset(buffer);
  137. }
  138. }
  139. storage_file_close(file);
  140. furi_string_free(buffer);
  141. storage_file_free(file);
  142. furi_record_close(RECORD_STORAGE);
  143. archive_set_item_count(browser, file_count);
  144. if(need_refresh) {
  145. archive_favourites_rescan();
  146. }
  147. return result;
  148. }
  149. bool archive_favorites_delete(const char* format, ...) {
  150. FuriString* buffer;
  151. FuriString* filename;
  152. va_list args;
  153. va_start(args, format);
  154. filename = furi_string_alloc_vprintf(format, args);
  155. va_end(args);
  156. buffer = furi_string_alloc();
  157. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  158. File* file = storage_file_alloc(fs_api);
  159. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  160. if(result) {
  161. while(1) {
  162. if(!archive_favorites_read_line(file, buffer)) {
  163. break;
  164. }
  165. if(!furi_string_size(buffer)) {
  166. continue;
  167. }
  168. if(furi_string_search(buffer, filename)) {
  169. archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", furi_string_get_cstr(buffer));
  170. }
  171. }
  172. }
  173. furi_string_free(buffer);
  174. furi_string_free(filename);
  175. storage_file_close(file);
  176. storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
  177. storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
  178. storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
  179. storage_file_free(file);
  180. furi_record_close(RECORD_STORAGE);
  181. return result;
  182. }
  183. bool archive_is_favorite(const char* format, ...) {
  184. FuriString* buffer;
  185. FuriString* filename;
  186. va_list args;
  187. va_start(args, format);
  188. filename = furi_string_alloc_vprintf(format, args);
  189. va_end(args);
  190. buffer = furi_string_alloc();
  191. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  192. File* file = storage_file_alloc(fs_api);
  193. bool found = false;
  194. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  195. if(result) {
  196. while(1) {
  197. if(!archive_favorites_read_line(file, buffer)) {
  198. break;
  199. }
  200. if(!furi_string_size(buffer)) {
  201. continue;
  202. }
  203. if(!furi_string_search(buffer, filename)) {
  204. found = true;
  205. break;
  206. }
  207. }
  208. }
  209. storage_file_close(file);
  210. furi_string_free(buffer);
  211. furi_string_free(filename);
  212. storage_file_free(file);
  213. furi_record_close(RECORD_STORAGE);
  214. return found;
  215. }
  216. bool archive_favorites_rename(const char* src, const char* dst) {
  217. furi_assert(src);
  218. furi_assert(dst);
  219. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  220. File* file = storage_file_alloc(fs_api);
  221. FuriString* path;
  222. FuriString* buffer;
  223. buffer = furi_string_alloc();
  224. path = furi_string_alloc();
  225. furi_string_printf(path, "%s", src);
  226. bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  227. if(result) {
  228. while(1) {
  229. if(!archive_favorites_read_line(file, buffer)) {
  230. break;
  231. }
  232. if(!furi_string_size(buffer)) {
  233. continue;
  234. }
  235. archive_file_append(
  236. ARCHIVE_FAV_TEMP_PATH,
  237. "%s\n",
  238. furi_string_search(buffer, path) ? furi_string_get_cstr(buffer) : dst);
  239. }
  240. }
  241. furi_string_free(buffer);
  242. furi_string_free(path);
  243. storage_file_close(file);
  244. storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
  245. storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
  246. storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
  247. storage_file_free(file);
  248. furi_record_close(RECORD_STORAGE);
  249. return result;
  250. }
  251. void archive_add_to_favorites(const char* file_path) {
  252. furi_assert(file_path);
  253. archive_file_append(ARCHIVE_FAV_PATH, "%s\n", file_path);
  254. }
  255. void archive_favorites_save(void* context) {
  256. furi_assert(context);
  257. ArchiveBrowserView* browser = context;
  258. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  259. File* file = storage_file_alloc(fs_api);
  260. for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
  261. ArchiveFile_t* item = archive_get_file_at(browser, i);
  262. archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", furi_string_get_cstr(item->path));
  263. }
  264. storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
  265. storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
  266. storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);
  267. storage_file_free(file);
  268. furi_record_close(RECORD_STORAGE);
  269. }