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>
51#include "expand_panel.h"
55#if !defined(__linux__) || defined(__ANDROID__)
66static bool hide_dongle_dialog;
67static bool hide_device_dialog;
69static const char*
const DONGLE_INTRO = _(R
"(
70An OpenCPN dongle is detected but cannot be used due to missing permissions.
72This problem can be fixed by installing a udev rules file. Once installed,
73it will ensure that the dongle permissions are OK.
76static const char*
const FLATPAK_INTRO_TRAILER = _(R
"(
78On flatpak, this must be done using the manual command instructions below
81static const char*
const DEVICE_INTRO = _(R
"(
82The device @DEVICE@ exists but cannot be used due to missing permissions.
84This problem can be fixed by installing a udev rules file. Once installed,
85the rules file will fix the permissions problem.
88static const char*
const DEVICE_LINK_INTRO = _(R
"(
90It will also create a new device called @SYMLINK@. It is recommended to use
91@SYMLINK@ instead of @DEVICE@ to avoid problems with changing device names,
92in particular on laptops.
95static const char*
const HIDE_DIALOG_LABEL =
96 _(
"Do not show this dialog next time");
98static const char*
const RULE_SUCCESS_TTYS_MSG = _(R
"(
99Rule successfully installed. To activate the new rule restart the system.
102static const char*
const RULE_SUCCESS_MSG = _(R
"(
103Rule successfully installed. To activate the new rule restart system or:
105- Unplug and re-insert the USB device.
109static const char*
const FLATPAK_INSTALL_MSG = _(R
"(
110To do after installing the rule according to instructions:
112- Unplug and re-insert the USB device.
116static const char*
const DEVICE_NOT_FOUND =
117 _(
"The device @device@ can not be found (disconnected?)");
119static const char*
const INSTRUCTIONS =
"@pkexec@ cp @PATH@ /etc/udev/rules.d";
122class DeviceNotFoundDlg :
public wxFrame {
125 static void Create(wxWindow* parent,
const std::string& device) {
126 wxWindow* dlg =
new DeviceNotFoundDlg(parent, device);
131 static void DestroyOpenWindows() {
132 for (
const auto& name : open_windows) {
133 auto window = wxWindow::FindWindowByName(name);
134 if (window) window->Destroy();
136 open_windows.clear();
140 static std::vector<std::string> open_windows;
144 ButtonsSizer(DeviceNotFoundDlg* parent) : wxStdDialogButtonSizer() {
145 auto button =
new wxButton(parent, wxID_OK);
151 DeviceNotFoundDlg(wxWindow* parent,
const std::string& device)
152 : wxFrame(parent, wxID_ANY, _(
"Opencpn: device not found"),
153 wxDefaultPosition, wxDefaultSize,
154 wxDEFAULT_FRAME_STYLE | wxFRAME_FLOAT_ON_PARENT) {
155 std::stringstream ss;
156 ss <<
"dlg-id-" << rand();
158 open_windows.push_back(ss.str());
160 Bind(wxEVT_CLOSE_WINDOW, [&](wxCloseEvent& e) {
164 Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent&) { OnClose(); });
166 auto vbox =
new wxBoxSizer(wxVERTICAL);
168 auto flags = wxSizerFlags().Expand().Border();
169 std::string txt(DEVICE_NOT_FOUND);
172 vbox->Add(
new wxStaticText(
this, wxID_ANY, txt), flags);
174 vbox->Add(
new wxStaticLine(
this), wxSizerFlags().Expand());
182 const std::string name(GetName().ToStdString());
184 std::find_if(open_windows.begin(), open_windows.end(),
185 [name](
const std::string& s) { return s == name; });
186 assert(found != std::end(open_windows) &&
187 "Cannot find dialog in window list");
188 open_windows.erase(found);
193std::vector<std::string> DeviceNotFoundDlg::open_windows;
198class HideCheckbox :
public wxCheckBox {
200 HideCheckbox(wxWindow* parent,
const char* label,
bool* state)
201 : wxCheckBox(parent, wxID_ANY, label, wxDefaultPosition, wxDefaultSize,
206 [&](wxCommandEvent& ev) { *m_state = ev.IsChecked(); });
214class HidePanel :
public wxPanel {
216 HidePanel(wxWindow* parent,
const char* label,
bool* state)
218 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
219 hbox->Add(
new HideCheckbox(
this, label, state), wxSizerFlags().Expand());
229 ManualInstructions(wxWindow* parent,
const char* cmd)
231 Create(GetCmd(parent, cmd));
232 auto flags = wxSizerFlags().Expand();
233 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
235 const char* label = _(
"Manual command line instructions");
236 hbox->Add(
new wxStaticText(
this, wxID_ANY, label), flags);
239 auto vbox =
new wxBoxSizer(wxVERTICAL);
241 flags = flags.Border(wxLEFT);
242 vbox->Add(
GetChild(), flags.ReserveSpaceEvenIfHidden());
250 wxTextCtrl* GetCmd(wxWindow* parent,
const char* tmpl) {
251 std::string cmd(tmpl);
254 ctrl->SetMinSize(parent->GetTextExtent(cmd +
"aaa"));
263 ReviewRule(wxWindow* parent,
const std::string& rule)
265 int from = rule[0] ==
'\n' ? 1 : 0;
266 Create(
new wxStaticText(
this, wxID_ANY, rule.substr(from)));
268 auto flags = wxSizerFlags().Expand();
269 auto hbox =
new wxBoxSizer(wxHORIZONTAL);
270 hbox->Add(
new wxStaticText(
this, wxID_ANY, _(
"Review rule")), flags);
272 auto vbox =
new wxBoxSizer(wxVERTICAL);
274 auto indent = parent->GetTextExtent(
"ABCDE").GetWidth();
275 flags = flags.Border(wxLEFT, indent);
277 vbox->Add(
GetChild(), flags.ReserveSpaceEvenIfHidden());
285static std::string GetRule(
const std::string& path) {
286 std::ifstream input(path.c_str());
287 std::ostringstream buf;
288 buf << input.rdbuf();
291 WARNING_LOG <<
"Cannot open rule file: " << path;
297class DongleInfoPanel :
public wxPanel {
299 DongleInfoPanel(wxWindow* parent) : wxPanel(parent) {
300 std::string cmd(INSTRUCTIONS);
304 auto vbox =
new wxBoxSizer(wxVERTICAL);
305 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
306 std::string rule_text = GetRule(rule_path);
307 vbox->Add(
new ReviewRule(
this, rule_text.c_str()));
314class DeviceInfoPanel :
public wxPanel {
316 DeviceInfoPanel(wxWindow* parent,
const std::string rule_path)
318 std::string cmd(INSTRUCTIONS);
321 auto vbox =
new wxBoxSizer(wxVERTICAL);
322 vbox->Add(
new ManualInstructions(
this, cmd.c_str()));
323 vbox->Add(
new ReviewRule(
this, GetRule(rule_path)));
330class Buttons :
public wxPanel {
332 Buttons(wxWindow* parent,
const char* rule_path)
333 : wxPanel(parent), m_rule_path(rule_path) {
334 auto sizer =
new wxBoxSizer(wxHORIZONTAL);
335 auto flags = wxSizerFlags().Bottom().Border(wxLEFT);
336 sizer->Add(1, 1, 100, wxEXPAND);
337 auto install =
new wxButton(
this, wxID_ANY, _(
"Install rule"));
338 install->Bind(wxEVT_COMMAND_BUTTON_CLICKED,
339 [&](wxCommandEvent& ev) { DoInstall(); });
340 install->Enable(getenv(
"FLATPAK_ID") == NULL);
341 sizer->Add(install, flags);
342 auto quit =
new wxButton(
this, wxID_EXIT, _(
"Quit"));
343 quit->Bind(wxEVT_COMMAND_BUTTON_CLICKED, [&](wxCommandEvent& ev) {
344 if (getenv(
"FLATPAK_ID")) {
345 auto flags = wxOK | wxICON_INFORMATION;
346 auto msg = FLATPAK_INSTALL_MSG;
347 OCPNMessageBox(
this, msg, _(
"OpenCPN"), flags);
349 dynamic_cast<wxDialog*
>(GetParent())->EndModal(0);
351 sizer->Add(quit, flags);
359 string cmd(INSTRUCTIONS);
362 ifstream f(m_rule_path);
364 string(istreambuf_iterator<char>(f), istreambuf_iterator<char>());
365 int sts = system(cmd.c_str());
366 int flags = wxOK | wxICON_WARNING;
367 const char* msg = _(
"Errors encountered installing rule.");
368 if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
369 if (rule.find(
"ttyS") != std::string::npos) {
370 msg = RULE_SUCCESS_TTYS_MSG;
372 msg = RULE_SUCCESS_MSG;
374 flags = wxOK | wxICON_INFORMATION;
376 OCPNMessageBox(
this, msg, _(
"OpenCPN Info"), flags);
380 std::string m_rule_path;
384class DongleRuleDialog :
public wxDialog {
386 DongleRuleDialog(wxWindow* parent)
387 : wxDialog(parent, wxID_ANY, _(
"Manage dongle udev rule")) {
388 auto sizer =
new wxBoxSizer(wxVERTICAL);
389 auto flags = wxSizerFlags().Expand().Border();
390 std::string intro(DONGLE_INTRO);
391 if (getenv(
"FLATPAK_ID")) {
392 intro += FLATPAK_INTRO_TRAILER;
394 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
395 sizer->Add(
new wxStaticLine(
this), flags);
396 sizer->Add(
new DongleInfoPanel(
this), flags);
397 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_dongle_dialog),
399 sizer->Add(
new wxStaticLine(
this), flags);
400 sizer->Add(
new Buttons(
this,
GetDongleRule().c_str()), flags);
408static std::string GetDeviceIntro(
const char* device, std::string
symlink) {
409 std::string intro(DEVICE_INTRO);
411 std::string dev_name(device);
414 intro += DEVICE_LINK_INTRO;
416 if (getenv(
"FLATPAK_ID")) {
417 intro += FLATPAK_INTRO_TRAILER;
420 while (intro.find(
"@SYMLINK@") != std::string::npos) {
423 while (intro.find(
"@DEVICE@") != std::string::npos) {
430class DeviceRuleDialog :
public wxDialog {
432 DeviceRuleDialog(wxWindow* parent,
const char* device_path)
433 : wxDialog(parent, wxID_ANY, _(
"Manage device udev rule")) {
434 auto sizer =
new wxBoxSizer(wxVERTICAL);
435 auto flags = wxSizerFlags().Expand().Border();
438 auto intro = GetDeviceIntro(device_path,
symlink.c_str());
440 sizer->Add(
new wxStaticText(
this, wxID_ANY, intro), flags);
441 sizer->Add(
new wxStaticLine(
this), flags);
442 sizer->Add(
new DeviceInfoPanel(
this, rule_path), flags);
443 sizer->Add(
new HidePanel(
this, HIDE_DIALOG_LABEL, &hide_device_dialog),
445 sizer->Add(
new wxStaticLine(
this), flags);
446 sizer->Add(
new Buttons(
this, rule_path.c_str()), flags);
455 if (hide_device_dialog) {
459 auto& noteman = NotificationManager::GetInstance();
460 std::string msg =
"Device not found: ";
462 noteman.AddNotification(NotificationSeverity::kInformational, msg, 60);
467 auto dialog =
new DeviceRuleDialog(parent, device.c_str());
468 result = dialog->ShowModal();
477 auto dialog =
new DongleRuleDialog(parent);
478 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.
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.