From 9fd3108726c04aa842000c6222361289ec723571 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Fri, 6 May 2022 14:47:20 +0200 Subject: [PATCH] Resolved IP dialog before uploading - needs better design and to not show if only 1x v4 and v6 is resolved. --- src/slic3r/GUI/BonjourDialog.cpp | 42 ++++++++++++++++++++++++++++++++ src/slic3r/GUI/BonjourDialog.hpp | 19 +++++++++++++-- src/slic3r/Utils/OctoPrint.cpp | 31 +++++++++++++++-------- src/slic3r/Utils/OctoPrint.hpp | 3 +-- 4 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/slic3r/GUI/BonjourDialog.cpp b/src/slic3r/GUI/BonjourDialog.cpp index 516b1ab4a..69ab23896 100644 --- a/src/slic3r/GUI/BonjourDialog.cpp +++ b/src/slic3r/GUI/BonjourDialog.cpp @@ -233,7 +233,49 @@ void BonjourDialog::on_timer_process() } } +IPListDialog::IPListDialog(wxWindow* parent, const wxString& hostname, const std::vector& ips, size_t& selected_index) + : wxDialog(parent, wxID_ANY, _(L("Multiple resolved IP addresses")), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) + , m_list(new wxListView(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxSIMPLE_BORDER)) + , m_selected_index (selected_index) +{ + const int em = GUI::wxGetApp().em_unit(); + m_list->SetMinSize(wxSize(20 * em, 30 * em)); + wxBoxSizer* vsizer = new wxBoxSizer(wxVERTICAL); + auto* label = new wxStaticText(this, wxID_ANY, GUI::format_wxstr(_L("There are several IP addresses resolving to hostname %1%.\n Please select one that should be used."), hostname)); + vsizer->Add(label, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, em); + + m_list->SetSingleStyle(wxLC_SINGLE_SEL); + m_list->AppendColumn(_(L("Address")), wxLIST_FORMAT_LEFT, 20 * em); + + for (size_t i = 0; i < ips.size(); i++) + auto item = m_list->InsertItem(i, boost::nowide::widen(ips[i].to_string())); + + m_list->Select(0); + + vsizer->Add(m_list, 1, wxEXPAND | wxALL, em); + + wxBoxSizer* button_sizer = new wxBoxSizer(wxHORIZONTAL); + button_sizer->Add(new wxButton(this, wxID_OK, "OK"), 0, wxALL, em); + button_sizer->Add(new wxButton(this, wxID_CANCEL, "Cancel"), 0, wxALL, em); + + vsizer->Add(button_sizer, 0, wxALIGN_CENTER); + SetSizerAndFit(vsizer); + + GUI::wxGetApp().UpdateDlgDarkUI(this); +} + +IPListDialog::~IPListDialog() +{ +} + +void IPListDialog::EndModal(int retCode) +{ + if (retCode == wxID_OK) { + m_selected_index = (size_t)m_list->GetFirstSelected(); + } + wxDialog::EndModal(retCode); +} } diff --git a/src/slic3r/GUI/BonjourDialog.hpp b/src/slic3r/GUI/BonjourDialog.hpp index def0838d7..6d1af491b 100644 --- a/src/slic3r/GUI/BonjourDialog.hpp +++ b/src/slic3r/GUI/BonjourDialog.hpp @@ -4,6 +4,7 @@ #include #include +#include #include "libslic3r/PrintConfig.hpp" @@ -11,7 +12,7 @@ class wxListView; class wxStaticText; class wxTimer; class wxTimerEvent; - +class boost::asio::ip::address; namespace Slic3r { @@ -41,12 +42,26 @@ private: unsigned timer_state; Slic3r::PrinterTechnology tech; - void on_reply(BonjourReplyEvent &); + virtual void on_reply(BonjourReplyEvent &); void on_timer(wxTimerEvent &); void on_timer_process(); }; +class IPListDialog : public wxDialog +{ +public: + IPListDialog(wxWindow* parent, const wxString& hostname, const std::vector& ips, size_t& selected_index); + IPListDialog(IPListDialog&&) = delete; + IPListDialog(const IPListDialog&) = delete; + IPListDialog& operator=(IPListDialog&&) = delete; + IPListDialog& operator=(const IPListDialog&) = delete; + ~IPListDialog(); + virtual void EndModal(int retCode) wxOVERRIDE; +private: + wxListView* m_list; + size_t& m_selected_index; +}; } diff --git a/src/slic3r/Utils/OctoPrint.cpp b/src/slic3r/Utils/OctoPrint.cpp index 4bbef8d38..3b18b1960 100644 --- a/src/slic3r/Utils/OctoPrint.cpp +++ b/src/slic3r/Utils/OctoPrint.cpp @@ -20,6 +20,7 @@ #include "Http.hpp" #include "libslic3r/AppConfig.hpp" #include "Bonjour.hpp" +#include "slic3r/GUI/BonjourDialog.hpp" namespace fs = boost::filesystem; namespace pt = boost::property_tree; @@ -115,7 +116,7 @@ bool OctoPrint::test_with_resolved_ip(wxString &msg) const auto url = substitute_host(make_url("api/version"), GUI::into_u8(msg)); msg.Clear(); - BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Get version at: %2%") % name % url; + BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Get version at: %2%") % name % url; auto http = Http::get(std::move(url)); set_auth(http); @@ -126,7 +127,7 @@ bool OctoPrint::test_with_resolved_ip(wxString &msg) const msg = format_error(body, error, status); }) .on_complete([&, this](std::string body, unsigned) { - BOOST_LOG_TRIVIAL(debug) << boost::format("%1%: Got version: %2%") % name % body; + BOOST_LOG_TRIVIAL(error) << boost::format("%1%: Got version: %2%") % name % body; try { std::stringstream ss(body); @@ -253,30 +254,40 @@ bool OctoPrint::upload(PrintHostUpload upload_data, ProgressFn prorgess_fn, Erro } if (resolved_addr.empty()) { BOOST_LOG_TRIVIAL(error) << "PrusaSlicer failed to resolve hostname " << m_host << " into the IP address. Starting upload with system resolving."; - return false;//upload_inner_with_host(upload_data, prorgess_fn, error_fn); + return upload_inner_with_host(upload_data, prorgess_fn, error_fn); } - return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr); + if (resolved_addr.size() > 1) { + size_t selected_index = resolved_addr.size(); + IPListDialog dialog(nullptr, boost::nowide::widen(m_host), resolved_addr, selected_index); + if (dialog.ShowModal() == wxID_OK && selected_index < resolved_addr.size()) { + return upload_inner_with_resolved_ip(upload_data, prorgess_fn, error_fn, resolved_addr[selected_index]); + } + } + return false; #endif // WIN32 } #ifdef WIN32 -bool OctoPrint::upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const std::vector& resolved_addr) const +bool OctoPrint::upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const boost::asio::ip::address& resolved_addr) const { wxString error_message; - for (const auto& ip : resolved_addr) { + //for (const auto& ip : resolved_addr) + { // If test fails, test_msg_or_host_ip contains the error message. // Otherwise on Windows it contains the resolved IP address of the host. // Test_msg already contains resolved ip and will be cleared on start of test(). - wxString test_msg_or_host_ip = GUI::from_u8(ip.to_string()); + wxString test_msg_or_host_ip = GUI::from_u8(resolved_addr.to_string()); if (!test_with_resolved_ip(test_msg_or_host_ip)) { error_message = test_msg_or_host_ip; - BOOST_LOG_TRIVIAL(info) << test_msg_or_host_ip; - continue; + //BOOST_LOG_TRIVIAL(info) << test_msg_or_host_ip; + error_fn(std::move(error_message)); + //continue; + return false; } const char* name = get_name(); const auto upload_filename = upload_data.upload_path.filename(); const auto upload_parent_path = upload_data.upload_path.parent_path(); - std::string url = substitute_host(make_url("api/files/local"), ip.to_string()); + std::string url = substitute_host(make_url("api/files/local"), resolved_addr.to_string()); bool result = true; BOOST_LOG_TRIVIAL(info) << boost::format("%1%: Uploading file %2% at %3%, filename: %4%, path: %5%, print: %6%") diff --git a/src/slic3r/Utils/OctoPrint.hpp b/src/slic3r/Utils/OctoPrint.hpp index 96a21d7c0..deaeec888 100644 --- a/src/slic3r/Utils/OctoPrint.hpp +++ b/src/slic3r/Utils/OctoPrint.hpp @@ -37,12 +37,11 @@ public: protected: virtual bool validate_version_text(const boost::optional &version_text) const; #ifdef WIN32 - virtual bool upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const std::vector& resolved_addr) const; + virtual bool upload_inner_with_resolved_ip(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, const boost::asio::ip::address& resolved_addr) const; virtual bool test_with_resolved_ip(wxString& curl_msg) const; #endif virtual bool upload_inner_with_host(PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn) const; - private: std::string m_host; std::string m_apikey;