KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_io_http_lib.h
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 (C) 2023 Andre F. K. Iwers <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <functional>
24#include <map>
25#include <memory>
26#include <set>
27#include <string>
28#include <vector>
29
32
33#include <sch_io/sch_io.h>
34#include <sch_io/sch_io_mgr.h>
36
37#include <atomic>
38#include <condition_variable>
39#include <mutex>
40#include <shared_mutex>
41#include <thread>
42#include <unordered_set>
43
45
53class SCH_IO_HTTP_LIB : public SCH_IO
54{
55public:
58
60 {
61 return IO_BASE::IO_FILE_DESC( _HKI( "KiCad HTTP library files" ), { FILEEXT::HTTPLibraryFileExtension } );
62 }
63
64 int GetModifyHash() const override { return m_modifyHash; }
65
66 void EnumerateSymbolLib( wxArrayString& aSymbolNameList, const wxString& aLibraryPath,
67 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
68
69 void EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList, const wxString& aLibraryPath,
70 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
71
72 void CheckLibrary( const wxString& aLibraryPath,
73 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
74
75 LIB_SYMBOL* LoadSymbol( const wxString& aLibraryPath, const wxString& aAliasName,
76 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
77
78 bool SupportsSubLibraries() const override { return true; }
79
80 void GetSubLibraryNames( std::vector<wxString>& aNames ) override;
81
82 wxString GetSubLibraryDescription( const wxString& aName ) override;
83
84 void GetAvailableSymbolFields( std::vector<wxString>& aNames ) override;
85
86 void GetDefaultSymbolFields( std::vector<wxString>& aNames ) override;
87
88 bool IsLibraryWritable( const wxString& aLibraryPath ) override { return false; }
89
91 {
92 m_adapter = aAdapter;
93 }
94
95 HTTP_LIB_SETTINGS* Settings() const { return m_settings.get(); }
96
97 using CONNECTION_BUILDER = std::function<std::unique_ptr<HTTP_LIB_CONNECTION>( const HTTP_LIB_SOURCE& )>;
98
100 {
101 m_connectionFactory = std::move( aFactory );
102 }
103
104 using SOURCE_PATCHER = std::function<void( HTTP_LIB_SOURCE& )>;
105
108 {
109 m_sourcePatcher = std::move( aOverride );
110 }
111
112 void SaveSymbol( const wxString& aLibraryPath, std::unique_ptr<LIB_SYMBOL> aSymbol,
113 const std::map<std::string, UTF8>* aProperties = nullptr ) override;
114
115 const wxString& GetError() const override { return m_lastError; }
116
117private:
118 void ensureSettings( const wxString& aSettingsPath );
119
120 void ensureConnection();
121
122 void connect();
123
124 LIB_SYMBOL* loadSymbolFromPart( const wxString& aLibraryPath, const wxString& aSymbolName,
125 const HTTP_LIB_CATEGORY& aCategory, const HTTP_LIB_PART& aPart,
126 std::set<wxString>& aCustomFields );
127
128 void cacheLib( const wxString& aLibraryPath );
129
130 size_t computeSignature( const std::map<std::string, HTTP_LIB_CATEGORY>& aCategoryData ) const;
131
132 void materializeCache( const wxString& aLibraryPath,
133 const std::map<std::string, HTTP_LIB_CATEGORY>& aCategoryData );
134
138
139private:
141
144
146
148 std::unique_ptr<HTTP_LIB_CONNECTION> m_conn;
149 std::unique_ptr<HTTP_LIB_SETTINGS> m_settings;
150 std::set<wxString> m_customFields;
151 std::set<wxString> m_defaultShownFields;
152 wxString m_lastError;
154
155 wxString symbol_field = "symbol";
156 wxString footprint_field = "footprint";
157 wxString description_field = "description";
158 wxString keywords_field = "keywords";
159 wxString value_field = "value";
160 wxString datasheet_field = "datasheet";
161 wxString reference_field = "reference";
162
163 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> m_symbolCache;
164
166 std::map<wxString, std::pair<std::string, std::string>> m_partIdMap;
167
168 std::atomic<bool> m_cachePopulated{ false };
171
173 std::shared_mutex m_cacheMutex;
174
177
178 std::thread m_refreshThread;
179 std::atomic<bool> m_refreshRunning{ false };
180 std::condition_variable m_refreshCV;
181 std::mutex m_refreshMutex;
182
183 bool m_inCacheLib = false;
184
185 std::unordered_set<wxString> m_inProgressLoads;
186};
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
Define a library symbol object.
Definition lib_symbol.h:119
void SetConnectionBuilder(CONNECTION_BUILDER aFactory)
std::thread m_refreshThread
LIB_SYMBOL * loadSymbolFromPart(const wxString &aLibraryPath, const wxString &aSymbolName, const HTTP_LIB_CATEGORY &aCategory, const HTTP_LIB_PART &aPart, std::set< wxString > &aCustomFields)
std::unique_ptr< HTTP_LIB_CONNECTION > m_conn
Generally will be null if no valid connection is established.
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void SetSourcePatcher(SOURCE_PATCHER aOverride)
Allows updating a source after initial load; used for QA testing.
HTTP_LIB_SETTINGS * Settings() const
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > m_symbolCache
std::unique_ptr< HTTP_LIB_SETTINGS > m_settings
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
wxString GetSubLibraryDescription(const wxString &aName) override
Gets a description of a sublibrary.
std::function< std::unique_ptr< HTTP_LIB_CONNECTION >(const HTTP_LIB_SOURCE &)> CONNECTION_BUILDER
SYMBOL_LIBRARY_ADAPTER * m_adapter
std::function< void(HTTP_LIB_SOURCE &)> SOURCE_PATCHER
bool SupportsSubLibraries() const override
void ensureSettings(const wxString &aSettingsPath)
int GetModifyHash() const override
Return the modification hash from the library cache.
~SCH_IO_HTTP_LIB() override
std::set< wxString > m_customFields
void GetDefaultSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that should be shown by default for this library in the symb...
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
std::map< wxString, std::pair< std::string, std::string > > m_partIdMap
Symbol name -> (part id, category id); used for the SelectOne fallback in LoadSymbol.
const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
Get the descriptor for the library container that this IO plugin operates on.
std::set< wxString > m_defaultShownFields
void SaveSymbol(const wxString &aLibraryPath, std::unique_ptr< LIB_SYMBOL > aSymbol, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aSymbol to an existing library located at aLibraryPath.
SOURCE_PATCHER m_sourcePatcher
std::unordered_set< wxString > m_inProgressLoads
void SetLibraryManagerAdapter(SYMBOL_LIBRARY_ADAPTER *aAdapter) override
Some library plugins need to interface with other loaded libraries.
std::mutex m_refreshMutex
std::mutex m_symbolLoadMutex
Serializes loadSymbolFromPart calls.
CONNECTION_BUILDER m_connectionFactory
Allows replacing actual HTTP connection for QA tests.
wxString description_field
std::atomic< bool > m_refreshRunning
void materializeCache(const wxString &aLibraryPath, const std::map< std::string, HTTP_LIB_CATEGORY > &aCategoryData)
size_t computeSignature(const std::map< std::string, HTTP_LIB_CATEGORY > &aCategoryData) const
std::atomic< bool > m_cachePopulated
void CheckLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Validate that the library at aLibraryPath is reachable and well-formed, without necessarily loading s...
const wxString & GetError() const override
Return an error string to the caller.
std::condition_variable m_refreshCV
void cacheLib(const wxString &aLibraryPath)
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
std::shared_mutex m_cacheMutex
Protects m_symbolCache, m_partIdMap, and m_customFields.
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
SCH_IO(const wxString &aName)
Definition sch_io.h:407
An interface to the global shared library manager that is schematic-specific and linked to one projec...
static const std::string HTTPLibraryFileExtension
#define _HKI(x)
Definition page_info.cpp:40
Connection parameters for one HTTP library.
Container that describes file type info.
Definition io_base.h:43
Definition of file extensions used in Kicad.