21#include <magic_enum.hpp>
41using namespace std::chrono_literals;
74 wxCHECK( aRow->
plugin && ret,
nullptr );
81 wxArrayString dummyList;
100 wxArrayString dummyList;
158 if( type == SCH_IO_MGR::SCH_NESTED_TABLE )
163 if( fileName.FileExists() )
166 msg = wxString::Format(
_(
"Nested table '%s' not found." ), row->
URI() );
170 else if( type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
173 wxString msg = wxString::Format(
_(
"Unknown library type %s " ), row->
Type() );
180 plugin->SetLibraryManagerAdapter(
this );
183 magic_enum::enum_name( row->
Scope() ) );
191 std::vector<LIB_SYMBOL*> symbols;
193 std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname );
200 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
218 LIB_ID id = symbol->GetLibId();
220 symbol->SetLibId(
id );
229 wxArrayString namesAS;
230 std::vector<wxString> names;
232 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
236 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
249 wxLogTrace(
traceLibraries,
"Sym: Exception enumerating library %s: %s",
254 for(
const wxString&
name : namesAS )
255 names.emplace_back(
name );
263 if( std::optional<const LIB_DATA*> lib =
fetchIfLoaded( aNickname ) )
265 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
267 std::map<std::string, UTF8> options = ( *lib )->row->GetOptionsMap();
271 LIB_ID id = symbol->GetLibId();
273 symbol->SetLibId(
id );
279 wxLogTrace(
traceLibraries,
"LoadSymbol: requested library %s not loaded", aNickname );
287 std::unique_ptr<LIB_SYMBOL> aSymbol,
bool aOverwrite )
293 if( !libResult.has_value() )
295 wxLogTrace(
traceLibraries,
"SaveSymbol: unable to load library %s: %s", aNickname,
296 libResult.error().message );
304 wxLogTrace(
traceLibraries,
"SaveSymbol: library %s not found", aNickname );
311 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
313 const wxString symbolName = aSymbol->GetName();
328 wxLogTrace(
traceLibraries,
"SaveSymbol: error checking for existing symbol %s:%s: %s", aNickname,
329 symbolName, e.
What() );
336 plugin->SaveSymbol(
getUri( lib->
row ), std::move( aSymbol ), &options );
340 wxLogTrace(
traceLibraries,
"SaveSymbol: error saving %s:%s: %s", aNickname, symbolName, e.
What() );
352 if( !libResult.has_value() || !*libResult )
354 wxLogTrace(
traceLibraries,
"DeleteSymbol: unable to load library %s", aNickname );
371 if( std::optional<const LIB_DATA*> lib =
fetchIfLoaded( aLib ) )
372 return ( *lib )->plugin->IsLibraryWritable(
getUri( ( *lib )->row ) );
380 std::vector<wxString> fields;
415 std::vector<SUB_LIBRARY> ret;
423 std::vector<wxString> names;
426 for(
const wxString&
name : names )
435 wxLogTrace(
traceLibraries,
"Sym: Exception getting sub-libraries for %s: %s",
436 aNickname, e.
What() );
447 static std::map<SCH_IO_MGR::SCH_FILE_T, bool> s_supported;
450 auto it = s_supported.find( type );
452 if( it == s_supported.end() )
455 it = s_supported.emplace( type,
plugin &&
plugin->SupportsConfigurationDialog() ).first;
470 plugin->SetLibraryManagerAdapter(
this );
474 std::unique_ptr<DIALOG_SHIM> dialog(
plugin->CreateConfigurationDialog( aParent,
path ) );
479 int ret = dialog->ShowModal();
486 if( committed && ( *committed )->URI() == aRow.
URI() && ( *committed )->Type() == aRow.
Type() )
503 wxCHECK2( rowData->
row,
continue );
517 wxCHECK( rowData->
row, std::nullopt );
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()
virtual const wxString Problem() const
what was the problem?
virtual const wxString Where() const
where did the Problem() occur?
A wrapper for static data that should not be destroyed at program exit.
void resetProjectCache()
Aborts pending loads and resets every project-scope cache entry in place (plugin and row cleared,...
void ReloadLibraryEntry(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
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.
wxString getUri(const LIBRARY_TABLE_ROW *aRow) const
LIBRARY_RESULT< LIB_DATA * > loadIfNeeded(const wxString &aNickname)
Fetches a loaded library, triggering a load of that library if it isn't loaded yet.
static std::recursive_mutex & pluginMutex(const wxString &aNickname)
Serializes access to a library's shared plugin instance so its single mutable cache is not raced by c...
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 & URI() 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 file type from the corresponding plugin type name: "kicad", "legacy", etc.
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 void DeleteSymbol(const wxString &aLibraryPath, const wxString &aSymbolName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete the entire LIB_SYMBOL associated with aAliasName from the library aLibraryPath.
virtual void CheckLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Validate that the library at aLibraryPath is reachable and well-formed, without necessarily loading s...
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.
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
static SCH_IO * schplugin(const LIB_DATA *aRow)
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.
std::optional< LIB_STATUS > CheckLibrary(LIB_DATA *aLib) override
Validates that a library is loadable. May not necessarily load the library!
SAVE_T SaveSymbol(const wxString &aNickname, std::unique_ptr< LIB_SYMBOL > aSymbol, bool aOverwrite=true)
Write aSymbol to an existing library given by aNickname.
void enumerateLibrary(LIB_DATA *aLib, const wxString &aUri) override
Override in derived class to perform library-specific enumeration.
std::vector< wxString > GetSymbolNames(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
bool SupportsSubLibraries(const wxString &aNickname) const
LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row) override
Creates a concrete plugin for the given row.
int ShowConfigurationDialog(const LIBRARY_TABLE_ROW &aRow, LIBRARY_TABLE_SCOPE aScope, wxWindow *aParent) override
Shows the plugin's configuration dialog for a library table row.
std::vector< SUB_LIBRARY > GetSubLibraries(const wxString &aNickname) const
SYMBOL_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
static wxString GlobalPathEnvVariableName()
int GetModifyHash() const
static const char * PropPowerSymsOnly
LIBRARY_TABLE_TYPE Type() const override
The type of library table this adapter works with.
IO_BASE * plugin(const LIB_DATA *aRow) override
std::vector< LIB_SYMBOL * > GetSymbols(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
static LEAK_AT_EXIT< std::shared_mutex > GlobalLibraryMutex
static LEAK_AT_EXIT< std::map< wxString, LIB_DATA > > GlobalLibraries
bool SupportsConfigurationDialog(const LIBRARY_TABLE_ROW &aRow) const override
Configuration works from the row itself, so libraries that failed to load or are not yet committed to...
std::vector< wxString > GetAvailableExtraFields(const wxString &aNickname)
Returns a list of additional (non-mandatory) symbol fields present in the given library.
SAVE_T
The set of return values from SaveSymbol() below.
static const char * PropNonPowerSymsOnly
bool IsSymbolLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
std::optional< int > GetLibraryModifyHash(const wxString &aNickname) const
Return the modify hash of a single library if it is currently loaded.
~SYMBOL_LIBRARY_ADAPTER() override
Functions related to environment variables, including help functions.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
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
Never destroyed while holding the map's mutex; a plugin's worker thread may be blocked on it.
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.
wxLogTrace helper definitions.