KiCad PCB EDA Suite
Loading...
Searching...
No Matches
legacy_cache_reconcile.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
22#include <set>
23
24#include <lib_id.h>
26#include <sch_screen.h>
27#include <sch_symbol.h>
28#include <widgets/kistatusbar.h>
29
30
31static bool loadLibrary( SYMBOL_LIBRARY_ADAPTER& aAdapter, const wxString& aNickname,
32 std::vector<KI_ERROR>& aErrors )
33{
34 // A library the preload already finished is fully loaded content-wise; forcing another
35 // LoadOne() through LoadLibraryEntry() would just re-enumerate it on the UI thread
36 if( aAdapter.IsLibraryLoaded( aNickname ) )
37 return true;
38
39 std::optional<LIB_STATUS> status = aAdapter.LoadLibraryEntry( aNickname );
40
41 if( status && status->load_status == LOAD_STATUS::LOADED )
42 return true;
43
44 KI_ERROR message;
45
46 if( status && status->error )
47 message.SetTitle( wxString::Format( _( "Library '%s': %s" ), aNickname, status->error->message ) );
48 else
49 message.SetTitle( wxString::Format( _( "Library '%s' could not be loaded." ), aNickname ) );
50
52 aErrors.emplace_back( std::move( message ) );
53
54 return false;
55}
56
57
58int ReconcileLegacyCacheSymbols( SYMBOL_LIBRARY_ADAPTER& aAdapter, const wxString& aCacheNickname,
59 SCH_SCREENS& aScreens, std::vector<KI_ERROR>& aErrors )
60{
61 // A startup preload worker can still be enumerating a library on another thread; joining it
62 // here before touching any LIB_DATA keeps the synchronous loads below from racing it
63 aAdapter.AbortAsyncLoad();
64
65 // Loading rather than observing what the preload reached is what keeps the result the same
66 // from one open to the next
67 if( !loadLibrary( aAdapter, aCacheNickname, aErrors ) )
68 return 0;
69
70 std::vector<wxString> cacheSymbols = aAdapter.GetSymbolNames( aCacheNickname );
71 std::set<wxString> cacheSymbolSet( cacheSymbols.begin(), cacheSymbols.end() );
72
73 if( cacheSymbolSet.empty() )
74 return 0;
75
76 std::vector<wxString> loadedLibs;
77
78 for( const LIBRARY_TABLE_ROW* row : aAdapter.Rows() )
79 {
80 const wxString& libName = row->Nickname();
81
82 // A disabled row is never contacted by the loader; forcing it here would reach a
83 // backend the user switched off
84 if( libName == aCacheNickname || row->Disabled() )
85 continue;
86
87 if( loadLibrary( aAdapter, libName, aErrors ) )
88 loadedLibs.push_back( libName );
89 }
90
91 int repointed = 0;
92
93 for( SCH_SCREEN* screen = aScreens.GetFirst(); screen; screen = aScreens.GetNext() )
94 {
95 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
96 {
97 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
98 LIB_ID newId = symbol->GetLibId();
99 UTF8 fullLibName = newId.Format();
100
101 if( !cacheSymbolSet.count( fullLibName.wx_str() ) )
102 continue;
103
104 bool alreadyExists = false;
105
106 for( const wxString& libName : loadedLibs )
107 {
108 if( aAdapter.LoadSymbol( libName, fullLibName.wx_str() ) )
109 {
110 alreadyExists = true;
111 break;
112 }
113 }
114
115 if( !alreadyExists )
116 {
117 newId.SetLibNickname( aCacheNickname );
118 newId.SetLibItemName( fullLibName );
119 symbol->SetLibId( newId );
120 ++repointed;
121 }
122 }
123 }
124
125 return repointed;
126}
Holds a structured error message.
Definition ki_error.h:38
KI_ERROR & SetSeverity(SEVERITY aSeverity)
Definition ki_error.h:76
KI_ERROR & SetTitle(const wxString &aTitle)
Definition ki_error.h:58
void AbortAsyncLoad()
Aborts any async load in progress; blocks until fully done aborting.
bool IsLibraryLoaded(const wxString &aNickname)
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Like LIBRARY_MANAGER::Rows but filtered to the LIBRARY_TABLE_TYPE of this adapter.
std::optional< LIB_STATUS > LoadLibraryEntry(const wxString &aNickname)
Synchronously loads the named library to LOADED state.
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
UTF8 Format() const
Definition lib_id.cpp:132
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:165
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:758
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
Schematic symbol object.
Definition sch_symbol.h:75
void SetLibId(const LIB_ID &aName)
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:164
An interface to the global shared library manager that is schematic-specific and linked to one projec...
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
std::vector< wxString > GetSymbolNames(const wxString &aNickname, SYMBOL_TYPE aType=SYMBOL_TYPE::ALL_SYMBOLS)
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
static bool loadLibrary(SYMBOL_LIBRARY_ADAPTER &aAdapter, const wxString &aNickname, std::vector< KI_ERROR > &aErrors)
int ReconcileLegacyCacheSymbols(SYMBOL_LIBRARY_ADAPTER &aAdapter, const wxString &aCacheNickname, SCH_SCREENS &aScreens, std::vector< KI_ERROR > &aErrors)
Point symbols that are only available in a legacy project cache library at that cache.
@ RPT_SEVERITY_ERROR
@ SCH_SYMBOL_T
Definition typeinfo.h:168