KiCad PCB EDA Suite
Loading...
Searching...
No Matches
remote_symbol_import_utils.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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <common.h>
23#include <eeschema_settings.h>
24#include <io/io_mgr.h>
25#include <lib_symbol.h>
27#include <pgm_base.h>
29#include <sch_edit_frame.h>
30#include <sch_io/sch_io.h>
31#include <sch_io/sch_io_mgr.h>
32#include <sch_symbol.h>
34#include <tool/tool_manager.h>
35#include <tools/sch_actions.h>
36
37#include <wx/ffile.h>
38#include <wx/filefn.h>
39#include <wx/intl.h>
40
41
42wxString SanitizeRemoteFileComponent( const wxString& aValue, const wxString& aDefault, bool aLower )
43{
44 wxString result = aValue;
45 result.Trim( true ).Trim( false );
46
47 if( result.IsEmpty() )
48 result = aDefault;
49
50 for( size_t i = 0; i < result.length(); ++i )
51 {
52 const wxUniChar ch = result[i];
53
54 if( !( wxIsalnum( ch ) || ch == '_' || ch == '-' || ch == '.' ) )
55 result[i] = '_';
56 }
57
58 return aLower ? result.Lower() : result;
59}
60
61
63{
65 wxString prefix = settings ? settings->m_RemoteSymbol.library_prefix : wxString();
66
67 if( prefix.IsEmpty() )
69
70 return SanitizeRemoteFileComponent( prefix, wxS( "remote" ), true );
71}
72
73
74bool WriteRemoteBinaryFile( const wxFileName& aOutput, const std::vector<uint8_t>& aPayload,
75 wxString& aError )
76{
77 if( aPayload.empty() )
78 {
79 aError = _( "Payload was empty." );
80 return false;
81 }
82
83 wxFileName targetDir = aOutput;
84 targetDir.SetFullName( wxEmptyString );
85
86 if( !targetDir.DirExists() && !targetDir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
87 {
88 aError = wxString::Format( _( "Unable to create '%s'." ), targetDir.GetFullPath() );
89 return false;
90 }
91
92 wxFFile file( aOutput.GetFullPath(), wxS( "wb" ) );
93
94 if( !file.IsOpened() )
95 {
96 aError = wxString::Format( _( "Unable to open '%s' for writing." ), aOutput.GetFullPath() );
97 return false;
98 }
99
100 if( file.Write( aPayload.data(), aPayload.size() ) != aPayload.size() )
101 {
102 aError = wxString::Format( _( "Failed to write '%s'." ), aOutput.GetFullPath() );
103 return false;
104 }
105
106 file.Close();
107 return true;
108}
109
110
111bool EnsureRemoteDestinationRoot( wxFileName& aOutDir, wxString& aError )
112{
114
115 if( !settings )
116 {
117 aError = _( "Unable to load schematic settings." );
118 return false;
119 }
120
121 wxString destination = settings->m_RemoteSymbol.destination_dir;
122
123 if( destination.IsEmpty() )
125
126 destination = ExpandEnvVarSubstitutions( destination, &Pgm().GetSettingsManager().Prj() );
127 destination.Trim( true ).Trim( false );
128
129 if( destination.IsEmpty() )
130 {
131 aError = _( "Destination directory is not configured." );
132 return false;
133 }
134
135 wxFileName dir = wxFileName::DirName( destination );
136 dir.Normalize( FN_NORMALIZE_FLAGS );
137
138 if( !dir.DirExists() && !dir.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
139 {
140 aError = wxString::Format( _( "Unable to create directory '%s'." ), dir.GetFullPath() );
141 return false;
142 }
143
144 aOutDir = dir;
145 return true;
146}
147
148
149bool EnsureRemoteLibraryEntry( LIBRARY_TABLE_TYPE aTableType, const wxFileName& aLibraryPath,
150 const wxString& aNickname, bool aGlobalTable, bool aStrict,
151 wxString& aError )
152{
154 std::optional<LIBRARY_TABLE*> tableOpt = manager.Table(
155 aTableType,
157
158 if( !tableOpt )
159 {
160 if( aStrict )
161 aError = _( "Unable to access the library table." );
162
163 return !aStrict;
164 }
165
166 LIBRARY_TABLE* table = *tableOpt;
167 const wxString fullPath = aLibraryPath.GetFullPath();
168
169 if( table->HasRow( aNickname ) )
170 {
171 if( std::optional<LIBRARY_TABLE_ROW*> rowOpt = table->Row( aNickname ); rowOpt )
172 {
173 LIBRARY_TABLE_ROW* row = *rowOpt;
174
175 if( row->URI() != fullPath )
176 {
177 row->SetURI( fullPath );
178
179 if( !table->Save() )
180 {
181 aError = _( "Failed to update the library table." );
182 return false;
183 }
184 }
185 }
186
187 return true;
188 }
189
190 LIBRARY_TABLE_ROW& row = table->InsertRow();
191 row.SetNickname( aNickname );
192 row.SetURI( fullPath );
193 row.SetType( wxS( "KiCad" ) );
194 row.SetOptions( wxString() );
195 row.SetDescription( _( "Remote download" ) );
196 row.SetOk( true );
197
198 if( !table->Save() )
199 {
200 aError = _( "Failed to save the library table." );
201 return false;
202 }
203
204 return true;
205}
206
207
208bool PlaceRemoteDownloadedSymbol( SCH_EDIT_FRAME* aFrame, const wxString& aNickname,
209 const wxString& aLibItemName, wxString& aError )
210{
211 if( !aFrame )
212 {
213 aError = _( "No schematic editor is available for placement." );
214 return false;
215 }
216
217 LIB_ID libId;
218 libId.SetLibNickname( aNickname );
219 libId.SetLibItemName( aLibItemName );
220
221 LIB_SYMBOL* libSymbol = aFrame->GetLibSymbol( libId );
222
223 if( !libSymbol )
224 {
225 aError = _( "Unable to load the downloaded symbol for placement." );
226 return false;
227 }
228
229 SCH_SYMBOL* symbol = new SCH_SYMBOL( *libSymbol, libId, &aFrame->GetCurrentSheet(), 1 );
230 symbol->SetParent( aFrame->GetScreen() );
231
232 if( EESCHEMA_SETTINGS* cfg = aFrame->eeconfig(); cfg && cfg->m_AutoplaceFields.enable )
233 symbol->AutoplaceFields( nullptr, AUTOPLACE_AUTO );
234
235 TOOL_MANAGER* toolMgr = aFrame->GetToolManager();
236
237 if( !toolMgr )
238 {
239 delete symbol;
240 aError = _( "Unable to access the schematic placement tools." );
241 return false;
242 }
243
244 aFrame->Raise();
246 SCH_ACTIONS::PLACE_SYMBOL_PARAMS{ symbol, true } );
247 return true;
248}
249
250
251std::unique_ptr<LIB_SYMBOL> LoadRemoteSymbolFromPayload( const std::vector<uint8_t>& aPayload,
252 const wxString& aLibItemName,
253 wxString& aError )
254{
255 if( aPayload.empty() )
256 {
257 aError = _( "Symbol payload was empty." );
258 return nullptr;
259 }
260
261 wxString tempPath = wxFileName::CreateTempFileName( wxS( "remote_symbol" ) );
262
263 if( tempPath.IsEmpty() )
264 {
265 aError = _( "Unable to create a temporary file for the symbol payload." );
266 return nullptr;
267 }
268
269 wxFileName tempFile( tempPath );
270 wxFFile file( tempFile.GetFullPath(), wxS( "wb" ) );
271
272 if( !file.IsOpened() )
273 {
274 aError = _( "Unable to create a temporary file for the symbol payload." );
275 wxRemoveFile( tempFile.GetFullPath() );
276 return nullptr;
277 }
278
279 if( file.Write( aPayload.data(), aPayload.size() ) != aPayload.size() )
280 {
281 aError = _( "Failed to write the temporary symbol payload." );
282 file.Close();
283 wxRemoveFile( tempFile.GetFullPath() );
284 return nullptr;
285 }
286
287 file.Close();
288
289 IO_RELEASER<SCH_IO> plugin( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD ) );
290
291 if( !plugin )
292 {
293 aError = _( "Unable to access the KiCad symbol plugin." );
294 wxRemoveFile( tempFile.GetFullPath() );
295 return nullptr;
296 }
297
298 std::unique_ptr<LIB_SYMBOL> symbol;
299
300 try
301 {
302 LIB_SYMBOL* loaded = plugin->LoadSymbol( tempFile.GetFullPath(), aLibItemName );
303
304 if( loaded )
305 symbol = std::make_unique<LIB_SYMBOL>( *loaded );
306 else
307 aError = _( "Symbol payload did not include the expected symbol." );
308 }
309 catch( const IO_ERROR& e )
310 {
311 aError = wxString::Format( _( "Unable to decode the symbol payload: %s" ), e.What() );
312 }
313
314 wxRemoveFile( tempFile.GetFullPath() );
315 return symbol;
316}
317
318
319LIB_ID BuildRemoteLibId( const wxString& aResolvedLibrary, const wxString& aResolvedItemName )
320{
321 const wxString nickname =
322 RemoteLibraryPrefix() + wxS( "_" )
323 + SanitizeRemoteFileComponent( aResolvedLibrary, wxS( "footprints" ), true );
324
325 const wxString itemName = SanitizeRemoteFileComponent( aResolvedItemName, wxS( "footprint" ) );
326
327 LIB_ID id;
328 id.SetLibNickname( nickname );
329 id.SetLibItemName( itemName );
330 return id;
331}
332
333
334void ApplyFootprintLinks( LIB_SYMBOL& aSymbol, const std::vector<LIB_ID>& aLinks )
335{
336 if( aLinks.empty() )
337 return;
338
339 aSymbol.SetFootprintProp( aLinks.front().GetUniStringLibId() );
340
341 if( aLinks.size() == 1 )
342 return;
343
344 wxArrayString filters = aSymbol.GetFPFilters();
345
346 for( size_t i = 1; i < aLinks.size(); ++i )
347 filters.Add( aLinks[i].GetUniStringLibItemName() );
348
349 aSymbol.SetFPFilters( filters );
350}
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
REMOTE_PROVIDER_SETTINGS m_RemoteSymbol
AUTOPLACE_FIELDS m_AutoplaceFields
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()
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void SetOptions(const wxString &aOptions)
void SetNickname(const wxString &aNickname)
void SetOk(bool aOk=true)
void SetType(const wxString &aType)
void SetDescription(const wxString &aDescription)
void SetURI(const wxString &aUri)
const wxString & URI() const
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int SetLibItemName(const UTF8 &aLibItemName)
Override the library item name portion of the LIB_ID to aLibItemName.
Definition lib_id.cpp:111
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:100
Define a library symbol object.
Definition lib_symbol.h:83
wxArrayString GetFPFilters() const
Definition lib_symbol.h:211
void SetFootprintProp(const wxString &aFootprint)
Definition lib_symbol.h:393
void SetFPFilters(const wxArrayString &aFilters)
Definition lib_symbol.h:209
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:132
static TOOL_ACTION placeSymbol
Definition sch_actions.h:66
EESCHEMA_SETTINGS * eeconfig() const
LIB_SYMBOL * GetLibSymbol(const LIB_ID &aLibId, bool aUseCacheLib=false, bool aShowErrorMsg=false)
Load symbol from symbol library table.
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_SHEET_PATH & GetCurrentSheet() const
Schematic symbol object.
Definition sch_symbol.h:76
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
Automatically orient all the fields in the symbol.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Master controller class:
bool PostAction(const std::string &aActionName, T aParam)
Run the specified action after the current action (coroutine) ends.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:708
The common library.
#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
PROJECT & Prj()
Definition kicad.cpp:655
LIBRARY_TABLE_TYPE
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 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.
@ AUTOPLACE_AUTO
Definition sch_item.h:71
T * GetAppSettings(const char *aFilename)
std::vector< std::vector< std::string > > table
wxString result
Test unit parsing edge cases and error handling.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:39