OpenCPN Partial API docs
Loading...
Searching...
No Matches
load_errors_dlg.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2022 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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
30#include <memory>
31#include <sstream>
32#include <string>
33#include <vector>
34
35#include <wx/event.h>
36#include <wx/window.h>
37
38#include "gui_lib.h"
39#include "load_errors_dlg.h"
40#include "observable_evt.h"
41#include "model/plugin_handler.h"
42#include "model/plugin_loader.h"
43#ifdef __ANDROID__
44#include "androidUTIL.h"
45#endif
46
47wxDEFINE_EVENT(EVT_LOAD_COMPLETE, ObservedEvt);
48
49static const char* const kBadPluginsIntro = _(R"(
50The following plugins have encountered errors during startup:
51
52)");
53
54static const char* const kBadPluginIntro = _(R"(
55The following plugin has encountered errors during startup:
56
57)");
58
59static const char* const kBadLibsIntro = _(R"(
60The following libraries have encountered errors during startup:
61
62)");
63
64static const char* const kBadLibIntro = _(R"(
65The following library has encountered errors during startup:
66
67)");
68static const char* const kBadPluginsFooter = _(R"(
69
70These plugins will be uninstalled. You might want to reinstall
71them after updating the catalog.
72)");
73
74static const char* const kBadPluginFooter = _(R"(
75
76This plugin will be uninstalled. You might want to reinstall
77it after updating the catalog.
78)");
79
80static const char* const kBadLibsFooter = _(R"(
81
82These libraries will be removed. You might want to reinstall their
83associated plugin after updating the catalog.)");
84
85static const char* const kBadLibFooter = _(R"(
86
87The library will be removed. You might want to reinstall it's
88associated plugin after updating the catalog.)");
89
92public:
93 class FormatCtx {
94 public:
95 std::vector<std::string> plugins;
96 std::vector<std::string> libs;
97
98 FormatCtx(const std::vector<LoadError> errors) {
99 auto handler = PluginHandler::getInstance();
100 for (const auto& e : errors) {
101 auto plugin = handler->getPluginByLibrary(e.lib_path);
102 if (plugin != "") plugins.push_back(plugin);
103 if (e.lib_path != "") libs.push_back(e.lib_path);
104 }
105 }
106 };
107
108 LoadErrorsDlg(wxWindow* parent, const FormatCtx& format_ctx)
109 : OCPNMessageDialog(parent, wxString(FormatMsg(format_ctx))) {}
110
111 std::string FormatMsg(const FormatCtx& ctx) {
112 auto handler = PluginHandler::getInstance();
113 std::stringstream ss;
114 if (ctx.plugins.size() > 0) {
115 ss << (ctx.plugins.size() == 1 ? kBadPluginIntro : kBadPluginsIntro);
116 for (const auto& p : ctx.plugins) {
117 ss << " " << p << "\n";
118 }
119 ss << (ctx.plugins.size() == 1 ? kBadPluginFooter : kBadPluginsFooter);
120 }
121 if (ctx.libs.size() > 0) {
122 ss << (ctx.libs.size() == 1 ? kBadLibIntro : kBadLibsIntro);
123 for (const auto& lib : ctx.libs) {
124 ss << " " << lib << "\n";
125 }
126 ss << (ctx.libs.size() == 1 ? kBadLibFooter : kBadLibsFooter);
127 }
128 return ss.str();
129 }
130};
131
133static void Run(wxWindow* parent, const std::vector<LoadError>& errors) {
134 LoadErrorsDlg::FormatCtx format_ctx(errors);
135 LoadErrorsDlg dlg(parent, format_ctx);
136
137#ifdef __ANDROID__
138 std::string ss = dlg.FormatMsg(format_ctx);
139 androidShowSimpleOKDialog("Error", ss);
140 int sts = wxID_YES;
141#else
142 int sts = dlg.ShowModal();
143#endif
144 if (sts == wxID_YES || sts == wxID_OK) {
145 for (const auto& plugin : format_ctx.plugins) {
146 PluginHandler::getInstance()->uninstall(plugin);
147 }
148 for (const auto& lib : format_ctx.libs) {
149 if (isRegularFile(lib.c_str())) remove(lib.c_str());
150 }
151 }
152}
153
154LoadErrorsDlgCtrl::LoadErrorsDlgCtrl(wxWindow* parent) : m_parent(parent) {
155 auto loader = PluginLoader::getInstance();
156
157 load_complete_listener.Listen(loader->evt_plugin_loadall_finalize, this,
158 EVT_LOAD_COMPLETE);
159 Bind(EVT_LOAD_COMPLETE, [&](ObservedEvt& ev) {
160 auto errors = UnpackEvtPointer<std::vector<LoadError>>(ev);
161 if (errors->size() != 0) Run(m_parent, *errors);
162 });
163}
Unloadable plugins report message box.
Adds a std::shared<void> element to wxCommandEvent.
bool uninstall(const std::string plugin)
Uninstall an installed and loaded plugin.
General purpose GUI support.