tdf#127909 save manually selected certificate path

Saves the value of the manual selection, independent from the real
active certificate path, to restore the setting for the dialog.

Change-Id: I5c423c594f38b1e2b25caa650b3ca2862cf49d82
Reviewed-on: https://gerrit.libreoffice.org/79979
Tested-by: Jenkins
Reviewed-by: Jan-Marek Glogowski <glogow@fbihome.de>
diff --git a/cui/source/options/certpath.cxx b/cui/source/options/certpath.cxx
index 0d8366f..6f52fcb 100644
--- a/cui/source/options/certpath.cxx
+++ b/cui/source/options/certpath.cxx
@@ -88,27 +88,9 @@ CertPathDialog::CertPathDialog(weld::Window* pParent)

    try
    {
        OUString sUserSetCertPath =
            officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());

        if (!sUserSetCertPath.isEmpty())
        {
            // check existence
            ::osl::DirectoryItem aUserPathItem;
            OUString sUserSetCertURLPath;
            osl::FileBase::getFileURLFromSystemPath(sUserSetCertPath, sUserSetCertURLPath);
            ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sUserSetCertURLPath, aUserPathItem );
            if ( result == ::osl::FileBase::E_None  )
            {
                ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
                result = aUserPathItem.getFileStatus( aStatus );
                if ( result == ::osl::FileBase::E_None  )
                {
                    // the cert path exists
                    AddCertPath(m_sManualLabel, sUserSetCertPath);
                }
            }
        }
        AddManualCertPath(officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString()));
        if (m_sManualPath.isEmpty())
            AddManualCertPath(officecfg::Office::Common::Security::Scripting::ManualCertDir::get(), false);
    }
    catch (const uno::Exception &)
    {
@@ -120,6 +102,24 @@ CertPathDialog::CertPathDialog(weld::Window* pParent)
        AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
}

void CertPathDialog::AddManualCertPath(const OUString& sUserSetCertPath, bool bSelect)
{
    if (sUserSetCertPath.isEmpty())
        return;

    // check existence
    ::osl::DirectoryItem aUserPathItem;
    OUString sUserSetCertURLPath;
    osl::FileBase::getFileURLFromSystemPath(sUserSetCertPath, sUserSetCertURLPath);
    if (::osl::FileBase::E_None == ::osl::DirectoryItem::get(sUserSetCertURLPath, aUserPathItem))
    {
        ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
        if (::osl::FileBase::E_None == aUserPathItem.getFileStatus(aStatus))
            // the cert path exists
            AddCertPath(m_sManualLabel, sUserSetCertPath, bSelect);
    }
}

IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl, weld::Button&, void)
{
    try
@@ -128,6 +128,7 @@ IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl, weld::Button&, void)
            comphelper::ConfigurationChanges::create());
        officecfg::Office::Common::Security::Scripting::CertDir::set(
            getDirectory(), batch);
        officecfg::Office::Common::Security::Scripting::ManualCertDir::set(m_sManualPath, batch);
        batch->commit();
    }
    catch (const uno::Exception &)
@@ -173,6 +174,7 @@ void CertPathDialog::HandleEntryChecked(int nRow)

void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath, const bool bSelect)
{
    int nRow = -1;
    for (int i = 0, nCount = m_xCertPathList->n_children(); i < nCount; ++i)
    {
        OUString sCertPath = m_xCertPathList->get_id(i);
@@ -184,10 +186,18 @@ void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath
            HandleEntryChecked(i);
            return;
        }
        else if (m_xCertPathList->get_text(i, 1) == rProfile)
            nRow = i;
    }

    m_xCertPathList->append();
    const int nRow = m_xCertPathList->n_children() - 1;
    if (m_sManualLabel == rProfile)
        m_sManualPath = rPath;

    if (nRow < 0)
    {
        m_xCertPathList->append();
        nRow = m_xCertPathList->n_children() - 1;
    }
    m_xCertPathList->set_toggle(nRow, bSelect ? TRISTATE_TRUE : TRISTATE_FALSE, 0);
    m_xCertPathList->set_text(nRow, rProfile, 1);
    m_xCertPathList->set_text(nRow, rPath, 2);
@@ -202,7 +212,10 @@ IMPL_LINK_NOARG(CertPathDialog, ManualHdl_Impl, weld::Button&, void)
        uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());

        OUString sURL;
        osl::Security().getHomeDir(sURL);
        if (!m_sManualPath.isEmpty())
            osl::FileBase::getFileURLFromSystemPath(m_sManualPath, sURL);
        if (sURL.isEmpty())
            osl::Security().getHomeDir(sURL);
        xFolderPicker->setDisplayDirectory(sURL);
        xFolderPicker->setDescription(m_sAddDialogText);

diff --git a/cui/source/options/certpath.hxx b/cui/source/options/certpath.hxx
index 70bf5e2..c23812e 100644
--- a/cui/source/options/certpath.hxx
+++ b/cui/source/options/certpath.hxx
@@ -19,6 +19,7 @@ class CertPathDialog : public weld::GenericDialogController
    std::unique_ptr<weld::TreeView> m_xCertPathList;
    OUString m_sAddDialogText;
    OUString m_sManualLabel;
    OUString m_sManualPath;

    typedef std::pair<int, int> row_col;
    DECL_LINK(CheckHdl_Impl, const row_col&, void);
@@ -27,6 +28,7 @@ class CertPathDialog : public weld::GenericDialogController

    void HandleEntryChecked(int nRow);
    void AddCertPath(const OUString &rProfile, const OUString &rPath, bool bSelect = true);
    void AddManualCertPath(const OUString& sUserSetCertPath, bool bSelect = true);

public:
    explicit CertPathDialog(weld::Window* pParent);
diff --git a/officecfg/registry/schema/org/openoffice/Office/Common.xcs b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
index 0c6b90f..5eeb6c5 100644
--- a/officecfg/registry/schema/org/openoffice/Office/Common.xcs
+++ b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
@@ -2640,6 +2640,11 @@
            <desc>Contains the path to the users NSS certificate directory.</desc>
          </info>
        </prop>
        <prop oor:name="ManualCertDir" oor:type="xs:string" oor:nillable="false">
          <info>
            <desc>Contains the last path manually selected by the user for the CertDir property.</desc>
          </info>
        </prop>
        <prop oor:name="TSAURLs" oor:type="oor:string-list">
          <info>
            <desc>Contains the URLs or Time Stamping Authority servers.</desc>