KiCad PCB EDA Suite
Loading...
Searching...
No Matches
remote_symbol_import_job.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 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
23
24#include <eeschema_settings.h>
25#include <io/io_mgr.h>
26#include <lib_id.h>
27#include <lib_symbol.h>
30#include <pgm_base.h>
31#include <project_sch.h>
33#include <sch_edit_frame.h>
34#include <sch_io/sch_io.h>
35#include <sch_io/sch_io_mgr.h>
37#include <string_view>
38
39#include <wx/intl.h>
40
41
42namespace
43{
44bool validateSymbolPayload( const std::vector<uint8_t>& aPayload, const wxString& aExpectedSymbolName,
45 wxString& aError )
46{
47 if( aExpectedSymbolName.IsEmpty() )
48 return true;
49
50 if( aPayload.empty() )
51 {
52 aError = _( "Downloaded symbol payload was empty." );
53 return false;
54 }
55
56 std::string_view payload( reinterpret_cast<const char*>( aPayload.data() ), aPayload.size() );
57
58 if( payload.find( "(kicad_symbol_lib" ) == std::string_view::npos )
59 {
60 aError = _( "Downloaded symbol payload was not a KiCad symbol library." );
61 return false;
62 }
63
64 const std::string symbolToken = wxString::Format( wxS( "(symbol \"%s\"" ), aExpectedSymbolName ).ToStdString();
65
66 if( payload.find( symbolToken ) == std::string_view::npos )
67 {
68 aError = wxString::Format( _( "Downloaded symbol payload did not include expected symbol '%s'." ),
69 aExpectedSymbolName );
70 return false;
71 }
72
73 return true;
74}
75} // namespace
76
77
79 REMOTE_SYMBOL_DOWNLOAD_MANAGER* aDownloader ) :
80 m_frame( aFrame ),
81 m_downloader( aDownloader )
82{
83 if( !m_downloader )
84 {
85 m_ownedDownloader = std::make_unique<REMOTE_SYMBOL_DOWNLOAD_MANAGER>();
87 }
88}
89
90
92 const REMOTE_SYMBOL_IMPORT_CONTEXT& aContext,
93 const REMOTE_PROVIDER_PART_MANIFEST& aManifest,
94 bool doPlaceSymbol, wxString& aError )
95{
96 aError.clear();
97
99
100 if( !settings )
101 {
102 aError = _( "Unable to load schematic settings." );
103 return false;
104 }
105
106 wxFileName baseDir;
107
108 if( !EnsureRemoteDestinationRoot( baseDir, aError ) )
109 return false;
110
111 long long remainingBudget = aProvider.max_download_bytes;
112 const bool addToGlobal = settings->m_RemoteSymbol.add_to_global_table;
113 const bool strictLibraryTables = m_frame != nullptr;
114 const wxString prefix = RemoteLibraryPrefix();
116 bool didImportSymbol = false;
117 wxString toPlaceNickname;
118 wxString toPlaceSymbolName;
119
120 // Sort asset indexes: footprints first, then 3D/SPICE, then symbols. The symbol's
121 // Footprint field references a LIB_ID that must already exist on disk and (when
122 // running interactive) be registered in the footprint library table by the time the
123 // symbol file is written, otherwise CvPcb / placement-time resolution misses it.
124 std::vector<size_t> footprintIndexes;
125 std::vector<size_t> otherIndexes;
126 std::vector<size_t> symbolIndexes;
127
128 for( size_t i = 0; i < aManifest.assets.size(); ++i )
129 {
130 const wxString& type = aManifest.assets[i].asset_type;
131
132 if( type == wxS( "footprint" ) )
133 footprintIndexes.push_back( i );
134 else if( type == wxS( "symbol" ) )
135 symbolIndexes.push_back( i );
136 else
137 otherIndexes.push_back( i );
138 }
139
140 std::vector<LIB_ID> footprintLinks;
141
142 auto downloadAsset =
143 [&]( size_t i, REMOTE_SYMBOL_FETCHED_ASSET& fetched ) -> bool
144 {
145 if( !downloader().DownloadAndVerify( aProvider, aManifest.assets[i], remainingBudget, fetched,
146 aError ) )
147 {
148 return false;
149 }
150
151 remainingBudget -= aManifest.assets[i].size_bytes;
152 return true;
153 };
154
155 // --- Footprints ---
156 for( size_t footprintIdx : footprintIndexes )
157 {
158 const REMOTE_PROVIDER_PART_ASSET& asset = aManifest.assets[footprintIdx];
160
161 if( !downloadAsset( footprintIdx, fetched ) )
162 return false;
163
164 wxFileName fpRoot = baseDir;
165 fpRoot.AppendDir( wxS( "footprints" ) );
166
167 // Resolve logical names with the same fallbacks the original code used.
168 const wxString resolvedLib = asset.target_library.IsEmpty() ? asset.name : asset.target_library;
169 const wxString resolvedName = asset.target_name.IsEmpty() ? asset.name : asset.target_name;
170 const LIB_ID fpLibId = BuildRemoteLibId( resolvedLib, resolvedName );
171 const wxString nickname = fpLibId.GetUniStringLibNickname();
172
173 wxFileName libDir = fpRoot;
174 libDir.AppendDir( nickname + wxS( ".pretty" ) );
175
176 wxString fileName = fpLibId.GetUniStringLibItemName() + wxS( ".kicad_mod" );
177
178 wxFileName outFile( libDir );
179 outFile.SetFullName( fileName );
180
181 if( !WriteRemoteBinaryFile( outFile, fetched.payload, aError ) )
182 return false;
183
184 if( strictLibraryTables )
185 {
186 if( !EnsureRemoteLibraryEntry( LIBRARY_TABLE_TYPE::FOOTPRINT, libDir, nickname, addToGlobal, true,
187 aError ) )
188 {
189 return false;
190 }
191
193 libMgr.ReloadLibraryEntry( LIBRARY_TABLE_TYPE::FOOTPRINT, nickname, scope );
194 }
195
196 footprintLinks.push_back( fpLibId );
197 }
198
199 // --- 3D models, SPICE ---
200 for( size_t otherIdx : otherIndexes )
201 {
202 const REMOTE_PROVIDER_PART_ASSET& asset = aManifest.assets[otherIdx];
204
205 if( !downloadAsset( otherIdx, fetched ) )
206 return false;
207
208 if( asset.asset_type == wxS( "3dmodel" ) )
209 {
210 wxFileName modelDir = baseDir;
211 modelDir.AppendDir( prefix + wxS( "_3d" ) );
212
213 wxString fileName = SanitizeRemoteFileComponent( asset.target_name.IsEmpty() ? asset.name
214 : asset.target_name,
215 prefix + wxS( "_model" ) );
216
217 wxFileName outFile( modelDir );
218 outFile.SetFullName( fileName );
219
220 if( !WriteRemoteBinaryFile( outFile, fetched.payload, aError ) )
221 return false;
222 }
223 else if( asset.asset_type == wxS( "spice" ) )
224 {
225 wxFileName spiceDir = baseDir;
226 spiceDir.AppendDir( prefix + wxS( "_spice" ) );
227
228 wxString fileName = SanitizeRemoteFileComponent( asset.target_name.IsEmpty() ? asset.name
229 : asset.target_name,
230 prefix + wxS( "_model.cir" ) );
231
232 if( !fileName.Lower().EndsWith( wxS( ".cir" ) ) )
233 fileName += wxS( ".cir" );
234
235 wxFileName outFile( spiceDir );
236 outFile.SetFullName( fileName );
237
238 if( !WriteRemoteBinaryFile( outFile, fetched.payload, aError ) )
239 return false;
240 }
241 }
242
243 // --- Symbols ---
244 for( size_t symbolIdx : symbolIndexes )
245 {
246 const REMOTE_PROVIDER_PART_ASSET& asset = aManifest.assets[symbolIdx];
248
249 if( !downloadAsset( symbolIdx, fetched ) )
250 return false;
251
252 wxFileName symbolDir = baseDir;
253 symbolDir.AppendDir( wxS( "symbols" ) );
254
255 const wxString libraryName = SanitizeRemoteFileComponent( asset.target_library.IsEmpty()
256 ? aContext.library_name
257 : asset.target_library,
258 wxS( "symbols" ), true );
259 const wxString symbolName = asset.target_name.IsEmpty() ? aContext.symbol_name : asset.target_name;
260 const wxString nickname = prefix + wxS( "_" ) + libraryName;
261
262 wxFileName outFile( symbolDir );
263 outFile.SetFullName( nickname + wxS( ".kicad_sym" ) );
264
265 if( strictLibraryTables && symbolName.IsEmpty() )
266 {
267 aError = _( "No symbol asset specified." );
268 return false;
269 }
270
271 if( !validateSymbolPayload( fetched.payload, symbolName, aError ) )
272 return false;
273
274 // Deserialize → mutate → save so the persisted symbol's Footprint field is
275 // already pointing at the local LIB_IDs of the bundle's footprints.
276 std::unique_ptr<LIB_SYMBOL> loadedSymbol = LoadRemoteSymbolFromPayload( fetched.payload, symbolName, aError );
277
278 if( loadedSymbol )
279 {
280 didImportSymbol = true;
281
282 loadedSymbol->SetName( symbolName );
283 LIB_ID savedId;
284 savedId.SetLibNickname( nickname );
285 savedId.SetLibItemName( symbolName );
286 loadedSymbol->SetLibId( savedId );
287
288 ApplyFootprintLinks( *loadedSymbol, footprintLinks );
289 }
290
291 if( !symbolDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
292 {
293 aError = wxString::Format( _( "Unable to create directory '%s'." ), symbolDir.GetPath() );
294 return false;
295 }
296
297 if( strictLibraryTables )
298 {
299 if( !loadedSymbol )
300 return false;
301
303
304 if( !adapter )
305 {
306 aError = _( "Unable to access the symbol library manager." );
307 return false;
308 }
309
310 if( !SaveRemoteSymbolToLibrary( *adapter, outFile, nickname, addToGlobal, std::move( loadedSymbol ),
311 aError ) )
312 {
313 return false;
314 }
315 }
316 else
317 {
318 // Headless: write the raw payload to disk so the SCH plugin's library cache has an existing
319 // file to load, then re-save through the plugin to replace the symbol entry with the mutated
320 // (link-applied) copy.
321 // While this step isn't strictly necessary when there are no links to apply, it's the only
322 // thing that validates the data, so we perform it regardless.
323 if( !WriteRemoteBinaryFile( outFile, fetched.payload, aError ) )
324 return false;
325
326 if( loadedSymbol )
327 {
328 try
329 {
330 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
331
332 if( !plugin )
333 {
334 aError = _( "Unable to access the KiCad symbol plugin." );
335 return false;
336 }
337
338 plugin->SaveSymbol( outFile.GetFullPath(), std::move( loadedSymbol ) );
339 }
340 catch( const IO_ERROR& e )
341 {
342 aError = wxString::Format( _( "Unable to save the downloaded symbol: %s" ), e.What() );
343 return false;
344 }
345 }
346 }
347
348 // TODO: the payload is expected to contain a single symbol. But if it contains more, should we
349 // be placing all of them? (And if so, as a block or singly?)
350 toPlaceNickname = nickname;
351 toPlaceSymbolName = symbolName;
352 }
353
354 if( doPlaceSymbol )
355 {
356 if( !didImportSymbol )
357 {
358 aError = _( "No symbol asset was available to place." );
359 return false;
360 }
361
362 if( !PlaceRemoteDownloadedSymbol( m_frame, toPlaceNickname, toPlaceSymbolName, aError ) )
363 return false;
364 }
365
366 return true;
367}
368
369
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.
Definition lib_id.h:45
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:124
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:113
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const wxString GetUniStringLibNickname() const
Definition lib_id.h:84
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:126
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 doPlaceSymbol, wxString &aError)
Schematic editor (Eeschema) main window.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
#define _(s)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
LIBRARY_TABLE_SCOPE
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
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