OpenCPN Partial API docs
Loading...
Searching...
No Matches
download_mgr.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2019 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include "config.h"
25#include "download_mgr.h"
26
27#include <fstream>
28#include <set>
29#include <sstream>
30
31#include <wx/bitmap.h>
32#include <wx/button.h>
33#include <wx/debug.h>
34#include <wx/file.h>
35#include <wx/image.h>
36#include <wx/log.h>
37#include <wx/panel.h>
38#include <wx/progdlg.h>
39#include <wx/sizer.h>
40#include <wx/statline.h>
41#include <wx/uri.h>
42
43#include "model/downloader.h"
44#include "model/plugin_cache.h"
46#include "model/semantic_vers.h"
47#include "model/svg_utils.h"
48
49#include "catalog_mgr.h"
50#include "expand_icon.h"
51#include "ocpn_platform.h"
52#include "picosha2.h"
53#include "pluginmanager.h"
54#include "styles.h"
55
56#undef major // Work around gnu's major() and minor() macros.
57#undef minor
58
59// Main window reload event
60wxDEFINE_EVENT(EVT_PLUGINS_RELOAD, wxCommandEvent);
61
65static bool checksum_ok(const std::string& path,
66 const PluginMetadata& metadata) {
67 wxLogDebug("Checksum test on %s", metadata.name.c_str());
68 if (metadata.checksum == "") {
69 wxLogDebug("No metadata checksum, aborting check,");
70 return true;
71 }
72 const size_t pos = metadata.checksum.find(':');
73 std::string checksum(metadata.checksum);
74 if (pos == std::string::npos) {
75 checksum = std::string("sha256:") + checksum;
76 }
77 std::ifstream f(path, std::ios::binary);
78 picosha2::hash256_one_by_one hasher;
79 while (!f.eof()) {
80 char buff[2048];
81 f.read(buff, sizeof(buff));
82 const std::string block(buff, f.gcount());
83 hasher.process(block.begin(), block.end());
84 }
85 hasher.finish();
86 std::string tarball_hash =
87 std::string("sha256:") + picosha2::get_hash_hex_string(hasher);
88
89 if (tarball_hash == checksum) {
90 wxLogDebug("Checksum ok: %s", tarball_hash.c_str());
91 return true;
92 }
93 wxLogMessage("Checksum fail on %s, tarball: %s, metadata: %s",
94 metadata.name.c_str(), tarball_hash.c_str(), checksum.c_str());
95 return false;
96}
97
102static ssize_t PlugInIxByName(const std::string name,
103 const ArrayOfPlugIns* plugins) {
104 for (unsigned i = 0; i < plugins->GetCount(); i += 1) {
105 if (name == plugins->Item(i)->m_common_name.Lower().ToStdString()) {
106 return i;
107 }
108 }
109 return -1;
110}
111
114 : Downloader(plugin.tarball_url),
115 m_downloaded(0),
116 m_dialog(0),
117 m_plugin(plugin),
118 m_parent(parent) {}
119
120std::string GuiDownloader::run(wxWindow* parent, bool remove_current) {
121 bool ok;
122 bool downloaded = false;
123 std::string path = ocpn::lookup_tarball(m_plugin.tarball_url.c_str());
124 if (!path.size()) {
125 long size = get_filesize();
126 std::string label(_("Downloading "));
127 label += url;
128 m_dialog =
129 new wxProgressDialog(_("Downloading"), label.c_str(), size, parent,
130 wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
131#ifdef __ANDROID__
132 m_dialog->SetBackgroundColour(wxColour(0x7c, 0xb0, 0xe9)); // light blue
133#endif
134 ok = download(path);
135 g_Platform->HideBusySpinner();
136
137 if (!ok) {
138 delete m_dialog;
139 showErrorDialog("Download error");
140 return "";
141 }
142
143 // Download aborted?
144 if (m_dialog == 0) {
145 showErrorDialog("Download aborted");
146 return "";
147 } else {
148 delete m_dialog;
149 }
150 if (!checksum_ok(path, m_plugin)) {
151 showErrorDialog("Checksum error");
152 return "";
153 }
154 m_dialog = 0; // make sure that on_chunk() doesn't misbehave.
155 downloaded = true;
156 }
157
158 auto pluginHandler = PluginHandler::GetInstance();
159 if (remove_current) {
160 wxLogMessage("Uninstalling %s", m_plugin.name.c_str());
161 pluginHandler->Uninstall(m_plugin.name);
162 }
163 ok = pluginHandler->InstallPlugin(m_plugin, path);
164 if (!ok) {
165 showErrorDialog("Installation error");
166 return "";
167 }
168
169 if (downloaded) {
170 // Cache the tarball from the tmp location to the plugin cache.
171 wxURI uri(wxString(m_plugin.tarball_url.c_str()));
172 wxFileName fn(uri.GetPath());
173 auto basename = fn.GetFullName().ToStdString();
174 if (ocpn::store_tarball(path.c_str(), basename.c_str())) {
175 wxLogMessage("Copied %s to local cache at %s", path.c_str(),
176 basename.c_str());
177 }
178 }
179
180 wxMessageDialog* dlg = new wxMessageDialog(
181 m_parent,
182 m_plugin.name + " " + m_plugin.version + _(" successfully installed"),
183 _("Installation complete"), wxOK | wxCENTRE | wxICON_INFORMATION);
184 dlg->ShowModal();
185 return path;
186}
187
188void GuiDownloader::on_chunk(const char* buff, unsigned bytes) {
189 Downloader::on_chunk(buff, bytes);
190 m_downloaded += bytes;
191 if (m_dialog && !m_dialog->Update(m_downloaded)) {
192 // User pushed Cancel button
193 delete m_dialog;
194 m_dialog = 0;
195 }
196}
197
198void GuiDownloader::showErrorDialog(const char* msg) {
199 auto dlg = new wxMessageDialog(m_parent, "", _("Installation error"),
200 wxOK | wxICON_ERROR);
201 auto last_error_msg = last_error();
202 std::string text = msg;
203 if (last_error_msg != "") {
204 text = text + ": " + error_msg;
205 }
206 text = text + "\nPlease check system log for more info.";
207 dlg->SetMessage(text);
208 dlg->ShowModal();
209 dlg->Destroy();
210}
Catalog options dialog, by default disabled.
Default downloader, usable in a CLI context.
Definition downloader.h:35
bool download(std::ostream *stream)
Download url into stream, return false on errors.
long get_filesize()
Try to get remote filesize, return 0 on failure.
std::string last_error()
Last Curl error message.
virtual void on_chunk(const char *buff, unsigned bytes)
Called when given bytes has been transferred from remote.
void on_chunk(const char *buff, unsigned bytes) override
Called when given bytes has been transferred from remote.
GuiDownloader(wxWindow *parent, PluginMetadata plugin)
Add progress and final message dialogs to the basic Downloader.
static PluginHandler * GetInstance()
Singleton factory.
Generic GUI downloads tool.
Handle downloading of files from remote urls.
std::string lookup_tarball(const char *uri)
Get path to tarball in cache for given filename.
bool store_tarball(const char *path, const char *basename)
Store a tarball in tarball cache, return success/fail.
OpenCPN Platform specific support utilities.
Downloaded plugins cache.
Plugin remote repositories installation and Uninstall/list operations.
PlugInManager and helper classes – Mostly gui parts (dialogs) and plugin API stuff.
Semantic version encode/decode object.
Plugin metadata, reflects the xml format directly.
Runtime representation of a plugin block.
Chart Symbols.
SVG utilities.