2023-09-19 01:09:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 Andre F. K. Iwers <iwers11@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
|
|
|
#include <fmt.h>
|
|
|
|
#include <lib_symbol.h>
|
|
|
|
#include <symbol_lib_table.h>
|
|
|
|
|
|
|
|
#include <http_lib/http_lib_connection.h>
|
2023-12-24 00:31:24 +00:00
|
|
|
#include "sch_io_http_lib.h"
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
|
2023-12-24 04:31:22 +00:00
|
|
|
SCH_IO_HTTP_LIB::SCH_IO_HTTP_LIB() : SCH_IO( wxS( "HTTP library" ) ),
|
2023-09-19 01:09:21 +00:00
|
|
|
m_libTable( nullptr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
SCH_IO_HTTP_LIB::~SCH_IO_HTTP_LIB()
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
|
2023-09-19 01:09:21 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
std::vector<LIB_SYMBOL*> symbols;
|
|
|
|
EnumerateSymbolLib( symbols, aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
for( LIB_SYMBOL* symbol : symbols )
|
|
|
|
aSymbolNameList.Add( symbol->GetName() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
|
2023-09-19 01:09:21 +00:00
|
|
|
const wxString& aLibraryPath,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
wxCHECK_RET( m_libTable, _("httplib plugin missing library table handle!") );
|
|
|
|
ensureSettings( aLibraryPath );
|
|
|
|
ensureConnection();
|
|
|
|
|
|
|
|
if( !m_conn)
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( m_lastError );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool powerSymbolsOnly =
|
|
|
|
( aProperties
|
|
|
|
&& aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->end() );
|
|
|
|
|
|
|
|
// clear buffer
|
|
|
|
m_cachedParts.clear();
|
|
|
|
|
|
|
|
for(const HTTP_LIB_CATEGORY& category : m_conn->getCategories() )
|
|
|
|
{
|
|
|
|
std::vector<HTTP_LIB_PART> found_parts;
|
|
|
|
|
|
|
|
if( !m_conn->SelectAll( category, found_parts ) )
|
|
|
|
{
|
|
|
|
if( !m_conn->GetLastError().empty() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "Error retriving data from HTTP library %s: %s" ),
|
|
|
|
category.name, m_conn->GetLastError() );
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache information for later use in LoadSymbol()
|
|
|
|
m_cachedParts.emplace( category.id, found_parts );
|
|
|
|
|
|
|
|
for( const HTTP_LIB_PART& part : found_parts )
|
|
|
|
{
|
|
|
|
wxString libIDString( part.name );
|
|
|
|
|
|
|
|
LIB_SYMBOL* symbol = loadSymbolFromPart( libIDString, category, part );
|
|
|
|
|
|
|
|
if( symbol && ( !powerSymbolsOnly || symbol->IsPower() ) )
|
|
|
|
aSymbolList.emplace_back( symbol );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
LIB_SYMBOL* SCH_IO_HTTP_LIB::LoadSymbol( const wxString& aLibraryPath,
|
2023-09-19 01:09:21 +00:00
|
|
|
const wxString& aAliasName,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
wxCHECK( m_libTable, nullptr );
|
|
|
|
ensureSettings( aLibraryPath );
|
|
|
|
ensureConnection();
|
|
|
|
|
|
|
|
if( !m_conn )
|
|
|
|
THROW_IO_ERROR( m_lastError );
|
|
|
|
|
|
|
|
std::string part_id = "";
|
|
|
|
|
|
|
|
std::string partName( aAliasName.ToUTF8() );
|
|
|
|
|
|
|
|
const HTTP_LIB_CATEGORY* foundCategory = nullptr;
|
|
|
|
HTTP_LIB_PART result;
|
|
|
|
|
|
|
|
std::vector<HTTP_LIB_CATEGORY> categories = m_conn->getCategories();
|
|
|
|
|
|
|
|
std::tuple relations = m_conn->getCachedParts()[partName];
|
|
|
|
|
|
|
|
// get the matching category
|
|
|
|
for( const HTTP_LIB_CATEGORY& categoryIter : categories )
|
|
|
|
{
|
|
|
|
std::string associatedCatID = std::get<1>( relations );
|
2023-09-19 01:37:34 +00:00
|
|
|
|
2023-09-19 01:09:21 +00:00
|
|
|
if( categoryIter.id == associatedCatID )
|
|
|
|
{
|
|
|
|
foundCategory = &categoryIter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return Null if no category was found. This should never happen
|
2023-09-19 01:37:34 +00:00
|
|
|
if( foundCategory == nullptr )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib, wxT( "loadSymbol: no category found for %s" ), partName );
|
2023-09-19 01:37:34 +00:00
|
|
|
return nullptr;
|
2023-09-19 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the matching query ID
|
2023-09-19 01:37:34 +00:00
|
|
|
for( const HTTP_LIB_PART& part : m_cachedParts[foundCategory->id] )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
if( part.id == std::get<0>( relations ) )
|
|
|
|
{
|
|
|
|
part_id = part.id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_conn->SelectOne( part_id, result ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib, wxT( "loadSymbol: SelectOne (%s) found in %s" ), part_id,
|
|
|
|
foundCategory->name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib, wxT( "loadSymbol: SelectOne (%s) failed for category %s" ),
|
|
|
|
part_id, foundCategory->name );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( m_lastError );
|
|
|
|
}
|
|
|
|
|
|
|
|
wxCHECK( foundCategory, nullptr );
|
|
|
|
|
|
|
|
return loadSymbolFromPart( aAliasName, *foundCategory, result );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::GetSubLibraryNames( std::vector<wxString>& aNames )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
ensureSettings( wxEmptyString );
|
|
|
|
|
|
|
|
aNames.clear();
|
|
|
|
|
|
|
|
std::set<wxString> categoryNames;
|
|
|
|
|
|
|
|
for( const HTTP_LIB_CATEGORY& categoryIter : m_conn->getCategories() )
|
|
|
|
{
|
|
|
|
if( categoryNames.count( categoryIter.name ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
aNames.emplace_back( categoryIter.name );
|
|
|
|
categoryNames.insert( categoryIter.name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::GetAvailableSymbolFields( std::vector<wxString>& aNames )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
// TODO: Implement this sometime; This is currently broken...
|
|
|
|
std::copy( m_customFields.begin(), m_customFields.end(), std::back_inserter( aNames ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::GetDefaultSymbolFields( std::vector<wxString>& aNames )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
std::copy( m_defaultShownFields.begin(), m_defaultShownFields.end(),
|
|
|
|
std::back_inserter( aNames ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::ensureSettings( const wxString& aSettingsPath )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
auto tryLoad = [&]()
|
|
|
|
{
|
|
|
|
if( !m_settings->LoadFromFile() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "HTTP library settings file %s missing or invalid" ),
|
|
|
|
aSettingsPath );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_settings->m_Source.api_version.empty() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2023-09-19 01:37:34 +00:00
|
|
|
_( "HTTP library settings file %s is missing the API version number!" ),
|
|
|
|
aSettingsPath );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_settings->getSupportedAPIVersion() != m_settings->m_Source.api_version )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2023-09-19 01:37:34 +00:00
|
|
|
_( "HTTP library settings file %s uses API version %s, but KiCad requires version %s" ),
|
2023-09-19 01:09:21 +00:00
|
|
|
aSettingsPath,
|
|
|
|
m_settings->m_Source.api_version,
|
|
|
|
m_settings->getSupportedAPIVersion() );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_settings->m_Source.root_url.empty() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
|
|
|
_( "HTTP library settings file %s is missing the root URL!" ),
|
|
|
|
aSettingsPath );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
// map lib source type
|
|
|
|
m_settings->m_Source.type = m_settings->get_HTTP_LIB_SOURCE_TYPE();
|
2023-09-19 01:37:34 +00:00
|
|
|
|
2023-09-19 01:09:21 +00:00
|
|
|
if( m_settings->m_Source.type == HTTP_LIB_SOURCE_TYPE::INVALID )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
2023-09-19 01:37:34 +00:00
|
|
|
_( "HTTP library settings file %s has an invalid library type" ),
|
|
|
|
aSettingsPath );
|
2023-09-19 01:09:21 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that the root url finishes with a forward slash
|
|
|
|
if( m_settings->m_Source.root_url.at( m_settings->m_Source.root_url.length() - 1 ) != '/' )
|
|
|
|
{
|
|
|
|
m_settings->m_Source.root_url += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append api version to root URL
|
|
|
|
m_settings->m_Source.root_url += m_settings->m_Source.api_version + "/";
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
if( !m_settings && !aSettingsPath.IsEmpty() )
|
|
|
|
{
|
|
|
|
std::string path( aSettingsPath.ToUTF8() );
|
|
|
|
m_settings = std::make_unique<HTTP_LIB_SETTINGS>( path );
|
|
|
|
|
|
|
|
m_settings->SetReadOnly( true );
|
|
|
|
|
|
|
|
tryLoad();
|
|
|
|
}
|
|
|
|
else if( m_settings )
|
|
|
|
{
|
|
|
|
// If we have valid settings but no connection yet; reload settings in case user is editing
|
|
|
|
tryLoad();
|
|
|
|
}
|
|
|
|
else if( !m_settings )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib, wxT( "ensureSettings: no settings available!" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::ensureConnection()
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( m_settings, "Call ensureSettings before ensureConnection!" );
|
|
|
|
|
|
|
|
connect();
|
|
|
|
|
2023-09-19 01:37:34 +00:00
|
|
|
if( !m_conn || !m_conn->IsValidEndpoint() )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
|
|
|
_( "Could not connect to %s. Errors: %s" ),
|
|
|
|
m_settings->m_Source.root_url, m_lastError );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::connect()
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( m_settings, "Call ensureSettings before connect()!" );
|
|
|
|
|
|
|
|
if( !m_conn )
|
|
|
|
{
|
|
|
|
m_conn = std::make_unique<HTTP_LIB_CONNECTION>( m_settings->m_Source, true );
|
|
|
|
|
2023-09-19 01:37:34 +00:00
|
|
|
if( !m_conn->IsValidEndpoint() )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
m_lastError = m_conn->GetLastError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
LIB_SYMBOL* SCH_IO_HTTP_LIB::loadSymbolFromPart( const wxString& aSymbolName,
|
2023-09-19 01:09:21 +00:00
|
|
|
const HTTP_LIB_CATEGORY& aCategory,
|
|
|
|
const HTTP_LIB_PART& aPart )
|
|
|
|
{
|
|
|
|
LIB_SYMBOL* symbol = nullptr;
|
|
|
|
LIB_SYMBOL* originalSymbol = nullptr;
|
|
|
|
LIB_ID symbolId;
|
|
|
|
|
|
|
|
std::string symbolIdStr = aPart.symbolIdStr;
|
|
|
|
|
|
|
|
// Get or Create the symbol using the found symbol
|
|
|
|
if( !symbolIdStr.empty() )
|
|
|
|
{
|
|
|
|
symbolId.Parse( symbolIdStr );
|
|
|
|
|
|
|
|
if( symbolId.IsValid() )
|
|
|
|
{
|
|
|
|
originalSymbol = m_libTable->LoadSymbol( symbolId );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( originalSymbol )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib, wxT( "loadSymbolFromPart: found original symbol '%s'" ),
|
|
|
|
symbolIdStr );
|
|
|
|
|
|
|
|
symbol = originalSymbol->Duplicate();
|
|
|
|
symbol->SetSourceLibId( symbolId );
|
|
|
|
|
|
|
|
symbol->LibId().SetSubLibraryName( aCategory.name );
|
|
|
|
}
|
|
|
|
else if( !symbolId.IsValid() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib,
|
|
|
|
wxT( "loadSymbolFromPart: source symbol id '%s' is invalid, "
|
|
|
|
"will create empty symbol" ),
|
|
|
|
symbolIdStr );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceHTTPLib,
|
|
|
|
wxT( "loadSymbolFromPart: source symbol '%s' not found, "
|
|
|
|
"will create empty symbol" ),
|
|
|
|
symbolIdStr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !symbol )
|
|
|
|
{
|
|
|
|
// Actual symbol not found: return metadata only; error will be
|
|
|
|
// indicated in the symbol chooser
|
|
|
|
symbol = new LIB_SYMBOL( aSymbolName );
|
|
|
|
symbol->LibId().SetSubLibraryName( aCategory.name );
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:37:01 +00:00
|
|
|
symbol->SetExcludedFromBOM( aPart.exclude_from_bom );
|
|
|
|
symbol->SetExcludedFromBoard( aPart.exclude_from_board );
|
|
|
|
symbol->SetExcludedFromSim( aPart.exclude_from_sim );
|
|
|
|
|
2023-09-19 01:09:21 +00:00
|
|
|
LIB_FIELD* field;
|
|
|
|
|
|
|
|
for( auto& _field : aPart.fields )
|
|
|
|
{
|
2023-09-27 00:38:25 +00:00
|
|
|
wxString fieldName = wxString( _field.first );
|
|
|
|
std::tuple fieldProperties = _field.second;
|
2023-09-19 01:09:21 +00:00
|
|
|
|
2023-09-27 00:38:25 +00:00
|
|
|
if( fieldName.Lower() == footprint_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
field = &symbol->GetFootprintField();
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
}
|
2023-09-27 00:38:25 +00:00
|
|
|
else if( fieldName.Lower() == description_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
field = &symbol->GetDescriptionField();
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
}
|
2023-09-27 00:38:25 +00:00
|
|
|
else if( fieldName.Lower() == value_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
field = &symbol->GetValueField();
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
}
|
2023-09-27 00:38:25 +00:00
|
|
|
else if( fieldName.Lower() == datasheet_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
field = &symbol->GetDatasheetField();
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
}
|
2023-09-27 00:38:25 +00:00
|
|
|
else if( fieldName.Lower() == reference_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
field = &symbol->GetReferenceField();
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
}
|
2023-09-27 00:38:25 +00:00
|
|
|
else if( fieldName.Lower() == keywords_field )
|
2023-09-19 01:09:21 +00:00
|
|
|
{
|
|
|
|
symbol->SetKeyWords( std::get<0>( fieldProperties ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Generic fields
|
|
|
|
field = new LIB_FIELD( symbol->GetNextAvailableFieldId() );
|
|
|
|
field->SetName( fieldName );
|
|
|
|
|
|
|
|
field->SetText( std::get<0>( fieldProperties ) );
|
|
|
|
field->SetVisible( std::get<1>( fieldProperties ) );
|
|
|
|
symbol->AddField( field );
|
|
|
|
|
|
|
|
m_customFields.insert( fieldName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2023-12-24 00:31:24 +00:00
|
|
|
void SCH_IO_HTTP_LIB::SaveSymbol( const wxString& aLibraryPath, const LIB_SYMBOL* aSymbol,
|
2023-09-19 01:09:21 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
// TODO: Implement this sometime;
|
2023-09-19 01:27:39 +00:00
|
|
|
}
|