Rename Auth to UserAccount

This commit is contained in:
David Kocik 2024-01-29 11:07:33 +01:00
parent 5791bcb43f
commit f3dd5fa17d
13 changed files with 210 additions and 199 deletions

View File

@ -130,7 +130,7 @@ bool load_secret(const std::string& opt, std::string& usr, std::string& psswd)
} }
} }
PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppConfig* app_config) UserAccountCommunication::UserAccountCommunication(wxEvtHandler* evt_handler, AppConfig* app_config)
: m_evt_handler(evt_handler) : m_evt_handler(evt_handler)
, m_app_config(app_config) , m_app_config(app_config)
{ {
@ -147,14 +147,14 @@ PrusaAuthCommunication::PrusaAuthCommunication(wxEvtHandler* evt_handler, AppCon
shared_session_key = m_app_config->get("shared_session_key"); shared_session_key = m_app_config->get("shared_session_key");
} }
bool has_token = !access_token.empty() && !refresh_token.empty(); bool has_token = !access_token.empty() && !refresh_token.empty();
m_session = std::make_unique<AuthSession>(evt_handler, access_token, refresh_token, shared_session_key, m_app_config->get_bool("connect_polling")); m_session = std::make_unique<UserAccountSession>(evt_handler, access_token, refresh_token, shared_session_key, m_app_config->get_bool("connect_polling"));
init_session_thread(); init_session_thread();
// perform login at the start, but only with tokens // perform login at the start, but only with tokens
if (has_token) if (has_token)
do_login(); do_login();
} }
PrusaAuthCommunication::~PrusaAuthCommunication() { UserAccountCommunication::~UserAccountCommunication() {
if (m_thread.joinable()) { if (m_thread.joinable()) {
// Stop the worker thread, if running. // Stop the worker thread, if running.
{ {
@ -168,7 +168,7 @@ PrusaAuthCommunication::~PrusaAuthCommunication() {
} }
} }
void PrusaAuthCommunication::set_username(const std::string& username) void UserAccountCommunication::set_username(const std::string& username)
{ {
m_username = username; m_username = username;
{ {
@ -185,14 +185,14 @@ void PrusaAuthCommunication::set_username(const std::string& username)
} }
} }
void PrusaAuthCommunication::set_remember_session(bool b) void UserAccountCommunication::set_remember_session(bool b)
{ {
m_remember_session = b; m_remember_session = b;
// tokens needs to be stored or deleted // tokens needs to be stored or deleted
set_username(m_username); set_username(m_username);
} }
std::string PrusaAuthCommunication::get_access_token() std::string UserAccountCommunication::get_access_token()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -200,7 +200,7 @@ std::string PrusaAuthCommunication::get_access_token()
} }
} }
void PrusaAuthCommunication::set_polling_enabled(bool enabled) void UserAccountCommunication::set_polling_enabled(bool enabled)
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -208,7 +208,7 @@ void PrusaAuthCommunication::set_polling_enabled(bool enabled)
} }
} }
void PrusaAuthCommunication::login_redirect() void UserAccountCommunication::login_redirect()
{ {
const std::string AUTH_HOST = "https://test-account.prusa3d.com"; const std::string AUTH_HOST = "https://test-account.prusa3d.com";
const std::string CLIENT_ID = client_id(); const std::string CLIENT_ID = client_id();
@ -223,11 +223,11 @@ void PrusaAuthCommunication::login_redirect()
wxQueueEvent(m_evt_handler,new OpenPrusaAuthEvent(GUI::EVT_OPEN_PRUSAAUTH, std::move(url))); wxQueueEvent(m_evt_handler,new OpenPrusaAuthEvent(GUI::EVT_OPEN_PRUSAAUTH, std::move(url)));
} }
bool PrusaAuthCommunication::is_logged() bool UserAccountCommunication::is_logged()
{ {
return !m_username.empty(); return !m_username.empty();
} }
void PrusaAuthCommunication::do_login() void UserAccountCommunication::do_login()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -239,13 +239,13 @@ void PrusaAuthCommunication::do_login()
} }
wakeup_session_thread(); wakeup_session_thread();
} }
void PrusaAuthCommunication::do_logout() void UserAccountCommunication::do_logout()
{ {
do_clear(); do_clear();
wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(GUI::EVT_LOGGEDOUT_PRUSAAUTH, {})); wxQueueEvent(m_evt_handler, new UserAccountSuccessEvent(GUI::EVT_UA_LOGGEDOUT, {}));
} }
void PrusaAuthCommunication::do_clear() void UserAccountCommunication::do_clear()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -254,7 +254,7 @@ void PrusaAuthCommunication::do_clear()
set_username({}); set_username({});
} }
void PrusaAuthCommunication::on_login_code_recieved(const std::string& url_message) void UserAccountCommunication::on_login_code_recieved(const std::string& url_message)
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -265,19 +265,19 @@ void PrusaAuthCommunication::on_login_code_recieved(const std::string& url_messa
} }
#if 0 #if 0
void PrusaAuthCommunication::enqueue_user_id_action() void UserAccountCommunication::enqueue_user_id_action()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
if (!m_session->is_initialized()) { if (!m_session->is_initialized()) {
return; return;
} }
m_session->enqueue_action(UserActionID::USER_ID, nullptr, nullptr, {}); m_session->enqueue_action(UserAccountActionID::USER_ID, nullptr, nullptr, {});
} }
wakeup_session_thread(); wakeup_session_thread();
} }
void PrusaAuthCommunication::enqueue_connect_dummy_action() void UserAccountCommunication::enqueue_connect_dummy_action()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -285,13 +285,13 @@ void PrusaAuthCommunication::enqueue_connect_dummy_action()
BOOST_LOG_TRIVIAL(error) << "Connect Dummy endpoint connection failed - Not Logged in."; BOOST_LOG_TRIVIAL(error) << "Connect Dummy endpoint connection failed - Not Logged in.";
return; return;
} }
m_session->enqueue_action(UserActionID::CONNECT_DUMMY, nullptr, nullptr, {}); m_session->enqueue_action(UserAccountActionID::CONNECT_DUMMY, nullptr, nullptr, {});
} }
wakeup_session_thread(); wakeup_session_thread();
} }
#endif 0 #endif 0
void PrusaAuthCommunication::enqueue_connect_printers_action() void UserAccountCommunication::enqueue_connect_printers_action()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -299,11 +299,11 @@ void PrusaAuthCommunication::enqueue_connect_printers_action()
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in."; BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return; return;
} }
m_session->enqueue_action(UserActionID::AUTH_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {}); m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {});
} }
wakeup_session_thread(); wakeup_session_thread();
} }
void PrusaAuthCommunication::enqueue_test_connection() void UserAccountCommunication::enqueue_test_connection()
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -311,12 +311,12 @@ void PrusaAuthCommunication::enqueue_test_connection()
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in."; BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return; return;
} }
m_session->enqueue_action(UserActionID::AUTH_ACTION_TEST_CONNECTION, nullptr, nullptr, {}); m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION, nullptr, nullptr, {});
} }
wakeup_session_thread(); wakeup_session_thread();
} }
void PrusaAuthCommunication::enqueue_avatar_action(const std::string url) void UserAccountCommunication::enqueue_avatar_action(const std::string url)
{ {
{ {
std::lock_guard<std::mutex> lock(m_session_mutex); std::lock_guard<std::mutex> lock(m_session_mutex);
@ -324,11 +324,11 @@ void PrusaAuthCommunication::enqueue_avatar_action(const std::string url)
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in."; BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return; return;
} }
m_session->enqueue_action(UserActionID::AUTH_ACTION_AVATAR, nullptr, nullptr, url); m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR, nullptr, nullptr, url);
} }
wakeup_session_thread(); wakeup_session_thread();
} }
void PrusaAuthCommunication::init_session_thread() void UserAccountCommunication::init_session_thread()
{ {
m_thread = std::thread([this]() { m_thread = std::thread([this]() {
for (;;) { for (;;) {
@ -349,7 +349,7 @@ void PrusaAuthCommunication::init_session_thread()
}); });
} }
void PrusaAuthCommunication::wakeup_session_thread() void UserAccountCommunication::wakeup_session_thread()
{ {
m_thread_stop_condition.notify_all(); m_thread_stop_condition.notify_all();
} }

View File

@ -26,10 +26,10 @@ private:
std::string sha256(const std::string& input); std::string sha256(const std::string& input);
}; };
class PrusaAuthCommunication { class UserAccountCommunication {
public: public:
PrusaAuthCommunication(wxEvtHandler* evt_handler, AppConfig* app_config); UserAccountCommunication(wxEvtHandler* evt_handler, AppConfig* app_config);
~PrusaAuthCommunication(); ~UserAccountCommunication();
// UI Session thread Interface // UI Session thread Interface
// //
@ -63,7 +63,7 @@ public:
void set_polling_enabled(bool enabled); void set_polling_enabled(bool enabled);
private: private:
std::unique_ptr<AuthSession> m_session; std::unique_ptr<UserAccountSession> m_session;
std::thread m_thread; std::thread m_thread;
std::mutex m_session_mutex; std::mutex m_session_mutex;
std::mutex m_thread_stop_mutex; std::mutex m_thread_stop_mutex;

View File

@ -19,15 +19,13 @@ namespace Slic3r {
namespace GUI { namespace GUI {
wxDEFINE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent); wxDEFINE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent);
wxDEFINE_EVENT(EVT_LOGGEDOUT_PRUSAAUTH, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDEFINE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_UA_AVATAR_SUCCESS, UserAccountSuccessEvent);
wxDEFINE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_UA_FAIL, UserAccountFailEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); wxDEFINE_EVENT(EVT_UA_RESET, UserAccountFailEvent);
wxDEFINE_EVENT(EVT_PA_AVATAR_FAIL, PrusaAuthFailEvent);
wxDEFINE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent);
void UserActionPost::perform(/*UNUSED*/ wxEvtHandler* evt_handler, /*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) void UserActionPost::perform(/*UNUSED*/ wxEvtHandler* evt_handler, /*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input)
@ -59,26 +57,26 @@ void UserActionGetWithEvent::perform(wxEvtHandler* evt_handler, const std::strin
fail_callback(body); fail_callback(body);
std::string message = GUI::format("%1% action failed (%2%): %3%", action_name, std::to_string(status), body); std::string message = GUI::format("%1% action failed (%2%): %3%", action_name, std::to_string(status), body);
if (fail_evt_type != wxEVT_NULL) if (fail_evt_type != wxEVT_NULL)
wxQueueEvent(evt_handler, new PrusaAuthFailEvent(fail_evt_type, std::move(message))); wxQueueEvent(evt_handler, new UserAccountFailEvent(fail_evt_type, std::move(message)));
}); });
http.on_complete([evt_handler, success_callback, succ_evt_type = m_succ_evt_type](std::string body, unsigned status) { http.on_complete([evt_handler, success_callback, succ_evt_type = m_succ_evt_type](std::string body, unsigned status) {
if (success_callback) if (success_callback)
success_callback(body); success_callback(body);
if (succ_evt_type != wxEVT_NULL) if (succ_evt_type != wxEVT_NULL)
wxQueueEvent(evt_handler, new PrusaAuthSuccessEvent(succ_evt_type, body)); wxQueueEvent(evt_handler, new UserAccountSuccessEvent(succ_evt_type, body));
}); });
http.perform_sync(); http.perform_sync();
} }
void AuthSession::process_action_queue() void UserAccountSession::process_action_queue()
{ {
if (!m_proccessing_enabled) if (!m_proccessing_enabled)
return; return;
if (m_priority_action_queue.empty() && m_action_queue.empty()) { if (m_priority_action_queue.empty() && m_action_queue.empty()) {
// update printers on every periodic wakeup call // update printers on every periodic wakeup call
if (m_polling_enabled) if (m_polling_enabled)
enqueue_action(UserActionID::AUTH_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {}); enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS, nullptr, nullptr, {});
else else
return; return;
} }
@ -98,14 +96,14 @@ void AuthSession::process_action_queue()
} }
} }
void AuthSession::enqueue_action(UserActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) void UserAccountSession::enqueue_action(UserAccountActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input)
{ {
m_proccessing_enabled = true; m_proccessing_enabled = true;
m_action_queue.push({ id, success_callback, fail_callback, input }); m_action_queue.push({ id, success_callback, fail_callback, input });
} }
void AuthSession::init_with_code(const std::string& code, const std::string& code_verifier) void UserAccountSession::init_with_code(const std::string& code, const std::string& code_verifier)
{ {
// Data we have // Data we have
const std::string REDIRECT_URI = "prusaslicer://login"; const std::string REDIRECT_URI = "prusaslicer://login";
@ -117,13 +115,13 @@ void AuthSession::init_with_code(const std::string& code, const std::string& cod
m_proccessing_enabled = true; m_proccessing_enabled = true;
// fail fn might be cancel_queue here // fail fn might be cancel_queue here
m_priority_action_queue.push({ UserActionID::AUTH_ACTION_CODE_FOR_TOKEN m_priority_action_queue.push({ UserAccountActionID::USER_ACCOUNT_ACTION_CODE_FOR_TOKEN
, std::bind(&AuthSession::token_success_callback, this, std::placeholders::_1) , std::bind(&UserAccountSession::token_success_callback, this, std::placeholders::_1)
, std::bind(&AuthSession::code_exchange_fail_callback, this, std::placeholders::_1) , std::bind(&UserAccountSession::code_exchange_fail_callback, this, std::placeholders::_1)
, post_fields }); , post_fields });
} }
void AuthSession::token_success_callback(const std::string& body) void UserAccountSession::token_success_callback(const std::string& body)
{ {
// Data we need // Data we need
std::string access_token, refresh_token, shared_session_key; std::string access_token, refresh_token, shared_session_key;
@ -145,7 +143,7 @@ void AuthSession::token_success_callback(const std::string& body)
} }
catch (const std::exception&) { catch (const std::exception&) {
std::string msg = "Could not parse server response after code exchange."; std::string msg = "Could not parse server response after code exchange.";
wxQueueEvent(p_evt_handler, new PrusaAuthFailEvent(EVT_PRUSAAUTH_RESET, std::move(msg))); wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(msg)));
return; return;
} }
@ -155,7 +153,7 @@ void AuthSession::token_success_callback(const std::string& body)
m_access_token = std::string(); m_access_token = std::string();
m_refresh_token = std::string(); m_refresh_token = std::string();
m_shared_session_key = std::string(); m_shared_session_key = std::string();
wxQueueEvent(p_evt_handler, new PrusaAuthFailEvent(EVT_PRUSAAUTH_RESET, std::move(msg))); wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(msg)));
return; return;
} }
@ -166,49 +164,49 @@ void AuthSession::token_success_callback(const std::string& body)
m_access_token = access_token; m_access_token = access_token;
m_refresh_token = refresh_token; m_refresh_token = refresh_token;
m_shared_session_key = shared_session_key; m_shared_session_key = shared_session_key;
enqueue_action(UserActionID::AUTH_ACTION_USER_ID, nullptr, nullptr, {}); enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID, nullptr, nullptr, {});
} }
void AuthSession::code_exchange_fail_callback(const std::string& body) void UserAccountSession::code_exchange_fail_callback(const std::string& body)
{ {
clear(); clear();
cancel_queue(); cancel_queue();
// Unlike refresh_fail_callback, no event was triggered so far, do it. (AUTH_ACTION_CODE_FOR_TOKEN does not send events) // Unlike refresh_fail_callback, no event was triggered so far, do it. (USER_ACCOUNT_ACTION_CODE_FOR_TOKEN does not send events)
wxQueueEvent(p_evt_handler, new PrusaAuthFailEvent(EVT_PRUSAAUTH_RESET, std::move(body))); wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(body)));
} }
void AuthSession::enqueue_test_with_refresh() void UserAccountSession::enqueue_test_with_refresh()
{ {
// on test fail - try refresh // on test fail - try refresh
m_proccessing_enabled = true; m_proccessing_enabled = true;
m_priority_action_queue.push({ UserActionID::AUTH_ACTION_TEST_ACCESS_TOKEN, nullptr, std::bind(&AuthSession::enqueue_refresh, this, std::placeholders::_1), {} }); m_priority_action_queue.push({ UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN, nullptr, std::bind(&UserAccountSession::enqueue_refresh, this, std::placeholders::_1), {} });
} }
void AuthSession::enqueue_refresh(const std::string& body) void UserAccountSession::enqueue_refresh(const std::string& body)
{ {
assert(!m_refresh_token.empty()); assert(!m_refresh_token.empty());
std::string post_fields = "grant_type=refresh_token" std::string post_fields = "grant_type=refresh_token"
"&client_id=" + client_id() + "&client_id=" + client_id() +
"&refresh_token=" + m_refresh_token; "&refresh_token=" + m_refresh_token;
m_priority_action_queue.push({ UserActionID::AUTH_ACTION_REFRESH_TOKEN m_priority_action_queue.push({ UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN
, std::bind(&AuthSession::token_success_callback, this, std::placeholders::_1) , std::bind(&UserAccountSession::token_success_callback, this, std::placeholders::_1)
, std::bind(&AuthSession::refresh_fail_callback, this, std::placeholders::_1) , std::bind(&UserAccountSession::refresh_fail_callback, this, std::placeholders::_1)
, post_fields }); , post_fields });
} }
void AuthSession::refresh_fail_callback(const std::string& body) void UserAccountSession::refresh_fail_callback(const std::string& body)
{ {
clear(); clear();
cancel_queue(); cancel_queue();
// No need to notify UI thread here // No need to notify UI thread here
// backtrace: load tokens -> TEST_TOKEN fail (access token bad) -> REFRESH_TOKEN fail (refresh token bad) // backtrace: load tokens -> TEST_TOKEN fail (access token bad) -> REFRESH_TOKEN fail (refresh token bad)
// AUTH_ACTION_TEST_ACCESS_TOKEN triggers EVT_PRUSAAUTH_FAIL, we need also RESET // USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN triggers EVT_UA_FAIL, we need also RESET
wxQueueEvent(p_evt_handler, new PrusaAuthFailEvent(EVT_PRUSAAUTH_RESET, std::move(body))); wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(body)));
} }
void AuthSession::cancel_queue() void UserAccountSession::cancel_queue()
{ {
while (!m_priority_action_queue.empty()) { while (!m_priority_action_queue.empty()) {
m_priority_action_queue.pop(); m_priority_action_queue.pop();

View File

@ -14,33 +14,34 @@ namespace Slic3r {
namespace GUI { namespace GUI {
using OpenPrusaAuthEvent = Event<wxString>; using OpenPrusaAuthEvent = Event<wxString>;
using PrusaAuthSuccessEvent = Event<std::string>; using UserAccountSuccessEvent = Event<std::string>;
using PrusaAuthFailEvent = Event<std::string>; using UserAccountFailEvent = Event<std::string>;
wxDECLARE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent); wxDECLARE_EVENT(EVT_OPEN_PRUSAAUTH, OpenPrusaAuthEvent);
wxDECLARE_EVENT(EVT_LOGGEDOUT_PRUSAAUTH, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_UA_LOGGEDOUT, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_UA_ID_USER_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDECLARE_EVENT(EVT_UA_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_UA_AVATAR_SUCCESS, UserAccountSuccessEvent);
wxDECLARE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_UA_FAIL, UserAccountFailEvent); // Soft fail - clears only after some number of fails
wxDECLARE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); // Soft fail - clears only after some number of fails wxDECLARE_EVENT(EVT_UA_RESET, UserAccountFailEvent); // Hard fail - clears all
wxDECLARE_EVENT(EVT_PA_AVATAR_FAIL, PrusaAuthFailEvent); // Soft fail - clears only after some number of fails
wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent); // Hard fail - clears all
typedef std::function<void(const std::string& body)> UserActionSuccessFn; typedef std::function<void(const std::string& body)> UserActionSuccessFn;
typedef std::function<void(const std::string& body)> UserActionFailFn; typedef std::function<void(const std::string& body)> UserActionFailFn;
// UserActions implements different operations via trigger() method. Stored in m_actions. // UserActions implements different operations via trigger() method. Stored in m_actions.
enum class UserActionID { enum class UserAccountActionID {
AUTH_ACTION_DUMMY, USER_ACCOUNT_ACTION_DUMMY,
AUTH_ACTION_REFRESH_TOKEN, USER_ACCOUNT_ACTION_REFRESH_TOKEN,
AUTH_ACTION_CODE_FOR_TOKEN, USER_ACCOUNT_ACTION_CODE_FOR_TOKEN,
AUTH_ACTION_USER_ID, USER_ACCOUNT_ACTION_USER_ID,
AUTH_ACTION_TEST_ACCESS_TOKEN, USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN,
AUTH_ACTION_TEST_CONNECTION, USER_ACCOUNT_ACTION_TEST_CONNECTION,
AUTH_ACTION_CONNECT_DUMMY, USER_ACCOUNT_ACTION_CONNECT_PRINTERS,
AUTH_ACTION_CONNECT_PRINTERS, USER_ACCOUNT_ACTION_AVATAR,
AUTH_ACTION_AVATAR, #if 0
USER_ACCOUNT_ACTION_CONNECT_DUMMY,
#endif // 0
}; };
class UserAction class UserAction
{ {
@ -86,16 +87,16 @@ public:
struct ActionQueueData struct ActionQueueData
{ {
UserActionID action_id; UserAccountActionID action_id;
UserActionSuccessFn success_callback; UserActionSuccessFn success_callback;
UserActionFailFn fail_callback; UserActionFailFn fail_callback;
std::string input; std::string input;
}; };
class AuthSession class UserAccountSession
{ {
public: public:
AuthSession(wxEvtHandler* evt_handler, const std::string& access_token, const std::string& refresh_token, const std::string& shared_session_key, bool polling_enabled) UserAccountSession(wxEvtHandler* evt_handler, const std::string& access_token, const std::string& refresh_token, const std::string& shared_session_key, bool polling_enabled)
: p_evt_handler(evt_handler) : p_evt_handler(evt_handler)
, m_access_token(access_token) , m_access_token(access_token)
, m_refresh_token(refresh_token) , m_refresh_token(refresh_token)
@ -105,28 +106,33 @@ public:
{ {
// do not forget to add delete to destructor // do not forget to add delete to destructor
m_actions[UserActionID::AUTH_ACTION_DUMMY] = std::make_unique<DummyUserAction>(); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY] = std::make_unique<DummyUserAction>();
m_actions[UserActionID::AUTH_ACTION_REFRESH_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/"); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/");
m_actions[UserActionID::AUTH_ACTION_CODE_FOR_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/"); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CODE_FOR_TOKEN] = std::make_unique<UserActionPost>("EXCHANGE_TOKENS", "https://test-account.prusa3d.com/o/token/");
m_actions[UserActionID::AUTH_ACTION_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_RESET); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", EVT_UA_ID_USER_SUCCESS, EVT_UA_RESET);
m_actions[UserActionID::AUTH_ACTION_TEST_ACCESS_TOKEN] = std::make_unique<UserActionGetWithEvent>("TEST_ACCESS_TOKEN", "https://test-account.prusa3d.com/api/v1/me/", EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN] = std::make_unique<UserActionGetWithEvent>("TEST_ACCESS_TOKEN", "https://test-account.prusa3d.com/api/v1/me/", EVT_UA_ID_USER_SUCCESS, EVT_UA_FAIL);
m_actions[UserActionID::AUTH_ACTION_TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("TEST_CONNECTION", "https://test-account.prusa3d.com/api/v1/me/", wxEVT_NULL, EVT_PRUSAAUTH_RESET); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("TEST_CONNECTION", "https://test-account.prusa3d.com/api/v1/me/", wxEVT_NULL, EVT_UA_RESET);
m_actions[UserActionID::AUTH_ACTION_CONNECT_DUMMY] = std::make_unique<UserActionGetWithEvent>("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy"/*"dev.connect.prusa:8000/slicer/dummy"*/, EVT_PRUSAAUTH_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers", EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, EVT_UA_FAIL);
m_actions[UserActionID::AUTH_ACTION_CONNECT_PRINTERS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers"/*"dev.connect.prusa:8000/slicer/printers"*/, EVT_PRUSACONNECT_PRINTERS_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR] = std::make_unique<UserActionGetWithEvent>("AVATAR", "https://test-media.printables.com/media/", EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL);
m_actions[UserActionID::AUTH_ACTION_AVATAR] = std::make_unique<UserActionGetWithEvent>("AVATAR", "https://test-media.printables.com/media/", EVT_PA_AVATAR_SUCCESS, EVT_PA_AVATAR_FAIL); #if 0
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DUMMY] = std::make_unique<UserActionGetWithEvent>("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy", EVT_UA_SUCCESS, EVT_UA_FAIL);
#endif // 0
} }
~AuthSession() ~UserAccountSession()
{ {
m_actions[UserActionID::AUTH_ACTION_DUMMY].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_DUMMY].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_REFRESH_TOKEN].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_REFRESH_TOKEN].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_CODE_FOR_TOKEN].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CODE_FOR_TOKEN].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_USER_ID].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_USER_ID].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_TEST_ACCESS_TOKEN].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_TEST_CONNECTION].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_CONNECT_DUMMY].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTERS].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_CONNECT_PRINTERS].reset(nullptr); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR].reset(nullptr);
m_actions[UserActionID::AUTH_ACTION_AVATAR].reset(nullptr); #if 0
//assert(m_actions.empty()); m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DUMMY].reset(nullptr);
#endif // 0
} }
void clear() { void clear() {
m_access_token.clear(); m_access_token.clear();
@ -137,7 +143,7 @@ public:
// Functions that automatically enable action queu processing // Functions that automatically enable action queu processing
void init_with_code(const std::string& code, const std::string& code_verifier); void init_with_code(const std::string& code, const std::string& code_verifier);
void enqueue_action(UserActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input); void enqueue_action(UserAccountActionID id, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input);
// Special enques, that sets callbacks. // Special enques, that sets callbacks.
void enqueue_test_with_refresh(); void enqueue_test_with_refresh();
@ -169,7 +175,7 @@ private:
std::queue<ActionQueueData> m_action_queue; std::queue<ActionQueueData> m_action_queue;
std::queue<ActionQueueData> m_priority_action_queue; std::queue<ActionQueueData> m_priority_action_queue;
std::map<UserActionID, std::unique_ptr<UserAction>> m_actions; std::map<UserAccountActionID, std::unique_ptr<UserAction>> m_actions;
wxEvtHandler* p_evt_handler; wxEvtHandler* p_evt_handler;
}; };

View File

@ -3685,8 +3685,8 @@ void GUI_App::handle_web_request(std::string cmd)
(is_installed ? GUI::format(_L("Installed and Select Printer:\n%1%"), preset->name) : (is_installed ? GUI::format(_L("Installed and Select Printer:\n%1%"), preset->name) :
GUI::format(_L("Select Printer:\n%1%"), preset->name) ): GUI::format(_L("Select Printer:\n%1%"), preset->name) ):
GUI::format(_L("Printer not found:\n%1%"), model_name); GUI::format(_L("Printer not found:\n%1%"), model_name);
this->plater()->get_notification_manager()->close_notification_of_type(NotificationType::PrusaAuthUserID); this->plater()->get_notification_manager()->close_notification_of_type(NotificationType::UserAccountID);
this->plater()->get_notification_manager()->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out); this->plater()->get_notification_manager()->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out);
} }
void GUI_App::show_monitor_tab(bool show, const std::string& address/* = {}*/) void GUI_App::show_monitor_tab(bool show, const std::string& address/* = {}*/)

View File

@ -904,10 +904,10 @@ void MainFrame::set_monitor_tab_url(const wxString& url)
m_monitor_webview->load_default_url(); m_monitor_webview->load_default_url();
} }
void Slic3r::GUI::MainFrame::refresh_auth_menu(bool avatar/* = false */) void Slic3r::GUI::MainFrame::refresh_account_menu(bool avatar/* = false */)
{ {
// Update User name in TopBar // Update User name in TopBar
dynamic_cast<TopBar*>(m_tabpanel)->GetTopBarItemsCtrl()->UpdateAuthMenu(avatar); dynamic_cast<TopBar*>(m_tabpanel)->GetTopBarItemsCtrl()->UpdateAccountMenu(avatar);
} }
void MainFrame::add_created_tab(Tab* panel, const std::string& bmp_name /*= ""*/) void MainFrame::add_created_tab(Tab* panel, const std::string& bmp_name /*= ""*/)

View File

@ -223,7 +223,7 @@ public:
void set_monitor_tab_url(const wxString& url); void set_monitor_tab_url(const wxString& url);
bool get_monitor_tab_added() const { return m_monitor_webview_added; } bool get_monitor_tab_added() const { return m_monitor_webview_added; }
void refresh_auth_menu(bool avatar = false); void refresh_account_menu(bool avatar = false);
PrintHostQueueDialog* printhost_queue_dlg() { return m_printhost_queue_dlg; } PrintHostQueueDialog* printhost_queue_dlg() { return m_printhost_queue_dlg; }

View File

@ -131,7 +131,7 @@ enum class NotificationType
// Config file was detected during startup, open wifi config dialog via hypertext // Config file was detected during startup, open wifi config dialog via hypertext
WifiConfigFileDetected, WifiConfigFileDetected,
// Info abouty successful login or logout // Info abouty successful login or logout
PrusaAuthUserID, UserAccountID,
// Debug notification for connect communication // Debug notification for connect communication
PrusaConnectPrinters, PrusaConnectPrinters,
}; };

View File

@ -883,59 +883,62 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
wxGetApp().open_login_browser_with_dialog(evt.data); wxGetApp().open_login_browser_with_dialog(evt.data);
}); });
this->q->Bind(EVT_LOGGEDOUT_PRUSAAUTH, [this](PrusaAuthSuccessEvent& evt) { this->q->Bind(EVT_UA_LOGGEDOUT, [this](UserAccountSuccessEvent& evt) {
user_account->clear(); user_account->clear();
std::string text = _u8L("Logged out from Prusa Account."); std::string text = _u8L("Logged out from Prusa Account.");
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->close_notification_of_type(NotificationType::UserAccountID);
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); this->notification_manager->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text);
this->main_frame->disable_connect_tab(); this->main_frame->disable_connect_tab();
this->main_frame->refresh_auth_menu(true); this->main_frame->refresh_account_menu(true);
}); });
this->q->Bind(EVT_PA_ID_USER_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { this->q->Bind(EVT_UA_ID_USER_SUCCESS, [this](UserAccountSuccessEvent& evt) {
std::string username; std::string username;
if (user_account->on_user_id_success(evt.data, username)) { if (user_account->on_user_id_success(evt.data, username)) {
// login notification // login notification
std::string text = format(_u8L("Logged to Prusa Account as %1%."), username); std::string text = format(_u8L("Logged to Prusa Account as %1%."), username);
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->close_notification_of_type(NotificationType::UserAccountID);
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); this->notification_manager->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text);
// show connect tab // show connect tab
this->main_frame->enable_connect_tab(); this->main_frame->enable_connect_tab();
// Update User name in TopBar // Update User name in TopBar
this->main_frame->refresh_auth_menu(); this->main_frame->refresh_account_menu();
} else { } else {
// data were corrupt and username was not retrieved // data were corrupt and username was not retrieved
// procced as if EVT_PRUSAAUTH_RESET was recieved // procced as if EVT_UA_RESET was recieved
BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Recieved data were corrupt."; BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Recieved data were corrupt.";
user_account->clear(); user_account->clear();
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->close_notification_of_type(NotificationType::UserAccountID);
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Failed to connect to Prusa Account.")); this->notification_manager->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Failed to connect to Prusa Account."));
// Update User name in TopBar // Update User name in TopBar
this->main_frame->refresh_auth_menu(); this->main_frame->refresh_account_menu();
// Update sidebar printer status // Update sidebar printer status
sidebar->update_printer_presets_combobox(); sidebar->update_printer_presets_combobox();
} }
}); });
this->q->Bind(EVT_PRUSAAUTH_RESET, [this](PrusaAuthFailEvent& evt) { this->q->Bind(EVT_UA_RESET, [this](UserAccountFailEvent& evt) {
BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Error message: " << evt.data; BOOST_LOG_TRIVIAL(error) << "Reseting Prusa Account communication. Error message: " << evt.data;
user_account->clear(); user_account->clear();
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); this->notification_manager->close_notification_of_type(NotificationType::UserAccountID);
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Failed to connect to Prusa Account.")); this->notification_manager->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::WarningNotificationLevel, _u8L("Failed to connect to Prusa Account."));
// Update User name in TopBar // Update User name in TopBar
this->main_frame->refresh_auth_menu(); this->main_frame->refresh_account_menu();
// Update sidebar printer status // Update sidebar printer status
sidebar->update_printer_presets_combobox(); sidebar->update_printer_presets_combobox();
}); });
this->q->Bind(EVT_PRUSAAUTH_FAIL, [this](PrusaAuthFailEvent& evt) { this->q->Bind(EVT_UA_FAIL, [this](UserAccountFailEvent& evt) {
BOOST_LOG_TRIVIAL(error) << "Failed communication with Prusa Account: " << evt.data; BOOST_LOG_TRIVIAL(error) << "Failed communication with Prusa Account: " << evt.data;
user_account->on_communication_fail(); user_account->on_communication_fail();
}); });
this->q->Bind(EVT_PRUSAAUTH_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { #if 0
this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); // for debug purposes only
this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, evt.data); this->q->Bind(EVT_UA_SUCCESS, [this](UserAccountSuccessEvent& evt) {
this->notification_manager->close_notification_of_type(NotificationType::UserAccountID);
this->notification_manager->push_notification(NotificationType::UserAccountID, NotificationManager::NotificationLevel::ImportantNotificationLevel, evt.data);
}); });
this->q->Bind(EVT_PRUSACONNECT_PRINTERS_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { #endif // 0
this->q->Bind(EVT_UA_PRUSACONNECT_PRINTERS_SUCCESS, [this](UserAccountSuccessEvent& evt) {
std::string text; std::string text;
bool printers_changed = false; bool printers_changed = false;
if (user_account->on_connect_printers_success(evt.data, wxGetApp().app_config, printers_changed)) { if (user_account->on_connect_printers_success(evt.data, wxGetApp().app_config, printers_changed)) {
@ -943,17 +946,17 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
sidebar->update_printer_presets_combobox(); sidebar->update_printer_presets_combobox();
} }
} else { } else {
// message was corrupt, procceed like EVT_PRUSAAUTH_FAIL // message was corrupt, procceed like EVT_UA_FAIL
user_account->on_communication_fail(); user_account->on_communication_fail();
} }
}); });
this->q->Bind(EVT_PA_AVATAR_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { this->q->Bind(EVT_UA_AVATAR_SUCCESS, [this](UserAccountSuccessEvent& evt) {
boost::filesystem::path png_path = boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png"; boost::filesystem::path png_path = boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png";
FILE* file; FILE* file;
file = fopen(png_path.string().c_str(), "wb"); file = fopen(png_path.string().c_str(), "wb");
fwrite(evt.data.c_str(), 1, evt.data.size(), file); fwrite(evt.data.c_str(), 1, evt.data.size(), file);
fclose(file); fclose(file);
this->main_frame->refresh_auth_menu(true); this->main_frame->refresh_account_menu(true);
}); });
wxGetApp().other_instance_message_handler()->init(this->q); wxGetApp().other_instance_message_handler()->init(this->q);

View File

@ -154,28 +154,30 @@ void TopBarItemsCtrl::ApplyWorkspacesMenu()
} }
} }
void TopBarItemsCtrl::CreateAuthMenu() void TopBarItemsCtrl::CreateAccountMenu()
{ {
m_user_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, "", "", m_user_menu_item = append_menu_item(&m_account_menu, wxID_ANY, "", "",
[this](wxCommandEvent& e) { [this](wxCommandEvent& e) {
m_auth_btn->set_selected(true); m_account_btn->set_selected(true);
wxGetApp().plater()->PopupMenu(&m_auth_menu, m_auth_btn->GetPosition()); wxGetApp().plater()->PopupMenu(&m_account_menu, m_account_btn->GetPosition());
}, get_bmp_bundle("user", 16)); }, get_bmp_bundle("user", 16));
m_auth_menu.AppendSeparator(); m_account_menu.AppendSeparator();
/*
m_connect_dummy_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, _L("PrusaConnect Printers"), "", #if 0
m_connect_dummy_menu_item = append_menu_item(&m_account_menu, wxID_ANY, _L("PrusaConnect Printers"), "",
[](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->enqueue_connect_printers_action(); }, [](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->enqueue_connect_printers_action(); },
"", nullptr, []() { return wxGetApp().plater()->get_user_account()->is_logged(); }, this->GetParent()); "", nullptr, []() { return wxGetApp().plater()->get_user_account()->is_logged(); }, this->GetParent());
*/ #endif // 0
wxMenuItem* remember_me_menu_item = append_menu_check_item(&m_auth_menu, wxID_ANY, _L("Remember me"), ""
wxMenuItem* remember_me_menu_item = append_menu_check_item(&m_account_menu, wxID_ANY, _L("Remember me"), ""
, [](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->toggle_remember_session(); } , [](wxCommandEvent&) { wxGetApp().plater()->get_user_account()->toggle_remember_session(); }
, &m_auth_menu , &m_account_menu
, []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->is_logged() : false; } , []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->is_logged() : false; }
, []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->get_remember_session() : false; } , []() { return wxGetApp().plater()->get_user_account() ? wxGetApp().plater()->get_user_account()->get_remember_session() : false; }
, this->GetParent()); , this->GetParent());
m_login_menu_item = append_menu_item(&m_auth_menu, wxID_ANY, "", "", m_login_menu_item = append_menu_item(&m_account_menu, wxID_ANY, "", "",
[](wxCommandEvent&) { [](wxCommandEvent&) {
auto user_account = wxGetApp().plater()->get_user_account(); auto user_account = wxGetApp().plater()->get_user_account();
if (user_account->is_logged()) if (user_account->is_logged())
@ -185,7 +187,7 @@ void TopBarItemsCtrl::CreateAuthMenu()
}, get_bmp_bundle("login", 16)); }, get_bmp_bundle("login", 16));
} }
void TopBarItemsCtrl::UpdateAuthMenu(bool avatar/* = false*/) void TopBarItemsCtrl::UpdateAccountMenu(bool avatar/* = false*/)
{ {
auto user_account = wxGetApp().plater()->get_user_account(); auto user_account = wxGetApp().plater()->get_user_account();
if (m_login_menu_item) { if (m_login_menu_item) {
@ -197,21 +199,21 @@ void TopBarItemsCtrl::UpdateAuthMenu(bool avatar/* = false*/)
if (m_user_menu_item) if (m_user_menu_item)
m_user_menu_item->SetItemLabel(user_name); m_user_menu_item->SetItemLabel(user_name);
m_auth_btn->SetLabel(user_name); m_account_btn->SetLabel(user_name);
if (avatar) { if (avatar) {
if (user_account->is_logged()) { if (user_account->is_logged()) {
boost::filesystem::path path = boost::filesystem::path(boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png"); boost::filesystem::path path = boost::filesystem::path(boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png");
ScalableBitmap new_logo(this, path, m_auth_btn->GetBitmapSize()); ScalableBitmap new_logo(this, path, m_account_btn->GetBitmapSize());
if (new_logo.IsOk()) if (new_logo.IsOk())
m_auth_btn->SetBitmap_(new_logo); m_account_btn->SetBitmap_(new_logo);
else else
m_auth_btn->SetBitmap_("user"); m_account_btn->SetBitmap_("user");
} }
else { else {
m_auth_btn->SetBitmap_("user"); m_account_btn->SetBitmap_("user");
} }
} }
m_auth_btn->Refresh(); m_account_btn->Refresh();
} }
void TopBarItemsCtrl::CreateSearch() void TopBarItemsCtrl::CreateSearch()
@ -286,20 +288,20 @@ TopBarItemsCtrl::TopBarItemsCtrl(wxWindow *parent) :
}); });
m_workspaces_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_workspace_btn->set_selected(false); }); m_workspaces_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_workspace_btn->set_selected(false); });
// create Auth menu // create Account menu
CreateAuthMenu(); CreateAccountMenu();
// m_auth_btn = new ButtonWithPopup(this, "user", 35); // m_account_btn = new ButtonWithPopup(this, "user", 35);
m_auth_btn = new ButtonWithPopup(this, _L("Anonymus"), "user"); m_account_btn = new ButtonWithPopup(this, _L("Anonymus"), "user");
right_sizer->Add(m_auth_btn, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxRIGHT | wxLEFT, m_btn_margin); right_sizer->Add(m_account_btn, 0, wxALIGN_CENTER_VERTICAL | wxALIGN_RIGHT | wxRIGHT | wxLEFT, m_btn_margin);
m_auth_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) { m_account_btn->Bind(wxEVT_BUTTON, [this](wxCommandEvent& event) {
UpdateAuthMenu(); UpdateAccountMenu();
m_auth_btn->set_selected(true); m_account_btn->set_selected(true);
wxPoint pos = m_auth_btn->GetPosition(); wxPoint pos = m_account_btn->GetPosition();
wxGetApp().plater()->PopupMenu(&m_auth_menu, pos); wxGetApp().plater()->PopupMenu(&m_account_menu, pos);
}); });
m_auth_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_auth_btn->set_selected(false); }); m_account_menu.Bind(wxEVT_MENU_CLOSE, [this](wxMenuEvent&) { m_account_btn->set_selected(false); });
m_sizer->Add(right_sizer, 0, wxALIGN_CENTER_VERTICAL); m_sizer->Add(right_sizer, 0, wxALIGN_CENTER_VERTICAL);
@ -366,7 +368,7 @@ void TopBarItemsCtrl::OnColorsChanged()
m_menu_btn->sys_color_changed(); m_menu_btn->sys_color_changed();
m_workspace_btn->sys_color_changed(); m_workspace_btn->sys_color_changed();
m_auth_btn->sys_color_changed(); m_account_btn->sys_color_changed();
m_search->SysColorsChanged(); m_search->SysColorsChanged();
UpdateSelection(); UpdateSelection();

View File

@ -50,12 +50,14 @@ class TopBarItemsCtrl : public wxControl
wxMenu m_main_menu; wxMenu m_main_menu;
wxMenu m_workspaces_menu; wxMenu m_workspaces_menu;
wxMenu m_auth_menu; wxMenu m_account_menu;
// Prusa Account (Auth) menu items // Prusa Account menu items
wxMenuItem* m_user_menu_item{ nullptr }; wxMenuItem* m_user_menu_item{ nullptr };
wxMenuItem* m_login_menu_item{ nullptr }; wxMenuItem* m_login_menu_item{ nullptr };
//wxMenuItem* m_connect_dummy_menu_item{ nullptr }; #if 0
wxMenuItem* m_connect_dummy_menu_item{ nullptr };
#endif // 0
::TextInput* m_search{ nullptr }; ::TextInput* m_search{ nullptr };
public: public:
@ -77,8 +79,8 @@ public:
void AppendMenuItem(wxMenu* menu, const wxString& title); void AppendMenuItem(wxMenu* menu, const wxString& title);
void AppendMenuSeparaorItem(); void AppendMenuSeparaorItem();
void ApplyWorkspacesMenu(); void ApplyWorkspacesMenu();
void CreateAuthMenu(); void CreateAccountMenu();
void UpdateAuthMenu(bool avatar = false); void UpdateAccountMenu(bool avatar = false);
void CreateSearch(); void CreateSearch();
wxWindow* GetSearchCtrl() { return m_search->GetTextCtrl(); } wxWindow* GetSearchCtrl() { return m_search->GetTextCtrl(); }
@ -88,7 +90,7 @@ private:
wxFlexGridSizer* m_sizer; wxFlexGridSizer* m_sizer;
ButtonWithPopup* m_menu_btn {nullptr}; ButtonWithPopup* m_menu_btn {nullptr};
ButtonWithPopup* m_workspace_btn {nullptr}; ButtonWithPopup* m_workspace_btn {nullptr};
ButtonWithPopup* m_auth_btn {nullptr}; ButtonWithPopup* m_account_btn {nullptr};
std::vector<Button*> m_pageButtons; std::vector<Button*> m_pageButtons;
int m_selection {-1}; int m_selection {-1};
int m_btn_margin; int m_btn_margin;

