KiCad PCB EDA Suite
|
Main class of Plugin and Content Manager subsystem. More...
#include <pcm.h>
Public Member Functions | |
PLUGIN_CONTENT_MANAGER (std::function< void(int)> aAvailableUpdateCallbac) | |
~PLUGIN_CONTENT_MANAGER () | |
void | SaveInstalledPackages () |
Saves metadata of installed packages to disk. | |
bool | FetchRepository (const wxString &aUrl, PCM_REPOSITORY &aRepository, PROGRESS_REPORTER *aReporter) |
Fetches repository metadata from given url. | |
void | ValidateJson (const nlohmann::json &aJson, const nlohmann::json_uri &aUri=nlohmann::json_uri("#")) const |
Validates json against a specific definition in the PCM schema. | |
bool | VerifyHash (std::istream &aStream, const wxString &aHash) const |
Verifies SHA256 hash of a binary stream. | |
void | SetRepositoryList (const STRING_PAIR_LIST &aRepositories) |
Set list of repositories. | |
void | DiscardRepositoryCache (const wxString &aRepositoryId) |
Discard in-memory and on-disk cache of a repository. | |
void | MarkInstalled (const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId) |
Mark package as installed. | |
void | MarkUninstalled (const PCM_PACKAGE &aPackage) |
Mark package as uninstalled. | |
const STRING_TUPLE_LIST & | GetRepositoryList () const |
Get list of repositories. | |
bool | CacheRepository (const wxString &aRepositoryId) |
Cache specified repository packages and other metadata. | |
const std::vector< PCM_PACKAGE > & | GetRepositoryPackages (const wxString &aRepositoryId) const |
Get the packages metadata from a previously cached repository. | |
const std::vector< PCM_INSTALLATION_ENTRY > | GetInstalledPackages () const |
Get list of installed packages. | |
const wxString & | GetInstalledPackageVersion (const wxString &aPackageId) const |
Get the current version of an installed package. | |
const wxString & | Get3rdPartyPath () const |
PCM_PACKAGE_STATE | GetPackageState (const wxString &aRepositoryId, const wxString &aPackageId) |
Get current state of the package. | |
bool | IsPackagePinned (const wxString &aPackageId) const |
Returns pinned status of a package. | |
void | SetPinned (const wxString &aPackageId, const bool aPinned) |
Set the pinned status of a package. | |
const wxString | GetPackageUpdateVersion (const PCM_PACKAGE &aPackage) |
Get the preferred package update version or empty string if there is none. | |
bool | DownloadToStream (const wxString &aUrl, std::ostream *aOutput, PROGRESS_REPORTER *aReporter, const size_t aSizeLimit=DEFAULT_DOWNLOAD_MEM_LIMIT) |
Downloads url to an output stream. | |
int | GetPackageSearchRank (const PCM_PACKAGE &aPackage, const wxString &aSearchTerm) |
Get the approximate measure of how much given package matches the search term. | |
std::unordered_map< wxString, wxBitmap > | GetRepositoryPackageBitmaps (const wxString &aRepositoryId) |
Get the icon bitmaps for repository packages. | |
std::unordered_map< wxString, wxBitmap > | GetInstalledPackageBitmaps () |
Get the icon bitmaps for installed packages. | |
void | SetDialogWindow (wxWindow *aDialog) |
Set the Dialog Window. | |
void | RunBackgroundUpdate () |
Runs a background update thread that checks for new package versions. | |
void | StopBackgroundUpdate () |
Interrupts and joins() the update thread. | |
void | ReadEnvVar () |
Stores 3rdparty path from environment variables. | |
Static Public Member Functions | |
static void | PreparePackage (PCM_PACKAGE &aPackage) |
Parses version strings and calculates compatibility. | |
Private Member Functions | |
bool | fetchPackages (const wxString &aUrl, const std::optional< wxString > &aHash, std::vector< PCM_PACKAGE > &aPackages, PROGRESS_REPORTER *aReporter) |
Downloads packages metadata to in memory stream, verifies hash and attempts to parse it. | |
const PCM_REPOSITORY & | getCachedRepository (const wxString &aRepositoryId) const |
Get the cached repository metadata. | |
void | updateInstalledPackagesMetadata (const wxString &aRepositoryId) |
Updates metadata of installed packages from freshly fetched repo. | |
time_t | getCurrentTimestamp () const |
Private Attributes | |
wxWindow * | m_dialog |
nlohmann::json_schema::json_validator | m_schema_validator |
wxString | m_3rdparty_path |
wxString | m_cache_path |
std::unordered_map< wxString, PCM_REPOSITORY > | m_repository_cache |
STRING_TUPLE_LIST | m_repository_list |
std::map< wxString, PCM_INSTALLATION_ENTRY > | m_installed |
std::function< void(int)> | m_availableUpdateCallback |
std::thread | m_updateThread |
std::shared_ptr< BACKGROUND_JOB > | m_updateBackgroundJob |
Static Private Attributes | |
static constexpr size_t | DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024 |
< Default download limit of 10 Mb to not use too much memory | |
static const std::tuple< int, int, int > | m_kicad_version |
Main class of Plugin and Content Manager subsystem.
This class handles logistics of repository management, caching, json validation, tracking installed packages and provides some utility methods.
Repository caching is done in $KICADX_3RD_PARTY/cache directory with each repository storing it's metadata, packages and optionally resource files under it's id subdirectory.
Repository id is a prefix of sha256 of it's main url.
JSON schema file is expected to be in $KICAD_DATA/schemas directory
Installed packages are stored in <user_settings>/installed_packages.json If that file is missing PCM will try to reconstruct it from existing directory structure inside $KICADX_3RD_PARTY but package descriptions and other metadata will be lost.
PLUGIN_CONTENT_MANAGER::PLUGIN_CONTENT_MANAGER | ( | std::function< void(int)> | aAvailableUpdateCallbac | ) |
Definition at line 72 of file pcm.cpp.
References _, PCM_INSTALLATION_ENTRY::current_version, dummy, FN_NORMALIZE_FLAGS, GetMajorMinorVersion(), PATHS::GetStockDataPath(), PATHS::GetUserSettingsPath(), PCM_PACKAGE::identifier, PCM_INSTALLATION_ENTRY::install_timestamp, PACKAGE_VERSION::kicad_version, m_3rdparty_path, m_installed, m_schema_validator, PCM_PACKAGE::name, PCM_INSTALLATION_ENTRY::package, PCM_PACKAGE_DIRECTORIES(), PVS_STABLE, ReadEnvVar(), PCM_INSTALLATION_ENTRY::repository_name, PACKAGE_VERSION::status, PACKAGE_VERSION::version, and PCM_PACKAGE::versions.
PLUGIN_CONTENT_MANAGER::~PLUGIN_CONTENT_MANAGER | ( | ) |
Definition at line 1197 of file pcm.cpp.
References StopBackgroundUpdate().
bool PLUGIN_CONTENT_MANAGER::CacheRepository | ( | const wxString & | aRepositoryId | ) |
Cache specified repository packages and other metadata.
This method fetches latest repository metadata, checks if there is cache on disk, compares it's last update timestamp to repository metadata and redownloads packages list if necessary. Then it checks sha256 if provided and parses the package list. Parsed packages metadata is stored in memory.
Process is repeated with resources file except it is just stored on disk and any errors at this stage are ignored.
aRepositoryId | id of the repository to cache |
Definition at line 392 of file pcm.cpp.
References _, DownloadToStream(), fetchPackages(), FetchRepository(), getCurrentTimestamp(), PATHS::GetUserCachePath(), m_dialog, m_repository_cache, m_repository_list, m_updateBackgroundJob, PCM_REPOSITORY::package_list, PCM_REPOSITORY::package_map, PCM_REPOSITORY::packages, PreparePackage(), PCM_REPOSITORY::resources, PCM_RESOURCE_REFERENCE::sha256, PCM_RESOURCE_REFERENCE::update_timestamp, updateInstalledPackagesMetadata(), PCM_RESOURCE_REFERENCE::url, and VerifyHash().
Referenced by GetPackageState(), and RunBackgroundUpdate().
void PLUGIN_CONTENT_MANAGER::DiscardRepositoryCache | ( | const wxString & | aRepositoryId | ) |
Discard in-memory and on-disk cache of a repository.
aRepositoryId | id of the repository |
Definition at line 751 of file pcm.cpp.
References PATHS::GetUserCachePath(), and m_repository_cache.
Referenced by SetRepositoryList().
bool PLUGIN_CONTENT_MANAGER::DownloadToStream | ( | const wxString & | aUrl, |
std::ostream * | aOutput, | ||
PROGRESS_REPORTER * | aReporter, | ||
const size_t | aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT |
||
) |
Downloads url to an output stream.
aUrl | URL to download |
aOutput | output stream |
aReporter | progress dialog to use |
aSizeLimit | maximum download size, 0 for unlimited |
Definition at line 214 of file pcm.cpp.
References _, KICAD_CURL_EASY::GetErrorText(), PROGRESS_REPORTER::IsCancelled(), PROGRESS_REPORTER::KeepRefreshing(), m_dialog, KICAD_CURL_EASY::Perform(), PROGRESS_REPORTER::Report(), PROGRESS_REPORTER::SetCurrentProgress(), KICAD_CURL_EASY::SetFollowRedirects(), KICAD_CURL_EASY::SetOutputStream(), KICAD_CURL_EASY::SetTransferCallback(), and KICAD_CURL_EASY::SetURL().
Referenced by CacheRepository(), fetchPackages(), and FetchRepository().
|
private |
Downloads packages metadata to in memory stream, verifies hash and attempts to parse it.
aUrl | URL of the packages metadata |
aHash | optional sha256 hash |
aPackages | resulting packages metadata list |
aReporter | progress dialog to use for download |
Definition at line 321 of file pcm.cpp.
References _, DownloadToStream(), m_dialog, PROGRESS_REPORTER::SetTitle(), ValidateJson(), and VerifyHash().
Referenced by CacheRepository().
bool PLUGIN_CONTENT_MANAGER::FetchRepository | ( | const wxString & | aUrl, |
PCM_REPOSITORY & | aRepository, | ||
PROGRESS_REPORTER * | aReporter | ||
) |
Fetches repository metadata from given url.
aUrl | URL of the repository |
aRepository | fetched repository metadata |
aReporter | progress reporter dialog to use for download |
Definition at line 272 of file pcm.cpp.
References _, DownloadToStream(), m_dialog, PROGRESS_REPORTER::SetTitle(), and ValidateJson().
Referenced by CacheRepository().
|
inline |
Definition at line 234 of file pcm.h.
References m_3rdparty_path.
|
private |
Get the cached repository metadata.
aRepositoryId | id of the repository |
Definition at line 383 of file pcm.cpp.
References m_repository_cache.
Referenced by GetPackageState(), GetRepositoryPackages(), MarkInstalled(), and updateInstalledPackagesMetadata().
|
private |
Definition at line 881 of file pcm.cpp.
Referenced by CacheRepository(), and MarkInstalled().
std::unordered_map< wxString, wxBitmap > PLUGIN_CONTENT_MANAGER::GetInstalledPackageBitmaps | ( | ) |
Get the icon bitmaps for installed packages.
Icons for installed packages are taken from package extracted files in $KICADX_3RD_PARTY/resources/<packageid> directories
Definition at line 1066 of file pcm.cpp.
References m_3rdparty_path.
const std::vector< PCM_INSTALLATION_ENTRY > PLUGIN_CONTENT_MANAGER::GetInstalledPackages | ( | ) | const |
Get list of installed packages.
Definition at line 912 of file pcm.cpp.
References m_installed.
const wxString & PLUGIN_CONTENT_MANAGER::GetInstalledPackageVersion | ( | const wxString & | aPackageId | ) | const |
Get the current version of an installed package.
aPackageId | id of the package |
std::out_of_range | if package with given id is not installed Returns current 3rd party directory path |
Definition at line 935 of file pcm.cpp.
References m_installed.
int PLUGIN_CONTENT_MANAGER::GetPackageSearchRank | ( | const PCM_PACKAGE & | aPackage, |
const wxString & | aSearchTerm | ||
) |
Get the approximate measure of how much given package matches the search term.
aPackage | package metadata object |
aSearchTerm | search term |
Definition at line 962 of file pcm.cpp.
References PCM_PACKAGE::author, PCM_PACKAGE::description, PCM_PACKAGE::description_full, PCM_PACKAGE::identifier, PCM_PACKAGE::license, PCM_PACKAGE::maintainer, PCM_CONTACT::name, PCM_PACKAGE::name, PCM_PACKAGE::resources, and PCM_PACKAGE::tags.
PCM_PACKAGE_STATE PLUGIN_CONTENT_MANAGER::GetPackageState | ( | const wxString & | aRepositoryId, |
const wxString & | aPackageId | ||
) |
Get current state of the package.
aRepositoryId | repository id |
aPackageId | package id |
Definition at line 807 of file pcm.cpp.
References CacheRepository(), getCachedRepository(), GetPackageUpdateVersion(), m_installed, PCM_REPOSITORY::package_list, PCM_REPOSITORY::package_map, PPS_AVAILABLE, PPS_INSTALLED, PPS_UNAVAILABLE, PPS_UPDATE_AVAILABLE, and PCM_PACKAGE::versions.
Referenced by RunBackgroundUpdate().
const wxString PLUGIN_CONTENT_MANAGER::GetPackageUpdateVersion | ( | const PCM_PACKAGE & | aPackage | ) |
Get the preferred package update version or empty string if there is none.
Works only for installed packages and returns highest compatible version greater than currently installed that is at the same or higher (numerically lower) version stability level.
aPackage | package |
Definition at line 853 of file pcm.cpp.
References PCM_PACKAGE::identifier, m_installed, PCM_INSTALLATION_ENTRY::package, and PCM_PACKAGE::versions.
Referenced by GetPackageState().
|
inline |
Get list of repositories.
Definition at line 188 of file pcm.h.
References m_repository_list.
std::unordered_map< wxString, wxBitmap > PLUGIN_CONTENT_MANAGER::GetRepositoryPackageBitmaps | ( | const wxString & | aRepositoryId | ) |
Get the icon bitmaps for repository packages.
Repository package icons are taken from repository's resources zip file
aRepositoryId | id of the repository |
Definition at line 1023 of file pcm.cpp.
References PATHS::GetUserCachePath(), image, and zip.
const std::vector< PCM_PACKAGE > & PLUGIN_CONTENT_MANAGER::GetRepositoryPackages | ( | const wxString & | aRepositoryId | ) | const |
Get the packages metadata from a previously cached repository.
This should only be called after a successful CacheRepository call
aRepositoryId | id of the repository |
Definition at line 707 of file pcm.cpp.
References empty(), getCachedRepository(), and PCM_REPOSITORY::package_list.
bool PLUGIN_CONTENT_MANAGER::IsPackagePinned | ( | const wxString & | aPackageId | ) | const |
Returns pinned status of a package.
aPackageId | package id |
Definition at line 944 of file pcm.cpp.
References m_installed.
void PLUGIN_CONTENT_MANAGER::MarkInstalled | ( | const PCM_PACKAGE & | aPackage, |
const wxString & | aVersion, | ||
const wxString & | aRepositoryId | ||
) |
Mark package as installed.
aPackage | package metadata |
aVersion | installed package version |
aRepositoryId | id of the source repository or empty estring if installed from a local file |
Definition at line 765 of file pcm.cpp.
References _, PCM_INSTALLATION_ENTRY::current_version, getCachedRepository(), getCurrentTimestamp(), PCM_PACKAGE::identifier, PCM_INSTALLATION_ENTRY::install_timestamp, m_installed, MarkUninstalled(), PCM_REPOSITORY::name, PCM_INSTALLATION_ENTRY::package, PCM_INSTALLATION_ENTRY::pinned, PCM_INSTALLATION_ENTRY::repository_id, and PCM_INSTALLATION_ENTRY::repository_name.
void PLUGIN_CONTENT_MANAGER::MarkUninstalled | ( | const PCM_PACKAGE & | aPackage | ) |
Mark package as uninstalled.
aPackage | package metadata |
Definition at line 801 of file pcm.cpp.
References PCM_PACKAGE::identifier, and m_installed.
Referenced by MarkInstalled().
|
static |
Parses version strings and calculates compatibility.
This should be called after loading package metadata from repository or from installation entries
aPackage | package metadata object |
Definition at line 629 of file pcm.cpp.
References PACKAGE_VERSION::compatible, PACKAGE_VERSION::kicad_version, PACKAGE_VERSION::kicad_version_max, m_kicad_version, PACKAGE_VERSION::parsed_version, PACKAGE_VERSION::platforms, PACKAGE_VERSION::version, PACKAGE_VERSION::version_epoch, and PCM_PACKAGE::versions.
Referenced by CacheRepository(), and PCM_TASK_MANAGER::InstallFromFile().
void PLUGIN_CONTENT_MANAGER::ReadEnvVar | ( | ) |
Stores 3rdparty path from environment variables.
Definition at line 202 of file pcm.cpp.
References PATHS::GetDefault3rdPartyPath(), PGM_BASE::GetLocalEnvVariables(), ENV_VAR::GetVersionedEnvVarValue(), m_3rdparty_path, and Pgm().
Referenced by PLUGIN_CONTENT_MANAGER().
void PLUGIN_CONTENT_MANAGER::RunBackgroundUpdate | ( | ) |
Runs a background update thread that checks for new package versions.
Definition at line 1109 of file pcm.cpp.
References _, CacheRepository(), BACKGROUND_JOBS_MONITOR::Create(), PGM_BASE::GetBackgroundJobMonitor(), GetPackageState(), PCM_PACKAGE::identifier, m_availableUpdateCallback, m_installed, m_repository_cache, m_repository_list, m_updateBackgroundJob, m_updateThread, name, PCM_INSTALLATION_ENTRY::package, Pgm(), PCM_INSTALLATION_ENTRY::pinned, PPS_UPDATE_AVAILABLE, BACKGROUND_JOBS_MONITOR::Remove(), and PCM_INSTALLATION_ENTRY::repository_id.
void PLUGIN_CONTENT_MANAGER::SaveInstalledPackages | ( | ) |
Saves metadata of installed packages to disk.
Path is <user settings>/installed_packages.json
Definition at line 888 of file pcm.cpp.
References PATHS::GetUserSettingsPath(), and m_installed.
|
inline |
Set the Dialog Window.
PCM can effectively run in "silent" mode with a background thread that reports to kicad manager window status bar. Setting valid window pointer here will switch it to GUI mode with WX_PROGRESS_DIALOG popup for downloads.
aDialog | parent dialog for progress window |
Definition at line 329 of file pcm.h.
References m_dialog.
void PLUGIN_CONTENT_MANAGER::SetPinned | ( | const wxString & | aPackageId, |
const bool | aPinned | ||
) |
Set the pinned status of a package.
no-op for not installed packages
aPackageId | package id |
aPinned | pinned status |
Definition at line 953 of file pcm.cpp.
References m_installed.
void PLUGIN_CONTENT_MANAGER::SetRepositoryList | ( | const STRING_PAIR_LIST & | aRepositories | ) |
Set list of repositories.
Discards cache of repositories that were previously cached but are not on the new list of repositories.
aRepositories | list of <URL, name> pairs of repositories |
Definition at line 722 of file pcm.cpp.
References DiscardRepositoryCache(), m_repository_cache, and m_repository_list.
void PLUGIN_CONTENT_MANAGER::StopBackgroundUpdate | ( | ) |
Interrupts and joins() the update thread.
Definition at line 1185 of file pcm.cpp.
References m_updateBackgroundJob, and m_updateThread.
Referenced by ~PLUGIN_CONTENT_MANAGER().
|
private |
Updates metadata of installed packages from freshly fetched repo.
This completely replaces all fields including description. Only exception is versions field, if currently installed version is missing from the repo metadata it is manually added back in to correctly display in the installed packages.
aRepositoryId | Returns current UTC timestamp |
Definition at line 565 of file pcm.cpp.
References getCachedRepository(), PCM_PACKAGE::identifier, m_installed, PCM_INSTALLATION_ENTRY::package, PCM_REPOSITORY::package_list, PCM_REPOSITORY::package_map, PCM_INSTALLATION_ENTRY::repository_id, tracePcm, and PCM_PACKAGE::versions.
Referenced by CacheRepository().
void PLUGIN_CONTENT_MANAGER::ValidateJson | ( | const nlohmann::json & | aJson, |
const nlohmann::json_uri & | aUri = nlohmann::json_uri( "#" ) |
||
) | const |
Validates json against a specific definition in the PCM schema.
aJson | JSON object to validate |
aUri | JSON URI of a definition to validate against, default is root |
std::invalid_argument | on validation failure |
Definition at line 313 of file pcm.cpp.
References m_schema_validator.
Referenced by fetchPackages(), and FetchRepository().
bool PLUGIN_CONTENT_MANAGER::VerifyHash | ( | std::istream & | aStream, |
const wxString & | aHash | ||
) | const |
Verifies SHA256 hash of a binary stream.
aStream | input stream |
aHash | sha256 lowercase hex string |
Definition at line 370 of file pcm.cpp.
Referenced by CacheRepository(), and fetchPackages().
|
staticconstexprprivate |
|
private |
Definition at line 397 of file pcm.h.
Referenced by Get3rdPartyPath(), GetInstalledPackageBitmaps(), PLUGIN_CONTENT_MANAGER(), and ReadEnvVar().
|
private |
Definition at line 404 of file pcm.h.
Referenced by RunBackgroundUpdate().
|
private |
Definition at line 395 of file pcm.h.
Referenced by CacheRepository(), DownloadToStream(), fetchPackages(), FetchRepository(), and SetDialogWindow().
|
private |
Definition at line 402 of file pcm.h.
Referenced by GetInstalledPackages(), GetInstalledPackageVersion(), GetPackageState(), GetPackageUpdateVersion(), IsPackagePinned(), MarkInstalled(), MarkUninstalled(), PLUGIN_CONTENT_MANAGER(), RunBackgroundUpdate(), SaveInstalledPackages(), SetPinned(), and updateInstalledPackagesMetadata().
|
staticprivate |
Definition at line 403 of file pcm.h.
Referenced by PreparePackage().
|
private |
Definition at line 399 of file pcm.h.
Referenced by CacheRepository(), DiscardRepositoryCache(), getCachedRepository(), RunBackgroundUpdate(), and SetRepositoryList().
|
private |
Definition at line 400 of file pcm.h.
Referenced by CacheRepository(), GetRepositoryList(), RunBackgroundUpdate(), and SetRepositoryList().
|
private |
Definition at line 396 of file pcm.h.
Referenced by PLUGIN_CONTENT_MANAGER(), and ValidateJson().
|
private |
Definition at line 407 of file pcm.h.
Referenced by CacheRepository(), RunBackgroundUpdate(), and StopBackgroundUpdate().
|
private |
Definition at line 405 of file pcm.h.
Referenced by RunBackgroundUpdate(), and StopBackgroundUpdate().