KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcm.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) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
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#ifndef PCM_H_
22#define PCM_H_
23
24#include "core/wx_stl_compat.h"
25#include "pcm_data.h"
28#include <functional>
29#include <iostream>
30#include <map>
31
32#include <thread>
33#include <tuple>
34#include <unordered_map>
35#include <unordered_set>
36#include <vector>
37#include <wx/wx.h>
38
39
41const std::unordered_set<wxString> PCM_PACKAGE_DIRECTORIES( {
42 "plugins",
43 "footprints",
44 "3dmodels",
45 "symbols",
46 "resources",
47 "colors",
48 "templates",
49 "scripts",
50 "design_blocks"
51} );
52
53
68
69
77
78
79typedef std::vector<std::pair<wxString, wxString>> STRING_PAIR_LIST;
80typedef std::vector<std::tuple<wxString, wxString, wxString>> STRING_TUPLE_LIST;
81
82
83struct BACKGROUND_JOB;
84class DIALOG_PCM;
85
86
107{
108public:
109 PLUGIN_CONTENT_MANAGER( std::function<void( int )> aAvailableUpdateCallbac );
111
118
128 bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository,
129 PROGRESS_REPORTER* aReporter );
130
138 void ValidateJson( const nlohmann::json& aJson,
139 const nlohmann::json_uri& aUri = nlohmann::json_uri( "#" ) ) const;
140
149 void ValidateJson( const nlohmann::json& aJson,
150 const JSON_SCHEMA_VALIDATOR& aValidator,
151 const nlohmann::json_uri& aUri ) const;
152
160 bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
161
170 void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
171
177 void DiscardRepositoryCache( const wxString& aRepositoryId );
179
188 void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
189 const wxString& aRepositoryId );
190
196 void MarkUninstalled( const PCM_PACKAGE& aPackage );
197
204
220 bool CacheRepository( const wxString& aRepositoryId );
221
230 const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
231
237 const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
238
246 const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
247
249 const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
250
258 PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
259
276 static wxString ResolveRepositoryId( const wxString& aPackageId, const wxString& aRecordedName,
277 const STRING_TUPLE_LIST& aRepositoryList,
278 const std::unordered_map<wxString, PCM_REPOSITORY>& aCache );
279
288
292 static bool UsesSWIGRuntime( const PCM_PACKAGE& aPackage, const wxString& aVersion );
293
301 bool IsPackagePinned( const wxString& aPackageId ) const;
302
311 void SetPinned( const wxString& aPackageId, const bool aPinned );
312
323 const wxString GetPackageUpdateVersion( const PCM_PACKAGE& aPackage );
324
336 bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
337 PROGRESS_REPORTER* aReporter,
338 const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT,
339 const std::string& aAccept = "" );
340
348 int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
349
358 std::unordered_map<wxString, wxBitmap>
359 GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
360
369 std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
370
380 void SetDialogWindow( DIALOG_PCM* aDialog ) { m_dialog = aDialog; };
381
385 void RunBackgroundUpdate();
386
391
395 void ReadEnvVar();
396
405 static void PreparePackage( PCM_PACKAGE& aPackage );
406
408
409private:
411 static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
412
423 bool fetchPackages( const wxString& aUrl, const std::optional<wxString>& aHash,
424 std::vector<PCM_PACKAGE>& aPackages, PROGRESS_REPORTER* aReporter,
425 int aSchemaVersion = 1 );
426
433 STRING_TUPLE_LIST::const_iterator findRepository( const wxString& aRepositoryId ) const;
434
441 const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
442
450
461 void updateInstalledPackagesMetadata( const wxString& aRepositoryId );
462
464 time_t getCurrentTimestamp() const;
465
467 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_v1_validator;
468 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_v2_validator;
470 wxString m_cache_path;
471 std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
472 STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
473
474 // Using sorted map to keep order of entries in installed list stable
475 std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
476 const static std::tuple<int, int, int> m_kicad_version;
477 std::function<void( int )> m_availableUpdateCallback;
478 std::thread m_updateThread;
479
480 std::shared_ptr<BACKGROUND_JOB> m_updateBackgroundJob;
482};
483
484#endif // PCM_H_
Implementing pcm main dialog.
Definition dialog_pcm.h:39
Code outside of kicommon must validate through this class rather than instantiating nlohmann::json_sc...
time_t getCurrentTimestamp() const
Returns current UTC timestamp.
Definition pcm.cpp:1054
const std::vector< PCM_PACKAGE > & GetRepositoryPackages(const wxString &aRepositoryId) const
Get the packages metadata from a previously cached repository.
Definition pcm.cpp:763
void SetRepositoryList(const STRING_PAIR_LIST &aRepositories)
Set list of repositories.
Definition pcm.cpp:778
DIALOG_PCM * m_dialog
Definition pcm.h:466
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_v1_validator
Definition pcm.h:467
bool DownloadToStream(const wxString &aUrl, std::ostream *aOutput, PROGRESS_REPORTER *aReporter, const size_t aSizeLimit=DEFAULT_DOWNLOAD_MEM_LIMIT, const std::string &aAccept="")
Downloads url to an output stream.
Definition pcm.cpp:202
void DiscardAllRepositoryCaches()
Definition pcm.cpp:826
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.
Definition pcm.cpp:324
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition pcm.h:471
bool m_apiEnablePromptNeeded
Definition pcm.h:481
const PCM_REPOSITORY & getCachedRepository(const wxString &aRepositoryId) const
Get the cached repository metadata.
Definition pcm.cpp:421
int GetPackageSearchRank(const PCM_PACKAGE &aPackage, const wxString &aSearchTerm)
Get the approximate measure of how much given package matches the search term.
Definition pcm.cpp:1135
const STRING_TUPLE_LIST & GetRepositoryList() const
Get list of repositories.
Definition pcm.h:203
void SetDialogWindow(DIALOG_PCM *aDialog)
Set the Dialog Window.
Definition pcm.h:380
const wxString & Get3rdPartyPath() const
Returns current 3rd party directory path.
Definition pcm.h:249
void MarkUninstalled(const PCM_PACKAGE &aPackage)
Mark package as uninstalled.
Definition pcm.cpp:893
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_v2_validator
Definition pcm.h:468
bool CacheRepository(const wxString &aRepositoryId)
Cache specified repository packages and other metadata.
Definition pcm.cpp:430
void SaveInstalledPackages()
Saves metadata of installed packages to disk.
Definition pcm.cpp:1061
const std::vector< PCM_INSTALLATION_ENTRY > GetInstalledPackages() const
Get list of installed packages.
Definition pcm.cpp:1085
wxString m_3rdparty_path
Definition pcm.h:469
void SetPinned(const wxString &aPackageId, const bool aPinned)
Set the pinned status of a package.
Definition pcm.cpp:1126
wxString m_cache_path
Definition pcm.h:470
static void PreparePackage(PCM_PACKAGE &aPackage)
Parses version strings and calculates compatibility.
Definition pcm.cpp:663
PLUGIN_CONTENT_MANAGER(std::function< void(int)> aAvailableUpdateCallbac)
Definition pcm.cpp:79
bool fetchPackages(const wxString &aUrl, const std::optional< wxString > &aHash, std::vector< PCM_PACKAGE > &aPackages, PROGRESS_REPORTER *aReporter, int aSchemaVersion=1)
Downloads packages metadata to in memory stream, verifies hash and attempts to parse it.
Definition pcm.cpp:341
std::thread m_updateThread
Definition pcm.h:478
PCM_PACKAGE_STATE GetPackageState(const wxString &aRepositoryId, const wxString &aPackageId)
Get current state of the package.
Definition pcm.cpp:899
STRING_TUPLE_LIST::const_iterator findRepository(const wxString &aRepositoryId) const
Find a configured repository by id.
Definition pcm.cpp:410
std::map< wxString, PCM_INSTALLATION_ENTRY > m_installed
Definition pcm.h:475
void RunBackgroundUpdate()
Runs a background update thread that checks for new package versions.
Definition pcm.cpp:1298
std::unordered_map< wxString, wxBitmap > GetRepositoryPackageBitmaps(const wxString &aRepositoryId)
Get the icon bitmaps for repository packages.
Definition pcm.cpp:1196
const wxString GetPackageUpdateVersion(const PCM_PACKAGE &aPackage)
Get the preferred package update version or empty string if there is none.
Definition pcm.cpp:1026
void MarkInstalled(const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId)
Mark package as installed.
Definition pcm.cpp:848
static bool UsesSWIGRuntime(const PCM_PACKAGE &aPackage, const wxString &aVersion)
Returns true if the selected package version requires SWIG.
Definition pcm.cpp:744
std::unordered_map< wxString, wxBitmap > GetInstalledPackageBitmaps()
Get the icon bitmaps for installed packages.
Definition pcm.cpp:1239
const wxString & GetInstalledPackageVersion(const wxString &aPackageId) const
Get the current version of an installed package.
Definition pcm.cpp:1108
void updateInstalledPackagesMetadata(const wxString &aRepositoryId)
Updates metadata of installed packages from freshly fetched repo.
Definition pcm.cpp:599
bool IsPackagePinned(const wxString &aPackageId) const
Returns pinned status of a package.
Definition pcm.cpp:1117
static const std::tuple< int, int, int > m_kicad_version
Definition pcm.h:476
std::shared_ptr< BACKGROUND_JOB > m_updateBackgroundJob
Definition pcm.h:480
void StopBackgroundUpdate()
Interrupts and joins() the update thread.
Definition pcm.cpp:1373
static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT
Default download limit of 10 Mb to not use too much memory.
Definition pcm.h:411
STRING_TUPLE_LIST m_repository_list
Definition pcm.h:472
std::function< void(int)> m_availableUpdateCallback
Definition pcm.h:477
static wxString ResolveRepositoryId(const wxString &aPackageId, const wxString &aRecordedName, const STRING_TUPLE_LIST &aRepositoryList, const std::unordered_map< wxString, PCM_REPOSITORY > &aCache)
Find the cached repository that publishes a given package.
Definition pcm.cpp:945
bool FetchRepository(const wxString &aUrl, PCM_REPOSITORY &aRepository, PROGRESS_REPORTER *aReporter)
Fetches repository metadata from given url.
Definition pcm.cpp:270
void ResolveInstalledPackageRepositories()
Repair the repository id of every installed package.
Definition pcm.cpp:1019
void ShowApiEnablePromptIfNeeded()
Definition pcm.cpp:1393
void DiscardRepositoryCache(const wxString &aRepositoryId)
Discard in-memory and on-disk cache of a repository.
Definition pcm.cpp:807
bool VerifyHash(std::istream &aStream, const wxString &aHash) const
Verifies SHA256 hash of a binary stream.
Definition pcm.cpp:397
void ReadEnvVar()
Stores 3rdparty path from environment variables.
Definition pcm.cpp:190
bool resolveInstalledPackageRepository(PCM_INSTALLATION_ENTRY &aEntry)
Repair the repository id of a single installed package.
Definition pcm.cpp:980
A progress reporter interface for use in multi-threaded environments.
std::vector< std::pair< wxString, wxString > > STRING_PAIR_LIST
Definition pcm.h:79
const std::unordered_set< wxString > PCM_PACKAGE_DIRECTORIES({ "plugins", "footprints", "3dmodels", "symbols", "resources", "colors", "templates", "scripts", "design_blocks" })
Contains list of all valid directories that get extracted from a package archive.
PCM_PACKAGE_STATE
Package states Package is available if it is not installed and there is a compatible version Package ...
Definition pcm.h:59
@ PPS_INSTALLED
Definition pcm.h:62
@ PPS_PENDING_UPDATE
Definition pcm.h:66
@ PPS_UNAVAILABLE
Definition pcm.h:61
@ PPS_UPDATE_AVAILABLE
Definition pcm.h:65
@ PPS_PENDING_UNINSTALL
Definition pcm.h:64
@ PPS_PENDING_INSTALL
Definition pcm.h:63
@ PPS_AVAILABLE
Definition pcm.h:60
PCM_PACKAGE_ACTION
Package actions.
Definition pcm.h:72
@ PPA_UNINSTALL
Definition pcm.h:74
@ PPA_UPDATE
Definition pcm.h:75
@ PPA_INSTALL
Definition pcm.h:73
std::vector< std::tuple< wxString, wxString, wxString > > STRING_TUPLE_LIST
Definition pcm.h:80
Package installation entry.
Definition pcm_data.h:159
Package metadata.
Definition pcm_data.h:114
Repository metadata.
Definition pcm_data.h:142