mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-14 14:35:54 +08:00
Download Prusa account Avatar
Missing adding avatar to topbar
This commit is contained in:
parent
ce91ef8f49
commit
7011322f62
@ -296,6 +296,18 @@ void PrusaAuthCommunication::enqueue_connect_printers_action()
|
|||||||
wakeup_session_thread();
|
wakeup_session_thread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrusaAuthCommunication::enqueue_avatar_action(const std::string url)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> 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()
|
void PrusaAuthCommunication::init_session_thread()
|
||||||
{
|
{
|
||||||
m_thread = std::thread([this]() {
|
m_thread = std::thread([this]() {
|
||||||
|
@ -43,8 +43,8 @@ public:
|
|||||||
void enqueue_connect_dummy_action();
|
void enqueue_connect_dummy_action();
|
||||||
#endif
|
#endif
|
||||||
void enqueue_connect_printers_action();
|
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.
|
// Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread.
|
||||||
//
|
//
|
||||||
// Called when browser returns code via prusaslicer:// custom url.
|
// Called when browser returns code via prusaslicer:// custom url.
|
||||||
|
@ -24,10 +24,12 @@ wxDEFINE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent);
|
|||||||
wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
|
wxDEFINE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
|
||||||
wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
|
wxDEFINE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
|
||||||
wxDEFINE_EVENT(EVT_PRUSACONNECT_PRINTERS_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_FAIL, PrusaAuthFailEvent);
|
||||||
wxDEFINE_EVENT(EVT_PRUSAAUTH_RESET, 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;
|
std::string url = m_url;
|
||||||
//BOOST_LOG_TRIVIAL(info) << m_action_name <<" POST " << url << " body: " << input;
|
//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();
|
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()
|
void AuthSession::process_action_queue()
|
||||||
{
|
{
|
||||||
if (!m_proccessing_enabled)
|
if (!m_proccessing_enabled)
|
||||||
|
@ -22,6 +22,7 @@ wxDECLARE_EVENT(EVT_PA_ID_USER_SUCCESS, PrusaAuthSuccessEvent);
|
|||||||
wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
|
wxDECLARE_EVENT(EVT_PA_ID_USER_FAIL, PrusaAuthFailEvent);
|
||||||
wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
|
wxDECLARE_EVENT(EVT_PRUSAAUTH_SUCCESS, PrusaAuthSuccessEvent);
|
||||||
wxDECLARE_EVENT(EVT_PRUSACONNECT_PRINTERS_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_FAIL, PrusaAuthFailEvent);
|
||||||
wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent);
|
wxDECLARE_EVENT(EVT_PRUSAAUTH_RESET, PrusaAuthFailEvent);
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ enum class UserActionID {
|
|||||||
LOGIN_USER_ID,
|
LOGIN_USER_ID,
|
||||||
CONNECT_DUMMY,
|
CONNECT_DUMMY,
|
||||||
CONNECT_PRINTERS,
|
CONNECT_PRINTERS,
|
||||||
|
AVATAR,
|
||||||
};
|
};
|
||||||
class UserAction
|
class UserAction
|
||||||
{
|
{
|
||||||
@ -66,6 +68,23 @@ private:
|
|||||||
wxEvtHandler* m_evt_handler;
|
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
|
class UserActionPost : public UserAction
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -107,6 +126,7 @@ public:
|
|||||||
m_actions[UserActionID::LOGIN_USER_ID] = std::make_unique<UserActionGetWithEvent>("USER_ID", "https://test-account.prusa3d.com/api/v1/me/", evt_handler, EVT_PA_ID_USER_SUCCESS, EVT_PRUSAAUTH_FAIL);
|
m_actions[UserActionID::LOGIN_USER_ID] = std::make_unique<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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_DUMMY] = std::make_unique<UserActionGetWithEvent>("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<UserActionGetWithEvent>("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::CONNECT_PRINTERS] = std::make_unique<UserActionGetWithEvent>("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<UserActionNoAuthGetWithEvent>("AVATAR", "https://test-media.printables.com/media/", evt_handler, EVT_PA_AVATAR_SUCCESS, EVT_PRUSAAUTH_FAIL);
|
||||||
}
|
}
|
||||||
~AuthSession()
|
~AuthSession()
|
||||||
{
|
{
|
||||||
@ -117,6 +137,7 @@ public:
|
|||||||
m_actions[UserActionID::LOGIN_USER_ID].reset(nullptr);
|
m_actions[UserActionID::LOGIN_USER_ID].reset(nullptr);
|
||||||
m_actions[UserActionID::CONNECT_DUMMY].reset(nullptr);
|
m_actions[UserActionID::CONNECT_DUMMY].reset(nullptr);
|
||||||
m_actions[UserActionID::CONNECT_PRINTERS].reset(nullptr);
|
m_actions[UserActionID::CONNECT_PRINTERS].reset(nullptr);
|
||||||
|
m_actions[UserActionID::AVATAR].reset(nullptr);
|
||||||
//assert(m_actions.empty());
|
//assert(m_actions.empty());
|
||||||
}
|
}
|
||||||
void clear() {
|
void clear() {
|
||||||
|
@ -940,7 +940,14 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
|
|||||||
// TODO
|
// 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);
|
wxGetApp().other_instance_message_handler()->init(this->q);
|
||||||
|
|
||||||
// collapse sidebar according to saved value
|
// collapse sidebar according to saved value
|
||||||
|
@ -72,6 +72,11 @@ void UserAccount::enqueue_connect_printers_action()
|
|||||||
m_auth_communication->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)
|
bool UserAccount::on_login_code_recieved(const std::string& url_message)
|
||||||
{
|
{
|
||||||
m_auth_communication->on_login_code_recieved(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) {
|
for (const auto& section : ptree) {
|
||||||
const auto opt = ptree.get_optional<std::string>(section.first);
|
const auto opt = ptree.get_optional<std::string>(section.first);
|
||||||
if (opt) {
|
if (opt) {
|
||||||
BOOST_LOG_TRIVIAL(debug) << static_cast<std::string>(section.first) << *opt;
|
BOOST_LOG_TRIVIAL(debug) << static_cast<std::string>(section.first) << " " << *opt;
|
||||||
m_user_data[section.first] = *opt;
|
m_user_data[section.first] = *opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
assert(m_user_data.find("public_username") != m_user_data.end());
|
assert(m_user_data.find("public_username") != m_user_data.end());
|
||||||
if (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 PrusaAuth did not contain public_username. Login failed. Message data: " << data;
|
||||||
BOOST_LOG_TRIVIAL(error) << "User ID message from Connect did not contain public_username. Login failed. Message data: " << data;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string public_username = m_user_data["public_username"];
|
std::string public_username = m_user_data["public_username"];
|
||||||
set_username(public_username, app_config);
|
set_username(public_username, app_config);
|
||||||
out_username = public_username;
|
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
|
// update printers list
|
||||||
enqueue_connect_printers_action();
|
enqueue_connect_printers_action();
|
||||||
return true;
|
return true;
|
||||||
|
@ -41,6 +41,7 @@ public:
|
|||||||
void enqueue_connect_dummy_action();
|
void enqueue_connect_dummy_action();
|
||||||
#endif
|
#endif
|
||||||
void enqueue_connect_printers_action();
|
void enqueue_connect_printers_action();
|
||||||
|
void enqueue_avatar_action(const std::string& url);
|
||||||
|
|
||||||
// Functions called from UI where events emmited from AuthSession are binded
|
// Functions called from UI where events emmited from AuthSession are binded
|
||||||
// Returns bool if data were correctly proccessed
|
// Returns bool if data were correctly proccessed
|
||||||
|
Loading…
x
Reference in New Issue
Block a user