LibreOffice Module cui (master)  1
certpath.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #include <officecfg/Office/Common.hxx>
11 #include <osl/file.hxx>
12 #include <osl/security.hxx>
13 #include <osl/thread.h>
14 #include <sal/log.hxx>
15 #include <tools/diagnose_ex.h>
17 #include "certpath.hxx"
18 
19 #include <com/sun/star/mozilla/MozillaBootstrap.hpp>
20 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
21 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
23 
24 using namespace ::com::sun::star;
25 
27  : GenericDialogController(pParent, "cui/ui/certdialog.ui", "CertDialog")
28  , m_xAddBtn(m_xBuilder->weld_button("add"))
29  , m_xOKBtn(m_xBuilder->weld_button("ok"))
30  , m_xCertPathList(m_xBuilder->weld_tree_view("paths"))
31  , m_xAddDialogLabel(m_xBuilder->weld_label("certdir"))
32  , m_xManualLabel(m_xBuilder->weld_label("manual"))
33 {
34  m_sAddDialogText = m_xAddDialogLabel->get_label();
35  m_sManual = m_xManualLabel->get_label();
36 
37  m_xCertPathList->set_size_request(m_xCertPathList->get_approximate_digit_width() * 70,
38  m_xCertPathList->get_height_rows(6));
39 
40  std::vector<int> aWidths;
41  aWidths.push_back(m_xCertPathList->get_checkbox_column_width());
42  aWidths.push_back(m_xCertPathList->get_approximate_digit_width() * 20);
43  m_xCertPathList->set_column_fixed_widths(aWidths);
44 
45  std::vector<int> aRadioColumns;
46  aRadioColumns.push_back(0);
47  m_xCertPathList->set_toggle_columns_as_radio(aRadioColumns);
48 
49  m_xCertPathList->connect_toggled(LINK(this, CertPathDialog, CheckHdl_Impl));
50  m_xAddBtn->connect_clicked( LINK( this, CertPathDialog, AddHdl_Impl ) );
51  m_xOKBtn->connect_clicked( LINK( this, CertPathDialog, OKHdl_Impl ) );
52 
53  try
54  {
55  mozilla::MozillaProductType const productTypes[3] = {
56  mozilla::MozillaProductType_Thunderbird,
57  mozilla::MozillaProductType_Firefox,
58  mozilla::MozillaProductType_Mozilla };
59  const char* const productNames[3] = {
60  "thunderbird",
61  "firefox",
62  "mozilla" };
63 
64  uno::Reference<mozilla::XMozillaBootstrap> xMozillaBootstrap = mozilla::MozillaBootstrap::create( comphelper::getProcessComponentContext() );
65 
66  for (sal_Int32 i = 0; i < sal_Int32(SAL_N_ELEMENTS(productTypes)); ++i)
67  {
68  OUString profile = xMozillaBootstrap->getDefaultProfile(productTypes[i]);
69 
70  if (!profile.isEmpty())
71  {
72  m_xCertPathList->append();
73  const int nRow = m_xCertPathList->n_children() - 1;
74  m_xCertPathList->set_toggle(nRow, TRISTATE_FALSE, 0);
75  OUString sEntry = OUString::createFromAscii(productNames[i]) + ":" + profile;
76  m_xCertPathList->set_text(nRow, sEntry, 1);
77  OUString sProfilePath = xMozillaBootstrap->getProfilePath( productTypes[i], profile );
78  m_xCertPathList->set_text(nRow, sProfilePath, 2);
79  m_xCertPathList->set_id(nRow, sProfilePath);
80  }
81  }
82  }
83  catch (const uno::Exception&)
84  {
85  }
86 
87  try
88  {
89  OUString sUserSetCertPath =
90  officecfg::Office::Common::Security::Scripting::CertDir::get().get_value_or(OUString());
91 
92  if (!sUserSetCertPath.isEmpty())
93  {
94  // check existence
95  ::osl::DirectoryItem aUserPathItem;
96  OUString sUserSetCertURLPath;
97  osl::FileBase::getFileURLFromSystemPath(sUserSetCertPath, sUserSetCertURLPath);
98  ::osl::FileBase::RC result = ::osl::DirectoryItem::get( sUserSetCertURLPath, aUserPathItem );
99  if ( result == ::osl::FileBase::E_None )
100  {
101  ::osl::FileStatus aStatus( osl_FileStatus_Mask_Validate );
102  result = aUserPathItem.getFileStatus( aStatus );
103  if ( result == ::osl::FileBase::E_None )
104  {
105  // the cert path exists
106  AddCertPath(m_sManual, sUserSetCertPath);
107  }
108  }
109  }
110  }
111  catch (const uno::Exception &)
112  {
113  TOOLS_WARN_EXCEPTION("cui.options", "CertPathDialog::CertPathDialog()");
114  }
115 
116  const char* pEnv = getenv("MOZILLA_CERTIFICATE_FOLDER");
117  if (pEnv)
118  AddCertPath("$MOZILLA_CERTIFICATE_FOLDER", OUString(pEnv, strlen(pEnv), osl_getThreadTextEncoding()));
119 
120  if (m_xCertPathList->n_children())
121  {
122  m_xCertPathList->set_toggle(0, TRISTATE_TRUE, 0);
124  }
125 }
126 
128 {
129  try
130  {
131  std::shared_ptr< comphelper::ConfigurationChanges > batch(
133  officecfg::Office::Common::Security::Scripting::CertDir::set(
134  getDirectory(), batch);
135  batch->commit();
136  }
137  catch (const uno::Exception &)
138  {
139  TOOLS_WARN_EXCEPTION("cui.options", "CertPathDialog::OKHdl_Impl()");
140  }
141 
142  m_xDialog->response(RET_OK);
143 }
144 
146 {
147  int nEntry = m_xCertPathList->get_selected_index();
148  if (nEntry == -1)
149  return OUString();
150  return m_xCertPathList->get_id(nEntry);
151 }
152 
154 {
155 }
156 
157 IMPL_LINK(CertPathDialog, CheckHdl_Impl, const row_col&, rRowCol, void)
158 {
159  HandleEntryChecked(rRowCol.first);
160 }
161 
163 {
164  m_xCertPathList->select(nRow);
165  bool bChecked = m_xCertPathList->get_toggle(nRow, 0) == TRISTATE_TRUE;
166  if (bChecked)
167  {
168  // we have radio button behavior -> so uncheck the other entries
169  int nCount = m_xCertPathList->n_children();
170  for (int i = 0; i < nCount; ++i)
171  {
172  if (i != nRow)
173  m_xCertPathList->set_toggle(i, TRISTATE_FALSE, 0);
174  }
175  }
176 }
177 
178 void CertPathDialog::AddCertPath(const OUString &rProfile, const OUString &rPath)
179 {
180  for (int i = 0, nCount = m_xCertPathList->n_children(); i < nCount; ++i)
181  {
182  OUString sCertPath = m_xCertPathList->get_id(i);
183  //already exists, just select the original one
184  if (sCertPath == rPath)
185  {
186  m_xCertPathList->set_toggle(i, TRISTATE_TRUE, 0);
188  return;
189  }
190  }
191 
192  m_xCertPathList->append();
193  const int nRow = m_xCertPathList->n_children() - 1;
194  m_xCertPathList->set_toggle(nRow, TRISTATE_TRUE, 0);
195  m_xCertPathList->set_text(nRow, rProfile, 1);
196  m_xCertPathList->set_text(nRow, rPath, 2);
197  m_xCertPathList->set_id(nRow, rPath);
198  HandleEntryChecked(nRow);
199 }
200 
202 {
203  try
204  {
205  uno::Reference<ui::dialogs::XFolderPicker2> xFolderPicker = ui::dialogs::FolderPicker::create(comphelper::getProcessComponentContext());
206 
207  OUString sURL;
208  osl::Security().getHomeDir(sURL);
209  xFolderPicker->setDisplayDirectory(sURL);
210  xFolderPicker->setDescription(m_sAddDialogText);
211 
212  if (xFolderPicker->execute() == ui::dialogs::ExecutableDialogResults::OK)
213  {
214  sURL = xFolderPicker->getDirectory();
215  OUString aPath;
216  if (osl::FileBase::E_None == osl::FileBase::getSystemPathFromFileURL(sURL, aPath))
217  AddCertPath(m_sManual, aPath);
218  }
219  }
220  catch (const uno::Exception &)
221  {
222  TOOLS_WARN_EXCEPTION("cui.options", "");
223  }
224 }
225 
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void HandleEntryChecked(int nRow)
Definition: certpath.cxx:162
IMPL_LINK(CertPathDialog, CheckHdl_Impl, const row_col &, rRowCol, void)
Definition: certpath.cxx:157
OUString getDirectory() const
Definition: certpath.cxx:145
OUString m_sAddDialogText
Definition: certpath.hxx:23
TRISTATE_TRUE
OUString m_sManual
Definition: certpath.hxx:24
std::unique_ptr< weld::Button > m_xAddBtn
Definition: certpath.hxx:18
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
#define SAL_N_ELEMENTS(arr)
virtual ~CertPathDialog() override
Definition: certpath.cxx:153
#define TOOLS_WARN_EXCEPTION(area, stream)
std::unique_ptr< weld::TreeView > m_xCertPathList
Definition: certpath.hxx:20
MetadataImporterPluginType * result
int i
TRISTATE_FALSE
std::unique_ptr< weld::Label > m_xManualLabel
Definition: certpath.hxx:22
IMPL_LINK_NOARG(CertPathDialog, OKHdl_Impl, weld::Button &, void)
Definition: certpath.cxx:127
std::unique_ptr< weld::Label > m_xAddDialogLabel
Definition: certpath.hxx:21
RET_OK
Reference< XComponentContext > getProcessComponentContext()
std::unique_ptr< weld::Button > m_xOKBtn
Definition: certpath.hxx:19
void AddCertPath(const OUString &rProfile, const OUString &rPath)
Definition: certpath.cxx:178
CertPathDialog(weld::Window *pParent)
Definition: certpath.cxx:26