bump product version to 6.3.0.0.beta1
[LibreOffice.git] / cui / source / customize / SvxConfigPageHelper.cxx
blobe57858d0048a78b4d9015d04be8bfcff42e6b21c
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/lang/IndexOutOfBoundsException.hpp>
24 #include <comphelper/random.hxx>
25 #include <comphelper/processfactory.hxx>
27 static sal_Int16 theImageType =
28 css::ui::ImageType::COLOR_NORMAL |
29 css::ui::ImageType::SIZE_DEFAULT;
31 void SvxConfigPageHelper::RemoveEntry( SvxEntries* pEntries, SvxConfigEntry const * pChildEntry )
33 SvxEntries::iterator iter = pEntries->begin();
35 while ( iter != pEntries->end() )
37 if ( pChildEntry == *iter )
39 pEntries->erase( iter );
40 break;
42 ++iter;
46 OUString SvxConfigPageHelper::replaceSaveInName( const OUString& rMessage, const OUString& rSaveInName )
48 const OUString placeholder("%SAVE IN SELECTION%" );
50 OUString name = rMessage.replaceFirst(placeholder, rSaveInName);
52 return name;
55 OUString SvxConfigPageHelper::stripHotKey( const OUString& str )
57 return str.replaceFirst("~", "");
60 OUString SvxConfigPageHelper::replaceSixteen( const OUString& str, sal_Int32 nReplacement )
62 return str.replaceAll( OUString::number( 16 ), OUString::number( nReplacement ));
65 sal_Int16 SvxConfigPageHelper::GetImageType()
67 return theImageType;
70 void SvxConfigPageHelper::InitImageType()
72 theImageType =
73 css::ui::ImageType::COLOR_NORMAL |
74 css::ui::ImageType::SIZE_DEFAULT;
76 if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_LARGE)
78 theImageType |= css::ui::ImageType::SIZE_LARGE;
80 else if (SvtMiscOptions().GetCurrentSymbolsSize() == SFX_SYMBOLS_SIZE_32)
82 theImageType |= css::ui::ImageType::SIZE_32;
86 css::uno::Reference< css::graphic::XGraphic > SvxConfigPageHelper::GetGraphic(
87 const css::uno::Reference< css::ui::XImageManager >& xImageManager,
88 const OUString& rCommandURL )
90 css::uno::Reference< css::graphic::XGraphic > result;
92 if ( xImageManager.is() )
94 // TODO handle large graphics
95 css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > aGraphicSeq;
97 css::uno::Sequence<OUString> aImageCmdSeq { rCommandURL };
99 try
101 aGraphicSeq =
102 xImageManager->getImages( GetImageType(), aImageCmdSeq );
104 if ( aGraphicSeq.getLength() > 0 )
106 result = aGraphicSeq[0];
109 catch ( css::uno::Exception& )
111 // will return empty XGraphic
115 return result;
118 OUString
119 SvxConfigPageHelper::generateCustomName(
120 const OUString& prefix,
121 SvxEntries* entries,
122 sal_Int32 suffix /*= 1*/ )
124 OUString name;
125 sal_Int32 pos = 0;
127 // find and replace the %n placeholder in the prefix string
128 name = prefix.replaceFirst( "%n", OUString::number( suffix ), &pos );
130 if ( pos == -1 )
132 // no placeholder found so just append the suffix
133 name += OUString::number( suffix );
136 if (!entries)
137 return name;
139 // now check if there is an already existing entry with this name
140 bool bFoundEntry = false;
141 for (auto const& entry : *entries)
143 if ( name.equals(entry->GetName()) )
145 bFoundEntry = true;
146 break;
150 if (bFoundEntry)
152 // name already exists so try the next number up
153 return generateCustomName( prefix, entries, ++suffix );
156 return name;
159 OUString SvxConfigPageHelper::generateCustomMenuURL(
160 SvxEntries* entries,
161 sal_Int32 suffix /*= 1*/ )
163 OUString url = "vnd.openoffice.org:CustomMenu" + OUString::number( suffix );
164 if (!entries)
165 return url;
167 // now check is there is an already existing entry with this url
168 bool bFoundEntry = false;
169 for (auto const& entry : *entries)
171 if ( url.equals(entry->GetCommand()) )
173 bFoundEntry = true;
174 break;
178 if (bFoundEntry)
180 // url already exists so try the next number up
181 return generateCustomMenuURL( entries, ++suffix );
184 return url;
187 sal_uInt32 SvxConfigPageHelper::generateRandomValue()
189 return comphelper::rng::uniform_uint_distribution(0, std::numeric_limits<unsigned int>::max());
192 OUString SvxConfigPageHelper::generateCustomURL( SvxEntries* entries )
194 OUString url = ITEM_TOOLBAR_URL;
195 url += CUSTOM_TOOLBAR_STR;
197 // use a random number to minimize possible clash with existing custom toolbars
198 url += OUString::number( generateRandomValue(), 16 );
200 // now check is there is an already existing entry with this url
201 bool bFoundEntry = false;
202 for (auto const& entry : *entries)
204 if ( url.equals(entry->GetCommand()) )
206 bFoundEntry = true;
207 break;
211 if (bFoundEntry)
213 // url already exists so try the next number up
214 return generateCustomURL( entries );
217 return url;
220 OUString SvxConfigPageHelper::GetModuleName( const OUString& aModuleId )
222 if ( aModuleId == "com.sun.star.text.TextDocument" ||
223 aModuleId == "com.sun.star.text.GlobalDocument" )
224 return OUString("Writer");
225 else if ( aModuleId == "com.sun.star.text.WebDocument" )
226 return OUString("Writer/Web");
227 else if ( aModuleId == "com.sun.star.drawing.DrawingDocument" )
228 return OUString("Draw");
229 else if ( aModuleId == "com.sun.star.presentation.PresentationDocument" )
230 return OUString("Impress");
231 else if ( aModuleId == "com.sun.star.sheet.SpreadsheetDocument" )
232 return OUString("Calc");
233 else if ( aModuleId == "com.sun.star.script.BasicIDE" )
234 return OUString("Basic");
235 else if ( aModuleId == "com.sun.star.formula.FormulaProperties" )
236 return OUString("Math");
237 else if ( aModuleId == "com.sun.star.sdb.RelationDesign" )
238 return OUString("Relation Design");
239 else if ( aModuleId == "com.sun.star.sdb.QueryDesign" )
240 return OUString("Query Design");
241 else if ( aModuleId == "com.sun.star.sdb.TableDesign" )
242 return OUString("Table Design");
243 else if ( aModuleId == "com.sun.star.sdb.DataSourceBrowser" )
244 return OUString("Data Source Browser" );
245 else if ( aModuleId == "com.sun.star.sdb.DatabaseDocument" )
246 return OUString("Database" );
248 return OUString();
251 OUString SvxConfigPageHelper::GetUIModuleName(
252 const OUString& aModuleId,
253 const css::uno::Reference< css::frame::XModuleManager2 >& rModuleManager )
255 assert(rModuleManager.is());
257 OUString aModuleUIName;
261 css::uno::Any a = rModuleManager->getByName( aModuleId );
262 css::uno::Sequence< css::beans::PropertyValue > aSeq;
264 if ( a >>= aSeq )
266 for ( sal_Int32 i = 0; i < aSeq.getLength(); ++i )
268 if ( aSeq[i].Name == "ooSetupFactoryUIName" )
270 aSeq[i].Value >>= aModuleUIName;
271 break;
276 catch ( css::uno::RuntimeException& )
278 throw;
280 catch ( css::uno::Exception& )
284 if ( aModuleUIName.isEmpty() )
285 aModuleUIName = GetModuleName( aModuleId );
287 return aModuleUIName;
290 bool SvxConfigPageHelper::GetMenuItemData(
291 const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
292 sal_Int32 nIndex,
293 OUString& rCommandURL,
294 OUString& rLabel,
295 sal_uInt16& rType,
296 sal_Int32& rStyle,
297 css::uno::Reference< css::container::XIndexAccess >& rSubMenu )
301 css::uno::Sequence< css::beans::PropertyValue > aProp;
302 if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
304 for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
306 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
308 aProp[i].Value >>= rCommandURL;
310 else if ( aProp[i].Name == ITEM_DESCRIPTOR_CONTAINER )
312 aProp[i].Value >>= rSubMenu;
314 else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
316 aProp[i].Value >>= rStyle;
318 else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
320 aProp[i].Value >>= rLabel;
322 else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
324 aProp[i].Value >>= rType;
328 return true;
331 catch ( css::lang::IndexOutOfBoundsException& )
335 return false;
338 bool SvxConfigPageHelper::GetToolbarItemData(
339 const css::uno::Reference< css::container::XIndexAccess >& rItemContainer,
340 sal_Int32 nIndex,
341 OUString& rCommandURL,
342 OUString& rLabel,
343 sal_uInt16& rType,
344 bool& rIsVisible,
345 sal_Int32& rStyle )
349 css::uno::Sequence< css::beans::PropertyValue > aProp;
350 if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
352 for ( sal_Int32 i = 0; i < aProp.getLength(); ++i )
354 if ( aProp[i].Name == ITEM_DESCRIPTOR_COMMANDURL )
356 aProp[i].Value >>= rCommandURL;
358 else if ( aProp[i].Name == ITEM_DESCRIPTOR_STYLE )
360 aProp[i].Value >>= rStyle;
362 else if ( aProp[i].Name == ITEM_DESCRIPTOR_LABEL )
364 aProp[i].Value >>= rLabel;
366 else if ( aProp[i].Name == ITEM_DESCRIPTOR_TYPE )
368 aProp[i].Value >>= rType;
370 else if ( aProp[i].Name == ITEM_DESCRIPTOR_ISVISIBLE )
372 aProp[i].Value >>= rIsVisible;
376 return true;
379 catch ( css::lang::IndexOutOfBoundsException& )
383 return false;
386 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertSvxConfigEntry(
387 const SvxConfigEntry* pEntry )
389 css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 4 );
391 aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
392 aPropSeq[0].Value <<= pEntry->GetCommand();
394 aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
395 aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
397 // If the name has not been changed, then the label can be stored
398 // as an empty string.
399 // It will be initialised again later using the command to label map.
400 aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
401 if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
403 aPropSeq[2].Value <<= OUString();
405 else
407 aPropSeq[2].Value <<= pEntry->GetName();
410 aPropSeq[3].Name = ITEM_DESCRIPTOR_STYLE;
411 aPropSeq[3].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
413 return aPropSeq;
416 css::uno::Sequence< css::beans::PropertyValue > SvxConfigPageHelper::ConvertToolbarEntry(
417 const SvxConfigEntry* pEntry )
419 css::uno::Sequence< css::beans::PropertyValue > aPropSeq( 5 );
421 aPropSeq[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
422 aPropSeq[0].Value <<= pEntry->GetCommand();
424 aPropSeq[1].Name = ITEM_DESCRIPTOR_TYPE;
425 aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
427 // If the name has not been changed, then the label can be stored
428 // as an empty string.
429 // It will be initialised again later using the command to label map.
430 aPropSeq[2].Name = ITEM_DESCRIPTOR_LABEL;
431 if ( !pEntry->HasChangedName() && !pEntry->GetCommand().isEmpty() )
433 aPropSeq[2].Value <<= OUString();
435 else
437 aPropSeq[2].Value <<= pEntry->GetName();
440 aPropSeq[3].Name = ITEM_DESCRIPTOR_ISVISIBLE;
441 aPropSeq[3].Value <<= pEntry->IsVisible();
443 aPropSeq[4].Name = ITEM_DESCRIPTOR_STYLE;
444 aPropSeq[4].Value <<= static_cast<sal_Int16>(pEntry->GetStyle());
446 return aPropSeq;
449 bool SvxConfigPageHelper::showKeyConfigTabPage(
450 const css::uno::Reference< css::frame::XFrame >& xFrame )
452 if (!xFrame.is())
454 return false;
456 OUString sModuleId(
457 css::frame::ModuleManager::create(
458 comphelper::getProcessComponentContext())
459 ->identify(xFrame));
460 return !sModuleId.isEmpty()
461 && sModuleId != "com.sun.star.frame.StartModule";
464 bool SvxConfigPageHelper::EntrySort( SvxConfigEntry const * a, SvxConfigEntry const * b )
466 return a->GetName().compareTo( b->GetName() ) < 0;
469 bool SvxConfigPageHelper::SvxConfigEntryModified( SvxConfigEntry const * pEntry )
471 SvxEntries* pEntries = pEntry->GetEntries();
472 if ( !pEntries )
473 return false;
475 for ( const auto& entry : *pEntries )
477 if ( entry->IsModified() || SvxConfigEntryModified( entry ) )
478 return true;
480 return false;
483 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */