Blender  V3.3
editors/asset/intern/asset_catalog.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BKE_asset_catalog.hh"
8 #include "BKE_asset_library.hh"
9 #include "BKE_main.h"
10 
11 #include "BLI_string_utils.h"
12 
13 #include "ED_asset_catalog.h"
14 #include "ED_asset_catalog.hh"
15 
16 #include "WM_api.h"
17 
18 using namespace blender;
19 using namespace blender::bke;
20 
24 };
25 
26 static bool catalog_name_exists_fn(void *arg, const char *name)
27 {
28  CatalogUniqueNameFnData &fn_data = *static_cast<CatalogUniqueNameFnData *>(arg);
29  AssetCatalogPath fullpath = AssetCatalogPath(fn_data.parent_path) / name;
30  return fn_data.catalog_service.find_catalog_by_path(fullpath);
31 }
32 
33 static std::string catalog_name_ensure_unique(AssetCatalogService &catalog_service,
34  StringRefNull name,
35  StringRef parent_path)
36 {
37  CatalogUniqueNameFnData fn_data = {catalog_service, parent_path};
38 
39  char unique_name[MAX_NAME] = "";
41  catalog_name_exists_fn, &fn_data, name.c_str(), '.', unique_name, sizeof(unique_name));
42 
43  return unique_name;
44 }
45 
47  StringRefNull name,
48  StringRef parent_path)
49 {
51  if (!catalog_service) {
52  return nullptr;
53  }
54 
55  std::string unique_name = catalog_name_ensure_unique(*catalog_service, name, parent_path);
56  AssetCatalogPath fullpath = AssetCatalogPath(parent_path) / unique_name;
57 
58  catalog_service->undo_push();
59  bke::AssetCatalog *new_catalog = catalog_service->create_catalog(fullpath);
60  if (!new_catalog) {
61  return nullptr;
62  }
63  catalog_service->tag_has_unsaved_changes(new_catalog);
64 
66  return new_catalog;
67 }
68 
70 {
72  if (!catalog_service) {
74  return;
75  }
76 
77  catalog_service->undo_push();
78  catalog_service->tag_has_unsaved_changes(nullptr);
79  catalog_service->prune_catalogs_by_id(catalog_id);
81 }
82 
84  const CatalogID catalog_id,
85  const StringRefNull new_name)
86 {
88  if (!catalog_service) {
90  return;
91  }
92 
93  AssetCatalog *catalog = catalog_service->find_catalog(catalog_id);
94 
95  const AssetCatalogPath new_path = catalog->path.parent() / StringRef(new_name);
96  const AssetCatalogPath clean_new_path = new_path.cleanup();
97 
98  if (new_path == catalog->path || clean_new_path == catalog->path) {
99  /* Nothing changed, so don't bother renaming for nothing. */
100  return;
101  }
102 
103  catalog_service->undo_push();
104  catalog_service->tag_has_unsaved_changes(catalog);
105  catalog_service->update_catalog_path(catalog_id, clean_new_path);
107 }
108 
110  const CatalogID src_catalog_id,
111  const std::optional<CatalogID> dst_parent_catalog_id)
112 {
114  if (!catalog_service) {
116  return;
117  }
118 
119  AssetCatalog *src_catalog = catalog_service->find_catalog(src_catalog_id);
120  if (!src_catalog) {
122  return;
123  }
124  AssetCatalog *dst_catalog = dst_parent_catalog_id ?
125  catalog_service->find_catalog(*dst_parent_catalog_id) :
126  nullptr;
127  if (!dst_catalog && dst_parent_catalog_id) {
129  return;
130  }
131 
133  *catalog_service, src_catalog->path.name(), dst_catalog ? dst_catalog->path.c_str() : "");
134  /* If a destination catalog was given, construct the path using that. Otherwise, the path is just
135  * the name of the catalog to be moved, which means it ends up at the root level. */
136  const AssetCatalogPath new_path = dst_catalog ? (dst_catalog->path / unique_name) :
138  const AssetCatalogPath clean_new_path = new_path.cleanup();
139 
140  if (new_path == src_catalog->path || clean_new_path == src_catalog->path) {
141  /* Nothing changed, so don't bother renaming for nothing. */
142  return;
143  }
144 
145  catalog_service->undo_push();
146  catalog_service->tag_has_unsaved_changes(src_catalog);
147  catalog_service->update_catalog_path(src_catalog_id, clean_new_path);
149 }
150 
152 {
154  if (!catalog_service) {
156  return;
157  }
158 
159  /* Since writing to disk also means loading any on-disk changes, it may be a good idea to store
160  * an undo step. */
161  catalog_service->undo_push();
162  catalog_service->write_to_disk(bmain->filepath);
163 }
164 
166 {
168 }
169 
171 {
173 }
blender::bke::AssetCatalogService * BKE_asset_library_get_catalog_service(const ::AssetLibrary *library)
#define BLI_assert_unreachable()
Definition: BLI_assert.h:93
bool BLI_uniquename_cb(UniquenameCheckCallback unique_check, void *arg, const char *defname, char delim, char *name, size_t name_len)
Definition: string_utils.c:233
#define MAX_NAME
Definition: DNA_defs.h:48
#define ND_SPACE_ASSET_PARAMS
Definition: WM_types.h:468
#define NC_SPACE
Definition: WM_types.h:342
constexpr const char * c_str() const
AssetCatalogPath cleanup() const
AssetCatalogPath parent() const
AssetCatalog * create_catalog(const AssetCatalogPath &catalog_path)
bool write_to_disk(const CatalogFilePath &blend_file_path)
AssetCatalog * find_catalog_by_path(const AssetCatalogPath &path) const
void update_catalog_path(CatalogID catalog_id, const AssetCatalogPath &new_catalog_path)
void tag_has_unsaved_changes(AssetCatalog *edited_catalog)
AssetCatalog * find_catalog(CatalogID catalog_id) const
void ED_asset_catalog_rename(::AssetLibrary *library, const CatalogID catalog_id, const StringRefNull new_name)
AssetCatalog * ED_asset_catalog_add(::AssetLibrary *library, StringRefNull name, StringRef parent_path)
bool ED_asset_catalogs_get_save_catalogs_when_file_is_saved()
void ED_asset_catalogs_save_from_main_path(::AssetLibrary *library, const Main *bmain)
void ED_asset_catalogs_set_save_catalogs_when_file_is_saved(const bool should_save)
void ED_asset_catalog_remove(::AssetLibrary *library, const CatalogID &catalog_id)
static bool catalog_name_exists_fn(void *arg, const char *name)
static std::string catalog_name_ensure_unique(AssetCatalogService &catalog_service, StringRefNull name, StringRef parent_path)
void ED_asset_catalog_move(::AssetLibrary *library, const CatalogID src_catalog_id, const std::optional< CatalogID > dst_parent_catalog_id)
static void unique_name(bNode *node)
const AssetCatalogService & catalog_service
Definition: BKE_main.h:121
char filepath[1024]
Definition: BKE_main.h:124
Universally Unique Identifier according to RFC4122.
static bool save_catalogs_when_file_is_saved
static FT_Library library
void WM_main_add_notifier(unsigned int type, void *reference)