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 .
21 #include "optimizerdialog.hxx"
24 #include "pppoptimizer.hxx"
25 #include "graphiccollector.hxx"
26 #include "pagecollector.hxx"
27 #include <com/sun/star/awt/PushButtonType.hpp>
28 #include <com/sun/star/awt/XSpinField.hpp>
29 #include <com/sun/star/awt/XTextComponent.hpp>
30 #include <com/sun/star/presentation/XCustomPresentationSupplier.hpp>
31 #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
32 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
33 #include <com/sun/star/awt/FontDescriptor.hpp>
34 #include <com/sun/star/awt/FontWeight.hpp>
35 #include <com/sun/star/frame/XStorable.hpp>
36 #include <rtl/ustrbuf.hxx>
37 #include <sal/macros.h>
38 #include <o3tl/string_view.hxx>
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::frame
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::drawing
;
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::presentation
;
47 void OptimizerDialog::InitNavigationBar()
52 void OptimizerDialog::UpdateControlStatesPage0()
54 int nSelectedItem
= -1;
55 std::vector
<OUString
> aItemList
;
56 const std::vector
< OptimizerSettings
>& rList( GetOptimizerSettings() );
57 if ( rList
.size() > 1 ) // the first session in the list is the actual one -> skipping first one
59 for ( std::vector
<OptimizerSettings
>::size_type i
= 1; i
< rList
.size(); i
++ )
61 aItemList
.push_back(rList
[i
].maName
);
62 if ( nSelectedItem
< 0 )
64 if ( rList
[ i
] == rList
[ 0 ] )
65 nSelectedItem
= static_cast< short >( i
- 1 );
69 bool bRemoveButtonEnabled
= false;
70 if ( nSelectedItem
>= 0 )
72 if ( nSelectedItem
> 2 ) // only allowing to delete custom themes, the first can|t be deleted
73 bRemoveButtonEnabled
= true;
75 mpPage0
->UpdateControlStates(aItemList
, nSelectedItem
, bRemoveButtonEnabled
);
78 void OptimizerDialog::InitPage0()
80 UpdateControlStatesPage0();
83 void OptimizerDialog::UpdateControlStatesPage1()
85 bool bDeleteUnusedMasterPages( GetConfigProperty( TK_DeleteUnusedMasterPages
, false ) );
86 bool bDeleteHiddenSlides( GetConfigProperty( TK_DeleteHiddenSlides
, false ) );
87 bool bDeleteNotesPages( GetConfigProperty( TK_DeleteNotesPages
, false ) );
88 mpPage1
->UpdateControlStates(bDeleteUnusedMasterPages
, bDeleteHiddenSlides
, bDeleteNotesPages
);
91 void OptimizerDialog::InitPage1()
93 Sequence
< OUString
> aCustomShowList
;
94 Reference
< XModel
> xModel( mxController
->getModel() );
97 Reference
< XCustomPresentationSupplier
> aXCPSup( xModel
, UNO_QUERY_THROW
);
98 Reference
< XNameContainer
> aXCont( aXCPSup
->getCustomPresentations() );
100 aCustomShowList
= aXCont
->getElementNames();
102 mpPage1
->Init(aCustomShowList
);
104 UpdateControlStatesPage1();
107 void OptimizerDialog::UpdateControlStatesPage2()
109 bool bJPEGCompression( GetConfigProperty( TK_JPEGCompression
, false ) );
110 bool bRemoveCropArea( GetConfigProperty( TK_RemoveCropArea
, false ) );
111 bool bEmbedLinkedGraphics( GetConfigProperty( TK_EmbedLinkedGraphics
, true ) );
112 sal_Int32
nJPEGQuality( GetConfigProperty( TK_JPEGQuality
, sal_Int32(90) ) );
113 sal_Int32
nImageResolution( GetConfigProperty( TK_ImageResolution
, sal_Int32(0) ) );
115 mpPage2
->UpdateControlStates(bJPEGCompression
, nJPEGQuality
, bRemoveCropArea
, nImageResolution
, bEmbedLinkedGraphics
);
118 void OptimizerDialog::InitPage2()
120 UpdateControlStatesPage2();
123 void OptimizerDialog::UpdateControlStatesPage3()
125 bool bConvertOLEObjects( GetConfigProperty( TK_OLEOptimization
, false ) );
126 sal_Int16
nOLEOptimizationType( GetConfigProperty( TK_OLEOptimizationType
, sal_Int16(0) ) );
128 mpPage3
->UpdateControlStates(bConvertOLEObjects
, nOLEOptimizationType
);
131 void OptimizerDialog::InitPage3()
134 Reference
< XModel
> xModel( mxController
->getModel() );
135 Reference
< XDrawPagesSupplier
> xDrawPagesSupplier( xModel
, UNO_QUERY_THROW
);
136 Reference
< XDrawPages
> xDrawPages( xDrawPagesSupplier
->getDrawPages(), UNO_SET_THROW
);
137 for ( sal_Int32 i
= 0; i
< xDrawPages
->getCount(); i
++ )
139 Reference
< XShapes
> xShapes( xDrawPages
->getByIndex( i
), UNO_QUERY_THROW
);
140 for ( sal_Int32 j
= 0; j
< xShapes
->getCount(); j
++ )
142 Reference
< XShape
> xShape( xShapes
->getByIndex( j
), UNO_QUERY_THROW
);
143 if ( xShape
->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
148 mpPage3
->Init(nOLECount
? getString(STR_OLE_OBJECTS_DESC
) : getString(STR_NO_OLE_OBJECTS_DESC
));
150 UpdateControlStatesPage3();
153 static OUString
ImpValueOfInMB( sal_Int64 rVal
, sal_Unicode nSeparator
)
155 double fVal( static_cast<double>( rVal
) );
158 OUStringBuffer
aVal( OUString::number( fVal
) );
159 sal_Int32
nX( aVal
.indexOf( '.' ) );
162 aVal
.setLength( nX
+ 2 );
163 aVal
[nX
] = nSeparator
;
165 aVal
.append( " MB" );
166 return aVal
.makeStringAndClear();
169 void OptimizerDialog::UpdateControlStatesPage4()
171 bool bSaveAs( GetConfigProperty( TK_SaveAs
, true ) );
175 std::vector
<OUString
> aItemList
;
176 const std::vector
< OptimizerSettings
>& rList( GetOptimizerSettings() );
177 if ( rList
.size() > 1 ) // the first session in the list is the actual one -> skipping first one
179 for ( std::vector
<OptimizerSettings
>::size_type w
= 1; w
< rList
.size(); w
++ )
180 aItemList
.push_back(rList
[ w
].maName
);
183 // now check if it is sensible to enable the combo box
184 bool bSaveSettingsEnabled
= true;
185 if ( rList
.size() > 1 ) // the first session in the list is the actual one -> skipping first one
187 for ( std::vector
<OptimizerSettings
>::size_type w
= 1; w
< rList
.size(); w
++ )
189 if ( rList
[ w
] == rList
[ 0 ] )
191 bSaveSettingsEnabled
= false;
197 std::vector
< OUString
> aSummaryStrings
;
199 // taking care of deleted slides
200 sal_Int32 nDeletedSlides
= 0;
201 OUString
sTKCustomShowName(mpPage1
->Get_TK_CustomShowName());
202 if (!sTKCustomShowName
.isEmpty())
203 SetConfigProperty(TK_CustomShowName
, Any(sTKCustomShowName
));
204 if ( GetConfigProperty( TK_DeleteHiddenSlides
, false ) )
206 Reference
< XDrawPagesSupplier
> xDrawPagesSupplier( mxController
->getModel(), UNO_QUERY_THROW
);
207 Reference
< XDrawPages
> xDrawPages( xDrawPagesSupplier
->getDrawPages(), UNO_SET_THROW
);
208 for( sal_Int32 i
= 0; i
< xDrawPages
->getCount(); i
++ )
210 Reference
< XDrawPage
> xDrawPage( xDrawPages
->getByIndex( i
), UNO_QUERY_THROW
);
211 Reference
< XPropertySet
> xPropSet( xDrawPage
, UNO_QUERY_THROW
);
213 bool bVisible
= true;
214 if ( xPropSet
->getPropertyValue( u
"Visible"_ustr
) >>= bVisible
)
221 if ( GetConfigProperty( TK_DeleteUnusedMasterPages
, false ) )
223 std::vector
< PageCollector::MasterPageEntity
> aMasterPageList
;
224 PageCollector::CollectMasterPages( mxController
->getModel(), aMasterPageList
);
225 Reference
< XMasterPagesSupplier
> xMasterPagesSupplier( mxController
->getModel(), UNO_QUERY_THROW
);
226 Reference
< XDrawPages
> xMasterPages( xMasterPagesSupplier
->getMasterPages(), UNO_SET_THROW
);
227 nDeletedSlides
+= std::count_if(aMasterPageList
.begin(), aMasterPageList
.end(),
228 [](const PageCollector::MasterPageEntity
& rEntity
) { return !rEntity
.bUsed
; });
230 if ( nDeletedSlides
> 1 )
232 OUString
aStr( getString( STR_DELETE_SLIDES
) );
233 OUString
aPlaceholder( u
"%SLIDES"_ustr
);
234 sal_Int32 i
= aStr
.indexOf( aPlaceholder
);
236 aStr
= aStr
.replaceAt( i
, aPlaceholder
.getLength(), OUString::number( nDeletedSlides
) );
237 aSummaryStrings
.push_back( aStr
);
240 // generating graphic compression info
241 sal_Int32 nGraphics
= 0;
242 bool bJPEGCompression( GetConfigProperty( TK_JPEGCompression
, false ) );
243 sal_Int32
nJPEGQuality( GetConfigProperty( TK_JPEGQuality
, sal_Int32(90) ) );
244 sal_Int32
nImageResolution( GetConfigProperty( TK_ImageResolution
, sal_Int32(0) ) );
245 GraphicSettings
aGraphicSettings( bJPEGCompression
, nJPEGQuality
, GetConfigProperty( TK_RemoveCropArea
, false ),
246 nImageResolution
, GetConfigProperty( TK_EmbedLinkedGraphics
, true ) );
247 GraphicCollector::CountGraphics( mxContext
, mxController
->getModel(), aGraphicSettings
, nGraphics
);
250 OUString
aStr( getString( STR_OPTIMIZE_IMAGES
) );
251 OUString
aImagePlaceholder( u
"%IMAGES"_ustr
);
252 OUString
aQualityPlaceholder( u
"%QUALITY"_ustr
);
253 OUString
aResolutionPlaceholder( u
"%RESOLUTION"_ustr
);
254 sal_Int32 i
= aStr
.indexOf( aImagePlaceholder
);
256 aStr
= aStr
.replaceAt( i
, aImagePlaceholder
.getLength(), OUString::number( nGraphics
) );
258 sal_Int32 j
= aStr
.indexOf( aQualityPlaceholder
);
260 aStr
= aStr
.replaceAt( j
, aQualityPlaceholder
.getLength(), OUString::number( nJPEGQuality
) );
262 sal_Int32 k
= aStr
.indexOf( aResolutionPlaceholder
);
264 aStr
= aStr
.replaceAt( k
, aResolutionPlaceholder
.getLength(), OUString::number( nImageResolution
) );
266 aSummaryStrings
.push_back( aStr
);
269 if ( GetConfigProperty( TK_OLEOptimization
, false ) )
271 sal_Int32 nOLEReplacements
= 0;
272 Reference
< XDrawPagesSupplier
> xDrawPagesSupplier( mxController
->getModel(), UNO_QUERY_THROW
);
273 Reference
< XDrawPages
> xDrawPages( xDrawPagesSupplier
->getDrawPages(), UNO_SET_THROW
);
274 for ( sal_Int32 i
= 0; i
< xDrawPages
->getCount(); i
++ )
276 Reference
< XShapes
> xShapes( xDrawPages
->getByIndex( i
), UNO_QUERY_THROW
);
277 for ( sal_Int32 j
= 0; j
< xShapes
->getCount(); j
++ )
279 Reference
< XShape
> xShape( xShapes
->getByIndex( j
), UNO_QUERY_THROW
);
280 if ( xShape
->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
284 if ( nOLEReplacements
> 1 )
286 OUString
aStr( getString( STR_CREATE_REPLACEMENT
) );
287 OUString
aPlaceholder( u
"%OLE"_ustr
);
288 sal_Int32 i
= aStr
.indexOf( aPlaceholder
);
290 aStr
= aStr
.replaceAt( i
, aPlaceholder
.getLength(), OUString::number( nOLEReplacements
) );
291 aSummaryStrings
.push_back( aStr
);
294 while( aSummaryStrings
.size() < 3 )
295 aSummaryStrings
.emplace_back( );
297 sal_Int64 nCurrentFileSize
= 0;
298 sal_Int64 nEstimatedFileSize
= 0;
299 Reference
< XStorable
> xStorable( mxController
->getModel(), UNO_QUERY
);
300 if ( xStorable
.is() && xStorable
->hasLocation() )
301 nCurrentFileSize
= PPPOptimizer::GetFileSize( xStorable
->getLocation() );
303 if ( nCurrentFileSize
)
305 double fE
= static_cast< double >( nCurrentFileSize
);
306 if ( nImageResolution
)
308 double v
= ( static_cast< double >( nImageResolution
) + 75.0 ) / 300.0;
312 if ( bJPEGCompression
)
314 double v
= 0.75 - ( ( 100.0 - static_cast< double >( nJPEGQuality
) ) / 400.0 ) ;
317 nEstimatedFileSize
= static_cast< sal_Int64
>( fE
);
319 sal_Unicode nSeparator
= '.';
320 OUString
aStr( getString( STR_FILESIZESEPARATOR
) );
321 if ( !aStr
.isEmpty() )
322 nSeparator
= aStr
[ 0 ];
323 mpPage4
->UpdateControlStates(bSaveAs
, bSaveSettingsEnabled
, aItemList
,
325 ImpValueOfInMB(nCurrentFileSize
, nSeparator
),
326 ImpValueOfInMB(nEstimatedFileSize
, nSeparator
));
327 SetConfigProperty( TK_EstimatedFileSize
, Any( nEstimatedFileSize
) );
330 void OptimizerDialog::InitPage4()
332 // creating a default session name that hasn't been used yet
333 OUString aSettingsName
;
334 OUString
aDefault( getString( STR_MY_SETTINGS
) );
335 sal_Int32 nSession
= 1;
336 std::vector
<OptimizerSettings
>::size_type i
;
337 const std::vector
< OptimizerSettings
>& rList( GetOptimizerSettings() );
340 OUString aTemp
= aDefault
+ OUString::number( nSession
++ );
341 for ( i
= 1; i
< rList
.size(); i
++ )
343 if ( rList
[ i
].maName
== aTemp
)
346 if ( i
== rList
.size() )
347 aSettingsName
= aTemp
;
349 while( aSettingsName
.isEmpty() );
351 mpPage4
->Init(aSettingsName
, mbIsReadonly
);
353 UpdateControlStatesPage4();
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */