tdf#144694 In direct SQL dialog, activate options 'Run SQL command
[LibreOffice.git] / cui / source / options / certpath.cxx
blob0a0a10e0ea6f9d39cda2fe17ece4b471814da48c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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, u"cui/ui/certdialog.ui"_ustr, u"CertDialog"_ustr)
26 , m_xManualButton(m_xBuilder->weld_button(u"add"_ustr))
27 , m_xOKButton(m_xBuilder->weld_button(u"ok"_ustr))
28 , m_xCertPathList(m_xBuilder->weld_tree_view(u"paths"_ustr))
29 , m_sAddDialogText(m_xBuilder->weld_label(u"certdir"_ustr)->get_label())
30 , m_sManualLabel(m_xBuilder->weld_label(u"manual"_ustr)->get_label())
32 m_xCertPathList->set_size_request(m_xCertPathList->get_approximate_digit_width() * 140,
33 m_xCertPathList->get_height_rows(6));
35 // needed for VLCPLUGIN != gtk3 (e.g. "gen")
36 int nControlWidth = m_xCertPathList->get_approximate_digit_width() * 40;
37 m_xCertPathList->set_column_fixed_widths({nControlWidth});
39 m_xCertPathList->enable_toggle_buttons(weld::ColumnToggleType::Radio);
40 m_xCertPathList->connect_toggled(LINK(this, CertPathDialog, CheckHdl_Impl));
42 m_xManualButton->connect_clicked( LINK( this, CertPathDialog, ManualHdl_Impl ) );
43 m_xOKButton->connect_clicked( LINK( this, CertPathDialog, OKHdl_Impl ) );
46 void CertPathDialog::Init()
48 m_xCertPathList->clear();
49 m_xCertPathList->set_sensitive(true);
51 try
53 const uno::Reference<uno::XComponentContext>& xContext = comphelper::getProcessComponentContext();
54 uno::Reference<xml::crypto::XNSSInitializer> xCipherContextSupplier = xml::crypto::NSSInitializer::create(xContext);
56 OUString sActivePath = xCipherContextSupplier->getNSSPath();
57 auto aProductList = xCipherContextSupplier->getNSSProfiles();
59 // these map to the integer values of mozilla::MozillaProductType
60 const char* const productNames[4] = {
61 "",
62 "mozilla",
63 "firefox",
64 "thunderbird"
67 for (const auto& rNSSProfile : aProductList)
69 if (rNSSProfile.Type == mozilla::MozillaProductType_Default)
71 if (rNSSProfile.Name == "MOZILLA_CERTIFICATE_FOLDER" && !rNSSProfile.Path.isEmpty())
73 AddCertPath(u"$MOZILLA_CERTIFICATE_FOLDER"_ustr, rNSSProfile.Path);
74 m_xCertPathList->set_sensitive(false);
76 else if (rNSSProfile.Name == "MANUAL")
77 AddManualCertPath(rNSSProfile.Path);
79 else
81 OUString sEntry = OUString::createFromAscii(
82 productNames[static_cast<int>(rNSSProfile.Type)]) + ":" + rNSSProfile.Name;
83 AddCertPath(sEntry, rNSSProfile.Path, rNSSProfile.Path == sActivePath);
87 OUString sManualCertPath = officecfg::Office::Common::Security::Scripting::ManualCertDir::get();
88 if (!sManualCertPath.isEmpty())
89 AddManualCertPath(sManualCertPath, false);
91 catch (const uno::Exception&)
96 void CertPathDialog::AddManualCertPath(const OUString& sUserSetCertPath, bool bSelect)
98 if (sUserSetCertPath.isEmpty())
99 return;
101 ::osl::DirectoryItem aUserPathItem;
102 OUString sUserSetCertURLPath;
103 osl::FileBase::getFileURLFromSystemPath(sUserSetCertPath, sUserSetCertURLPath);
104 if (::osl::FileBase::E_None == ::osl::DirectoryItem::get(sUserSetCertURLPath, aUserPathItem))
106 ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
107 if (::osl::FileBase::E_None == aUserPathItem.getFileStatus(aStatus))
108 // the cert path exists
109 AddCertPath(m_sManualLabel, sUserSetCertPath, bSelect);
113 IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl, weld::Button&, void)
117 std::shared_ptr< comphelper::ConfigurationChanges > batch(
118 comphelper::ConfigurationChanges::create());
119 const int nEntry = m_xCertPathList->get_selected_index();
120 officecfg::Office::Common::Security::Scripting::CertDir::set(
121 nEntry == -1 ? OUString() : m_xCertPathList->get_id(nEntry), batch);
122 officecfg::Office::Common::Security::Scripting::ManualCertDir::set(m_sManualPath, batch);
123 batch->commit();
125 catch (const uno::Exception &)
127 TOOLS_WARN_EXCEPTION("cui.options", "CertPathDialog::OKHdl_Impl()");
130 m_xDialog->response(RET_OK);
133 bool CertPathDialog::isActiveServicePath() const
135 int nEntry = m_xCertPathList->get_selected_index();
136 if (nEntry == -1)
137 return true;
141 const uno::Reference<uno::XComponentContext>& xContext = comphelper::getProcessComponentContext();
142 uno::Reference<xml::crypto::XNSSInitializer> xCipherContextSupplier = xml::crypto::NSSInitializer::create(xContext);
144 if (!xCipherContextSupplier->getIsNSSinitialized())
145 return true;
146 return (xCipherContextSupplier->getNSSPath() == m_xCertPathList->get_id(nEntry));
148 catch (const uno::Exception&)
150 return false;
154 CertPathDialog::~CertPathDialog()
158 IMPL_LINK(CertPathDialog, CheckHdl_Impl, const weld::TreeView::iter_col&, rRowCol, void)
160 HandleEntryChecked(m_xCertPathList->get_iter_index_in_parent(rRowCol.first));
163 void CertPathDialog::HandleEntryChecked(int nRow)
165 const bool bChecked = m_xCertPathList->get_toggle(nRow) == TRISTATE_TRUE;
166 if (bChecked)
168 // we have radio button behavior -> so uncheck the other entries
169 m_xCertPathList->select(nRow);
170 const int nCount = m_xCertPathList->n_children();
171 for (int i = 0; i < nCount; ++i)
173 if (i != nRow)
174 m_xCertPathList->set_toggle(i, TRISTATE_FALSE);
179 void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath, const bool bSelect)
181 int nRow = -1;
182 for (int i = 0, nCount = m_xCertPathList->n_children(); i < nCount; ++i)
184 OUString sCertPath = m_xCertPathList->get_id(i);
185 //already exists, just select the original one
186 if (sCertPath == rPath)
188 const bool bWantSelected = bSelect || m_xCertPathList->get_toggle(i);
189 m_xCertPathList->set_toggle(i, bWantSelected ? TRISTATE_TRUE : TRISTATE_FALSE);
190 HandleEntryChecked(i);
191 return;
193 else if (m_xCertPathList->get_text(i, 0) == rProfile)
194 nRow = i;
197 if (m_sManualLabel == rProfile)
198 m_sManualPath = rPath;
200 if (nRow < 0)
202 m_xCertPathList->append();
203 nRow = m_xCertPathList->n_children() - 1;
205 m_xCertPathList->set_toggle(nRow, bSelect ? TRISTATE_TRUE : TRISTATE_FALSE);
206 m_xCertPathList->set_text(nRow, rProfile, 0);
207 m_xCertPathList->set_text(nRow, rPath, 1);
208 m_xCertPathList->set_id(nRow, rPath);
209 HandleEntryChecked(nRow);
212 IMPL_LINK_NOARG(CertPathDialog, ManualHdl_Impl, weld::Button&, void)
216 uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = sfx2::createFolderPicker(
217 comphelper::getProcessComponentContext(), m_xDialog.get());
219 OUString sURL;
220 if (!m_sManualPath.isEmpty())
221 osl::FileBase::getFileURLFromSystemPath(m_sManualPath, sURL);
222 if (sURL.isEmpty())
223 osl::Security().getHomeDir(sURL);
224 xFolderPicker->setDisplayDirectory(sURL);
225 xFolderPicker->setDescription(m_sAddDialogText);
227 if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
229 sURL = xFolderPicker->getDirectory();
230 OUString aPath;
231 if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
232 AddCertPath(m_sManualLabel, aPath);
235 catch (const uno::Exception &)
237 TOOLS_WARN_EXCEPTION("cui.options", "");
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */