Merge branch 'dk_login'

This commit is contained in:
Lukas Matena 2024-06-21 14:56:29 +02:00
commit bd6b570e95
17 changed files with 296 additions and 49 deletions

View File

@ -215,6 +215,8 @@ set(SLIC3R_GUI_SOURCES
GUI/ConfigWizard.cpp
GUI/ConfigWizard.hpp
GUI/ConfigWizard_private.hpp
GUI/ConfigWizardWebViewPage.cpp
GUI/ConfigWizardWebViewPage.hpp
GUI/MsgDialog.cpp
GUI/MsgDialog.hpp
GUI/UpdateDialogs.cpp

View File

@ -10,6 +10,7 @@
// FIXME: extract absolute units -> em
#include "ConfigWizard_private.hpp"
#include "ConfigWizardWebViewPage.hpp"
#include <algorithm>
#include <numeric>
@ -677,7 +678,7 @@ PageUpdateManager::PageUpdateManager(ConfigWizard* parent_in)
auto revert_page_selection = [this]() -> void {
CallAfter([this]() {
wizard_p()->index->go_to(1);
wizard_p()->index->go_to(this);
if (!this->IsShown())
this->Show();
});
@ -2586,6 +2587,7 @@ void ConfigWizard::priv::load_pages()
index->clear();
index->add_page(page_welcome);
index->add_page(page_login);
index->add_page(page_update_manager);
if (is_config_from_archive) {
@ -2772,7 +2774,7 @@ void ConfigWizard::priv::load_vendors()
void ConfigWizard::priv::add_page(ConfigWizardPage *page)
{
const int proportion = (page->shortname == _L("Filaments")) || (page->shortname == _L("SLA Materials")) ? 1 : 0;
const int proportion = (page == page_login || page == page_filaments || page == page_sla_materials);
hscroll_sizer->Add(page, proportion, wxEXPAND);
all_pages.push_back(page);
}
@ -3981,6 +3983,7 @@ ConfigWizard::ConfigWizard(wxWindow *parent)
wxGetApp().SetWindowVariantForButton(p->btn_cancel);
p->add_page(p->page_welcome = new PageWelcome(this));
p->add_page(p->page_login = new ConfigWizardWebViewPage(this));
p->add_page(p->page_update_manager = new PageUpdateManager(this));
// other pages will be loaded later after confirm repositories selection
@ -4033,8 +4036,9 @@ ConfigWizard::ConfigWizard(wxWindow *parent)
// In that case don't leave the page and the function above queried the user whether to install default materials.
return;
if (active_page == p->page_update_manager && p->index->active_is_last()) {
size_t next_active = p->index->pages_cnt();
p->page_update_manager->Hide();
p->index->go_to(2);
p->index->go_to(next_active);
return;
}
this->p->index->go_next();
@ -4132,6 +4136,16 @@ bool ConfigWizard::run(RunReason reason, StartPage start_page)
}
}
void ConfigWizard::update_login()
{
if (p->page_login && p->page_login->login_changed()) {
// repos changed - we need rebuild
wxGetApp().plater()->get_preset_archive_database()->sync_blocking();
// now change PageUpdateManager
p->page_update_manager->manager->update();
}
}
const wxString& ConfigWizard::name(const bool from_menu/* = false*/)
{
// A different naming convention is used for the Wizard on Windows & GTK vs. OSX.

View File

@ -87,6 +87,7 @@ public:
// Run the Wizard. Return whether it was completed.
bool run(RunReason reason, StartPage start_page = SP_WELCOME);
void update_login();
static const wxString& name(const bool from_menu = false);
protected:

View File

@ -0,0 +1,100 @@
#include "ConfigWizardWebViewPage.hpp"
#include "WebView.hpp"
#include "UserAccount.hpp"
#include "GUI_App.hpp"
#include "Plater.hpp"
#include "slic3r/GUI/I18N.hpp"
#include "format.hpp"
#include <wx/webview.h>
namespace Slic3r {
namespace GUI {
wxDEFINE_EVENT(EVT_LOGIN_VIA_WIZARD, Event<std::string>);
ConfigWizardWebViewPage::ConfigWizardWebViewPage(ConfigWizard *parent)
// TRN Config wizard page headline.
: ConfigWizardPage(parent, _L("Log into the Prusa Account (optional)"), _L("Log in (optional)"))
{
p_user_account = wxGetApp().plater()->get_user_account();
assert(p_user_account);
bool logged = p_user_account->is_logged();
// Create the webview
m_browser_sizer = new wxBoxSizer(wxHORIZONTAL);
m_browser = WebView::CreateWebView(this, p_user_account->get_login_redirect_url(), {});
if (!m_browser) {
// TRN Config wizard page with a log in page.
wxStaticText* fail_text = new wxStaticText(this, wxID_ANY, _L("Failed to load a web browser. Logging in is not possible in the moment."));
append(fail_text);
return;
}
if (logged) {
// TRN Config wizard page with a log in web.
m_text = new wxStaticText(this, wxID_ANY, format_wxstr("You are logged as %1%.", p_user_account->get_username()));
} else {
// TRN Config wizard page with a log in web. first line of text.
m_text = new wxStaticText(this, wxID_ANY, _L("Please log into your Prusa Account."));
// TRN Config wizard page with a log in web. second line of text.
}
append(m_text);
m_browser_sizer->Add(m_browser, 1, wxEXPAND);
append(m_browser_sizer, 1, wxEXPAND);
m_browser_sizer->Show(!logged);
// Connect the webview events
Bind(wxEVT_WEBVIEW_ERROR, &ConfigWizardWebViewPage::on_error, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_NAVIGATING, &ConfigWizardWebViewPage::on_navigation_request, this, m_browser->GetId());
}
bool ConfigWizardWebViewPage::login_changed()
{
assert(p_user_account && m_browser_sizer && m_text);
bool logged = p_user_account->is_logged();
m_browser_sizer->Show(!logged);
if (logged) {
// TRN Config wizard page with a log in web.
m_text->SetLabel(format_wxstr("You are logged as %1%.", p_user_account->get_username()));
} else {
// TRN Config wizard page with a log in web. first line of text.
m_text->SetLabel(_L("Please log into your Prusa Account."));
}
return logged;
}
void ConfigWizardWebViewPage::on_error(wxWebViewEvent &evt)
{
#define WX_ERROR_CASE(type) \
case type: \
category = #type; \
break;
wxString category;
switch (evt.GetInt())
{
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CONNECTION);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_CERTIFICATE);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_AUTH);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_SECURITY);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_NOT_FOUND);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_REQUEST);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_USER_CANCELLED);
WX_ERROR_CASE(wxWEBVIEW_NAV_ERR_OTHER);
}
BOOST_LOG_TRIVIAL(error) << "WebViewPanel error: " << category;
}
void ConfigWizardWebViewPage::on_navigation_request(wxWebViewEvent &evt)
{
wxString url = evt.GetURL();
if (url.starts_with(L"prusaslicer")) {
evt.Veto();
wxPostEvent(wxGetApp().plater(), Event<std::string>(EVT_LOGIN_VIA_WIZARD, into_u8(url)));
}
}
}} // namespace Slic3r::GUI

