From 3523bb421f17b6ac3b0587b9b206bfce7d006754 Mon Sep 17 00:00:00 2001
From: David Kocik <kocikdav@gmail.com>
Date: Thu, 10 Dec 2020 10:58:40 +0100
Subject: [PATCH 1/2] Ongoing uploads while exiting dialog

Ongoing uploads while exiting dialog

Ongoing uploads while exiting dialog
---
 src/slic3r/GUI/GUI_App.cpp              | 28 +++++++++++++++++++++++++
 src/slic3r/GUI/GUI_App.hpp              |  1 +
 src/slic3r/GUI/MainFrame.cpp            |  4 ++++
 src/slic3r/GUI/MainFrame.hpp            | 13 ++++++------
 src/slic3r/GUI/PrintHostDialogs.cpp     | 16 ++++++++++++--
 src/slic3r/GUI/PrintHostDialogs.hpp     |  5 +++--
 src/slic3r/GUI/UnsavedChangesDialog.cpp |  1 +
 src/slic3r/GUI/UnsavedChangesDialog.hpp |  1 -
 src/slic3r/Utils/PrintHost.cpp          |  5 ++++-
 src/slic3r/Utils/PrintHost.hpp          |  1 +
 10 files changed, 63 insertions(+), 12 deletions(-)

diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp
index 1acf91c74..7d276700f 100644
--- a/src/slic3r/GUI/GUI_App.cpp
+++ b/src/slic3r/GUI/GUI_App.cpp
@@ -65,6 +65,7 @@
 #include "NotificationManager.hpp"
 #include "UnsavedChangesDialog.hpp"
 #include "SavePresetDialog.hpp"
+#include "PrintHostDialogs.hpp"
 
 #include "BitmapCache.hpp"
 
@@ -1707,6 +1708,33 @@ bool GUI_App::check_unsaved_changes(const wxString &header)
     return true;
 }
 
+bool GUI_App::check_print_host_queue()
+{
+    wxString dirty;
+    std::vector<std::pair<std::string, std::string>> jobs;
+    // Get ongoing jobs from dialog
+    mainframe->m_printhost_queue_dlg->get_active_jobs(jobs);
+    if (jobs.empty())
+        return true;
+    // Show dialog
+    wxString job_string = wxString();
+    for (const auto& job : jobs) {
+        job_string += (boost::format("   %1% : %2% \n") % job.first % job.second).str();
+    }
+    wxString message;
+    message += _(L("The uploads are still ongoing")) + ":\n\n" + job_string +"\n" + _(L("Stop them and continue anyway?"));
+    wxMessageDialog dialog(mainframe,
+        message,
+        wxString(SLIC3R_APP_NAME) + " - " + _(L("Ongoing uploads")),
+        wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT);
+    if (dialog.ShowModal() == wxID_YES)
+        return true;
+
+    // TODO: If already shown, bring forward
+    mainframe->m_printhost_queue_dlg->Show();
+    return false;
+}
+
 bool GUI_App::checked_tab(Tab* tab)
 {
     bool ret = true;
diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp
index 672904459..db9f743cc 100644
--- a/src/slic3r/GUI/GUI_App.hpp
+++ b/src/slic3r/GUI/GUI_App.hpp
@@ -205,6 +205,7 @@ public:
 
     void            add_config_menu(wxMenuBar *menu);
     bool            check_unsaved_changes(const wxString &header = wxString());
+    bool            check_print_host_queue();
     bool            checked_tab(Tab* tab);
     void            load_current_presets(bool check_printer_presets = true);
 
diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp
index f2f9db400..9482258de 100644
--- a/src/slic3r/GUI/MainFrame.cpp
+++ b/src/slic3r/GUI/MainFrame.cpp
@@ -209,6 +209,10 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S
             event.Veto();
             return;
         }
+        if (event.CanVeto() && !wxGetApp().check_print_host_queue()) {
+            event.Veto();
+            return;
+        }
         this->shutdown();
         // propagate event
         event.Skip();
diff --git a/src/slic3r/GUI/MainFrame.hpp b/src/slic3r/GUI/MainFrame.hpp
index fb393f619..9504376b4 100644
--- a/src/slic3r/GUI/MainFrame.hpp
+++ b/src/slic3r/GUI/MainFrame.hpp
@@ -79,7 +79,7 @@ class MainFrame : public DPIFrame
     wxMenuItem* m_menu_item_reslice_now { nullptr };
     wxSizer*    m_main_sizer{ nullptr };
 
-    PrintHostQueueDialog *m_printhost_queue_dlg;
+    
 
     size_t      m_last_selected_tab;
 
@@ -187,11 +187,12 @@ public:
 
     PrintHostQueueDialog* printhost_queue_dlg() { return m_printhost_queue_dlg; }
 
-    Plater*             m_plater { nullptr };
-    wxNotebook*         m_tabpanel { nullptr };
-    SettingsDialog      m_settings_dialog;
-    wxWindow*           m_plater_page{ nullptr };
-    wxProgressDialog*   m_progress_dialog { nullptr };
+    Plater*               m_plater { nullptr };
+    wxNotebook*           m_tabpanel { nullptr };
+    SettingsDialog        m_settings_dialog;
+    wxWindow*             m_plater_page{ nullptr };
+    wxProgressDialog*     m_progress_dialog { nullptr };
+    PrintHostQueueDialog* m_printhost_queue_dlg;
     std::shared_ptr<ProgressStatusBar>  m_statusbar;
 
 #ifdef __APPLE__
diff --git a/src/slic3r/GUI/PrintHostDialogs.cpp b/src/slic3r/GUI/PrintHostDialogs.cpp
index ec9e36c45..ef072397b 100644
--- a/src/slic3r/GUI/PrintHostDialogs.cpp
+++ b/src/slic3r/GUI/PrintHostDialogs.cpp
@@ -230,6 +230,7 @@ void PrintHostQueueDialog::append_job(const PrintHostJob &job)
     fields.push_back(wxVariant(job.upload_data.upload_path.string()));
     fields.push_back(wxVariant(""));
     job_list->AppendItem(fields, static_cast<wxUIntPtr>(ST_NEW));
+    upload_names.emplace_back(job.printhost->get_host(), job.upload_data.upload_path.string());
 }
 
 void PrintHostQueueDialog::on_dpi_changed(const wxRect &suggested_rect)
@@ -317,6 +318,17 @@ void PrintHostQueueDialog::on_cancel(Event &evt)
 
     on_list_select();
 }
-
-
+void PrintHostQueueDialog::get_active_jobs(std::vector<std::pair<std::string, std::string>>& ret)
+{
+    int ic = job_list->GetItemCount();
+    for (size_t i = 0; i < ic; i++)
+    {
+        auto item = job_list->RowToItem(i);
+        auto data = job_list->GetItemData(item);
+        JobState st = static_cast<JobState>(data);
+        if(st == JobState::ST_NEW || st == JobState::ST_PROGRESS)
+            ret.emplace_back(upload_names[i]);       
+    }
+    //job_list->data
+}
 }}
diff --git a/src/slic3r/GUI/PrintHostDialogs.hpp b/src/slic3r/GUI/PrintHostDialogs.hpp
index 71973b64e..1fda0db66 100644
--- a/src/slic3r/GUI/PrintHostDialogs.hpp
+++ b/src/slic3r/GUI/PrintHostDialogs.hpp
@@ -64,7 +64,7 @@ public:
     PrintHostQueueDialog(wxWindow *parent);
 
     void append_job(const PrintHostJob &job);
-
+    void get_active_jobs(std::vector<std::pair<std::string, std::string>>& ret);
 protected:
     void on_dpi_changed(const wxRect &suggested_rect) override;
 
@@ -103,13 +103,14 @@ private:
     void on_progress(Event&);
     void on_error(Event&);
     void on_cancel(Event&);
+    // This vector keep adress and filename of uploads. It is used when checking for running uploads during exit.
+    std::vector<std::pair<std::string, std::string>> upload_names;
 };
 
 wxDECLARE_EVENT(EVT_PRINTHOST_PROGRESS, PrintHostQueueDialog::Event);
 wxDECLARE_EVENT(EVT_PRINTHOST_ERROR, PrintHostQueueDialog::Event);
 wxDECLARE_EVENT(EVT_PRINTHOST_CANCEL, PrintHostQueueDialog::Event);
 
-
 }}
 
 #endif
diff --git a/src/slic3r/GUI/UnsavedChangesDialog.cpp b/src/slic3r/GUI/UnsavedChangesDialog.cpp
index db0e65da9..946bdd8c1 100644
--- a/src/slic3r/GUI/UnsavedChangesDialog.cpp
+++ b/src/slic3r/GUI/UnsavedChangesDialog.cpp
@@ -1208,6 +1208,7 @@ FullCompareDialog::FullCompareDialog(const wxString& option_name, const wxString
 }
 
 
+
 }
 
 }    // namespace Slic3r::GUI
diff --git a/src/slic3r/GUI/UnsavedChangesDialog.hpp b/src/slic3r/GUI/UnsavedChangesDialog.hpp
index c799eba7e..232802b66 100644
--- a/src/slic3r/GUI/UnsavedChangesDialog.hpp
+++ b/src/slic3r/GUI/UnsavedChangesDialog.hpp
@@ -270,7 +270,6 @@ public:
     ~FullCompareDialog() {}
 };
 
-
 } 
 }
 
diff --git a/src/slic3r/Utils/PrintHost.cpp b/src/slic3r/Utils/PrintHost.cpp
index ace7e2fe4..111af5c33 100644
--- a/src/slic3r/Utils/PrintHost.cpp
+++ b/src/slic3r/Utils/PrintHost.cpp
@@ -274,6 +274,9 @@ void PrintHostJobQueue::cancel(size_t id)
 {
     p->channel_cancels.push(id);
 }
-
+bool PrintHostJobQueue::can_close()
+{
+    return !p->bg_thread.joinable();
+}
 
 }
diff --git a/src/slic3r/Utils/PrintHost.hpp b/src/slic3r/Utils/PrintHost.hpp
index 35a870b29..1323f28a4 100644
--- a/src/slic3r/Utils/PrintHost.hpp
+++ b/src/slic3r/Utils/PrintHost.hpp
@@ -109,6 +109,7 @@ public:
     void enqueue(PrintHostJob job);
     void cancel(size_t id);
 
+    bool can_close();
 private:
     struct priv;
     std::shared_ptr<priv> p;

From 8522c06812f976f538f13b214d5d2a4e9f7aab6c Mon Sep 17 00:00:00 2001
From: David Kocik <kocikdav@gmail.com>
Date: Thu, 10 Dec 2020 11:00:14 +0100
Subject: [PATCH 2/2] cleanup

---
 src/slic3r/Utils/PrintHost.cpp | 4 ----
 src/slic3r/Utils/PrintHost.hpp | 1 -
 2 files changed, 5 deletions(-)

diff --git a/src/slic3r/Utils/PrintHost.cpp b/src/slic3r/Utils/PrintHost.cpp
index 111af5c33..589679e47 100644
--- a/src/slic3r/Utils/PrintHost.cpp
+++ b/src/slic3r/Utils/PrintHost.cpp
@@ -274,9 +274,5 @@ void PrintHostJobQueue::cancel(size_t id)
 {
     p->channel_cancels.push(id);
 }
-bool PrintHostJobQueue::can_close()
-{
-    return !p->bg_thread.joinable();
-}
 
 }
diff --git a/src/slic3r/Utils/PrintHost.hpp b/src/slic3r/Utils/PrintHost.hpp
index 1323f28a4..35a870b29 100644
--- a/src/slic3r/Utils/PrintHost.hpp
+++ b/src/slic3r/Utils/PrintHost.hpp
@@ -109,7 +109,6 @@ public:
     void enqueue(PrintHostJob job);
     void cancel(size_t id);
 
-    bool can_close();
 private:
     struct priv;
     std::shared_ptr<priv> p;