Tidy up code to report failures to load config in the UI.
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
index 4d84f2f339e02af8dc9312e7bc4e60bc96616541..f877ad21f8510d4848f4f162660e563440aca29b 100644 (file)
@@ -22,7 +22,6 @@
 #include "wx/about_dialog.h"
 #include "wx/dcpomatic_button.h"
 #include "wx/editable_list.h"
-#include "wx/file_dialog_wrapper.h"
 #include "wx/file_picker_ctrl.h"
 #include "wx/full_config_dialog.h"
 #include "wx/job_view_dialog.h"
 #include "lib/compose.hpp"
 #include "lib/config.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/screen.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
@@ -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()
                                                );
 
@@ -444,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());
-
-                               auto new_dkdm = make_shared<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 ();
@@ -510,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;
                }
 
@@ -620,14 +638,14 @@ public:
 
 private:
 
-       bool OnInit ()
+       bool OnInit () override
        {
                wxSplashScreen* splash = nullptr;
 
                try {
                        wxInitAllImageHandlers ();
 
-                       Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
+                       Config::FailedToLoad.connect(boost::bind(&App::config_failed_to_load, this, _1));
                        Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
 
                        splash = maybe_show_splash ();
@@ -690,7 +708,7 @@ private:
        }
 
        /* An unhandled exception has occurred inside the main event loop */
-       bool OnExceptionInMainLoop ()
+       bool OnExceptionInMainLoop () override
        {
                try {
                        throw;
@@ -719,7 +737,7 @@ private:
                return false;
        }
 
-       void OnUnhandledException ()
+       void OnUnhandledException () override
        {
                error_dialog (nullptr, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
        }
@@ -729,9 +747,9 @@ private:
                signal_manager->ui_idle ();
        }
 
-       void config_failed_to_load ()
+       void config_failed_to_load(Config::LoadFailure what)
        {
-               message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
+               report_config_load_failure(_frame, what);
        }
 
        void config_warning (string m)