View File

@ -0,0 +1,63 @@
#ifndef slic3r_ConfigWizardWebViewPage_hpp_
#define slic3r_ConfigWizardWebViewPage_hpp_
#include "ConfigWizard_private.hpp"
#include "Event.hpp"
class wxWebView;
class wxWebViewEvent;
namespace Slic3r {
namespace GUI {
wxDECLARE_EVENT(EVT_LOGIN_VIA_WIZARD, Event<std::string>);
/*
struct ConfigWizardPage: wxPanel
{
ConfigWizard *parent;
const wxString shortname;
wxBoxSizer *content;
const unsigned indent;
ConfigWizardPage(ConfigWizard *parent, wxString title, wxString shortname, unsigned indent = 0);
virtual ~ConfigWizardPage();
template<class T>
T* append(T *thing, int proportion = 0, int flag = wxEXPAND|wxTOP|wxBOTTOM, int border = 10)
{
content->Add(thing, proportion, flag, border);
return thing;
}
wxStaticText* append_text(wxString text);
void append_spacer(int space);
ConfigWizard::priv *wizard_p() const { return parent->p.get(); }
virtual void apply_custom_config(DynamicPrintConfig &config) {}
virtual void set_run_reason(ConfigWizard::RunReason run_reason) {}
virtual void on_activate() {}
};
*/
class UserAccount;
class ConfigWizardWebViewPage : public ConfigWizardPage
{
public:
ConfigWizardWebViewPage( ConfigWizard *parent);
virtual ~ConfigWizardWebViewPage() {}
void on_error(wxWebViewEvent &evt);
void on_navigation_request(wxWebViewEvent &evt);
// returns true if logged in - wizard needs to update repos
bool login_changed();
private:
wxWebView *m_browser{nullptr};
UserAccount *p_user_account{nullptr};
wxBoxSizer *m_browser_sizer{nullptr};
wxStaticText *m_text{nullptr};
};
}} // namespace Slic3r::GUI
#endif

View File

@ -98,6 +98,7 @@ struct BundleMap : std::map<std::string /* = vendor ID */, Bundle>
struct Materials;
class RepositoryUpdateUIManager;
class ConfigWizardWebViewPage;
struct PrinterPickerEvent;
@ -549,6 +550,7 @@ public:
size_t active_item() const { return item_active; }
ConfigWizardPage* active_page() const;
bool active_is_last() const { return item_active < items.size() && item_active == last_page; }
size_t pages_cnt() const { return items.size(); }
void go_prev();
void go_next();
@ -631,6 +633,7 @@ struct ConfigWizard::priv
wxButton *btn_cancel = nullptr;
PageWelcome *page_welcome = nullptr;
ConfigWizardWebViewPage *page_login = nullptr;
PageUpdateManager*page_update_manager = nullptr;
PageMaterials *page_filaments = nullptr;
PageMaterials *page_sla_materials = nullptr;

View File

@ -3286,18 +3286,19 @@ bool GUI_App::run_wizard(ConfigWizard::RunReason reason, ConfigWizard::StartPage
preset_updater->config_update(app_config->orig_version(), PresetUpdater::UpdateParams::SHOW_TEXT_BOX, repos);
}
auto wizard = new ConfigWizard(mainframe);
m_config_wizard = new ConfigWizard(mainframe);
cw_loading_dlg->Close();
const bool res = wizard->run(reason, start_page);
const bool res = m_config_wizard->run(reason, start_page);
// !!! Deallocate memory after close ConfigWizard.
// Note, that mainframe is a parent of ConfigWizard.
// So, wizard will be destroyed only during destroying of mainframe
// To avoid this state the wizard have to be disconnected from mainframe and Destroyed explicitly
mainframe->RemoveChild(wizard);
wizard->Destroy();
mainframe->RemoveChild(m_config_wizard);
m_config_wizard->Destroy();
m_config_wizard = nullptr;
if (res) {
load_current_presets();
@ -3309,15 +3310,13 @@ bool GUI_App::run_wizard(ConfigWizard::RunReason reason, ConfigWizard::StartPage
return res;
}
#if 0
void GUI_App::update_login_dialog()
void GUI_App::update_wizard_login_page()
{
if (!m_login_dialog) {
if (!m_config_wizard) {
return;
}
m_login_dialog->update_account();
m_config_wizard->update_login();
}
#endif // 0
void GUI_App::show_desktop_integration_dialog()
{

View File

@ -59,7 +59,6 @@ class NotificationManager;
class Downloader;
struct GUI_InitParams;
class GalleryDialog;
class LoginDialog;
class PresetArchiveDatabase;
enum FileType
@ -371,9 +370,7 @@ public:
void open_web_page_localized(const std::string &http_address);
bool may_switch_to_SLA_preset(const wxString& caption);
bool run_wizard(ConfigWizard::RunReason reason, ConfigWizard::StartPage start_page = ConfigWizard::SP_WELCOME);
#if 0
void update_login_dialog();
#endif // 0
void update_wizard_login_page();
void show_desktop_integration_dialog();
void show_downloader_registration_dialog();
@ -454,9 +451,9 @@ private:
// change to vector of items when adding more items that require update
//wxMenuItem* m_login_config_menu_item { nullptr };
std::map< ConfigMenuIDs, wxMenuItem*> m_config_menu_updatable_items;
#if 0
std::unique_ptr<LoginDialog> m_login_dialog;
#endif // 0
ConfigWizard* m_config_wizard {nullptr};
};
DECLARE_APP(GUI_App)

View File

@ -97,6 +97,7 @@
#include "Mouse3DController.hpp"
#include "Tab.hpp"
#include "Jobs/ArrangeJob2.hpp"
#include "ConfigWizardWebViewPage.hpp"
#include "Jobs/RotoptimizeJob.hpp"
#include "Jobs/SLAImportJob.hpp"
@ -868,18 +869,19 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
BOOST_LOG_TRIVIAL(trace) << "Received login from other instance event.";
user_account->on_login_code_recieved(evt.data);
});
this->q->Bind(EVT_OPEN_PRUSAAUTH, [](OpenPrusaAuthEvent& evt) {
BOOST_LOG_TRIVIAL(info) << "open browser: " << evt.data;
// first register url to be sure to get the code back
//auto downloader_worker = new DownloaderUtils::Worker(nullptr);
DownloaderUtils::Worker::perform_register(wxGetApp().app_config->get("url_downloader_dest"));
#ifdef __linux__
if (DownloaderUtils::Worker::perform_registration_linux)
DesktopIntegrationDialog::perform_downloader_desktop_integration();
#endif // __linux__
// than open url
wxGetApp().open_login_browser_with_dialog(evt.data);
});
this->q->Bind(EVT_LOGIN_VIA_WIZARD, [this](Event<std::string> &evt) {
BOOST_LOG_TRIVIAL(trace) << "Received login from wizard.";
user_account->on_login_code_recieved(evt.data);
});
this->q->Bind(EVT_OPEN_PRUSAAUTH, [this](OpenPrusaAuthEvent& evt) {
BOOST_LOG_TRIVIAL(info) << "open login browser: " << evt.data;
std::string dialog_msg;
LoginWebViewDialog dialog(this->q, dialog_msg, evt.data);
if (dialog.ShowModal() != wxID_OK) {
return;
}
user_account->on_login_code_recieved(dialog_msg);
});
this->q->Bind(EVT_UA_LOGGEDOUT, [this](UserAccountSuccessEvent& evt) {
user_account->clear();
@ -890,10 +892,11 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
this->main_frame->refresh_account_menu(true);
// Update sidebar printer status
sidebar->update_printer_presets_combobox();
#if 0
wxGetApp().update_login_dialog();
#endif // 0
wxGetApp().update_wizard_login_page();
this->show_action_buttons(this->ready_to_slice);
LogoutWebViewDialog dlg(this->q);
dlg.ShowModal();
});
this->q->Bind(EVT_UA_ID_USER_SUCCESS, [this](UserAccountSuccessEvent& evt) {
@ -909,9 +912,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
this->main_frame->add_connect_webview_tab();
// Update User name in TopBar
this->main_frame->refresh_account_menu();
#if 0
wxGetApp().update_login_dialog();
#endif // 0
wxGetApp().update_wizard_login_page();
this->show_action_buttons(this->ready_to_slice);
} else {
@ -980,9 +981,6 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
fwrite(evt.data.c_str(), 1, evt.data.size(), file);
fclose(file);
this->main_frame->refresh_account_menu(true);
#if 0
wxGetApp().update_login_dialog();
#endif // 0
});
this->q->Bind(EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, [this](UserAccountSuccessEvent& evt) {
this->user_account->set_current_printer_data(evt.data);

View File

@ -60,8 +60,6 @@ class RepositoryUpdateUIManager
void fill_entries(bool init_selection = false);
void fill_grids();
void update();
void remove_offline_repos(const std::string& id);
void load_offline_repos();
void check_selection();
@ -71,6 +69,8 @@ public:
RepositoryUpdateUIManager(wxWindow* parent, PresetArchiveDatabase* pad, int em);
~RepositoryUpdateUIManager() {}
void update();
wxSizer* get_sizer() { return m_main_sizer; }
bool set_selected_repositories();
bool is_selection_changed() const { return m_is_selection_changed; }

View File

@ -38,6 +38,7 @@ public:
bool is_logged();
void do_login();
void do_logout();
wxString get_login_redirect_url() { return m_communication->get_login_redirect_url(); }
void set_remember_session(bool remember);
void toggle_remember_session();

View File

@ -8,6 +8,7 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/log/trivial.hpp>
#include <boost/beast/core/detail/base64.hpp>
#include <boost/algorithm/string.hpp>
#include <curl/curl.h>
#include <string>
@ -256,19 +257,25 @@ void UserAccountCommunication::on_uuid_map_success()
}
}
void UserAccountCommunication::login_redirect()
{
wxString UserAccountCommunication::get_login_redirect_url() {
const std::string AUTH_HOST = "https://account.prusa3d.com";
const std::string CLIENT_ID = client_id();
const std::string REDIRECT_URI = "prusaslicer://login";
CodeChalengeGenerator ccg;
m_code_verifier = ccg.generate_verifier();
std::string code_challenge = ccg.generate_chalenge(m_code_verifier);
wxString language = GUI::wxGetApp().current_language_code();
language = language.SubString(0, 1);
BOOST_LOG_TRIVIAL(info) << "code verifier: " << m_code_verifier;
BOOST_LOG_TRIVIAL(info) << "code challenge: " << code_challenge;
wxString url = GUI::format_wxstr(L"%1%/o/authorize/?client_id=%2%&response_type=code&code_challenge=%3%&code_challenge_method=S256&scope=basic_info&redirect_uri=%4%&choose_account=1", AUTH_HOST, CLIENT_ID, code_challenge, REDIRECT_URI);
wxString url = GUI::format_wxstr(L"%1%/o/authorize/?embed=1&client_id=%2%&response_type=code&code_challenge=%3%&code_challenge_method=S256&scope=basic_info&redirect_uri=%4%&choose_account=1&language=%5%", AUTH_HOST, CLIENT_ID, code_challenge, REDIRECT_URI, language);
return url;
}
void UserAccountCommunication::login_redirect()
{
wxString url = get_login_redirect_url();
wxQueueEvent(m_evt_handler,new OpenPrusaAuthEvent(GUI::EVT_OPEN_PRUSAAUTH, std::move(url)));
}

View File

@ -5,6 +5,8 @@
#include "Event.hpp"
#include "libslic3r/AppConfig.hpp"
#include <wx/timer.h>
#include <queue>
#include <condition_variable>
#include <map>
@ -40,6 +42,7 @@ public:
void do_login();
void do_logout();
void do_clear();
wxString get_login_redirect_url();
// Trigger function starts various remote operations
void enqueue_connect_status_action();
void enqueue_connect_printer_models_action();

View File

@ -7,7 +7,7 @@
#include <boost/log/trivial.hpp>
wxWebView* WebView::CreateWebView(wxWindow * parent, const wxString& url, std::vector<std::string>& message_handlers)
wxWebView* WebView::CreateWebView(wxWindow * parent, const wxString& url, const std::vector<std::string>& message_handlers)
{
#if wxUSE_WEBVIEW_EDGE
bool backend_available = wxWebView::IsBackendAvailable(wxWebViewBackendEdge);

View File

@ -10,7 +10,7 @@ class wxString;
namespace WebView
{
wxWebView *CreateWebView(wxWindow *parent, const wxString& url, std::vector<std::string>& message_handlers);
wxWebView *CreateWebView(wxWindow *parent, const wxString& url, const std::vector<std::string>& message_handlers);
};
#endif // !slic3r_GUI_WebView_hpp_

View File

@ -860,6 +860,8 @@ WebViewDialog::WebViewDialog(wxWindow* parent, const wxString& url, const wxStri
Bind(wxEVT_MENU, &WebViewDialog::on_run_script_custom, this, m_script_custom->GetId());
Bind(wxEVT_MENU, &WebViewDialog::on_add_user_script, this, addUserScript->GetId());
#endif
Bind(wxEVT_WEBVIEW_NAVIGATING, &WebViewDialog::on_navigation_request, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_LOADED, &WebViewDialog::on_loaded, this, m_browser->GetId());
Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent& evt) { EndModal(wxID_CANCEL); }));
@ -943,6 +945,11 @@ void WebViewDialog::on_reload_button(wxCommandEvent& WXUNUSED(evt))
m_browser->Reload();
}
void WebViewDialog::on_navigation_request(wxWebViewEvent &evt)
{
}
void WebViewDialog::on_script_message(wxWebViewEvent& evt)
{
}
@ -1248,6 +1255,38 @@ void PrinterPickWebViewDialog::request_compatible_printers_SLA()
wxString script = GUI::format_wxstr("window._prusaConnect_v1.requestCompatiblePrinter(%1%)", request);
run_script(script);
}
LoginWebViewDialog::LoginWebViewDialog(wxWindow *parent, std::string &ret_val, const wxString& url)
: WebViewDialog(parent
, url
, _L("Log in dialog")
, wxSize(parent->GetClientSize().x / 3, parent->GetClientSize().y / 4 * 3)
, {})
, m_ret_val(ret_val)
{
Centre();
}
void LoginWebViewDialog::on_navigation_request(wxWebViewEvent &evt)
{
wxString url = evt.GetURL();
if (url.starts_with(L"prusaslicer")) {
evt.Veto();
m_ret_val = into_u8(url);
EndModal(wxID_OK);
}
}
LogoutWebViewDialog::LogoutWebViewDialog(wxWindow *parent)
: WebViewDialog(parent
, L"https://account.prusa3d.com/logout"
, _L("Logout dialog")
, wxSize(std::max(parent->GetClientSize().x / 4, 10 * wxGetApp().em_unit()), std::max(parent->GetClientSize().y / 4, 10 * wxGetApp().em_unit()))
, {})
{
Centre();
}
void LogoutWebViewDialog::on_loaded(wxWebViewEvent &evt)
{
EndModal(wxID_OK);
}
} // GUI
} // Slic3r

