diff --git a/src/slic3r/GUI/Auth.cpp b/src/slic3r/GUI/Auth.cpp index 0006789404..5b8503edf2 100644 --- a/src/slic3r/GUI/Auth.cpp +++ b/src/slic3r/GUI/Auth.cpp @@ -296,6 +296,18 @@ void PrusaAuthCommunication::enqueue_connect_printers_action() wakeup_session_thread(); } +void PrusaAuthCommunication::enqueue_avatar_action(const std::string url) +{ + { + std::lock_guard lock(m_session_mutex); + if (!m_session->is_initialized()) { + BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in."; + return; + } + m_session->enqueue_action(UserActionID::AVATAR, nullptr, nullptr, url); + } + wakeup_session_thread(); +} void PrusaAuthCommunication::init_session_thread() { m_thread = std::thread([this]() { diff --git a/src/slic3r/GUI/Auth.hpp b/src/slic3r/GUI/Auth.hpp index 96832861d9..0a96242a78 100644 --- a/src/slic3r/GUI/Auth.hpp +++ b/src/slic3r/GUI/Auth.hpp @@ -43,8 +43,8 @@ public: void enqueue_connect_dummy_action(); #endif void enqueue_connect_printers_action(); - - + void enqueue_avatar_action(const std::string url); + // Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread. // // Called when browser returns code via prusaslicer:// custom url. diff --git a/src/slic3r/GUI/AuthSession.cpp b/src/slic3r/GUI/AuthSession.cpp index 2955a95e60..ad97778449 100644 --- a/src/slic3r/GUI/AuthSession.cpp +++ b/src/slic3r/GUI/AuthSession.cpp @@ -24,10 +24,12 @@ wxDEFINE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); +wxDEFINE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent); wxDEFINE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); wxDEFINE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent); -void UserActionPost::perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) + +void UserActionPost::perform( /*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) { std::string url = m_url; //BOOST_LOG_TRIVIAL(info) << m_action_name <<" POST " << url << " body: " << input; @@ -73,6 +75,30 @@ void UserActionGetWithEvent::perform(const std::string& access_token, UserAction http.perform_sync(); } +void UserActionNoAuthGetWithEvent::perform(/*UNUSED*/ const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) +{ + std::string url = m_url + input; + //BOOST_LOG_TRIVIAL(info) << m_action_name << " GET " << url; + auto http = Http::get(url); + http.on_error([&](std::string body, std::string error, unsigned status) { + //BOOST_LOG_TRIVIAL(error) << m_action_name << " action failed. status: " << status << " Body: " << body; + if (fail_callback) + fail_callback(body); + std::string message = GUI::format("%1% action failed (%2%): %3%", m_action_name, std::to_string(status), body); + if (m_succ_evt_type != wxEVT_NULL) + wxQueueEvent(m_evt_handler, new PrusaAuthFailEvent(m_fail_evt_type, std::move(message))); + }); + http.on_complete([&, this](std::string body, unsigned status) { + //BOOST_LOG_TRIVIAL(info) << m_action_name << " action success. Status: " << status << " Body: " << body; + if (success_callback) + success_callback(body); + if (m_succ_evt_type != wxEVT_NULL) + wxQueueEvent(m_evt_handler, new PrusaAuthSuccessEvent(m_succ_evt_type, body)); + }); + + http.perform_sync(); +} + void AuthSession::process_action_queue() { if (!m_proccessing_enabled) diff --git a/src/slic3r/GUI/AuthSession.hpp b/src/slic3r/GUI/AuthSession.hpp index 5ed7e4a1ae..2995ed5230 100644 --- a/src/slic3r/GUI/AuthSession.hpp +++ b/src/slic3r/GUI/AuthSession.hpp @@ -22,6 +22,7 @@ wxDECLARE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent); wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_PRUSACONNECT_PRINTERS_SUCCESS, PrusaAuthSuccessEvent); +wxDECLARE_EVENT(EVT_PA_AVATAR_SUCCESS, PrusaAuthSuccessEvent); wxDECLARE_EVENT(EVT_PRUSAAUTH_FAIL, PrusaAuthFailEvent); wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent); @@ -37,6 +38,7 @@ enum class UserActionID { LOGIN_USER_ID, CONNECT_DUMMY, CONNECT_PRINTERS, + AVATAR, }; class UserAction { @@ -66,6 +68,23 @@ private: wxEvtHandler* m_evt_handler; }; +class UserActionNoAuthGetWithEvent : public UserAction +{ +public: + UserActionNoAuthGetWithEvent(const std::string name, const std::string url, wxEvtHandler* evt_handler, wxEventType succ_event_type, wxEventType fail_event_type) + : m_succ_evt_type(succ_event_type) + , m_fail_evt_type(fail_event_type) + , m_evt_handler(evt_handler) + , UserAction(name, url) + {} + ~UserActionNoAuthGetWithEvent() {} + void perform(const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) override; +private: + wxEventType m_succ_evt_type; + wxEventType m_fail_evt_type; + wxEvtHandler* m_evt_handler; +}; + class UserActionPost : public UserAction { public: @@ -107,6 +126,7 @@ public: m_actions[UserActionID::LOGIN_USER_ID] = std::make_unique("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserActionID::CONNECT_DUMMY] = std::make_unique("CONNECT_DUMMY", "https://dev.connect.prusa3d.com/slicer/dummy"/*"dev.connect.prusa:8000/slicer/dummy"*/, evt_handler, EVT_PRUSAAUTH_SUCCESS, EVT_PRUSAAUTH_FAIL); m_actions[UserActionID::CONNECT_PRINTERS] = std::make_unique("CONNECT_PRINTERS", "https://dev.connect.prusa3d.com/slicer/printers"/*"dev.connect.prusa:8000/slicer/printers"*/, evt_handler, EVT_PRUSACONNECT_PRINTERS_SUCCESS, EVT_PRUSAAUTH_FAIL); + m_actions[UserActionID::AVATAR] = std::make_unique("AVATAR", "https://test-media.printables.com/media/", evt_handler, EVT_PA_AVATAR_SUCCESS, EVT_PRUSAAUTH_FAIL); } ~AuthSession() { @@ -117,6 +137,7 @@ public: m_actions[UserActionID::LOGIN_USER_ID].reset(nullptr); m_actions[UserActionID::CONNECT_DUMMY].reset(nullptr); m_actions[UserActionID::CONNECT_PRINTERS].reset(nullptr); + m_actions[UserActionID::AVATAR].reset(nullptr); //assert(m_actions.empty()); } void clear() { diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 2e34055b44..13582375ec 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -940,7 +940,14 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) // TODO } }); - + this->q->Bind(EVT_PA_AVATAR_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { + boost::filesystem::path png_path = boost::filesystem::path(Slic3r::data_dir()) / "cache" / "avatar.png"; + FILE* file; + file = fopen(png_path.string().c_str(), "wb"); + fwrite(evt.data.c_str(), 1, evt.data.size(), file); + fclose(file); + }); + wxGetApp().other_instance_message_handler()->init(this->q); // collapse sidebar according to saved value diff --git a/src/slic3r/GUI/UserAccount.cpp b/src/slic3r/GUI/UserAccount.cpp index 36b15426ca..731b07310f 100644 --- a/src/slic3r/GUI/UserAccount.cpp +++ b/src/slic3r/GUI/UserAccount.cpp @@ -72,6 +72,11 @@ void UserAccount::enqueue_connect_printers_action() m_auth_communication->enqueue_connect_printers_action(); } +void UserAccount::enqueue_avatar_action(const std::string& url) +{ + m_auth_communication->enqueue_avatar_action(url); +} + bool UserAccount::on_login_code_recieved(const std::string& url_message) { m_auth_communication->on_login_code_recieved(url_message); @@ -93,20 +98,26 @@ bool UserAccount::on_user_id_success(const std::string data, AppConfig* app_conf for (const auto& section : ptree) { const auto opt = ptree.get_optional(section.first); if (opt) { - BOOST_LOG_TRIVIAL(debug) << static_cast(section.first) << *opt; + BOOST_LOG_TRIVIAL(debug) << static_cast(section.first) << " " << *opt; m_user_data[section.first] = *opt; } } assert(m_user_data.find("public_username") != m_user_data.end()); - if (m_user_data.find("public_username") == m_user_data.end()) - { - BOOST_LOG_TRIVIAL(error) << "User ID message from Connect did not contain public_username. Login failed. Message data: " << data; + if (m_user_data.find("public_username") == m_user_data.end()) { + BOOST_LOG_TRIVIAL(error) << "User ID message from PrusaAuth did not contain public_username. Login failed. Message data: " << data; return false; } std::string public_username = m_user_data["public_username"]; set_username(public_username, app_config); out_username = public_username; + // equeue GET with avatar url + if (m_user_data.find("avatar") != m_user_data.end()) { + enqueue_avatar_action(m_user_data["avatar"]); + } + else { + BOOST_LOG_TRIVIAL(warning) << "User ID message from PrusaAcuth did not contain avatar."; + } // update printers list enqueue_connect_printers_action(); return true; diff --git a/src/slic3r/GUI/UserAccount.hpp b/src/slic3r/GUI/UserAccount.hpp index 12a7d036a3..34e63a97ce 100644 --- a/src/slic3r/GUI/UserAccount.hpp +++ b/src/slic3r/GUI/UserAccount.hpp @@ -41,6 +41,7 @@ public: void enqueue_connect_dummy_action(); #endif void enqueue_connect_printers_action(); + void enqueue_avatar_action(const std::string& url); // Functions called from UI where events emmited from AuthSession are binded // Returns bool if data were correctly proccessed