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 <osl/thread.h>
14 #include <svtools/stdctrl.hxx>
15 #include "svtools/treelistentry.hxx"
16 #include <unotools/securityoptions.hxx>
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(Window
* pParent
)
29 : ModalDialog(pParent
, "CertDialog", "cui/ui/certdialog.ui")
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());
38 new svx::SvxRadioButtonListBox(*m_pCertPathListContainer
, 0);
39 m_sAddDialogText
= get
<FixedText
>("certdir")->GetText();
40 m_sManual
= get
<FixedText
>("manual")->GetText();
42 static long aStaticTabs
[]=
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
, HIB_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
) );
62 mozilla::MozillaProductType productTypes
[3] = {
63 mozilla::MozillaProductType_Thunderbird
,
64 mozilla::MozillaProductType_Firefox
,
65 mozilla::MozillaProductType_Mozilla
};
66 const char* productNames
[3] = {
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);
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");
115 AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv
, strlen(pEnv
), osl_getThreadTextEncoding()));
118 IMPL_LINK_NOARG(CertPathDialog
, OKHdl_Impl
)
122 boost::shared_ptr
< comphelper::ConfigurationChanges
> batch(
123 comphelper::ConfigurationChanges::create());
124 officecfg::Office::Common::Security::Scripting::CertDir::set(
125 getDirectory(), batch
);
128 catch (const uno::Exception
&e
)
130 SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e
.Message
);
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 SvTreeListEntry
* pEntry
= m_pCertPathList
->First();
150 OUString
* pCertPath
= static_cast<OUString
*>(pEntry
->GetUserData());
152 pEntry
= m_pCertPathList
->Next( pEntry
);
154 delete m_pCertPathList
;
157 IMPL_LINK( CertPathDialog
, CheckHdl_Impl
, SvSimpleTable
*, pList
)
159 SvTreeListEntry
* pEntry
= pList
? m_pCertPathList
->GetEntry(m_pCertPathList
->GetCurMousePoint())
160 : m_pCertPathList
->FirstSelected();
162 m_pCertPathList
->HandleEntryChecked(pEntry
);
166 void CertPathDialog::HandleCheckEntry( SvTreeListEntry
* _pEntry
)
168 m_pCertPathList
->Select( _pEntry
, true );
169 SvButtonState eState
= m_pCertPathList
->GetCheckButtonState( _pEntry
);
171 if (SV_BUTTON_CHECKED
== eState
)
173 // uncheck the other entries
174 SvTreeListEntry
* pEntry
= m_pCertPathList
->First();
177 if (pEntry
!= _pEntry
)
178 m_pCertPathList
->SetCheckButtonState(pEntry
, SV_BUTTON_UNCHECKED
);
179 pEntry
= m_pCertPathList
->Next(pEntry
);
183 m_pCertPathList
->SetCheckButtonState(_pEntry
, SV_BUTTON_CHECKED
);
186 void CertPathDialog::AddCertPath(const OUString
&rProfile
, const OUString
&rPath
)
188 SvTreeListEntry
* pEntry
= m_pCertPathList
->First();
191 OUString
* pCertPath
= static_cast<OUString
*>(pEntry
->GetUserData());
192 //already exists, just select the original one
193 if (pCertPath
->equals(rPath
))
195 m_pCertPathList
->SetCheckButtonState(pEntry
, SV_BUTTON_CHECKED
);
196 HandleCheckEntry(pEntry
);
199 pEntry
= m_pCertPathList
->Next(pEntry
);
202 OUStringBuffer sEntry
;
203 sEntry
.append('\t').append(rProfile
).append('\t').append(rPath
);
204 pEntry
= m_pCertPathList
->InsertEntry(sEntry
.makeStringAndClear());
205 OUString
* pCertPath
= new OUString(rPath
);
206 pEntry
->SetUserData(pCertPath
);
207 m_pCertPathList
->SetCheckButtonState(pEntry
, SV_BUTTON_CHECKED
);
208 HandleCheckEntry(pEntry
);
211 IMPL_LINK_NOARG(CertPathDialog
, AddHdl_Impl
)
215 uno::Reference
<ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
218 osl::Security().getHomeDir(sURL
);
219 xFolderPicker
->setDisplayDirectory(sURL
);
220 xFolderPicker
->setDescription(m_sAddDialogText
);
222 if (xFolderPicker
->execute() == ui::dialogs::ExecutableDialogResults::OK
)
224 sURL
= xFolderPicker
->getDirectory();
226 if (osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(sURL
, aPath
))
227 AddCertPath(m_sManual
, aPath
);
230 catch (uno::Exception
& e
)
232 SAL_WARN("cui.options", "caught UNO exception: " << e
.Message
);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */