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 (C) 1992-2021 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"
27#include <functional>
28#include <iostream>
29#include <map>
30#include <json_common.h>
31#include <nlohmann/json-schema.hpp>
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
58{
66};
67
68
71{
75};
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;
83
84
105{
106public:
107 PLUGIN_CONTENT_MANAGER( std::function<void( int )> aAvailableUpdateCallbac );
109
116
126 bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository,
127 PROGRESS_REPORTER* aReporter );
128
136 void ValidateJson( const nlohmann::json& aJson,
137 const nlohmann::json_uri& aUri = nlohmann::json_uri( "#" ) ) const;
138
146 bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
147
156 void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
157
163 void DiscardRepositoryCache( const wxString& aRepositoryId );
164
173 void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
174 const wxString& aRepositoryId );
175
181 void MarkUninstalled( const PCM_PACKAGE& aPackage );
182
189
205 bool CacheRepository( const wxString& aRepositoryId );
206
215 const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
216
222 const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
223
231 const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
232
234 const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
235
243 PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
244
252 bool IsPackagePinned( const wxString& aPackageId ) const;
253
262 void SetPinned( const wxString& aPackageId, const bool aPinned );
263
274 const wxString GetPackageUpdateVersion( const PCM_PACKAGE& aPackage );
275
286 bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
287 PROGRESS_REPORTER* aReporter,
288 const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT );
289
297 int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
298
307 std::unordered_map<wxString, wxBitmap>
308 GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
309
318 std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
319
329 void SetDialogWindow( wxWindow* aDialog ) { m_dialog = aDialog; };
330
334 void RunBackgroundUpdate();
335
340
344 void ReadEnvVar();
345
354 static void PreparePackage( PCM_PACKAGE& aPackage );
355
356private:
358 static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
359
369 bool fetchPackages( const wxString& aUrl, const std::optional<wxString>& aHash,
370 std::vector<PCM_PACKAGE>& aPackages, PROGRESS_REPORTER* aReporter );
371
378 const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
379
390 void updateInstalledPackagesMetadata( const wxString& aRepositoryId );
391
393 time_t getCurrentTimestamp() const;
394
395 wxWindow* m_dialog;
396 nlohmann::json_schema::json_validator m_schema_validator;
398 wxString m_cache_path;
399 std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
400 STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
401 // Using sorted map to keep order of entries in installed list stable
402 std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
403 const static std::tuple<int, int, int> m_kicad_version;
404 std::function<void( int )> m_availableUpdateCallback;
405 std::thread m_updateThread;
406
407 std::shared_ptr<BACKGROUND_JOB> m_updateBackgroundJob;
408};
409
410#endif // PCM_H_
Main class of Plugin and Content Manager subsystem.
Definition: pcm.h:105
time_t getCurrentTimestamp() const
Definition: pcm.cpp:869
const std::vector< PCM_PACKAGE > & GetRepositoryPackages(const wxString &aRepositoryId) const
Get the packages metadata from a previously cached repository.
Definition: pcm.cpp:695
void SetRepositoryList(const STRING_PAIR_LIST &aRepositories)
Set list of repositories.
Definition: pcm.cpp:710
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:299
wxWindow * m_dialog
Definition: pcm.h:395
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition: pcm.h:399
const PCM_REPOSITORY & getCachedRepository(const wxString &aRepositoryId) const
Get the cached repository metadata.
Definition: pcm.cpp:369
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:950
const STRING_TUPLE_LIST & GetRepositoryList() const
Get list of repositories.
Definition: pcm.h:188
const wxString & Get3rdPartyPath() const
Definition: pcm.h:234
void MarkUninstalled(const PCM_PACKAGE &aPackage)
Mark package as uninstalled.
Definition: pcm.cpp:789
bool CacheRepository(const wxString &aRepositoryId)
Cache specified repository packages and other metadata.
Definition: pcm.cpp:378
void SaveInstalledPackages()
Saves metadata of installed packages to disk.
Definition: pcm.cpp:876
const std::vector< PCM_INSTALLATION_ENTRY > GetInstalledPackages() const
Get list of installed packages.
Definition: pcm.cpp:900
wxString m_3rdparty_path
Definition: pcm.h:397
void SetPinned(const wxString &aPackageId, const bool aPinned)
Set the pinned status of a package.
Definition: pcm.cpp:941
wxString m_cache_path
Definition: pcm.h:398
static void PreparePackage(PCM_PACKAGE &aPackage)
Parses version strings and calculates compatibility.
Definition: pcm.cpp:615
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.
Definition: pcm.cpp:200
std::thread m_updateThread
Definition: pcm.h:405
PCM_PACKAGE_STATE GetPackageState(const wxString &aRepositoryId, const wxString &aPackageId)
Get current state of the package.
Definition: pcm.cpp:795
std::map< wxString, PCM_INSTALLATION_ENTRY > m_installed
Definition: pcm.h:402
void RunBackgroundUpdate()
Runs a background update thread that checks for new package versions.
Definition: pcm.cpp:1097
std::unordered_map< wxString, wxBitmap > GetRepositoryPackageBitmaps(const wxString &aRepositoryId)
Get the icon bitmaps for repository packages.
Definition: pcm.cpp:1011
const wxString GetPackageUpdateVersion(const PCM_PACKAGE &aPackage)
Get the preferred package update version or empty string if there is none.
Definition: pcm.cpp:841
void MarkInstalled(const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId)
Mark package as installed.
Definition: pcm.cpp:753
nlohmann::json_schema::json_validator m_schema_validator
Definition: pcm.h:396
std::unordered_map< wxString, wxBitmap > GetInstalledPackageBitmaps()
Get the icon bitmaps for installed packages.
Definition: pcm.cpp:1054
void SetDialogWindow(wxWindow *aDialog)
Set the Dialog Window.
Definition: pcm.h:329
const wxString & GetInstalledPackageVersion(const wxString &aPackageId) const
Get the current version of an installed package.
Definition: pcm.cpp:923
void updateInstalledPackagesMetadata(const wxString &aRepositoryId)
Updates metadata of installed packages from freshly fetched repo.
Definition: pcm.cpp:551
bool IsPackagePinned(const wxString &aPackageId) const
Returns pinned status of a package.
Definition: pcm.cpp:932
static const std::tuple< int, int, int > m_kicad_version
Definition: pcm.h:403
std::shared_ptr< BACKGROUND_JOB > m_updateBackgroundJob
Definition: pcm.h:407
void StopBackgroundUpdate()
Interrupts and joins() the update thread.
Definition: pcm.cpp:1174
static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT
< Default download limit of 10 Mb to not use too much memory
Definition: pcm.h:358
STRING_TUPLE_LIST m_repository_list
Definition: pcm.h:400
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.
Definition: pcm.cpp:307
std::function< void(int)> m_availableUpdateCallback
Definition: pcm.h:404
bool FetchRepository(const wxString &aUrl, PCM_REPOSITORY &aRepository, PROGRESS_REPORTER *aReporter)
Fetches repository metadata from given url.
Definition: pcm.cpp:258
void DiscardRepositoryCache(const wxString &aRepositoryId)
Discard in-memory and on-disk cache of a repository.
Definition: pcm.cpp:739
bool VerifyHash(std::istream &aStream, const wxString &aHash) const
Verifies SHA256 hash of a binary stream.
Definition: pcm.cpp:356
void ReadEnvVar()
Stores 3rdparty path from environment variables.
Definition: pcm.cpp:188
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:96
Package installation entry.
Definition: pcm_data.h:123