View File

@ -104,8 +104,8 @@ public:
WebViewDialog(wxWindow* parent, const wxString& url, const wxString& dialog_name, const wxSize& size, const std::vector<std::string>& message_handler_names, const std::string& loading_html = "loading");
virtual ~WebViewDialog();
virtual void on_show(wxShowEvent& evt) = 0;
virtual void on_script_message(wxWebViewEvent& evt) = 0;
virtual void on_show(wxShowEvent& evt) {};
virtual void on_script_message(wxWebViewEvent& evt);
void on_idle(wxIdleEvent& evt);
void on_url(wxCommandEvent& evt);
@ -127,6 +127,9 @@ public:
void on_select_all(wxCommandEvent& evt);
void On_enable_context_menu(wxCommandEvent& evt);
void On_enable_dev_tools(wxCommandEvent& evt);
virtual void on_navigation_request(wxWebViewEvent &evt);
virtual void on_loaded(wxWebViewEvent &evt) {}
void run_script(const wxString& javascript);
@ -247,6 +250,23 @@ public:
SourceViewDialog(wxWindow* parent, wxString source);
};
class LoginWebViewDialog : public WebViewDialog
{
public:
LoginWebViewDialog(wxWindow *parent, std::string &ret_val, const wxString& url);
void on_navigation_request(wxWebViewEvent &evt) override;
private:
std::string &m_ret_val;
};
class LogoutWebViewDialog : public WebViewDialog
{
public:
LogoutWebViewDialog(wxWindow* parent);
void on_loaded(wxWebViewEvent &evt) override;
};
} // GUI
} // Slic3r