OpenCPN Partial API docs
plugin_handler.h
1 /******************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Copyright (C) 2019 Alec Leamas *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  ***************************************************************************
23  */
24 
56 #ifndef PLUGIN_HANDLER_H__
57 #define PLUGIN_HANDLER_H__
58 
59 #include "config.h"
60 
61 #include <map>
62 #include <memory>
63 #include <string>
64 #include <unordered_map>
65 #include <vector>
66 
67 #include <wx/cmdline.h>
68 
69 #include <archive.h>
70 
71 #include "model/catalog_parser.h"
72 #include "observable_evtvar.h"
73 
74 bool isRegularFile(const char* path);
75 
76 class CompatOs {
77 public:
78  static CompatOs* getInstance();
79  std::string name() const { return _name; }
80  std::string version() const { return _version; }
81 
82 private:
83  CompatOs();
84  std::string _name;
85  std::string _version;
86 };
87 
89 public:
90 
91  EventVar evt_download_ok;
92  EventVar evt_download_failed;
93 
94  static PluginHandler* getInstance();
95 
97  static void cleanup(const std::string& filelist, const std::string& plugname);
98  static void cleanupFiles(const std::string& manifestFile,
99  const std::string& plugname);
100 
102  static std::string pluginsInstallDataPath();
103 
105  static std::string fileListPath(std::string name);
106 
108  static std::string versionPath(std::string name);
109 
111  static std::string ImportedMetadataPath(std::string name);
112 
114  static std::vector<std::string> GetImportPaths();
115 
117  static bool isCompatible(const PluginMetadata& metadata,
118  const char* os = PKG_TARGET,
119  const char* os_version = PKG_TARGET_VERSION);
120 
122  bool isPluginWritable(std::string name);
123 
125  const std::vector<PluginMetadata> getInstalled();
126 
131  std::vector<std::string> GetInstalldataPlugins();
132 
134  void SetInstalledMetadata(const PluginMetadata& pm);
135 
137  const std::vector<PluginMetadata> getAvailable();
138 
143  std::vector<PluginMetadata> getCompatiblePlugins();
144 
146  const std::map<std::string, int> getCountByTarget();
147 
149  std::string getPluginByLibrary(const std::string& filename);
150 
152  std::string getMetadataPath();
153 
155  void setMetadata(std::string path) { metadataPath = path; }
156 
158  bool installPlugin(PluginMetadata plugin);
159 
161  bool installPlugin(PluginMetadata plugin, std::string path);
162 
164  bool ExtractMetadata(const std::string& path, PluginMetadata& metadata);
165 
166  /* Install a new, downloaded but not installed plugin tarball. */
167  bool installPlugin(const std::string& path);
168 
170  bool uninstall(const std::string plugin);
171 
173  bool ClearInstallData(const std::string plugin_name);
174 
177 
178  std::string getLastErrorMsg() { return last_error_msg; }
179 
180  CatalogData* GetCatalogData() { return &catalogData; }
181 
182 protected:
183  PluginHandler();
184 
185 private:
186  std::string metadataPath;
187  std::vector<PluginMetadata> installed;
188  CatalogData catalogData;
189  std::string last_error_msg;
190  bool InstallPlugin(const std::string& path,std::string& filelist,
191  const std::string metadata_path,
192  bool only_metadata);
193 
194  bool explodeTarball(struct archive* src, struct archive* dest,
195  std::string& filelist,
196  const std::string& metadata_path,
197  bool only_metadata);
198  bool extractTarball(const std::string path, std::string& filelist,
199  const std::string metadata_path = "",
200  bool only_metadata = false);
201  bool archive_check(int r, const char* msg, struct archive* a);
202  std::unordered_map<std::string, std::vector<std::string>> files_by_plugin;
203  bool DoClearInstallData(const std::string plugin_name);
204 };
205 
206 #endif // PLUGIN_HANDLER_H__
Generic event handling between MVC Model and Controller based on a shared EventVar variable.
const std::vector< PluginMetadata > getInstalled()
Return list of all installed and loaded plugins.
void setMetadata(std::string path)
Set path to metadata XML file.
static void cleanup(const std::string &filelist, const std::string &plugname)
Cleanup failed installation attempt using filelist for plugin.
static std::vector< std::string > GetImportPaths()
List of paths for imported plugins metadata.
bool installPluginFromCache(PluginMetadata plugin)
Install plugin tarball from local cache.
const std::map< std::string, int > getCountByTarget()
Map of available plugin targets -> number of occurences.
std::vector< PluginMetadata > getCompatiblePlugins()
Return list of available, unique and compatible plugins from configured XML catalog.
static std::string ImportedMetadataPath(std::string name)
Return path to imported metadata for given plugin.
std::string getPluginByLibrary(const std::string &filename)
Return plugin containing given filename or "" if not found.
bool isPluginWritable(std::string name)
Check if given plugin can be installed/updated.
static std::string pluginsInstallDataPath()
Return base directory for installation data.
std::vector< std::string > GetInstalldataPlugins()
Return list of installed plugins lower case names, not necessarily loaded.
static std::string fileListPath(std::string name)
Return path to installation manifest for given plugin.
static std::string versionPath(std::string name)
Return path to file containing version for given plugin.
bool uninstall(const std::string plugin)
Uninstall an installed and loaded plugin.
bool installPlugin(PluginMetadata plugin)
Download and install a new, not installed plugin.
bool ClearInstallData(const std::string plugin_name)
Remove installation data for not loaded plugin.
std::string getMetadataPath()
Return path to metadata XML file.
void SetInstalledMetadata(const PluginMetadata &pm)
Set metadata for an installed plugin.
bool ExtractMetadata(const std::string &path, PluginMetadata &metadata)
Extract metadata in given tarball path.
const std::vector< PluginMetadata > getAvailable()
Update catalog and return list of available, not installed plugins.
static bool isCompatible(const PluginMetadata &metadata, const char *os=PKG_TARGET, const char *os_version=PKG_TARGET_VERSION)
Return true if given plugin is loadable on given os/version.
Datatypes and methods to parse ocpn-plugins.xml XML data, either complete catalog or a single plugin.
Plugin metadata, reflects the xml format directly.