Blender  V3.3
preferences.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
9 #include <string.h>
10 
11 #include "MEM_guardedalloc.h"
12 
13 #include "BLI_fileops.h"
14 #include "BLI_listbase.h"
15 #include "BLI_path_util.h"
16 #include "BLI_string.h"
17 #include "BLI_string_utf8.h"
18 #include "BLI_string_utils.h"
19 
20 #include "BKE_appdir.h"
21 #include "BKE_preferences.h"
22 
23 #include "BLT_translation.h"
24 
25 #include "DNA_userdef_types.h"
26 
27 #define U BLI_STATIC_ASSERT(false, "Global 'U' not allowed, only use arguments passed in!")
28 
29 /* -------------------------------------------------------------------- */
34  const char *name,
35  const char *path)
36 {
37  bUserAssetLibrary *library = MEM_callocN(sizeof(*library), "bUserAssetLibrary");
38 
40 
41  if (name) {
43  }
44  if (path) {
45  BLI_strncpy(library->path, path, sizeof(library->path));
46  }
47 
48  return library;
49 }
50 
52 {
54 }
55 
58  const char *name)
59 {
60  BLI_strncpy_utf8(library->name, name, sizeof(library->name));
62  library,
63  name,
64  '.',
65  offsetof(bUserAssetLibrary, name),
66  sizeof(library->name));
67 }
68 
70 {
71  BLI_strncpy(library->path, path, sizeof(library->path));
72  if (BLI_is_file(library->path)) {
74  }
75 }
76 
78 {
79  return BLI_findlink(&userdef->asset_libraries, index);
80 }
81 
83  const char *name)
84 {
85  return BLI_findstring(&userdef->asset_libraries, name, offsetof(bUserAssetLibrary, name));
86 }
87 
89  const char *path)
90 {
91  LISTBASE_FOREACH (bUserAssetLibrary *, asset_lib_pref, &userdef->asset_libraries) {
92  if (BLI_path_contains(asset_lib_pref->path, path)) {
93  return asset_lib_pref;
94  }
95  }
96  return NULL;
97 }
98 
100  const bUserAssetLibrary *library)
101 {
102  return BLI_findindex(&userdef->asset_libraries, library);
103 }
104 
106 {
107  char documents_path[FILE_MAXDIR];
108 
109  /* No home or documents path found, not much we can do. */
110  if (!BKE_appdir_folder_documents(documents_path) || !documents_path[0]) {
111  return;
112  }
113 
116 
117  /* Add new "Default" library under '[doc_path]/Blender/Assets'. */
119  library->path, sizeof(library->path), documents_path, N_("Blender"), N_("Assets"), NULL);
120 }
121 
bool BKE_appdir_folder_documents(char *dir)
Definition: appdir.c:173
#define BKE_PREFS_ASSET_LIBRARY_DEFAULT_NAME
File and directory operations.
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:402
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:239
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void * BLI_findstring(const struct ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1717
size_t BLI_path_join(char *__restrict dst, size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
bool BLI_path_parent_dir(char *path) ATTR_NONNULL()
Definition: path_util.c:623
#define FILE_MAXDIR
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL(1
bool BLI_uniquename(struct ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_len)
Definition: string_utils.c:309
#define DATA_(msgid)
Read Guarded memory(de)allocation.
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
bUserAssetLibrary * BKE_preferences_asset_library_containing_path(const UserDef *userdef, const char *path)
Definition: preferences.c:88
void BKE_preferences_asset_library_default_add(UserDef *userdef)
Definition: preferences.c:105
bUserAssetLibrary * BKE_preferences_asset_library_find_from_name(const UserDef *userdef, const char *name)
Definition: preferences.c:82
bUserAssetLibrary * BKE_preferences_asset_library_find_from_index(const UserDef *userdef, int index)
Definition: preferences.c:77
void BKE_preferences_asset_library_path_set(bUserAssetLibrary *library, const char *path)
Definition: preferences.c:69
int BKE_preferences_asset_library_get_index(const UserDef *userdef, const bUserAssetLibrary *library)
Definition: preferences.c:99
void BKE_preferences_asset_library_name_set(UserDef *userdef, bUserAssetLibrary *library, const char *name)
Definition: preferences.c:56
void BKE_preferences_asset_library_remove(UserDef *userdef, bUserAssetLibrary *library)
Definition: preferences.c:51
bUserAssetLibrary * BKE_preferences_asset_library_add(UserDef *userdef, const char *name, const char *path)
Definition: preferences.c:33
struct ListBase asset_libraries
#define N_(msgid)
static FT_Library library