+ wxArrayTreeItemIds sel;
+ _dkdm->GetSelections (sel);
+ _create->Enable (!_screens->screens().empty() && sel.GetCount() > 0);
+ }
+
+ void dkdm_begin_drag (wxTreeEvent& ev)
+ {
+ ev.Allow ();
+ }
+
+ void dkdm_end_drag (wxTreeEvent& ev)
+ {
+ DKDMMap::iterator from = _dkdm_id.find (_dkdm->GetSelection ());
+ DKDMMap::iterator to = _dkdm_id.find (ev.GetItem ());
+ if (from == _dkdm_id.end() || to == _dkdm_id.end() || from->first == to->first) {
+ return;
+ }
+
+ shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup> (to->second);
+ if (!group) {
+ group = to->second->parent();
+ }
+
+ DCPOMATIC_ASSERT (group);
+ DCPOMATIC_ASSERT (from->second->parent ());
+
+ from->second->parent()->remove (from->second);
+ add_dkdm_model (from->second, group, dynamic_pointer_cast<DKDM>(to->second));
+
+ _dkdm->Delete (from->first);
+ _dkdm_id.erase (from->first);
+ add_dkdm_view (from->second, dynamic_pointer_cast<DKDM>(to->second) ? to->first : optional<wxTreeItemId>());
+ }
+
+ void add_dkdm_clicked ()
+ {
+ wxFileDialog* d = new wxFileDialog (this, _("Select DKDM file"));
+ if (d->ShowModal() == wxID_OK) {
+ shared_ptr<DKDMBase> new_dkdm;
+ 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 */
+ shared_ptr<const dcp::CertificateChain> chain = Config::instance()->decryption_chain();
+ DCPOMATIC_ASSERT (chain->key());
+ dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
+
+ new_dkdm.reset(new DKDM(ekdm));
+ shared_ptr<DKDMGroup> 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())
+ );
+ }
+ }
+ d->Destroy ();
+ }
+
+ void add_dkdm_folder_clicked ()
+ {
+ NewDKDMFolderDialog* d = new NewDKDMFolderDialog (this);
+ if (d->ShowModal() == wxID_OK) {
+ shared_ptr<DKDMBase> new_dkdm (new DKDMGroup (wx_to_std (d->get ())));
+ shared_ptr<DKDMGroup> parent = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
+ if (!parent) {
+ parent = Config::instance()->dkdms ();
+ }
+ add_dkdm_model (new_dkdm, parent);
+ add_dkdm_view (new_dkdm);
+ }
+ d->Destroy ();
+ }
+
+ /** @param dkdm Thing to add.
+ * @param parent Parent group, or 0.
+ */
+ void add_dkdm_view (shared_ptr<DKDMBase> base, optional<wxTreeItemId> previous = optional<wxTreeItemId>())
+ {
+ if (!base->parent()) {
+ /* This is the root group */
+ _dkdm_id[_dkdm->AddRoot("root")] = base;
+ } else {
+ /* Add base to the view */
+ wxTreeItemId added;
+ if (previous) {
+ added = _dkdm->InsertItem(dkdm_to_id(base->parent()), *previous, std_to_wx(base->name()));
+ } else {
+ added = _dkdm->AppendItem(dkdm_to_id(base->parent()), std_to_wx(base->name()));
+ }
+ _dkdm_id[added] = base;
+ }
+
+ /* Add children */
+ shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup> (base);
+ if (g) {
+ BOOST_FOREACH (shared_ptr<DKDMBase> i, g->children()) {
+ add_dkdm_view (i);
+ }
+ }
+ }
+
+ /** @param group Group to add dkdm to */
+ void add_dkdm_model (shared_ptr<DKDMBase> dkdm, shared_ptr<DKDMGroup> group, shared_ptr<DKDM> previous = shared_ptr<DKDM> ())
+ {
+ group->add (dkdm, previous);
+ /* We're messing with a Config-owned object here, so tell it that something has changed.
+ This isn't nice.
+ */
+ Config::instance()->changed ();
+ }
+
+ wxTreeItemId dkdm_to_id (shared_ptr<DKDMBase> dkdm)
+ {
+ for (DKDMMap::iterator i = _dkdm_id.begin(); i != _dkdm_id.end(); ++i) {
+ if (i->second == dkdm) {
+ return i->first;
+ }
+ }
+ DCPOMATIC_ASSERT (false);
+ }
+
+ void remove_dkdm_clicked ()
+ {
+ shared_ptr<DKDMBase> removed = selected_dkdm ();
+ if (!removed) {
+ return;
+ }
+
+ _dkdm->Delete (dkdm_to_id (removed));
+ shared_ptr<DKDMGroup> dkdms = Config::instance()->dkdms ();
+ dkdms->remove (removed);
+ Config::instance()->changed ();