23#include <magic_enum.hpp>
33#include <api/common/types/base_types.pb.h>
34#include <api/common/commands/editor_commands.pb.h>
35#include <api/common/types/library_types.pb.h>
40using namespace kiapi::common::commands;
41using kiapi::common::types::LibraryCommandStatus;
42using kiapi::common::types::LibraryLoadStatus;
43using kiapi::common::types::LibraryTableScope;
44using kiapi::common::types::LibraryType;
74 const wxString& aNickname )
const
86 e.set_status( ApiStatusCode::AS_UNHANDLED );
87 return tl::unexpected( e );
96 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
97 e.set_error_message(
"no library adapter is available for this library type" );
98 return tl::unexpected( e );
101 std::vector<wxString> nicknames;
103 if( aCtx.
Request.nickname_size() > 0 )
105 for(
const std::string& nickname : aCtx.
Request.nickname() )
107 wxString nicknameStr = wxString::FromUTF8( nickname );
112 nicknames.emplace_back( nicknameStr );
118 nicknames.emplace_back( row->Nickname() );
121 LibraryItemsResponse response;
123 for(
const wxString& nickname : nicknames )
125 for(
const wxString& itemName :
getItemNames( *adapter, nickname ) )
127 kiapi::common::types::LibraryIdentifier*
id = response.add_items();
128 id->set_library_nickname( nickname.ToUTF8() );
129 id->set_entry_name( itemName.ToUTF8() );
140 aEntry.set_nickname( aRow.
Nickname().ToUTF8() );
141 aEntry.set_uri( aRow.
URI().ToUTF8() );
144 aEntry.set_description( aRow.
Description().ToUTF8() );
147 ( *aEntry.mutable_options() )[key] = value;
149 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter =
Pgm().GetLibraryManager().Adapter( aTableType ) )
150 aEntry.set_writable( ( *adapter )->IsWritable( aRow.
Nickname() ) );
152 aEntry.set_disabled( aRow.
Disabled() );
153 aEntry.set_hidden( aRow.
Hidden() );
164 aEntry->set_error_message( aStatus.
error->message.ToUTF8() );
176 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
177 e.set_error_message(
"unknown library table scope" );
178 return tl::unexpected( e );
182 LibraryStatusResponse response;
186 if( !aCtx.
Request.types().empty() )
190 for(
int typeProto : aCtx.
Request.types() )
197 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
198 e.set_error_message( wxString::Format(
"invalid library table type %d", typeProto ).ToUTF8() );
199 return tl::unexpected( e );
202 types.push_back( type );
210 if( std::optional<LIBRARY_MANAGER_ADAPTER*> optAdapter = manager.
Adapter( tableType ) )
211 adapter = *optAdapter;
215 if( row->Disabled() )
218 kiapi::common::types::LibraryStatusEntry* entry = response.add_libraries();
222 if( std::optional<LIB_STATUS> status = adapter->
GetLibraryStatus( row->Nickname() ) )
241 []( LibraryCommandStatus::Code aCode,
const wxString& aMessage = wxEmptyString )
243 LibraryCommandStatus status;
244 status.set_code( aCode );
246 if( !aMessage.empty() )
247 status.set_error_message( aMessage.ToUTF8() );
252 if( aCtx.
Request.type() == LibraryType::LT_UNKNOWN )
255 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
256 e.set_error_message(
"a valid library type is required" );
257 return tl::unexpected( e );
260 if( aCtx.
Request.scope() == LibraryTableScope::LTS_BOTH || aCtx.
Request.scope() == LibraryTableScope::LTS_UNKNOWN )
263 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
264 e.set_error_message(
"LTS_BOTH is invalid for ReloadLibrary; choose one table" );
265 return tl::unexpected( e );
275 if( !manager.
Adapter( tableType ) )
278 e.set_status( ApiStatusCode::AS_UNHANDLED );
279 return tl::unexpected( e );
282 std::vector<wxString> nicknames;
284 if( aCtx.
Request.nickname_size() > 0 )
286 for(
const std::string& nickname : aCtx.
Request.nickname() )
288 std::optional<LIBRARY_TABLE_ROW*> row = manager.
GetRow( tableType, nickname, scope );
290 if( !row.has_value() )
292 return makeStatus( LibraryCommandStatus::LCS_NOT_FOUND,
293 wxString::Format(
"Library '%s' not found", nickname ) );
295 else if( row.value()->Disabled() )
297 return makeStatus( LibraryCommandStatus::LCS_NOT_FOUND,
298 wxString::Format(
"Library '%s' not enabled", nickname ) );
301 nicknames.emplace_back( wxString::FromUTF8( nickname ) );
308 if( !row->Disabled() )
309 nicknames.emplace_back( row->Nickname() );
313 if( nicknames.empty() )
314 return makeStatus( LibraryCommandStatus::LCS_NOT_FOUND,
"No libraries found to reload" );
316 for(
const wxString& nickname : nicknames )
319 return makeStatus( LibraryCommandStatus::LCS_OK );
327 []( LibraryCommandStatus::Code aCode,
const wxString& aMessage = wxEmptyString )
329 LibraryCommandStatus status;
330 status.set_code( aCode );
332 if( !aMessage.empty() )
333 status.set_error_message( aMessage.ToUTF8() );
341 e.set_status( ApiStatusCode::AS_UNIMPLEMENTED );
342 e.set_error_message(
"LoadAllLibraries is not available in GUI mode" );
343 return tl::unexpected( e );
346 std::map<LIBRARY_TABLE_TYPE, KIWAY::FACE_T> typesToLoad;
348 if( aCtx.
Request.type_size() == 0 )
356 for(
int protoRaw : aCtx.
Request.type() )
358 LibraryType protoType =
static_cast<LibraryType
>( protoRaw );
370 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
371 e.set_error_message( wxString::Format( wxS(
"invalid library type %s" ),
372 magic_enum::enum_name( protoType ) ).ToUTF8() );
373 return tl::unexpected( e );
383 e.set_status( ApiStatusCode::AS_NOT_READY );
384 e.set_error_message(
"internal error while attempting to load all libraries" );
385 return tl::unexpected( e );
391 for(
const auto& [type, face] : typesToLoad )
397 if( status.code() != LibraryCommandStatus::LCS_OK )
417 if( !
kiface->LoadAllLibraries() )
421 return makeStatus( error ? LibraryCommandStatus::LCS_ERROR : LibraryCommandStatus::LCS_OK );
429 LibraryCommandStatus status;
433 status.set_code( LibraryCommandStatus::LCS_ERROR );
438 status.set_code( LibraryCommandStatus::LCS_OK );
449 e.set_status( ApiStatusCode::AS_UNHANDLED );
450 return tl::unexpected( e );
453 wxString projectPath = wxString::FromUTF8( aCtx.
Request.document().project().path() );
460 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
461 e.set_error_message(
"the requested project is not open" );
462 return tl::unexpected( e );
470 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
471 e.set_error_message(
"no library adapter is available for this library type" );
472 return tl::unexpected( e );
475 GetItemsResponse response;
476 response.mutable_header()->mutable_document()->CopyFrom( aCtx.
Request.document() );
477 google::protobuf::Any
any;
479 if( aCtx.
Request.item_ids().empty() )
482 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
483 e.set_error_message(
"specify at least one lib_id to retrieve" );
484 return tl::unexpected( e );
487 std::map<wxString, std::vector<wxString>> byNickname;
489 for(
const kiapi::common::types::LibraryIdentifier&
id : aCtx.
Request.item_ids() )
491 byNickname[wxString::FromUTF8(
id.library_nickname() )].emplace_back( wxString::FromUTF8(
id.entry_name() ) );
494 for(
auto& [nickname, entryNames] : byNickname )
499 for(
const wxString& entryName : entryNames )
503 any.Swap( response.add_items() );
509 if( response.items().empty() )
512 e.set_status( ApiStatusCode::AS_BAD_REQUEST );
513 e.set_error_message(
"none of the requested items were found" );
514 return tl::unexpected( e );
517 response.set_status( kiapi::common::types::ItemRequestStatus::IRS_OK );
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
tl::expected< T, ApiResponseStatus > HANDLER_RESULT
static void packLibraryStatusEntry(kiapi::common::types::LibraryStatusEntry *aEntry, LIBRARY_TABLE_TYPE aTableType, const LIBRARY_TABLE_ROW &aRow, const LIB_STATUS &aStatus)
static void packTableEntry(kiapi::common::types::LibraryTableEntry &aEntry, LIBRARY_TABLE_TYPE aTableType, const LIBRARY_TABLE_ROW &aRow)
HANDLER_RESULT< kiapi::common::commands::LibraryItemsResponse > handleGetLibraryItems(const HANDLER_CONTEXT< kiapi::common::commands::GetLibraryItems > &aCtx)
HANDLER_RESULT< kiapi::common::commands::LibraryStatusResponse > handleGetLibraryStatuses(const HANDLER_CONTEXT< kiapi::common::commands::GetLibraryStatuses > &aCtx)
~API_HANDLER_LIBRARIES() override
HANDLER_RESULT< kiapi::common::types::LibraryCommandStatus > handleReloadLibrary(const HANDLER_CONTEXT< kiapi::common::commands::ReloadLibrary > &aCtx)
kiapi::common::types::LibraryCommandStatus loadAllLibraries()
Starts a background load of all libraries of this handler's type.
virtual std::vector< wxString > getItemNames(LIBRARY_MANAGER_ADAPTER &aAdapter, const wxString &aNickname) const
Returns the names of all entries in the given library, or an empty vector if the library is not loade...
virtual LIBRARY_MANAGER_ADAPTER * adapterForProject(PROJECT &aProject) const
LIBRARY_TABLE_TYPE m_type
API_HANDLER_LIBRARIES(LIBRARY_TABLE_TYPE aType=LIBRARY_TABLE_TYPE::DESIGN_BLOCK)
std::function< void(KIFACE *)> m_handlerRegisterCallback
Invoked with a lazily-loaded KIFACE so its handlers can be registered on the server.
KIWAY * m_kiway
Used to lazily load kifaces for LoadAllLibraries in headless; null in GUI mode.
virtual bool packLibraryItem(const LIB_ID &aId, google::protobuf::Any &aOutput) const
HANDLER_RESULT< kiapi::common::types::LibraryCommandStatus > handleLoadAllLibraries(const HANDLER_CONTEXT< kiapi::common::commands::LoadAllLibraries > &aCtx)
HANDLER_RESULT< kiapi::common::commands::GetItemsResponse > handleGetItemsFromLibrary(const HANDLER_CONTEXT< kiapi::common::commands::GetItemsFromLibrary > &aCtx)
void registerHandler(HANDLER_RESULT< ResponseType >(HandlerType::*aHandler)(const HANDLER_CONTEXT< RequestType > &), bool aAllowedDuringTask=false)
Registers an API command handler for the given message types.
DESIGN_BLOCK * LoadDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName, bool aKeepUUID=false, wxString *aErrorMsg=nullptr)
Load a design block having aDesignBlockName from the library given by aNickname.
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
virtual KIFACE * KiFACE(FACE_T aFaceId, bool doLoad=true)
Return the KIFACE* given a FACE_T.
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
std::optional< LIB_STATUS > GetLibraryStatus(const wxString &aNickname) const
Returns the status of a loaded library, or nullopt if the library hasn't been loaded (yet)
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.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
std::optional< LIB_STATUS > LoadLibraryEntry(const wxString &aNickname)
Synchronously loads the named library to LOADED state.
std::optional< LIBRARY_MANAGER_ADAPTER * > Adapter(LIBRARY_TABLE_TYPE aType) const
void ReloadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
std::optional< LIBRARY_TABLE_ROW * > GetRow(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
LIBRARY_TABLE_SCOPE Scope() const
std::map< std::string, UTF8 > GetOptionsMap() const
const wxString & Description() const
const wxString & URI() const
const wxString & Nickname() const
A logical library item identifier and consists of various portions much like a URI.
const UTF8 & GetLibItemName() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
virtual SETTINGS_MANAGER & GetSettingsManager() const
virtual LIBRARY_MANAGER & GetLibraryManager() const
Container for project specific data.
virtual DESIGN_BLOCK_LIBRARY_ADAPTER * DesignBlockLibs()
Return the table of design block libraries.
PROJECT * GetProjectForPath(const wxString &aProjectPath) const
Return the active project iff its path matches aProjectPath, else nullptr.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
A type-safe container of any type.
PGM_BASE & Pgm()
The global program "get" accessor.
RequestMessageType Request
Implement a participant in the KIWAY alchemy.
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)