OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler Class Referencefinal
Inheritance diagram for Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler:
Collaboration diagram for Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler:

Public Member Functions

 ElegooPrinterWebViewHandler (PrinterWebView &owner)
 ~ElegooPrinterWebViewHandler () override
void on_script_message (wxWebViewEvent &evt) override
virtual void on_loaded (wxWebViewEvent &evt)

Protected Member Functions

PrinterWebView & owner () const
wxWebView * browser () const

Private Member Functions

void send_ipc_message (const char *type, const std::string &request_id, const std::string &method, int code, const std::string &message, const std::string &data_json="{}")
void handle_upload_request (const std::string &request_id, const std::string &method, const std::string &params_json)
void handle_open_file_dialog_request (const std::string &request_id, const std::string &method, const std::string &params_json)
void handle_get_sn_request (const std::string &request_id, const std::string &method)

Private Attributes

std::atomic< bool > upload_in_progress { false }
std::atomic< bool > stop_upload { false }
std::thread upload_thread
PrinterWebView & m_owner

Detailed Description

Constructor & Destructor Documentation

◆ ElegooPrinterWebViewHandler()

Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::ElegooPrinterWebViewHandler ( PrinterWebView & owner)
inlineexplicit
103 {
104 }
PrinterWebViewHandler(PrinterWebView &owner)
Definition PrinterWebViewHandler.cpp:32
PrinterWebView & owner() const
Definition PrinterWebViewHandler.cpp:47

References Slic3r::GUI::PrinterWebViewHandler::PrinterWebViewHandler(), and Slic3r::GUI::PrinterWebViewHandler::owner().

Here is the call graph for this function:

◆ ~ElegooPrinterWebViewHandler()

Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::~ElegooPrinterWebViewHandler ( )
inlineoverride
107 {
108 stop_upload = true;
109 if (upload_thread.joinable())
110 upload_thread.join();
111 }
std::atomic< bool > stop_upload
Definition PrinterWebViewHandler.cpp:311

References stop_upload, and upload_thread.

Member Function Documentation

◆ browser()

wxWebView * Slic3r::GUI::PrinterWebViewHandler::browser ( ) const
protectedinherited
53{
54 return m_owner.m_browser;
55}
PrinterWebView & m_owner
Definition PrinterWebViewHandler.hpp:30

References m_owner.

Referenced by Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::send_ipc_message().

Here is the caller graph for this function:

◆ handle_get_sn_request()

void Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_get_sn_request ( const std::string & request_id,
const std::string & method )
inlineprivate
297 {
298 // Panel always calls get_sn with a 10s IPC timeout. Answer immediately from
299 // dev_sn / cache — do not spawn a thread or perform HTTP (panel uses URL sn on miss).
300 std::string sn;
301 if (DynamicPrintConfig* config = get_active_printer_config()) {
302 const std::unique_ptr<PrintHost> host(PrintHost::get_print_host(config));
303 if (host)
304 sn = host->get_sn();
305 }
306 json data = { { "sn", sn } };
307 send_ipc_message("response", request_id, method, 0, "success", dump_json(data));
308 }
void send_ipc_message(const char *type, const std::string &request_id, const std::string &method, int code, const std::string &message, const std::string &data_json="{}")
Definition PrinterWebViewHandler.cpp:159
static PrintHost * get_print_host(DynamicPrintConfig *config)
Definition PrintHost.cpp:58
DynamicPrintConfig * get_active_printer_config()
Definition PrinterWebViewHandler.cpp:59
std::string dump_json(const json &node)
Definition PrinterWebViewHandler.cpp:73
nlohmann::json json
Definition AssembleList.cpp:42

References Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::dump_json(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::get_active_printer_config(), Slic3r::PrintHost::get_print_host(), and send_ipc_message().

Referenced by on_script_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_open_file_dialog_request()

void Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_open_file_dialog_request ( const std::string & request_id,
const std::string & method,
const std::string & params_json )
inlineprivate
275 {
276 json params = json::parse(params_json, nullptr, false);
277 if (params.is_discarded())
278 params = json::object();
279
280 const std::string filter = json_string(params, "filter").empty() ? _u8L("All files (*.*)|*.*") : json_string(params, "filter");
281
282 wxWindow* parent = owner().GetParent();
283 if (parent == nullptr)
284 parent = wxGetApp().GetTopWindow();
285
286 wxFileDialog open_file_dialog(parent, _L("Open File"), "", "", wxString::FromUTF8(filter), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
287
288 json data = json::object();
289 data["files"] = json::array();
290 if (open_file_dialog.ShowModal() != wxID_CANCEL)
291 data["files"].push_back(open_file_dialog.GetPath().ToUTF8().data());
292
293 send_ipc_message("response", request_id, method, 0, "success", dump_json(data));
294 }
#define _L(s)
Definition OBJ.cpp:34
std::string json_string(const json &node, const char *key)
Definition PrinterWebViewHandler.cpp:67
#define _u8L(s)
Definition I18N.hpp:11

References _L, _u8L, Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::dump_json(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::json_string(), Slic3r::GUI::PrinterWebViewHandler::owner(), and send_ipc_message().

Referenced by on_script_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_upload_request()

void Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_upload_request ( const std::string & request_id,
const std::string & method,
const std::string & params_json )
inlineprivate
191 {
192 if (upload_in_progress.exchange(true)) {
193 send_ipc_message("response", request_id, method, 1, "Upload already in progress");
194 return;
195 }
196
197 if (upload_thread.joinable())
198 upload_thread.join();
199
200 json params = json::parse(params_json, nullptr, false);
201 if (params.is_discarded())
202 params = json::object();
203
204 std::string file_path = json_string(params, "filePath");
205 std::string file_name = json_string(params, "fileName");
206
207 if (file_path.empty()) {
208 upload_in_progress = false;
209 send_ipc_message("response", request_id, method, 1, "Missing filePath");
210 return;
211 }
212
213 // HTML IPC passes UTF-8 strings; decode explicitly to avoid Windows codepage issues.
214 boost::filesystem::path source_path = path_from_utf8(file_path);
215 if (file_name.empty())
216 file_name = filename_to_utf8(source_path);
217
218 DynamicPrintConfig* config = get_active_printer_config();
219 std::unique_ptr<PrintHost> print_host(config == nullptr ? nullptr : PrintHost::get_print_host(config));
220 if (print_host == nullptr) {
221 upload_in_progress = false;
222 send_ipc_message("response", request_id, method, 1, "Could not get a valid Printer Host reference");
223 return;
224 }
225
226 stop_upload = false;
227 upload_thread = std::thread([this, request_id, method, file_path, file_name, source_path, print_host = std::move(print_host)]() mutable {
228 std::string error_message;
229
230 PrintHostUpload upload_data;
231 upload_data.use_3mf = false;
232 upload_data.post_action = PrintHostPostUploadAction::None;
233 upload_data.source_path = source_path;
234 upload_data.upload_path = path_from_utf8(file_name);
235
236 const bool success = print_host->upload(
237 std::move(upload_data),
238 [this, request_id](Http::Progress progress, bool& cancel) {
239 cancel = stop_upload.load();
240 json data = {
241 {"uploadedBytes", static_cast<uint64_t>(progress.ulnow)},
242 {"totalBytes", static_cast<uint64_t>(progress.ultotal)}
243 };
244 send_ipc_message("event", request_id, "upload_progress", 0, "", dump_json(data));
245 },
246 [&error_message](wxString error) {
247 error_message = error.ToUTF8().data();
248 },
249 [this, request_id](wxString tag, wxString status) {
250 json data = {
251 {"tag", tag.ToUTF8().data()},
252 {"status", status.ToUTF8().data()}
253 };
254 send_ipc_message("event", request_id, "upload_info", 0, "", dump_json(data));
255 });
256
257 upload_in_progress = false;
258
259 if (success) {
260 json data = {
261 {"success", true},
262 {"filePath", file_path},
263 {"fileName", file_name}
264 };
265 send_ipc_message("response", request_id, method, 0, "success", dump_json(data));
266 } else {
267 if (error_message.empty())
268 error_message = "Upload failed";
269 send_ipc_message("response", request_id, method, 1, error_message);
270 }
271 });
272 }
std::atomic< bool > upload_in_progress
Definition PrinterWebViewHandler.cpp:310
std::string filename_to_utf8(const boost::filesystem::path &path)
Definition PrinterWebViewHandler.cpp:88
boost::filesystem::path path_from_utf8(const std::string &utf8_path)
Definition PrinterWebViewHandler.cpp:78
@ PrintHostUpload
Definition NotificationManager.hpp:114
@ None
Definition PrintHost.hpp:24

References Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::dump_json(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::filename_to_utf8(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::get_active_printer_config(), Slic3r::PrintHost::get_print_host(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::json_string(), Slic3r::None, Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::path_from_utf8(), Slic3r::PrintHostUpload::post_action, send_ipc_message(), Slic3r::PrintHostUpload::source_path, stop_upload, Slic3r::Http::Progress::ulnow, Slic3r::Http::Progress::ultotal, upload_in_progress, Slic3r::PrintHostUpload::upload_path, upload_thread, and Slic3r::PrintHostUpload::use_3mf.

Referenced by on_script_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_loaded()

void Slic3r::GUI::PrinterWebViewHandler::on_loaded ( wxWebViewEvent & evt)
virtualinherited
40{
41}

◆ on_script_message()

void Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::on_script_message ( wxWebViewEvent & evt)
inlineoverridevirtual

Reimplemented from Slic3r::GUI::PrinterWebViewHandler.

114 {
115 const wxString message = evt.GetString();
116 if (message.empty())
117 return;
118
119 json root = json::parse(message.ToUTF8().data(), nullptr, false);
120 if (root.is_discarded() || !root.is_object())
121 return;
122
123 std::string request_id = json_string(root, "id");
124 std::string method = json_string(root, "method");
125 json params = root.contains("params") && root["params"].is_object() ? root["params"] : json::object();
126
127 if (method.empty()) {
128 method = json_string(root, "command");
129 if (params.empty() && root.contains("data") && root["data"].is_object())
130 params = root["data"];
131 }
132
133 if (method == "open" || method == "common_openurl") {
134 const std::string url = json_string(params, "url").empty() ? json_string(root, "url") : json_string(params, "url");
135 if (!url.empty())
136 wxLaunchDefaultBrowser(url);
137 if (!request_id.empty())
138 send_ipc_message("response", request_id, method, 0, "success");
139 return;
140 }
141
142 if (method == "upload_file") {
143 handle_upload_request(request_id, method, dump_json(params));
144 return;
145 }
146
147 if (method == "open_file_dialog") {
148 handle_open_file_dialog_request(request_id, method, dump_json(params));
149 return;
150 }
151
152 if (method == "get_sn") {
153 handle_get_sn_request(request_id, method);
154 return;
155 }
156 }
void handle_open_file_dialog_request(const std::string &request_id, const std::string &method, const std::string &params_json)
Definition PrinterWebViewHandler.cpp:274
void handle_upload_request(const std::string &request_id, const std::string &method, const std::string &params_json)
Definition PrinterWebViewHandler.cpp:190
void handle_get_sn_request(const std::string &request_id, const std::string &method)
Definition PrinterWebViewHandler.cpp:296

References Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::dump_json(), handle_get_sn_request(), handle_open_file_dialog_request(), handle_upload_request(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::json_string(), and send_ipc_message().

Here is the call graph for this function:

◆ owner()

PrinterWebView & Slic3r::GUI::PrinterWebViewHandler::owner ( ) const
protectedinherited
48{
49 return m_owner;
50}

References m_owner.

Referenced by Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::ElegooPrinterWebViewHandler(), PrinterWebViewHandler(), and Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::handle_open_file_dialog_request().

Here is the caller graph for this function:

◆ send_ipc_message()

void Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::send_ipc_message ( const char * type,
const std::string & request_id,
const std::string & method,
int code,
const std::string & message,
const std::string & data_json = "{}" )
inlineprivate
160 {}")
161 {
162 if (browser() == nullptr)
163 return;
164
165 json body = json::object();
166 body["type"] = type;
167 if (!request_id.empty())
168 body["id"] = request_id;
169 if (!method.empty())
170 body["method"] = method;
171
172 json data = json::parse(data_json, nullptr, false);
173 if (data.is_discarded())
174 data = json::object();
175 body["data"] = std::move(data);
176
177 if (std::string(type) == "response") {
178 body["code"] = code;
179 body["message"] = message;
180 }
181
182 const wxString payload = wxString::FromUTF8(dump_json(body));
183 const wxString script = "if (typeof HandleStudio === 'function') { HandleStudio(" + payload + "); } else { window.postMessage(" + payload + ", '*'); }";
184 wxGetApp().CallAfter([this, script]() {
185 if (browser() != nullptr)
186 WebView::RunScript(browser(), script);
187 });
188 }

References Slic3r::GUI::PrinterWebViewHandler::browser(), Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::dump_json(), and WebView::RunScript().

Referenced by handle_get_sn_request(), handle_open_file_dialog_request(), handle_upload_request(), and on_script_message().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_owner

PrinterWebView& Slic3r::GUI::PrinterWebViewHandler::m_owner
privateinherited

◆ stop_upload

std::atomic<bool> Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::stop_upload { false }
private
311{ false };

Referenced by ~ElegooPrinterWebViewHandler(), and handle_upload_request().

◆ upload_in_progress

std::atomic<bool> Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::upload_in_progress { false }
private
310{ false };

Referenced by handle_upload_request().

◆ upload_thread

std::thread Slic3r::GUI::anonymous_namespace{PrinterWebViewHandler.cpp}::ElegooPrinterWebViewHandler::upload_thread
private

The documentation for this class was generated from the following file: