Blender V4.3
editors/asset/intern/asset_catalog.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "AS_asset_library.hh"
10
11#include "AS_asset_catalog.hh"
13
14#include "BKE_main.hh"
15
16#include "BLI_string_utils.hh"
17
18#include "RNA_access.hh"
19#include "RNA_prototypes.hh"
20
21#include "ED_asset_catalog.hh"
22
23#include "WM_api.hh"
24
25namespace blender::ed::asset {
26
27using namespace blender::asset_system;
28
29bool catalogs_read_only(const AssetLibrary &library)
30{
31 const asset_system::AssetCatalogService &catalog_service = library.catalog_service();
32 return catalog_service.is_read_only();
33}
34
39
40static bool catalog_name_exists_fn(void *arg, const char *name)
41{
42 CatalogUniqueNameFnData &fn_data = *static_cast<CatalogUniqueNameFnData *>(arg);
43 AssetCatalogPath fullpath = AssetCatalogPath(fn_data.parent_path) / name;
44 return fn_data.catalog_service.find_catalog_by_path(fullpath);
45}
46
47static std::string catalog_name_ensure_unique(AssetCatalogService &catalog_service,
48 StringRefNull name,
49 StringRef parent_path)
50{
51 CatalogUniqueNameFnData fn_data = {catalog_service, parent_path};
52
53 char unique_name[MAX_NAME] = "";
55 catalog_name_exists_fn, &fn_data, name.c_str(), '.', unique_name, sizeof(unique_name));
56
57 return unique_name;
58}
59
61 StringRefNull name,
62 StringRef parent_path)
63{
64 asset_system::AssetCatalogService &catalog_service = library->catalog_service();
65 if (catalog_service.is_read_only()) {
66 return nullptr;
67 }
68
69 std::string unique_name = catalog_name_ensure_unique(catalog_service, name, parent_path);
70 AssetCatalogPath fullpath = AssetCatalogPath(parent_path) / unique_name;
71
72 catalog_service.undo_push();
73 asset_system::AssetCatalog *new_catalog = catalog_service.create_catalog(fullpath);
74 if (!new_catalog) {
75 return nullptr;
76 }
77 catalog_service.tag_has_unsaved_changes(new_catalog);
78
80 return new_catalog;
81}
82
83void catalog_remove(AssetLibrary *library, const CatalogID &catalog_id)
84{
85 asset_system::AssetCatalogService &catalog_service = library->catalog_service();
86 if (catalog_service.is_read_only()) {
87 return;
88 }
89
90 catalog_service.undo_push();
91 catalog_service.tag_has_unsaved_changes(nullptr);
92 catalog_service.prune_catalogs_by_id(catalog_id);
94}
95
97 const CatalogID catalog_id,
98 const StringRefNull new_name)
99{
100 asset_system::AssetCatalogService &catalog_service = library->catalog_service();
101 if (catalog_service.is_read_only()) {
102 return;
103 }
104
105 AssetCatalog *catalog = catalog_service.find_catalog(catalog_id);
106
107 const AssetCatalogPath new_path = catalog->path.parent() / StringRef(new_name);
108 const AssetCatalogPath clean_new_path = new_path.cleanup();
109
110 if (new_path == catalog->path || clean_new_path == catalog->path) {
111 /* Nothing changed, so don't bother renaming for nothing. */
112 return;
113 }
114
115 catalog_service.undo_push();
116 catalog_service.tag_has_unsaved_changes(catalog);
117 catalog_service.update_catalog_path(catalog_id, clean_new_path);
119}
120
122 const CatalogID src_catalog_id,
123 const std::optional<CatalogID> dst_parent_catalog_id)
124{
125 asset_system::AssetCatalogService &catalog_service = library->catalog_service();
126 if (catalog_service.is_read_only()) {
127 return;
128 }
129
130 AssetCatalog *src_catalog = catalog_service.find_catalog(src_catalog_id);
131 if (!src_catalog) {
133 return;
134 }
135 AssetCatalog *dst_catalog = dst_parent_catalog_id ?
136 catalog_service.find_catalog(*dst_parent_catalog_id) :
137 nullptr;
138 if (!dst_catalog && dst_parent_catalog_id) {
140 return;
141 }
142
144 catalog_service, src_catalog->path.name(), dst_catalog ? dst_catalog->path.c_str() : "");
145 /* If a destination catalog was given, construct the path using that. Otherwise, the path is just
146 * the name of the catalog to be moved, which means it ends up at the root level. */
147 const AssetCatalogPath new_path = dst_catalog ? (dst_catalog->path / unique_name) :
149 const AssetCatalogPath clean_new_path = new_path.cleanup();
150
151 if (new_path == src_catalog->path || clean_new_path == src_catalog->path) {
152 /* Nothing changed, so don't bother renaming for nothing. */
153 return;
154 }
155
156 catalog_service.undo_push();
157 catalog_service.tag_has_unsaved_changes(src_catalog);
158 catalog_service.update_catalog_path(src_catalog_id, clean_new_path);
160}
161
163{
164 asset_system::AssetCatalogService &catalog_service = library->catalog_service();
165 if (catalog_service.is_read_only()) {
166 return;
167 }
168
169 /* Since writing to disk also means loading any on-disk changes, it may be a good idea to store
170 * an undo step. */
171 catalog_service.undo_push();
172 catalog_service.write_to_disk(bmain->filepath);
173}
174
179
184
185} // namespace blender::ed::asset
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
size_t void BLI_uniquename_cb(UniquenameCheckCallback unique_check, void *arg, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(1
#define MAX_NAME
Definition DNA_defs.h:50
#define ND_SPACE_ASSET_PARAMS
Definition WM_types.hh:491
#define NC_SPACE
Definition WM_types.hh:359
AssetCatalog * find_catalog_by_path(const AssetCatalogPath &path) const
AssetCatalog * find_catalog(CatalogID catalog_id) const
bool write_to_disk(const CatalogFilePath &blend_file_path)
AssetCatalog * create_catalog(const AssetCatalogPath &catalog_path)
void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path)
AssetCatalogService & catalog_service() const
void catalog_remove(asset_system::AssetLibrary *library, const asset_system::CatalogID &catalog_id)
bool catalogs_read_only(const asset_system::AssetLibrary &library)
void catalogs_save_from_main_path(asset_system::AssetLibrary *library, const Main *bmain)
static std::string catalog_name_ensure_unique(AssetCatalogService &catalog_service, StringRefNull name, StringRef parent_path)
asset_system::AssetCatalog * catalog_add(asset_system::AssetLibrary *library, StringRefNull name, StringRef parent_path=nullptr)
void catalog_move(asset_system::AssetLibrary *library, asset_system::CatalogID src_catalog_id, std::optional< asset_system::CatalogID > dst_parent_catalog_id=std::nullopt)
void catalogs_set_save_catalogs_when_file_is_saved(bool should_save)
static bool catalog_name_exists_fn(void *arg, const char *name)
void catalog_rename(asset_system::AssetLibrary *library, asset_system::CatalogID catalog_id, StringRefNull new_name)
static void unique_name(bNode *node)
char filepath[1024]
Definition BKE_main.hh:136
Universally Unique Identifier according to RFC4122.
void WM_main_add_notifier(uint type, void *reference)