36#include <wx/checkbox.h>
37#include <wx/dcclient.h>
42#include <wx/statline.h>
43#include <wx/stattext.h>
44#include <wx/textctrl.h>
54#if !defined(__linux__) || defined(__ANDROID__)
65static bool hide_dongle_dialog;
66static bool hide_device_dialog;
68static const char*
const DONGLE_INTRO = _(R
"(
69An OpenCPN dongle is detected but cannot be used due to missing permissions.
71This problem can be fixed by installing a udev rules file. Once installed,
72it will ensure that the dongle permissions are OK.
75static const char*
const FLATPAK_INTRO_TRAILER = _(R
"(
77On flatpak, this must be done using the manual command instructions below
80static const char*
const DEVICE_INTRO = _(R
"(
81The device @DEVICE@ exists but cannot be used due to missing permissions.
83This problem can be fixed by installing a udev rules file. Once installed,
84the rules file will fix the permissions problem.
87static const char*
const DEVICE_LINK_INTRO = _(R
"(
89It will also create a new device called @SYMLINK@. It is recommended to use
90@SYMLINK@ instead of @DEVICE@ to avoid problems with changing device names,
91in particular on laptops.
94static const char*
const HIDE_DIALOG_LABEL =
95 _(
"Do not show this dialog next time");
97static const char*
const RULE_SUCCESS_TTYS_MSG = _(R
"(
98Rule successfully installed. To activate the new rule restart the system.
101static const char*
const RULE_SUCCESS_MSG = _(R
"(
102Rule successfully installed. To activate the new rule restart system or:
104- Unplug and re-insert the USB device.
108static const char*
const FLATPAK_INSTALL_MSG = _(R
"(
109To do after installing the rule according to instructions:
111- Unplug and re-insert the USB device.
115static const char*
const DEVICE_NOT_FOUND =
116 _(
"The device @device@ can not be found (disconnected?)");
118static const char*
const INSTRUCTIONS =
"@pkexec@ cp @PATH@ /etc/udev/rules.d";
121class DeviceNotFoundDlg :
public wxFrame {
124 static void Create(wxWindow* parent,
const std::string& device) {
125 wxWindow* dlg =
new DeviceNotFoundDlg(parent, device);
130 static void DestroyOpenWindows() {
131 for (
const auto& name : open_windows) {
132 auto window = wxWindow::FindWindowByName(name);
133 if (window) window->Destroy();
135 open_windows.clear();
139 static std::vector<std::string> open_windows;
143 ButtonsSizer(DeviceNotFoundDlg* parent) : wxStdDialogButtonSizer() {
144 auto button =
new wxButton(parent, wxID_OK);
150 DeviceNotFoundDlg(wxWindow* parent,
const std::string& device)
151 : wxFrame(parent, wxID_ANY, _(
"Opencpn: device not found"),
152 wxDefaultPosition, wxDefaultSize,
153 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT) {
154 std::stringstream ss;
155 ss <<
"dlg-id-" << rand();
157 open_windows.push_back(ss.str());
159 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent& e) {
163 Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent&) { OnClose(); });
165 auto vbox =
new wxBoxSizer(wxVERTICAL);
167 auto flags = wxSizerFlags().Expand().Border();
168 std::string txt(DEVICE_NOT_FOUND);
171 vbox->Add(
new wxStaticText(
this, wxID_ANY, txt), flags);
173 vbox->Add(
new wxStaticLine(
this), wxSizerFlags().Expand());
181 const std::string name(GetName().ToStdString());
183 std::find_if(open_windows.begin(), open_windows.end(),
184 [name](
const std::string& s) { return s == name; });
185 assert(found != std::end(open_windows) &&
186 "Cannot find dialog in window list");
187 open_windows.erase(found);
192std::vector<std::string> DeviceNotFoundDlg::open_windows;
197class HideCheckbox :
public wxCheckBox {
199 HideCheckbox(wxWindow* parent,
const char* label,
bool* state)
200 : wxCheckBox(parent, wxID_ANY, label, wxDefaultPosition, wxDefaultSize,
205 [&](wxCommandEvent& ev) { *m_state = ev.IsChecked(); });
213class HidePanel :
public wxPanel {
215 HidePanel(wxWindow* parent,
const char* label,
bool* state)
217 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
218 hbox->Add(
new HideCheckbox(
this, label, state), wxSizerFlags().Expand());
226class HideShowPanel :
public wxPanel {
228 HideShowPanel(wxWindow* parent, wxWindow* child)
229 : wxPanel(parent), m_show(true), m_child(child) {
230 m_arrow =
new wxStaticText(
this, wxID_ANY,
"");
231 m_arrow->Bind(wxEVT_LEFT_DOWN, [&](wxMouseEvent& ev) { Toggle(); });
240 wxStaticText* m_arrow;
243 static const auto ARROW_DOWN = L
"\u25BC";
244 static const auto ARROW_RIGHT = L
"\u25BA";
247 m_child->Show(m_show);
248 m_arrow->SetLabel(std::string(
" ") + (m_show ? ARROW_DOWN : ARROW_RIGHT));
249 GetGrandParent()->Fit();
250 GetGrandParent()->Layout();
255class ManualInstructions :
public HideShowPanel {
257 ManualInstructions(wxWindow* parent,
const char* cmd)
258 : HideShowPanel(parent, 0) {
259 m_child = GetCmd(parent, cmd);
261 auto flags = wxSizerFlags().Expand();
263 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
264 const char* label = _(
"Manual command line instructions");
265 hbox->Add(
new wxStaticText(
this, wxID_ANY, label), flags);
268 auto vbox =
new wxBoxSizer(wxVERTICAL);
271 flags = flags.Border(wxLEFT);
272 vbox->Add(m_child, flags.ReserveSpaceEvenIfHidden());
280 wxTextCtrl* GetCmd(wxWindow* parent,
const char* tmpl) {
281 std::string cmd(tmpl);
284 ctrl->SetMinSize(parent->GetTextExtent(cmd +
"aaa"));
291class ReviewRule :
public HideShowPanel {
293 ReviewRule(wxWindow* parent,
const std::string& rule)
294 : HideShowPanel(parent, 0) {
295 int from = rule[0] ==
'\n' ? 1 : 0;
296 m_child =
new wxStaticText(
this, wxID_ANY, rule.substr(from));
299 auto flags = wxSizerFlags().Expand();
300 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
301 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Review rule")), flags);
304 auto vbox =
new wxBoxSizer(wxVERTICAL);
306 auto indent = parent->GetTextExtent(
"ABCDE").GetWidth();
307 flags = flags.Border(wxLEFT, indent);
308 vbox->Add(m_child, flags.ReserveSpaceEvenIfHidden());
316static std::string GetRule(
const std::string& path) {
317 std::ifstream input(path.c_str());
318 std::ostringstream buf;
319 buf << input.rdbuf();
322 WARNING_LOG <<
"Cannot open rule file: " << path;
328class DongleInfoPanel :
public wxPanel {
330 DongleInfoPanel(wxWindow* parent) : wxPanel(parent) {
331 std::string cmd(INSTRUCTIONS);
335 auto vbox =
new wxBoxSizer(wxVERTICAL);
336 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
337 std::string rule_text = GetRule(rule_path);
338 vbox->Add(
new ReviewRule(
this, rule_text.c_str()));
345class DeviceInfoPanel :
public wxPanel {
347 DeviceInfoPanel(wxWindow* parent,
const std::string rule_path)
349 std::string cmd(INSTRUCTIONS);
352 auto vbox =
new wxBoxSizer(wxVERTICAL);
353 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
354 vbox->Add(
new ReviewRule(
this, GetRule(rule_path)));
361class Buttons :
public wxPanel {
363 Buttons(wxWindow* parent,
const char* rule_path)
364 : wxPanel(parent), m_rule_path(rule_path) {
365 auto sizer =
new wxBoxSizer(wxHORIZONTAL);
366 auto flags = wxSizerFlags().Bottom().Border(wxLEFT);
367 sizer->Add(1, 1, 100, wxEXPAND);
368 auto install =
new wxButton(
this, wxID_ANY, _(
"Install rule"));
369 install->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
370 [&](wxCommandEvent& ev) { DoInstall(); });
371 install->Enable(getenv(
"FLATPAK_ID") == NULL);
372 sizer->Add(install, flags);
373 auto quit =
new wxButton(
this, wxID_EXIT, _(
"Quit"));
374 quit->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent& ev) {
375 if (getenv(
"FLATPAK_ID")) {
376 auto flags = wxOK | wxICON_INFORMATION;
377 auto msg = FLATPAK_INSTALL_MSG;
378 OCPNMessageBox(
this, msg, _(
"OpenCPN"), flags);
380 dynamic_cast<wxDialog*
>(GetParent())->EndModal(0);
382 sizer->Add(quit, flags);
390 string cmd(INSTRUCTIONS);
393 ifstream f(m_rule_path);
395 string(istreambuf_iterator<char>(f), istreambuf_iterator<char>());
396 int sts = system(cmd.c_str());
397 int flags = wxOK | wxICON_WARNING;
398 const char* msg = _(
"Errors encountered installing rule.");
399 if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
400 if (rule.find(
"ttyS") != std::string::npos) {
401 msg = RULE_SUCCESS_TTYS_MSG;
403 msg = RULE_SUCCESS_MSG;
405 flags = wxOK | wxICON_INFORMATION;
407 OCPNMessageBox(
this, msg, _(
"OpenCPN Info"), flags);
411 std::string m_rule_path;
415class DongleRuleDialog :
public wxDialog {
417 DongleRuleDialog(wxWindow* parent)
418 : wxDialog(parent, wxID_ANY, _(
"Manage dongle udev rule")) {
419 auto sizer =
new wxBoxSizer(wxVERTICAL);
420 auto flags = wxSizerFlags().Expand().Border();
421 std::string intro(DONGLE_INTRO);
422 if (getenv(
"FLATPAK_ID")) {
423 intro += FLATPAK_INTRO_TRAILER;
425 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
426 sizer->Add(
new wxStaticLine(
this), flags);
427 sizer->Add(
new DongleInfoPanel(
this), flags);
428 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_dongle_dialog),
430 sizer->Add(
new wxStaticLine(
this), flags);
431 sizer->Add(
new Buttons(
this,
GetDongleRule().c_str()), flags);
439static std::string GetDeviceIntro(
const char* device, std::string
symlink) {
440 std::string intro(DEVICE_INTRO);
442 std::string dev_name(device);
445 intro += DEVICE_LINK_INTRO;
447 if (getenv(
"FLATPAK_ID")) {
448 intro += FLATPAK_INTRO_TRAILER;
451 while (intro.find(
"@SYMLINK@") != std::string::npos) {
454 while (intro.find(
"@DEVICE@") != std::string::npos) {
461class DeviceRuleDialog :
public wxDialog {
463 DeviceRuleDialog(wxWindow* parent,
const char* device_path)
464 : wxDialog(parent, wxID_ANY, _(
"Manage device udev rule")) {
465 auto sizer =
new wxBoxSizer(wxVERTICAL);
466 auto flags = wxSizerFlags().Expand().Border();
469 auto intro = GetDeviceIntro(device_path,
symlink.c_str());
471 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
472 sizer->Add(
new wxStaticLine(
this), flags);
473 sizer->Add(
new DeviceInfoPanel(
this, rule_path), flags);
474 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_device_dialog),
476 sizer->Add(
new wxStaticLine(
this), flags);
477 sizer->Add(
new Buttons(
this, rule_path.c_str()), flags);
486 if (hide_device_dialog) {
490 auto& noteman = NotificationManager::GetInstance();
491 std::string msg =
"Device not found: ";
493 noteman.AddNotification(NotificationSeverity::kInformational, msg, 60);
498 auto dialog =
new DeviceRuleDialog(parent, device.c_str());
499 result = dialog->ShowModal();
508 auto dialog =
new DongleRuleDialog(parent);
509 result = dialog->ShowModal();
Non-editable TextCtrl, used like wxStaticText but is copyable.
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.
Low level udev usb device management.
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)
Class NotificationManager.
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.