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 along
18 * with this program. If not, see <http://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} );
51
52
67
68
76
77
78typedef std::vector<std::pair<wxString, wxString>> STRING_PAIR_LIST;
79typedef std::vector<std::tuple<wxString, wxString, wxString>> STRING_TUPLE_LIST;
80
81
82struct BACKGROUND_JOB;
83class DIALOG_PCM;
84
85
106{
107public:
108 PLUGIN_CONTENT_MANAGER( std::function<void( int )> aAvailableUpdateCallbac );
110
117
127 bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository,
128 PROGRESS_REPORTER* aReporter );
129
137 void ValidateJson( const nlohmann::json& aJson,
138 const nlohmann::json_uri& aUri = nlohmann::json_uri( "#" ) ) const;
139
148 void ValidateJson( const nlohmann::json& aJson,
149 const JSON_SCHEMA_VALIDATOR& aValidator,
150 const nlohmann::json_uri& aUri ) const;
151
159 bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
160
169 void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
170
176 void DiscardRepositoryCache( const wxString& aRepositoryId );
177
186 void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
187 const wxString& aRepositoryId );
188
194 void MarkUninstalled( const PCM_PACKAGE& aPackage );
195
202
218 bool CacheRepository( const wxString& aRepositoryId );
219
228 const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
229
235 const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
236
244 const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
245
247 const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
248
256 PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
257
265 bool IsPackagePinned( const wxString& aPackageId ) const;
266
275 void SetPinned( const wxString& aPackageId, const bool aPinned );
276
287 const wxString GetPackageUpdateVersion( const PCM_PACKAGE& aPackage );
288
300 bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
301 PROGRESS_REPORTER* aReporter,
302 const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT,
303 const std::string& aAccept = "" );
304
312 int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
313
322 std::unordered_map<wxString, wxBitmap>
323 GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
324
333 std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
334
344 void SetDialogWindow( DIALOG_PCM* aDialog ) { m_dialog = aDialog; };
345
349 void RunBackgroundUpdate();
350
355
359 void ReadEnvVar();
360
369 static void PreparePackage( PCM_PACKAGE& aPackage );
370
372
373private:
375 static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
376
386 bool fetchPackages( const wxString& aUrl, const std::optional<wxString>& aHash,
387 std::vector<PCM_PACKAGE>& aPackages, PROGRESS_REPORTER* aReporter,
388 int aSchemaVersion = 1 );
389
396 const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
397
408 void updateInstalledPackagesMetadata( const wxString& aRepositoryId );
409
411 time_t getCurrentTimestamp() const;
412
414 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_v1_validator;
415 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_v2_validator;
417 wxString m_cache_path;
418 std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
419 STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
420 // Using sorted map to keep order of entries in installed list stable
421 std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
422 const static std::tuple<int, int, int> m_kicad_version;
423 std::function<void( int )> m_availableUpdateCallback;
424 std::thread m_updateThread;
425
426 std::shared_ptr<BACKGROUND_JOB> m_updateBackgroundJob;
428};
429
430#endif // PCM_H_
Implementing pcm main dialog.
Definition dialog_pcm.h:38
time_t getCurrentTimestamp() const
Definition pcm.cpp:912
const std::vector< PCM_PACKAGE > & GetRepositoryPackages(const wxString &aRepositoryId) const
Get the packages metadata from a previously cached repository.
Definition pcm.cpp:729
void SetRepositoryList(const STRING_PAIR_LIST &aRepositories)
Set list of repositories.
Definition pcm.cpp:744
DIALOG_PCM * m_dialog
Definition pcm.h:413
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_v1_validator
Definition pcm.h:414
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 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:318
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition pcm.h:418
bool m_apiEnablePromptNeeded
Definition pcm.h:427
const PCM_REPOSITORY & getCachedRepository(const wxString &aRepositoryId) const
Get the cached repository metadata.
Definition pcm.cpp:404
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:993
const STRING_TUPLE_LIST & GetRepositoryList() const
Get list of repositories.
Definition pcm.h:201
void SetDialogWindow(DIALOG_PCM *aDialog)
Set the Dialog Window.
Definition pcm.h:344
const wxString & Get3rdPartyPath() const
Definition pcm.h:247
void MarkUninstalled(const PCM_PACKAGE &aPackage)
Mark package as uninstalled.
Definition pcm.cpp:832
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_v2_validator
Definition pcm.h:415
bool CacheRepository(const wxString &aRepositoryId)
Cache specified repository packages and other metadata.
Definition pcm.cpp:413
void SaveInstalledPackages()
Saves metadata of installed packages to disk.
Definition pcm.cpp:919
const std::vector< PCM_INSTALLATION_ENTRY > GetInstalledPackages() const
Get list of installed packages.
Definition pcm.cpp:943
wxString m_3rdparty_path
Definition pcm.h:416
void SetPinned(const wxString &aPackageId, const bool aPinned)
Set the pinned status of a package.
Definition pcm.cpp:984
wxString m_cache_path
Definition pcm.h:417
static void PreparePackage(PCM_PACKAGE &aPackage)
Parses version strings and calculates compatibility.
Definition pcm.cpp:651
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:335
std::thread m_updateThread
Definition pcm.h:424
PCM_PACKAGE_STATE GetPackageState(const wxString &aRepositoryId, const wxString &aPackageId)
Get current state of the package.
Definition pcm.cpp:838
std::map< wxString, PCM_INSTALLATION_ENTRY > m_installed
Definition pcm.h:421
void RunBackgroundUpdate()
Runs a background update thread that checks for new package versions.
Definition pcm.cpp:1156
std::unordered_map< wxString, wxBitmap > GetRepositoryPackageBitmaps(const wxString &aRepositoryId)
Get the icon bitmaps for repository packages.
Definition pcm.cpp:1054
const wxString GetPackageUpdateVersion(const PCM_PACKAGE &aPackage)
Get the preferred package update version or empty string if there is none.
Definition pcm.cpp:884
void MarkInstalled(const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId)
Mark package as installed.
Definition pcm.cpp:787
std::unordered_map< wxString, wxBitmap > GetInstalledPackageBitmaps()
Get the icon bitmaps for installed packages.
Definition pcm.cpp:1097
const wxString & GetInstalledPackageVersion(const wxString &aPackageId) const
Get the current version of an installed package.
Definition pcm.cpp:966
void updateInstalledPackagesMetadata(const wxString &aRepositoryId)
Updates metadata of installed packages from freshly fetched repo.
Definition pcm.cpp:587
bool IsPackagePinned(const wxString &aPackageId) const
Returns pinned status of a package.
Definition pcm.cpp:975
static const std::tuple< int, int, int > m_kicad_version
Definition pcm.h:422
std::shared_ptr< BACKGROUND_JOB > m_updateBackgroundJob
Definition pcm.h:426
void StopBackgroundUpdate()
Interrupts and joins() the update thread.
Definition pcm.cpp:1231
static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT
< Default download limit of 10 Mb to not use too much memory
Definition pcm.h:375
STRING_TUPLE_LIST m_repository_list
Definition pcm.h:419
std::function< void(int)> m_availableUpdateCallback
Definition pcm.h:423
bool FetchRepository(const wxString &aUrl, PCM_REPOSITORY &aRepository, PROGRESS_REPORTER *aReporter)
Fetches repository metadata from given url.
Definition pcm.cpp:264
void ShowApiEnablePromptIfNeeded()
Definition pcm.cpp:1251
void DiscardRepositoryCache(const wxString &aRepositoryId)
Discard in-memory and on-disk cache of a repository.
Definition pcm.cpp:773
bool VerifyHash(std::istream &aStream, const wxString &aHash) const
Verifies SHA256 hash of a binary stream.
Definition pcm.cpp:391
void ReadEnvVar()
Stores 3rdparty path from environment variables.
Definition pcm.cpp:190
A progress reporter interface for use in multi-threaded environments.
std::vector< std::pair< wxString, wxString > > STRING_PAIR_LIST
Definition pcm.h:78
PCM_PACKAGE_STATE
Definition pcm.h:58
@ PPS_INSTALLED
Definition pcm.h:61
@ PPS_PENDING_UPDATE
Definition pcm.h:65
@ PPS_UNAVAILABLE
Definition pcm.h:60
@ PPS_UPDATE_AVAILABLE
Definition pcm.h:64
@ PPS_PENDING_UNINSTALL
Definition pcm.h:63
@ PPS_PENDING_INSTALL
Definition pcm.h:62
@ PPS_AVAILABLE
Definition pcm.h:59
PCM_PACKAGE_ACTION
Definition pcm.h:71
@ PPA_UNINSTALL
Definition pcm.h:73
@ PPA_UPDATE
Definition pcm.h:74
@ PPA_INSTALL
Definition pcm.h:72
const std::unordered_set< wxString > PCM_PACKAGE_DIRECTORIES({ "plugins", "footprints", "3dmodels", "symbols", "resources", "colors", "templates", "scripts" })
< Contains list of all valid directories that get extracted from a package archive
std::vector< std::tuple< wxString, wxString, wxString > > STRING_TUPLE_LIST
Definition pcm.h:79
Repository reference to a resource.
Definition pcm_data.h:114
Package installation entry.
Definition pcm_data.h:142