mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-01 01:12:59 -05:00
* Add orca-translations Claude skill and PO tool
Add a .claude skill that guides translation passes over the localization/i18n gettext catalogs. It covers the workflow: run gettext, settle new glossary terms, translate in batches, verify, and report.
Includes translation-rules.md, which condenses the placeholder, line-break and glossary rules plus the guidance on review notes.
Also adds scripts/po_tool.py (standard library only). It lists pending entries with glossary hints, validates and applies translations, adding '# AI Translated' and '# Needs human review' comments, and rewrites only the entries it changes. It can also check catalogs, look up existing translations, suggest glossary candidates and update the wiki glossary TSV.
* Let po_tool apply translations by entry id
pending now gives each entry a short id, so a translation file can name
entries by id instead of repeating the English msgid. apply accepts either.
A "review": "" item clears an earlier review note when overwriting an
entry, and check gives each msgfmt run its own temporary output file so
parallel checks do not collide.
* Fill all empty translations
Regenerate the template with run_gettext --full and translate every empty
entry in the 22 catalogs (7,089 entries) with the orca-translations skill,
following the localization glossary, including the new belt printer,
IDEX/IQEX and plugin terms. Every new entry is marked "# AI Translated";
122 also carry "# Needs human review" with what to check.
* Show entry ids in po_tool lookup and search translations
lookup now prints each entry's id, so an entry found there can be
rewritten with apply --overwrite without repeating the English, and
--msgstr searches the translations instead of the msgids, which finds
every entry that still uses a term being changed.
SKILL.md describes how to change an established term: check the
catalog's usage first, update the glossary cell, then rewrite only the
entries where the term has that meaning.
* Fix mistranslated terms and align catalogs with the glossary
Correct labels that the glossary had copied, and the entries that used
them:
- ca: Raft was rendered as the Brim term; it is now "Rai". Exhaust fan,
Monotonic line, Unretract, Slice, Lateral Honeycomb and PA Tower
labels fixed.
- cs: Lightning infill is "Blesk", not "Osvětlení" (illumination).
- sv: Retraction is "Retraktion", not "Reduktion"; Unretract,
Lagerhöjd, Delkylningsfläkt, Utsugsfläkt, Aligned Rectilinear and the
Preset label fixed.
- de: Brim is "Rand" and Skirt is "Umrandung", as most entries already
had them.
- lt: brim entries used the Skirt word "apvadas"; they now use "kraštas".
- es: Sparse infill is "relleno de baja densidad", the catalog majority.
- eu: nozzle wipe is "garbiketa", not purge.
- th: the project plate is "เพลต", distinct from the bed.
- uk: Quarter Cubic, Latin "PA" in PA Tower, and Belt purge tower.
- pl: Cross Hatch, Tri-hexagon, Volumetric speed and Raft labels.
- zh_TW: Brim stays "Brim", as the existing labels have it.
- tr: "Prime Tower" read "Prime Minister Tower".
- ko: the plugin "Capabilities" label read "performance".
- Build Volume labels fixed in tr, de, cs and eu.
All 242 rewritten entries are marked "# AI Translated".
* Make more UI text translatable and fix pluralization and context
Audit the sources for text that translators could not translate
correctly and fix it.
Mark text that reached users in English only: IDEX/IQEX role names,
plugin permission prompts and errors, 3MF/preset loading errors, G-code
placeholder parser errors, calibration and device messages, notification
texts, file dialog filters, and the config validation messages.
Replace sentences built from translated fragments, such as the prime
tower warnings, "left"/"right" nozzle names, and "Filament " + number,
with single msgids that have placeholders, so translators can reorder
and inflect them.
Use plural forms for texts with a count ("%1% mixed filaments",
"%d objects", "%1% triangles") instead of "(s)" and mismatched verbs.
Fix the swapped singular/plural pair in "The following presets inherit
this preset".
Add translation contexts where one English word has several meanings
(Subscribed/Local, Standard, Pause, View, Step, Model, Camera, Flow,
Filter, Others, Prepare, the Design tools). Show "Size" instead of
"Build Volume" in the Cut tool, which displays the object size.
Fix code that compared translated text with English (the Edit G-code
dialog group, the plugin registration failure check), and add the
bundled web pages' missing string-table entries. Use the single
character for degrees Celsius, say "G-code", and describe the head
filament map without internal jargon.
Install the libslic3r translate callback before the presets load, so
their load errors are translated, and list the 28 source files that had
marked strings but were never extracted into localization/i18n/list.txt.
* Suggest earlier translations and list fuzzy entries in po_tool pending
pending now attaches the closest earlier translation of a similar
English text (from obsolete or translated entries), so a reworded or
split string keeps its wording, and it lists fuzzy entries by default,
since users never see them. A fuzzy entry also carries its stale
translation as previous_translation.
* Fix wording of the non-boolean operands error
The logical operation error said "operators" where it means the
operands the operation was applied to.
* Translate the newly translatable strings in all languages
Regenerate the template after the translatability changes and fill every
new or changed entry in the 22 catalogs (about 12,800 entries): the
strings that became translatable, the plural forms, the entries split by
context, and the previously fuzzy singular entries that became plurals.
Every new entry is marked "# AI Translated", and about 360 also carry a
"# Needs human review" note saying what to check. Terms follow the
localization glossary. Earlier translations of reworded strings were
reused where the meaning was unchanged. Existing entries were not
changed, and obsolete entries are kept.
* Document the source translatability audit in the orca-translations skill
Add a reference describing how strings reach translators, what to look
for (untranslated text, spliced sentences, missing plurals, ambiguous
words), what not to translate, where a context cannot be carried, and
what to do after the edits, plus a scanner that lists untranslated
user-facing string literals.
* Document how to write translatable strings in AGENTS.md
Tell developers to keep each message complete in one msgid: use
placeholders instead of concatenating translated fragments, plural forms
for counts, and contexts for ambiguous words, and to list new source
files in localization/i18n/list.txt. Add a matching code review item.
* Translate the strings added since the rebase
Regenerate the template against the current main and translate the 26
new entries per language: the Fix Model commands and messages, and the
texture displacement image adjustment panel. Each new entry is marked
"# AI Translated"; existing entries are unchanged.
* tidy
686 lines
25 KiB
C++
686 lines
25 KiB
C++
#include "3DPrinterOS.hpp"
|
|
|
|
#include <algorithm>
|
|
#include "slic3r/GUI/Widgets/DropDown.hpp"
|
|
#include <memory>
|
|
#include <boost/filesystem/operations.hpp>
|
|
#include <boost/optional/optional.hpp>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <system_error>
|
|
#include <exception>
|
|
#include <boost/format.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <boost/property_tree/ptree.hpp>
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <wx/arrstr.h>
|
|
#include <wx/gdicmn.h>
|
|
#include <utility>
|
|
#include <wx/progdlg.h>
|
|
#include <wx/string.h>
|
|
#include <wx/event.h>
|
|
#include <wx/dialog.h>
|
|
#include <wx/radiobut.h>
|
|
|
|
#include "PrintHost.hpp"
|
|
#include "libslic3r/PrintConfig.hpp"
|
|
#include "libslic3r/Utils.hpp"
|
|
#include "slic3r/GUI/I18N.hpp"
|
|
#include "slic3r/GUI/GUI.hpp"
|
|
#include "slic3r/GUI/format.hpp"
|
|
#include "slic3r/GUI/GUI_Utils.hpp"
|
|
#include "slic3r/GUI/MsgDialog.hpp"
|
|
#include "slic3r/GUI/Widgets/ComboBox.hpp"
|
|
#include "slic3r/GUI/Widgets/Button.hpp"
|
|
#include "slic3r/GUI/GUI_App.hpp"
|
|
|
|
#include "Http.hpp"
|
|
#include <wx/busyinfo.h>
|
|
#include <wx/timer.h>
|
|
#include <wx/utils.h>
|
|
|
|
|
|
namespace pt = boost::property_tree;
|
|
|
|
namespace {
|
|
|
|
class UploadOptionsDialog : public Slic3r::GUI::DPIDialog
|
|
{
|
|
public:
|
|
UploadOptionsDialog(wxWindow* parent,
|
|
const wxArrayString& cloud_projects,
|
|
const wxArrayString& cloud_printer_types,
|
|
const wxString preset_name)
|
|
: Slic3r::GUI::DPIDialog(parent,
|
|
wxID_ANY,
|
|
_L("3DPrinterOS Cloud upload options"),
|
|
wxDefaultPosition,
|
|
wxSize(100 * Slic3r::GUI::wxGetApp().em_unit(), -1),
|
|
wxDEFAULT_DIALOG_STYLE),
|
|
okButton(nullptr)
|
|
{
|
|
SetFont(Slic3r::GUI::wxGetApp().normal_font());
|
|
SetBackgroundColour(*wxWHITE);
|
|
SetForegroundColour(*wxBLACK);
|
|
|
|
singleRadio = new wxRadioButton(this, wxID_ANY, _L("Single file"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP);
|
|
projectRadio = new wxRadioButton(this, wxID_ANY, _L("Project File"));
|
|
projectsLabel = new wxStaticText(this, wxID_ANY, _L("Project:"));
|
|
wxStaticText* printerLabel = new wxStaticText(this, wxID_ANY, _L("Printer type:"));
|
|
|
|
projectsComboBox = new wxComboBox(this, wxID_ANY, wxString(""), wxDefaultPosition, wxDefaultSize, 0, nullptr, DD_NO_CHECK_ICON);
|
|
printerTypeComboBox = new wxComboBox(this, wxID_ANY, wxString(""), wxDefaultPosition, wxDefaultSize, 0, nullptr, DD_NO_CHECK_ICON | wxTE_READONLY);
|
|
|
|
printerWarningLabel = new wxStaticText(this, wxID_ANY, _L("Printer type not found, please select manually."));
|
|
printerWarningLabel->SetForegroundColour(*wxRED);
|
|
printerWarningLabel->Hide();
|
|
|
|
for (int i = 0; i < cloud_projects.size(); i++) {
|
|
projectsComboBox->Append(cloud_projects[i]);
|
|
}
|
|
if (cloud_printer_types.size() > 0) {
|
|
for (int i = 0; i < cloud_printer_types.size(); i++) {
|
|
printerTypeComboBox->Append(cloud_printer_types[i]);
|
|
if (cloud_printer_types[i].Find(preset_name) != wxNOT_FOUND && printerTypeComboBox->GetSelection() == -1) {
|
|
printerTypeComboBox->SetSelection(i);
|
|
}
|
|
}
|
|
if (printerTypeComboBox->GetCount() > 1) {
|
|
printerWarningLabel->Show();
|
|
} else {
|
|
printerTypeComboBox->SetSelection(0);
|
|
}
|
|
}
|
|
|
|
okButton = new wxButton(this, wxID_OK, _L("OK"));
|
|
wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _L("Cancel"));
|
|
|
|
wxBoxSizer* radioSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
wxBoxSizer* btnSizer = new wxBoxSizer(wxHORIZONTAL);
|
|
radioSizer->Add(singleRadio, 0, wxALL, 5);
|
|
radioSizer->Add(projectRadio, 0, wxALL, 5);
|
|
btnSizer->Add(okButton, 0, wxALL | wxALIGN_CENTER, 5);
|
|
btnSizer->Add(cancelButton, 0, wxALL | wxALIGN_CENTER, 5);
|
|
|
|
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
|
|
sizer->Add(radioSizer, 0, wxALL, 5);
|
|
sizer->Add(projectsLabel, 0, wxALL, 5);
|
|
sizer->Add(projectsComboBox, 0, wxALL | wxEXPAND, 5);
|
|
sizer->Add(printerLabel, 0, wxALL, 5);
|
|
sizer->Add(printerTypeComboBox, 0, wxALL | wxEXPAND, 5);
|
|
sizer->Add(printerWarningLabel, 0, wxLEFT | wxRIGHT | wxBOTTOM, 5);
|
|
sizer->Add(btnSizer, 0, wxALL | wxALIGN_CENTER, 5);
|
|
SetSizer(sizer);
|
|
sizer->Fit(this);
|
|
projectsComboBox->Hide();
|
|
projectsLabel->Hide();
|
|
projectRadio->Bind(wxEVT_RADIOBUTTON, &UploadOptionsDialog::OnRadioButtonSelected, this);
|
|
singleRadio->Bind(wxEVT_RADIOBUTTON, &UploadOptionsDialog::OnRadioButtonSelected, this);
|
|
// Bind combo box selection change to validation
|
|
printerTypeComboBox->Bind(wxEVT_COMBOBOX, &UploadOptionsDialog::OnPrinterTypeChanged, this);
|
|
ValidateOkButton(); // Initial validation
|
|
Slic3r::GUI::wxGetApp().UpdateDlgDarkUI(this);
|
|
|
|
CenterOnParent();
|
|
}
|
|
|
|
void OnRadioButtonSelected(wxCommandEvent& event)
|
|
{
|
|
wxRadioButton* selectedRadio = dynamic_cast<wxRadioButton*>(event.GetEventObject());
|
|
if (selectedRadio) {
|
|
wxString label = selectedRadio->GetLabel();
|
|
if (label == _L("Project File")) {
|
|
projectsComboBox->Show();
|
|
projectsLabel->Show();
|
|
} else {
|
|
projectsComboBox->Hide();
|
|
projectsLabel->Hide();
|
|
}
|
|
Layout();
|
|
}
|
|
}
|
|
|
|
void on_dpi_changed(const wxRect& suggested_rect) {}
|
|
|
|
void OnPrinterTypeChanged(wxCommandEvent& event)
|
|
{
|
|
ValidateOkButton();
|
|
event.Skip();
|
|
}
|
|
|
|
void ValidateOkButton()
|
|
{
|
|
bool hasSelection = (printerTypeComboBox->GetSelection() != wxNOT_FOUND);
|
|
okButton->Enable(hasSelection);
|
|
}
|
|
|
|
void GetValues(std::string& project, std::string& printer_type)
|
|
{
|
|
project = projectRadio->GetValue() ? std::string(projectsComboBox->GetValue().c_str()) : "";
|
|
printer_type = std::string(printerTypeComboBox->GetValue().c_str());
|
|
}
|
|
|
|
private:
|
|
wxComboBox* projectsComboBox;
|
|
wxComboBox* printerTypeComboBox;
|
|
wxStaticText* projectsLabel;
|
|
wxStaticText* printerWarningLabel;
|
|
wxRadioButton* singleRadio;
|
|
wxRadioButton* projectRadio;
|
|
wxButton* okButton;
|
|
};
|
|
|
|
|
|
class TokenAuthDialog : public Slic3r::GUI::DPIDialog
|
|
{
|
|
public:
|
|
TokenAuthDialog(wxWindow* parent, const std::string &url, const std::string& token, const std::string &cafile, pt::ptree& resp)
|
|
: Slic3r::GUI::DPIDialog(parent,
|
|
wxID_ANY,
|
|
"3DPrinterOS",
|
|
wxDefaultPosition,
|
|
wxSize(45 * Slic3r::GUI::wxGetApp().em_unit(), -1),
|
|
wxDEFAULT_DIALOG_STYLE)
|
|
, m_url(url)
|
|
, m_token(token)
|
|
, m_cafile(cafile)
|
|
, m_resp(resp)
|
|
{
|
|
SetFont(Slic3r::GUI::wxGetApp().normal_font());
|
|
SetBackgroundColour(*wxWHITE);
|
|
SetForegroundColour(*wxBLACK);
|
|
|
|
auto* sizer = new wxBoxSizer(wxVERTICAL);
|
|
sizer->Add(new wxStaticText(this, wxID_ANY, _L("Authorizing...")), 1, wxALL | wxCENTER, 10);
|
|
auto* cancelBtn = new wxButton(this, wxID_CANCEL, _L("Cancel"));
|
|
sizer->Add(cancelBtn, 0, wxALL | wxALIGN_CENTER, 10);
|
|
SetSizerAndFit(sizer);
|
|
|
|
Bind(wxEVT_THREAD, [this](wxThreadEvent& e) { EndModal(e.GetId()); });
|
|
Bind(wxEVT_TIMER, &TokenAuthDialog::OnRetry, this);
|
|
Bind(wxEVT_SHOW, &TokenAuthDialog::OnShow, this);
|
|
Bind(wxEVT_BUTTON, &TokenAuthDialog::OnCancel, this, wxID_CANCEL);
|
|
|
|
m_timer.SetOwner(this);
|
|
Slic3r::GUI::wxGetApp().UpdateDlgDarkUI(this);
|
|
CenterOnParent();
|
|
}
|
|
|
|
void on_dpi_changed(const wxRect& suggested_rect) {}
|
|
|
|
private:
|
|
|
|
void OnShow(wxShowEvent& event)
|
|
{
|
|
if (event.IsShown() && !m_started) {
|
|
m_started = true;
|
|
SendRequest();
|
|
}
|
|
event.Skip();
|
|
}
|
|
|
|
void OnCancel(wxCommandEvent&)
|
|
{
|
|
m_cancelled = true;
|
|
if (m_http_ptr) {
|
|
m_http_ptr->cancel(); // abort the background request
|
|
}
|
|
EndModal(wxID_CANCEL);
|
|
}
|
|
|
|
void OnRetry(wxTimerEvent&) { SendRequest(); }
|
|
|
|
void SendRequest()
|
|
{
|
|
if (m_cancelled || m_attempt >= m_max_retries) {
|
|
if (m_attempt >= m_max_retries) {
|
|
m_resp.put("result", false);
|
|
m_resp.put("message", _u8L("Maximum login retries exceeded"));
|
|
}
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_ABORT));
|
|
return;
|
|
}
|
|
|
|
m_attempt++;
|
|
std::string postBody = "token=" + m_token;
|
|
auto http = Slic3r::Http::post(m_url);
|
|
http.timeout_max(60);
|
|
if (!m_cafile.empty()) {
|
|
http.ca_file(m_cafile);
|
|
}
|
|
http.header("Content-Length", std::to_string(postBody.size()));
|
|
http.set_post_body(postBody);
|
|
http.on_error([this](std::string, std::string error, unsigned status) {
|
|
if (!m_cancelled) {
|
|
m_resp.put("result", false);
|
|
// TRN %1% is an HTTP status code.
|
|
m_resp.put("message", (status != 200) ? Slic3r::GUI::format(_u8L("HTTP error: %1%"), status) : error);
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_ABORT));
|
|
}
|
|
})
|
|
.on_complete([this](std::string body, unsigned status) {
|
|
if (!m_cancelled) {
|
|
if (status != 200) {
|
|
m_resp.put("result", false);
|
|
m_resp.put("message", Slic3r::GUI::format(_u8L("HTTP error: %1%"), status));
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_ABORT));
|
|
return;
|
|
}
|
|
|
|
try {
|
|
std::stringstream ss(body);
|
|
pt::read_json(ss, m_resp);
|
|
} catch (...) {
|
|
m_resp.put("result", false);
|
|
m_resp.put("message", _u8L("Could not parse server response."));
|
|
}
|
|
|
|
if (m_resp.get<bool>("result", false) && m_resp.get_optional<std::string>("message.session").has_value()) {
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_OK));
|
|
} else if (m_resp.get<bool>("result", false)) {
|
|
if (m_attempt < m_max_retries)
|
|
m_timer.StartOnce(m_retry_delay_ms);
|
|
else
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_ABORT));
|
|
} else {
|
|
wxQueueEvent(this, new wxThreadEvent(wxEVT_THREAD, wxID_ABORT));
|
|
}
|
|
}
|
|
});
|
|
m_http_ptr = http.perform();
|
|
}
|
|
|
|
private:
|
|
std::string m_token;
|
|
std::string m_url;
|
|
std::string m_cafile;
|
|
pt::ptree& m_resp;
|
|
wxTimer m_timer;
|
|
std::shared_ptr<Slic3r::Http> m_http_ptr;
|
|
bool m_cancelled{false};
|
|
bool m_started{false};
|
|
int m_attempt{0};
|
|
const int m_max_retries{10};
|
|
const int m_retry_delay_ms{500};
|
|
};
|
|
} // namespace
|
|
|
|
namespace Slic3r {
|
|
|
|
static const std::string API_CREDENTIALS_PATH = "3dprinteros_api_cred.json";
|
|
|
|
C3DPrinterOS::C3DPrinterOS(DynamicPrintConfig *config)
|
|
: m_host(config->opt_string("print_host"))
|
|
, m_apikey(config->opt_string("printhost_apikey"))
|
|
, m_preset_name(config->opt_string("printer_model"))
|
|
{
|
|
m_api_session_file_path = (boost::filesystem::path(Slic3r::data_dir()) / API_CREDENTIALS_PATH)
|
|
.make_preferred()
|
|
.string();
|
|
load_api_session();
|
|
}
|
|
|
|
const char *C3DPrinterOS::get_name() const { return "3DPrinterOS"; }
|
|
|
|
bool C3DPrinterOS::test(wxString &msg) const
|
|
{
|
|
return check_session(msg);
|
|
}
|
|
|
|
bool C3DPrinterOS::login(wxString& msg) const
|
|
{
|
|
// Get token for auth
|
|
msg.clear();
|
|
std::string token = get_api_auth_token(msg);
|
|
if (token.empty()) {
|
|
msg = _L("Error. Can't get API token for authorization");
|
|
return false;
|
|
}
|
|
|
|
auto login_url = make_url("noauth/apiglobal_login_with_token/" + token);
|
|
wxLaunchDefaultBrowser(login_url);
|
|
pt::ptree login_resp;
|
|
login_with_token(login_resp, token);
|
|
std::string session, email;
|
|
try {
|
|
if (login_resp.get<bool>("result")) {
|
|
session = login_resp.get<std::string>("message.session");
|
|
email = login_resp.get<std::string>("message.email");
|
|
} else {
|
|
msg = wxString::FromUTF8(login_resp.get<std::string>("message"));
|
|
return false;
|
|
}
|
|
} catch (const std::exception&) {
|
|
msg = _L("Could not parse server response.");
|
|
return false;
|
|
}
|
|
bool res = save_api_session(session, email);
|
|
if (!res) {
|
|
msg = _L("Error saving session to file");
|
|
}
|
|
return res;
|
|
}
|
|
|
|
wxString C3DPrinterOS::get_test_ok_msg() const
|
|
{
|
|
wxString msg = _L("Connection to 3DPrinterOS cloud works correctly.");
|
|
if (!m_username.empty())
|
|
// TRN %1% is the 3DPrinterOS account (e-mail address).
|
|
msg += " " + GUI::format_wxstr(_L("Logged in as user: %1%"), m_username);
|
|
return msg;
|
|
}
|
|
|
|
wxString C3DPrinterOS::get_test_failed_msg(wxString &msg) const
|
|
{
|
|
return GUI::format_wxstr("%s: %s\n\n", _L("Error session check"), msg);
|
|
}
|
|
|
|
bool C3DPrinterOS::upload(
|
|
PrintHostUpload upload_data, ProgressFn prorgess_fn, ErrorFn error_fn, InfoFn info_fn
|
|
) const
|
|
{
|
|
const char *name = get_name();
|
|
const auto upload_filename = upload_data.upload_path.filename();
|
|
const auto upload_parent_path = upload_data.upload_path.parent_path();
|
|
wxString test_msg;
|
|
if (!check_session(test_msg)) {
|
|
error_fn(std::move(test_msg));
|
|
return false;
|
|
}
|
|
|
|
pt::ptree cloud_project_resp;
|
|
pt::ptree cloud_printer_types_resp;
|
|
|
|
get_cloud_projects_list(cloud_project_resp);
|
|
get_cloud_printer_types(cloud_printer_types_resp, m_preset_name);
|
|
wxArrayString cloud_projects_list;
|
|
wxArrayString cloud_printer_types_list;
|
|
|
|
try {
|
|
if (cloud_project_resp.get<bool>("result")) {
|
|
for (const auto &messageItem : cloud_project_resp.get_child("message")) {
|
|
cloud_projects_list.Add(messageItem.second.get<std::string>("name"));
|
|
}
|
|
}
|
|
|
|
if (cloud_printer_types_resp.get<bool>("result")) {
|
|
for (const auto &messageItem : cloud_printer_types_resp.get_child("message")) {
|
|
cloud_printer_types_list.Add(messageItem.second.get<std::string>("description"));
|
|
}
|
|
}
|
|
} catch (const std::exception &) {
|
|
error_fn(_L("Could not parse server response."));
|
|
return false;
|
|
}
|
|
|
|
// Show "Confirm cloud printer type and project for 3DPrinterOS upload
|
|
|
|
UploadOptionsDialog dlg(GUI::wxGetApp().GetTopWindow(), cloud_projects_list, cloud_printer_types_list, m_preset_name);
|
|
|
|
if (dlg.ShowModal() != wxID_OK) {
|
|
error_fn(_L("Canceled"));
|
|
return false;
|
|
}
|
|
|
|
std::string selected_project;
|
|
std::string selected_printer_type;
|
|
dlg.GetValues(selected_project, selected_printer_type);
|
|
std::string project_id;
|
|
std::string printer_type_id;
|
|
|
|
// search for cloud project_id by name
|
|
if (!selected_project.empty()) {
|
|
for (const auto& messageItem : cloud_project_resp.get_child("message")) {
|
|
if (messageItem.second.get<std::string>("name", "") == selected_project) {
|
|
project_id = messageItem.second.get<std::string>("id", "");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// search for cloud printer_type_id by name
|
|
for (const auto& messageItem : cloud_printer_types_resp.get_child("message")) {
|
|
if (messageItem.second.get<std::string>("description", "") == selected_printer_type) {
|
|
printer_type_id = messageItem.second.get<std::string>("id", "");
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool res = true;
|
|
auto url = make_url("apiglobal/upload");
|
|
std::string file_id;
|
|
pt::ptree uploadResponse;
|
|
auto http = Http::post(std::move(url));
|
|
if (!m_cafile.empty()) {
|
|
http.ca_file(m_cafile);
|
|
}
|
|
http.form_add("session", m_apikey)
|
|
.form_add("upload_type_id", "7")
|
|
.form_add("upload_soft_name", "OrcaSlicer")
|
|
.form_add("zip", "false")
|
|
.form_add_file("file", upload_data.source_path.string(), upload_filename.string());
|
|
|
|
if (!project_id.empty()) {
|
|
http.form_add("project_id", project_id);
|
|
} else if (!selected_project.empty()) {
|
|
http.form_add("project_name", selected_project);
|
|
http.form_add("project_color", "grey");
|
|
}
|
|
|
|
http.on_complete([&](std::string body, unsigned status) {
|
|
std::stringstream ss(body);
|
|
try {
|
|
pt::read_json(ss, uploadResponse);
|
|
} catch (const std::exception &) {
|
|
uploadResponse.put("result", false);
|
|
uploadResponse.put("message", _u8L("Could not parse server response."));
|
|
}
|
|
})
|
|
.on_error([&](std::string body, std::string error, unsigned status) {
|
|
error_fn(format_error(body, error, status));
|
|
res = false;
|
|
})
|
|
.on_progress([&](Http::Progress progress, bool &cancel) {
|
|
prorgess_fn(std::move(progress), cancel);
|
|
if (cancel) {
|
|
res = false;
|
|
}
|
|
})
|
|
.perform_sync();
|
|
|
|
try {
|
|
if (uploadResponse.get<bool>("result")) {
|
|
file_id = uploadResponse.get<std::string>("message.file_id");
|
|
} else {
|
|
res = false;
|
|
error_fn(wxString::FromUTF8(uploadResponse.get<std::string>("message")));
|
|
}
|
|
} catch (const std::exception &) {
|
|
res = false;
|
|
error_fn(_L("Error during file upload"));
|
|
}
|
|
// set printer type for uploaded gcode
|
|
if (res) {
|
|
pt::ptree update_file_response;
|
|
update_file(update_file_response, file_id, printer_type_id, "OrcaSlicer");
|
|
try {
|
|
if (!update_file_response.get<bool>("result")) {
|
|
const std::string msg = update_file_response.get<std::string>("message", "Unknown update error");
|
|
BOOST_LOG_TRIVIAL(warning) << "Failed to update uploaded file: " << msg;
|
|
}
|
|
} catch (const std::exception& ex) {
|
|
BOOST_LOG_TRIVIAL(warning) << "Could not parse update response: " << ex.what();
|
|
}
|
|
if (upload_data.post_action == PrintHostPostUploadAction::StartPrint && !upload_data.use_3mf) {
|
|
auto quick_print_url = make_url("quickprint?file_id=" + file_id);
|
|
wxLaunchDefaultBrowser(quick_print_url);
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
void C3DPrinterOS::log_out() const
|
|
{
|
|
boost::filesystem::remove(m_api_session_file_path.c_str());
|
|
}
|
|
|
|
bool C3DPrinterOS::validate_version_text(const boost::optional<std::string> &version_text) const
|
|
{
|
|
return version_text ? boost::starts_with(*version_text, "3DPrinterOS") : true;
|
|
}
|
|
|
|
std::string C3DPrinterOS::make_url(const std::string &path) const
|
|
{
|
|
if (m_host.find("http://") == 0 || m_host.find("https://") == 0) {
|
|
if (m_host.back() == '/') {
|
|
return (boost::format("%1%%2%") % m_host % path).str();
|
|
} else {
|
|
return (boost::format("%1%/%2%") % m_host % path).str();
|
|
}
|
|
} else {
|
|
return (boost::format("https://%1%/%2%") % m_host % path).str();
|
|
}
|
|
}
|
|
|
|
std::string C3DPrinterOS::get_api_auth_token(wxString &err) const
|
|
{
|
|
std::string result;
|
|
pt::ptree resp;
|
|
std::string postBody = "app_type=plugin&app_name=" + Http::url_encode("OrcaSlicer");
|
|
send_form("apiglobal/generate_login_token", postBody, resp);
|
|
try {
|
|
if (resp.get<bool>("result")) {
|
|
result = resp.get<std::string>("message");
|
|
} else {
|
|
err = wxString::FromUTF8(resp.get<std::string>("message"));
|
|
}
|
|
} catch (const std::exception &) {
|
|
err = _L("Could not parse server response.");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void C3DPrinterOS::login_with_token(pt::ptree &resp, const std::string &token) const {
|
|
auto url = make_url("apiglobal/login_with_token");
|
|
TokenAuthDialog dlg(GUI::wxGetApp().GetTopWindow(), url, token, m_cafile, resp);
|
|
dlg.ShowModal();
|
|
}
|
|
|
|
bool C3DPrinterOS::check_session(wxString &msg) const {
|
|
std::string postBody = "session=" + m_apikey;
|
|
pt::ptree resp;
|
|
send_form("apiglobal/check_session", postBody, resp);
|
|
try {
|
|
if (resp.get<bool>("result")) {
|
|
return true;
|
|
} else {
|
|
msg = wxString::FromUTF8(resp.get<std::string>("message"));
|
|
return false;
|
|
}
|
|
|
|
} catch (const std::exception &) {
|
|
msg = _L("Could not parse server response.");
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool C3DPrinterOS::save_api_session(const std::string &session, const std::string &email) const {
|
|
pt::ptree j;
|
|
j.put("session", session);
|
|
j.put("email", email);
|
|
try {
|
|
std::ostringstream json;
|
|
pt::write_json(json, j);
|
|
if (const std::error_code ec = write_file_atomically(m_api_session_file_path, json.str()))
|
|
throw std::system_error(ec);
|
|
} catch (const std::exception &err) {
|
|
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": failed to write json to file. Path = "
|
|
<< m_api_session_file_path
|
|
<< " Reason = " << err.what();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void C3DPrinterOS::load_api_session()
|
|
{
|
|
m_apikey.clear();
|
|
if (boost::filesystem::exists(m_api_session_file_path)) {
|
|
pt::ptree j;
|
|
try {
|
|
pt::read_json(m_api_session_file_path, j);
|
|
m_apikey = j.get<std::string>("session");
|
|
m_username = j.get<std::string>("email");
|
|
} catch (const std::exception &err) {
|
|
BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": load_api_session failed, reason = " << err.what();
|
|
// remove corrupted file to avoid repeated failures
|
|
try {
|
|
boost::filesystem::remove(m_api_session_file_path);
|
|
} catch (...) {}
|
|
}
|
|
};
|
|
}
|
|
|
|
void C3DPrinterOS::send_form(
|
|
const std::string &endpoint,
|
|
const std::string &postBody,
|
|
boost::property_tree::ptree &responseTree
|
|
) const
|
|
{
|
|
responseTree.clear();
|
|
auto url = make_url(endpoint);
|
|
auto http = Http::post(std::move(url));
|
|
if (!m_cafile.empty()) {
|
|
http.ca_file(m_cafile);
|
|
}
|
|
http.header("Content-length", std::to_string(postBody.size()));
|
|
http.set_post_body(postBody);
|
|
http.on_error([&](std::string body, std::string error, unsigned status) {
|
|
BOOST_LOG_TRIVIAL(error) << boost::format("Error sending form: %1%") % error;
|
|
responseTree.put("result", false);
|
|
responseTree.put("message", error);
|
|
})
|
|
.on_complete([&](std::string body, unsigned) {
|
|
std::stringstream ss(body);
|
|
try {
|
|
pt::read_json(ss, responseTree);
|
|
} catch (const std::exception &) {
|
|
responseTree.put("result", false);
|
|
responseTree.put("message", _u8L("Could not parse server response."));
|
|
}
|
|
})
|
|
.perform_sync();
|
|
}
|
|
|
|
void C3DPrinterOS::get_cloud_projects_list(boost::property_tree::ptree &response) const
|
|
{
|
|
std::string postBody = std::string("session=" + m_apikey);
|
|
send_form("apiglobal/get_projects", postBody, response);
|
|
}
|
|
|
|
void C3DPrinterOS::get_cloud_printer_types(boost::property_tree::ptree &response, const std::string &query) const
|
|
{
|
|
std::string postBody = std::string("session=" + m_apikey);
|
|
if (!query.empty()) {
|
|
postBody += "&description=" + Http::url_encode(query) + "&software_version=" + Http::url_encode("OrcaSlicer");
|
|
}
|
|
send_form("apiglobal/get_printer_types", postBody, response);
|
|
}
|
|
|
|
void C3DPrinterOS::update_file(boost::property_tree::ptree &response, const std::string &file_id, const std::string &ptype, const std::string >ype) const
|
|
{
|
|
std::string postBody = "session=" + m_apikey
|
|
+ "&updates[" + file_id + "][ptype]=" + ptype
|
|
+ "&updates[" + file_id + "][gtype]=" + Http::url_encode(gtype)
|
|
+ "&updates[" + file_id + "][zip]=false";
|
|
send_form("apiglobal/file_update", postBody, response);
|
|
}
|
|
|
|
};
|
|
// namespace Slic3r
|