1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * Major Contributor(s):
16 * Copyright (C) 2012 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
19 * All Rights Reserved.
21 * For minor contributions see the git repository.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
30 #include <officecfg/Office/Common.hxx>
31 #include <osl/file.hxx>
32 #include <osl/security.hxx>
33 #include <svtools/stdctrl.hxx>
34 #include "svtools/treelistentry.hxx"
35 #include <unotools/securityoptions.hxx>
37 #include "certpath.hxx"
38 #include "certpath.hrc"
39 #include "dialmgr.hxx"
41 #include <com/sun/star/mozilla/XMozillaBootstrap.hpp>
42 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
43 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
44 #include <comphelper/processfactory.hxx>
46 using namespace ::com::sun::star
;
48 CertPathDialog::CertPathDialog( Window
* pParent
) :
49 ModalDialog( pParent
, CUI_RES( RID_SVXDLG_CERTPATH
) )
50 , m_aCertPathFL ( this, CUI_RES( FL_CERTPATH
) )
51 , m_aCertPathFT ( this, CUI_RES( FT_CERTPATH
) )
52 , m_aCertPathListContainer( this, CUI_RES( LB_CERTPATH
) )
53 , m_aCertPathList( m_aCertPathListContainer
)
54 , m_aAddBtn ( this, CUI_RES( PB_ADD
) )
55 , m_aButtonsFL ( this, CUI_RES( FL_BUTTONS
) )
56 , m_aOKBtn ( this, CUI_RES( PB_OK
) )
57 , m_aCancelBtn ( this, CUI_RES( PB_CANCEL
) )
58 , m_aHelpBtn ( this, CUI_RES( PB_HELP
) )
59 , m_sAddDialogText(CUI_RESSTR(STR_ADDDLGTEXT
))
60 , m_sManual(CUI_RESSTR(STR_MANUAL
))
62 static long aStaticTabs
[]=
67 m_aCertPathList
.SvxSimpleTable::SetTabs( aStaticTabs
);
69 rtl::OUString
sProfile(CUI_RESSTR(STR_PROFILE
));
70 rtl::OUString
sDirectory(CUI_RESSTR(STR_DIRECTORY
));
72 rtl::OUStringBuffer sHeader
;
73 sHeader
.append('\t').append(sProfile
).append('\t').append(sDirectory
);
74 m_aCertPathList
.InsertHeaderEntry( sHeader
.makeStringAndClear(), HEADERBAR_APPEND
, HIB_LEFT
);
75 m_aCertPathList
.SetCheckButtonHdl( LINK( this, CertPathDialog
, CheckHdl_Impl
) );
77 m_aAddBtn
.SetClickHdl( LINK( this, CertPathDialog
, AddHdl_Impl
) );
78 m_aOKBtn
.SetClickHdl( LINK( this, CertPathDialog
, OKHdl_Impl
) );
84 mozilla::MozillaProductType productTypes
[3] = {
85 mozilla::MozillaProductType_Thunderbird
,
86 mozilla::MozillaProductType_Firefox
,
87 mozilla::MozillaProductType_Mozilla
};
88 const char* productNames
[3] = {
92 sal_Int32 nProduct
= SAL_N_ELEMENTS(productTypes
);
94 uno::Reference
<uno::XInterface
> xInstance
= comphelper::getProcessServiceFactory()->createInstance(
95 "com.sun.star.mozilla.MozillaBootstrap");
97 uno::Reference
<mozilla::XMozillaBootstrap
> xMozillaBootstrap(xInstance
, uno::UNO_QUERY_THROW
);
99 for (sal_Int32 i
= 0; i
< nProduct
; ++i
)
101 ::rtl::OUString profile
= xMozillaBootstrap
->getDefaultProfile(productTypes
[i
]);
103 if (!profile
.isEmpty())
105 ::rtl::OUString sProfilePath
= xMozillaBootstrap
->getProfilePath( productTypes
[i
], profile
);
106 rtl::OUStringBuffer sEntry
;
107 sEntry
.append('\t').appendAscii(productNames
[i
]).append(':').append(profile
).append('\t').append(sProfilePath
);
108 SvTreeListEntry
*pEntry
= m_aCertPathList
.InsertEntry(sEntry
.makeStringAndClear());
109 rtl::OUString
* pCertPath
= new rtl::OUString(sProfilePath
);
110 pEntry
->SetUserData(pCertPath
);
114 catch (const uno::Exception
&)
118 SvTreeListEntry
*pEntry
= m_aCertPathList
.GetEntry(0);
121 m_aCertPathList
.SetCheckButtonState(pEntry
, SV_BUTTON_CHECKED
);
122 HandleCheckEntry(pEntry
);
127 rtl::OUString sUserSetCertPath
=
128 officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(rtl::OUString());
130 if (!sUserSetCertPath
.isEmpty())
131 AddCertPath(m_sManual
, sUserSetCertPath
);
133 catch (const uno::Exception
&e
)
135 SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e
.Message
);
138 const char* pEnv
= getenv("MOZILLA_CERTIFICATE_FOLDER");
140 AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", rtl::OUString(pEnv
, strlen(pEnv
), osl_getThreadTextEncoding()));
143 IMPL_LINK_NOARG(CertPathDialog
, OKHdl_Impl
)
145 fprintf(stderr
, "dir is %s\n", rtl::OUStringToOString(getDirectory(), RTL_TEXTENCODING_UTF8
).getStr());
149 boost::shared_ptr
< comphelper::ConfigurationChanges
> batch(
150 comphelper::ConfigurationChanges::create());
151 officecfg::Office::Common::Security::Scripting::CertDir::set(
152 getDirectory(), batch
);
155 catch (const uno::Exception
&e
)
157 SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e
.Message
);
165 rtl::OUString
CertPathDialog::getDirectory() const
167 SvTreeListEntry
* pEntry
= m_aCertPathList
.FirstSelected();
168 void* pCertPath
= pEntry
? pEntry
->GetUserData() : NULL
;
169 return pCertPath
? *static_cast<rtl::OUString
*>(pCertPath
) : rtl::OUString();
172 CertPathDialog::~CertPathDialog()
174 SvTreeListEntry
* pEntry
= m_aCertPathList
.First();
177 rtl::OUString
* pCertPath
= static_cast<rtl::OUString
*>(pEntry
->GetUserData());
179 pEntry
= m_aCertPathList
.Next( pEntry
);
183 IMPL_LINK( CertPathDialog
, CheckHdl_Impl
, SvxSimpleTable
*, pList
)
185 SvTreeListEntry
* pEntry
= pList
? m_aCertPathList
.GetEntry(m_aCertPathList
.GetCurMousePoint())
186 : m_aCertPathList
.FirstSelected();
188 m_aCertPathList
.HandleEntryChecked(pEntry
);
192 void CertPathDialog::HandleCheckEntry( SvTreeListEntry
* _pEntry
)
194 m_aCertPathList
.Select( _pEntry
, true );
195 SvButtonState eState
= m_aCertPathList
.GetCheckButtonState( _pEntry
);
197 if (SV_BUTTON_CHECKED
== eState
)
199 // uncheck the other entries
200 SvTreeListEntry
* pEntry
= m_aCertPathList
.First();
203 if (pEntry
!= _pEntry
)
204 m_aCertPathList
.SetCheckButtonState(pEntry
, SV_BUTTON_UNCHECKED
);
205 pEntry
= m_aCertPathList
.Next(pEntry
);
209 m_aCertPathList
.SetCheckButtonState(_pEntry
, SV_BUTTON_CHECKED
);
212 void CertPathDialog::AddCertPath(const rtl::OUString
&rProfile
, const rtl::OUString
&rPath
)
214 SvTreeListEntry
* pEntry
= m_aCertPathList
.First();
217 rtl::OUString
* pCertPath
= static_cast<rtl::OUString
*>(pEntry
->GetUserData());
218 //already exists, just select the original one
219 if (pCertPath
->equals(rPath
))
221 m_aCertPathList
.SetCheckButtonState(pEntry
, SV_BUTTON_CHECKED
);
222 HandleCheckEntry(pEntry
);
225 pEntry
= m_aCertPathList
.Next(pEntry
);
228 rtl::OUStringBuffer sEntry
;
229 sEntry
.append('\t').append(rProfile
).append('\t').append(rPath
);
230 pEntry
= m_aCertPathList
.InsertEntry(sEntry
.makeStringAndClear());
231 rtl::OUString
* pCertPath
= new rtl::OUString(rPath
);
232 pEntry
->SetUserData(pCertPath
);
233 m_aCertPathList
.SetCheckButtonState(pEntry
, SV_BUTTON_CHECKED
);
234 HandleCheckEntry(pEntry
);
237 IMPL_LINK_NOARG(CertPathDialog
, AddHdl_Impl
)
241 uno::Reference
<ui::dialogs::XFolderPicker2
> xFolderPicker
= ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
244 osl::Security().getHomeDir(sURL
);
245 xFolderPicker
->setDisplayDirectory(sURL
);
246 xFolderPicker
->setDescription(m_sAddDialogText
);
248 if (xFolderPicker
->execute() == ui::dialogs::ExecutableDialogResults::OK
)
250 sURL
= xFolderPicker
->getDirectory();
252 if (osl::FileBase::E_None
== osl::FileBase::getSystemPathFromFileURL(sURL
, aPath
))
253 AddCertPath(m_sManual
, aPath
);
256 catch (uno::Exception
& e
)
258 SAL_WARN("cui.options", "caught UNO exception: " << e
.Message
);
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */