Sort DKDMs by CPL name (#2297).
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
index ee5e454579bfda8812f6b2e324437dd5a4c59401..62955142a7aaf4dd073d77d51c910cffd67bc0b9 100644 (file)
 */
 
 
-#include "wx/full_config_dialog.h"
 #include "wx/about_dialog.h"
-#include "wx/report_problem_dialog.h"
+#include "wx/dcpomatic_button.h"
+#include "wx/editable_list.h"
 #include "wx/file_picker_ctrl.h"
-#include "wx/wx_util.h"
-#include "wx/wx_signal_manager.h"
-#include "wx/screens_panel.h"
-#include "wx/kdm_timing_panel.h"
-#include "wx/kdm_output_panel.h"
+#include "wx/full_config_dialog.h"
 #include "wx/job_view_dialog.h"
-#include "wx/file_dialog_wrapper.h"
+#include "wx/kdm_output_panel.h"
+#include "wx/kdm_timing_panel.h"
+#include "wx/nag_dialog.h"
 #include "wx/new_dkdm_folder_dialog.h"
-#include "wx/editable_list.h"
+#include "wx/report_problem_dialog.h"
+#include "wx/screens_panel.h"
 #include "wx/static_text.h"
-#include "wx/dcpomatic_button.h"
-#include "wx/nag_dialog.h"
+#include "wx/wx_signal_manager.h"
+#include "wx/wx_util.h"
+#include "lib/cinema.h"
+#include "lib/compose.hpp"
 #include "lib/config.h"
-#include "lib/util.h"
-#include "lib/screen.h"
+#include "lib/cross.h"
+#include "lib/dcpomatic_log.h"
+#include "lib/dkdm_wrapper.h"
+#include "lib/exceptions.h"
+#include "lib/file_log.h"
 #include "lib/job_manager.h"
 #include "lib/kdm_with_metadata.h"
-#include "lib/exceptions.h"
+#include "lib/screen.h"
 #include "lib/send_kdm_email_job.h"
-#include "lib/compose.hpp"
-#include "lib/cinema.h"
-#include "lib/dkdm_wrapper.h"
-#include "lib/cross.h"
+#include "lib/util.h"
 #include <dcp/encrypted_kdm.h>
 #include <dcp/decrypted_kdm.h>
 #include <dcp/exceptions.h>
-#include "lib/warnings.h"
-DCPOMATIC_DISABLE_WARNINGS
-#include <wx/wx.h>
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
+#include <wx/filepicker.h>
 #include <wx/preferences.h>
 #include <wx/splash.h>
-#include <wx/filepicker.h>
-DCPOMATIC_ENABLE_WARNINGS
+#include <wx/treectrl.h>
+#include <wx/wx.h>
+LIBDCP_ENABLE_WARNINGS
 #ifdef __WXOSX__
 #include <ApplicationServices/ApplicationServices.h>
 #endif
@@ -68,11 +70,12 @@ DCPOMATIC_ENABLE_WARNINGS
 
 using std::exception;
 using std::list;
-using std::string;
-using std::vector;
-using std::pair;
+using std::make_shared;
 using std::map;
+using std::pair;
 using std::shared_ptr;
+using std::string;
+using std::vector;
 using boost::bind;
 using boost::optional;
 using boost::ref;
@@ -92,7 +95,7 @@ class DOMFrame : public wxFrame
 {
 public:
        explicit DOMFrame (wxString const & title)
-               : wxFrame (0, -1, title)
+               : wxFrame (nullptr, -1, title)
                , _config_dialog (nullptr)
                , _job_view (nullptr)
        {
@@ -179,8 +182,7 @@ public:
                h = new StaticText (overall_panel, _("Output"));
                h->SetFont (subheading_font);
                right->Add (h, 0, wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
-               /* XXX: hard-coded non-interop here */
-               _output = new KDMOutputPanel (overall_panel, false);
+               _output = new KDMOutputPanel (overall_panel);
                right->Add (_output, 0, wxALL, DCPOMATIC_SIZER_Y_GAP);
 
                _create = new Button (overall_panel, _("Create KDMs"));
@@ -203,6 +205,8 @@ public:
                _export_dkdm->Bind (wxEVT_BUTTON, bind (&DOMFrame::export_dkdm_clicked, this));
 
                setup_sensitivity ();
+
+               dcpomatic_log = make_shared<FileLog>(State::write_path("kdm.log"));
        }
 
 private:
@@ -285,7 +289,7 @@ private:
                        if (id) {
                                *id = 0;
                        }
-                       return shared_ptr<DKDMBase> ();
+                       return {};
                }
 
                if (id) {
@@ -294,7 +298,7 @@ private:
 
                auto i = _dkdm_id.find (selections[0]);
                if (i == _dkdm_id.end()) {
-                       return shared_ptr<DKDMBase> ();
+                       return {};
                }
 
                return i->second;
@@ -338,7 +342,7 @@ private:
                                                begin,
                                                end,
                                                decrypted.annotation_text().get_value_or (""),
-                                               decrypted.content_title_text(),
+                                               title,
                                                dcp::LocalTime().as_string()
                                                );
 
@@ -361,11 +365,7 @@ private:
                                        name_values['i'] = encrypted.cpl_id ();
 
                                        /* Encrypt */
-                                       kdms.push_back (
-                                               KDMWithMetadataPtr(
-                                                       new KDMWithMetadata(name_values, i->cinema.get(), i->cinema->emails, encrypted)
-                                                       )
-                                               );
+                                       kdms.push_back (make_shared<KDMWithMetadata>(name_values, i->cinema.get(), i->cinema->emails, encrypted));
                                }
                        }
 
@@ -410,9 +410,9 @@ private:
                _output->setup_sensitivity ();
                wxArrayTreeItemIds sel;
                _dkdm->GetSelections (sel);
-               shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
-               shared_ptr<DKDM> dkdm = dynamic_pointer_cast<DKDM>(selected_dkdm());
-               _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0);
+               auto group = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
+               auto dkdm = dynamic_pointer_cast<DKDM>(selected_dkdm());
+               _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0 && dkdm);
                _remove_dkdm->Enable (sel.GetCount() > 0 && (!group || group->name() != "root"));
                _export_dkdm->Enable (sel.GetCount() > 0 && dkdm);
        }
@@ -448,41 +448,53 @@ private:
 
        void add_dkdm_clicked ()
        {
-               auto d = new wxFileDialog (this, _("Select DKDM file"));
+               auto d = new wxFileDialog(
+                       this,
+                       _("Select DKDM file"),
+                       wxEmptyString,
+                       wxEmptyString,
+                       wxT("XML files|*.xml|All files|*.*"),
+                       wxFD_MULTIPLE
+                       );
+
                if (d->ShowModal() == wxID_OK) {
                        auto chain = Config::instance()->decryption_chain();
                        DCPOMATIC_ASSERT (chain->key());
 
-                       try {
-                               dcp::EncryptedKDM ekdm(dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE));
-                               /* Decrypt the DKDM to make sure that we can */
-                               dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
-
-                               shared_ptr<DKDMBase> new_dkdm(new DKDM(ekdm));
-                               auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
-                               if (!group) {
-                                       group = Config::instance()->dkdms ();
+                       wxArrayString paths;
+                       d->GetPaths(paths);
+                       for (unsigned int i = 0; i < paths.GetCount(); ++i) {
+                               try {
+                                       dcp::EncryptedKDM ekdm(dcp::file_to_string(wx_to_std(paths[i]), MAX_KDM_SIZE));
+                                       /* Decrypt the DKDM to make sure that we can */
+                                       dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
+
+                                       auto new_dkdm = make_shared<DKDM>(ekdm);
+                                       auto group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm());
+                                       if (!group) {
+                                               group = Config::instance()->dkdms ();
+                                       }
+                                       add_dkdm_model (new_dkdm, group);
+                                       add_dkdm_view (new_dkdm);
+                               } catch (dcp::KDMFormatError& e) {
+                                       error_dialog (
+                                               this,
+                                               _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
+                                               std_to_wx(e.what())
+                                               );
+                                       return;
+                               } catch (dcp::KDMDecryptionError &) {
+                                       error_dialog (
+                                               this,
+                                               _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
+                                               );
+                               } catch (dcp::MiscError& e) {
+                                       error_dialog (
+                                               this,
+                                               _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
+                                               std_to_wx(e.what())
+                                               );
                                }
-                               add_dkdm_model (new_dkdm, group);
-                               add_dkdm_view (new_dkdm);
-                       } catch (dcp::KDMFormatError& e) {
-                               error_dialog (
-                                       this,
-                                       _("Could not read file as a KDM.  Perhaps it is badly formatted, or not a KDM at all."),
-                                       std_to_wx(e.what())
-                                       );
-                               return;
-                       } catch (dcp::KDMDecryptionError &) {
-                               error_dialog (
-                                       this,
-                                       _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
-                                       );
-                       } catch (dcp::MiscError& e) {
-                               error_dialog (
-                                       this,
-                                       _("Could not read file as a KDM.  It is much too large.  Make sure you are loading a DKDM (XML) file."),
-                                       std_to_wx(e.what())
-                                       );
                        }
                }
                d->Destroy ();
@@ -492,8 +504,8 @@ private:
        {
                auto d = new NewDKDMFolderDialog (this);
                if (d->ShowModal() == wxID_OK) {
-                       shared_ptr<DKDMBase> new_dkdm (new DKDMGroup (wx_to_std (d->get ())));
-                       auto parent = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
+                       auto new_dkdm = make_shared<DKDMGroup>(wx_to_std(d->get()));
+                       auto parent = dynamic_pointer_cast<DKDMGroup>(selected_dkdm());
                        if (!parent) {
                                parent = Config::instance()->dkdms ();
                        }
@@ -514,11 +526,13 @@ private:
                } else {
                        /* Add base to the view */
                        wxTreeItemId added;
+                       auto parent_id = dkdm_to_id(base->parent());
                        if (previous) {
-                               added = _dkdm->InsertItem(dkdm_to_id(base->parent()), *previous, std_to_wx(base->name()));
+                               added = _dkdm->InsertItem(parent_id, *previous, std_to_wx(base->name()));
                        } else {
-                               added = _dkdm->AppendItem(dkdm_to_id(base->parent()), std_to_wx(base->name()));
+                               added = _dkdm->AppendItem(parent_id, std_to_wx(base->name()));
                        }
+                       _dkdm->SortChildren(parent_id);
                        _dkdm_id[added] = base;
                }
 
@@ -610,6 +624,7 @@ private:
        JobViewDialog* _job_view;
 };
 
+
 /** @class App
  *  @brief The magic App class for wxWidgets.
  */
@@ -618,12 +633,12 @@ class App : public wxApp
 public:
        App ()
                : wxApp ()
-               , _frame (0)
+               , _frame (nullptr)
        {}
 
 private:
 
-       bool OnInit ()
+       bool OnInit () override
        {
                wxSplashScreen* splash = nullptr;
 
@@ -674,7 +689,7 @@ private:
                        _frame->Maximize ();
                        if (splash) {
                                splash->Destroy ();
-                               splash = 0;
+                               splash = nullptr;
                        }
                        _frame->Show ();
 
@@ -693,7 +708,7 @@ private:
        }
 
        /* An unhandled exception has occurred inside the main event loop */
-       bool OnExceptionInMainLoop ()
+       bool OnExceptionInMainLoop () override
        {
                try {
                        throw;
@@ -708,10 +723,10 @@ private:
                                );
                } catch (exception& e) {
                        error_dialog (
-                               0,
+                               nullptr,
                                wxString::Format (
                                        _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
-                                       std_to_wx (e.what ())
+                                       std_to_wx(e.what())
                                        )
                                );
                } catch (...) {
@@ -722,9 +737,9 @@ private:
                return false;
        }
 
-       void OnUnhandledException ()
+       void OnUnhandledException () override
        {
-               error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
+               error_dialog (nullptr, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
        }
 
        void idle ()
@@ -739,7 +754,7 @@ private:
 
        void config_warning (string m)
        {
-               message_dialog (_frame, std_to_wx (m));
+               message_dialog (_frame, std_to_wx(m));
        }
 
        DOMFrame* _frame;