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 "gl_headers.h" // Must come before anything including GL stuff
25
26#include "config.h"
27#include "download_mgr.h"
28
29#include <fstream>
30#include <set>
31#include <sstream>
32
33#include <wx/bitmap.h>
34#include <wx/button.h>
35#include <wx/debug.h>
36#include <wx/file.h>
37#include <wx/image.h>
38#include <wx/log.h>
39#include <wx/panel.h>
40#include <wx/progdlg.h>
41#include <wx/sizer.h>
42#include <wx/statline.h>
43#include <wx/uri.h>
44
45#include "model/downloader.h"
46#include "model/plugin_cache.h"
48#include "model/semantic_vers.h"
49#include "model/svg_utils.h"
50
51#include "catalog_mgr.h"
52#include "expand_icon.h"
53#include "ocpn_platform.h"
54#include "picosha2.h"
55#include "pluginmanager.h"
56#include "styles.h"
57
58#undef major // Work around gnu's major() and minor() macros.
59#undef minor
60
61// Main window reload event
62wxDEFINE_EVENT(EVT_PLUGINS_RELOAD, wxCommandEvent);
63
67static bool checksum_ok(const std::string& path,
68 const PluginMetadata& metadata) {
69 wxLogDebug("Checksum test on %s", metadata.name.c_str());
70 if (metadata.checksum == "") {
71 wxLogDebug("No metadata checksum, aborting check,");
72 return true;
73 }
74 const size_t pos = metadata.checksum.find(':');
75 std::string checksum(metadata.checksum);
76 if (pos == std::string::npos) {
77 checksum = std::string("sha256:") + checksum;
78 }
79 std::ifstream f(path, std::ios::binary);
80 picosha2::hash256_one_by_one hasher;
81 while (!f.eof()) {
82 char buff[2048];
83 f.read(buff, sizeof(buff));
84 const std::string block(buff, f.gcount());
85 hasher.process(block.begin(), block.end());
86 }
87 hasher.finish();
88 std::string tarball_hash =
89 std::string("sha256:") + picosha2::get_hash_hex_string(hasher);
90
91 if (tarball_hash == checksum) {
92 wxLogDebug("Checksum ok: %s", tarball_hash.c_str());
93 return true;
94 }
95 wxLogMessage("Checksum fail on %s, tarball: %s, metadata: %s",
96 metadata.name.c_str(), tarball_hash.c_str(), checksum.c_str());
97 return false;
98}
99
104static ssize_t PlugInIxByName(const std::string name,
105 const ArrayOfPlugIns* plugins) {
106 for (unsigned i = 0; i < plugins->GetCount(); i += 1) {
107 if (name == plugins->Item(i)->m_common_name.Lower().ToStdString()) {
108 return i;
109 }
110 }
111 return -1;
112}
113
116 : Downloader(plugin.tarball_url),
117 m_downloaded(0),
118 m_dialog(0),
119 m_plugin(plugin),
120 m_parent(parent) {}
121
122std::string GuiDownloader::run(wxWindow* parent, bool remove_current) {
123 bool ok;
124 bool downloaded = false;
125 std::string path = ocpn::lookup_tarball(m_plugin.tarball_url.c_str());
126 if (!path.size()) {
127 long size = get_filesize();
128 std::string label(_("Downloading "));
129 label += url;
130 m_dialog =
131 new wxProgressDialog(_("Downloading"), label.c_str(), size, parent,
132 wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_CAN_ABORT);
133#ifdef __ANDROID__
134 m_dialog->SetBackgroundColour(wxColour(0x7c, 0xb0, 0xe9)); // light blue
135#endif
136 ok = download(path);
137 g_Platform->HideBusySpinner();
138
139 if (!ok) {
140 delete m_dialog;
141 showErrorDialog("Download error");
142 return "";
143 }
144
145 // Download aborted?
146 if (m_dialog == 0) {
147 showErrorDialog("Download aborted");
148 return "";
149 } else {
150 delete m_dialog;
151 }
152 if (!checksum_ok(path, m_plugin)) {
153 showErrorDialog("Checksum error");
154 return "";
155 }
156 m_dialog = 0; // make sure that on_chunk() doesn't misbehave.
157 downloaded = true;
158 }
159
160 auto pluginHandler = PluginHandler::GetInstance();
161 if (remove_current) {
162 wxLogMessage("Uninstalling %s", m_plugin.name.c_str());
163 pluginHandler->Uninstall(m_plugin.name);
164 }
165 ok = pluginHandler->InstallPlugin(m_plugin, path);
166 if (!ok) {
167 showErrorDialog("Installation error");
168 return "";
169 }
170
171 if (downloaded) {
172 // Cache the tarball from the tmp location to the plugin cache.
173 wxURI uri(wxString(m_plugin.tarball_url.c_str()));
174 wxFileName fn(uri.GetPath());
175 auto basename = fn.GetFullName().ToStdString();
176 if (ocpn::store_tarball(path.c_str(), basename.c_str())) {
177 wxLogMessage("Copied %s to local cache at %s", path.c_str(),
178 basename.c_str());
179 }
180 }
181
182 wxMessageDialog* dlg = new wxMessageDialog(
183 m_parent,
184 m_plugin.name + " " + m_plugin.version + _(" successfully installed"),
185 _("Installation complete"), wxOK | wxCENTRE | wxICON_INFORMATION);
186 dlg->ShowModal();
187 return path;
188}
189
190void GuiDownloader::on_chunk(const char* buff, unsigned bytes) {
191 Downloader::on_chunk(buff, bytes);
192 m_downloaded += bytes;
193 if (m_dialog && !m_dialog->Update(m_downloaded)) {
194 // User pushed Cancel button
195 delete m_dialog;
196 m_dialog = 0;
197 }
198}
199
200void GuiDownloader::showErrorDialog(const char* msg) {
201 auto dlg = new wxMessageDialog(m_parent, "", _("Installation error"),
202 wxOK | wxICON_ERROR);
203 auto last_error_msg = last_error();
204 std::string text = msg;
205 if (last_error_msg != "") {
206 text = text + ": " + error_msg;
207 }
208 text = text + "\nPlease check system log for more info.";
209 dlg->SetMessage(text);
210 dlg->ShowModal();
211 dlg->Destroy();
212}
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.
Platform independent GL includes.
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.