bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / options / certpath.cxx
blob10c2ebc1bf90896104258ecf06dc9c92a78dd6f3
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 <osl/thread.h>
14 #include <svtools/stdctrl.hxx>
15 #include "svtools/treelistentry.hxx"
16 #include <unotools/securityoptions.hxx>
17 #include <cuires.hrc>
18 #include "certpath.hxx"
19 #include "dialmgr.hxx"
21 #include <com/sun/star/mozilla/MozillaBootstrap.hpp>
22 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
23 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
24 #include <comphelper/processfactory.hxx>
26 using namespace ::com::sun::star;
28 CertPathDialog::CertPathDialog(vcl::Window* pParent)
29 : ModalDialog(pParent, "CertDialog", "cui/ui/certdialog.ui")
31 get(m_pOKBtn, "ok");
32 get(m_pAddBtn, "add");
33 get(m_pCertPathListContainer, "paths");
34 Size aSize(LogicToPixel(Size(210, 60), MAP_APPFONT));
35 m_pCertPathListContainer->set_width_request(aSize.Width());
36 m_pCertPathListContainer->set_height_request(aSize.Height());
37 m_pCertPathList =
38 VclPtr<svx::SvxRadioButtonListBox>::Create(*m_pCertPathListContainer, 0);
39 m_sAddDialogText = get<FixedText>("certdir")->GetText();
40 m_sManual = get<FixedText>("manual")->GetText();
42 static long aStaticTabs[]=
44 3, 0, 15, 75
47 m_pCertPathList->SvSimpleTable::SetTabs( aStaticTabs );
49 OUString sProfile(get<FixedText>("profile")->GetText());
50 OUString sDirectory(get<FixedText>("dir")->GetText());
52 OUStringBuffer sHeader;
53 sHeader.append('\t').append(sProfile).append('\t').append(sDirectory);
54 m_pCertPathList->InsertHeaderEntry( sHeader.makeStringAndClear(), HEADERBAR_APPEND, HeaderBarItemBits::LEFT );
55 m_pCertPathList->SetCheckButtonHdl( LINK( this, CertPathDialog, CheckHdl_Impl ) );
57 m_pAddBtn->SetClickHdl( LINK( this, CertPathDialog, AddHdl_Impl ) );
58 m_pOKBtn->SetClickHdl( LINK( this, CertPathDialog, OKHdl_Impl ) );
60 try
62 mozilla::MozillaProductType productTypes[3] = {
63 mozilla::MozillaProductType_Thunderbird,
64 mozilla::MozillaProductType_Firefox,
65 mozilla::MozillaProductType_Mozilla };
66 const char* productNames[3] = {
67 "thunderbird",
68 "firefox",
69 "mozilla" };
70 sal_Int32 nProduct = SAL_N_ELEMENTS(productTypes);
72 uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap = mozilla::MozillaBootstrap::create( comphelper::getProcessComponentContext() );
74 for (sal_Int32 i = 0; i < nProduct; ++i)
76 OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);
78 if (!profile.isEmpty())
80 OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
81 OUStringBuffer sEntry;
82 sEntry.append('\t').appendAscii(productNames[i]).append(':').append(profile).append('\t').append(sProfilePath);
83 SvTreeListEntry *pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
84 OUString* pCertPath = new OUString(sProfilePath);
85 pEntry->SetUserData(pCertPath);
89 catch (const uno::Exception&)
93 SvTreeListEntry *pEntry = m_pCertPathList->GetEntry(0);
94 if (pEntry)
96 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
97 HandleCheckEntry(pEntry);
102 OUString sUserSetCertPath =
103 officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());
105 if (!sUserSetCertPath.isEmpty())
106 AddCertPath(m_sManual, sUserSetCertPath);
108 catch (const uno::Exception &e)
110 SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e.Message);
113 const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
114 if (pEnv)
115 AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
118 IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl)
122 std::shared_ptr< comphelper::ConfigurationChanges > batch(
123 comphelper::ConfigurationChanges::create());
124 officecfg::Office::Common::Security::Scripting::CertDir::set(
125 getDirectory(), batch);
126 batch->commit();
128 catch (const uno::Exception &e)
130 SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e.Message);
133 EndDialog(RET_OK);
135 return 0;
138 OUString CertPathDialog::getDirectory() const
140 SvTreeListEntry* pEntry = m_pCertPathList->FirstSelected();
141 void* pCertPath = pEntry ? pEntry->GetUserData() : NULL;
142 return pCertPath ? *static_cast<OUString*>(pCertPath) : OUString();
145 CertPathDialog::~CertPathDialog()
147 disposeOnce();
150 void CertPathDialog::dispose()
152 SvTreeListEntry* pEntry = m_pCertPathList->First();
153 while (pEntry)
155 OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
156 delete pCertPath;
157 pEntry = m_pCertPathList->Next( pEntry );
159 m_pCertPathList.disposeAndClear();
160 m_pCertPathListContainer.clear();
161 m_pAddBtn.clear();
162 m_pOKBtn.clear();
163 ModalDialog::dispose();
166 IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvSimpleTable *, pList )
168 SvTreeListEntry* pEntry = pList ? m_pCertPathList->GetEntry(m_pCertPathList->GetCurMousePoint())
169 : m_pCertPathList->FirstSelected();
170 if (pEntry)
171 m_pCertPathList->HandleEntryChecked(pEntry);
172 return 0;
175 void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
177 m_pCertPathList->Select( _pEntry, true );
178 SvButtonState eState = m_pCertPathList->GetCheckButtonState( _pEntry );
180 if (SV_BUTTON_CHECKED == eState)
182 // uncheck the other entries
183 SvTreeListEntry* pEntry = m_pCertPathList->First();
184 while (pEntry)
186 if (pEntry != _pEntry)
187 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_UNCHECKED);
188 pEntry = m_pCertPathList->Next(pEntry);
191 else
192 m_pCertPathList->SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
195 void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
197 SvTreeListEntry* pEntry = m_pCertPathList->First();
198 while (pEntry)
200 OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
201 //already exists, just select the original one
202 if (pCertPath->equals(rPath))
204 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
205 HandleCheckEntry(pEntry);
206 return;
208 pEntry = m_pCertPathList->Next(pEntry);
211 OUStringBuffer sEntry;
212 sEntry.append('\t').append(rProfile).append('\t').append(rPath);
213 pEntry = m_pCertPathList->InsertEntry(sEntry.makeStringAndClear());
214 OUString* pCertPath = new OUString(rPath);
215 pEntry->SetUserData(pCertPath);
216 m_pCertPathList->SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
217 HandleCheckEntry(pEntry);
220 IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl)
224 uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
226 OUString sURL;
227 osl::Security().getHomeDir(sURL);
228 xFolderPicker->setDisplayDirectory(sURL);
229 xFolderPicker->setDescription(m_sAddDialogText);
231 if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
233 sURL = xFolderPicker->getDirectory();
234 OUString aPath;
235 if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
236 AddCertPath(m_sManual, aPath);
239 catch (uno::Exception & e)
241 SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
244 return 0;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */