Use more ScopeGuards.
[dcpomatic.git] / src / tools / dcpomatic_batch.cc
index 8bb36476d1b3a6d22d94eea4efcdb223644c9762..64015a0e30179faa7a0dd7b78bf05a2109dc2e21 100644 (file)
@@ -33,6 +33,7 @@
 #include "lib/job.h"
 #include "lib/job_manager.h"
 #include "lib/make_dcp.h"
+#include "lib/scope_guard.h"
 #include "lib/transcode_job.h"
 #include "lib/util.h"
 #include "lib/version.h"
@@ -40,6 +41,7 @@
 LIBDCP_DISABLE_WARNINGS
 #include <wx/aboutdlg.h>
 #include <wx/cmdline.h>
+#include <wx/dnd.h>
 #include <wx/preferences.h>
 #include <wx/splash.h>
 #include <wx/stdpaths.h>
@@ -115,6 +117,31 @@ public:
                PAUSE
        };
 
+       class DCPDropTarget : public wxFileDropTarget
+       {
+       public:
+               DCPDropTarget(DOMFrame* owner)
+                       : _frame(owner)
+               {}
+
+               bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
+               {
+                       if (filenames.GetCount() == 1) {
+                               /* Try to load a directory */
+                               auto path = boost::filesystem::path(wx_to_std(filenames[0]));
+                               if (boost::filesystem::is_directory(path)) {
+                                       _frame->start_job(wx_to_std(filenames[0]));
+                                       return true;
+                               }
+                       }
+
+                       return false;
+               }
+
+       private:
+               DOMFrame* _frame;
+       };
+
        explicit DOMFrame (wxString const & title)
                : wxFrame (nullptr, -1, title)
                , _sizer (new wxBoxSizer(wxVERTICAL))
@@ -156,6 +183,8 @@ public:
 
                Bind (wxEVT_CLOSE_WINDOW, boost::bind(&DOMFrame::close, this, _1));
                Bind (wxEVT_SIZE, boost::bind(&DOMFrame::sized, this, _1));
+
+               SetDropTarget(new DCPDropTarget(this));
        }
 
        void tool_clicked(wxCommandEvent& ev)
@@ -246,10 +275,9 @@ private:
                        _("Unfinished jobs"),
                        wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
                        );
+               ScopeGuard sg = [d]{ d->Destroy(); };
 
-               bool const r = d->ShowModal() == wxID_YES;
-               d->Destroy ();
-               return r;
+               return d->ShowModal() == wxID_YES;
        }
 
        void close (wxCloseEvent& ev)
@@ -294,21 +322,22 @@ private:
        void help_about ()
        {
                auto d = new AboutDialog (this);
+               ScopeGuard sg = [d]() { d->Destroy(); };
                d->ShowModal ();
-               d->Destroy ();
        }
 
        void add_film ()
        {
-               auto c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
+               auto dialog = new wxDirDialog(this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
+               ScopeGuard sg = [dialog]() { dialog->Destroy(); };
                if (_last_parent) {
-                       c->SetPath (std_to_wx(_last_parent.get().string()));
+                       dialog->SetPath(std_to_wx(_last_parent.get().string()));
                }
 
                int r;
                while (true) {
-                       r = c->ShowModal ();
-                       if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
+                       r = dialog->ShowModal();
+                       if (r == wxID_OK && dialog->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
                                error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
                        } else {
                                break;
@@ -316,12 +345,10 @@ private:
                }
 
                if (r == wxID_OK) {
-                       start_job (wx_to_std (c->GetPath ()));
+                       start_job(wx_to_std(dialog->GetPath()));
                }
 
-               _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
-
-               c->Destroy ();
+               _last_parent = boost::filesystem::path(wx_to_std(dialog->GetPath())).parent_path();
        }
 
        void config_changed (Config::Property what)