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 =
65 wxString::Format( wxS(
"(symbol \"%s\"" ), aExpectedSymbolName ).ToStdString();
67 if( payload.find( symbolToken ) == std::string_view::npos )
69 aError = wxString::Format(
70 _(
"Downloaded symbol payload did not include expected symbol '%s'." ),
71 aExpectedSymbolName );
96 bool aPlaceSymbol, wxString& aError )
104 aError =
_(
"Unable to load schematic settings." );
115 const bool strictLibraryTables =
m_frame !=
nullptr;
119 bool importedSymbol =
false;
120 wxString placedNickname;
121 wxString placedSymbolName;
127 std::vector<size_t> footprintIdx, otherIdx, symbolIdx;
129 for(
size_t i = 0; i < aManifest.
assets.size(); ++i )
131 const wxString& type = aManifest.
assets[i].asset_type;
133 if( type == wxS(
"footprint" ) )
134 footprintIdx.push_back( i );
135 else if( type == wxS(
"symbol" ) )
136 symbolIdx.push_back( i );
138 otherIdx.push_back( i );
141 std::vector<LIB_ID> footprintLinks;
145 if( !
downloader().DownloadAndVerify( aProvider, aManifest.
assets[i], remainingBudget,
149 remainingBudget -= aManifest.
assets[i].size_bytes;
154 for(
size_t i : footprintIdx )
159 if( !downloadAsset( i, fetched ) )
162 wxFileName fpRoot = baseDir;
163 fpRoot.AppendDir( wxS(
"footprints" ) );
166 const wxString resolvedLib =
168 const wxString resolvedName =
174 wxFileName libDir = fpRoot;
175 libDir.AppendDir( nickname + wxS(
".pretty" ) );
179 if( !fileName.Lower().EndsWith( wxS(
".kicad_mod" ) ) )
180 fileName += wxS(
".kicad_mod" );
182 wxFileName outFile( libDir );
183 outFile.SetFullName( fileName );
188 if( strictLibraryTables )
191 addToGlobal,
true, aError ) )
199 footprintLinks.push_back( fpLibId );
203 for(
size_t i : otherIdx )
208 if( !downloadAsset( i, fetched ) )
213 wxFileName modelDir = baseDir;
214 modelDir.AppendDir( prefix + wxS(
"_3d" ) );
218 prefix + wxS(
"_model" ) );
220 wxFileName outFile( modelDir );
221 outFile.SetFullName( fileName );
228 wxFileName spiceDir = baseDir;
229 spiceDir.AppendDir( prefix + wxS(
"_spice" ) );
233 prefix + wxS(
"_model.cir" ) );
235 if( !fileName.Lower().EndsWith( wxS(
".cir" ) ) )
236 fileName += wxS(
".cir" );
238 wxFileName outFile( spiceDir );
239 outFile.SetFullName( fileName );
247 for(
size_t i : symbolIdx )
252 if( !downloadAsset( i, fetched ) )
255 wxFileName symbolDir = baseDir;
256 symbolDir.AppendDir( wxS(
"symbols" ) );
260 wxS(
"symbols" ),
true );
261 const wxString symbolName =
263 const wxString nickname = prefix + wxS(
"_" ) + libraryName;
265 wxFileName outFile( symbolDir );
266 outFile.SetFullName( nickname + wxS(
".kicad_sym" ) );
268 if( !validateSymbolPayload( fetched.
payload, symbolName, aError ) )
273 std::unique_ptr<LIB_SYMBOL> loaded =
279 loaded->SetName( symbolName );
283 loaded->SetLibId( savedId );
287 symbolDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
289 if( strictLibraryTables )
292 addToGlobal,
true, aError ) )
298 || adapter->
SaveSymbol( nickname, loaded.get(),
true )
301 aError =
_(
"Unable to save the downloaded symbol." );
305 (void) loaded.release();
321 if( !footprintLinks.empty() )
329 aError =
_(
"Unable to access the KiCad symbol plugin." );
333 plugin->SaveSymbol( outFile.GetFullPath(), loaded.get() );
334 (void) loaded.release();
338 aError = wxString::Format(
_(
"Unable to save the downloaded symbol: %s" ),
345 importedSymbol =
true;
346 placedNickname = nickname;
347 placedSymbolName = symbolName;
352 if( !importedSymbol )
354 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)
std::optional< LIB_STATUS > LoadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname)
Synchronously loads the named library to LOADED state for the given type.
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...
SAVE_T SaveSymbol(const wxString &aNickname, const LIB_SYMBOL *aSymbol, bool aOverwrite=true)
Write aSymbol to an existing library given by aNickname.
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 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