Blender V5.3
fsmenu.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_fileops.hh"
16#include "BLI_listbase.hh"
17#include "BLI_path_utils.hh"
18#include "BLI_string.hh"
19#include "BLI_string_utils.hh"
20#include "BLI_utildefines.hh"
21
22#include "DNA_userdef_types.h"
23
24#include "BLT_translation.hh"
25
26#include "BKE_appdir.hh"
27
28#include "ED_fileselect.hh"
29
30#include "UI_resources.hh"
31
32#include "fsmenu.hh" /* include ourselves */
33
34namespace blender {
35
36/* FSMENU HANDLING */
37
45
46static FSMenu *g_fsmenu = nullptr;
47
49{
50 if (!g_fsmenu) {
52 }
53 return g_fsmenu;
54}
55
57{
58 FSMenuEntry *fsm_head = nullptr;
59
60 switch (category) {
62 fsm_head = fsmenu->fsmenu_system;
63 break;
65 fsm_head = fsmenu->fsmenu_system_bookmarks;
66 break;
68 fsm_head = fsmenu->fsmenu_bookmarks;
69 break;
71 fsm_head = fsmenu->fsmenu_recent;
72 break;
74 fsm_head = fsmenu->fsmenu_other;
75 break;
76 }
77 return fsm_head;
78}
79
80void ED_fsmenu_set_category(FSMenu *fsmenu, FSMenuCategory category, FSMenuEntry *fsm_head)
81{
82 switch (category) {
84 fsmenu->fsmenu_system = fsm_head;
85 break;
87 fsmenu->fsmenu_system_bookmarks = fsm_head;
88 break;
90 fsmenu->fsmenu_bookmarks = fsm_head;
91 break;
93 fsmenu->fsmenu_recent = fsm_head;
94 break;
96 fsmenu->fsmenu_other = fsm_head;
97 break;
98 }
99}
100
102{
103 FSMenuEntry *fsm_iter;
104 int count = 0;
105
106 for (fsm_iter = ED_fsmenu_get_category(fsmenu, category); fsm_iter; fsm_iter = fsm_iter->next) {
107 count++;
108 }
109
110 return count;
111}
112
114{
115 FSMenuEntry *fsm_iter;
116
117 for (fsm_iter = ED_fsmenu_get_category(fsmenu, category); fsm_iter && idx;
118 fsm_iter = fsm_iter->next)
119 {
120 idx--;
121 }
122
123 return fsm_iter;
124}
125
127{
128 return fsentry->path;
129}
130
131void ED_fsmenu_entry_set_path(FSMenuEntry *fsentry, const char *path)
132{
133 if ((!fsentry->path || !path || !STREQ(path, fsentry->path)) && (fsentry->path != path)) {
134 char tmp_name[FILE_MAXFILE];
135
136 MEM_SAFE_DELETE(fsentry->path);
137
138 fsentry->path = (path && path[0]) ? BLI_strdup(path) : nullptr;
139
140 const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
141 BLENDER_USER_CONFIG, nullptr);
142
143 if (user_config_dir.has_value()) {
144 BLI_path_join(tmp_name, sizeof(tmp_name), user_config_dir->c_str(), BLENDER_BOOKMARK_FILE);
145 fsmenu_write_file(ED_fsmenu_get(), tmp_name);
146 }
147 }
148}
149
151{
152 return (fsentry->icon) ? fsentry->icon : ICON_FILE_FOLDER;
153}
154
155void ED_fsmenu_entry_set_icon(FSMenuEntry *fsentry, const int icon)
156{
157 fsentry->icon = icon;
158}
159
160static void fsmenu_entry_generate_name(FSMenuEntry *fsentry, char *name, size_t name_size)
161{
162 int offset = 0;
163 int len = name_size;
164
165 if (BLI_path_name_at_index(fsentry->path, -1, &offset, &len)) {
166 /* use as size */
167 len += 1;
168 }
169
170 BLI_strncpy(name, &fsentry->path[offset], std::min(size_t(len), name_size));
171 if (!name[0]) {
172 name[0] = '/';
173 name[1] = '\0';
174 }
175}
176
178{
179 if (fsentry->name[0]) {
180 return fsentry->name;
181 }
182
183 /* Here we abuse fsm_iter->name, keeping first char nullptr. */
184 char *name = fsentry->name + 1;
185 size_t name_size = sizeof(fsentry->name) - 1;
186
187 fsmenu_entry_generate_name(fsentry, name, name_size);
188 return name;
189}
190
191void ED_fsmenu_entry_set_name(FSMenuEntry *fsentry, const char *name)
192{
193 if (!STREQ(name, fsentry->name)) {
194 char tmp_name[FILE_MAXFILE];
195 size_t tmp_name_size = sizeof(tmp_name);
196
197 fsmenu_entry_generate_name(fsentry, tmp_name, tmp_name_size);
198 if (!name[0] || STREQ(tmp_name, name)) {
199 /* reset name to default behavior. */
200 fsentry->name[0] = '\0';
201 }
202 else {
203 STRNCPY(fsentry->name, name);
204 }
205
206 const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
207 BLENDER_USER_CONFIG, nullptr);
208
209 if (user_config_dir.has_value()) {
210 BLI_path_join(tmp_name, sizeof(tmp_name), user_config_dir->c_str(), BLENDER_BOOKMARK_FILE);
211 fsmenu_write_file(ED_fsmenu_get(), tmp_name);
212 }
213 }
214}
215
216short fsmenu_can_save(FSMenu *fsmenu, FSMenuCategory category, int idx)
217{
218 FSMenuEntry *fsm_iter;
219
220 for (fsm_iter = ED_fsmenu_get_category(fsmenu, category); fsm_iter && idx;
221 fsm_iter = fsm_iter->next)
222 {
223 idx--;
224 }
225
226 return fsm_iter ? fsm_iter->save : 0;
227}
228
230 FSMenuCategory category,
231 const char *path,
232 const char *name,
233 int icon,
234 FSMenuInsert flag)
235{
236 /* NOTE: this function must *not* perform file-system checks on `path`,
237 * although the path may be inspected as a literal string.
238 *
239 * This is important because accessing the file system may reference drives
240 * which are offline, network drives requiring an internet connection,
241 * external drives that aren't plugged in, etc.
242 * Delays in any file-system checks can causes hanging on startup.
243 * See !138218 for details. */
244
245 const uint path_len = strlen(path);
246 BLI_assert(path_len > 0);
247 if (path_len == 0) {
248 return;
249 }
251 const bool has_trailing_slash = (path[path_len - 1] == SEP);
252 FSMenuEntry *fsm_prev;
253 FSMenuEntry *fsm_iter;
254 FSMenuEntry *fsm_head;
255
256 fsm_head = ED_fsmenu_get_category(fsmenu, category);
257 fsm_prev = fsm_head; /* this is odd and not really correct? */
258
259 for (fsm_iter = fsm_head; fsm_iter; fsm_prev = fsm_iter, fsm_iter = fsm_iter->next) {
260 if (fsm_iter->path) {
261 if (fsm_iter->icon == FSMENU_CURRENT_FILE_ICON) {
262 continue;
263 }
264 /* Compare, with/without the trailing slash in 'path'. */
265 const int cmp_ret = BLI_path_ncmp(path, fsm_iter->path, path_len);
266 if (cmp_ret == 0 && STREQ(fsm_iter->path + path_len, has_trailing_slash ? "" : SEP_STR)) {
267 if (flag & FS_INSERT_FIRST) {
268 if (fsm_iter != fsm_head) {
269 fsm_prev->next = fsm_iter->next;
270 fsm_iter->next = fsm_head;
271 ED_fsmenu_set_category(fsmenu, category, fsm_iter);
272 }
273 }
274 return;
275 }
276 if ((flag & FS_INSERT_SORTED) && cmp_ret < 0) {
277 break;
278 }
279 }
280 else {
281 /* if we're bookmarking this, file should come
282 * before the last separator, only automatically added
283 * current dir go after the last separator. */
284 if (flag & FS_INSERT_SAVE) {
285 break;
286 }
287 }
288 }
289
290 fsm_iter = MEM_new_uninitialized<FSMenuEntry>("fsme");
291 fsm_iter->path = has_trailing_slash ? BLI_strdup(path) : BLI_string_joinN(path, SEP_STR);
292 fsm_iter->save = (flag & FS_INSERT_SAVE) != 0;
293
294 /* If entry is also in another list, use that icon and maybe name. */
295 /* On macOS we get icons and names for System Bookmarks from the FS_CATEGORY_OTHER list. */
297
298 const FSMenuCategory cats[] = {
303 };
304 int i = ARRAY_SIZE(cats);
305 if (category == FS_CATEGORY_BOOKMARKS) {
306 i--;
307 }
308
309 while (i--) {
310 FSMenuEntry *tfsm = ED_fsmenu_get_category(fsmenu, cats[i]);
311 for (; tfsm; tfsm = tfsm->next) {
312 if (tfsm->icon == FSMENU_CURRENT_FILE_ICON) {
313 continue;
314 }
315 if (STREQ(tfsm->path, fsm_iter->path)) {
316 icon = tfsm->icon;
317 if (tfsm->name[0] && (!name || !name[0])) {
318 name = DATA_(tfsm->name);
319 }
320 break;
321 }
322 }
323 if (tfsm) {
324 break;
325 }
326 }
327 }
328
329 if (name && name[0]) {
330 STRNCPY(fsm_iter->name, name);
331 }
332 else {
333 fsm_iter->name[0] = '\0';
334 }
335
336 ED_fsmenu_entry_set_icon(fsm_iter, icon);
337
338 if (fsm_prev) {
339 if (flag & FS_INSERT_FIRST) {
340 fsm_iter->next = fsm_head;
341 ED_fsmenu_set_category(fsmenu, category, fsm_iter);
342 }
343 else {
344 fsm_iter->next = fsm_prev->next;
345 fsm_prev->next = fsm_iter;
346 }
347 }
348 else {
349 fsm_iter->next = fsm_head;
350 ED_fsmenu_set_category(fsmenu, category, fsm_iter);
351 }
352}
353
354void fsmenu_remove_entry(FSMenu *fsmenu, FSMenuCategory category, int idx)
355{
356 FSMenuEntry *fsm_prev = nullptr;
357 FSMenuEntry *fsm_iter;
358 FSMenuEntry *fsm_head;
359
360 fsm_head = ED_fsmenu_get_category(fsmenu, category);
361
362 for (fsm_iter = fsm_head; fsm_iter && idx; fsm_prev = fsm_iter, fsm_iter = fsm_iter->next) {
363 idx--;
364 }
365
366 if (fsm_iter) {
367 /* you should only be able to remove entries that were
368 * not added by default, like windows drives.
369 * also separators (where path == nullptr) shouldn't be removed */
370 if (fsm_iter->save && fsm_iter->path) {
371
372 /* remove fsme from list */
373 if (fsm_prev) {
374 fsm_prev->next = fsm_iter->next;
375 }
376 else {
377 fsm_head = fsm_iter->next;
378 ED_fsmenu_set_category(fsmenu, category, fsm_head);
379 }
380 /* free entry */
381 MEM_delete(fsm_iter->path);
382 MEM_delete(fsm_iter);
383 }
384 }
385}
386
387bool fsmenu_write_file(FSMenu *fsmenu, const char *filepath)
388{
389 FSMenuEntry *fsm_iter = nullptr;
390 char fsm_name[FILE_MAX];
391 int nwritten = 0;
392
393 FILE *fp = BLI_fopen(filepath, "w");
394 if (!fp) {
395 return false;
396 }
397
398 bool has_error = false;
399 has_error |= (fprintf(fp, "[Bookmarks]\n") < 0);
400 for (fsm_iter = ED_fsmenu_get_category(fsmenu, FS_CATEGORY_BOOKMARKS); fsm_iter;
401 fsm_iter = fsm_iter->next)
402 {
403 if (fsm_iter->path && fsm_iter->save) {
404 fsmenu_entry_generate_name(fsm_iter, fsm_name, sizeof(fsm_name));
405 if (fsm_iter->name[0] && !STREQ(fsm_iter->name, fsm_name)) {
406 has_error |= (fprintf(fp, "!%s\n", fsm_iter->name) < 0);
407 }
408 has_error |= (fprintf(fp, "%s\n", fsm_iter->path) < 0);
409 }
410 }
411 has_error = (fprintf(fp, "[Recent]\n") < 0);
412 for (fsm_iter = ED_fsmenu_get_category(fsmenu, FS_CATEGORY_RECENT);
413 fsm_iter && (nwritten < FSMENU_RECENT_MAX);
414 fsm_iter = fsm_iter->next, nwritten++)
415 {
416 if (fsm_iter->path && fsm_iter->save) {
417 fsmenu_entry_generate_name(fsm_iter, fsm_name, sizeof(fsm_name));
418 if (fsm_iter->name[0] && !STREQ(fsm_iter->name, fsm_name)) {
419 has_error |= (fprintf(fp, "!%s\n", fsm_iter->name) < 0);
420 }
421 has_error |= (fprintf(fp, "%s\n", fsm_iter->path) < 0);
422 }
423 }
424 fclose(fp);
425
426 return !has_error;
427}
428
429void fsmenu_read_bookmarks(FSMenu *fsmenu, const char *filepath)
430{
431 char line[FILE_MAXDIR];
432 char name[FILE_MAXFILE];
434 FILE *fp;
435
436 fp = BLI_fopen(filepath, "r");
437 if (!fp) {
438 return;
439 }
440
441 name[0] = '\0';
442
443 while (fgets(line, sizeof(line), fp) != nullptr) { /* read a line */
444 if (STRPREFIX(line, "[Bookmarks]")) {
445 category = FS_CATEGORY_BOOKMARKS;
446 }
447 else if (STRPREFIX(line, "[Recent]")) {
448 category = FS_CATEGORY_RECENT;
449 }
450 else if (line[0] == '!') {
451 int len = strlen(line);
452 if (len > 0) {
453 if (line[len - 1] == '\n') {
454 line[len - 1] = '\0';
455 }
456 STRNCPY(name, line + 1);
457 }
458 }
459 else {
460 int len = strlen(line);
461 if (len > 0) {
462 if (line[len - 1] == '\n') {
463 line[len - 1] = '\0';
464 }
465 /* Don't check if the path exists before adding because it can be slow on network drives,
466 * having a bookmark from a drive that's ejected or so isn't all that bad.
467 * See !138218 for details. */
468 fsmenu_insert_entry(fsmenu, category, line, name, ICON_FILE_FOLDER, FS_INSERT_SAVE);
469 }
470 /* always reset name. */
471 name[0] = '\0';
472 }
473 }
474 fclose(fp);
475}
476
477static void fsmenu_free_category(FSMenu *fsmenu, FSMenuCategory category)
478{
479 FSMenuEntry *fsm_iter = ED_fsmenu_get_category(fsmenu, category);
480
481 while (fsm_iter) {
482 FSMenuEntry *fsm_next = fsm_iter->next;
483
484 if (fsm_iter->path) {
485 MEM_delete(fsm_iter->path);
486 }
487 MEM_delete(fsm_iter);
488
489 fsm_iter = fsm_next;
490 }
491}
492
494{
497
500
501 /* Add all entries to system category */
502 fsmenu_read_system(fsmenu, true);
503}
504
505static void fsmenu_free_ex(FSMenu **fsmenu)
506{
507 if (*fsmenu != nullptr) {
513 MEM_delete(*fsmenu);
514 }
515
516 *fsmenu = nullptr;
517}
518
520{
522}
523
524int fsmenu_get_active_indices(FSMenu *fsmenu, enum FSMenuCategory category, const char *dir)
525{
526 FSMenuEntry *fsm_iter = ED_fsmenu_get_category(fsmenu, category);
527 int i;
528
529 for (i = 0; fsm_iter; fsm_iter = fsm_iter->next, i++) {
530 if (BLI_path_cmp(dir, fsm_iter->path) == 0) {
531 return i;
532 }
533 }
534
535 return -1;
536}
537
539{
540 /* For all platforms, we add some directories from User Preferences to
541 * the FS_CATEGORY_OTHER category so that these directories
542 * have the appropriate icons when they are added to the Bookmarks.
543 *
544 * NOTE: of the preferences support as `//` prefix.
545 * Skip them since they depend on the current loaded blend file. */
546
547 auto add_user_dir = [fsmenu](const char *dir, int icon) {
548 if (dir[0] && !BLI_path_is_rel(dir)) {
549 fsmenu_insert_entry(fsmenu, FS_CATEGORY_OTHER, dir, nullptr, icon, FS_INSERT_LAST);
550 }
551 };
552
553 add_user_dir(U.fontdir, ICON_FILE_FONT);
554 add_user_dir(U.textudir, ICON_FILE_IMAGE);
555
556 for (bUserScriptDirectory &script_dir : U.script_directories) {
557 if (script_dir.dir_path[0] == '\0') [[unlikely]] {
558 continue;
559 }
560 fsmenu_insert_entry(fsmenu,
562 script_dir.dir_path,
563 script_dir.name,
564 ICON_FILE_SCRIPT,
566 }
567
568 add_user_dir(U.sounddir, ICON_FILE_SOUND);
569 add_user_dir(U.tempdir, ICON_TEMP);
570}
571
572} // namespace blender
#define BLENDER_BOOKMARK_FILE
File and directory operations.
#define BLI_path_ncmp
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define BLI_path_cmp
#define BLI_string_joinN(...)
#define DATA_(msgid)
Read Blender memory allocation and freeing.
#define STRPREFIX(a, b)
#define ELEM(...)
#define STREQ(a, b)
#define FSMENU_RECENT_MAX
Definition fsmenu.hh:16
#define FSMENU_CURRENT_FILE_ICON
Definition fsmenu.hh:18
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
FSMenu * ED_fsmenu_get()
Definition fsmenu.cc:48
char * STRNCPY(char(&dst)[N], const char *src)
unsigned int uint
Definition BKE_text.h:55
static FSMenu * g_fsmenu
Definition fsmenu.cc:46
static void fsmenu_entry_generate_name(FSMenuEntry *fsentry, char *name, size_t name_size)
Definition fsmenu.cc:160
std::optional< std::string > BKE_appdir_folder_id_create(int folder_id, const char *subfolder) ATTR_WARN_UNUSED_RESULT
Definition appdir.cc:822
FSMenuEntry * ED_fsmenu_get_entry(FSMenu *fsmenu, FSMenuCategory category, int idx)
Definition fsmenu.cc:113
char * ED_fsmenu_entry_get_name(FSMenuEntry *fsentry)
Definition fsmenu.cc:177
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void ED_fsmenu_entry_set_path(FSMenuEntry *fsentry, const char *path)
Definition fsmenu.cc:131
void fsmenu_refresh_system_category(FSMenu *fsmenu)
Definition fsmenu.cc:493
bool fsmenu_write_file(FSMenu *fsmenu, const char *filepath)
Definition fsmenu.cc:387
int ED_fsmenu_entry_get_icon(FSMenuEntry *fsentry)
Definition fsmenu.cc:150
void ED_fsmenu_set_category(FSMenu *fsmenu, FSMenuCategory category, FSMenuEntry *fsm_head)
Definition fsmenu.cc:80
void ED_fsmenu_entry_set_name(FSMenuEntry *fsentry, const char *name)
Definition fsmenu.cc:191
void fsmenu_insert_entry(FSMenu *fsmenu, FSMenuCategory category, const char *path, const char *name, int icon, FSMenuInsert flag)
Definition fsmenu.cc:229
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
int fsmenu_get_active_indices(FSMenu *fsmenu, enum FSMenuCategory category, const char *dir)
Definition fsmenu.cc:524
static void fsmenu_free_ex(FSMenu **fsmenu)
Definition fsmenu.cc:505
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
void fsmenu_free()
Definition fsmenu.cc:519
bool BLI_path_name_at_index(const char *__restrict path, int index, int *__restrict r_offset, int *__restrict r_len) ATTR_NONNULL(1
void ED_fsmenu_entry_set_icon(FSMenuEntry *fsentry, int icon)
Definition fsmenu.cc:155
char * ED_fsmenu_entry_get_path(FSMenuEntry *fsentry)
Definition fsmenu.cc:126
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void fsmenu_add_common_platform_directories(FSMenu *fsmenu)
Definition fsmenu.cc:538
short fsmenu_can_save(FSMenu *fsmenu, FSMenuCategory category, int idx)
Definition fsmenu.cc:216
@ FS_CATEGORY_BOOKMARKS
@ FS_CATEGORY_SYSTEM_BOOKMARKS
void fsmenu_remove_entry(FSMenu *fsmenu, FSMenuCategory category, int idx)
Definition fsmenu.cc:354
int ED_fsmenu_get_nentries(FSMenu *fsmenu, FSMenuCategory category)
Definition fsmenu.cc:101
@ BLENDER_USER_CONFIG
constexpr char SEP
void fsmenu_read_system(FSMenu *fsmenu, int read_bookmarks)
FSMenuEntry * ED_fsmenu_get_category(FSMenu *fsmenu, FSMenuCategory category)
Definition fsmenu.cc:56
static void fsmenu_free_category(FSMenu *fsmenu, FSMenuCategory category)
Definition fsmenu.cc:477
void fsmenu_read_bookmarks(FSMenu *fsmenu, const char *filepath)
Definition fsmenu.cc:429
FSMenuEntry * fsmenu_system
Definition fsmenu.cc:39
FSMenuEntry * fsmenu_recent
Definition fsmenu.cc:42
FSMenuEntry * fsmenu_bookmarks
Definition fsmenu.cc:41
FSMenuEntry * fsmenu_other
Definition fsmenu.cc:43
FSMenuEntry * fsmenu_system_bookmarks
Definition fsmenu.cc:40
#define SEP_STR
Definition unit.cc:41