KiCad PCB EDA Suite
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 <iostream>
28 #include <map>
29 #include <nlohmann/json-schema.hpp>
30 #include <tuple>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include <vector>
34 #include <wx/wx.h>
35 
36 
38 const std::unordered_set<wxString> PCM_PACKAGE_DIRECTORIES( {
39  wxT( "plugins" ),
40  wxT( "footprints" ),
41  wxT( "3dmodels" ),
42  wxT( "symbols" ),
43  wxT( "resources" ),
44  wxT( "colors" ),
45 } );
46 
47 
53 {
59 };
60 
61 
64 {
67 };
68 
69 
70 typedef std::vector<std::pair<wxString, wxString>> STRING_PAIR_LIST;
71 typedef std::vector<std::tuple<wxString, wxString, wxString>> STRING_TUPLE_LIST;
72 
73 
94 {
95 public:
96  PLUGIN_CONTENT_MANAGER( wxWindow* aParent );
98 
108  bool FetchRepository( const wxString& aUrl, PCM_REPOSITORY& aRepository,
109  WX_PROGRESS_REPORTER* aReporter );
110 
118  void ValidateJson( const nlohmann::json& aJson,
119  const nlohmann::json_uri& aUri = nlohmann::json_uri( "#" ) ) const;
120 
128  bool VerifyHash( std::istream& aStream, const wxString& aHash ) const;
129 
138  void SetRepositoryList( const STRING_PAIR_LIST& aRepositories );
139 
145  void DiscardRepositoryCache( const wxString& aRepositoryId );
146 
155  void MarkInstalled( const PCM_PACKAGE& aPackage, const wxString& aVersion,
156  const wxString& aRepositoryId );
157 
163  void MarkUninstalled( const PCM_PACKAGE& aPackage );
164 
171 
187  const bool CacheRepository( const wxString& aRepositoryId );
188 
197  const std::vector<PCM_PACKAGE>& GetRepositoryPackages( const wxString& aRepositoryId ) const;
198 
204  const std::vector<PCM_INSTALLATION_ENTRY> GetInstalledPackages() const;
205 
213  const wxString& GetInstalledPackageVersion( const wxString& aPackageId ) const;
214 
216  const wxString& Get3rdPartyPath() const { return m_3rdparty_path; };
217 
225  PCM_PACKAGE_STATE GetPackageState( const wxString& aRepositoryId, const wxString& aPackageId );
226 
237  bool DownloadToStream( const wxString& aUrl, std::ostream* aOutput,
238  WX_PROGRESS_REPORTER* aReporter,
239  const size_t aSizeLimit = DEFAULT_DOWNLOAD_MEM_LIMIT );
240 
248  int GetPackageSearchRank( const PCM_PACKAGE& aPackage, const wxString& aSearchTerm );
249 
258  std::unordered_map<wxString, wxBitmap>
259  GetRepositoryPackageBitmaps( const wxString& aRepositoryId );
260 
269  std::unordered_map<wxString, wxBitmap> GetInstalledPackageBitmaps();
270 
271 private:
273  static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT = 10 * 1024 * 1024;
274 
284  bool fetchPackages( const wxString& aUrl, const boost::optional<wxString>& aHash,
285  std::vector<PCM_PACKAGE>& aPackages, WX_PROGRESS_REPORTER* aReporter );
286 
293  const PCM_REPOSITORY& getCachedRepository( const wxString& aRepositoryId ) const;
294 
303  static void preparePackage( PCM_PACKAGE& aPackage );
304 
306  time_t getCurrentTimestamp() const;
307 
308  wxWindow* m_dialog;
309  nlohmann::json_schema::json_validator m_schema_validator;
310  wxString m_3rdparty_path;
311  wxString m_cache_path;
312  std::unordered_map<wxString, PCM_REPOSITORY> m_repository_cache;
313  STRING_TUPLE_LIST m_repository_list; // (id, name, url) tuples
314  // Using sorted map to keep order of entries in installed list stable
315  std::map<wxString, PCM_INSTALLATION_ENTRY> m_installed;
316  const static std::tuple<int, int> m_kicad_version;
317 };
318 
319 #endif // PCM_H_
std::unordered_map< wxString, wxBitmap > GetRepositoryPackageBitmaps(const wxString &aRepositoryId)
Get the icon bitmaps for repository packages.
Definition: pcm.cpp:781
const wxString & GetInstalledPackageVersion(const wxString &aPackageId) const
Get the current version of an installed package.
Definition: pcm.cpp:714
wxString m_cache_path
Definition: pcm.h:311
PLUGIN_CONTENT_MANAGER(wxWindow *aParent)
Definition: pcm.cpp:63
std::map< wxString, PCM_INSTALLATION_ENTRY > m_installed
Definition: pcm.h:315
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:723
void DiscardRepositoryCache(const wxString &aRepositoryId)
Discard in-memory and on-disk cache of a repository.
Definition: pcm.cpp:591
void MarkUninstalled(const PCM_PACKAGE &aPackage)
Mark package as uninstalled.
Definition: pcm.cpp:624
nlohmann::json json
Definition: gerbview.cpp:41
STRING_TUPLE_LIST m_repository_list
Definition: pcm.h:313
bool fetchPackages(const wxString &aUrl, const boost::optional< wxString > &aHash, std::vector< PCM_PACKAGE > &aPackages, WX_PROGRESS_REPORTER *aReporter)
Downloads packages metadata to in memory stream, verifies hash and attempts to parse it.
Definition: pcm.cpp:283
static const std::tuple< int, int > m_kicad_version
Definition: pcm.h:316
bool FetchRepository(const wxString &aUrl, PCM_REPOSITORY &aRepository, WX_PROGRESS_REPORTER *aReporter)
Fetches repository metadata from given url.
Definition: pcm.cpp:242
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
void MarkInstalled(const PCM_PACKAGE &aPackage, const wxString &aVersion, const wxString &aRepositoryId)
Mark package as installed.
Definition: pcm.cpp:605
bool VerifyHash(std::istream &aStream, const wxString &aHash) const
Verifies SHA256 hash of a binary stream.
Definition: pcm.cpp:323
static constexpr size_t DEFAULT_DOWNLOAD_MEM_LIMIT
< Default download limit of 10 Mb to not use too much memory
Definition: pcm.h:273
Repository reference to a resource.
Definition: pcm_data.h:93
const std::vector< PCM_PACKAGE > & GetRepositoryPackages(const wxString &aRepositoryId) const
Get the packages metadata from a previously cached repository.
Definition: pcm.cpp:556
const PCM_REPOSITORY & getCachedRepository(const wxString &aRepositoryId) const
Get the cached repository metadata.
Definition: pcm.cpp:336
const bool CacheRepository(const wxString &aRepositoryId)
Cache specified repository packages and other metadata.
Definition: pcm.cpp:345
std::unordered_map< wxString, wxBitmap > GetInstalledPackageBitmaps()
Get the icon bitmaps for installed packages.
Definition: pcm.cpp:824
const wxString & Get3rdPartyPath() const
Definition: pcm.h:216
wxString m_3rdparty_path
Definition: pcm.h:310
wxWindow * m_dialog
Definition: pcm.h:308
static void preparePackage(PCM_PACKAGE &aPackage)
Parses version strings and calculates compatibility.
Definition: pcm.cpp:488
std::vector< std::pair< wxString, wxString > > STRING_PAIR_LIST
Definition: pcm.h:70
const std::vector< PCM_INSTALLATION_ENTRY > GetInstalledPackages() const
Get list of installed packages.
Definition: pcm.cpp:699
PCM_PACKAGE_STATE
Definition: pcm.h:52
const STRING_TUPLE_LIST & GetRepositoryList() const
Get list of repositories.
Definition: pcm.h:170
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:275
PCM_PACKAGE_STATE GetPackageState(const wxString &aRepositoryId, const wxString &aPackageId)
Get current state of the package.
Definition: pcm.cpp:630
nlohmann::json_schema::json_validator m_schema_validator
Definition: pcm.h:309
std::vector< std::tuple< wxString, wxString, wxString > > STRING_TUPLE_LIST
Definition: pcm.h:71
void SetRepositoryList(const STRING_PAIR_LIST &aRepositories)
Set list of repositories.
Definition: pcm.cpp:562
Main class of Plugin and Content Manager subsystem.
Definition: pcm.h:93
const std::unordered_set< wxString > PCM_PACKAGE_DIRECTORIES({ wxT("plugins"), wxT("footprints"), wxT("3dmodels"), wxT("symbols"), wxT("resources"), wxT("colors"), })
< Contains list of all valid directories that get extracted from a package archive
Package installation entry.
Definition: pcm_data.h:119
bool DownloadToStream(const wxString &aUrl, std::ostream *aOutput, WX_PROGRESS_REPORTER *aReporter, const size_t aSizeLimit=DEFAULT_DOWNLOAD_MEM_LIMIT)
Downloads url to an output stream.
Definition: pcm.cpp:187
time_t getCurrentTimestamp() const
Definition: pcm.cpp:665
PCM_PACKAGE_ACTION
Definition: pcm.h:63
std::unordered_map< wxString, PCM_REPOSITORY > m_repository_cache
Definition: pcm.h:312