KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block_library_adapter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include "design_block.h"
23
25#include <design_block_io.h>
26#include <env_vars.h>
27#include <ki_exception.h>
28#include <trace_helpers.h>
29
30#include <magic_enum.hpp>
31#include <wx/log.h>
32
33
35
37
38
43
44
49
50
52{
53 return ENV_VAR::GetVersionedEnvVarName( wxS( "DESIGN_BLOCK_DIR" ) );
54}
55
56
58{
59 DESIGN_BLOCK_IO* ret = dynamic_cast<DESIGN_BLOCK_IO*>( aRow->plugin.get() );
60 wxCHECK( aRow->plugin && ret, nullptr );
61 return ret;
62}
63
64
66{
67 // TODO(JE) do we maintain a precondition that URIs are absolute paths after expansion?
69
71 {
72 wxString msg;
73 wxFileName fileName( m_manager.GetFullURI( row, true ) );
74
75 if( fileName.FileExists() )
76 return tl::unexpected( LIBRARY_TABLE_OK() );
77 else
78 msg = wxString::Format( _( "Nested table '%s' not found." ), row->URI() );
79
80 return tl::unexpected( LIBRARY_ERROR( msg ) );
81 }
83 {
84 wxLogTrace( traceLibraries, "Sym: Plugin type %s is unknown!", row->Type() );
85 wxString msg = wxString::Format( _( "Unknown library type %s " ), row->Type() );
86 return tl::unexpected( LIBRARY_ERROR( msg ) );
87 }
88
90 wxCHECK( plugin, tl::unexpected( LIBRARY_ERROR( _( "Internal error" ) ) ) );
91
92 return plugin;
93}
94
95
97{
98 return dbplugin( aRow );
99}
100
101
103{
104 wxArrayString dummyList;
105 std::map<std::string, UTF8> options = aLib->row->GetOptionsMap();
106 dbplugin( aLib )->DesignBlockEnumerate( dummyList, aUri, false, &options );
107}
108
109
110std::optional<LIB_STATUS> DESIGN_BLOCK_LIBRARY_ADAPTER::LoadOne( LIB_DATA* aLib )
111{
113
114 std::map<std::string, UTF8> options = aLib->row->GetOptionsMap();
115
116 try
117 {
118 wxArrayString dummyList;
119 dbplugin( aLib )->DesignBlockEnumerate( dummyList, getUri( aLib->row ), false, &options );
120 wxLogTrace( traceLibraries, "DB: %s: library enumerated %zu items", aLib->row->Nickname(), dummyList.size() );
122 }
123 catch( IO_ERROR& e )
124 {
126 aLib->status.error = LIBRARY_ERROR( { e.What() } );
127 wxLogTrace( traceLibraries, "DB: %s: plugin threw exception: %s", aLib->row->Nickname(), e.What() );
128 }
129
130 return aLib->status;
131}
132
133
134std::optional<LIB_STATUS> DESIGN_BLOCK_LIBRARY_ADAPTER::LoadOne( const wxString& nickname )
135{
137
138 if( result.has_value() )
139 return LoadOne( *result );
140
141 return LIB_STATUS{
142 .load_status = LOAD_STATUS::LOAD_ERROR,
143 .error = LIBRARY_ERROR( { result.error() } )
144 };
145}
146
147
148std::vector<DESIGN_BLOCK*> DESIGN_BLOCK_LIBRARY_ADAPTER::GetDesignBlocks( const wxString& aNickname )
149{
150 std::vector<DESIGN_BLOCK*> blocks;
151
152 std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname );
153
154 if( !maybeLib )
155 return blocks;
156
157 const LIB_DATA* lib = *maybeLib;
158 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
159 wxArrayString blockNames;
160
161 try
162 {
163 dbplugin( lib )->DesignBlockEnumerate( blockNames, getUri( lib->row ), false, &options );
164 }
165 catch( IO_ERROR& e )
166 {
167 wxLogTrace( traceLibraries, "DB: Exception enumerating library %s: %s", lib->row->Nickname(), e.What() );
168 }
169
170 for( const wxString& blockName : blockNames )
171 {
172 try
173 {
174 blocks.emplace_back( dbplugin( lib )->DesignBlockLoad( getUri( lib->row ), blockName, false, &options ) );
175 }
176 catch( IO_ERROR& e )
177 {
178 wxLogTrace( traceLibraries, "DB: Exception enumerating design block %s: %s", blockName, e.What() );
179 }
180 }
181
182 return blocks;
183}
184
185
186std::vector<wxString> DESIGN_BLOCK_LIBRARY_ADAPTER::GetDesignBlockNames( const wxString& aNickname )
187{
188 // TODO(JE) can we kill wxArrayString in internal API?
189 wxArrayString namesAS;
190 std::vector<wxString> names;
191
192 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
193 {
194 const LIB_DATA* lib = *maybeLib;
195 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
196
197 dbplugin( lib )->DesignBlockEnumerate( namesAS, getUri( lib->row ), true, &options );
198 }
199
200 for( const wxString& name : namesAS )
201 names.emplace_back( name );
202
203 return names;
204}
205
206
208 const wxString& aDesignBlockName, bool aKeepUUID )
209{
210 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
211 {
212 const LIB_DATA* lib = *maybeLib;
213 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
214
215 try
216 {
217 if( DESIGN_BLOCK* db =
218 dbplugin( lib )->DesignBlockLoad( getUri( lib->row ), aDesignBlockName, aKeepUUID, &options ) )
219 {
220 db->GetLibId().SetLibNickname( aNickname );
221 return db;
222 }
223 }
224 catch( const IO_ERROR& ioe )
225 {
226 wxLogTrace( traceLibraries, "LoadDesignBlock: error loading %s:%s: %s", aNickname, aDesignBlockName,
227 ioe.What() );
228 }
229 }
230 else
231 {
232 wxLogTrace( traceLibraries, "LoadDesignBlock: requested library %s not loaded", aNickname );
233 }
234
235 return nullptr;
236}
237
238
239bool DESIGN_BLOCK_LIBRARY_ADAPTER::DesignBlockExists( const wxString& aNickname, const wxString& aDesignBlockName )
240{
241 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
242 {
243 const LIB_DATA* lib = *maybeLib;
244 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
245
246 return dbplugin( lib )->DesignBlockExists( getUri( lib->row ), aDesignBlockName, &options );
247 }
248
249 return false;
250}
251
252
254 const wxString& aDesignBlockName )
255{
256 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
257 {
258 const LIB_DATA* lib = *maybeLib;
259 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
260
261 return dbplugin( lib )->GetEnumeratedDesignBlock( getUri( lib->row ), aDesignBlockName, &options );
262 }
263
264 return nullptr;
265}
266
267
269 const DESIGN_BLOCK* aDesignBlock,
270 bool aOverwrite )
271{
272 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
273 {
274 const LIB_DATA* lib = *maybeLib;
275 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
276
277 if( !aOverwrite && dbplugin( lib )->DesignBlockExists( getUri( lib->row ), aDesignBlock->GetName(), &options ) )
278 return SAVE_SKIPPED;
279
280 dbplugin( lib )->DesignBlockSave( getUri( lib->row ), aDesignBlock, &options );
281 }
282
283 return SAVE_OK;
284}
285
286
288 const wxString& aDesignBlockName )
289{
290 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
291 {
292 const LIB_DATA* lib = *maybeLib;
293 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
294 return dbplugin( lib )->DesignBlockDelete( getUri( lib->row ), aDesignBlockName, &options );
295 }
296}
297
298
300{
301 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
302 {
303 const LIB_DATA* lib = *maybeLib;
304 return plugin( lib )->IsLibraryWritable( getUri( lib->row ) );
305 }
306
307 return false;
308}
309
310
312 bool aKeepUUID )
313{
314 wxString nickname = aDesignBlockId.GetLibNickname();
315 wxString DesignBlockname = aDesignBlockId.GetLibItemName();
316
317 if( nickname.size() )
318 return LoadDesignBlock( nickname, DesignBlockname, aKeepUUID );
319
320 // nickname is empty, sequentially search (alphabetically) all libs/nicks for first match:
321 for( const wxString& library : GetLibraryNames() )
322 {
323 // DesignBlockLoad() returns NULL on not found, does not throw exception
324 // unless there's an IO_ERROR.
325 if( DESIGN_BLOCK* ret = LoadDesignBlock( library, DesignBlockname, aKeepUUID ) )
326 return ret;
327 }
328
329 return nullptr;
330}
const char * name
@ DESIGN_BLOCK_FILE_UNKNOWN
0 is not a legal menu id on Mac
static DESIGN_BLOCK_FILE_T EnumFromStr(const wxString &aFileType)
static DESIGN_BLOCK_IO * FindPlugin(DESIGN_BLOCK_FILE_T aFileType)
bool DesignBlockExists(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockDelete(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockSave(const wxString &aLibraryPath, const DESIGN_BLOCK *aDesignBlock, const std::map< std::string, UTF8 > *aProperties=nullptr)
const DESIGN_BLOCK * GetEnumeratedDesignBlock(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockEnumerate(wxArrayString &aDesignBlockNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
std::vector< wxString > GetDesignBlockNames(const wxString &aNickname)
SAVE_T
The set of return values from DesignBlockSave() below.
std::vector< DESIGN_BLOCK * > GetDesignBlocks(const wxString &aNickname)
IO_BASE * plugin(const LIB_DATA *aRow) override
static LEAK_AT_EXIT< std::shared_mutex > GlobalLibraryMutex
static DESIGN_BLOCK_IO * dbplugin(const LIB_DATA *aRow)
Helper to cast the ABC plugin in the LIB_DATA* to a concrete plugin.
void DeleteDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName)
Delete the aDesignBlockName from the library given by aNickname.
LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row) override
Creates a concrete plugin for the given row.
static LEAK_AT_EXIT< std::map< wxString, LIB_DATA > > GlobalLibraries
DESIGN_BLOCK * LoadDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName, bool aKeepUUID=false)
Load a design block having aDesignBlockName from the library given by aNickname.
DESIGN_BLOCK * DesignBlockLoadWithOptionalNickname(const LIB_ID &aDesignBlockId, bool aKeepUUID=false)
Load a design block having aDesignBlockId with possibly an empty nickname.
SAVE_T SaveDesignBlock(const wxString &aNickname, const DESIGN_BLOCK *aDesignBlock, bool aOverwrite=true)
Write aDesignBlock to an existing library given by aNickname.
DESIGN_BLOCK_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
bool IsDesignBlockLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
const DESIGN_BLOCK * GetEnumeratedDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName)
A version of #DesignBlockLoad() for use after #DesignBlockEnumerate() for more efficient cache manage...
bool DesignBlockExists(const wxString &aNickname, const wxString &aDesignBlockName)
Indicates whether or not the given design block already exists in the given library.
void enumerateLibrary(LIB_DATA *aLib, const wxString &aUri) override
Override in derived class to perform library-specific enumeration.
wxString GetName() const override
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition io_base.cpp:60
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A wrapper for static data that should not be destroyed at program exit.
void evictOwnedGlobalEntries()
Erases this adapter's own entries (LIB_DATA::global_owner == this) from the process-wide globalLibs()...
LIBRARY_MANAGER_ADAPTER(LIBRARY_MANAGER &aManager)
Constructs a type-specific adapter into the library manager.
LIBRARY_RESULT< LIB_DATA * > loadIfNeeded(const wxString &aNickname)
Fetches a loaded library, triggering a load of that library if it isn't loaded yet.
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
LIBRARY_MANAGER & m_manager
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
std::map< std::string, UTF8 > GetOptionsMap() const
const wxString & Type() const
const wxString & URI() const
const wxString & Nickname() const
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
#define _(s)
Functions related to environment variables, including help functions.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:77
Storage for an actual loaded library (including library content owned by the plugin)
LIB_STATUS status
std::unique_ptr< IO_BASE > plugin
const LIBRARY_TABLE_ROW * row
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
LOAD_STATUS load_status
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.