KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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
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;
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
147 bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
148
157 void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
158
164 void DiscardRepositoryCache( const wxString& aRepositoryId );
165
174 void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
175 const wxString& aRepositoryId );
176
182 void MarkUninstalled( const PCM_PACKAGE& aPackage );
183
190
206 bool CacheRepository( const wxString& aRepositoryId );
207
216 const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
217
223 const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
224
232 const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
233
235 const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
236
244 PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
245
253 bool IsPackagePinned( const wxString& aPackageId ) const;
254
263 void SetPinned( const wxString& aPackageId, const bool aPinned );
264
275 const wxString GetPackageUpdateVersion( const PCM_PACKAGE& aPackage );
276
287 bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
288 PROGRESS_REPORTER* aReporter,
289 const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT );
290
298 int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
299
308 std::unordered_map<wxString, wxBitmap>
309 GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
310
319 std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
320
330 void SetDialogWindow( DIALOG_PCM* aDialog ) { m_dialog = aDialog; };
331
335 void RunBackgroundUpdate();
336
341
345 void ReadEnvVar();
346
355 static void PreparePackage( PCM_PACKAGE& aPackage );
356
357private:
359 static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
360
370 bool fetchPackages( const wxString& aUrl, const std::optional<wxString>& aHash,
371 std::vector<PCM_PACKAGE>& aPackages, PROGRESS_REPORTER* aReporter );
372
379 const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
380
391 void updateInstalledPackagesMetadata( const wxString& aRepositoryId );
392
394 time_t getCurrentTimestamp() const;
395
397 std::unique_ptr<JSON_SCHEMA_VALIDATOR> m_schema_validator;
399 wxString m_cache_path;
400 std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
401 STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
402 // Using sorted map to keep order of entries in installed list stable
403 std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
404 const static std::tuple<int, int, int> m_kicad_version;
405 std::function<void( int )> m_availableUpdateCallback;
406 std::thread m_updateThread;
407
408 std::shared_ptr<BACKGROUND_JOB> m_updateBackgroundJob;
409};
410
411#endif // PCM_H_
Implementing pcm main dialog.
Definition: dialog_pcm.h:38
Main class of Plugin and Content Manager subsystem.
Definition: pcm.h:106
time_t getCurrentTimestamp() const
Definition: pcm.cpp:871
const std::vector< PCM_PACKAGE > & GetRepositoryPackages(const wxString &aRepositoryId) const
Get the packages metadata from a previously cached repository.
Definition: pcm.cpp:682
void SetRepositoryList(const STRING_PAIR_LIST &aRepositories)
Set list of repositories.
Definition: pcm.cpp:697
DIALOG_PCM * m_dialog
Definition: pcm.h:396
std::unique_ptr< JSON_SCHEMA_VALIDATOR > m_schema_validator
Definition: pcm.h:397
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:288
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition: pcm.h:400
const PCM_REPOSITORY & getCachedRepository(const wxString &aRepositoryId) const
Get the cached repository metadata.
Definition: pcm.cpp:358
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:952
const STRING_TUPLE_LIST & GetRepositoryList() const
Get list of repositories.
Definition: pcm.h:189
void SetDialogWindow(DIALOG_PCM *aDialog)
Set the Dialog Window.
Definition: pcm.h:330
const wxString & Get3rdPartyPath() const
Definition: pcm.h:235
void MarkUninstalled(const PCM_PACKAGE &aPackage)
Mark package as uninstalled.
Definition: pcm.cpp:791
bool CacheRepository(const wxString &aRepositoryId)
Cache specified repository packages and other metadata.
Definition: pcm.cpp:367
void SaveInstalledPackages()
Saves metadata of installed packages to disk.
Definition: pcm.cpp:878
const std::vector< PCM_INSTALLATION_ENTRY > GetInstalledPackages() const
Get list of installed packages.
Definition: pcm.cpp:902
wxString m_3rdparty_path
Definition: pcm.h:398
void SetPinned(const wxString &aPackageId, const bool aPinned)
Set the pinned status of a package.
Definition: pcm.cpp:943
wxString m_cache_path
Definition: pcm.h:399
static void PreparePackage(PCM_PACKAGE &aPackage)
Parses version strings and calculates compatibility.
Definition: pcm.cpp:604
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:194
std::thread m_updateThread
Definition: pcm.h:406
PCM_PACKAGE_STATE GetPackageState(const wxString &aRepositoryId, const wxString &aPackageId)
Get current state of the package.
Definition: pcm.cpp:797
std::map< wxString, PCM_INSTALLATION_ENTRY > m_installed
Definition: pcm.h:403
void RunBackgroundUpdate()
Runs a background update thread that checks for new package versions.
Definition: pcm.cpp:1115
std::unordered_map< wxString, wxBitmap > GetRepositoryPackageBitmaps(const wxString &aRepositoryId)
Get the icon bitmaps for repository packages.
Definition: pcm.cpp:1013
const wxString GetPackageUpdateVersion(const PCM_PACKAGE &aPackage)
Get the preferred package update version or empty string if there is none.
Definition: pcm.cpp:843
void MarkInstalled(const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId)
Mark package as installed.
Definition: pcm.cpp:740
std::unordered_map< wxString, wxBitmap > GetInstalledPackageBitmaps()
Get the icon bitmaps for installed packages.
Definition: pcm.cpp:1056
const wxString & GetInstalledPackageVersion(const wxString &aPackageId) const
Get the current version of an installed package.
Definition: pcm.cpp:925
void updateInstalledPackagesMetadata(const wxString &aRepositoryId)
Updates metadata of installed packages from freshly fetched repo.
Definition: pcm.cpp:540
bool IsPackagePinned(const wxString &aPackageId) const
Returns pinned status of a package.
Definition: pcm.cpp:934
static const std::tuple< int, int, int > m_kicad_version
Definition: pcm.h:404
std::shared_ptr< BACKGROUND_JOB > m_updateBackgroundJob
Definition: pcm.h:408
void StopBackgroundUpdate()
Interrupts and joins() the update thread.
Definition: pcm.cpp:1190
static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT
< Default download limit of 10 Mb to not use too much memory
Definition: pcm.h:359
STRING_TUPLE_LIST m_repository_list
Definition: pcm.h:401
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:296
std::function< void(int)> m_availableUpdateCallback
Definition: pcm.h:405
bool FetchRepository(const wxString &aUrl, PCM_REPOSITORY &aRepository, PROGRESS_REPORTER *aReporter)
Fetches repository metadata from given url.
Definition: pcm.cpp:252
void DiscardRepositoryCache(const wxString &aRepositoryId)
Discard in-memory and on-disk cache of a repository.
Definition: pcm.cpp:726
bool VerifyHash(std::istream &aStream, const wxString &aHash) const
Verifies SHA256 hash of a binary stream.
Definition: pcm.cpp:345
void ReadEnvVar()
Stores 3rdparty path from environment variables.
Definition: pcm.cpp:182
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:113
Package installation entry.
Definition: pcm_data.h:141