Support for SimplyPrint cloud integration (#4525)

* Make httpserver more generic and reusable

* Add OAuthJob

* Fix issue caused by the fact that the backing widget of the `TextCtrl` is no longer `wxTextCtrl`

* Implement login and token refresh

* Implement file upload

* Try fix build error

* Support BBL printers

* Show error message if user hasn't done OAuth

* Fix typo

* Update error message

* Disable unsupported options when SimplyPrint is selected
This commit is contained in:
Noisyfox 2024-03-23 10:08:48 +08:00 committed by GitHub
parent f3b3e92782
commit e29bbac193
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 1075 additions and 228 deletions

View file

@ -34,6 +34,8 @@
#include "BitmapCache.hpp"
#include "BonjourDialog.hpp"
#include "MsgDialog.hpp"
#include "OAuthDialog.hpp"
#include "SimplyPrint.hpp"
namespace Slic3r {
namespace GUI {
@ -174,20 +176,24 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
result = host->test(msg);
if (!result && host->is_cloud()) {
PrinterCloudAuthDialog dlg(this->GetParent(), host.get());
dlg.ShowModal();
if (const auto h = dynamic_cast<SimplyPrint*>(host.get()); h) {
OAuthDialog dlg(this, h->get_oauth_params());
dlg.ShowModal();
auto api_key = dlg.GetApiKey();
m_config->opt_string("printhost_apikey") = api_key;
result = !api_key.empty();
if (result) {
if (Field* print_host_webui_field = this->m_optgroup->get_field("printhost_apikey"); print_host_webui_field) {
if (TextInput* temp_input = dynamic_cast<TextInput*>(print_host_webui_field->getWindow()); temp_input) {
if (wxTextCtrl* temp = temp_input->GetTextCtrl()) {
temp->SetValue(wxString(api_key));
}
}
const auto& r = dlg.get_result();
result = r.success;
if (r.success) {
h->save_oauth_credential(r);
} else {
msg = r.error_message;
}
} else {
PrinterCloudAuthDialog dlg(this->GetParent(), host.get());
dlg.ShowModal();
const auto api_key = dlg.GetApiKey();
m_config->opt_string("printhost_apikey") = api_key;
result = !api_key.empty();
}
}
}
@ -195,11 +201,36 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
show_info(this, host->get_test_ok_msg(), _L("Success!"));
else
show_error(this, host->get_test_failed_msg(msg));
update();
});
return sizer;
};
auto print_host_logout = [&](wxWindow* parent) {
auto sizer = create_sizer_with_btn(parent, &m_printhost_logout_btn, "", _L("Log Out"));
m_printhost_logout_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& e) {
std::unique_ptr<PrintHost> host(PrintHost::get_print_host(m_config));
if (!host) {
const wxString text = _L("Could not get a valid Printer Host reference");
show_error(this, text);
return;
}
wxString msg_text = _L("Are you sure to log out?");
MessageDialog dialog(this, msg_text, "", wxICON_QUESTION | wxYES_NO);
if (dialog.ShowModal() == wxID_YES) {
host->log_out();
update();
}
});
return sizer;
};
auto print_host_printers = [this, create_sizer_with_btn](wxWindow* parent) {
//add_scaled_button(parent, &m_printhost_port_browse_btn, "browse", _(L("Refresh Printers")), wxBU_LEFT | wxBU_EXACTFIT);
auto sizer = create_sizer_with_btn(parent, &m_printhost_port_browse_btn, "monitor_signal_strong", _(L("Refresh Printers")));
@ -215,6 +246,7 @@ void PhysicalPrinterDialog::build_printhost_settings(ConfigOptionsGroup* m_optgr
Line host_line = m_optgroup->create_single_option_line(option);
host_line.append_widget(printhost_browse);
host_line.append_widget(print_host_test);
host_line.append_widget(print_host_logout);
m_optgroup->append_line(host_line);
option = m_optgroup->get_option("print_host_webui");
@ -375,7 +407,9 @@ void PhysicalPrinterDialog::update_printhost_buttons()
std::unique_ptr<PrintHost> host(PrintHost::get_print_host(m_config));
if (host) {
m_printhost_test_btn->Enable(!m_config->opt_string("print_host").empty() && host->can_test());
m_printhost_browse_btn->Enable(host->has_auto_discovery());
m_printhost_browse_btn->Show(host->has_auto_discovery());
m_printhost_logout_btn->Show(host->is_logged_in());
m_printhost_test_btn->SetLabel(host->is_cloud() ? _L("Login/Test") : _L("Test"));
}
}
@ -469,15 +503,29 @@ void PhysicalPrinterDialog::update(bool printer_change)
const auto opt = m_config->option<ConfigOptionEnum<PrintHostType>>("host_type");
m_optgroup->show_field("host_type");
// hide PrusaConnect address
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (wxTextCtrl* temp = dynamic_cast<wxTextCtrl*>(printhost_field->getWindow()); temp && temp->GetValue() == L"https://connect.prusa3d.com") {
temp->SetValue(wxString());
}
m_optgroup->enable_field("print_host");
m_optgroup->enable_field("print_host_webui");
m_optgroup->enable_field("printhost_cafile");
m_optgroup->enable_field("printhost_ssl_ignore_revoke");
if (m_printhost_cafile_browse_btn)
m_printhost_cafile_browse_btn->Enable();
if (TextInput* temp_input = dynamic_cast<TextInput*>(printhost_field->getWindow()); temp_input) {
if (wxTextCtrl* temp = temp_input->GetTextCtrl(); temp &&temp->GetValue() == L"https://app.obico.io") {
temp->SetValue(wxString());
// hide pre-configured address, in case user switched to a different host type
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_field)->text_ctrl(); temp) {
const auto current_host = temp->GetValue();
if (current_host == L"https://connect.prusa3d.com" ||
current_host == L"https://app.obico.io" ||
current_host == "https://simplyprint.io") {
temp->SetValue(wxString());
}
}
}
if (Field* printhost_webui_field = m_optgroup->get_field("print_host_webui"); printhost_webui_field) {
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_webui_field)->text_ctrl(); temp) {
const auto current_host = temp->GetValue();
if (current_host == "https://simplyprint.io/panel") {
temp->SetValue(wxString());
}
}
}
@ -492,25 +540,42 @@ void PhysicalPrinterDialog::update(bool printer_change)
m_optgroup->show_field("printhost_apikey", true);
for (const std::string& opt_key : std::vector<std::string>{ "printhost_user", "printhost_password" })
m_optgroup->hide_field(opt_key);
supports_multiple_printers = opt && opt->value == htRepetier;
supports_multiple_printers = opt->value == htRepetier || opt->value == htObico;
if (opt->value == htPrusaConnect) { // automatically show default prusaconnect address
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (wxTextCtrl* temp = dynamic_cast<wxTextCtrl*>(printhost_field->getWindow()); temp && temp->GetValue().IsEmpty()) {
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_field)->text_ctrl(); temp && temp->GetValue().IsEmpty()) {
temp->SetValue(L"https://connect.prusa3d.com");
}
}
}
}
if (opt->value == htObico) {
supports_multiple_printers = true;
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (TextInput* temp_input = dynamic_cast<TextInput*>(printhost_field->getWindow()); temp_input) {
if (wxTextCtrl* temp = temp_input->GetTextCtrl(); temp && temp->GetValue().IsEmpty()) {
} else if (opt->value == htObico) { // automatically show default obico address
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_field)->text_ctrl(); temp && temp->GetValue().IsEmpty()) {
temp->SetValue(L"https://app.obico.io");
m_config->opt_string("print_host") = "https://app.obico.io";
}
}
} else if (opt->value == htSimplyPrint) {
// Set the host url
if (Field* printhost_field = m_optgroup->get_field("print_host"); printhost_field) {
printhost_field->disable();
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_field)->text_ctrl(); temp && temp->GetValue().IsEmpty()) {
temp->SetValue("https://simplyprint.io");
}
m_config->opt_string("print_host") = "https://simplyprint.io";
}
if (Field* printhost_webui_field = m_optgroup->get_field("print_host_webui"); printhost_webui_field) {
printhost_webui_field->disable();
if (wxTextCtrl* temp = dynamic_cast<TextCtrl*>(printhost_webui_field)->text_ctrl(); temp && temp->GetValue().IsEmpty()) {
temp->SetValue("https://simplyprint.io/panel");
}
m_config->opt_string("print_host_webui") = "https://simplyprint.io/panel";
}
m_optgroup->hide_field("printhost_apikey");
m_optgroup->disable_field("printhost_cafile");
m_optgroup->disable_field("printhost_ssl_ignore_revoke");
if (m_printhost_cafile_browse_btn)
m_printhost_cafile_browse_btn->Disable();
}
}
@ -599,6 +664,7 @@ void PhysicalPrinterDialog::on_dpi_changed(const wxRect& suggested_rect)
m_printhost_browse_btn->msw_rescale();
m_printhost_test_btn->msw_rescale();
m_printhost_logout_btn->msw_rescale();
if (m_printhost_cafile_browse_btn)
m_printhost_cafile_browse_btn->msw_rescale();