Skip to content

Commit

Permalink
support cura:// and thingiverse
Browse files Browse the repository at this point in the history
  • Loading branch information
SoftFever committed May 28, 2024
1 parent 6519e85 commit 524efcf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/libslic3r/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ inline bool is_thingiverse_link(const std::string& url) {
const std::regex url_regex("(http|https)://www.thingiverse.com", std::regex_constants::icase);
return std::regex_match(url, url_regex);
}

// sanitize a string to be used as a filename
inline std::string sanitize_filename(const std::string &filename){
const std::regex special_chars("[/\\\\:*?\"<>|]");
return std::regex_replace(filename, special_chars, "_");
}
// File path / name / extension splitting utilities, working with UTF-8,
// to be published to Perl.
namespace PerlUtils {
Expand Down
47 changes: 38 additions & 9 deletions src/slic3r/GUI/DownloaderFileGet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "format.hpp"
#include "GUI.hpp"
#include "I18N.hpp"
#include "libslic3r/Utils.hpp"

namespace Slic3r {
namespace GUI {
Expand Down Expand Up @@ -124,7 +125,15 @@ FileGet::priv::priv(int ID, std::string&& url, const std::string& filename, wxEv
, m_dest_folder(dest_folder)
{
}

std::string extract_remote_filename(const std::string& str) {
std::regex r("filename=\"([^\"]*)\"");
std::smatch match;
if (std::regex_search(str.begin(), str.end(), match, r)) {
return match.str(1);
} else {
return "";
}
}
void FileGet::priv::get_perform()
{
assert(m_evt_handler);
Expand All @@ -135,10 +144,11 @@ void FileGet::priv::get_perform()
m_stopped = false;

// open dest file
std::string extension;
if (m_written == 0)
{
boost::filesystem::path dest_path = m_dest_folder / m_filename;
std::string extension = boost::filesystem::extension(dest_path);
extension = dest_path.extension().string();
std::string just_filename = m_filename.substr(0, m_filename.size() - extension.size());
std::string final_filename = just_filename;
// Find unsed filename
Expand All @@ -164,18 +174,20 @@ void FileGet::priv::get_perform()
m_evt_handler->QueueEvent(evt);
return;
}

m_filename = final_filename + extension;

m_tmp_path = m_dest_folder / (m_filename + "." + std::to_string(get_current_pid()) + ".download");
m_filename = sanitize_filename(final_filename + extension);

m_tmp_path = m_dest_folder / (m_filename + "." + std::to_string(get_current_pid()) + ".download");

wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_NAME_CHANGE);
evt->SetString(boost::nowide::widen(m_filename));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}

boost::filesystem::path dest_path = m_dest_folder / m_filename;
boost::filesystem::path dest_path;
if(!extension.empty())
dest_path = m_dest_folder / m_filename;

wxString temp_path_wstring(m_tmp_path.wstring());

Expand Down Expand Up @@ -208,6 +220,19 @@ void FileGet::priv::get_perform()
Http::get(m_url)
.size_limit(DOWNLOAD_SIZE_LIMIT) //more?
.set_range(range_string)
.on_header_callback([&](std::string header) {
if(dest_path.empty()) {
std::string filename = extract_remote_filename(header);
if (!filename.empty()) {
m_filename = filename;
dest_path = m_dest_folder / m_filename;
wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_NAME_CHANGE);
evt->SetString(boost::nowide::widen(m_filename));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}
}
})
.on_progress([&](Http::Progress progress, bool& cancel) {
// to prevent multiple calls into following ifs (m_cancel / m_pause)
if (m_stopped){
Expand Down Expand Up @@ -292,14 +317,18 @@ void FileGet::priv::get_perform()
//}
try
{
/*
// Orca: thingiverse need this
if (m_written < body.size())
{
// this code should never be entered. As there should be on_progress call after last bit downloaded.
std::string part_for_write = body.substr(m_written);
fwrite(part_for_write.c_str(), 1, part_for_write.size(), file);
}
*/

wxCommandEvent* evt = new wxCommandEvent(EVT_DWNLDR_FILE_PROGRESS);
evt->SetString(std::to_string(100));
evt->SetInt(m_id);
m_evt_handler->QueueEvent(evt);
}
fclose(file);
boost::filesystem::rename(m_tmp_path, dest_path);
}
Expand Down
28 changes: 21 additions & 7 deletions src/slic3r/GUI/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ wxBoxSizer *PreferencesDialog::create_item_checkbox(wxString title, wxWindow *pa
}

wxBoxSizer* PreferencesDialog::create_item_button(
wxString title, wxString title2, wxWindow* parent, wxString tooltip, wxString tooltip2, std::function<void()> onclick)
wxString title, wxString title2, wxWindow* parent, wxString tooltip, wxString tooltip2, std::function<void()> onclick, bool button_on_left/* = false*/)
{
wxBoxSizer *m_sizer_checkbox = new wxBoxSizer(wxHORIZONTAL);

Expand Down Expand Up @@ -776,8 +776,13 @@ wxBoxSizer* PreferencesDialog::create_item_button(

m_button_download->Bind(wxEVT_BUTTON, [this, onclick](auto &e) { onclick(); });

m_sizer_checkbox->Add(m_staticTextPath, 0, wxALIGN_CENTER_VERTICAL | wxALL, FromDIP(5));
m_sizer_checkbox->Add(m_button_download, 0, wxALL, FromDIP(5));
if (button_on_left) {
m_sizer_checkbox->Add(m_button_download, 0, wxALL, FromDIP(5));
m_sizer_checkbox->Add(m_staticTextPath, 0, wxALIGN_CENTER_VERTICAL | wxALL, FromDIP(5));
} else {
m_sizer_checkbox->Add(m_staticTextPath, 0, wxALIGN_CENTER_VERTICAL | wxALL, FromDIP(5));
m_sizer_checkbox->Add(m_button_download, 0, wxALL, FromDIP(5));
}

return m_sizer_checkbox;
}
Expand Down Expand Up @@ -1059,6 +1064,7 @@ wxWindow* PreferencesDialog::create_general_page()

#ifdef _WIN32
auto title_associate_file = create_item_title(_L("Associate files to OrcaSlicer"), page, _L("Associate files to OrcaSlicer"));
auto title_associate_url = create_item_title(_L("Associate web links to OrcaSlicer"), page, _L("Associate URLs to OrcaSlicer"));

// associate file
auto item_associate_3mf = create_item_checkbox(_L("Associate .3mf files to OrcaSlicer"), page,
Expand All @@ -1076,12 +1082,18 @@ wxWindow* PreferencesDialog::create_general_page()
auto associate_url_prusaslicer = create_item_button(_L("Current association: ") + reg_bin, _L("Associate prusaslicer://"), page,
reg_bin.empty() ? _L("Not associated to any application") : reg_bin,
_L("Associate OrcaSlicer with prusaslicer:// links so that Orca can open models from Printable.com"),
[]() { wxGetApp().associate_url(L"prusaslicer"); });
[]() { wxGetApp().associate_url(L"prusaslicer"); }, false);
wxGetApp().check_url_association(L"bambustudio", reg_bin);
auto associate_url_bambustudio = create_item_button(_L("Current association: ") + reg_bin, _L("Associate bambustudio://"), page,
reg_bin.empty() ? _L("Not associated to any application") : reg_bin,
_L("Associate OrcaSlicer with bambustudio:// links so that Orca can open models from makerworld.com"),
[]() { wxGetApp().associate_url(L"bambustudio"); });
[]() { wxGetApp().associate_url(L"bambustudio"); }, false);

wxGetApp().check_url_association(L"cura", reg_bin);
auto associate_url_cura = create_item_button(_L("Current association: ") + reg_bin, _L("Associate cura://"), page,
reg_bin.empty() ? _L("Not associated to any application") : reg_bin,
_L("Associate OrcaSlicer with cura:// links so that Orca can open models from thingiverse.com"),
[]() { wxGetApp().associate_url(L"cura"); }, false);
#endif

// auto title_modelmall = create_item_title(_L("Online Models"), page, _L("Online Models"));
Expand Down Expand Up @@ -1147,8 +1159,10 @@ wxWindow* PreferencesDialog::create_general_page()
sizer_page->Add(item_associate_step, 0, wxTOP, FromDIP(3));
#endif // _WIN32
#if !defined(__APPLE__)
sizer_page->Add(associate_url_prusaslicer, 0, wxTOP | wxEXPAND, FromDIP(20));
sizer_page->Add(associate_url_bambustudio, 0, wxTOP | wxEXPAND, FromDIP(20));
sizer_page->Add(title_associate_url, 0, wxTOP| wxEXPAND, FromDIP(20));
sizer_page->Add(associate_url_prusaslicer, 0, wxTOP, FromDIP(3));
sizer_page->Add(associate_url_bambustudio, 0, wxTOP, FromDIP(3));
sizer_page->Add(associate_url_cura, 0, wxTOP, FromDIP(3));
#endif
// auto item_title_modelmall = sizer_page->Add(title_modelmall, 0, wxTOP | wxEXPAND, FromDIP(20));
// auto item_item_modelmall = sizer_page->Add(item_modelmall, 0, wxTOP, FromDIP(3));
Expand Down
2 changes: 1 addition & 1 deletion src/slic3r/GUI/Preferences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PreferencesDialog : public DPIDialog
wxBoxSizer *create_item_checkbox(wxString title, wxWindow *parent, wxString tooltip, int padding_left, std::string param);
wxBoxSizer *create_item_darkmode_checkbox(wxString title, wxWindow *parent, wxString tooltip, int padding_left, std::string param);
void set_dark_mode();
wxBoxSizer *create_item_button(wxString title, wxString title2, wxWindow *parent, wxString tooltip, wxString tooltip2, std::function<void()> onclick);
wxBoxSizer *create_item_button(wxString title, wxString title2, wxWindow *parent, wxString tooltip, wxString tooltip2, std::function<void()> onclick, bool button_on_left = false);
wxWindow* create_item_downloads(wxWindow* parent, int padding_left, std::string param);
wxBoxSizer *create_item_input(wxString title, wxString title2, wxWindow *parent, wxString tooltip, std::string param, std::function<void(wxString)> onchange = {});
wxBoxSizer *create_item_backup_input(wxString title, wxWindow *parent, wxString tooltip, std::string param);
Expand Down

0 comments on commit 524efcf

Please sign in to comment.