diff --git a/src/slic3r/GUI/Event.hpp b/src/slic3r/GUI/Event.hpp index a3989d7330..9fbf2d8fcc 100644 --- a/src/slic3r/GUI/Event.hpp +++ b/src/slic3r/GUI/Event.hpp @@ -2,6 +2,7 @@ #define slic3r_Events_hpp_ #include +#include #include @@ -86,6 +87,27 @@ template struct Event : public wxEvent } }; + +class LoadPrinterViewEvent : public wxCommandEvent +{ +public: + LoadPrinterViewEvent(wxEventType commandType = wxEVT_NULL, int winid = 0) + : wxCommandEvent(commandType, winid) + { } + + LoadPrinterViewEvent(const LoadPrinterViewEvent& event) + : wxCommandEvent(event) + { m_APIkey = event.m_APIkey; } + + const wxString& GetAPIkey() const { return m_APIkey; } + void SetAPIkey(const wxString& apikey) { m_APIkey = apikey; } + + virtual wxEvent *Clone() const wxOVERRIDE { return new LoadPrinterViewEvent(*this); } + +private: + wxString m_APIkey; + +}; } } diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 325e082987..cbbf581e9b 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -84,7 +84,7 @@ wxDEFINE_EVENT(EVT_UPDATE_PRESET_CB, SimpleEvent); // BBS: backup wxDEFINE_EVENT(EVT_BACKUP_POST, wxCommandEvent); wxDEFINE_EVENT(EVT_LOAD_URL, wxCommandEvent); -wxDEFINE_EVENT(EVT_LOAD_PRINTER_URL, wxCommandEvent); +wxDEFINE_EVENT(EVT_LOAD_PRINTER_URL, LoadPrinterViewEvent); enum class ERescaleTarget { @@ -1051,13 +1051,14 @@ void MainFrame::init_tabpanel() { m_tabpanel->AddPage(m_monitor, _L("Device"), std::string("tab_monitor_active"), std::string("tab_monitor_active")); m_printer_view = new PrinterWebView(m_tabpanel); - Bind(EVT_LOAD_PRINTER_URL, [this](wxCommandEvent &evt) { + Bind(EVT_LOAD_PRINTER_URL, [this](LoadPrinterViewEvent &evt) { wxString url = evt.GetString(); + wxString key = evt.GetAPIkey(); //select_tab(MainFrame::tpMonitor); - m_printer_view->load_url(url); + m_printer_view->load_url(url, key); }); m_printer_view->Hide(); - + m_project = new ProjectPanel(m_tabpanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); m_project->SetBackgroundColour(*wxWHITE); m_tabpanel->AddPage(m_project, _L("Project"), std::string("tab_auxiliary_avtice"), std::string("tab_auxiliary_avtice")); @@ -1090,22 +1091,22 @@ void MainFrame::show_device(bool bBBLPrinter) { if (bBBLPrinter) { if (m_tabpanel->GetPage(tpMonitor) != m_monitor) { m_printer_view->Hide(); - m_monitor->Show(true); + m_monitor->Show(true); m_tabpanel->RemovePage(tpMonitor); m_tabpanel->InsertPage(tpMonitor, m_monitor, _L("Device"), std::string("tab_monitor_active"), std::string("tab_monitor_active")); - m_tabpanel->SetSelection(tp3DEditor); + //m_tabpanel->SetSelection(tp3DEditor); } } else { if (m_tabpanel->GetPage(tpMonitor) != m_printer_view) { m_printer_view->Show(); - m_monitor->Show(false); + m_monitor->Show(false); m_tabpanel->RemovePage(tpMonitor); m_tabpanel->InsertPage(tpMonitor, m_printer_view, _L("Device"), std::string("tab_monitor_active"), std::string("tab_monitor_active")); - m_tabpanel->SetSelection(tp3DEditor); + //m_tabpanel->SetSelection(tp3DEditor); } } @@ -3486,11 +3487,12 @@ void MainFrame::load_url(wxString url) wxQueueEvent(this, evt); } -void MainFrame::load_printer_url(wxString url) +void MainFrame::load_printer_url(wxString url, wxString apikey) { BOOST_LOG_TRIVIAL(trace) << "load_printer_url:" << url; - auto evt = new wxCommandEvent(EVT_LOAD_PRINTER_URL, this->GetId()); + auto evt = new LoadPrinterViewEvent(EVT_LOAD_PRINTER_URL, this->GetId()); evt->SetString(url); + evt->SetAPIkey(apikey); wxQueueEvent(this, evt); } @@ -3499,15 +3501,17 @@ void MainFrame::load_printer_url() PresetBundle &preset_bundle = *wxGetApp().preset_bundle; if (preset_bundle.is_bbl_vendor()) return; - - auto cfg = preset_bundle.printers.get_edited_preset().config; - wxString url = - cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui"); + + auto cfg = preset_bundle.printers.get_edited_preset().config; + wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui"); + wxString apikey; + if (cfg.has("printhost_apikey")) + apikey = cfg.opt_string("printhost_apikey"); if (!url.empty()) { if (!url.Lower().starts_with("http")) url = wxString::Format("http://%s", url); - load_printer_url(url); + load_printer_url(url, apikey); } } diff --git a/src/slic3r/GUI/MainFrame.hpp b/src/slic3r/GUI/MainFrame.hpp index 0361ed7c54..42b37fe53f 100644 --- a/src/slic3r/GUI/MainFrame.hpp +++ b/src/slic3r/GUI/MainFrame.hpp @@ -335,7 +335,7 @@ public: //BBS void load_url(wxString url); - void load_printer_url(wxString url); + void load_printer_url(wxString url, wxString apikey = ""); void load_printer_url(); bool is_printer_view() const; void refresh_plugin_tips(); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 43424d98be..354f0764a3 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -1067,7 +1067,10 @@ void Sidebar::update_all_preset_comboboxes() if(!url.Lower().starts_with("http")) url = wxString::Format("http://%s",url); - p_mainframe->load_printer_url(url); + wxString apikey; + if (cfg.has("printhost_apikey")) + apikey = cfg.opt_string("printhost_apikey"); + p_mainframe->load_printer_url(url, apikey); } m_bed_type_list->SelectAndNotify(btPEI-1); diff --git a/src/slic3r/GUI/PrinterWebView.cpp b/src/slic3r/GUI/PrinterWebView.cpp index 5830d13798..991cadb065 100644 --- a/src/slic3r/GUI/PrinterWebView.cpp +++ b/src/slic3r/GUI/PrinterWebView.cpp @@ -1,12 +1,14 @@ #include "PrinterWebView.hpp" #include "I18N.hpp" +#include "slic3r/GUI/PrinterWebView.hpp" #include "slic3r/GUI/wxExtensions.hpp" #include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/MainFrame.hpp" #include "libslic3r_version.h" #include +#include #include #include @@ -61,13 +63,15 @@ PrinterWebView::~PrinterWebView() } -void PrinterWebView::load_url(wxString& url) +void PrinterWebView::load_url(wxString& url, wxString apikey) { // this->Show(); // this->Raise(); if (m_browser == nullptr) return; m_browser->LoadURL(url); + if(!apikey.IsEmpty()) + SendAPIKey(apikey); //m_browser->SetFocus(); UpdateState(); } @@ -85,6 +89,23 @@ void PrinterWebView::OnClose(wxCloseEvent& evt) this->Hide(); } +void PrinterWebView::SendAPIKey(wxString apikey) +{ + // After the page is fully loaded, run: + wxString script = wxString::Format(R"( + // Example for overriding the global fetch method + const originalFetch = window.fetch; + window.fetch = function(input, init = {}) { + init.headers = init.headers || {}; + init.headers['X-API-Key'] = '%s'; + return originalFetch(input, init); + }; +)", + apikey); + + m_browser->RunScript(script); +} + void PrinterWebView::OnError(wxWebViewEvent &evt) { auto e = "unknown error"; diff --git a/src/slic3r/GUI/PrinterWebView.hpp b/src/slic3r/GUI/PrinterWebView.hpp index bdf5f4c68f..7ccc833ec8 100644 --- a/src/slic3r/GUI/PrinterWebView.hpp +++ b/src/slic3r/GUI/PrinterWebView.hpp @@ -7,6 +7,7 @@ #include "wx/notifmsg.h" #include "wx/settings.h" #include "wx/webview.h" +#include #if wxUSE_WEBVIEW_EDGE #include "wx/msw/webview_edge.h" @@ -35,12 +36,12 @@ public: PrinterWebView(wxWindow *parent); virtual ~PrinterWebView(); - void load_url(wxString& url); + void load_url(wxString& url, wxString apikey = ""); void UpdateState(); void OnClose(wxCloseEvent& evt); void OnError(wxWebViewEvent& evt); - private: + void SendAPIKey(wxString apikey); wxWebView* m_browser; long m_zoomFactor;