bump product version to 6.4.0.3
[LibreOffice.git] / cui / source / customize / SvxConfigPageHelper.cxx
blobf59ccb118adc58f07faf799bc43ab56d8dab0448
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <SvxConfigPageHelper.hxx>
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>
27 #include <comphelper/random.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <svtools/imgdef.hxx>
30 #include <svtools/miscopt.hxx>
32 static sal_Int16 theImageType =
33 css::ui::ImageType::COLOR_NORMAL |
34 css::ui::ImageType::SIZE_DEFAULT;
36 void SvxConfigPageHelper::RemoveEntry( SvxEntries* pEntries, SvxConfigEntry const * pChildEntry )
38 SvxEntries::iterator iter = pEntries->begin();
40 while ( iter != pEntries->end() )
42 if ( pChildEntry == *iter )
44 pEntries->erase( iter );
45 break;
47 ++iter;
51 OUString SvxConfigPageHelper::replaceSaveInName( const OUString& rMessage, const OUString& rSaveInName )
53 const OUString placeholder("%SAVE IN SELECTION%" );
55 OUString name = rMessage.replaceFirst(placeholder, rSaveInName);
57 return name;
60 OUString SvxConfigPageHelper::stripHotKey( const OUString& str )
62 return str.replaceFirst("~", "");
65 OUString SvxConfigPageHelper::replaceSixteen( const OUString& str, sal_Int32 nReplacement )
67 return str.replaceAll( OUString::number( 16 ), OUString::number( nReplacement ));
70 sal_Int16 SvxConfigPageHelper::GetImageType()
72 return theImageType;
75 void SvxConfigPageHelper::InitImageType()
77 theImageType =
78 css::ui::ImageType::COLOR_NORMAL |
79 css::ui::ImageType::SIZE_DEFAULT;
81 if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE)
83 theImageType |= css::ui::ImageType::SIZE_LARGE;
85 else if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32)
87 theImageType |= css::ui::ImageType::SIZE_32;
91 css::uno::Reference< css::graphic::XGraphic > SvxConfigPageHelper::GetGraphic(
92 const css::uno::Reference< css::ui::XImageManager >& xImageManager,
93 const OUString& rCommandURL )
95 css::uno::Reference< css::graphic::XGraphic > result;
97 if ( xImageManager.is() )
99 // TODO handle large graphics
100 css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > aGraphicSeq;
102 css::uno::Sequence<OUString> aImageCmdSeq { rCommandURL };
106 aGraphicSeq =
107 xImageManager->getImages( GetImageType(), aImageCmdSeq );
109 if ( aGraphicSeq.hasElements() )
111 result = aGraphicSeq[0];
114 catch ( css::uno::Exception& )
116 // will return empty XGraphic
120 return result;
123 OUString
124 SvxConfigPageHelper::generateCustomName(
125 const OUString& prefix,
126 SvxEntries* entries,
127 sal_Int32 suffix /*= 1*/ )
129 OUString name;
130 sal_Int32 pos = 0;
132 // find and replace the %n placeholder in the prefix string
133 name = prefix.replaceFirst( "%n", OUString::number( suffix ), &pos );
135 if ( pos == -1 )
137 // no placeholder found so just append the suffix
138 name += OUString::number( suffix );
141 if (!entries)
142 return name;
144 // now check if there is an already existing entry with this name
145 bool bFoundEntry = false;
146 for (auto const& entry : *entries)
148 if ( name.equals(entry->GetName()) )
150 bFoundEntry = true;
151 break;
155 if (bFoundEntry)
157 // name already exists so try the next number up
158 return generateCustomName( prefix, entries, ++suffix );
161 return name;
164 OUString SvxConfigPageHelper::generateCustomMenuURL(
165 SvxEntries* entries,
166 sal_Int32 suffix /*= 1*/ )
168 OUString url = "vnd.openoffice.org:CustomMenu" + OUString::number( suffix );
169 if (!entries)
170 return url;
172 // now check is there is an already existing entry with this url
173 bool bFoundEntry = false;
174 for (auto const& entry : *entries)
176 if ( url.equals(entry->GetCommand()) )
178 bFoundEntry = true;
179 break;
183 if (bFoundEntry)
185 // url already exists so try the next number up
186 return generateCustomMenuURL( entries, ++suffix );
189 return url;
192 sal_uInt32 SvxConfigPageHelper::generateRandomValue()
194 return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<unsigned int>::max());
197 OUString SvxConfigPageHelper::generateCustomURL( SvxEntries* entries )
199 OUString url = OUStringLiteral(ITEM_TOOLBAR_URL) + CUSTOM_TOOLBAR_STR +
200 // use a random number to minimize possible clash with existing custom toolbars
201 OUString::number( generateRandomValue(), 16 );
203 // now check is there is an already existing entry with this url
204 bool bFoundEntry = false;
205 for (auto const& entry : *entries)
207 if ( url.equals(entry->GetCommand()) )
209 bFoundEntry = true;
210 break;
214 if (bFoundEntry)
216 // url already exists so try the next number up
217 return generateCustomURL( entries );
220 return url;
223 OUString SvxConfigPageHelper::GetModuleName( const OUString& aModuleId )
225 if ( aModuleId == "com.sun.star.text.TextDocument" ||
226 aModuleId == "com.sun.star.text.GlobalDocument" )
227 return "Writer";
228 else if ( aModuleId == "com.sun.star.text.WebDocument" )
229 return "Writer/Web";
230 else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
231 return "Draw";
232 else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
233 return "Impress";
234 else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
235 return "Calc";
236 else if ( aModuleId == "com.sun.star.script.BasicIDE" )
237 return "Basic";
238 else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
239 return "Math";
240 else if ( aModuleId == "com.sun.star.sdb.RelationDesign" )
241 return "Relation Design";
242 else if ( aModuleId == "com.sun.star.sdb.QueryDesign" )
243 return "Query Design";
244 else if ( aModuleId == "com.sun.star.sdb.TableDesign" )
245 return "Table Design";
246 else if ( aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
247 return "Data Source Browser";
248 else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" )
249 return "Database";
251 return OUString();
254 OUString SvxConfigPageHelper::GetUIModuleName(
255 const OUString& aModuleId,
256 const css::uno::Reference< css::frame::XModuleManager2 >& rModuleManager )
258 assert(rModuleManager.is());
260 OUString aModuleUIName;
264 css::uno::Any a = rModuleManager->getByName( aModuleId );
265 css::uno::Sequence< css::beans::PropertyValue > aSeq;
267 if ( a >>= aSeq )
269 for ( sal_Int32 i = 0; i < aSeq.getLength(); ++i )
271 if ( aSeq[i].Name == "ooSetupFactoryUIName" )
273 aSeq[i].Value >>= aModuleUIName;
274 break;
279 catch ( css::uno::RuntimeException& )
281 throw;
283 catch ( css::uno::Exception& )
287 if ( aModuleUIName.isEmpty() )
288 aModuleUIName = GetModuleName( aModuleId );
290 return aModuleUIName;
293 bool SvxConfigPageHelper::GetMenuItemData(
294 const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
295 sal_Int32 nIndex,
296 OUString& rCommandURL,
297 OUString& rLabel,
298 sal_uInt16& rType,
299 sal_Int32& rStyle,
300 css::uno::Reference< css::container::XIndexAccess >& rSubMenu )
304 css::uno::Sequence< css::beans::PropertyValue > aProp;
305 if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
307 for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
309 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
311 aProp[i].Value >>= rCommandURL;
313 else if ( aProp[i].Name == ITEM_DESCRIPTOR_CONTAINER )
315 aProp[i].Value >>= rSubMenu;
317 else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
319 aProp[i].Value >>= rStyle;
321 else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
323 aProp[i].Value >>= rLabel;
325 else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
327 aProp[i].Value >>= rType;
331 return true;
334 catch ( css::lang::IndexOutOfBoundsException& )
338 return false;
341 bool SvxConfigPageHelper::GetToolbarItemData(
342 const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
343 sal_Int32 nIndex,
344 OUString& rCommandURL,
345 OUString& rLabel,
346 sal_uInt16& rType,
347 bool& rIsVisible,
348 sal_Int32& rStyle )
352 css::uno::Sequence< css::beans::PropertyValue > aProp;
353 if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
355 for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
357 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
359 aProp[i].Value >>= rCommandURL;
361 else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
363 aProp[i].Value >>= rStyle;
365 else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
367 aProp[i].Value >>= rLabel;
369 else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
371 aProp[i].Value >>= rType;
373 else if ( aProp[i].Name == ITEM_DESCRIPTOR_ISVISIBLE )
375 aProp[i].Value >>= rIsVisible;
379 return true;
382 catch ( css::lang::IndexOutOfBoundsException& )
386 return false;
389 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertSvxConfigEntry(
390 const SvxConfigEntry* pEntry )
392 css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 4 );
394 aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
395 aPropSeq[0].Value <<= pEntry->GetCommand();
397 aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
398 aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
400 // If the name has not been changed, then the label can be stored
401 // as an empty string.
402 // It will be initialised again later using the command to label map.
403 aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
404 if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
406 aPropSeq[2].Value <<= OUString();
408 else
410 aPropSeq[2].Value <<= pEntry->GetName();
413 aPropSeq[3].Name = ITEM_DESCRIPTOR_STYLE;
414 aPropSeq[3].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
416 return aPropSeq;
419 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertToolbarEntry(
420 const SvxConfigEntry* pEntry )
422 css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 5 );
424 aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
425 aPropSeq[0].Value <<= pEntry->GetCommand();
427 aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
428 aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
430 // If the name has not been changed, then the label can be stored
431 // as an empty string.
432 // It will be initialised again later using the command to label map.
433 aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
434 if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
436 aPropSeq[2].Value <<= OUString();
438 else
440 aPropSeq[2].Value <<= pEntry->GetName();
443 aPropSeq[3].Name = ITEM_DESCRIPTOR_ISVISIBLE;
444 aPropSeq[3].Value <<= pEntry->IsVisible();
446 aPropSeq[4].Name = ITEM_DESCRIPTOR_STYLE;
447 aPropSeq[4].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
449 return aPropSeq;
452 bool SvxConfigPageHelper::showKeyConfigTabPage(
453 const css::uno::Reference< css::frame::XFrame >& xFrame )
455 if (!xFrame.is())
457 return false;
459 OUString sModuleId(
460 css::frame::ModuleManager::create(
461 comphelper::getProcessComponentContext())
462 ->identify(xFrame));
463 return !sModuleId.isEmpty()
464 && sModuleId != "com.sun.star.frame.StartModule";
467 bool SvxConfigPageHelper::EntrySort( SvxConfigEntry const * a, SvxConfigEntry const * b )
469 return a->GetName().compareTo( b->GetName() ) < 0;
472 bool SvxConfigPageHelper::SvxConfigEntryModified( SvxConfigEntry const * pEntry )
474 SvxEntries* pEntries = pEntry->GetEntries();
475 if ( !pEntries )
476 return false;
478 for ( const auto& entry : *pEntries )
480 if ( entry->IsModified() || SvxConfigEntryModified( entry ) )
481 return true;
483 return false;
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */