LibreOffice Module cui (master)  1
SvxConfigPageHelper.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 <SvxConfigPageHelper.hxx>
21 
22 #include <com/sun/star/frame/ModuleManager.hpp>
23 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
24 #include <com/sun/star/ui/ImageType.hpp>
25 #include <com/sun/star/ui/ItemType.hpp>
26 
27 #include <comphelper/random.hxx>
29 #include <svtools/imgdef.hxx>
30 #include <svtools/miscopt.hxx>
31 
32 static sal_Int16 theImageType =
33  css::ui::ImageType::COLOR_NORMAL |
34  css::ui::ImageType::SIZE_DEFAULT;
35 
36 void SvxConfigPageHelper::RemoveEntry( SvxEntries* pEntries, SvxConfigEntry const * pChildEntry )
37 {
38  SvxEntries::iterator iter = pEntries->begin();
39 
40  while ( iter != pEntries->end() )
41  {
42  if ( pChildEntry == *iter )
43  {
44  pEntries->erase( iter );
45  break;
46  }
47  ++iter;
48  }
49 }
50 
51 OUString SvxConfigPageHelper::replaceSaveInName( const OUString& rMessage, const OUString& rSaveInName )
52 {
53  const OUString placeholder("%SAVE IN SELECTION%" );
54 
55  OUString name = rMessage.replaceFirst(placeholder, rSaveInName);
56 
57  return name;
58 }
59 
60 OUString SvxConfigPageHelper::stripHotKey( const OUString& str )
61 {
62  return str.replaceFirst("~", "");
63 }
64 
65 OUString SvxConfigPageHelper::replaceSixteen( const OUString& str, sal_Int32 nReplacement )
66 {
67  return str.replaceAll( OUString::number( 16 ), OUString::number( nReplacement ));
68 }
69 
71 {
72  return theImageType;
73 }
74 
76 {
77  theImageType =
78  css::ui::ImageType::COLOR_NORMAL |
79  css::ui::ImageType::SIZE_DEFAULT;
80 
81  if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE)
82  {
83  theImageType |= css::ui::ImageType::SIZE_LARGE;
84  }
85  else if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32)
86  {
87  theImageType |= css::ui::ImageType::SIZE_32;
88  }
89 }
90 
91 css::uno::Reference< css::graphic::XGraphic > SvxConfigPageHelper::GetGraphic(
92  const css::uno::Reference< css::ui::XImageManager >& xImageManager,
93  const OUString& rCommandURL )
94 {
95  css::uno::Reference< css::graphic::XGraphic > result;
96 
97  if ( xImageManager.is() )
98  {
99  // TODO handle large graphics
100  css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > aGraphicSeq;
101 
102  css::uno::Sequence<OUString> aImageCmdSeq { rCommandURL };
103 
104  try
105  {
106  aGraphicSeq =
107  xImageManager->getImages( GetImageType(), aImageCmdSeq );
108 
109  if ( aGraphicSeq.hasElements() )
110  {
111  result = aGraphicSeq[0];
112  }
113  }
114  catch ( css::uno::Exception& )
115  {
116  // will return empty XGraphic
117  }
118  }
119 
120  return result;
121 }
122 
123 OUString
125  const OUString& prefix,
126  SvxEntries* entries,
127  sal_Int32 suffix /*= 1*/ )
128 {
129  OUString name;
130  sal_Int32 pos = 0;
131 
132  // find and replace the %n placeholder in the prefix string
133  name = prefix.replaceFirst( "%n", OUString::number( suffix ), &pos );
134 
135  if ( pos == -1 )
136  {
137  // no placeholder found so just append the suffix
138  name += OUString::number( suffix );
139  }
140 
141  if (!entries)
142  return name;
143 
144  // now check if there is an already existing entry with this name
145  bool bFoundEntry = false;
146  for (auto const& entry : *entries)
147  {
148  if ( name.equals(entry->GetName()) )
149  {
150  bFoundEntry = true;
151  break;
152  }
153  }
154 
155  if (bFoundEntry)
156  {
157  // name already exists so try the next number up
158  return generateCustomName( prefix, entries, ++suffix );
159  }
160 
161  return name;
162 }
163 
165  SvxEntries* entries,
166  sal_Int32 suffix /*= 1*/ )
167 {
168  OUString url = "vnd.openoffice.org:CustomMenu" + OUString::number( suffix );
169  if (!entries)
170  return url;
171 
172  // now check is there is an already existing entry with this url
173  bool bFoundEntry = false;
174  for (auto const& entry : *entries)
175  {
176  if ( url.equals(entry->GetCommand()) )
177  {
178  bFoundEntry = true;
179  break;
180  }
181  }
182 
183  if (bFoundEntry)
184  {
185  // url already exists so try the next number up
186  return generateCustomMenuURL( entries, ++suffix );
187  }
188 
189  return url;
190 }
191 
193 {
194  return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<unsigned int>::max());
195 }
196 
198 {
199  OUString url = ITEM_TOOLBAR_URL;
200  url += CUSTOM_TOOLBAR_STR;
201 
202  // use a random number to minimize possible clash with existing custom toolbars
203  url += OUString::number( generateRandomValue(), 16 );
204 
205  // now check is there is an already existing entry with this url
206  bool bFoundEntry = false;
207  for (auto const& entry : *entries)
208  {
209  if ( url.equals(entry->GetCommand()) )
210  {
211  bFoundEntry = true;
212  break;
213  }
214  }
215 
216  if (bFoundEntry)
217  {
218  // url already exists so try the next number up
219  return generateCustomURL( entries );
220  }
221 
222  return url;
223 }
224 
225 OUString SvxConfigPageHelper::GetModuleName( const OUString& aModuleId )
226 {
227  if ( aModuleId == "com.sun.star.text.TextDocument" ||
228  aModuleId == "com.sun.star.text.GlobalDocument" )
229  return "Writer";
230  else if ( aModuleId == "com.sun.star.text.WebDocument" )
231  return "Writer/Web";
232  else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
233  return "Draw";
234  else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
235  return "Impress";
236  else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
237  return "Calc";
238  else if ( aModuleId == "com.sun.star.script.BasicIDE" )
239  return "Basic";
240  else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
241  return "Math";
242  else if ( aModuleId == "com.sun.star.sdb.RelationDesign" )
243  return "Relation Design";
244  else if ( aModuleId == "com.sun.star.sdb.QueryDesign" )
245  return "Query Design";
246  else if ( aModuleId == "com.sun.star.sdb.TableDesign" )
247  return "Table Design";
248  else if ( aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
249  return "Data Source Browser";
250  else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" )
251  return "Database";
252 
253  return OUString();
254 }
255 
257  const OUString& aModuleId,
258  const css::uno::Reference< css::frame::XModuleManager2 >& rModuleManager )
259 {
260  assert(rModuleManager.is());
261 
262  OUString aModuleUIName;
263 
264  try
265  {
266  css::uno::Any a = rModuleManager->getByName( aModuleId );
267  css::uno::Sequence< css::beans::PropertyValue > aSeq;
268 
269  if ( a >>= aSeq )
270  {
271  for ( sal_Int32 i = 0; i < aSeq.getLength(); ++i )
272  {
273  if ( aSeq[i].Name == "ooSetupFactoryUIName" )
274  {
275  aSeq[i].Value >>= aModuleUIName;
276  break;
277  }
278  }
279  }
280  }
281  catch ( css::uno::RuntimeException& )
282  {
283  throw;
284  }
285  catch ( css::uno::Exception& )
286  {
287  }
288 
289  if ( aModuleUIName.isEmpty() )
290  aModuleUIName = GetModuleName( aModuleId );
291 
292  return aModuleUIName;
293 }
294 
296  const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
297  sal_Int32 nIndex,
298  OUString& rCommandURL,
299  OUString& rLabel,
300  sal_uInt16& rType,
301  sal_Int32& rStyle,
302  css::uno::Reference< css::container::XIndexAccess >& rSubMenu )
303 {
304  try
305  {
306  css::uno::Sequence< css::beans::PropertyValue > aProp;
307  if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
308  {
309  for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
310  {
311  if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
312  {
313  aProp[i].Value >>= rCommandURL;
314  }
315  else if ( aProp[i].Name == ITEM_DESCRIPTOR_CONTAINER )
316  {
317  aProp[i].Value >>= rSubMenu;
318  }
319  else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
320  {
321  aProp[i].Value >>= rStyle;
322  }
323  else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
324  {
325  aProp[i].Value >>= rLabel;
326  }
327  else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
328  {
329  aProp[i].Value >>= rType;
330  }
331  }
332 
333  return true;
334  }
335  }
336  catch ( css::lang::IndexOutOfBoundsException& )
337  {
338  }
339 
340  return false;
341 }
342 
344  const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
345  sal_Int32 nIndex,
346  OUString& rCommandURL,
347  OUString& rLabel,
348  sal_uInt16& rType,
349  bool& rIsVisible,
350  sal_Int32& rStyle )
351 {
352  try
353  {
354  css::uno::Sequence< css::beans::PropertyValue > aProp;
355  if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
356  {
357  for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
358  {
359  if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
360  {
361  aProp[i].Value >>= rCommandURL;
362  }
363  else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
364  {
365  aProp[i].Value >>= rStyle;
366  }
367  else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
368  {
369  aProp[i].Value >>= rLabel;
370  }
371  else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
372  {
373  aProp[i].Value >>= rType;
374  }
375  else if ( aProp[i].Name == ITEM_DESCRIPTOR_ISVISIBLE )
376  {
377  aProp[i].Value >>= rIsVisible;
378  }
379  }
380 
381  return true;
382  }
383  }
384  catch ( css::lang::IndexOutOfBoundsException& )
385  {
386  }
387 
388  return false;
389 }
390 
391 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertSvxConfigEntry(
392  const SvxConfigEntry* pEntry )
393 {
394  css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 4 );
395 
396  aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
397  aPropSeq[0].Value <<= pEntry->GetCommand();
398 
399  aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
400  aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
401 
402  // If the name has not been changed, then the label can be stored
403  // as an empty string.
404  // It will be initialised again later using the command to label map.
405  aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
406  if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
407  {
408  aPropSeq[2].Value <<= OUString();
409  }
410  else
411  {
412  aPropSeq[2].Value <<= pEntry->GetName();
413  }
414 
415  aPropSeq[3].Name = ITEM_DESCRIPTOR_STYLE;
416  aPropSeq[3].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
417 
418  return aPropSeq;
419 }
420 
421 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertToolbarEntry(
422  const SvxConfigEntry* pEntry )
423 {
424  css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 5 );
425 
426  aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
427  aPropSeq[0].Value <<= pEntry->GetCommand();
428 
429  aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
430  aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
431 
432  // If the name has not been changed, then the label can be stored
433  // as an empty string.
434  // It will be initialised again later using the command to label map.
435  aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
436  if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
437  {
438  aPropSeq[2].Value <<= OUString();
439  }
440  else
441  {
442  aPropSeq[2].Value <<= pEntry->GetName();
443  }
444 
445  aPropSeq[3].Name = ITEM_DESCRIPTOR_ISVISIBLE;
446  aPropSeq[3].Value <<= pEntry->IsVisible();
447 
448  aPropSeq[4].Name = ITEM_DESCRIPTOR_STYLE;
449  aPropSeq[4].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
450 
451  return aPropSeq;
452 }
453 
455  const css::uno::Reference< css::frame::XFrame >& xFrame )
456 {
457  if (!xFrame.is())
458  {
459  return false;
460  }
461  OUString sModuleId(
462  css::frame::ModuleManager::create(
464  ->identify(xFrame));
465  return !sModuleId.isEmpty()
466  && sModuleId != "com.sun.star.frame.StartModule";
467 }
468 
470 {
471  return a->GetName().compareTo( b->GetName() ) < 0;
472 }
473 
475 {
476  SvxEntries* pEntries = pEntry->GetEntries();
477  if ( !pEntries )
478  return false;
479 
480  for ( const auto& entry : *pEntries )
481  {
482  if ( entry->IsModified() || SvxConfigEntryModified( entry ) )
483  return true;
484  }
485  return false;
486 }
487 
488 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString replaceSixteen(const OUString &str, sal_Int32 nReplacement)
SvxEntries * GetEntries() const
Definition: cfg.hxx:287
static const char ITEM_DESCRIPTOR_TYPE[]
Definition: cfg.hxx:45
const OUString & GetName() const
Definition: cfg.hxx:275
static OUString generateCustomURL(SvxEntries *entries)
Generates a custom resource URL for a new toolbar.
bool IsVisible() const
Definition: cfg.hxx:304
static OUString stripHotKey(const OUString &str)
static bool showKeyConfigTabPage(const css::uno::Reference< css::frame::XFrame > &xFrame)
static sal_Int16 GetImageType()
static OUString GetUIModuleName(const OUString &aModuleId, const css::uno::Reference< css::frame::XModuleManager2 > &rModuleManager)
static const char ITEM_DESCRIPTOR_ISVISIBLE[]
Definition: cfg.hxx:47
static const char CUSTOM_TOOLBAR_STR[]
Definition: cfg.hxx:54
OUString Name
static css::uno::Reference< css::graphic::XGraphic > GetGraphic(const css::uno::Reference< css::ui::XImageManager > &xImageManager, const OUString &rCommandURL)
unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
static OUString GetModuleName(const OUString &aModuleId)
std::vector< SvxConfigEntry * > SvxEntries
Definition: cfg.hxx:59
const OUString & GetCommand() const
Definition: cfg.hxx:273
static bool SvxConfigEntryModified(SvxConfigEntry const *pEntry)
static void RemoveEntry(SvxEntries *pEntries, SvxConfigEntry const *pChildEntry)
static OUString generateCustomMenuURL(SvxEntries *entries, sal_Int32 suffix=1)
static OUString generateCustomName(const OUString &prefix, SvxEntries *entries, sal_Int32 suffix=1)
static OUString replaceSaveInName(const OUString &rMessage, const OUString &rSaveInName)
static bool EntrySort(SvxConfigEntry const *a, SvxConfigEntry const *b)
static const char ITEM_DESCRIPTOR_STYLE[]
Definition: cfg.hxx:46
static const char ITEM_DESCRIPTOR_LABEL[]
Definition: cfg.hxx:44
static css::uno::Sequence< css::beans::PropertyValue > ConvertToolbarEntry(const SvxConfigEntry *pEntry)
MetadataImporterPluginType * result
int i
static css::uno::Sequence< css::beans::PropertyValue > ConvertSvxConfigEntry(const SvxConfigEntry *pEntry)
static const char ITEM_TOOLBAR_URL[]
Definition: cfg.hxx:52
static sal_uInt32 generateRandomValue()
sal_Int32 GetStyle() const
Definition: cfg.hxx:312
static const char ITEM_DESCRIPTOR_CONTAINER[]
Definition: cfg.hxx:43
SFX_SYMBOLS_SIZE_32
Reference< XComponentContext > getProcessComponentContext()
const char * name
static const char ITEM_DESCRIPTOR_COMMANDURL[]
Definition: cfg.hxx:42
SFX_SYMBOLS_SIZE_LARGE
static bool GetToolbarItemData(const css::uno::Reference< css::container::XIndexAccess > &rItemContainer, sal_Int32 nIndex, OUString &rCommandURL, OUString &rLabel, sal_uInt16 &rType, bool &rIsVisible, sal_Int32 &rStyle)
static bool GetMenuItemData(const css::uno::Reference< css::container::XIndexAccess > &rItemContainer, sal_Int32 nIndex, OUString &rCommandURL, OUString &rLabel, sal_uInt16 &rType, sal_Int32 &rStyle, css::uno::Reference< css::container::XIndexAccess > &rSubMenu)
static sal_Int16 theImageType
bool HasChangedName() const
Definition: cfg.hxx:277