Welcome to mirror list, hosted at ThFree Co, Russian Federation.

archive_favorites.c « helpers « archive « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc0cad575f60b16c3fb74432898dd75313b1e016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

#include "archive_favorites.h"
#include "archive_files.h"
#include "archive_apps.h"
#include "archive_browser.h"

#define ARCHIVE_FAV_FILE_BUF_LEN 32

static bool archive_favorites_read_line(File* file, string_t str_result) {
    string_reset(str_result);
    uint8_t buffer[ARCHIVE_FAV_FILE_BUF_LEN];
    bool result = false;

    do {
        uint16_t read_count = storage_file_read(file, buffer, ARCHIVE_FAV_FILE_BUF_LEN);
        if(storage_file_get_error(file) != FSE_OK) {
            return false;
        }

        for(uint16_t i = 0; i < read_count; i++) {
            if(buffer[i] == '\n') {
                uint32_t position = storage_file_tell(file);
                if(storage_file_get_error(file) != FSE_OK) {
                    return false;
                }

                position = position - read_count + i + 1;

                storage_file_seek(file, position, true);
                if(storage_file_get_error(file) != FSE_OK) {
                    return false;
                }

                result = true;
                break;
            } else {
                string_push_back(str_result, buffer[i]);
            }
        }

        if(result || read_count == 0) {
            break;
        }
    } while(true);

    return result;
}

uint16_t archive_favorites_count(void* context) {
    furi_assert(context);

    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);

    string_t buffer;
    string_init(buffer);

    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
    uint16_t lines = 0;

    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }
            ++lines;
        }
    }

    storage_file_close(file);

    string_clear(buffer);
    storage_file_free(file);
    furi_record_close(RECORD_STORAGE);

    return lines;
}

static bool archive_favourites_rescan() {
    string_t buffer;
    string_init(buffer);
    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);
    File* fav_item_file = storage_file_alloc(fs_api);

    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }

            if(string_search(buffer, "/app:") == 0) {
                if(archive_app_is_available(NULL, string_get_cstr(buffer))) {
                    archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
                }
            } else {
                bool file_exists = storage_file_open(
                    fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
                if(file_exists) {
                    storage_file_close(fav_item_file);
                    archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
                } else {
                    storage_file_close(fav_item_file);
                }
            }
        }
    }

    string_clear(buffer);

    storage_file_close(file);
    storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
    storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
    storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);

    storage_file_free(file);
    storage_file_free(fav_item_file);
    furi_record_close(RECORD_STORAGE);

    return result;
}

bool archive_favorites_read(void* context) {
    furi_assert(context);

    ArchiveBrowserView* browser = context;
    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);
    File* fav_item_file = storage_file_alloc(fs_api);

    string_t buffer;
    FileInfo file_info;
    string_init(buffer);

    bool need_refresh = false;
    uint16_t file_count = 0;

    archive_file_array_rm_all(browser);

    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }

            if(string_search(buffer, "/app:") == 0) {
                if(archive_app_is_available(browser, string_get_cstr(buffer))) {
                    archive_add_app_item(browser, string_get_cstr(buffer));
                    file_count++;
                } else {
                    need_refresh = true;
                }
            } else {
                bool file_exists = storage_file_open(
                    fav_item_file, string_get_cstr(buffer), FSAM_READ, FSOM_OPEN_EXISTING);
                if(file_exists) {
                    storage_common_stat(fs_api, string_get_cstr(buffer), &file_info);
                    storage_file_close(fav_item_file);
                    archive_add_file_item(
                        browser, (file_info.flags & FSF_DIRECTORY), string_get_cstr(buffer));
                    file_count++;
                } else {
                    storage_file_close(fav_item_file);
                    need_refresh = true;
                }
            }

            string_reset(buffer);
        }
    }
    storage_file_close(file);
    string_clear(buffer);
    storage_file_free(file);
    storage_file_free(fav_item_file);
    furi_record_close(RECORD_STORAGE);

    archive_set_item_count(browser, file_count);

    if(need_refresh) {
        archive_favourites_rescan();
    }

    return result;
}

bool archive_favorites_delete(const char* format, ...) {
    string_t buffer;
    string_t filename;
    va_list args;
    va_start(args, format);
    string_init_vprintf(filename, format, args);
    va_end(args);

    string_init(buffer);
    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);

    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }

            if(string_search(buffer, filename)) {
                archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
            }
        }
    }

    string_clear(buffer);
    string_clear(filename);

    storage_file_close(file);
    storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
    storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
    storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);

    storage_file_free(file);
    furi_record_close(RECORD_STORAGE);

    return result;
}

bool archive_is_favorite(const char* format, ...) {
    string_t buffer;
    string_t filename;
    va_list args;
    va_start(args, format);
    string_init_vprintf(filename, format, args);
    va_end(args);

    string_init(buffer);
    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);

    bool found = false;
    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }
            if(!string_search(buffer, filename)) {
                found = true;
                break;
            }
        }
    }

    storage_file_close(file);
    string_clear(buffer);
    string_clear(filename);
    storage_file_free(file);
    furi_record_close(RECORD_STORAGE);

    return found;
}

bool archive_favorites_rename(const char* src, const char* dst) {
    furi_assert(src);
    furi_assert(dst);

    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);

    string_t path;
    string_t buffer;

    string_init(buffer);
    string_init(path);

    string_printf(path, "%s", src);
    bool result = storage_file_open(file, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);

    if(result) {
        while(1) {
            if(!archive_favorites_read_line(file, buffer)) {
                break;
            }
            if(!string_size(buffer)) {
                break;
            }

            archive_file_append(
                ARCHIVE_FAV_TEMP_PATH,
                "%s\n",
                string_search(buffer, path) ? string_get_cstr(buffer) : dst);
        }
    }

    string_clear(buffer);
    string_clear(path);

    storage_file_close(file);
    storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
    storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
    storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);

    storage_file_free(file);
    furi_record_close(RECORD_STORAGE);

    return result;
}

void archive_add_to_favorites(const char* file_path) {
    furi_assert(file_path);

    archive_file_append(ARCHIVE_FAV_PATH, "%s\n", file_path);
}

void archive_favorites_save(void* context) {
    furi_assert(context);

    ArchiveBrowserView* browser = context;
    Storage* fs_api = furi_record_open(RECORD_STORAGE);
    File* file = storage_file_alloc(fs_api);

    for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
        ArchiveFile_t* item = archive_get_file_at(browser, i);
        archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->path));
    }

    storage_common_remove(fs_api, ARCHIVE_FAV_PATH);
    storage_common_rename(fs_api, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
    storage_common_remove(fs_api, ARCHIVE_FAV_TEMP_PATH);

    storage_file_free(file);
    furi_record_close(RECORD_STORAGE);
}