LibreOffice Module cui (master)  1
optgenrl.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <comphelper/string.hxx>
22 
23 #include <config_gpgme.h>
24 #if HAVE_FEATURE_GPGME
25 # include <com/sun/star/xml/crypto/GPGSEInitializer.hpp>
26 # include <com/sun/star/xml/crypto/XXMLSecurityContext.hpp>
27 #endif
28 
30 #include <i18nlangtag/mslangid.hxx>
31 #include <o3tl/safeint.hxx>
32 #include <vcl/svapp.hxx>
33 #include <unotools/saveopt.hxx>
34 #include <svl/intitem.hxx>
35 #include <vcl/settings.hxx>
36 
37 #include <unotools/useroptions.hxx>
38 #include <cuioptgenrl.hxx>
39 #include <svx/svxids.hrc>
40 #include <svx/optgenrl.hxx>
41 
42 using namespace css;
43 
44 namespace
45 {
46 
47 // rows
48 enum RowType
49 {
50  Row_Company,
51  Row_Name,
52  Row_Name_Russian,
53  Row_Name_Eastern,
54  Row_Street,
55  Row_Street_Russian,
56  Row_City,
57  Row_City_US,
58  Row_Country,
59  Row_TitlePos,
60  Row_Phone,
61  Row_FaxMail,
62 
63  nRowCount
64 };
65 
66 // language flags
67 namespace Lang
68 {
69  unsigned const Others = 1;
70  unsigned const Russian = 2;
71  unsigned const Eastern = 4;
72  unsigned const US = 8;
73  unsigned const All = static_cast<unsigned>(-1);
74 }
75 
76 
77 // vRowInfo[] -- rows (text + one or more edit boxes)
78 // The order is the same as in RowType above, which is up to down.
79 
80 struct
81 {
82  // id of the text
83  const char *pTextId;
84  // language flags (see Lang above):
85  // which language is this row for?
86  unsigned nLangFlags;
87 }
88 const vRowInfo[] =
89 {
90  { "companyft", Lang::All },
91  { "nameft", Lang::All & ~Lang::Russian & ~Lang::Eastern },
92  { "rusnameft", Lang::Russian },
93  { "eastnameft", Lang::Eastern },
94  { "streetft", Lang::All & ~Lang::Russian },
95  { "russtreetft", Lang::Russian },
96  { "icityft", Lang::All & ~Lang::US },
97  { "cityft", Lang::US },
98  { "countryft", Lang::All },
99  { "titleft", Lang::All },
100  { "phoneft", Lang::All },
101  { "faxft", Lang::All },
102 };
103 
104 
105 // vFieldInfo[] -- edit boxes
106 // The order is up to down, and then left to right.
107 
108 struct
109 {
110  // in which row?
111  RowType eRow;
112  // id of the edit box
113  const char *pEditId;
114  // id for SvtUserOptions in unotools/useroptions.hxx
115  UserOptToken nUserOptionsId;
116  // id for settings the focus (defined in svx/optgenrl.hxx)
117  EditPosition nGrabFocusId;
118 }
119 const vFieldInfo[] =
120 {
121  // Company
122  { Row_Company, "company", UserOptToken::Company, EditPosition::COMPANY },
123  // Name
124  { Row_Name, "firstname", UserOptToken::FirstName, EditPosition::FIRSTNAME },
125  { Row_Name, "lastname", UserOptToken::LastName, EditPosition::LASTNAME },
126  { Row_Name, "shortname", UserOptToken::ID, EditPosition::SHORTNAME },
127  // Name (russian)
128  { Row_Name_Russian, "ruslastname", UserOptToken::LastName, EditPosition::LASTNAME },
129  { Row_Name_Russian, "rusfirstname", UserOptToken::FirstName, EditPosition::FIRSTNAME },
130  { Row_Name_Russian, "rusfathersname", UserOptToken::FathersName, EditPosition::UNKNOWN },
131  { Row_Name_Russian, "russhortname", UserOptToken::ID, EditPosition::SHORTNAME },
132  // Name (eastern: reversed name ord
133  { Row_Name_Eastern, "eastlastname", UserOptToken::LastName, EditPosition::LASTNAME },
134  { Row_Name_Eastern, "eastfirstname", UserOptToken::FirstName, EditPosition::FIRSTNAME },
135  { Row_Name_Eastern, "eastshortname", UserOptToken::ID, EditPosition::SHORTNAME },
136  // Street
137  { Row_Street, "street", UserOptToken::Street, EditPosition::STREET },
138  // Street (russian)
139  { Row_Street_Russian, "russtreet", UserOptToken::Street, EditPosition::STREET },
140  { Row_Street_Russian, "apartnum", UserOptToken::Apartment, EditPosition::UNKNOWN },
141  // City
142  { Row_City, "izip", UserOptToken::Zip, EditPosition::PLZ },
143  { Row_City, "icity", UserOptToken::City, EditPosition::CITY },
144  // City (US)
145  { Row_City_US, "city", UserOptToken::City, EditPosition::CITY },
146  { Row_City_US, "state", UserOptToken::State, EditPosition::STATE },
147  { Row_City_US, "zip", UserOptToken::Zip, EditPosition::PLZ },
148  // Country
149  { Row_Country, "country", UserOptToken::Country, EditPosition::COUNTRY },
150  // Title/Position
151  { Row_TitlePos, "title", UserOptToken::Title, EditPosition::TITLE },
152  { Row_TitlePos, "position", UserOptToken::Position, EditPosition::POSITION },
153  // Phone
154  { Row_Phone, "home", UserOptToken::TelephoneHome, EditPosition::TELPRIV },
155  { Row_Phone, "work", UserOptToken::TelephoneWork, EditPosition::TELCOMPANY },
156  // Fax/Mail
157  { Row_FaxMail, "fax", UserOptToken::Fax, EditPosition::FAX },
158  { Row_FaxMail, "email", UserOptToken::Email, EditPosition::EMAIL },
159 };
160 
161 
162 } // namespace
163 
164 
165 // Row
166 
168 {
169  // row label
170  std::unique_ptr<weld::Label> xLabel;
171  // first and last field in the row (last is exclusive)
172  unsigned nFirstField, nLastField;
173 
174 public:
175  explicit Row (std::unique_ptr<weld::Label> xLabel_)
176  : xLabel(std::move(xLabel_))
177  , nFirstField(0)
178  , nLastField(0)
179  {
180  xLabel->show();
181  }
182 };
183 
184 
185 // Field
186 
188 {
189  // which field is this? (in vFieldInfo[] above)
190  unsigned iField;
191  // edit box
192  std::unique_ptr<weld::Entry> xEdit;
193  std::unique_ptr<weld::Container> xParent;
194 
195 public:
196  Field (std::unique_ptr<weld::Entry> xEdit_, unsigned iField_)
197  : iField(iField_)
198  , xEdit(std::move(xEdit_))
199  , xParent(xEdit->weld_parent())
200  {
201  //We want all widgets inside a container, so each row of the toplevel
202  //grid has another container in it. To avoid adding spacing to these
203  //empty grids they all default to invisible, so show them if their
204  //children are visible
205  xParent->show();
206  xEdit->show();
207  }
208 };
209 
211  : SfxTabPage(pPage, pController, "cui/ui/optuserpage.ui", "OptUserPage", &rCoreSet)
212  , m_xUseDataCB(m_xBuilder->weld_check_button("usefordocprop"))
213  , m_xCryptoFrame(m_xBuilder->weld_widget( "cryptography"))
214  , m_xSigningKeyLB(m_xBuilder->weld_combo_box("signingkey"))
215  , m_xEncryptionKeyLB(m_xBuilder->weld_combo_box("encryptionkey"))
216  , m_xEncryptToSelfCB(m_xBuilder->weld_check_button("encrypttoself"))
217 {
218  InitControls();
219 #if HAVE_FEATURE_GPGME
221 #else
222  m_xCryptoFrame->hide();
223 #endif
224 
225  SetExchangeSupport(); // this page needs ExchangeSupport
226  SetLinks();
227 }
228 
230 {
231 }
232 
233 // Initializes the titles and the edit boxes,
234 // according to vRowInfo[] and vFieldInfo[] above.
236 {
237  // which language bit do we use? (see Lang and vRowInfo[] above)
238  unsigned LangBit;
240  if (l == LANGUAGE_ENGLISH_US)
241  LangBit = Lang::US;
242  else if (l == LANGUAGE_RUSSIAN)
243  LangBit = Lang::Russian;
244  else
245  {
247  LangBit = Lang::Eastern;
248  else
249  LangBit = Lang::Others;
250  }
251 
252  // creating rows
253  unsigned iField = 0;
254  for (unsigned iRow = 0; iRow != nRowCount; ++iRow)
255  {
256  RowType const eRow = static_cast<RowType>(iRow);
257  // is the row visible?
258  if (!(vRowInfo[iRow].nLangFlags & LangBit))
259  continue;
260  // creating row
261  vRows.push_back(std::make_shared<Row>(
262  m_xBuilder->weld_label(vRowInfo[iRow].pTextId)));
263  Row& rRow = *vRows.back();
264  // fields in the row
265  static unsigned const nFieldCount = SAL_N_ELEMENTS(vFieldInfo);
266  // skipping other (invisible) rows
267  while (iField != nFieldCount && vFieldInfo[iField].eRow != eRow)
268  ++iField;
269  // fields in the row
270  rRow.nFirstField = vFields.size();
271  for ( ; iField != nFieldCount && vFieldInfo[iField].eRow == eRow; ++iField)
272  {
273  // creating edit field
274  vFields.push_back(std::make_shared<Field>(
275  m_xBuilder->weld_entry(vFieldInfo[iField].pEditId), iField));
276  // "short name" field?
277  if (vFieldInfo[iField].nUserOptionsId == UserOptToken::ID)
278  {
279  nNameRow = vRows.size() - 1;
280  nShortNameField = vFields.size() - 1;
281  }
282  }
283  rRow.nLastField = vFields.size();
284  }
285 }
286 
288 {
289 #if HAVE_FEATURE_GPGME
290  m_xCryptoFrame->show();
291 
292  uno::Reference< xml::crypto::XSEInitializer > xSEInitializer;
293  try
294  {
295  xSEInitializer = xml::crypto::GPGSEInitializer::create( comphelper::getProcessComponentContext() );
296  uno::Reference<xml::crypto::XXMLSecurityContext> xSC = xSEInitializer->createSecurityContext( OUString() );
297  if (xSC.is())
298  {
299  uno::Reference<xml::crypto::XSecurityEnvironment> xSE = xSC->getSecurityEnvironment();
300  uno::Sequence<uno::Reference<security::XCertificate>> xCertificates = xSE->getPersonalCertificates();
301 
302  if (xCertificates.hasElements())
303  {
304  for (auto& xCert : xCertificates)
305  {
306  m_xSigningKeyLB->append_text( xCert->getIssuerName());
307  m_xEncryptionKeyLB->append_text( xCert->getIssuerName());
308  }
309  }
310 
311  //tdf#115015: wrap checkbox text and listboxes if necessary
312  int nPrefWidth(m_xEncryptToSelfCB->get_preferred_size().Width());
313  int nMaxWidth = m_xEncryptToSelfCB->get_approximate_digit_width() * 40;
314  if (nPrefWidth > nMaxWidth)
315  {
316  m_xSigningKeyLB->set_size_request(nMaxWidth, -1);
317  m_xEncryptionKeyLB->set_size_request(nMaxWidth, -1);
318  m_xEncryptToSelfCB->set_label_line_wrap(true);
319  m_xEncryptToSelfCB->set_size_request(nMaxWidth, -1);
320  }
321  }
322  }
323  catch ( uno::Exception const & )
324  {}
325 #endif
326 
327 }
328 
330 {
331  // link for updating the initials
332  Link<weld::Entry&,void> aLink = LINK( this, SvxGeneralTabPage, ModifyHdl_Impl );
333  Row& rNameRow = *vRows[nNameRow];
334  for (unsigned i = rNameRow.nFirstField; i != rNameRow.nLastField - 1; ++i)
335  vFields[i]->xEdit->connect_changed(aLink);
336 }
337 
338 
339 std::unique_ptr<SfxTabPage> SvxGeneralTabPage::Create( weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet )
340 {
341  return std::make_unique<SvxGeneralTabPage>( pPage, pController, *rAttrSet );
342 }
343 
345 {
346  // remove leading and trailing whitespaces
347  for (auto const & i: vFields)
348  i->xEdit->set_text(comphelper::string::strip(i->xEdit->get_text(), ' '));
349 
350  bool bModified = false;
351  bModified |= GetData_Impl();
352  SvtSaveOptions aSaveOpt;
353  if (m_xUseDataCB->get_active() != aSaveOpt.IsUseUserData())
354  {
355  aSaveOpt.SetUseUserData(m_xUseDataCB->get_active());
356  bModified = true;
357  }
358  return bModified;
359 }
360 
362 {
363  SetData_Impl();
364 
365  sal_uInt16 const nWhich = GetWhich(SID_FIELD_GRABFOCUS);
366 
367  if (rSet->GetItemState(nWhich) == SfxItemState::SET)
368  {
369  EditPosition nField = static_cast<EditPosition>(static_cast<const SfxUInt16Item&>(rSet->Get(nWhich)).GetValue());
370  if (nField != EditPosition::UNKNOWN)
371  {
372  for (auto const & i: vFields)
373  if (nField == vFieldInfo[i->iField].nGrabFocusId)
374  i->xEdit->grab_focus();
375  }
376  else
377  vFields.front()->xEdit->grab_focus();
378  }
379 
380  m_xUseDataCB->set_active(SvtSaveOptions().IsUseUserData());
381 }
382 
383 
384 // ModifyHdl_Impl()
385 // This handler updates the initials (short name)
386 // when one of the name fields was updated.
387 IMPL_LINK( SvxGeneralTabPage, ModifyHdl_Impl, weld::Entry&, rEdit, void )
388 {
389  // short name field and row
390  Field& rShortName = *vFields[nShortNameField];
391  Row& rNameRow = *vRows[nNameRow];
392  // number of initials
393  unsigned const nInits = rNameRow.nLastField - rNameRow.nFirstField - 1;
394  // which field was updated? (in rNameRow)
395  unsigned nField = nInits;
396  for (unsigned i = 0; i != nInits; ++i)
397  {
398  if (vFields[rNameRow.nFirstField + i]->xEdit.get() == &rEdit)
399  nField = i;
400  }
401  // updating the initial
402  if (nField < nInits && rShortName.xEdit->get_sensitive())
403  {
404  OUString sShortName = rShortName.xEdit->get_text();
405  // clear short name if it contains more characters than the number of initials
406  if (o3tl::make_unsigned(sShortName.getLength()) > nInits)
407  {
408  rShortName.xEdit->set_text(OUString());
409  }
410  while (o3tl::make_unsigned(sShortName.getLength()) < nInits)
411  sShortName += " ";
412  OUString sName = rEdit.get_text();
413  OUString sLetter = sName.isEmpty()
414  ? OUString(u' ') : sName.copy(0, 1);
415  rShortName.xEdit->set_text(sShortName.replaceAt(nField, 1, sLetter).trim());
416  }
417 }
418 
419 
421 {
422  // updating
423  SvtUserOptions aUserOpt;
424  for (auto const & i: vFields)
425  aUserOpt.SetToken(
426  vFieldInfo[i->iField].nUserOptionsId,
427  i->xEdit->get_text()
428  );
429 
430  // modified?
431  bool bModified = false;
432  for (auto const & i: vFields)
433  {
434  if (i->xEdit->get_value_changed_from_saved())
435  {
436  bModified = true;
437  break;
438  }
439  }
440 
441 #if HAVE_FEATURE_GPGME
442  OUString aSK = m_xSigningKeyLB->get_active() == 0 ? OUString() //i.e. no key
443  : m_xSigningKeyLB->get_active_text();
444  OUString aEK = m_xEncryptionKeyLB->get_active() == 0 ? OUString()
445  : m_xEncryptionKeyLB->get_active_text();
446 
447  aUserOpt.SetToken( UserOptToken::SigningKey, aSK );
448  aUserOpt.SetToken( UserOptToken::EncryptionKey, aEK );
449  aUserOpt.SetBoolValue( UserOptToken::EncryptToSelf, m_xEncryptToSelfCB->get_active() );
450 
451  bModified |= m_xSigningKeyLB->get_value_changed_from_saved() ||
452  m_xEncryptionKeyLB->get_value_changed_from_saved() ||
453  m_xEncryptToSelfCB->get_state_changed_from_saved();
454 #endif
455 
456  return bModified;
457 }
458 
459 
461 {
462  // updating and disabling edit boxes
463  SvtUserOptions aUserOpt;
464  for (auto const & i: vRows)
465  {
466  Row& rRow = *i;
467  // the label is enabled if any of its edit fields are enabled
468  bool bEnableLabel = false;
469  for (unsigned iField = rRow.nFirstField; iField != rRow.nLastField; ++iField)
470  {
471  Field& rField = *vFields[iField];
472  // updating content
473  UserOptToken const nToken = vFieldInfo[rField.iField].nUserOptionsId;
474  rField.xEdit->set_text(aUserOpt.GetToken(nToken));
475  // is enabled?
476  bool const bEnableEdit = !aUserOpt.IsTokenReadonly(nToken);
477  rField.xEdit->set_sensitive(bEnableEdit);
478  bEnableLabel = bEnableLabel || bEnableEdit;
479  }
480  rRow.xLabel->set_sensitive(bEnableLabel);
481  }
482 
483  // saving
484  for (auto const & i: vFields)
485  i->xEdit->save_value();
486 
487 #if HAVE_FEATURE_GPGME
488  OUString aSK = aUserOpt.GetToken(UserOptToken::SigningKey);
489  aSK.isEmpty() ? m_xSigningKeyLB->set_active( 0 ) //i.e. 'No Key'
490  : m_xSigningKeyLB->set_active_text( aSK );
491 
492  OUString aEK = aUserOpt.GetToken(UserOptToken::EncryptionKey);
493  aEK.isEmpty() ? m_xEncryptionKeyLB->set_active( 0 ) //i.e. 'No Key'
494  : m_xEncryptionKeyLB->set_active_text( aEK );
495 
496  m_xEncryptToSelfCB->set_active( aUserOpt.GetEncryptToSelf() );
497 #endif
498 }
499 
500 
502 {
503  if ( pSet_ )
504  FillItemSet( pSet_ );
505  return DeactivateRC::LeavePage;
506 }
507 
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString GetToken(UserOptToken nToken) const
#define LANGUAGE_ENGLISH_US
const LanguageTag & GetUILanguageTag() const
std::string GetValue
unsigned nShortNameField
Definition: cuioptgenrl.hxx:48
LanguageType getLanguageType(bool bResolveSystem=true) const
static const AllSettings & GetSettings()
#define LANGUAGE_RUSSIAN
std::unique_ptr< weld::Entry > xEdit
Definition: optgenrl.cxx:192
std::unique_ptr< weld::Widget > m_xCryptoFrame
Definition: cuioptgenrl.hxx:36
RowType
Definition: optgenrl.cxx:48
std::unique_ptr< weld::ComboBox > m_xSigningKeyLB
Definition: cuioptgenrl.hxx:37
std::vector< std::shared_ptr< Row > > vRows
Definition: cuioptgenrl.hxx:41
bool IsTokenReadonly(UserOptToken nToken) const
std::unique_ptr< weld::CheckButton > m_xEncryptToSelfCB
Definition: cuioptgenrl.hxx:39
void SetExchangeSupport()
SvxGeneralTabPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: optgenrl.cxx:210
const char * sName
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: optgenrl.cxx:344
#define SAL_N_ELEMENTS(arr)
void SetUseUserData(bool b)
Field(std::unique_ptr< weld::Entry > xEdit_, unsigned iField_)
Definition: optgenrl.cxx:196
static bool isFamilyNameFirst(LanguageType nLang)
void SetBoolValue(UserOptToken nToken, bool bNewValue)
void InitCryptography()
Definition: optgenrl.cxx:287
bool IsUseUserData() const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
bool GetEncryptToSelf() const
int i
std::unique_ptr< weld::CheckButton > m_xUseDataCB
Definition: cuioptgenrl.hxx:35
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
float u
IMPL_LINK(SvxGeneralTabPage, ModifyHdl_Impl, weld::Entry &, rEdit, void)
Definition: optgenrl.cxx:387
virtual OUString get_text() const =0
virtual DeactivateRC DeactivatePage(SfxItemSet *pSet) override
Definition: optgenrl.cxx:501
void SetToken(UserOptToken nToken, OUString const &rNewToken)
std::vector< std::shared_ptr< Field > > vFields
Definition: cuioptgenrl.hxx:44
weld::Entry & rEdit
UserOptToken
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
OString strip(const OString &rIn, char c)
virtual void Reset(const SfxItemSet *rSet) override
Definition: optgenrl.cxx:361
Reference< XComponentContext > getProcessComponentContext()
std::unique_ptr< weld::Label > xLabel
Definition: optgenrl.cxx:170
DeactivateRC
virtual ~SvxGeneralTabPage() override
Definition: optgenrl.cxx:229
std::unique_ptr< weld::ComboBox > m_xEncryptionKeyLB
Definition: cuioptgenrl.hxx:38
DefTokenId const nToken
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: optgenrl.cxx:339
Row(std::unique_ptr< weld::Label > xLabel_)
Definition: optgenrl.cxx:175
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const
std::unique_ptr< weld::Container > xParent
Definition: optgenrl.cxx:193