1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <officecfg/Office/Common.hxx>
11 #include <osl/file.hxx>
12 #include <osl/security.hxx>
13 #include <sfx2/filedlghelper.hxx>
14 #include <comphelper/diagnose_ex.hxx>
15 #include "certpath.hxx"
17 #include <com/sun/star/xml/crypto/NSSInitializer.hpp>
18 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
19 #include <com/sun/star/ui/dialogs/XFolderPicker2.hpp>
20 #include <comphelper/processfactory.hxx>
22 using namespace ::com::sun::star
;
24 CertPathDialog::CertPathDialog(weld::Window
* pParent
)
25 : GenericDialogController(pParent
, "cui/ui/certdialog.ui", "CertDialog")
26 , m_xManualButton(m_xBuilder
->weld_button("add"))
27 , m_xOKButton(m_xBuilder
->weld_button("ok"))
28 , m_xCertPathList(m_xBuilder
->weld_tree_view("paths"))
29 , m_sAddDialogText(m_xBuilder
->weld_label("certdir")->get_label())
30 , m_sManualLabel(m_xBuilder
->weld_label("manual")->get_label())
32 m_xCertPathList
->set_size_request(m_xCertPathList
->get_approximate_digit_width() * 70,
33 m_xCertPathList
->get_height_rows(6));
35 m_xCertPathList
->enable_toggle_buttons(weld::ColumnToggleType::Radio
);
36 m_xCertPathList
->connect_toggled(LINK(this, CertPathDialog
, CheckHdl_Impl
));
38 m_xManualButton
->connect_clicked( LINK( this, CertPathDialog
, ManualHdl_Impl
) );
39 m_xOKButton
->connect_clicked( LINK( this, CertPathDialog
, OKHdl_Impl
) );
42 void CertPathDialog::Init()
44 m_xCertPathList
->clear();
45 m_xCertPathList
->set_sensitive(true);
49 uno::Reference
<uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
50 uno::Reference
<xml::crypto::XNSSInitializer
> xCipherContextSupplier
= xml::crypto::NSSInitializer::create(xContext
);
52 OUString sActivePath
= xCipherContextSupplier
->getNSSPath();
53 auto aProductList
= xCipherContextSupplier
->getNSSProfiles();
55 // these map to the integer values of mozilla::MozillaProductType
56 const char* const productNames
[4] = {
63 for (const auto& rNSSProfile
: std::as_const(aProductList
))
65 if (rNSSProfile
.Type
== mozilla::MozillaProductType_Default
)
67 if (rNSSProfile
.Name
== "MOZILLA_CERTIFICATE_FOLDER" && !rNSSProfile
.Path
.isEmpty())
69 AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", rNSSProfile
.Path
);
70 m_xCertPathList
->set_sensitive(false);
72 else if (rNSSProfile
.Name
== "MANUAL")
73 AddManualCertPath(rNSSProfile
.Path
);
77 OUString sEntry
= OUString::createFromAscii(
78 productNames
[static_cast<int>(rNSSProfile
.Type
)]) + ":" + rNSSProfile
.Name
;
79 AddCertPath(sEntry
, rNSSProfile
.Path
, rNSSProfile
.Path
== sActivePath
);
83 OUString sManualCertPath
= officecfg::Office::Common::Security::Scripting::ManualCertDir::get();
84 if (!sManualCertPath
.isEmpty())
85 AddManualCertPath(sManualCertPath
, false);
87 catch (const uno::Exception
&)
92 void CertPathDialog::AddManualCertPath(const OUString
& sUserSetCertPath
, bool bSelect
)
94 if (sUserSetCertPath
.isEmpty())
97 ::osl::DirectoryItem aUserPathItem
;
98 OUString sUserSetCertURLPath
;
99 osl::FileBase::getFileURLFromSystemPath(sUserSetCertPath
, sUserSetCertURLPath
);
100 if (::osl::FileBase::E_None
== ::osl::DirectoryItem::get(sUserSetCertURLPath
, aUserPathItem
))
102 ::osl::FileStatus
aStatus( osl_FileStatus_Mask_Validate
);
103 if (::osl::FileBase::E_None
== aUserPathItem
.getFileStatus(aStatus
))
104 // the cert path exists
105 AddCertPath(m_sManualLabel
, sUserSetCertPath
, bSelect
);
109 IMPL_LINK_NOARG(CertPathDialog
, OKHdl_Impl
, weld::Button
&, void)
113 std::shared_ptr
< comphelper::ConfigurationChanges
> batch(
114 comphelper::ConfigurationChanges::create());
115 const int nEntry
= m_xCertPathList
->get_selected_index();
116 officecfg::Office::Common::Security::Scripting::CertDir::set(
117 nEntry
== -1 ? OUString() : m_xCertPathList
->get_id(nEntry
), batch
);
118 officecfg::Office::Common::Security::Scripting::ManualCertDir::set(m_sManualPath
, batch
);
121 catch (const uno::Exception
&)
123 TOOLS_WARN_EXCEPTION("cui.options", "CertPathDialog::OKHdl_Impl()");
126 m_xDialog
->response(RET_OK
);
129 bool CertPathDialog::isActiveServicePath() const
131 int nEntry
= m_xCertPathList
->get_selected_index();
137 uno::Reference
<uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
138 uno::Reference
<xml::crypto::XNSSInitializer
> xCipherContextSupplier
= xml::crypto::NSSInitializer::create(xContext
);
140 if (!xCipherContextSupplier
->getIsNSSinitialized())
142 return (xCipherContextSupplier
->getNSSPath() == m_xCertPathList
->get_id(nEntry
));
144 catch (const uno::Exception
&)
150 CertPathDialog::~CertPathDialog()
154 IMPL_LINK(CertPathDialog
, CheckHdl_Impl
, const weld::TreeView::iter_col
&, rRowCol
, void)
156 HandleEntryChecked(m_xCertPathList
->get_iter_index_in_parent(rRowCol
.first
));
159 void CertPathDialog::HandleEntryChecked(int nRow
)
161 const bool bChecked
= m_xCertPathList
->get_toggle(nRow
) == TRISTATE_TRUE
;
164 // we have radio button behavior -> so uncheck the other entries
165 m_xCertPathList
->select(nRow
);
166 const int nCount
= m_xCertPathList
->n_children();
167 for (int i
= 0; i
< nCount
; ++i
)
170 m_xCertPathList
->set_toggle(i
, TRISTATE_FALSE
);
175 void CertPathDialog::AddCertPath(const OUString
&rProfile
, const OUString
&rPath
, const bool bSelect
)
178 for (int i
= 0, nCount
= m_xCertPathList
->n_children(); i
< nCount
; ++i
)
180 OUString sCertPath
= m_xCertPathList
->get_id(i
);
181 //already exists, just select the original one
182 if (sCertPath
== rPath
)
184 const bool bWantSelected
= bSelect
|| m_xCertPathList
->get_toggle(i
);
185 m_xCertPathList
->set_toggle(i
, bWantSelected
? TRISTATE_TRUE
: TRISTATE_FALSE
);
186 HandleEntryChecked(i
);
189 else if (m_xCertPathList
->get_text(i
, 0) == rProfile
)
193 if (m_sManualLabel
== rProfile
)
194 m_sManualPath
= rPath
;
198 m_xCertPathList
->append();
199 nRow
= m_xCertPathList
->n_children() - 1;
201 m_xCertPathList
->set_toggle(nRow
, bSelect
? TRISTATE_TRUE
: TRISTATE_FALSE
);
202 m_xCertPathList
->set_text(nRow
, rProfile
, 0);
203 m_xCertPathList
->set_text(nRow
, rPath
, 1);
204 m_xCertPathList
->set_id(nRow
, rPath
);
205 HandleEntryChecked(nRow
);
208 IMPL_LINK_NOARG(CertPathDialog
, ManualHdl_Impl
, weld::Button
&, void)
212 uno::Reference
<ui::dialogs::XFolderPicker2
> xFolderPicker
= sfx2::createFolderPicker(
213 comphelper::getProcessComponentContext(), m_xDialog
.get());
216 if (!m_sManualPath
.isEmpty())
217 osl::FileBase::getFileURLFromSystemPath(m_sManualPath
, sURL
);
219 osl::Security().getHomeDir(sURL
);
220 xFolderPicker
->setDisplayDirectory(sURL
);
221 xFolderPicker
->setDescription(m_sAddDialogText
);
223 if (xFolderPicker
->execute() == ui::dialogs::ExecutableDialogResults::OK
)
225 sURL
= xFolderPicker
->getDirectory();
227 if (osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(sURL
, aPath
))
228 AddCertPath(m_sManualLabel
, aPath
);
231 catch (const uno::Exception
&)
233 TOOLS_WARN_EXCEPTION("cui.options", "");
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */