21#include <magic_enum.hpp>
62 wxCHECK( aRow->
plugin && ret,
nullptr );
72 std::lock_guard lock ( lib->
mutex );
79 wxArrayString dummyList;
81 wxLogTrace(
traceLibraries,
"Sym: %s: library enumerated %zu items", aNickname, dummyList.size() );
88 wxLogTrace(
traceLibraries,
"Sym: %s: plugin threw exception: %s", aNickname, e.
What() );
102 if( type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
105 wxString msg = wxString::Format(
_(
"Unknown library type %s " ), row->
Type() );
112 plugin->SetLibraryManagerAdapter(
this );
114 wxLogTrace(
traceLibraries,
"Sym: Library %s (%s) plugin created",
124 std::vector<LIB_SYMBOL*> symbols;
126 std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname );
143 wxLogTrace(
traceLibraries,
"Sym: Exception enumerating library %s: %s",
149 LIB_ID id = symbol->GetLibId();
151 symbol->SetLibId(
id );
161 wxArrayString namesAS;
162 std::vector<wxString> names;
164 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
175 for(
const wxString&
name : namesAS )
176 names.emplace_back(
name );
184 if( std::optional<const LIB_DATA*> lib =
fetchIfLoaded( aNickname ) )
188 LIB_ID id = symbol->GetLibId();
190 symbol->SetLibId(
id );
196 wxLogTrace(
traceLibraries,
"LoadSymbol: requested library %s not loaded", aNickname );
212 wxCHECK_MSG(
false, ,
"Unimplemented!" );
232 [](
const std::future<void>& aFuture ) {
return aFuture.valid(); } );
236 wxLogTrace(
traceLibraries,
"Sym: Cannot AsyncLoad, futures from a previous call remain!" );
247 wxLogTrace(
traceLibraries,
"Sym: AsyncLoad: no libraries left to load; exiting" );
254 [](
const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::mutex& aMutex )
256 std::lock_guard lock( aMutex );
258 if( aMap.contains( aLib ) )
271 wxString nickname = row->Nickname();
288 [
this, nickname, scope]()
293 LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( nickname );
295 if( result.has_value() )
297 LIB_DATA* lib = *result;
298 wxArrayString dummyList;
299 std::lock_guard lock ( lib->mutex );
300 lib->status.load_status = LOAD_STATUS::LOADING;
302 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
306 plugin( lib )->EnumerateSymbolLib( dummyList, getUri( lib->row ), &options );
307 wxLogTrace( traceLibraries,
"Sym: %s: library enumerated %zu items", nickname, dummyList.size() );
308 lib->status.load_status = LOAD_STATUS::LOADED;
312 lib->status.load_status = LOAD_STATUS::LOAD_ERROR;
313 lib->status.error = LIBRARY_ERROR( { e.What() } );
314 wxLogTrace( traceLibraries,
"Sym: %s: plugin threw exception: %s", nickname, e.What() );
321 case LIBRARY_TABLE_SCOPE::GLOBAL:
323 std::lock_guard lock( GlobalLibraryMutex );
325 GlobalLibraries[nickname].status = LIB_STATUS( {
326 .load_status = LOAD_STATUS::LOAD_ERROR,
327 .error = result.error()
333 case LIBRARY_TABLE_SCOPE::PROJECT:
335 wxLogTrace( traceLibraries,
"Sym: project library error: %s: %s", nickname, result.error().message );
336 std::lock_guard lock( m_libraries_mutex );
338 m_libraries[nickname].status = LIB_STATUS( {
339 .load_status = LOAD_STATUS::LOAD_ERROR,
340 .error = result.error()
347 wxFAIL_MSG(
"Unexpected library table scope" );
355 wxLogTrace(
traceLibraries,
"Sym: Started async load of %zu libraries", m_loadTotal );
360 const wxString& aNickname )
const
412 std::vector<std::pair<wxString, LIB_STATUS>> ret;
418 ret.emplace_back( std::make_pair( row->Nickname(), *
result ) );
423 ret.emplace_back( std::make_pair( row->Nickname(),
LIB_STATUS( {
425 .error =
LIBRARY_ERROR(
_(
"Library not found in library table" ) )
435 const wxString& aNickname )
437 std::vector<wxString> fields;
470 const wxString& aNickname )
const
472 std::vector<SUB_LIBRARY> ret;
477 std::vector<wxString> names;
480 for(
const wxString&
name : names )
496 return ( *result )->plugin->SupportsConfigurationDialog();
503 wxWindow* aParent )
const
505 std::optional<const LIB_DATA*> optRow =
fetchIfLoaded( aNickname );
507 if( !optRow || !( *optRow )->plugin->SupportsConfigurationDialog() )
510 DIALOG_SHIM* dialog = ( *optRow )->plugin->CreateConfigurationDialog( aParent );
524 wxCHECK2( rowData->
row,
continue );
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
virtual void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Create a new empty library at aLibraryPath empty.
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()
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::map< wxString, LIB_DATA > m_libraries
std::vector< std::future< void > > m_futures
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
std::mutex m_libraries_mutex
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
LIBRARY_TABLE_SCOPE Scope() const
std::map< std::string, UTF8 > GetOptionsMap() const
const wxString & Type() const
const wxString & Nickname() const
A logical library item identifier and consists of various portions much like a URI.
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Define a library symbol object.
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the #SCH_FILE_T from the corresponding plugin type name: "kicad", "legacy",...
Base class that schematic file and library loading and saving plugins should derive from.
virtual void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
virtual bool SupportsSubLibraries() const
virtual int GetModifyHash() const =0
Return the modification hash from the library cache.
virtual void GetAvailableSymbolFields(std::vector< wxString > &aNames)
Retrieves a list of (custom) field names that are present on symbols in this library.
virtual LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aPartName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
virtual wxString GetSubLibraryDescription(const wxString &aName)
Gets a description of a sublibrary.
virtual void GetSubLibraryNames(std::vector< wxString > &aNames)
Retrieves a list of sub-libraries in this library.
bool SupportsConfigurationDialog(const wxString &aNickname) const override
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
void DeleteSymbol(const wxString &aNickname, const wxString &aSymbolName)
Deletes the aSymbolName from the library given by aNickname.
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
std::vector< wxString > GetSymbolNames(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
bool SupportsSubLibraries(const wxString &aNickname) const
bool IsWritable(const wxString &aNickname) const override
Return true if the given nickname exists and is not a read-only library.
std::optional< LIB_STATUS > LoadOne(const wxString &aNickname)
Loads or reloads the given library, if it exists.
SAVE_T SaveSymbol(const wxString &aNickname, const LIB_SYMBOL *aSymbol, bool aOverwrite=true)
Write aSymbol to an existing library given by aNickname.
LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row) override
Creates a concrete plugin for the given row.
std::vector< SUB_LIBRARY > GetSubLibraries(const wxString &aNickname) const
static SCH_IO * plugin(const LIB_DATA *aRow)
Helper to cast the ABC plugin in the LIB_DATA* to a concrete plugin.
SYMBOL_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
static wxString GlobalPathEnvVariableName()
int GetModifyHash() const
static const char * PropPowerSymsOnly
std::vector< std::pair< wxString, LIB_STATUS > > GetLibraryStatuses() const
Returns a list of all library nicknames and their status (even if they failed to load)
LIBRARY_TABLE_TYPE Type() const override
The type of library table this adapter works with.
static std::map< wxString, LIB_DATA > GlobalLibraries
std::vector< LIB_SYMBOL * > GetSymbols(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
void AsyncLoad() override
Loads all available libraries for this adapter type in the background.
std::vector< wxString > GetAvailableExtraFields(const wxString &aNickname)
Returns a list of additional (non-mandatory) symbol fields present in the given library.
void ShowConfigurationDialog(const wxString &aNickname, wxWindow *aParent) const override
SAVE_T
The set of return values from SaveSymbol() below.
static const char * PropNonPowerSymsOnly
std::optional< LIBRARY_ERROR > LibraryError(const wxString &aNickname) const
std::optional< LIB_STATUS > GetLibraryStatus(const wxString &aNickname) const
Returns the status of a loaded library, or nullopt if the library hasn't been loaded (yet)
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
static std::mutex GlobalLibraryMutex
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.
Storage for an actual loaded library (including library content owned by the plugin)
std::vector< wxString > available_fields_cache
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
A descriptor for a sub-library (supported by database and http libraries)
wxString result
Test unit parsing edge cases and error handling.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::thread_pool< 0 > thread_pool
wxLogTrace helper definitions.