44bool validateSymbolPayload(
const std::vector<uint8_t>& aPayload,
const wxString& aExpectedSymbolName,
47 if( aExpectedSymbolName.IsEmpty() )
50 if( aPayload.empty() )
52 aError =
_(
"Downloaded symbol payload was empty." );
56 std::string_view payload(
reinterpret_cast<const char*
>( aPayload.data() ), aPayload.size() );
58 if( payload.find(
"(kicad_symbol_lib" ) == std::string_view::npos )
60 aError =
_(
"Downloaded symbol payload was not a KiCad symbol library." );
64 const std::string symbolToken = wxString::Format( wxS(
"(symbol \"%s\"" ), aExpectedSymbolName ).ToStdString();
66 if( payload.find( symbolToken ) == std::string_view::npos )
68 aError = wxString::Format(
_(
"Downloaded symbol payload did not include expected symbol '%s'." ),
69 aExpectedSymbolName );
94 bool aPlaceSymbol, wxString& aError )
102 aError =
_(
"Unable to load schematic settings." );
113 const bool strictLibraryTables =
m_frame !=
nullptr;
116 bool importedSymbol =
false;
117 wxString placedNickname;
118 wxString placedSymbolName;
124 std::vector<size_t> footprintIdx, otherIdx, symbolIdx;
126 for(
size_t i = 0; i < aManifest.
assets.size(); ++i )
128 const wxString& type = aManifest.
assets[i].asset_type;
130 if( type == wxS(
"footprint" ) )
131 footprintIdx.push_back( i );
132 else if( type == wxS(
"symbol" ) )
133 symbolIdx.push_back( i );
135 otherIdx.push_back( i );
138 std::vector<LIB_ID> footprintLinks;
142 if( !
downloader().DownloadAndVerify( aProvider, aManifest.
assets[i], remainingBudget, fetched, aError ) )
145 remainingBudget -= aManifest.
assets[i].size_bytes;
150 for(
size_t i : footprintIdx )
155 if( !downloadAsset( i, fetched ) )
158 wxFileName fpRoot = baseDir;
159 fpRoot.AppendDir( wxS(
"footprints" ) );
167 wxFileName libDir = fpRoot;
168 libDir.AppendDir( nickname + wxS(
".pretty" ) );
172 wxFileName outFile( libDir );
173 outFile.SetFullName( fileName );
178 if( strictLibraryTables )
190 footprintLinks.push_back( fpLibId );
194 for(
size_t i : otherIdx )
199 if( !downloadAsset( i, fetched ) )
204 wxFileName modelDir = baseDir;
205 modelDir.AppendDir( prefix + wxS(
"_3d" ) );
209 prefix + wxS(
"_model" ) );
211 wxFileName outFile( modelDir );
212 outFile.SetFullName( fileName );
219 wxFileName spiceDir = baseDir;
220 spiceDir.AppendDir( prefix + wxS(
"_spice" ) );
224 prefix + wxS(
"_model.cir" ) );
226 if( !fileName.Lower().EndsWith( wxS(
".cir" ) ) )
227 fileName += wxS(
".cir" );
229 wxFileName outFile( spiceDir );
230 outFile.SetFullName( fileName );
238 for(
size_t i : symbolIdx )
243 if( !downloadAsset( i, fetched ) )
246 wxFileName symbolDir = baseDir;
247 symbolDir.AppendDir( wxS(
"symbols" ) );
252 wxS(
"symbols" ),
true );
254 const wxString nickname = prefix + wxS(
"_" ) + libraryName;
256 wxFileName outFile( symbolDir );
257 outFile.SetFullName( nickname + wxS(
".kicad_sym" ) );
259 if( strictLibraryTables && symbolName.IsEmpty() )
261 aError =
_(
"No symbol asset specified." );
265 if( !validateSymbolPayload( fetched.
payload, symbolName, aError ) )
274 loaded->SetName( symbolName );
278 loaded->SetLibId( savedId );
283 symbolDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
285 if( strictLibraryTables )
294 aError =
_(
"Unable to access the symbol library manager." );
311 if( !footprintLinks.empty() )
319 aError =
_(
"Unable to access the KiCad symbol plugin." );
323 plugin->SaveSymbol( outFile.GetFullPath(), std::move( loaded ) );
327 aError = wxString::Format(
_(
"Unable to save the downloaded symbol: %s" ), e.
What() );
333 importedSymbol = loaded !=
nullptr;
334 placedNickname = nickname;
335 placedSymbolName = symbolName;
340 if( !importedSymbol )
342 aError =
_(
"No symbol asset was available to place." );
REMOTE_PROVIDER_SETTINGS m_RemoteSymbol
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()
void ReloadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
A logical library item identifier and consists of various portions much like a URI.
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
const wxString GetUniStringLibNickname() const
virtual LIBRARY_MANAGER & GetLibraryManager() const
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
const REMOTE_SYMBOL_DOWNLOAD_MANAGER & downloader() const
std::unique_ptr< REMOTE_SYMBOL_DOWNLOAD_MANAGER > m_ownedDownloader
REMOTE_SYMBOL_DOWNLOAD_MANAGER * m_downloader
REMOTE_SYMBOL_IMPORT_JOB(SCH_EDIT_FRAME *aFrame, REMOTE_SYMBOL_DOWNLOAD_MANAGER *aDownloader=nullptr)
bool Import(const REMOTE_PROVIDER_METADATA &aProvider, const REMOTE_SYMBOL_IMPORT_CONTEXT &aContext, const REMOTE_PROVIDER_PART_MANIFEST &aManifest, bool aPlaceSymbol, wxString &aError)
Schematic editor (Eeschema) main window.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
PGM_BASE & Pgm()
The global program "get" accessor.
void ApplyFootprintLinks(LIB_SYMBOL &aSymbol, const std::vector< LIB_ID > &aLinks)
Apply a list of footprint LIB_IDs to a symbol about to be saved.
std::unique_ptr< LIB_SYMBOL > LoadRemoteSymbolFromPayload(const std::vector< uint8_t > &aPayload, const wxString &aLibItemName, wxString &aError)
Deserialize a remote-downloaded symbol payload into a LIB_SYMBOL.
LIB_ID BuildRemoteLibId(const wxString &aResolvedLibrary, const wxString &aResolvedItemName)
Build the local LIB_ID for a remote-downloaded library item.
bool PlaceRemoteDownloadedSymbol(SCH_EDIT_FRAME *aFrame, const wxString &aNickname, const wxString &aLibItemName, wxString &aError)
Place a symbol from a remote download into the schematic editor.
bool EnsureRemoteLibraryEntry(LIBRARY_TABLE_TYPE aTableType, const wxFileName &aLibraryPath, const wxString &aNickname, bool aGlobalTable, bool aStrict, wxString &aError)
Add or update a library table entry for a remote download library.
bool WriteRemoteBinaryFile(const wxFileName &aOutput, const std::vector< uint8_t > &aPayload, wxString &aError)
Write binary data to a file, creating parent directories as needed.
bool SaveRemoteSymbolToLibrary(SYMBOL_LIBRARY_ADAPTER &aAdapter, const wxFileName &aLibraryFile, const wxString &aNickname, bool aGlobalTable, std::unique_ptr< LIB_SYMBOL > aSymbol, wxString &aError)
Save a downloaded symbol into the remote symbol library aNickname backed by aLibraryFile.
bool EnsureRemoteDestinationRoot(wxFileName &aOutDir, wxString &aError)
Resolve and create the configured destination root directory for remote symbol downloads.
wxString RemoteLibraryPrefix()
Return the configured (or default) library prefix for remote downloads, sanitized for use as a filena...
wxString SanitizeRemoteFileComponent(const wxString &aValue, const wxString &aDefault, bool aLower)
Replace non-alphanumeric characters (other than _ - .) with underscores.
T * GetAppSettings(const char *aFilename)
std::vector< REMOTE_PROVIDER_PART_ASSET > assets
std::vector< uint8_t > payload