From 3f69799047a3244717bfe6fd2e5cebb1f09d7260 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Fri, 20 Jan 2023 08:40:54 +0100 Subject: [PATCH] App updater download directory path cehecking and selection --- src/slic3r/GUI/GUI_App.cpp | 4 ++++ src/slic3r/GUI/UpdateDialogs.cpp | 39 ++++++++++++++++++++++++-------- src/slic3r/GUI/UpdateDialogs.hpp | 1 + src/slic3r/Utils/AppUpdater.cpp | 1 + 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp index 12f3a82f0..56b2d2eb1 100644 --- a/src/slic3r/GUI/GUI_App.cpp +++ b/src/slic3r/GUI/GUI_App.cpp @@ -3374,6 +3374,10 @@ void GUI_App::app_updater(bool from_user) if (dialog_result != wxID_OK) { return; } + if (dwnld_dlg.get_download_path().parent_path().empty() || !boost::filesystem::exists(dwnld_dlg.get_download_path().parent_path())) { + show_error(nullptr,GUI::format_wxstr(_L("Download can't proceed. Target directory doesn't exists: %1%"), dwnld_dlg.get_download_path().parent_path().string())); + return; + } app_data.target_path =dwnld_dlg.get_download_path(); // start download diff --git a/src/slic3r/GUI/UpdateDialogs.cpp b/src/slic3r/GUI/UpdateDialogs.cpp index 258a9c784..81e9d6a34 100644 --- a/src/slic3r/GUI/UpdateDialogs.cpp +++ b/src/slic3r/GUI/UpdateDialogs.cpp @@ -147,13 +147,14 @@ AppUpdateDownloadDialog::AppUpdateDownloadDialog( const Semver& ver_online, boos #endif content_sizer->AddSpacer(VERT_SPACING); content_sizer->AddSpacer(VERT_SPACING); - content_sizer->Add(new wxStaticText(this, wxID_ANY, _(L("Target path:")))); + content_sizer->Add(new wxStaticText(this, wxID_ANY, _(L("Target directory:")))); content_sizer->AddSpacer(VERT_SPACING); - txtctrl_path = new wxTextCtrl(this, wxID_ANY, path.wstring()); + txtctrl_path = new wxTextCtrl(this, wxID_ANY, GUI::format_wxstr(path.parent_path().string())); + filename = GUI::format_wxstr(path.filename().string()); content_sizer->Add(txtctrl_path, 1, wxEXPAND); content_sizer->AddSpacer(VERT_SPACING); - wxButton* btn = new wxButton(this, wxID_ANY, _L("Select path")); + wxButton* btn = new wxButton(this, wxID_ANY, _L("Select directory")); content_sizer->Add(btn/*, 1, wxEXPAND*/); // button to open file dialog @@ -165,13 +166,14 @@ AppUpdateDownloadDialog::AppUpdateDownloadDialog( const Semver& ver_online, boos wxString wxext = boost::nowide::widen(extension); wildcard = GUI::format_wxstr("%1% Files (*.%2%)|*.%2%", wxext.Upper(), wxext); } - wxFileDialog save_dlg( + wxDirDialog save_dlg( this - , _L("Save as:") + , _L("Select directory:") , txtctrl_path->GetValue() - , boost::nowide::widen(AppUpdater::get_filename_from_url(txtctrl_path->GetValue().ToUTF8().data())) + /* + , filename //boost::nowide::widen(AppUpdater::get_filename_from_url(txtctrl_path->GetValue().ToUTF8().data())) , wildcard - , wxFD_SAVE | wxFD_OVERWRITE_PROMPT + , wxFD_SAVE | wxFD_OVERWRITE_PROMPT*/ ); if (save_dlg.ShowModal() == wxID_OK) { txtctrl_path->SetValue(save_dlg.GetPath()); @@ -185,8 +187,25 @@ AppUpdateDownloadDialog::AppUpdateDownloadDialog( const Semver& ver_online, boos if (auto* btn_ok = get_button(wxID_OK); btn_ok != NULL) { btn_ok->SetLabel(_L("Download")); btn_ok->Bind(wxEVT_BUTTON, ([this, path](wxCommandEvent& e){ - if (boost::filesystem::exists(boost::filesystem::path(txtctrl_path->GetValue().ToUTF8().data()))) { - MessageDialog msgdlg(nullptr, GUI::format_wxstr(_L("File %1% already exists. Do you wish to overwrite it?"), txtctrl_path->GetValue()),_L("Notice"), wxYES_NO); + boost::filesystem::path path =boost::filesystem::path(txtctrl_path->GetValue().ToUTF8().data()) / GUI::format(filename); + boost::system::error_code ec; + if (path.parent_path().string().empty()) { + MessageDialog msgdlg(nullptr, _L("Directory path is empty."), _L("Notice"), wxYES_NO); + return; + } + ec.clear(); + if (!boost::filesystem::exists(path.parent_path(), ec) || !boost::filesystem::is_directory(path.parent_path(),ec) || ec) { + MessageDialog msgdlg(nullptr, GUI::format_wxstr(_L("Directory %1% doesn't exists. Do you wish to create it?"), GUI::format_wxstr(path.parent_path().string())), _L("Notice"), wxYES_NO); + if (msgdlg.ShowModal() != wxID_YES) + return; + if(!boost::filesystem::create_directory(path.parent_path())) + { + MessageDialog msgdlg(nullptr, _L("Failed to creeate directory."), _L("Notice"), wxYES_NO); + return; + } + } + if (boost::filesystem::exists(path)) { + MessageDialog msgdlg(nullptr, GUI::format_wxstr(_L("File %1% already exists. Do you wish to overwrite it?"), GUI::format_wxstr(path.string())),_L("Notice"), wxYES_NO); if (msgdlg.ShowModal() != wxID_YES) return; } @@ -211,7 +230,7 @@ bool AppUpdateDownloadDialog::run_after_download() const boost::filesystem::path AppUpdateDownloadDialog::get_download_path() const { - return boost::filesystem::path(txtctrl_path->GetValue().ToUTF8().data()); + return boost::filesystem::path(txtctrl_path->GetValue().ToUTF8().data()) / GUI::format(filename); } // MsgUpdateConfig diff --git a/src/slic3r/GUI/UpdateDialogs.hpp b/src/slic3r/GUI/UpdateDialogs.hpp index 0f5e1d6fb..deedd9a50 100644 --- a/src/slic3r/GUI/UpdateDialogs.hpp +++ b/src/slic3r/GUI/UpdateDialogs.hpp @@ -73,6 +73,7 @@ public: private: wxCheckBox* cbox_run; wxTextCtrl* txtctrl_path; + wxString filename; }; // Confirmation dialog informing about configuration update. Lists updated bundles & their versions. diff --git a/src/slic3r/Utils/AppUpdater.cpp b/src/slic3r/Utils/AppUpdater.cpp index aa3339f1e..fc847ec53 100644 --- a/src/slic3r/Utils/AppUpdater.cpp +++ b/src/slic3r/Utils/AppUpdater.cpp @@ -203,6 +203,7 @@ boost::filesystem::path AppUpdater::priv::download_file(const DownloadAppData& d BOOST_LOG_TRIVIAL(error) << "Download from " << data.url << " could not start. Destination path is empty."; return boost::filesystem::path(); } + std::string error_message; bool res = http_get_file(data.url, 130 * 1024 * 1024 //2.4.0 windows installer is 65MB //lm:I don't know, but larger. The binaries will grow. // dk: changed to 130, to have 100% more space. We should put this information into version file. // on_progress