bump product version to 4.1.6.2
[LibreOffice.git] / cui / source / options / certpath.cxx
blobe3ebcbe0375bce93a81fcb41fd5f09fd4c713382
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 <svtools/stdctrl.hxx>
14 #include "svtools/treelistentry.hxx"
15 #include <unotools/securityoptions.hxx>
16 #include <cuires.hrc>
17 #include "certpath.hxx"
18 #include "certpath.hrc"
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, CUI_RES( RID_SVXDLG_CERTPATH ) )
30 , m_aCertPathFL ( this, CUI_RES( FL_CERTPATH ) )
31 , m_aCertPathFT ( this, CUI_RES( FT_CERTPATH ) )
32 , m_aCertPathListContainer( this, CUI_RES( LB_CERTPATH ) )
33 , m_aCertPathList( m_aCertPathListContainer, WB_BORDER )
34 , m_aAddBtn ( this, CUI_RES( PB_ADD ) )
35 , m_aButtonsFL ( this, CUI_RES( FL_BUTTONS ) )
36 , m_aOKBtn ( this, CUI_RES( PB_OK ) )
37 , m_aCancelBtn ( this, CUI_RES( PB_CANCEL ) )
38 , m_aHelpBtn ( this, CUI_RES( PB_HELP ) )
39 , m_sAddDialogText(CUI_RESSTR(STR_ADDDLGTEXT))
40 , m_sManual(CUI_RESSTR(STR_MANUAL))
42 static long aStaticTabs[]=
44 3, 0, 15, 75
47 m_aCertPathList.SvxSimpleTable::SetTabs( aStaticTabs );
49 OUString sProfile(CUI_RESSTR(STR_PROFILE));
50 OUString sDirectory(CUI_RESSTR(STR_DIRECTORY));
52 OUStringBuffer sHeader;
53 sHeader.append('\t').append(sProfile).append('\t').append(sDirectory);
54 m_aCertPathList.InsertHeaderEntry( sHeader.makeStringAndClear(), HEADERBAR_APPEND, HIB_LEFT );
55 m_aCertPathList.SetCheckButtonHdl( LINK( this, CertPathDialog, CheckHdl_Impl ) );
57 m_aAddBtn.SetClickHdl( LINK( this, CertPathDialog, AddHdl_Impl ) );
58 m_aOKBtn.SetClickHdl( LINK( this, CertPathDialog, OKHdl_Impl ) );
60 FreeResource();
62 try
64 mozilla::MozillaProductType productTypes[3] = {
65 mozilla::MozillaProductType_Thunderbird,
66 mozilla::MozillaProductType_Firefox,
67 mozilla::MozillaProductType_Mozilla };
68 const char* productNames[3] = {
69 "thunderbird",
70 "firefox",
71 "mozilla" };
72 sal_Int32 nProduct = SAL_N_ELEMENTS(productTypes);
74 uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap = mozilla::MozillaBootstrap::create( comphelper::getProcessComponentContext() );
76 for (sal_Int32 i = 0; i < nProduct; ++i)
78 OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);
80 if (!profile.isEmpty())
82 OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
83 OUStringBuffer sEntry;
84 sEntry.append('\t').appendAscii(productNames[i]).append(':').append(profile).append('\t').append(sProfilePath);
85 SvTreeListEntry *pEntry = m_aCertPathList.InsertEntry(sEntry.makeStringAndClear());
86 OUString* pCertPath = new OUString(sProfilePath);
87 pEntry->SetUserData(pCertPath);
91 catch (const uno::Exception&)
95 SvTreeListEntry *pEntry = m_aCertPathList.GetEntry(0);
96 if (pEntry)
98 m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
99 HandleCheckEntry(pEntry);
104 OUString sUserSetCertPath =
105 officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());
107 if (!sUserSetCertPath.isEmpty())
108 AddCertPath(m_sManual, sUserSetCertPath);
110 catch (const uno::Exception &e)
112 SAL_WARN("cui.options", "CertPathDialog::CertPathDialog(): caught exception" << e.Message);
115 const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
116 if (pEnv)
117 AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
120 IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl)
122 fprintf(stderr, "dir is %s\n", OUStringToOString(getDirectory(), RTL_TEXTENCODING_UTF8).getStr());
126 boost::shared_ptr< comphelper::ConfigurationChanges > batch(
127 comphelper::ConfigurationChanges::create());
128 officecfg::Office::Common::Security::Scripting::CertDir::set(
129 getDirectory(), batch);
130 batch->commit();
132 catch (const uno::Exception &e)
134 SAL_WARN("cui.options", "CertPathDialog::OKHdl_Impl(): caught exception" << e.Message);
137 EndDialog(true);
139 return 0;
142 OUString CertPathDialog::getDirectory() const
144 SvTreeListEntry* pEntry = m_aCertPathList.FirstSelected();
145 void* pCertPath = pEntry ? pEntry->GetUserData() : NULL;
146 return pCertPath ? *static_cast<OUString*>(pCertPath) : OUString();
149 CertPathDialog::~CertPathDialog()
151 SvTreeListEntry* pEntry = m_aCertPathList.First();
152 while (pEntry)
154 OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
155 delete pCertPath;
156 pEntry = m_aCertPathList.Next( pEntry );
160 IMPL_LINK( CertPathDialog, CheckHdl_Impl, SvxSimpleTable *, pList )
162 SvTreeListEntry* pEntry = pList ? m_aCertPathList.GetEntry(m_aCertPathList.GetCurMousePoint())
163 : m_aCertPathList.FirstSelected();
164 if (pEntry)
165 m_aCertPathList.HandleEntryChecked(pEntry);
166 return 0;
169 void CertPathDialog::HandleCheckEntry( SvTreeListEntry* _pEntry )
171 m_aCertPathList.Select( _pEntry, true );
172 SvButtonState eState = m_aCertPathList.GetCheckButtonState( _pEntry );
174 if (SV_BUTTON_CHECKED == eState)
176 // uncheck the other entries
177 SvTreeListEntry* pEntry = m_aCertPathList.First();
178 while (pEntry)
180 if (pEntry != _pEntry)
181 m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_UNCHECKED);
182 pEntry = m_aCertPathList.Next(pEntry);
185 else
186 m_aCertPathList.SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
189 void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
191 SvTreeListEntry* pEntry = m_aCertPathList.First();
192 while (pEntry)
194 OUString* pCertPath = static_cast<OUString*>(pEntry->GetUserData());
195 //already exists, just select the original one
196 if (pCertPath->equals(rPath))
198 m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
199 HandleCheckEntry(pEntry);
200 return;
202 pEntry = m_aCertPathList.Next(pEntry);
205 OUStringBuffer sEntry;
206 sEntry.append('\t').append(rProfile).append('\t').append(rPath);
207 pEntry = m_aCertPathList.InsertEntry(sEntry.makeStringAndClear());
208 OUString* pCertPath = new OUString(rPath);
209 pEntry->SetUserData(pCertPath);
210 m_aCertPathList.SetCheckButtonState(pEntry, SV_BUTTON_CHECKED);
211 HandleCheckEntry(pEntry);
214 IMPL_LINK_NOARG(CertPathDialog, AddHdl_Impl)
218 uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
220 OUString sURL;
221 osl::Security().getHomeDir(sURL);
222 xFolderPicker->setDisplayDirectory(sURL);
223 xFolderPicker->setDescription(m_sAddDialogText);
225 if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
227 sURL = xFolderPicker->getDirectory();
228 OUString aPath;
229 if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
230 AddCertPath(m_sManual, aPath);
233 catch (uno::Exception & e)
235 SAL_WARN("cui.options", "caught UNO exception: " << e.Message);
238 return 0;
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */