Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::GUI::Downloader Class Reference

#include <src/slic3r/GUI/Downloader.hpp>

+ Inheritance diagram for Slic3r::GUI::Downloader:
+ Collaboration diagram for Slic3r::GUI::Downloader:

Public Member Functions

 Downloader ()
 
bool get_initialized ()
 
void init (const boost::filesystem::path &dest_folder)
 
void start_download (const std::string &full_url)
 
bool user_action_callback (DownloaderUserAction action, int id)
 

Private Member Functions

size_t get_next_id ()
 
void on_progress (wxCommandEvent &event)
 
void on_error (wxCommandEvent &event)
 
void on_complete (wxCommandEvent &event)
 
void on_name_change (wxCommandEvent &event)
 
void on_paused (wxCommandEvent &event)
 
void on_canceled (wxCommandEvent &event)
 
void set_download_state (int id, DownloadState state)
 

Private Attributes

bool m_initialized { false }
 
std::vector< std::unique_ptr< Download > > m_downloads
 
boost::filesystem::path m_dest_folder
 
size_t m_next_id { 0 }
 

Detailed Description

Constructor & Destructor Documentation

◆ Downloader()

Slic3r::GUI::Downloader::Downloader ( )
113 : wxEvtHandler()
114{
115 //Bind(EVT_DWNLDR_FILE_COMPLETE, [](const wxCommandEvent& evt) {});
116 //Bind(EVT_DWNLDR_FILE_PROGRESS, [](const wxCommandEvent& evt) {});
117 //Bind(EVT_DWNLDR_FILE_ERROR, [](const wxCommandEvent& evt) {});
118 //Bind(EVT_DWNLDR_FILE_NAME_CHANGE, [](const wxCommandEvent& evt) {});
119
120 Bind(EVT_DWNLDR_FILE_COMPLETE, &Downloader::on_complete, this);
121 Bind(EVT_DWNLDR_FILE_PROGRESS, &Downloader::on_progress, this);
122 Bind(EVT_DWNLDR_FILE_ERROR, &Downloader::on_error, this);
123 Bind(EVT_DWNLDR_FILE_NAME_CHANGE, &Downloader::on_name_change, this);
124 Bind(EVT_DWNLDR_FILE_PAUSED, &Downloader::on_paused, this);
125 Bind(EVT_DWNLDR_FILE_CANCELED, &Downloader::on_canceled, this);
126}
void on_name_change(wxCommandEvent &event)
Definition Downloader.cpp:217
void on_paused(wxCommandEvent &event)
Definition Downloader.cpp:222
void on_canceled(wxCommandEvent &event)
Definition Downloader.cpp:229
void on_error(wxCommandEvent &event)
Definition Downloader.cpp:174
void on_progress(wxCommandEvent &event)
Definition Downloader.cpp:165
void on_complete(wxCommandEvent &event)
Definition Downloader.cpp:183

References on_canceled(), on_complete(), on_error(), on_name_change(), on_paused(), and on_progress().

+ Here is the call graph for this function:

Member Function Documentation

◆ get_initialized()

bool Slic3r::GUI::Downloader::get_initialized ( )
inline
59{ return m_initialized; }
bool m_initialized
Definition Downloader.hpp:69

References m_initialized.

◆ get_next_id()

size_t Slic3r::GUI::Downloader::get_next_id ( )
inlineprivate
75{ return ++m_next_id; }
size_t m_next_id
Definition Downloader.hpp:74

References m_next_id.

Referenced by start_download().

+ Here is the caller graph for this function:

◆ init()

void Slic3r::GUI::Downloader::init ( const boost::filesystem::path &  dest_folder)
inline
61 {
62 m_dest_folder = dest_folder;
63 m_initialized = true;
64 }
boost::filesystem::path m_dest_folder
Definition Downloader.hpp:72

References m_dest_folder, and m_initialized.

◆ on_canceled()

void Slic3r::GUI::Downloader::on_canceled ( wxCommandEvent &  event)
private
230{
231 size_t id = event.GetInt();
232 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
233 ntf_mngr->set_download_URL_canceled(id);
234}

References Slic3r::GUI::NotificationManager::set_download_URL_canceled().

Referenced by Downloader().

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

◆ on_complete()

void Slic3r::GUI::Downloader::on_complete ( wxCommandEvent &  event)
private
184{
185 // TODO: is this always true? :
186 // here we open the file itself, notification should get 1.f progress from on progress.
188 wxArrayString paths;
189 paths.Add(event.GetString());
190 wxGetApp().plater()->load_files(paths);
191}
void set_download_state(int id, DownloadState state)
Definition Downloader.cpp:236
@ DownloadDone
Definition Downloader.hpp:18

References Slic3r::GUI::DownloadDone, and set_download_state().

Referenced by Downloader().

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

◆ on_error()

void Slic3r::GUI::Downloader::on_error ( wxCommandEvent &  event)
private
175{
176 size_t id = event.GetInt();
178 BOOST_LOG_TRIVIAL(error) << "Download error: " << event.GetString();
179 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
180 ntf_mngr->set_download_URL_error(id, boost::nowide::narrow(event.GetString()));
181 show_error(nullptr, format_wxstr(L"%1%\n%2%", _L("The download has failed") + ":", event.GetString()));
182}
void show_error(wxWindow *parent, const wxString &message, bool monospaced_font)
Definition GUI.cpp:224
wxString format_wxstr(const char *fmt, TArgs &&... args)
Definition format.hpp:42
@ DownloadError
Definition Downloader.hpp:19
char * narrow(char *output, size_t output_size, wchar_t const *source)
Convert NULL terminated UTF source string to NULL terminated output string of size at most output_siz...
Definition convert.hpp:73
#define L(s)
Definition I18N.hpp:18
#define _L(s)
Definition I18N.hpp:3
static char error[256]
Definition tga.cpp:50

References _L, Slic3r::GUI::DownloadError, error, Slic3r::GUI::format_wxstr(), L, boost::nowide::narrow(), set_download_state(), Slic3r::GUI::NotificationManager::set_download_URL_error(), and Slic3r::GUI::show_error().

Referenced by Downloader().

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

◆ on_name_change()

void Slic3r::GUI::Downloader::on_name_change ( wxCommandEvent &  event)
private
218{
219
220}

Referenced by Downloader().

+ Here is the caller graph for this function:

◆ on_paused()

void Slic3r::GUI::Downloader::on_paused ( wxCommandEvent &  event)
private
223{
224 size_t id = event.GetInt();
225 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
226 ntf_mngr->set_download_URL_paused(id);
227}

References Slic3r::GUI::NotificationManager::set_download_URL_paused().

Referenced by Downloader().

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

◆ on_progress()

void Slic3r::GUI::Downloader::on_progress ( wxCommandEvent &  event)
private
166{
167 size_t id = event.GetInt();
168 float percent = (float)std::stoi(boost::nowide::narrow(event.GetString())) / 100.f;
169 //BOOST_LOG_TRIVIAL(error) << "progress " << id << ": " << percent;
170 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
171 BOOST_LOG_TRIVIAL(trace) << "Download "<< id << ": " << percent;
172 ntf_mngr->set_download_URL_progress(id, percent);
173}

References boost::nowide::narrow(), and Slic3r::GUI::NotificationManager::set_download_URL_progress().

Referenced by Downloader().

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

◆ set_download_state()

void Slic3r::GUI::Downloader::set_download_state ( int  id,
DownloadState  state 
)
private
237{
238 for (size_t i = 0; i < m_downloads.size(); ++i) {
239 if (m_downloads[i]->get_id() == id) {
240 m_downloads[i]->set_state(state);
241 return;
242 }
243 }
244}
std::vector< std::unique_ptr< Download > > m_downloads
Definition Downloader.hpp:71

References m_downloads.

Referenced by on_complete(), and on_error().

+ Here is the caller graph for this function:

◆ start_download()

void Slic3r::GUI::Downloader::start_download ( const std::string &  full_url)
129{
130 assert(m_initialized);
131
132 // TODO: There is a misterious slash appearing in recieved msg on windows
133#ifdef _WIN32
134 if (!boost::starts_with(full_url, "prusaslicer://open/?file=")) {
135#else
136 if (!boost::starts_with(full_url, "prusaslicer://open?file=")) {
137#endif
138 BOOST_LOG_TRIVIAL(error) << "Could not start download due to wrong URL: " << full_url;
139 // TODO: show error?
140 return;
141 }
142 size_t id = get_next_id();
143 // TODO: still same mistery
144#ifdef _WIN32
145 std::string escaped_url = FileGet::escape_url(full_url.substr(25));
146#else
147 std::string escaped_url = FileGet::escape_url(full_url.substr(24));
148#endif
149 if (!boost::starts_with(escaped_url, "https://") || !FileGet::is_subdomain(escaped_url, "printables.com")) {
150 std::string msg = format(_L("Download won't start. Download URL doesn't point to https://printables.com : %1%"), escaped_url);
151 BOOST_LOG_TRIVIAL(error) << msg;
152 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
154 return;
155 }
156
157 std::string text(escaped_url);
158 m_downloads.emplace_back(std::make_unique<Download>(id, std::move(escaped_url), this, m_dest_folder));
159 NotificationManager* ntf_mngr = wxGetApp().notification_manager();
160 ntf_mngr->push_download_URL_progress_notification(id, m_downloads.back()->get_filename(), std::bind(&Downloader::user_action_callback, this, std::placeholders::_1, std::placeholders::_2));
161 m_downloads.back()->start();
162 BOOST_LOG_TRIVIAL(debug) << "started download";
163}
size_t get_next_id()
Definition Downloader.hpp:75
bool user_action_callback(DownloaderUserAction action, int id)
Definition Downloader.cpp:192
static std::string escape_url(const std::string &url)
Definition DownloaderFileGet.cpp:21
static bool is_subdomain(const std::string &url, const std::string &domain)
Definition DownloaderFileGet.cpp:36
std::string format(const char *fmt, TArgs &&... args)
Definition format.hpp:56

References _L, Slic3r::GUI::CustomNotification, error, Slic3r::GUI::FileGet::escape_url(), Slic3r::GUI::format(), get_next_id(), Slic3r::GUI::FileGet::is_subdomain(), m_dest_folder, m_downloads, m_initialized, Slic3r::GUI::NotificationManager::push_download_URL_progress_notification(), Slic3r::GUI::NotificationManager::push_notification(), Slic3r::GUI::NotificationManager::RegularNotificationLevel, and user_action_callback().

+ Here is the call graph for this function:

◆ user_action_callback()

bool Slic3r::GUI::Downloader::user_action_callback ( DownloaderUserAction  action,
int  id 
)
193{
194 for (size_t i = 0; i < m_downloads.size(); ++i) {
195 if (m_downloads[i]->get_id() == id) {
196 switch (action) {
198 m_downloads[i]->cancel();
199 return true;
201 m_downloads[i]->pause();
202 return true;
204 m_downloads[i]->resume();
205 return true;
207 open_folder(m_downloads[i]->get_dest_folder());
208 return true;
209 default:
210 return false;
211 }
212 }
213 }
214 return false;
215}
void open_folder(const std::string &path)
Definition Downloader.cpp:13
@ DownloadUserContinued
Definition Downloader.hpp:28
@ DownloadUserCanceled
Definition Downloader.hpp:26
@ DownloadUserPaused
Definition Downloader.hpp:27
@ DownloadUserOpenedFolder
Definition Downloader.hpp:29

References Slic3r::GUI::DownloadUserCanceled, Slic3r::GUI::DownloadUserContinued, Slic3r::GUI::DownloadUserOpenedFolder, Slic3r::GUI::DownloadUserPaused, and m_downloads.

Referenced by start_download().

+ Here is the caller graph for this function:

Member Data Documentation

◆ m_dest_folder

boost::filesystem::path Slic3r::GUI::Downloader::m_dest_folder
private

Referenced by init(), and start_download().

◆ m_downloads

std::vector<std::unique_ptr<Download> > Slic3r::GUI::Downloader::m_downloads
private

◆ m_initialized

bool Slic3r::GUI::Downloader::m_initialized { false }
private

◆ m_next_id

size_t Slic3r::GUI::Downloader::m_next_id { 0 }
private

Referenced by get_next_id().


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