34#include <wx/checkbox.h>
35#include <wx/dcclient.h>
40#include <wx/statline.h>
41#include <wx/stattext.h>
42#include <wx/textctrl.h>
49#include "expand_panel.h"
53#if !defined(__linux__) || defined(__ANDROID__)
64static bool hide_dongle_dialog;
65static bool hide_device_dialog;
67static const char*
const DONGLE_INTRO = _(R
"(
68An OpenCPN dongle is detected but cannot be used due to missing permissions.
70This problem can be fixed by installing a udev rules file. Once installed,
71it will ensure that the dongle permissions are OK.
74static const char*
const FLATPAK_INTRO_TRAILER = _(R
"(
76On flatpak, this must be done using the manual command instructions below
79static const char*
const DEVICE_INTRO = _(R
"(
80The device @DEVICE@ exists but cannot be used due to missing permissions.
82This problem can be fixed by installing a udev rules file. Once installed,
83the rules file will fix the permissions problem.
86static const char*
const DEVICE_LINK_INTRO = _(R
"(
88It will also create a new device called @SYMLINK@. It is recommended to use
89@SYMLINK@ instead of @DEVICE@ to avoid problems with changing device names,
90in particular on laptops.
93static const char*
const HIDE_DIALOG_LABEL =
94 _(
"Do not show this dialog next time");
96static const char*
const RULE_SUCCESS_TTYS_MSG = _(R
"(
97Rule successfully installed. To activate the new rule restart the system.
100static const char*
const RULE_SUCCESS_MSG = _(R
"(
101Rule successfully installed. To activate the new rule restart system or:
103- Unplug and re-insert the USB device.
107static const char*
const FLATPAK_INSTALL_MSG = _(R
"(
108To do after installing the rule according to instructions:
110- Unplug and re-insert the USB device.
114static const char*
const DEVICE_NOT_FOUND =
115 _(
"The device @device@ can not be found (disconnected?)");
117static const char*
const INSTRUCTIONS =
"@pkexec@ cp @PATH@ /etc/udev/rules.d";
120class DeviceNotFoundDlg :
public wxFrame {
123 static void Create(wxWindow* parent,
const std::string& device) {
124 wxWindow* dlg =
new DeviceNotFoundDlg(parent, device);
129 static void DestroyOpenWindows() {
130 for (
const auto& name : open_windows) {
131 auto window = wxWindow::FindWindowByName(name);
132 if (window) window->Destroy();
134 open_windows.clear();
138 static std::vector<std::string> open_windows;
142 ButtonsSizer(DeviceNotFoundDlg* parent) : wxStdDialogButtonSizer() {
143 auto button =
new wxButton(parent, wxID_OK);
149 DeviceNotFoundDlg(wxWindow* parent,
const std::string& device)
150 : wxFrame(parent, wxID_ANY, _(
"Opencpn: device not found"),
151 wxDefaultPosition, wxDefaultSize,
152 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT) {
153 std::stringstream ss;
154 ss <<
"dlg-id-" << rand();
156 open_windows.push_back(ss.str());
158 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent& e) {
162 Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent&) { OnClose(); });
164 auto vbox =
new wxBoxSizer(wxVERTICAL);
166 auto flags = wxSizerFlags().Expand().Border();
167 std::string txt(DEVICE_NOT_FOUND);
170 vbox->Add(
new wxStaticText(
this, wxID_ANY, txt), flags);
172 vbox->Add(
new wxStaticLine(
this), wxSizerFlags().Expand());
180 const std::string name(GetName().ToStdString());
182 std::find_if(open_windows.begin(), open_windows.end(),
183 [name](
const std::string& s) { return s == name; });
184 assert(found != std::end(open_windows) &&
185 "Cannot find dialog in window list");
186 open_windows.erase(found);
191std::vector<std::string> DeviceNotFoundDlg::open_windows;
196class HideCheckbox :
public wxCheckBox {
198 HideCheckbox(wxWindow* parent,
const char* label,
bool* state)
199 : wxCheckBox(parent, wxID_ANY, label, wxDefaultPosition, wxDefaultSize,
204 [&](wxCommandEvent& ev) { *m_state = ev.IsChecked(); });
212class HidePanel :
public wxPanel {
214 HidePanel(wxWindow* parent,
const char* label,
bool* state)
216 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
217 hbox->Add(
new HideCheckbox(
this, label, state), wxSizerFlags().Expand());
227 ManualInstructions(wxWindow* parent,
const char* cmd)
229 Create(GetCmd(parent, cmd));
230 auto flags = wxSizerFlags().Expand();
231 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
233 const char* label = _(
"Manual command line instructions");
234 hbox->Add(
new wxStaticText(
this, wxID_ANY, label), flags);
237 auto vbox =
new wxBoxSizer(wxVERTICAL);
239 flags = flags.Border(wxLEFT);
240 vbox->Add(
GetChild(), flags.ReserveSpaceEvenIfHidden());
248 wxTextCtrl* GetCmd(wxWindow* parent,
const char* tmpl) {
249 std::string cmd(tmpl);
252 ctrl->SetMinSize(parent->GetTextExtent(cmd +
"aaa"));
261 ReviewRule(wxWindow* parent,
const std::string& rule)
263 int from = rule[0] ==
'\n' ? 1 : 0;
264 Create(
new wxStaticText(
this, wxID_ANY, rule.substr(from)));
266 auto flags = wxSizerFlags().Expand();
267 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
268 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Review rule")), flags);
270 auto vbox =
new wxBoxSizer(wxVERTICAL);
272 auto indent = parent->GetTextExtent(
"ABCDE").GetWidth();
273 flags = flags.Border(wxLEFT, indent);
275 vbox->Add(
GetChild(), flags.ReserveSpaceEvenIfHidden());
283static std::string GetRule(
const std::string& path) {
284 std::ifstream input(path.c_str());
285 std::ostringstream buf;
286 buf << input.rdbuf();
289 WARNING_LOG <<
"Cannot open rule file: " << path;
295class DongleInfoPanel :
public wxPanel {
297 DongleInfoPanel(wxWindow* parent) : wxPanel(parent) {
298 std::string cmd(INSTRUCTIONS);
302 auto vbox =
new wxBoxSizer(wxVERTICAL);
303 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
304 std::string rule_text = GetRule(rule_path);
305 vbox->Add(
new ReviewRule(
this, rule_text.c_str()));
312class DeviceInfoPanel :
public wxPanel {
314 DeviceInfoPanel(wxWindow* parent,
const std::string rule_path)
316 std::string cmd(INSTRUCTIONS);
319 auto vbox =
new wxBoxSizer(wxVERTICAL);
320 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
321 vbox->Add(
new ReviewRule(
this, GetRule(rule_path)));
328class Buttons :
public wxPanel {
330 Buttons(wxWindow* parent,
const char* rule_path)
331 : wxPanel(parent), m_rule_path(rule_path) {
332 auto sizer =
new wxBoxSizer(wxHORIZONTAL);
333 auto flags = wxSizerFlags().Bottom().Border(wxLEFT);
334 sizer->Add(1, 1, 100, wxEXPAND);
335 auto install =
new wxButton(
this, wxID_ANY, _(
"Install rule"));
336 install->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
337 [&](wxCommandEvent& ev) { DoInstall(); });
338 install->Enable(getenv(
"FLATPAK_ID") == NULL);
339 sizer->Add(install, flags);
340 auto quit =
new wxButton(
this, wxID_EXIT, _(
"Quit"));
341 quit->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent& ev) {
342 if (getenv(
"FLATPAK_ID")) {
343 auto flags = wxOK | wxICON_INFORMATION;
344 auto msg = FLATPAK_INSTALL_MSG;
345 OCPNMessageBox(
this, msg, _(
"OpenCPN"), flags);
347 dynamic_cast<wxDialog*
>(GetParent())->EndModal(0);
349 sizer->Add(quit, flags);
357 string cmd(INSTRUCTIONS);
360 ifstream f(m_rule_path);
362 string(istreambuf_iterator<char>(f), istreambuf_iterator<char>());
363 int sts = system(cmd.c_str());
364 int flags = wxOK | wxICON_WARNING;
365 const char* msg = _(
"Errors encountered installing rule.");
366 if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
367 if (rule.find(
"ttyS") != std::string::npos) {
368 msg = RULE_SUCCESS_TTYS_MSG;
370 msg = RULE_SUCCESS_MSG;
372 flags = wxOK | wxICON_INFORMATION;
374 OCPNMessageBox(
this, msg, _(
"OpenCPN Info"), flags);
378 std::string m_rule_path;
382class DongleRuleDialog :
public wxDialog {
384 DongleRuleDialog(wxWindow* parent)
385 : wxDialog(parent, wxID_ANY, _(
"Manage dongle udev rule")) {
386 auto sizer =
new wxBoxSizer(wxVERTICAL);
387 auto flags = wxSizerFlags().Expand().Border();
388 std::string intro(DONGLE_INTRO);
389 if (getenv(
"FLATPAK_ID")) {
390 intro += FLATPAK_INTRO_TRAILER;
392 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
393 sizer->Add(
new wxStaticLine(
this), flags);
394 sizer->Add(
new DongleInfoPanel(
this), flags);
395 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_dongle_dialog),
397 sizer->Add(
new wxStaticLine(
this), flags);
398 sizer->Add(
new Buttons(
this,
GetDongleRule().c_str()), flags);
406static std::string GetDeviceIntro(
const char* device, std::string
symlink) {
407 std::string intro(DEVICE_INTRO);
409 std::string dev_name(device);
412 intro += DEVICE_LINK_INTRO;
414 if (getenv(
"FLATPAK_ID")) {
415 intro += FLATPAK_INTRO_TRAILER;
418 while (intro.find(
"@SYMLINK@") != std::string::npos) {
421 while (intro.find(
"@DEVICE@") != std::string::npos) {
428class DeviceRuleDialog :
public wxDialog {
430 DeviceRuleDialog(wxWindow* parent,
const char* device_path)
431 : wxDialog(parent, wxID_ANY, _(
"Manage device udev rule")) {
432 auto sizer =
new wxBoxSizer(wxVERTICAL);
433 auto flags = wxSizerFlags().Expand().Border();
436 auto intro = GetDeviceIntro(device_path,
symlink.c_str());
438 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
439 sizer->Add(
new wxStaticLine(
this), flags);
440 sizer->Add(
new DeviceInfoPanel(
this, rule_path), flags);
441 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_device_dialog),
443 sizer->Add(
new wxStaticLine(
this), flags);
444 sizer->Add(
new Buttons(
this, rule_path.c_str()), flags);
453 if (hide_device_dialog) {
457 auto& noteman = NotificationManager::GetInstance();
458 std::string msg =
"Device not found: ";
460 noteman.AddNotification(NotificationSeverity::kInformational, msg, 60);
465 auto dialog =
new DeviceRuleDialog(parent, device.c_str());
466 result = dialog->ShowModal();
475 auto dialog =
new DongleRuleDialog(parent);
476 result = dialog->ShowModal();
Non-editable TextCtrl, used like wxStaticText but is copyable.
An ExpandIcon together with a child window.
void Create(wxWindow *child, std::function< void()> on_resize=[] {})
Second part of two step creation.
wxWindow * GetIcon() const
Return the ExpandableIcon reflecting expanded/collapsed state.
wxWindow * GetChild() const
Return the managed window which is shown or hidden.
General purpose GUI support.
std::string MakeUdevLink()
Get next available udev rule base name.
bool IsDevicePermissionsOk(const char *path)
Check device path permissions.
std::string GetDongleRule()
std::string GetDeviceRule(const char *device, const char *symlink)
Get device udev rule.
bool IsDonglePermissionsWrong()
Return true if an existing dongle cannot be accessed.
Implement linux_devices.h – low level udev usb device management (Linux only).
Enhanced logging interface on top of wx/log.h.
bool replace(std::string &str, const std::string &from, const std::string &to)
Perform in place substitution in str, replacing "from" with "to".
bool startswith(const std::string &str, const std::string &prefix)
Return true if s starts with given prefix.
bool exists(const std::string &name)
User notifications manager.
Miscellaneous utilities, many of which string related.
void DestroyDeviceNotFoundDialogs()
Destroy all open "Device not found" dialog windows.
bool CheckDongleAccess(wxWindow *parent)
Runs checks and if required dialogs to make dongle accessible.
bool CheckSerialAccess(wxWindow *parent, const std::string device)
Run checks and possible dialogs to ensure device is accessible.
Access checks for comm devices and dongle.