Files
OrcaSlicer/src/slic3r/GUI/PrinterCloudAuthDialog.cpp
T
HanifKoh 73d32d4791 Stop Leaking json Through Headers and Drop Includes Kept Only for the Name (#16221)
* Stop Leaking json Through Headers and Drop Includes Kept Only for the Name

AppConfig.hpp, DeviceManager.hpp and UserManager.hpp carried a global
"using namespace nlohmann;", json_diff.hpp a global "using json =
nlohmann::json;" and PrinterFileSystem.h a global "using nlohmann::json;".
Every file that included one of them, directly or not, could write a
bare json, and 63 did without declaring it.

The last two also made the include checker treat json_diff.hpp and
PrinterFileSystem.h as the headers that provide json, so they were
included from files that use nothing else from them: 57 of the 59
includers of json_diff.hpp never name json_diff.

The five statements are removed. Headers that use the type now spell
nlohmann::json, source files declare their own "using json =
nlohmann::json;", and the includes that only supplied the name are
dropped or replaced by <nlohmann/json.hpp>.

Eight files reached json_diff.hpp only through an include that is now
gone and with it lost that header's "using namespace std;". The std
names they used unqualified are qualified.

* Declare json in OrcaSlicer.cpp on Every Platform

OrcaSlicer.cpp had its "using namespace nlohmann;" and the json include
inside the Linux-only include block, so on Windows and macOS it took
json from AppConfig.hpp's global directive, which is gone. The include
and a "using json = nlohmann::json;" now sit outside the block.
2026-10-07 14:22:16 +08:00

119 lines
3.5 KiB
C++

#include "PrinterCloudAuthDialog.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/Gizmos/GLGizmoBase.hpp"
#include <boost/log/trivial.hpp>
#include <wx/log.h>
#include <wx/gdicmn.h>
#include <wx/settings.h>
#include <cstddef>
#include "slic3r/GUI/GUI.hpp"
#include <exception>
#include <wx/msgdlg.h>
#include <wx/sizer.h>
#include <wx/toolbar.h>
#include <wx/textdlg.h>
#include <wx/webview.h>
#include <wx/wx.h>
#include <wx/fileconf.h>
#include <wx/file.h>
#include <wx/wfstream.h>
#include <boost/cast.hpp>
#include <boost/lexical_cast.hpp>
#include <nlohmann/json.hpp>
#include "MainFrame.hpp"
#include "PrintHost.hpp"
#include <boost/dll.hpp>
#include <sstream>
#include <slic3r/GUI/Widgets/WebView.hpp>
using json = nlohmann::json;
//------------------------------------------
// PrinterCloundAuthDialog
//------------------------------------------
namespace Slic3r { namespace GUI {
PrinterCloudAuthDialog::PrinterCloudAuthDialog(wxWindow* parent, PrintHost* host)
: wxDialog((wxWindow*) (wxGetApp().mainframe), wxID_ANY, _L("Login"))
{
SetBackgroundColour(*wxWHITE);
// Url
host->get_login_url(m_TargetUrl);
BOOST_LOG_TRIVIAL(info) << "login url = " << m_TargetUrl.ToStdString();
// Create the webview
m_browser = WebView::CreateWebView(this, m_TargetUrl);
if (m_browser == nullptr) {
wxLogError("Could not init m_browser");
return;
}
m_browser->Hide();
m_browser->SetSize(0, 0);
// Connect the webview events
Bind(wxEVT_WEBVIEW_NAVIGATING, &PrinterCloudAuthDialog::OnNavigationRequest, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_NAVIGATED, &PrinterCloudAuthDialog::OnNavigationComplete, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_LOADED, &PrinterCloudAuthDialog::OnDocumentLoaded, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_NEWWINDOW, &PrinterCloudAuthDialog::OnNewWindow, this, m_browser->GetId());
Bind(wxEVT_WEBVIEW_SCRIPT_MESSAGE_RECEIVED, &PrinterCloudAuthDialog::OnScriptMessage, this, m_browser->GetId());
// UI
SetTitle(_L("Login"));
// Set a more sensible size for web browsing
wxSize pSize = FromDIP(wxSize(650, 840));
SetSize(pSize);
int screenheight = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y, NULL);
int screenwidth = wxSystemSettings::GetMetric(wxSYS_SCREEN_X, NULL);
int MaxY = (screenheight - pSize.y) > 0 ? (screenheight - pSize.y) / 2 : 0;
wxPoint tmpPT((screenwidth - pSize.x) / 2, MaxY);
Move(tmpPT);
}
PrinterCloudAuthDialog::~PrinterCloudAuthDialog() {}
void PrinterCloudAuthDialog::OnNavigationRequest(wxWebViewEvent& evt)
{
//todo
}
void PrinterCloudAuthDialog::OnNavigationComplete(wxWebViewEvent& evt)
{
m_browser->Show();
Layout();
//fortest
//WebView::RunScript(m_browser, "window.wx.postMessage('This is a web message')");
}
void PrinterCloudAuthDialog::OnDocumentLoaded(wxWebViewEvent& evt)
{
// todo
}
void PrinterCloudAuthDialog::OnNewWindow(wxWebViewEvent& evt) {
}
void PrinterCloudAuthDialog::OnScriptMessage(wxWebViewEvent& evt)
{
wxString str_input = evt.GetString();
try {
json j = json::parse(into_u8(str_input));
wxString strCmd = j["command"];
if (strCmd == "login_token") {
auto token = j["data"]["token"];
m_apikey = token;
}
Close();
} catch (std::exception& e) {
wxMessageBox(e.what(), _L("parse json failed"), wxICON_WARNING);
Close();
}
}
}
} // namespace Slic3r::GUI