1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
);
51 OUString
SvxConfigPageHelper::replaceSaveInName( const OUString
& rMessage
, const OUString
& rSaveInName
)
53 const OUString
placeholder("%SAVE IN SELECTION%" );
55 OUString name
= rMessage
.replaceFirst(placeholder
, rSaveInName
);
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()
75 void SvxConfigPageHelper::InitImageType()
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
};
107 xImageManager
->getImages( GetImageType(), aImageCmdSeq
);
109 if ( aGraphicSeq
.hasElements() )
111 result
= aGraphicSeq
[0];
114 catch ( css::uno::Exception
& )
116 // will return empty XGraphic
124 SvxConfigPageHelper::generateCustomName(
125 const OUString
& prefix
,
127 sal_Int32 suffix
/*= 1*/ )
132 // find and replace the %n placeholder in the prefix string
133 name
= prefix
.replaceFirst( "%n", OUString::number( suffix
), &pos
);
137 // no placeholder found so just append the suffix
138 name
+= OUString::number( suffix
);
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()) )
157 // name already exists so try the next number up
158 return generateCustomName( prefix
, entries
, ++suffix
);
164 OUString
SvxConfigPageHelper::generateCustomMenuURL(
166 sal_Int32 suffix
/*= 1*/ )
168 OUString url
= "vnd.openoffice.org:CustomMenu" + OUString::number( suffix
);
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()) )
185 // url already exists so try the next number up
186 return generateCustomMenuURL( entries
, ++suffix
);
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()) )
216 // url already exists so try the next number up
217 return generateCustomURL( entries
);
223 OUString
SvxConfigPageHelper::GetModuleName( const OUString
& aModuleId
)
225 if ( aModuleId
== "com.sun.star.text.TextDocument" ||
226 aModuleId
== "com.sun.star.text.GlobalDocument" )
228 else if ( aModuleId
== "com.sun.star.text.WebDocument" )
230 else if ( aModuleId
== "com.sun.star.drawing.DrawingDocument" )
232 else if ( aModuleId
== "com.sun.star.presentation.PresentationDocument" )
234 else if ( aModuleId
== "com.sun.star.sheet.SpreadsheetDocument" )
236 else if ( aModuleId
== "com.sun.star.script.BasicIDE" )
238 else if ( aModuleId
== "com.sun.star.formula.FormulaProperties" )
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" )
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
;
269 for ( sal_Int32 i
= 0; i
< aSeq
.getLength(); ++i
)
271 if ( aSeq
[i
].Name
== "ooSetupFactoryUIName" )
273 aSeq
[i
].Value
>>= aModuleUIName
;
279 catch ( css::uno::RuntimeException
& )
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
,
296 OUString
& rCommandURL
,
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
;
334 catch ( css::lang::IndexOutOfBoundsException
& )
341 bool SvxConfigPageHelper::GetToolbarItemData(
342 const css::uno::Reference
< css::container::XIndexAccess
>& rItemContainer
,
344 OUString
& rCommandURL
,
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
;
382 catch ( css::lang::IndexOutOfBoundsException
& )
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();
410 aPropSeq
[2].Value
<<= pEntry
->GetName();
413 aPropSeq
[3].Name
= ITEM_DESCRIPTOR_STYLE
;
414 aPropSeq
[3].Value
<<= static_cast<sal_Int16
>(pEntry
->GetStyle());
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();
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());
452 bool SvxConfigPageHelper::EntrySort( SvxConfigEntry
const * a
, SvxConfigEntry
const * b
)
454 return a
->GetName().compareTo( b
->GetName() ) < 0;
457 bool SvxConfigPageHelper::SvxConfigEntryModified( SvxConfigEntry
const * pEntry
)
459 SvxEntries
* pEntries
= pEntry
->GetEntries();
463 for ( const auto& entry
: *pEntries
)
465 if ( entry
->IsModified() || SvxConfigEntryModified( entry
) )
471 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */