Revert overwrite changes; remove our replacement of file extension (#1383).
[dcpomatic.git] / src / wx / config_dialog.cc
index 3871f738c9711ba02e477730b53211fd2e217540..34bdee8b33693d699de5915414cd5a3b2b193ac9 100644 (file)
 #include "nag_dialog.h"
 
 using std::string;
+using std::vector;
+using std::pair;
+using std::make_pair;
+using std::map;
 using boost::bind;
 using boost::optional;
 using boost::shared_ptr;
+using boost::function;
 
 static
-void
+bool
 do_nothing ()
 {
-
+       return false;
 }
 
 Page::Page (wxSize panel_size, int border)
@@ -39,7 +44,7 @@ Page::Page (wxSize panel_size, int border)
        , _panel_size (panel_size)
        , _window_exists (false)
 {
-       _config_connection = Config::instance()->Changed.connect (boost::bind (&Page::config_changed_wrapper, this));
+       _config_connection = Config::instance()->Changed.connect (bind (&Page::config_changed_wrapper, this));
 }
 
 wxWindow*
@@ -53,7 +58,7 @@ Page::create_window (wxWindow* parent)
        _window_exists = true;
        config_changed ();
 
-       _panel->Bind (wxEVT_DESTROY, boost::bind (&Page::window_destroyed, this));
+       _panel->Bind (wxEVT_DESTROY, bind (&Page::window_destroyed, this));
 
        return _panel;
 }
@@ -108,25 +113,25 @@ void
 GeneralPage::add_language_controls (wxGridBagSizer* table, int& r)
 {
        _set_language = new wxCheckBox (_panel, wxID_ANY, _("Set language"));
-       table->Add (_set_language, wxGBPosition (r, 0));
+       table->Add (_set_language, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
        _language = new wxChoice (_panel, wxID_ANY);
-       std::vector<std::pair<std::string, std::string> > languages;
-       languages.push_back (std::make_pair ("Čeština", "cs_CZ"));
-       languages.push_back (std::make_pair ("汉语/漢語", "zh_CN"));
-       languages.push_back (std::make_pair ("Dansk", "da_DK"));
-       languages.push_back (std::make_pair ("Deutsch", "de_DE"));
-       languages.push_back (std::make_pair ("English", "en_GB"));
-       languages.push_back (std::make_pair ("Español", "es_ES"));
-       languages.push_back (std::make_pair ("Français", "fr_FR"));
-       languages.push_back (std::make_pair ("Italiano", "it_IT"));
-       languages.push_back (std::make_pair ("Nederlands", "nl_NL"));
-       languages.push_back (std::make_pair ("Русский", "ru_RU"));
-       languages.push_back (std::make_pair ("Polski", "pl_PL"));
-       languages.push_back (std::make_pair ("Português europeu", "pt_PT"));
-       languages.push_back (std::make_pair ("Português do Brasil", "pt_BR"));
-       languages.push_back (std::make_pair ("Svenska", "sv_SE"));
-       languages.push_back (std::make_pair ("Slovenský jazyk", "sk_SK"));
-       languages.push_back (std::make_pair ("українська мова", "uk_UA"));
+       vector<pair<string, string> > languages;
+       languages.push_back (make_pair ("Čeština", "cs_CZ"));
+       languages.push_back (make_pair ("汉语/漢語", "zh_CN"));
+       languages.push_back (make_pair ("Dansk", "da_DK"));
+       languages.push_back (make_pair ("Deutsch", "de_DE"));
+       languages.push_back (make_pair ("English", "en_GB"));
+       languages.push_back (make_pair ("Español", "es_ES"));
+       languages.push_back (make_pair ("Français", "fr_FR"));
+       languages.push_back (make_pair ("Italiano", "it_IT"));
+       languages.push_back (make_pair ("Nederlands", "nl_NL"));
+       languages.push_back (make_pair ("Русский", "ru_RU"));
+       languages.push_back (make_pair ("Polski", "pl_PL"));
+       languages.push_back (make_pair ("Português europeu", "pt_PT"));
+       languages.push_back (make_pair ("Português do Brasil", "pt_BR"));
+       languages.push_back (make_pair ("Svenska", "sv_SE"));
+       languages.push_back (make_pair ("Slovenský jazyk", "sk_SK"));
+       languages.push_back (make_pair ("українська мова", "uk_UA"));
        checked_set (_language, languages);
        table->Add (_language, wxGBPosition (r, 1));
        ++r;
@@ -140,15 +145,15 @@ GeneralPage::add_language_controls (wxGridBagSizer* table, int& r)
        restart->SetFont (font);
        ++r;
 
-       _set_language->Bind (wxEVT_CHECKBOX, boost::bind (&GeneralPage::set_language_changed, this));
-       _language->Bind     (wxEVT_CHOICE,   boost::bind (&GeneralPage::language_changed,     this));
+       _set_language->Bind (wxEVT_CHECKBOX, bind (&GeneralPage::set_language_changed, this));
+       _language->Bind     (wxEVT_CHOICE,   bind (&GeneralPage::language_changed,     this));
 }
 
 void
 GeneralPage::add_play_sound_controls (wxGridBagSizer* table, int& r)
 {
        _sound = new wxCheckBox (_panel, wxID_ANY, _("Play sound via"));
-       table->Add (_sound, wxGBPosition (r, 0));
+       table->Add (_sound, wxGBPosition (r, 0), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
        _sound_output = new wxChoice (_panel, wxID_ANY);
        table->Add (_sound_output, wxGBPosition (r, 1));
        ++r;
@@ -161,8 +166,8 @@ GeneralPage::add_play_sound_controls (wxGridBagSizer* table, int& r)
                }
        }
 
-       _sound->Bind        (wxEVT_CHECKBOX, boost::bind (&GeneralPage::sound_changed, this));
-       _sound_output->Bind (wxEVT_CHOICE,   boost::bind (&GeneralPage::sound_output_changed, this));
+       _sound->Bind        (wxEVT_CHECKBOX, bind (&GeneralPage::sound_changed, this));
+       _sound_output->Bind (wxEVT_CHOICE,   bind (&GeneralPage::sound_output_changed, this));
 }
 
 void
@@ -176,8 +181,8 @@ GeneralPage::add_update_controls (wxGridBagSizer* table, int& r)
        table->Add (_check_for_test_updates, wxGBPosition (r, 0), wxGBSpan (1, 2));
        ++r;
 
-       _check_for_updates->Bind (wxEVT_CHECKBOX, boost::bind (&GeneralPage::check_for_updates_changed, this));
-       _check_for_test_updates->Bind (wxEVT_CHECKBOX, boost::bind (&GeneralPage::check_for_test_updates_changed, this));
+       _check_for_updates->Bind (wxEVT_CHECKBOX, bind (&GeneralPage::check_for_updates_changed, this));
+       _check_for_test_updates->Bind (wxEVT_CHECKBOX, bind (&GeneralPage::check_for_test_updates_changed, this));
 }
 
 void
@@ -189,7 +194,7 @@ GeneralPage::config_changed ()
 
        /* Backwards compatibility of config file */
 
-       std::map<std::string, std::string> compat_map;
+       map<string, string> compat_map;
        compat_map["fr"] = "fr_FR";
        compat_map["it"] = "it_IT";
        compat_map["es"] = "es_ES";
@@ -204,7 +209,7 @@ GeneralPage::config_changed ()
        compat_map["cs"] = "cs_CZ";
        compat_map["uk"] = "uk_UA";
 
-       std::string lang = config->language().get_value_or ("en_GB");
+       string lang = config->language().get_value_or ("en_GB");
        if (compat_map.find (lang) != compat_map.end ()) {
                lang = compat_map[lang];
        }
@@ -216,8 +221,8 @@ GeneralPage::config_changed ()
 
        checked_set (_sound, config->sound ());
 
-       boost::optional<std::string> const current_so = get_sound_output ();
-       boost::optional<std::string> configured_so;
+       optional<string> const current_so = get_sound_output ();
+       optional<string> configured_so;
 
        if (config->sound_output()) {
                configured_so = config->sound_output().get();
@@ -255,12 +260,12 @@ GeneralPage::setup_sensitivity ()
 }
 
 /** @return Currently-selected preview sound output in the dialogue */
-boost::optional<std::string>
+optional<string>
 GeneralPage::get_sound_output ()
 {
        int const sel = _sound_output->GetSelection ();
        if (sel == wxNOT_FOUND) {
-               return boost::optional<std::string> ();
+               return optional<string> ();
        }
 
        return wx_to_std (_sound_output->GetString (sel));
@@ -310,7 +315,7 @@ void
 GeneralPage::sound_output_changed ()
 {
        RtAudio audio (DCPOMATIC_RTAUDIO_API);
-       boost::optional<std::string> const so = get_sound_output();
+       optional<string> const so = get_sound_output();
        if (!so || *so == audio.getDeviceInfo(audio.getDefaultOutputDevice()).name) {
                Config::instance()->unset_sound_output ();
        } else {
@@ -322,9 +327,9 @@ CertificateChainEditor::CertificateChainEditor (
        wxWindow* parent,
        wxString title,
        int border,
-       boost::function<void (boost::shared_ptr<dcp::CertificateChain>)> set,
-       boost::function<boost::shared_ptr<const dcp::CertificateChain> (void)> get,
-       boost::function<void (void)> nag_remake
+       function<void (shared_ptr<dcp::CertificateChain>)> set,
+       function<shared_ptr<const dcp::CertificateChain> (void)> get,
+       function<bool (void)> nag_remake
        )
        : wxDialog (parent, wxID_ANY, title)
        , _set (set)
@@ -410,14 +415,14 @@ CertificateChainEditor::CertificateChainEditor (
        table->Add (_private_key_bad, wxGBPosition (r, 0), wxGBSpan (1, 3));
        ++r;
 
-       _add_certificate->Bind     (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::add_certificate, this));
-       _remove_certificate->Bind  (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::remove_certificate, this));
-       _export_certificate->Bind  (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::export_certificate, this));
-       _certificates->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind (&CertificateChainEditor::update_sensitivity, this));
-       _certificates->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&CertificateChainEditor::update_sensitivity, this));
-       _remake_certificates->Bind (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::remake_certificates, this));
-       _import_private_key->Bind  (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::import_private_key, this));
-       _export_private_key->Bind  (wxEVT_BUTTON,       boost::bind (&CertificateChainEditor::export_private_key, this));
+       _add_certificate->Bind     (wxEVT_BUTTON,       bind (&CertificateChainEditor::add_certificate, this));
+       _remove_certificate->Bind  (wxEVT_BUTTON,       bind (&CertificateChainEditor::remove_certificate, this));
+       _export_certificate->Bind  (wxEVT_BUTTON,       bind (&CertificateChainEditor::export_certificate, this));
+       _certificates->Bind        (wxEVT_LIST_ITEM_SELECTED,   bind (&CertificateChainEditor::update_sensitivity, this));
+       _certificates->Bind        (wxEVT_LIST_ITEM_DESELECTED, bind (&CertificateChainEditor::update_sensitivity, this));
+       _remake_certificates->Bind (wxEVT_BUTTON,       bind (&CertificateChainEditor::remake_certificates, this));
+       _import_private_key->Bind  (wxEVT_BUTTON,       bind (&CertificateChainEditor::import_private_key, this));
+       _export_private_key->Bind  (wxEVT_BUTTON,       bind (&CertificateChainEditor::export_private_key, this));
 
        wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
        if (buttons) {
@@ -446,11 +451,11 @@ CertificateChainEditor::add_certificate ()
        if (d->ShowModal() == wxID_OK) {
                try {
                        dcp::Certificate c;
-                       std::string extra;
+                       string extra;
                        try {
                                extra = c.read_string (dcp::file_to_string (wx_to_std (d->GetPath ())));
                        } catch (boost::filesystem::filesystem_error& e) {
-                               error_dialog (this, wxString::Format (_("Could not import certificate (%s)"), d->GetPath().data()));
+                               error_dialog (this, _("Could not import certificate (%s)"), d->GetPath());
                                d->Destroy ();
                                return;
                        }
@@ -476,7 +481,7 @@ CertificateChainEditor::add_certificate ()
                                update_certificate_list ();
                        }
                } catch (dcp::MiscError& e) {
-                       error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), e.what ()));
+                       error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
                }
        }
 
@@ -521,12 +526,13 @@ CertificateChainEditor::export_certificate ()
        }
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
+               boost::filesystem::path path (wx_to_std(d->GetPath()));
+               FILE* f = fopen_boost (path, "w");
                if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       throw OpenFileError (path, errno, false);
                }
 
-               std::string const s = j->certificate (true);
+               string const s = j->certificate (true);
                fwrite (s.c_str(), 1, s.length(), f);
                fclose (f);
        }
@@ -570,13 +576,13 @@ CertificateChainEditor::update_certificate_list ()
 void
 CertificateChainEditor::remake_certificates ()
 {
-       boost::shared_ptr<const dcp::CertificateChain> chain = _get();
+       shared_ptr<const dcp::CertificateChain> chain = _get();
 
-       std::string subject_organization_name;
-       std::string subject_organizational_unit_name;
-       std::string root_common_name;
-       std::string intermediate_common_name;
-       std::string leaf_common_name;
+       string subject_organization_name;
+       string subject_organizational_unit_name;
+       string root_common_name;
+       string intermediate_common_name;
+       string leaf_common_name;
 
        dcp::CertificateChain::List all = chain->root_to_leaf ();
 
@@ -599,7 +605,10 @@ CertificateChainEditor::remake_certificates ()
                intermediate_common_name = i->subject_common_name ();
        }
 
-       _nag_remake ();
+       if (_nag_remake()) {
+               /* Cancel was clicked */
+               return;
+       }
 
        MakeChainDialog* d = new MakeChainDialog (
                this,
@@ -667,7 +676,7 @@ CertificateChainEditor::import_private_key ()
                        _set (chain);
                        update_private_key ();
                } catch (dcp::MiscError& e) {
-                       error_dialog (this, wxString::Format (_("Could not read certificate file (%s)"), e.what ()));
+                       error_dialog (this, _("Could not read certificate file."), std_to_wx(e.what()));
                }
        }
 
@@ -679,7 +688,7 @@ CertificateChainEditor::import_private_key ()
 void
 CertificateChainEditor::export_private_key ()
 {
-       boost::optional<std::string> key = _get()->key();
+       optional<string> key = _get()->key();
        if (!key) {
                return;
        }
@@ -690,12 +699,13 @@ CertificateChainEditor::export_private_key ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
+               boost::filesystem::path path (wx_to_std(d->GetPath()));
+               FILE* f = fopen_boost (path, "w");
                if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       throw OpenFileError (path, errno, false);
                }
 
-               std::string const s = _get()->key().get ();
+               string const s = _get()->key().get ();
                fwrite (s.c_str(), 1, s.length(), f);
                fclose (f);
        }
@@ -785,9 +795,10 @@ KeysPage::export_decryption_chain_and_key ()
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "dom"), "w");
+               boost::filesystem::path path (wx_to_std(d->GetPath()));
+               FILE* f = fopen_boost (path, "w");
                if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       throw OpenFileError (path, errno, false);
                }
 
                string const chain = Config::instance()->decryption_chain()->chain();
@@ -827,7 +838,6 @@ KeysPage::import_decryption_chain_and_key ()
                                new_chain->add (dcp::Certificate (current));
                                current = "";
                        } else if (strncmp (buffer, "-----END RSA PRIVATE KEY-----", 29) == 0) {
-                               std::cout << "the key is " << current << "\n";
                                new_chain->set_key (current);
                                current = "";
                        }
@@ -843,13 +853,14 @@ KeysPage::import_decryption_chain_and_key ()
        d->Destroy ();
 }
 
-void
+bool
 KeysPage::nag_remake_decryption_chain ()
 {
-       NagDialog::maybe_nag (
+       return NagDialog::maybe_nag (
                _panel,
                Config::NAG_REMAKE_DECRYPTION_CHAIN,
-               _("If you continue with this operation you will no longer be able to use any DKDMs that you have created.  Also, any KDMs that have been sent to you will become useless.  Proceed with caution!")
+               _("If you continue with this operation you will no longer be able to use any DKDMs that you have created.  Also, any KDMs that have been sent to you will become useless.  Proceed with caution!"),
+               true
                );
 }
 
@@ -857,17 +868,18 @@ void
 KeysPage::export_decryption_chain ()
 {
        wxFileDialog* d = new wxFileDialog (
-               _panel, _("Select Chain File"), wxEmptyString, wxEmptyString, wxT ("PEM files (*.pem)|*.pem"),
+               _panel, _("Select Chain File"), wxEmptyString, _("dcpomatic_kdm_decryption_chain.pem"), wxT ("PEM files (*.pem)|*.pem"),
                wxFD_SAVE | wxFD_OVERWRITE_PROMPT
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
+               boost::filesystem::path path (wx_to_std(d->GetPath()));
+               FILE* f = fopen_boost (path, "w");
                if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       throw OpenFileError (path, errno, false);
                }
 
-               std::string const s = Config::instance()->decryption_chain()->chain();
+               string const s = Config::instance()->decryption_chain()->chain();
                fwrite (s.c_str(), 1, s.length(), f);
                fclose (f);
        }
@@ -878,19 +890,21 @@ void
 KeysPage::export_decryption_certificate ()
 {
        wxFileDialog* d = new wxFileDialog (
-               _panel, _("Select Certificate File"), wxEmptyString, wxEmptyString, wxT ("PEM files (*.pem)|*.pem"),
+               _panel, _("Select Certificate File"), wxEmptyString, _("dcpomatic_kdm_decryption_cert.pem"), wxT ("PEM files (*.pem)|*.pem"),
                wxFD_SAVE | wxFD_OVERWRITE_PROMPT
                );
 
        if (d->ShowModal () == wxID_OK) {
-               FILE* f = fopen_boost (path_from_file_dialog (d, "pem"), "w");
+               boost::filesystem::path path (wx_to_std(d->GetPath()));
+               FILE* f = fopen_boost (path, "w");
                if (!f) {
-                       throw OpenFileError (wx_to_std (d->GetPath ()), errno, false);
+                       throw OpenFileError (path, errno, false);
                }
 
-               std::string const s = Config::instance()->decryption_chain()->leaf().certificate (true);
+               string const s = Config::instance()->decryption_chain()->leaf().certificate (true);
                fwrite (s.c_str(), 1, s.length(), f);
                fclose (f);
        }
+
        d->Destroy ();
 }