View File

@ -13,7 +13,7 @@ namespace Slic3r {
namespace GUI { namespace GUI {
UserAccount::UserAccount(wxEvtHandler* evt_handler, AppConfig* app_config) UserAccount::UserAccount(wxEvtHandler* evt_handler, AppConfig* app_config)
: m_auth_communication(std::make_unique<PrusaAuthCommunication>(evt_handler, app_config)) : m_communication(std::make_unique<UserAccountCommunication>(evt_handler, app_config))
{} {}
UserAccount::~UserAccount() UserAccount::~UserAccount()
@ -22,7 +22,7 @@ UserAccount::~UserAccount()
void UserAccount::set_username(const std::string& username) void UserAccount::set_username(const std::string& username)
{ {
m_username = username; m_username = username;
m_auth_communication->set_username(username); m_communication->set_username(username);
} }
void UserAccount::clear() void UserAccount::clear()
@ -30,63 +30,63 @@ void UserAccount::clear()
m_username = {}; m_username = {};
m_user_data.clear(); m_user_data.clear();
m_printer_map.clear(); m_printer_map.clear();
m_auth_communication->do_clear(); m_communication->do_clear();
} }
void UserAccount::set_remember_session(bool remember) void UserAccount::set_remember_session(bool remember)
{ {
m_auth_communication->set_remember_session(remember); m_communication->set_remember_session(remember);
} }
void UserAccount::toggle_remember_session() void UserAccount::toggle_remember_session()
{ {
m_auth_communication->set_remember_session(!m_auth_communication->get_remember_session()); m_communication->set_remember_session(!m_communication->get_remember_session());
} }
bool UserAccount::get_remember_session() bool UserAccount::get_remember_session()
{ {
return m_auth_communication->get_remember_session(); return m_communication->get_remember_session();
} }
bool UserAccount::is_logged() bool UserAccount::is_logged()
{ {
return m_auth_communication->is_logged(); return m_communication->is_logged();
} }
void UserAccount::do_login() void UserAccount::do_login()
{ {
m_auth_communication->do_login(); m_communication->do_login();
} }
void UserAccount::do_logout() void UserAccount::do_logout()
{ {
m_auth_communication->do_logout(); m_communication->do_logout();
} }
std::string UserAccount::get_access_token() std::string UserAccount::get_access_token()
{ {
return m_auth_communication->get_access_token(); return m_communication->get_access_token();
} }
#if 0 #if 0
void UserAccount::enqueue_user_id_action() void UserAccount::enqueue_user_id_action()
{ {
m_auth_communication->enqueue_user_id_action(); m_communication->enqueue_user_id_action();
} }
void UserAccount::enqueue_connect_dummy_action() void UserAccount::enqueue_connect_dummy_action()
{ {
m_auth_communication->enqueue_connect_dummy_action(); m_communication->enqueue_connect_dummy_action();
} }
#endif #endif
void UserAccount::enqueue_connect_printers_action() void UserAccount::enqueue_connect_printers_action()
{ {
m_auth_communication->enqueue_connect_printers_action(); m_communication->enqueue_connect_printers_action();
} }
void UserAccount::enqueue_avatar_action() void UserAccount::enqueue_avatar_action()
{ {
m_auth_communication->enqueue_avatar_action(m_user_data["avatar"]); m_communication->enqueue_avatar_action(m_user_data["avatar"]);
} }
bool UserAccount::on_login_code_recieved(const std::string& url_message) bool UserAccount::on_login_code_recieved(const std::string& url_message)
{ {
m_auth_communication->on_login_code_recieved(url_message); m_communication->on_login_code_recieved(url_message);
return true; return true;
} }
@ -134,7 +134,7 @@ void UserAccount::on_communication_fail()
m_fail_counter++; m_fail_counter++;
if (m_fail_counter > 5) // there is no deeper reason why 5 if (m_fail_counter > 5) // there is no deeper reason why 5
{ {
m_auth_communication->enqueue_test_connection(); m_communication->enqueue_test_connection();
m_fail_counter = 0; m_fail_counter = 0;
} }
} }

