From 78c9aa668cbfdf63f3840283885eaea021efdcfd Mon Sep 17 00:00:00 2001 From: David Kocik Date: Fri, 8 Dec 2023 17:06:47 +0100 Subject: [PATCH] User Account: Store more user data, not only username Compare new and old printer maps --- src/slic3r/GUI/MainFrame.cpp | 2 +- src/slic3r/GUI/Plater.cpp | 35 ++++++++++------ src/slic3r/GUI/UserAccount.cpp | 77 ++++++++++++++++++++++++---------- src/slic3r/GUI/UserAccount.hpp | 18 ++++---- 4 files changed, 91 insertions(+), 41 deletions(-) diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index ba7155564e..ece8cfc1e4 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -836,7 +836,7 @@ void MainFrame::create_preset_tabs() add_created_tab(new TabPrinter(m_tabpanel), wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology() == ptFFF ? "printer" : "sla_printer"); m_webview = new WebViewPanel(m_tabpanel); - m_tabpanel->AddPage(m_webview, "Web View"); + m_tabpanel->AddPage(m_webview, "PrusaConnect"); /* m_media = new MediaMainPanel(this); m_tabpanel->AddPage(m_media, "Media"); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 94926e7643..f8a8160c91 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -891,11 +891,17 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) }); this->q->Bind(EVT_PA_ID_USER_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { - std::string username = user_account->on_user_id_success(evt.data, wxGetApp().app_config); - std::string text = format(_u8L("Logged as %1%."), username); - this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); - this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); - wxGetApp().update_config_menu(); + std::string username; + bool succ = user_account->on_user_id_success(evt.data, wxGetApp().app_config, username); + if (succ) { + std::string text = format(_u8L("Logged as %1%."), username); + this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); + this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, text); + wxGetApp().update_config_menu(); + } else { + // TODO + } + }); this->q->Bind(EVT_PRUSAAUTH_FAIL, [this](PrusaAuthFailEvent& evt) { BOOST_LOG_TRIVIAL(error) << "Network error message: " << evt.data; @@ -908,13 +914,18 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, evt.data); }); this->q->Bind(EVT_PRUSACONNECT_PRINTERS_SUCCESS, [this](PrusaAuthSuccessEvent& evt) { - BOOST_LOG_TRIVIAL(error) << "PrusaConnect printers message: " << evt.data; - std::string text = user_account->on_connect_printers_success(evt.data, wxGetApp().app_config); - std::string out = GUI::format( "Printers in your PrusaConnect team:\n%1%", text); - this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); - this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out); - - sidebar->update_printer_presets_combobox(); + std::string text; + bool printers_changed = false; + bool succ = user_account->on_connect_printers_success(evt.data, wxGetApp().app_config, printers_changed, text); + if (succ) { + std::string out = GUI::format("Printers in your PrusaConnect team %1%:\n%2%", (printers_changed ? "changed" : "didn't changed"), text); + this->notification_manager->close_notification_of_type(NotificationType::PrusaAuthUserID); + this->notification_manager->push_notification(NotificationType::PrusaAuthUserID, NotificationManager::NotificationLevel::ImportantNotificationLevel, out); + if (printers_changed) + sidebar->update_printer_presets_combobox(); + } else { + // TODO + } }); wxGetApp().other_instance_message_handler()->init(this->q); diff --git a/src/slic3r/GUI/UserAccount.cpp b/src/slic3r/GUI/UserAccount.cpp index 7261f4e4c7..3d780db2de 100644 --- a/src/slic3r/GUI/UserAccount.cpp +++ b/src/slic3r/GUI/UserAccount.cpp @@ -61,39 +61,50 @@ void UserAccount::enqueue_connect_printers_action() m_auth_communication->enqueue_connect_printers_action(); } -void 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); + return true; } -std::string UserAccount::on_user_id_success(const std::string data, AppConfig* app_config) +bool UserAccount::on_user_id_success(const std::string data, AppConfig* app_config, std::string& out_username) { - std::string public_username; + boost::property_tree::ptree ptree; try { std::stringstream ss(data); - boost::property_tree::ptree ptree; boost::property_tree::read_json(ss, ptree); - const auto public_username_optional = ptree.get_optional("public_username"); - if (public_username_optional) - public_username = *public_username_optional; } catch (const std::exception&) { BOOST_LOG_TRIVIAL(error) << "UserIDUserAction Could not parse server response."; + return false; } - assert(!public_username.empty()); + m_user_data.clear(); + for (const auto& section : ptree) { + const auto opt = ptree.get_optional(section.first); + if (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()); + std::string public_username = m_user_data["public_username"]; set_username(public_username, app_config); - return public_username; + out_username = public_username; + return true; } -void UserAccount::on_communication_fail(const std::string data, AppConfig* app_config) +bool UserAccount::on_communication_fail(const std::string data, AppConfig* app_config) { // TODO: should we just declare disconnect on every fail? //set_username({}, app_config); + return true; } -void UserAccount::on_logout( AppConfig* app_config) +bool UserAccount::on_logout( AppConfig* app_config) { set_username({}, app_config); + return true; } namespace { @@ -112,9 +123,9 @@ namespace { } } -std::string UserAccount::on_connect_printers_success(const std::string data, AppConfig* app_config) +bool UserAccount::on_connect_printers_success(const std::string data, AppConfig* app_config, bool& out_printers_changed, std::string& out_message) { - + BOOST_LOG_TRIVIAL(debug) << "PrusaConnect printers message: " << data; pt::ptree ptree; try { std::stringstream ss(data); @@ -122,11 +133,13 @@ std::string UserAccount::on_connect_printers_success(const std::string data, App } catch (const std::exception& e) { BOOST_LOG_TRIVIAL(error) << "Could not parse prusaconnect message. " << e.what(); - return {}; + return false; } // fill m_printer_map with data from ptree // tree string is in format {"printers": [{..}, {..}]} - m_printer_map.clear(); + + ConnectPrinterStateMap new_printer_map; + assert(ptree.front().first == "printers"); for (const auto& printer_tree : ptree.front().second) { std::string name; @@ -152,26 +165,48 @@ std::string UserAccount::on_connect_printers_success(const std::string data, App assert(true); // On this assert, printer_state_table and ConnectPrinterState needs to be updated continue; } - if (auto counter = m_printer_map.find(name); counter != m_printer_map.end()) { - m_printer_map[name][static_cast(state)]++; + if (auto counter = new_printer_map.find(name); counter != new_printer_map.end()) { + new_printer_map[name][static_cast(state)]++; } else { - m_printer_map[name].reserve(static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT)); + new_printer_map[name].reserve(static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT)); for (size_t i = 0; i < static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT); i++) { - m_printer_map[name].push_back(i == static_cast(state) ? 1 : 0); + new_printer_map[name].push_back(i == static_cast(state) ? 1 : 0); } } } + + // compare new and old printer map and update old map into new + out_printers_changed = true; + for (const auto& it : new_printer_map) { + if (m_printer_map.find(it.first) == m_printer_map.end()) { + // printer is not in old map, add it by copying data from new map + out_printers_changed = false; + m_printer_map[it.first].reserve(static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT)); + for (size_t i = 0; i < static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT); i++) { + m_printer_map[it.first].push_back(new_printer_map[it.first][i]); + } + } else { + // printer is in old map, check state by state + for (size_t i = 0; i < static_cast(ConnectPrinterState::CONNECT_PRINTER_STATE_COUNT); i++) { + if (m_printer_map[it.first][i] != new_printer_map[it.first][i]) { + out_printers_changed = false; + m_printer_map[it.first][i] = new_printer_map[it.first][i]; + } + } + } + } + std::string out; for (const auto& it : m_printer_map) { - out += GUI::format("%1%: O%2% I%3% P%4% F%5% \n" + out_message += GUI::format("%1%: O%2% I%3% P%4% F%5% \n" , it.first , std::to_string(it.second[static_cast(ConnectPrinterState::CONNECT_PRINTER_OFFLINE)]) , std::to_string(it.second[static_cast(ConnectPrinterState::CONNECT_PRINTER_IDLE)]) , std::to_string(it.second[static_cast(ConnectPrinterState::CONNECT_PRINTER_PRINTING)]) , std::to_string(it.second[static_cast(ConnectPrinterState::CONNECT_PRINTER_FINISHED)])); } - return out; + return true; } std::string UserAccount::get_model_from_json(const std::string& message) const diff --git a/src/slic3r/GUI/UserAccount.hpp b/src/slic3r/GUI/UserAccount.hpp index ae5b1f9e14..4cb1184caf 100644 --- a/src/slic3r/GUI/UserAccount.hpp +++ b/src/slic3r/GUI/UserAccount.hpp @@ -42,15 +42,18 @@ public: #endif void enqueue_connect_printers_action(); - void on_login_code_recieved(const std::string& url_message); - std::string on_user_id_success(const std::string data, AppConfig* app_config); - void on_communication_fail(const std::string data, AppConfig* app_config); - void on_logout(AppConfig* app_config); - std::string on_connect_printers_success(const std::string data, AppConfig* app_config); + // Functions called from UI where events emmited from AuthSession are binded + // Returns bool if data were correctly proccessed + bool on_login_code_recieved(const std::string& url_message); + bool on_user_id_success(const std::string data, AppConfig* app_config, std::string& out_username); + bool on_communication_fail(const std::string data, AppConfig* app_config); + bool on_logout(AppConfig* app_config); + bool on_connect_printers_success(const std::string data, AppConfig* app_config, bool& out_printers_changed, std::string& out_message); std::string get_username() const { return m_username; } std::string get_access_token(); const ConnectPrinterStateMap& get_printer_state_map() const { return m_printer_map; } + const std::map get_user_data() const { return m_user_data; } // standalone utility methods std::string get_model_from_json(const std::string& message) const; @@ -60,8 +63,9 @@ private: std::unique_ptr m_auth_communication; - std::string m_username; - ConnectPrinterStateMap m_printer_map; + ConnectPrinterStateMap m_printer_map; + std::map m_user_data; + std::string m_username; const std::map printer_type_and_name_table = { {"1.3.0", "MK3" },