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, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
28#include <memory>
29#include <sstream>
30#include <string>
31#include <vector>
32
33#include <wx/event.h>
34#include <wx/window.h>
35
36#include "observable/event.h"
37
39#include "model/plugin_loader.h"
40
41#include "gui_lib.h"
42#include "load_errors_dlg.h"
43
44#ifdef __ANDROID__
45#include "androidUTIL.h"
46#endif
47
48wxDEFINE_EVENT(EVT_LOAD_COMPLETE, ObservedEvt);
49
50static const char* const kBadPluginsIntro = _(R"(
51The following plugins have encountered errors during startup:
52
53)");
54
55static const char* const kBadPluginIntro = _(R"(
56The following plugin has encountered errors during startup:
57
58)");
59
60static const char* const kBadLibsIntro = _(R"(
61The following libraries have encountered errors during startup:
62
63)");
64
65static const char* const kBadLibIntro = _(R"(
66The following library has encountered errors during startup:
67
68)");
69static const char* const kBadPluginsFooter = _(R"(
70
71These plugins will be uninstalled. You might want to reinstall
72them after updating the catalog.
73)");
74
75static const char* const kBadPluginFooter = _(R"(
76
77This plugin will be uninstalled. You might want to reinstall
78it after updating the catalog.
79)");
80
81static const char* const kBadLibsFooter = _(R"(
82
83These libraries will be removed. You might want to reinstall their
84associated plugin after updating the catalog.)");
85
86static const char* const kBadLibFooter = _(R"(
87
88The library will be removed. You might want to reinstall it's
89associated plugin after updating the catalog.)");
90
93public:
94 class FormatCtx {
95 public:
96 std::vector<std::string> plugins;
97 std::vector<std::string> libs;
98
99 FormatCtx(const std::vector<LoadError> errors) {
100 auto handler = PluginHandler::GetInstance();
101 for (const auto& e : errors) {
102 auto plugin = handler->GetPluginByLibrary(e.lib_path);
103 if (plugin != "") plugins.push_back(plugin);
104 if (e.lib_path != "") libs.push_back(e.lib_path);
105 }
106 }
107 };
108
109 LoadErrorsDlg(wxWindow* parent, const FormatCtx& format_ctx)
110 : OCPNMessageDialog(parent, wxString(FormatMsg(format_ctx))) {}
111
112 std::string FormatMsg(const FormatCtx& ctx) {
113 auto handler = PluginHandler::GetInstance();
114 std::stringstream ss;
115 if (ctx.plugins.size() > 0) {
116 ss << (ctx.plugins.size() == 1 ? kBadPluginIntro : kBadPluginsIntro);
117 for (const auto& p : ctx.plugins) {
118 ss << " " << p << "\n";
119 }
120 ss << (ctx.plugins.size() == 1 ? kBadPluginFooter : kBadPluginsFooter);
121 }
122 if (ctx.libs.size() > 0) {
123 ss << (ctx.libs.size() == 1 ? kBadLibIntro : kBadLibsIntro);
124 for (const auto& lib : ctx.libs) {
125 ss << " " << lib << "\n";
126 }
127 ss << (ctx.libs.size() == 1 ? kBadLibFooter : kBadLibsFooter);
128 }
129 return ss.str();
130 }
131};
132
134static void Run(wxWindow* parent, const std::vector<LoadError>& errors) {
135 LoadErrorsDlg::FormatCtx format_ctx(errors);
136 LoadErrorsDlg dlg(parent, format_ctx);
137
138#ifdef __ANDROID__
139 std::string ss = dlg.FormatMsg(format_ctx);
140 androidShowSimpleOKDialog("Error", ss);
141 int sts = wxID_YES;
142#else
143 int sts = dlg.ShowModal();
144#endif
145 if (sts == wxID_YES || sts == wxID_OK) {
146 for (const auto& plugin : format_ctx.plugins) {
148 }
149 for (const auto& lib : format_ctx.libs) {
150 if (isRegularFile(lib.c_str())) remove(lib.c_str());
151 }
152 }
153}
154
155LoadErrorsDlgCtrl::LoadErrorsDlgCtrl(wxWindow* parent) : m_parent(parent) {
156 auto loader = PluginLoader::GetInstance();
157
158 load_complete_listener.Listen(loader->evt_plugin_loadall_finalize, this,
159 EVT_LOAD_COMPLETE);
160 Bind(EVT_LOAD_COMPLETE, [&](ObservedEvt& ev) {
161 auto errors = obs::UnpackEvtPointer<std::vector<LoadError>>(ev);
162 if (errors->size() != 0) Run(m_parent, *errors);
163 });
164}
Unloadable plugins report message box.
Custom event class for OpenCPN's notification system.
bool Uninstall(const std::string plugin)
Uninstall an installed and loaded plugin.
static PluginHandler * GetInstance()
Singleton factory.
General purpose GUI support.
Plugin remote repositories installation and Uninstall/list operations.
Low level code to load plugins from disk, notably the PluginLoader class.