View File

@ -23,7 +23,7 @@ enum class ConnectPrinterState {
typedef std::map<std::string, std::vector<size_t>> ConnectPrinterStateMap; typedef std::map<std::string, std::vector<size_t>> ConnectPrinterStateMap;
// Class UserAccount should handle every request for entities outside PrusaSlicer like PrusaAuth or PrusaConnect. // Class UserAccount should handle every request for entities outside PrusaSlicer like PrusaAuth or PrusaConnect.
// Outside communication is implemented in class PrusaAuthCommunication in Auth.hpp. // Outside communication is implemented in class UserAccountCommunication that runs separate thread. Results come back in events to Plater.
// All incoming data shoud be stored in UserAccount. // All incoming data shoud be stored in UserAccount.
class UserAccount { class UserAccount {
public: public:
@ -44,13 +44,13 @@ public:
void enqueue_connect_printers_action(); void enqueue_connect_printers_action();
void enqueue_avatar_action(); void enqueue_avatar_action();
// Functions called from UI where events emmited from AuthSession are binded // Functions called from UI where events emmited from UserAccountSession are binded
// Returns bool if data were correctly proccessed // Returns bool if data were correctly proccessed
bool on_login_code_recieved(const std::string& url_message); bool on_login_code_recieved(const std::string& url_message);
bool on_user_id_success(const std::string data, std::string& out_username); bool on_user_id_success(const std::string data, std::string& out_username);
// Called on EVT_PRUSAAUTH_FAIL, triggers test after several calls // Called on EVT_UA_FAIL, triggers test after several calls
void on_communication_fail(); void on_communication_fail();
// Clears all data and connections, called on logout or EVT_PRUSAAUTH_RESET // Clears all data and connections, called on logout or EVT_UA_RESET
void clear(); void clear();
bool on_connect_printers_success(const std::string data, AppConfig* app_config, bool& out_printers_changed); bool on_connect_printers_success(const std::string data, AppConfig* app_config, bool& out_printers_changed);
@ -68,7 +68,7 @@ private:
void set_username(const std::string& username); void set_username(const std::string& username);
std::unique_ptr<Slic3r::GUI::PrusaAuthCommunication> m_auth_communication; std::unique_ptr<Slic3r::GUI::UserAccountCommunication> m_communication;
ConnectPrinterStateMap m_printer_map; ConnectPrinterStateMap m_printer_map;
std::map<std::string, std::string> m_user_data; std::map<std::string, std::string> m_user_data;