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
21 #include "optimizerdialog.hxx"
22
23
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>
39
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;
46
InitNavigationBar()47 void OptimizerDialog::InitNavigationBar()
48 {
49 m_xHelp->hide();
50 }
51
UpdateControlStatesPage0()52 void OptimizerDialog::UpdateControlStatesPage0()
53 {
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
58 {
59 for ( std::vector<OptimizerSettings>::size_type i = 1; i < rList.size(); i++ )
60 {
61 aItemList.push_back(rList[i].maName);
62 if ( nSelectedItem < 0 )
63 {
64 if ( rList[ i ] == rList[ 0 ] )
65 nSelectedItem = static_cast< short >( i - 1 );
66 }
67 }
68 }
69 bool bRemoveButtonEnabled = false;
70 if ( nSelectedItem >= 0 )
71 {
72 if ( nSelectedItem > 2 ) // only allowing to delete custom themes, the first can|t be deleted
73 bRemoveButtonEnabled = true;
74 }
75 mpPage0->UpdateControlStates(aItemList, nSelectedItem, bRemoveButtonEnabled);
76 }
77
InitPage0()78 void OptimizerDialog::InitPage0()
79 {
80 UpdateControlStatesPage0();
81 }
82
UpdateControlStatesPage1()83 void OptimizerDialog::UpdateControlStatesPage1()
84 {
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);
89 }
90
InitPage1()91 void OptimizerDialog::InitPage1()
92 {
93 Sequence< OUString > aCustomShowList;
94 Reference< XModel > xModel( mxController->getModel() );
95 if ( xModel.is() )
96 {
97 Reference< XCustomPresentationSupplier > aXCPSup( xModel, UNO_QUERY_THROW );
98 Reference< XNameContainer > aXCont( aXCPSup->getCustomPresentations() );
99 if ( aXCont.is() )
100 aCustomShowList = aXCont->getElementNames();
101 }
102 mpPage1->Init(aCustomShowList);
103
104 UpdateControlStatesPage1();
105 }
106
UpdateControlStatesPage2()107 void OptimizerDialog::UpdateControlStatesPage2()
108 {
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) ) );
114
115 mpPage2->UpdateControlStates(bJPEGCompression, nJPEGQuality, bRemoveCropArea, nImageResolution, bEmbedLinkedGraphics);
116 }
117
InitPage2()118 void OptimizerDialog::InitPage2()
119 {
120 UpdateControlStatesPage2();
121 }
122
UpdateControlStatesPage3()123 void OptimizerDialog::UpdateControlStatesPage3()
124 {
125 bool bConvertOLEObjects( GetConfigProperty( TK_OLEOptimization, false ) );
126 sal_Int16 nOLEOptimizationType( GetConfigProperty( TK_OLEOptimizationType, sal_Int16(0) ) );
127
128 mpPage3->UpdateControlStates(bConvertOLEObjects, nOLEOptimizationType);
129 }
130
InitPage3()131 void OptimizerDialog::InitPage3()
132 {
133 int nOLECount = 0;
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++ )
138 {
139 Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
140 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
141 {
142 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
143 if ( xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
144 nOLECount++;
145 }
146 }
147
148 mpPage3->Init(nOLECount ? getString(STR_OLE_OBJECTS_DESC) : getString(STR_NO_OLE_OBJECTS_DESC));
149
150 UpdateControlStatesPage3();
151 }
152
ImpValueOfInMB(sal_Int64 rVal,sal_Unicode nSeparator)153 static OUString ImpValueOfInMB( sal_Int64 rVal, sal_Unicode nSeparator )
154 {
155 double fVal( static_cast<double>( rVal ) );
156 fVal /= ( 1 << 20 );
157 fVal += 0.05;
158 OUStringBuffer aVal( OUString::number( fVal ) );
159 sal_Int32 nX( aVal.indexOf( '.' ) );
160 if ( nX >= 0 )
161 {
162 aVal.setLength( nX + 2 );
163 aVal[nX] = nSeparator;
164 }
165 aVal.append( " MB" );
166 return aVal.makeStringAndClear();
167 }
168
UpdateControlStatesPage4()169 void OptimizerDialog::UpdateControlStatesPage4()
170 {
171 bool bSaveAs( GetConfigProperty( TK_SaveAs, true ) );
172 if ( mbIsReadonly )
173 bSaveAs = true;
174
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
178 {
179 for ( std::vector<OptimizerSettings>::size_type w = 1; w < rList.size(); w++ )
180 aItemList.push_back(rList[ w ].maName);
181 }
182
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
186 {
187 for ( std::vector<OptimizerSettings>::size_type w = 1; w < rList.size(); w++ )
188 {
189 if ( rList[ w ] == rList[ 0 ] )
190 {
191 bSaveSettingsEnabled = false;
192 break;
193 }
194 }
195 }
196
197 std::vector< OUString > aSummaryStrings;
198
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 ) )
205 {
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++ )
209 {
210 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
211 Reference< XPropertySet > xPropSet( xDrawPage, UNO_QUERY_THROW );
212
213 bool bVisible = true;
214 if ( xPropSet->getPropertyValue( u"Visible"_ustr ) >>= bVisible )
215 {
216 if (!bVisible )
217 nDeletedSlides++;
218 }
219 }
220 }
221 if ( GetConfigProperty( TK_DeleteUnusedMasterPages, false ) )
222 {
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; });
229 }
230 if ( nDeletedSlides > 1 )
231 {
232 OUString aStr( getString( STR_DELETE_SLIDES ) );
233 OUString aPlaceholder( u"%SLIDES"_ustr );
234 sal_Int32 i = aStr.indexOf( aPlaceholder );
235 if ( i >= 0 )
236 aStr = aStr.replaceAt( i, aPlaceholder.getLength(), OUString::number( nDeletedSlides ) );
237 aSummaryStrings.push_back( aStr );
238 }
239
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 );
248 if ( nGraphics > 1 )
249 {
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 );
255 if ( i >= 0 )
256 aStr = aStr.replaceAt( i, aImagePlaceholder.getLength(), OUString::number( nGraphics ) );
257
258 sal_Int32 j = aStr.indexOf( aQualityPlaceholder );
259 if ( j >= 0 )
260 aStr = aStr.replaceAt( j, aQualityPlaceholder.getLength(), OUString::number( nJPEGQuality ) );
261
262 sal_Int32 k = aStr.indexOf( aResolutionPlaceholder );
263 if ( k >= 0 )
264 aStr = aStr.replaceAt( k, aResolutionPlaceholder.getLength(), OUString::number( nImageResolution ) );
265
266 aSummaryStrings.push_back( aStr );
267 }
268
269 if ( GetConfigProperty( TK_OLEOptimization, false ) )
270 {
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++ )
275 {
276 Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
277 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
278 {
279 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
280 if ( xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
281 nOLEReplacements++;
282 }
283 }
284 if ( nOLEReplacements > 1 )
285 {
286 OUString aStr( getString( STR_CREATE_REPLACEMENT ) );
287 OUString aPlaceholder( u"%OLE"_ustr );
288 sal_Int32 i = aStr.indexOf( aPlaceholder );
289 if ( i >= 0 )
290 aStr = aStr.replaceAt( i, aPlaceholder.getLength(), OUString::number( nOLEReplacements ) );
291 aSummaryStrings.push_back( aStr );
292 }
293 }
294 while( aSummaryStrings.size() < 3 )
295 aSummaryStrings.emplace_back( );
296
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() );
302
303 if ( nCurrentFileSize )
304 {
305 double fE = static_cast< double >( nCurrentFileSize );
306 if ( nImageResolution )
307 {
308 double v = ( static_cast< double >( nImageResolution ) + 75.0 ) / 300.0;
309 if ( v < 1.0 )
310 fE *= v;
311 }
312 if ( bJPEGCompression )
313 {
314 double v = 0.75 - ( ( 100.0 - static_cast< double >( nJPEGQuality ) ) / 400.0 ) ;
315 fE *= v;
316 }
317 nEstimatedFileSize = static_cast< sal_Int64 >( fE );
318 }
319 sal_Unicode nSeparator = '.';
320 OUString aStr( getString( STR_FILESIZESEPARATOR ) );
321 if ( !aStr.isEmpty() )
322 nSeparator = aStr[ 0 ];
323 mpPage4->UpdateControlStates(bSaveAs, bSaveSettingsEnabled, aItemList,
324 aSummaryStrings,
325 ImpValueOfInMB(nCurrentFileSize, nSeparator),
326 ImpValueOfInMB(nEstimatedFileSize, nSeparator));
327 SetConfigProperty( TK_EstimatedFileSize, Any( nEstimatedFileSize ) );
328 }
329
InitPage4()330 void OptimizerDialog::InitPage4()
331 {
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() );
338 do
339 {
340 OUString aTemp = aDefault + OUString::number( nSession++ );
341 for ( i = 1; i < rList.size(); i++ )
342 {
343 if ( rList[ i ].maName == aTemp )
344 break;
345 }
346 if ( i == rList.size() )
347 aSettingsName = aTemp;
348 }
349 while( aSettingsName.isEmpty() );
350
351 mpPage4->Init(aSettingsName, mbIsReadonly);
352
353 UpdateControlStatesPage4();
354 }
355
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
357