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 <nlohmann/json-schema.hpp>
31#include <thread>
32#include <tuple>
33#include <unordered_map>
34#include <unordered_set>
35#include <vector>
36#include <wx/wx.h>
37
38
40const std::unordered_set<wxString> PCM_PACKAGE_DIRECTORIES( {
41 "plugins",
42 "footprints",
43 "3dmodels",
44 "symbols",
45 "resources",
46 "colors",
47 "templates",
48 "scripts"
49} );
50
51
57{
65};
66
67
70{
74};
75
76
77typedef std::vector<std::pair<wxString, wxString>> STRING_PAIR_LIST;
78typedef std::vector<std::tuple<wxString, wxString, wxString>> STRING_TUPLE_LIST;
79
80
81struct BACKGROUND_JOB;
82
83
104{
105public:
106 PLUGIN_CONTENT_MANAGER( std::function<void( int )> aAvailableUpdateCallbac );
108
115
125 bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository,
126 PROGRESS_REPORTER* aReporter );
127
135 void ValidateJson( const nlohmann::json& aJson,
136 const nlohmann::json_uri& aUri = nlohmann::json_uri( "#" ) ) const;
137
145 bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
146
155 void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
156
162 void DiscardRepositoryCache( const wxString& aRepositoryId );
163
172 void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
173 const wxString& aRepositoryId );
174
180 void MarkUninstalled( const PCM_PACKAGE& aPackage );
181
188
204 bool CacheRepository( const wxString& aRepositoryId );
205
214 const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
215
221 const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
222
230 const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
231
233 const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
234
242 PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
243
251 bool IsPackagePinned( const wxString& aPackageId ) const;
252
261 void SetPinned( const wxString& aPackageId, const bool aPinned );
262
273 const wxString GetPackageUpdateVersion( const PCM_PACKAGE& aPackage );
274
285 bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
286 PROGRESS_REPORTER* aReporter,
287 const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT );
288
296 int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
297
306 std::unordered_map<wxString, wxBitmap>
307 GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
308
317 std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
318
328 void SetDialogWindow( wxWindow* aDialog ) { m_dialog = aDialog; };
329
333 void RunBackgroundUpdate();
334
339
343 void ReadEnvVar();
344
353 static void PreparePackage( PCM_PACKAGE& aPackage );
354
355private:
357 static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
358
368 bool fetchPackages( const wxString& aUrl, const std::optional<wxString>& aHash,
369 std::vector<PCM_PACKAGE>& aPackages, PROGRESS_REPORTER* aReporter );
370
377 const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
378
389 void updateInstalledPackagesMetadata( const wxString& aRepositoryId );
390
392 time_t getCurrentTimestamp() const;
393
394 wxWindow* m_dialog;
395 nlohmann::json_schema::json_validator m_schema_validator;
397 wxString m_cache_path;
398 std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
399 STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
400 // Using sorted map to keep order of entries in installed list stable
401 std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
402 const static std::tuple<int, int, int> m_kicad_version;
403 std::function<void( int )> m_availableUpdateCallback;
404 std::thread m_updateThread;
405
406 std::shared_ptr<BACKGROUND_JOB> m_updateBackgroundJob;
407};
408
409#endif // PCM_H_
Main class of Plugin and Content Manager subsystem.
Definition: pcm.h:104
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:394
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition: pcm.h:398
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:187
const wxString & Get3rdPartyPath() const
Definition: pcm.h:233
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:396
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:397
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:404
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:401
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:395
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:328
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:402
std::shared_ptr< BACKGROUND_JOB > m_updateBackgroundJob
Definition: pcm.h:406
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:357
STRING_TUPLE_LIST m_repository_list
Definition: pcm.h:399
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:403
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:187
A progress reporter interface for use in multi-threaded environments.
std::vector< std::pair< wxString, wxString > > STRING_PAIR_LIST
Definition: pcm.h:77
PCM_PACKAGE_STATE
Definition: pcm.h:57
@ PPS_INSTALLED
Definition: pcm.h:60
@ PPS_PENDING_UPDATE
Definition: pcm.h:64
@ PPS_UNAVAILABLE
Definition: pcm.h:59
@ PPS_UPDATE_AVAILABLE
Definition: pcm.h:63
@ PPS_PENDING_UNINSTALL
Definition: pcm.h:62
@ PPS_PENDING_INSTALL
Definition: pcm.h:61
@ PPS_AVAILABLE
Definition: pcm.h:58
PCM_PACKAGE_ACTION
Definition: pcm.h:70
@ PPA_UNINSTALL
Definition: pcm.h:72
@ PPA_UPDATE
Definition: pcm.h:73
@ PPA_INSTALL
Definition: pcm.h:71
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:78
Repository reference to a resource.
Definition: pcm_data.h:96
Package installation entry.
Definition: pcm_data.h:123