Merge branch 'dk_avatar_new'

This commit is contained in:
Lukas Matena 2025-02-17 12:39:18 +01:00
commit 92b9dcec58
8 changed files with 65 additions and 31 deletions

View File

@ -1034,17 +1034,17 @@ void Plater::priv::init()
user_account->on_communication_fail();
}
});
this->q->Bind(EVT_UA_AVATAR_SUCCESS, [this](UserAccountSuccessEvent& evt) {
boost::filesystem::path path = user_account->get_avatar_path(true);
FILE* file;
file = boost::nowide::fopen(path.generic_string().c_str(), "wb");
if (file == NULL) {
BOOST_LOG_TRIVIAL(error) << "Failed to create file to store avatar picture at: " << path;
return;
}
fwrite(evt.data.c_str(), 1, evt.data.size(), file);
fclose(file);
this->main_frame->refresh_account_menu(true);
this->q->Bind(EVT_UA_AVATAR_SUCCESS, [this](UserAccountSuccessEvent& evt) {
boost::filesystem::path path = user_account->get_avatar_path(true);
FILE* file;
file = boost::nowide::fopen(path.generic_string().c_str(), "wb");
if (file == NULL) {
BOOST_LOG_TRIVIAL(error) << "Failed to create file to store avatar picture at: " << path;
return;
}
fwrite(evt.data.c_str(), 1, evt.data.size(), file);
fclose(file);
this->main_frame->refresh_account_menu(true);
});
this->q->Bind(EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, [this](UserAccountSuccessEvent& evt) {
this->user_account->set_current_printer_data(evt.data);

View File

@ -98,9 +98,13 @@ void UserAccount::enqueue_connect_status_action()
{
m_communication->enqueue_connect_status_action();
}
void UserAccount::enqueue_avatar_action()
void UserAccount::enqueue_avatar_old_action()
{
m_communication->enqueue_avatar_action(m_account_user_data["avatar"]);
m_communication->enqueue_avatar_old_action(m_account_user_data["avatar"]);
}
void UserAccount::enqueue_avatar_new_action(const std::string& url)
{
m_communication->enqueue_avatar_new_action(url);
}
void UserAccount::enqueue_printer_data_action(const std::string& uuid)
{
@ -145,10 +149,19 @@ bool UserAccount::on_user_id_success(const std::string data, std::string& out_us
set_username(public_username);
out_username = public_username;
// enqueue GET with avatar url
if (m_account_user_data.find("avatar") != m_account_user_data.end()) {
if (m_account_user_data.find("avatar_small") != m_account_user_data.end()) {
const boost::filesystem::path server_file(m_account_user_data["avatar_small"]);
m_avatar_extension = server_file.extension().string();
enqueue_avatar_new_action(m_account_user_data["avatar_small"]);
} else if (m_account_user_data.find("avatar_large") != m_account_user_data.end()) {
const boost::filesystem::path server_file(m_account_user_data["avatar_large"]);
m_avatar_extension = server_file.extension().string();
enqueue_avatar_new_action(m_account_user_data["avatar_large"]);
} else if (m_account_user_data.find("avatar") != m_account_user_data.end()) {
const boost::filesystem::path server_file(m_account_user_data["avatar"]);
m_avatar_extension = server_file.extension().string();
enqueue_avatar_action();
enqueue_avatar_old_action();
}
else {
BOOST_LOG_TRIVIAL(error) << "User ID message from PrusaAuth did not contain avatar.";

View File

@ -46,7 +46,8 @@ public:
bool get_remember_session();
void enqueue_connect_status_action();
void enqueue_connect_printer_models_action();
void enqueue_avatar_action();
void enqueue_avatar_old_action();
void enqueue_avatar_new_action(const std::string& url);
void enqueue_printer_data_action(const std::string& uuid);
void request_refresh();
// Clears all data and connections, called on logout or EVT_UA_RESET

View File

@ -421,13 +421,23 @@ void UserAccountCommunication::enqueue_test_connection()
wakeup_session_thread();
}
void UserAccountCommunication::enqueue_avatar_action(const std::string& url)
void UserAccountCommunication::enqueue_avatar_old_action(const std::string& url)
{
if (!m_session->is_initialized()) {
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return;
}
m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR, nullptr, nullptr, url);
m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_OLD, nullptr, nullptr, url);
wakeup_session_thread();
}
void UserAccountCommunication::enqueue_avatar_new_action(const std::string& url)
{
if (!m_session->is_initialized()) {
BOOST_LOG_TRIVIAL(error) << "Connect Printers endpoint connection failed - Not Logged in.";
return;
}
m_session->enqueue_action(UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_NEW, nullptr, nullptr, url);
wakeup_session_thread();
}

View File

@ -49,7 +49,8 @@ public:
// Trigger function starts various remote operations
void enqueue_connect_status_action();
void enqueue_connect_printer_models_action();
void enqueue_avatar_action(const std::string& url);
void enqueue_avatar_old_action(const std::string& url);
void enqueue_avatar_new_action(const std::string& url);
void enqueue_test_connection();
void enqueue_printer_data_action(const std::string& uuid);
void enqueue_refresh();

View File

@ -131,7 +131,8 @@ void UserAccountSession::process_action_queue_inner()
}
}
if (call_priority || call_standard) {
m_actions[selected_data.action_id]->perform(p_evt_handler, get_access_token(), selected_data.success_callback, selected_data.fail_callback, selected_data.input);
bool use_token = m_actions[selected_data.action_id]->get_requires_auth_token();
m_actions[selected_data.action_id]->perform(p_evt_handler, use_token ? get_access_token() : std::string(), selected_data.success_callback, selected_data.fail_callback, selected_data.input);
process_action_queue_inner();
}
}

View File

@ -45,28 +45,30 @@ enum class UserAccountActionID {
USER_ACCOUNT_ACTION_TEST_CONNECTION,
USER_ACCOUNT_ACTION_CONNECT_STATUS, // status of all printers by UUID
USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS, // status of all printers by UUID with printer_model. Should be called once to save printer models.
USER_ACCOUNT_ACTION_AVATAR,
USER_ACCOUNT_ACTION_AVATAR_OLD,
USER_ACCOUNT_ACTION_AVATAR_NEW,
USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID,
};
class UserAction
{
public:
UserAction(const std::string name, const std::string url) : m_action_name(name), m_url(url){}
UserAction(const std::string name, const std::string url, bool requires_auth_token) : m_action_name(name), m_url(url), m_requires_auth_token(requires_auth_token){}
virtual ~UserAction() = default;
virtual void perform(wxEvtHandler* evt_handler, const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) const = 0;
bool get_requires_auth_token() { return m_requires_auth_token; }
protected:
std::string m_action_name;
std::string m_url;
bool m_requires_auth_token;
};
class UserActionGetWithEvent : public UserAction
{
public:
UserActionGetWithEvent(const std::string name, const std::string url, wxEventType succ_event_type, wxEventType fail_event_type)
UserActionGetWithEvent(const std::string name, const std::string url, wxEventType succ_event_type, wxEventType fail_event_type, bool requires_auth_token = true)
: m_succ_evt_type(succ_event_type)
, m_fail_evt_type(fail_event_type)
, UserAction(name, url)
, UserAction(name, url, requires_auth_token)
{}
~UserActionGetWithEvent() {}
void perform(wxEvtHandler* evt_handler, const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) const override;
@ -78,7 +80,7 @@ private:
class UserActionPost : public UserAction
{
public:
UserActionPost(const std::string name, const std::string url) : UserAction(name, url) {}
UserActionPost(const std::string name, const std::string url, bool requires_auth_token = true) : UserAction(name, url, requires_auth_token) {}
~UserActionPost() {}
void perform(wxEvtHandler* evt_handler, const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) const override;
};
@ -86,7 +88,7 @@ public:
class DummyUserAction : public UserAction
{
public:
DummyUserAction() : UserAction("Dummy", {}) {}
DummyUserAction() : UserAction("Dummy", {}, false) {}
~DummyUserAction() {}
void perform(wxEvtHandler* evt_handler, const std::string& access_token, UserActionSuccessFn success_callback, UserActionFailFn fail_callback, const std::string& input) const override { }
};
@ -121,7 +123,8 @@ public:
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION] = std::make_unique<UserActionGetWithEvent>("TEST_CONNECTION", sc.account_me_url(), wxEVT_NULL, EVT_UA_RESET);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique<UserActionGetWithEvent>("CONNECT_STATUS", sc.connect_status_url(), EVT_UA_PRUSACONNECT_STATUS_SUCCESS, EVT_UA_FAIL);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTER_MODELS", sc.connect_printer_list_url(), EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, EVT_UA_FAIL);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR] = std::make_unique<UserActionGetWithEvent>("AVATAR", sc.media_url(), EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_OLD] = std::make_unique<UserActionGetWithEvent>("AVATAR", sc.media_url(), EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL, false);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_NEW] = std::make_unique<UserActionGetWithEvent>("AVATAR", std::string(), EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL, false);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID] = std::make_unique<UserActionGetWithEvent>("USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID", sc.connect_printers_url(), EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL);
}
~UserAccountSession()
@ -133,7 +136,8 @@ public:
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_ACCESS_TOKEN].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_TEST_CONNECTION].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_OLD].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR_NEW].reset(nullptr);
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID].reset(nullptr);
}
void clear() {

View File

@ -12,6 +12,7 @@
#include <wx/accel.h>
#include <boost/algorithm/string/replace.hpp>
#include <boost/log/trivial.hpp>
#include "BitmapCache.hpp"
#include "GUI.hpp"
@ -487,8 +488,11 @@ ScalableBitmap::ScalableBitmap(wxWindow* parent, boost::filesystem::path& icon_p
const std::string ext = icon_path.extension().string();
if (ext == ".png" || ext == ".jpg") {
bitmap.LoadFile(path, ext == ".png" ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_JPEG);
if (!bitmap.LoadFile(path, ext == ".png" ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_JPEG)) {
BOOST_LOG_TRIVIAL(error) << "Failed to load bitmap " << path;
return;
}
// check if the bitmap has a square shape
if (wxSize sz = bitmap.GetSize(); sz.x != sz.y) {