KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_local_symbol_libs.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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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
22#include <wx/filefn.h>
23#include <wx/filename.h>
24#include <wx/log.h>
25
29#include <pgm_base.h>
30#include <project.h>
31#include <project_sch.h>
32#include <reporter.h>
33#include <sch_io/sch_io_mgr.h>
34
35
36std::vector<wxString> RegisterLocalSymbolLibraries( PROJECT& aProject, const wxString& aSourceProjectDir,
37 const std::vector<wxString>& aLibraries, REPORTER& aReporter )
38{
39 std::vector<wxString> added;
40
41 if( aLibraries.empty() )
42 return added;
43
46 std::optional<LIBRARY_TABLE*> optTable = manager.Table( LIBRARY_TABLE_TYPE::SYMBOL, LIBRARY_TABLE_SCOPE::PROJECT );
47
48 wxCHECK_MSG( adapter && optTable.has_value(), added, "Could not load symbol lib table." );
49
50 LIBRARY_TABLE* table = optTable.value();
51 wxString projectPath = aProject.GetProjectPath();
52
53 for( const wxString& file : aLibraries )
54 {
55 if( file.empty() )
56 continue;
57
58 wxFileName fn( file );
59
60 // a project can list a library that was never shipped
61 if( !fn.FileExists() && !wxDirExists( fn.GetFullPath() ) )
62 continue;
63
64 SCH_IO_MGR::SCH_FILE_T type = SCH_IO_MGR::GuessPluginTypeFromLibPath( fn.GetFullPath() );
65
66 if( type == SCH_IO_MGR::SCH_FILE_UNKNOWN )
67 {
68 wxLogTrace( "KIWAY", "Unknown file type: %s", fn.GetFullPath() );
69 continue;
70 }
71
72 // Only libraries that live under the source project are relocated; a plain path prefix would
73 // also match a sibling directory sharing the project name's stem.
74 wxFileName relFn( fn );
75 bool isProjectLocal = !aSourceProjectDir.IsEmpty() && relFn.MakeRelativeTo( aSourceProjectDir )
76 && !relFn.IsAbsolute() && !relFn.GetFullPath().StartsWith( wxS( ".." ) );
77 wxString libTableUri;
78
79 if( isProjectLocal )
80 {
81 // Copy a project-local library into the KiCad project directory and reference it
82 // with a project-relative path so the sym-lib-table stays portable.
83 if( !fn.FileExists() )
84 continue;
85
86 wxFileName projectFn( projectPath, fn.GetFullName() );
87
88 if( fn.GetFullPath() != projectFn.GetFullPath() && !projectFn.FileExists()
89 && !wxCopyFile( fn.GetFullPath(), projectFn.GetFullPath(), false ) )
90 {
91 aReporter.Report( wxString::Format( _( "Error copying symbol library '%s'." ), fn.GetFullPath() ),
93 continue;
94 }
95
96 libTableUri = wxS( "${KIPRJMOD}/" ) + fn.GetFullName();
97 }
98 else
99 {
100 // External library referenced by absolute path. Preserve the original path.
101 libTableUri = fn.GetFullPath();
102 }
103
104 if( !table->HasRow( fn.GetName() ) )
105 {
106 LIBRARY_TABLE_ROW& row = table->InsertRow();
107 row.SetNickname( fn.GetName() );
108 row.SetURI( libTableUri );
109 row.SetType( SCH_IO_MGR::ShowType( type ) );
110 added.emplace_back( fn.GetName() );
111 }
112 }
113
114 if( added.empty() )
115 return added;
116
117 bool success = true;
118
119 table->Save().map_error(
120 [&]( const LIBRARY_ERROR& aError )
121 {
122 aReporter.Report( _( "Error saving project library table:\n\n" ) + aError.message,
124 success = false;
125 } );
126
127 if( !success )
128 return {};
129
130 manager.AbortAsyncLoads();
132
133 for( const wxString& nick : added )
134 adapter->LoadOne( nick );
135
136 return added;
137}
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 AbortAsyncLoads()
Abort any async library loading operations in progress.
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
void SetNickname(const wxString &aNickname)
void SetType(const wxString &aType)
void SetURI(const wxString &aUri)
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:125
static SYMBOL_LIBRARY_ADAPTER * SymbolLibAdapter(PROJECT *aProject)
Accessor for project symbol library manager adapter.
Container for project specific data.
Definition project.h:63
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition project.cpp:276
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
static const wxString ShowType(SCH_FILE_T aFileType)
Return a brief name for a plugin, given aFileType enum.
static SCH_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
Return a plugin type given a symbol library using the file extension of aLibPath.
An interface to the global shared library manager that is schematic-specific and linked to one projec...
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
#define _(s)
std::vector< wxString > RegisterLocalSymbolLibraries(PROJECT &aProject, const wxString &aSourceProjectDir, const std::vector< wxString > &aLibraries, REPORTER &aReporter)
Register the symbol libraries a source project names in the project symbol library table.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
wxString message