xref: /AOO41X/main/sdext/source/minimizer/impoptimizer.cxx (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "impoptimizer.hxx"
28 #include "pppoptimizer.hxx"
29 #include "graphiccollector.hxx"
30 #include "pagecollector.hxx"
31 #include "informationdialog.hxx"
32 
33 #include "minimizer.hrc"
34 
35 #include <vector>
36 #include "com/sun/star/util/URL.hpp"
37 #include "com/sun/star/util/XURLTransformer.hpp"
38 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <com/sun/star/awt/Rectangle.hpp>
40 #include <com/sun/star/awt/Size.hpp>
41 #include <com/sun/star/util/MeasureUnit.hpp>
42 #include <com/sun/star/frame/XModel.hpp>
43 #include <com/sun/star/frame/XDesktop.hpp>
44 #include <com/sun/star/awt/XWindow.hpp>
45 #include <com/sun/star/frame/XStorable.hpp>
46 #include <com/sun/star/frame/FrameSearchFlag.hpp>
47 #include <com/sun/star/frame/XDispatchProvider.hpp>
48 #include <com/sun/star/graphic/XGraphicProvider.hpp>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
50 #include <com/sun/star/container/XNamed.hpp>
51 #include <com/sun/star/drawing/XShapes.hpp>
52 #include <com/sun/star/drawing/XMasterPageTarget.hpp>
53 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
54 #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
55 #include <com/sun/star/presentation/XPresentationSupplier.hpp>
56 #include <com/sun/star/container/XNameAccess.hpp>
57 #include <com/sun/star/presentation/XPresentation.hpp>
58 #include <com/sun/star/presentation/XPresentationPage.hpp>
59 #include <com/sun/star/document/XFilter.hpp>
60 #include <com/sun/star/document/XExporter.hpp>
61 #include <com/sun/star/uno/RuntimeException.hpp>
62 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
63 #include <com/sun/star/graphic/XGraphicProvider.hpp>
64 #include <com/sun/star/graphic/GraphicType.hpp>
65 #include <com/sun/star/io/XStream.hpp>
66 #include <com/sun/star/io/XSeekable.hpp>
67 #include <com/sun/star/frame/XComponentLoader.hpp>
68 #include <com/sun/star/util/URL.hpp>
69 
70 using namespace ::std;
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::io;
73 using namespace ::com::sun::star::awt;
74 using namespace ::com::sun::star::uno;
75 using namespace ::com::sun::star::lang;
76 using namespace ::com::sun::star::util;
77 using namespace ::com::sun::star::frame;
78 using namespace ::com::sun::star::beans;
79 using namespace ::com::sun::star::drawing;
80 using namespace ::com::sun::star::graphic;
81 using namespace ::com::sun::star::document;
82 using namespace ::com::sun::star::container;
83 using namespace ::com::sun::star::presentation;
84 
85 using ::rtl::OUString;
86 
ImpExtractCustomShow(const Reference<XModel> & rxModel,const OUString & rCustomShowName)87 void ImpExtractCustomShow( const Reference< XModel >& rxModel, const OUString& rCustomShowName )
88 {
89     vector< Reference< XDrawPage > > vNonUsedPageList;
90     try
91     {
92         PageCollector::CollectNonCustomShowPages( rxModel, rCustomShowName, vNonUsedPageList );
93         Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
94         Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
95         vector< Reference< XDrawPage > >::iterator aIter( vNonUsedPageList.begin() );
96         while( aIter != vNonUsedPageList.end() )
97             xDrawPages->remove( *aIter++ );
98     }
99     catch( Exception& )
100     {
101 
102     }
103 }
104 
ImpDeleteUnusedMasterPages(const Reference<XModel> & rxModel)105 void ImpDeleteUnusedMasterPages( const Reference< XModel >& rxModel )
106 {
107     vector< PageCollector::MasterPageEntity > aMasterPageList;
108     PageCollector::CollectMasterPages( rxModel, aMasterPageList );
109 
110     // now master pages that are not marked can be deleted
111     Reference< XMasterPagesSupplier > xMasterPagesSupplier( rxModel, UNO_QUERY_THROW );
112     Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_QUERY_THROW );
113     vector< PageCollector::MasterPageEntity >::iterator aIter( aMasterPageList.begin() );
114     while( aIter != aMasterPageList.end() )
115     {
116         if ( !aIter->bUsed )
117             xMasterPages->remove( aIter->xMasterPage );
118         aIter++;
119     }
120 }
121 
ImpDeleteHiddenSlides(const Reference<XModel> & rxModel)122 void ImpDeleteHiddenSlides(  const Reference< XModel >& rxModel )
123 {
124     try
125     {
126         Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
127         Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
128         for( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
129         {
130             Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
131             Reference< XPropertySet > xPropSet( xDrawPage, UNO_QUERY_THROW );
132 
133             sal_Bool bVisible = sal_True;
134             const OUString sVisible( RTL_CONSTASCII_USTRINGPARAM( "Visible" ) );
135             if ( xPropSet->getPropertyValue( sVisible ) >>= bVisible )
136             {
137                 if (!bVisible )
138                 {
139                     xDrawPages->remove( xDrawPage );
140                     i--;
141                 }
142             }
143         }
144     }
145     catch( Exception& )
146     {
147     }
148 }
149 
ImpDeleteNotesPages(const Reference<XModel> & rxModel)150 void ImpDeleteNotesPages( const Reference< XModel >& rxModel )
151 {
152     try
153     {
154         Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
155         Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
156         sal_Int32 i, nPages = xDrawPages->getCount();
157         for( i = 0; i < nPages; i++ )
158         {
159             Reference< XPresentationPage > xPresentationPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
160             Reference< XPropertySet > xPropSet( xPresentationPage->getNotesPage(), UNO_QUERY_THROW );
161             Reference< XShapes > xShapes( xPropSet, UNO_QUERY_THROW );
162             while( xShapes->getCount() )
163                 xShapes->remove( Reference< XShape >( xShapes->getByIndex( xShapes->getCount() - 1 ), UNO_QUERY_THROW ) );
164 
165             const OUString sLayout( RTL_CONSTASCII_USTRINGPARAM( "Layout" ) );
166             xPropSet->setPropertyValue( sLayout, Any( (sal_Int16)21 ) );
167         }
168     }
169     catch( Exception& )
170     {
171     }
172 }
173 
ImpConvertOLE(const Reference<XModel> & rxModel,sal_Int32 nOLEOptimizationType)174 void ImpConvertOLE( const Reference< XModel >& rxModel, sal_Int32 nOLEOptimizationType )
175 {
176     try
177     {
178         Reference< XDrawPagesSupplier > xDrawPagesSupplier( rxModel, UNO_QUERY_THROW );
179         Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_QUERY_THROW );
180         for ( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
181         {
182             Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
183             for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
184             {
185                 const OUString sOLE2Shape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.OLE2Shape" ) );
186                 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
187                 if ( xShape->getShapeType() == sOLE2Shape )
188                 {
189                     Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
190 
191                     sal_Bool bConvertOLE = nOLEOptimizationType == 0;
192                     if ( nOLEOptimizationType == 1 )
193                     {
194                         sal_Bool bIsInternal = sal_True;
195                         xPropSet->getPropertyValue( TKGet( TK_IsInternal ) ) >>= bIsInternal;
196                         bConvertOLE = !bIsInternal;
197                     }
198                     if ( bConvertOLE )
199                     {
200                         Reference< XGraphic > xGraphic;
201                         if ( xPropSet->getPropertyValue( TKGet( TK_Graphic ) ) >>= xGraphic )
202                         {
203                             const OUString sGraphicShape( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.GraphicObjectShape" ) );
204                             Reference< XMultiServiceFactory > xFact( rxModel, UNO_QUERY_THROW );
205                             Reference< XShape > xShape2( xFact->createInstance( sGraphicShape ), UNO_QUERY_THROW );
206                             xShapes->add( xShape2 );
207                             xShape2->setPosition( xShape->getPosition() );
208                             xShape2->setSize( xShape->getSize() );
209                             Reference< XPropertySet > xPropSet2( xShape2, UNO_QUERY_THROW );
210                             xPropSet2->setPropertyValue( TKGet( TK_Graphic ), Any( xGraphic ) );
211                             xShapes->remove( xShape );
212                             xPropSet2->setPropertyValue( TKGet( TK_ZOrder ), Any( j ) );
213                         }
214                     }
215                 }
216             }
217         }
218     }
219     catch( Exception& )
220     {
221     }
222 }
223 
ImpCompressGraphic(Reference<XGraphicProvider> & rxGraphicProvider,const Reference<XGraphic> & rxGraphic,Reference<XOutputStream> & rxOutputStream,const OUString & rDestMimeType,const awt::Size & rLogicalSize,sal_Int32 nJPEGQuality,sal_Int32 nImageResolution,sal_Bool bRemoveCropping,const text::GraphicCrop & rGraphicCropLogic)224 void ImpCompressGraphic( Reference< XGraphicProvider >& rxGraphicProvider, const Reference< XGraphic >& rxGraphic, Reference< XOutputStream >& rxOutputStream,
225     const OUString& rDestMimeType, const awt::Size& rLogicalSize, sal_Int32 nJPEGQuality, sal_Int32 nImageResolution, sal_Bool bRemoveCropping, const text::GraphicCrop& rGraphicCropLogic )
226 {
227     try
228     {
229         if ( rxGraphicProvider.is() && rxOutputStream.is() )
230         {
231             Sequence< PropertyValue > aFilterData( 8 );
232             aFilterData[ 0 ].Name = TKGet( TK_ImageResolution );
233             aFilterData[ 0 ].Value <<= nImageResolution;
234             aFilterData[ 1 ].Name = TKGet( TK_ColorMode );      // todo: jpeg color mode (0->true color, 1->greyscale)
235             aFilterData[ 1 ].Value <<= (sal_Int32)0;
236             aFilterData[ 2 ].Name = TKGet( TK_Quality );        // quality that is used if we export to jpeg
237             aFilterData[ 2 ].Value <<= nJPEGQuality;
238             aFilterData[ 3 ].Name = TKGet( TK_Compression );    // compression that is used if we export to png
239             aFilterData[ 3 ].Value <<= (sal_Int32)6;
240             aFilterData[ 4 ].Name = TKGet( TK_Interlaced );     // interlaced is turned off if we export to png
241             aFilterData[ 4 ].Value <<= (sal_Int32)0;
242             aFilterData[ 5 ].Name = TKGet( TK_LogicalSize );
243             aFilterData[ 5 ].Value <<= rLogicalSize;
244             aFilterData[ 6 ].Name = TKGet( TK_RemoveCropArea );
245             aFilterData[ 6 ].Value <<= bRemoveCropping;
246             aFilterData[ 7 ].Name = TKGet( TK_GraphicCropLogic );
247             aFilterData[ 7 ].Value <<= rGraphicCropLogic;
248 
249             Sequence< PropertyValue > aArgs( 3 );
250             aArgs[ 0 ].Name = TKGet( TK_MimeType );             // the GraphicProvider is using "MimeType", the GraphicExporter "MediaType"...
251             aArgs[ 0 ].Value <<= rDestMimeType;
252             aArgs[ 1 ].Name = TKGet( TK_OutputStream );
253             aArgs[ 1 ].Value <<= rxOutputStream;
254             aArgs[ 2 ].Name = TKGet( TK_FilterData );
255             aArgs[ 2 ].Value <<= aFilterData;
256 
257             rxGraphicProvider->storeGraphic( rxGraphic, aArgs );
258         }
259     }
260     catch( Exception& )
261     {
262     }
263 }
264 
ImpCompressGraphic(const Reference<XComponentContext> & rxContext,const Reference<XGraphic> & xGraphic,const awt::Size & aLogicalSize,const text::GraphicCrop & aGraphicCropLogic,const GraphicSettings & rGraphicSettings)265 Reference< XGraphic > ImpCompressGraphic( const Reference< XComponentContext >& rxContext,
266     const Reference< XGraphic >& xGraphic, const awt::Size& aLogicalSize, const text::GraphicCrop& aGraphicCropLogic,
267         const GraphicSettings& rGraphicSettings )
268 {
269     Reference< XGraphic > xNewGraphic;
270     try
271     {
272         OUString aSourceMimeType;
273         Reference< XPropertySet > xGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
274         if ( xGraphicPropertySet->getPropertyValue( TKGet( TK_MimeType ) ) >>= aSourceMimeType )
275         {
276             sal_Int8 nGraphicType( xGraphic->getType() );
277             if ( nGraphicType == com::sun::star::graphic::GraphicType::PIXEL )
278             {
279                 sal_Bool bTransparent = sal_False;
280                 sal_Bool bAlpha       = sal_False;
281                 sal_Bool bAnimated    = sal_False;
282 
283                 awt::Size aSourceSizePixel( 0, 0 );
284                 text::GraphicCrop aGraphicCropPixel( 0, 0, 0, 0 );
285 
286                 if ( ( xGraphicPropertySet->getPropertyValue( TKGet( TK_SizePixel ) ) >>= aSourceSizePixel ) &&
287                     ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Transparent ) ) >>= bTransparent ) &&
288                     ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Alpha ) ) >>= bAlpha ) &&
289                     ( xGraphicPropertySet->getPropertyValue( TKGet( TK_Animated ) ) >>= bAnimated ) )
290                 {
291                     awt::Size aDestSizePixel( aSourceSizePixel );
292                     if ( !bAnimated )
293                     {
294                         sal_Bool bNeedsOptimizing = sal_False;
295                         sal_Bool bRemoveCropArea( rGraphicSettings.mbRemoveCropArea );
296 
297                         // cropping has to be removed from SourceSizePixel
298                         if ( aGraphicCropLogic.Left || aGraphicCropLogic.Top || aGraphicCropLogic.Right || aGraphicCropLogic.Bottom )
299                         {
300                             const awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxContext, xGraphic ) );
301 
302                             if ( bRemoveCropArea )
303                                 bNeedsOptimizing = sal_True;
304 
305                             if ( aSize100thMM.Width && aSize100thMM.Height )
306                             {
307                                 aGraphicCropPixel.Left = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Width * aGraphicCropLogic.Left ) / aSize100thMM.Width );
308                                 aGraphicCropPixel.Top = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Height* aGraphicCropLogic.Top ) / aSize100thMM.Height );
309                                 aGraphicCropPixel.Right = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Width * ( aSize100thMM.Width - aGraphicCropLogic.Right ) ) / aSize100thMM.Width );
310                                 aGraphicCropPixel.Bottom = static_cast< sal_Int32 >( ( (double)aSourceSizePixel.Height* ( aSize100thMM.Height - aGraphicCropLogic.Bottom ) ) / aSize100thMM.Height );
311 
312                                 // first calculating new SourceSizePixel by removing the cropped area
313                                 aSourceSizePixel.Width = aGraphicCropPixel.Right - aGraphicCropPixel.Left;
314                                 aSourceSizePixel.Height= aGraphicCropPixel.Bottom - aGraphicCropPixel.Top;
315                             }
316                             else
317                             {
318                                 bRemoveCropArea = sal_False;
319                             }
320                         }
321                         if ( ( aSourceSizePixel.Width > 0 ) && ( aSourceSizePixel.Height > 0 ) )
322                         {
323                             OUString aDestMimeType( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
324                             if ( rGraphicSettings.mbJPEGCompression && !bTransparent && !bAlpha && !bAnimated )
325                             {
326                                 aDestMimeType = OUString( RTL_CONSTASCII_USTRINGPARAM( "image/jpeg" ) );
327 //                                      if( aSourceMimeType != aDestMimeType )
328                                 bNeedsOptimizing = sal_True;
329                             }
330                             if ( bRemoveCropArea )
331                                 aDestSizePixel = aSourceSizePixel;
332                             if ( rGraphicSettings.mnImageResolution && aLogicalSize.Width && aLogicalSize.Height )
333                             {
334                                 const double fSourceDPIX = ((double)aSourceSizePixel.Width / ((double)aLogicalSize.Width / 2540.0 ));
335                                 const double fSourceDPIY = ((double)aSourceSizePixel.Height/ ((double)aLogicalSize.Height/ 2540.0 ));
336 
337                                 // check, if the bitmap DPI exceeds the maximum DPI
338                                 if( ( fSourceDPIX > rGraphicSettings.mnImageResolution ) || ( fSourceDPIY > rGraphicSettings.mnImageResolution ) )
339                                 {
340                                     const double fNewSizePixelX = ((double)aDestSizePixel.Width * rGraphicSettings.mnImageResolution ) / fSourceDPIX;
341                                     const double fNewSizePixelY = ((double)aDestSizePixel.Height* rGraphicSettings.mnImageResolution ) / fSourceDPIY;
342 
343                                     aDestSizePixel = awt::Size( (sal_Int32)fNewSizePixelX, (sal_Int32)fNewSizePixelY );
344                                     bNeedsOptimizing = sal_True;
345                                 }
346                             }
347                             if ( bNeedsOptimizing && aDestSizePixel.Width && aDestSizePixel.Height )
348                             {
349                                 Reference< XStream > xTempFile( rxContext->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.io.TempFile" ), rxContext ), UNO_QUERY_THROW );
350                                 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
351                                 Reference< XGraphicProvider > xGraphicProvider( rxContext->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.graphic.GraphicProvider" ), rxContext ), UNO_QUERY_THROW );
352 
353                                 ImpCompressGraphic( xGraphicProvider, xGraphic, xOutputStream, aDestMimeType, aLogicalSize, rGraphicSettings.mnJPEGQuality, rGraphicSettings.mnImageResolution, bRemoveCropArea, aGraphicCropLogic );
354                                 Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
355                                 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
356                                 xSeekable->seek( 0 );
357                                 Sequence< PropertyValue > aArgs( 1 );
358                                 aArgs[ 0 ].Name = TKGet( TK_InputStream );
359                                 aArgs[ 0 ].Value <<= xInputStream;
360                                 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
361                             }
362                         }
363                     }
364                 }
365             }
366             else // this is a metafile
367             {
368                 rtl::OUString aDestMimeType( aSourceMimeType );
369                 Reference< XStream > xTempFile( rxContext->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.io.TempFile" ), rxContext ), UNO_QUERY_THROW );
370                 Reference< XOutputStream > xOutputStream( xTempFile->getOutputStream() );
371                 Reference< XGraphicProvider > xGraphicProvider( rxContext->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.graphic.GraphicProvider" ), rxContext ), UNO_QUERY_THROW );
372                 ImpCompressGraphic( xGraphicProvider, xGraphic, xOutputStream, aDestMimeType, aLogicalSize, rGraphicSettings.mnJPEGQuality, rGraphicSettings.mnImageResolution, sal_False, aGraphicCropLogic );
373                 Reference< XInputStream > xInputStream( xTempFile->getInputStream() );
374                 Reference< XSeekable > xSeekable( xInputStream, UNO_QUERY_THROW );
375                 xSeekable->seek( 0 );
376                 Sequence< PropertyValue > aArgs( 1 );
377                 aArgs[ 0 ].Name = TKGet( TK_InputStream );
378                 aArgs[ 0 ].Value <<= xInputStream;
379                 xNewGraphic = xGraphicProvider->queryGraphic( aArgs );
380             }
381         }
382     }
383     catch( Exception& )
384     {
385     }
386     return xNewGraphic;
387 }
388 
CompressGraphics(ImpOptimizer & rOptimizer,const Reference<XComponentContext> & rxContext,const GraphicSettings & rGraphicSettings,std::vector<GraphicCollector::GraphicEntity> & rGraphicList)389 void CompressGraphics( ImpOptimizer& rOptimizer, const Reference< XComponentContext >& rxContext, const GraphicSettings& rGraphicSettings,
390     std::vector< GraphicCollector::GraphicEntity >& rGraphicList )
391 {
392     try
393     {
394         std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIter( rGraphicList.begin() );
395         std::vector< GraphicCollector::GraphicEntity >::iterator aGraphicIEnd( rGraphicList.end() );
396         double i = 0;
397         while( aGraphicIter != aGraphicIEnd )
398         {
399             i++;
400             sal_Int32 nProgress = static_cast< sal_Int32 >( 40.0 * ( i / static_cast< double >( rGraphicList.size() ) ) ) + 50;
401             rOptimizer.SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( nProgress ) ) );
402             rOptimizer.DispatchStatus();
403 
404             if ( aGraphicIter->maUser.size() )
405             {
406                 GraphicSettings aGraphicSettings( rGraphicSettings );
407                 aGraphicSettings.mbRemoveCropArea = aGraphicIter->mbRemoveCropArea;
408 
409                 Reference< XGraphic > xGraphic;
410                 if ( aGraphicIter->maUser[ 0 ].mbFillBitmap && aGraphicIter->maUser[ 0 ].mxPropertySet.is() )
411                 {
412                     Reference< XBitmap > xFillBitmap;
413                     if ( aGraphicIter->maUser[ 0 ].mxPropertySet->getPropertyValue( TKGet( TK_FillBitmap ) ) >>= xFillBitmap )
414                         xGraphic = Reference< XGraphic >( xFillBitmap, UNO_QUERY_THROW );
415                 }
416                 else if ( aGraphicIter->maUser[ 0 ].mxShape.is() )
417                 {
418                     Reference< XPropertySet > xShapePropertySet( aGraphicIter->maUser[ 0 ].mxShape, UNO_QUERY_THROW );
419                     xShapePropertySet->getPropertyValue( TKGet( TK_Graphic ) ) >>= xGraphic;
420                 }
421                 if ( xGraphic.is() )
422                 {
423                     Reference< XPropertySet > xNewGraphicPropertySet( xGraphic, UNO_QUERY_THROW );
424                     awt::Size aSize100thMM( GraphicCollector::GetOriginalSize( rxContext, xGraphic ) );
425                     Reference< XGraphic > xNewGraphic( ImpCompressGraphic( rxContext, xGraphic, aGraphicIter->maLogicalSize, aGraphicIter->maGraphicCropLogic, aGraphicSettings ) );
426                     if ( xNewGraphic.is() )
427                     {
428                         // applying graphic to each user
429                         std::vector< GraphicCollector::GraphicUser >::iterator aGraphicUserIter( aGraphicIter->maUser.begin() );
430                         while( aGraphicUserIter != aGraphicIter->maUser.end() )
431                         {
432                             if ( aGraphicUserIter->mxShape.is() )
433                             {
434                                 rtl::OUString sEmptyGraphicURL;
435                                 Reference< XPropertySet > xShapePropertySet( aGraphicUserIter->mxShape, UNO_QUERY_THROW );
436                                 xShapePropertySet->setPropertyValue( TKGet( TK_GraphicURL ), Any( sEmptyGraphicURL ) );
437                                 xShapePropertySet->setPropertyValue( TKGet( TK_Graphic ), Any( xNewGraphic ) );
438 
439                                 if ( aGraphicUserIter->maGraphicCropLogic.Left || aGraphicUserIter->maGraphicCropLogic.Top
440                                 || aGraphicUserIter->maGraphicCropLogic.Right || aGraphicUserIter->maGraphicCropLogic.Bottom )
441                                 {   // removing crop area was not possible or should't been applied
442                                     text::GraphicCrop aGraphicCropLogic( 0, 0, 0, 0 );
443                                     if ( !aGraphicSettings.mbRemoveCropArea )
444                                     {
445                                         awt::Size aNewSize( GraphicCollector::GetOriginalSize( rxContext, xNewGraphic ) );
446                                         aGraphicCropLogic.Left = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Left * ((double)aNewSize.Width / (double)aSize100thMM.Width));
447                                         aGraphicCropLogic.Top = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Top * ((double)aNewSize.Height / (double)aSize100thMM.Height));
448                                         aGraphicCropLogic.Right = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Right * ((double)aNewSize.Width / (double)aSize100thMM.Width));
449                                         aGraphicCropLogic.Bottom = (sal_Int32)((double)aGraphicUserIter->maGraphicCropLogic.Bottom * ((double)aNewSize.Height / (double)aSize100thMM.Height));
450                                     }
451                                     xShapePropertySet->setPropertyValue( TKGet( TK_GraphicCrop ), Any( aGraphicCropLogic ) );
452                                 }
453                             }
454                             else if ( aGraphicUserIter->mxPropertySet.is() )
455                             {
456                                 Reference< XBitmap > xFillBitmap( xNewGraphic, UNO_QUERY );
457                                 if ( xFillBitmap.is() )
458                                 {
459                                     awt::Size aSize;
460                                     sal_Bool bLogicalSize;
461 
462                                     Reference< XPropertySet >& rxPropertySet( aGraphicUserIter->mxPropertySet );
463                                     rxPropertySet->setPropertyValue( TKGet( TK_FillBitmap ), Any( xFillBitmap ) );
464                                     if ( ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapLogicalSize ) ) >>= bLogicalSize )
465                                         && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeX ) ) >>= aSize.Width )
466                                         && ( rxPropertySet->getPropertyValue( TKGet( TK_FillBitmapSizeY ) ) >>= aSize.Height ) )
467                                     {
468                                         if ( !aSize.Width || !aSize.Height )
469                                         {
470                                             rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapLogicalSize ), Any( sal_True ) );
471                                             rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapSizeX ), Any( aGraphicUserIter->maLogicalSize.Width ) );
472                                             rxPropertySet->setPropertyValue( TKGet( TK_FillBitmapSizeY ), Any( aGraphicUserIter->maLogicalSize.Height ) );
473                                         }
474                                     }
475                                     if ( aGraphicUserIter->mxPagePropertySet.is() )
476                                         aGraphicUserIter->mxPagePropertySet->setPropertyValue( TKGet( TK_Background ), Any( rxPropertySet ) );
477                                 }
478                             }
479                             aGraphicUserIter++;
480                         }
481                     }
482                 }
483             }
484             aGraphicIter++;
485         }
486     }
487     catch ( Exception& )
488     {
489     }
490 }
491 
492 // ----------------
493 // - ImpOptimizer -
494 // ----------------
495 
ImpOptimizer(const Reference<XComponentContext> & rxContext,const Reference<XModel> & rxModel)496 ImpOptimizer::ImpOptimizer( const Reference< XComponentContext >& rxContext, const Reference< XModel >& rxModel ) :
497     mxContext                       ( rxContext ),
498     mxModel                     ( rxModel ),
499     mbJPEGCompression           ( sal_False ),
500     mnJPEGQuality               ( 90 ),
501     mbRemoveCropArea            ( sal_False ),
502     mnImageResolution           ( 0 ),
503     mbEmbedLinkedGraphics       ( sal_True ),
504     mbOLEOptimization           ( sal_False ),
505     mnOLEOptimizationType       ( 0 ),
506     mbDeleteUnusedMasterPages   ( sal_False ),
507     mbDeleteHiddenSlides        ( sal_False ),
508     mbDeleteNotesPages          ( sal_False ),
509     mbOpenNewDocument           ( sal_True )
510 {
511     OSL_TRACE("ImpOptimizer::ImpOptimizer");
512     Reference< XController > xController( mxModel->getCurrentController() );
513     if (xController.is() )
514         mxFrame.set( xController->getFrame() );
515 }
516 
517 // -----------------------------------------------------------------------------
518 
~ImpOptimizer()519 ImpOptimizer::~ImpOptimizer()
520 {
521     OSL_TRACE("ImpOptimizer::~ImpOptimizer");
522 }
523 
524 // -----------------------------------------------------------------------------
525 
DispatchStatus()526 void ImpOptimizer::DispatchStatus()
527 {
528     if ( mxStatusListener.is() )
529     {
530         FeatureStateEvent aState;
531         aState.IsEnabled = sal_True;
532         aState.State <<= GetStatusSequence();
533         mxStatusListener->statusChanged( aState );
534     }
535 }
536 
537 // -----------------------------------------------------------------------------
538 
ImplOptimize()539 sal_Bool ImpOptimizer::ImplOptimize()
540 {
541 
542     if ( maCustomShowName.getLength() )
543         ImpExtractCustomShow( mxModel, maCustomShowName );
544 
545     if ( mbDeleteUnusedMasterPages )
546     {
547         SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 40 ) ) );
548         SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_DELETING_SLIDES ) ) );
549         DispatchStatus();
550         ImpDeleteUnusedMasterPages( mxModel );
551     }
552 
553     if ( mbDeleteHiddenSlides )
554     {
555         SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 40 ) ) );
556         SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_DELETING_SLIDES ) ) );
557         DispatchStatus();
558         ImpDeleteHiddenSlides( mxModel );
559     }
560 
561     if ( mbDeleteNotesPages )
562     {
563         SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_DELETING_SLIDES ) ) );
564         DispatchStatus();
565         ImpDeleteNotesPages( mxModel );
566     }
567 
568     if ( mbOLEOptimization )
569     {
570         SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 45 ) ) );
571         SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_CREATING_OLE_REPLACEMENTS ) ) );
572         DispatchStatus();
573         ImpConvertOLE( mxModel, mnOLEOptimizationType );
574     }
575 
576     if ( mbJPEGCompression || mbRemoveCropArea || mnImageResolution )
577     {
578         SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 50 ) ) );
579         SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_OPTIMIZING_GRAPHICS ) ) );
580         DispatchStatus();
581 
582         std::vector< GraphicCollector::GraphicEntity > aGraphicList;
583         GraphicSettings aGraphicSettings( mbJPEGCompression, mnJPEGQuality, mbRemoveCropArea, mnImageResolution, mbEmbedLinkedGraphics );
584         GraphicCollector::CollectGraphics( mxContext, mxModel, aGraphicSettings, aGraphicList );
585         CompressGraphics( *this, mxContext, aGraphicSettings, aGraphicList );
586     }
587     SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 100 ) ) );
588     DispatchStatus();
589     return sal_True;
590 }
591 
DispatchURL(Reference<XComponentContext> xMSF,OUString sURL,Reference<XFrame> xFrame)592 static void DispatchURL( Reference< XComponentContext > xMSF, OUString sURL, Reference< XFrame > xFrame )
593 {
594     try
595     {
596         Reference< XURLTransformer > xURLTransformer( xMSF->getServiceManager()->createInstanceWithContext(
597                 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), xMSF ), UNO_QUERY_THROW );
598         util::URL aUrl;
599         aUrl.Complete = sURL;
600         xURLTransformer->parseStrict( aUrl );
601         Sequence< PropertyValue > aArgs;
602         Reference< XDispatchProvider > xDispatchProvider( xFrame, UNO_QUERY_THROW );
603         Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aUrl, OUString(), 0 );  // "_self"
604         if ( xDispatch.is() )
605             xDispatch->dispatch( aUrl, aArgs );
606     }
607     catch( Exception& )
608     {
609     }
610 }
611 
612 // -----------------------------------------------------------------------------
613 
Optimize(const Sequence<PropertyValue> & rArguments)614 sal_Bool ImpOptimizer::Optimize( const Sequence< PropertyValue >& rArguments )
615 {
616     OSL_TRACE("ImpOptimizer::Optimize");
617     sal_Bool bRet = sal_True;
618 
619     if ( mxModel.is() )
620     {
621         Reference< XWindowPeer > xParentWindow;
622         sal_Int64 nEstimatedFileSize = 0;
623         SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 0 ) ) );
624         DispatchStatus();
625 
626         int i, nICount;
627         for ( i = 0, nICount = rArguments.getLength(); i < nICount; i++ )
628         {
629             switch( TKGet( rArguments[ i ].Name ) )
630             {
631                 case TK_StatusListener : rArguments[ i ].Value >>= mxStatusListener; break;
632                 case TK_ParentWindow: rArguments[ i ].Value >>= xParentWindow; break;
633                 case TK_Settings :
634                 {
635                     com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue > aSettings;
636                     int j, nJCount;
637                     rArguments[ i ].Value >>= aSettings;
638                     for ( j = 0, nJCount = aSettings.getLength(); j < nJCount; j++ )
639                     {
640                         switch( TKGet( aSettings[ j ].Name ) )
641                         {
642                             case TK_JPEGCompression         : aSettings[ j ].Value >>= mbJPEGCompression; break;
643                             case TK_JPEGQuality             : aSettings[ j ].Value >>= mnJPEGQuality; break;
644                             case TK_RemoveCropArea          : aSettings[ j ].Value >>= mbRemoveCropArea; break;
645                             case TK_ImageResolution         : aSettings[ j ].Value >>= mnImageResolution; break;
646                             case TK_EmbedLinkedGraphics     : aSettings[ j ].Value >>= mbEmbedLinkedGraphics; break;
647                             case TK_OLEOptimization         : aSettings[ j ].Value >>= mbOLEOptimization; break;
648                             case TK_OLEOptimizationType     : aSettings[ j ].Value >>= mnOLEOptimizationType; break;
649                             case TK_CustomShowName          : aSettings[ j ].Value >>= maCustomShowName; break;
650                             case TK_DeleteUnusedMasterPages : aSettings[ j ].Value >>= mbDeleteUnusedMasterPages; break;
651                             case TK_DeleteHiddenSlides      : aSettings[ j ].Value >>= mbDeleteHiddenSlides; break;
652                             case TK_DeleteNotesPages        : aSettings[ j ].Value >>= mbDeleteNotesPages; break;
653                             case TK_SaveAsURL               : aSettings[ j ].Value >>= maSaveAsURL; break;
654                             case TK_FilterName              : aSettings[ j ].Value >>= maFilterName; break;
655                             case TK_OpenNewDocument         : aSettings[ j ].Value >>= mbOpenNewDocument; break;
656                             case TK_EstimatedFileSize       : aSettings[ j ].Value >>= nEstimatedFileSize; break;
657                             default: break;
658                         }
659                     }
660                 }
661                 break;
662                 default: break;
663             }
664         }
665 
666         sal_Int64 nSourceSize = 0;
667         sal_Int64 nDestSize = 0;
668 
669         Reference< XFrame > xSelf;
670         if ( maSaveAsURL.getLength() )
671         {
672 
673             SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 10 ) ) );
674             SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_DUPLICATING_PRESENTATION ) ) );
675             DispatchStatus();
676 
677             Reference< XStorable >xStorable( mxModel, UNO_QUERY );
678             if ( xStorable.is() )
679             {
680                 if ( xStorable->hasLocation() )
681                     nSourceSize = PPPOptimizer::GetFileSize( xStorable->getLocation() );
682 
683                 Sequence< PropertyValue > aArguments;
684                 if ( maFilterName.getLength() )
685                 {
686                     int nLength = aArguments.getLength();
687                     aArguments.realloc( nLength + 1 );
688                     aArguments[ nLength ].Name = TKGet( TK_FilterName );
689                     aArguments[ nLength ].Value <<= maFilterName;
690                 }
691                 xStorable->storeToURL( maSaveAsURL, aArguments );
692                 if ( !nSourceSize )
693                     nSourceSize = PPPOptimizer::GetFileSize( maSaveAsURL );
694 
695                 SetStatusValue( TK_Progress, Any( static_cast< sal_Int32 >( 30 ) ) );
696                 SetStatusValue( TK_Status, Any( ConfigurationAccess::getString( STR_DUPLICATING_PRESENTATION ) ) );
697                 DispatchStatus();
698 
699                 Reference< XDesktop > xDesktop( mxContext->getServiceManager()->createInstanceWithContext(
700                         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), mxContext ), UNO_QUERY );
701                 Reference< XFrame > xFrame( xDesktop, UNO_QUERY );
702                 xSelf = xFrame->findFrame( TKGet( TK__blank ), FrameSearchFlag::CREATE );
703                 Reference< XComponentLoader > xComponentLoader( xSelf, UNO_QUERY );
704 
705                 Sequence< PropertyValue > aLoadProps( 1 );
706                 aLoadProps[ 0 ].Name = TKGet( TK_Hidden );
707                 aLoadProps[ 0 ].Value <<= (sal_Bool)( sal_True );
708                 mxModel = Reference< XModel >( xComponentLoader->loadComponentFromURL(
709                     maSaveAsURL, TKGet( TK__self ), 0, aLoadProps ), UNO_QUERY );
710             }
711         }
712 
713         // check if the document is ReadOnly -> error
714         Reference< XStorable > xStorable( mxModel, UNO_QUERY );
715         if ( xStorable.is() && !xStorable->isReadonly() )
716         {
717             mxModel->lockControllers();
718             bRet = ImplOptimize();
719             mxModel->unlockControllers();
720 
721             // clearing undo stack:
722             Reference< XFrame > xFrame( mxFrame );
723             if ( xFrame.is() )
724             {
725                 const OUString sSlot( RTL_CONSTASCII_USTRINGPARAM( "slot:27115" ) );
726                 DispatchURL( mxContext, sSlot, xFrame );
727             }
728         }
729 
730         if ( maSaveAsURL.getLength() )
731         {
732             if ( xStorable.is() )
733             {
734                 xStorable->store();
735                 nDestSize = PPPOptimizer::GetFileSize( maSaveAsURL );
736             }
737         }
738 
739         if ( xParentWindow.is() )
740         {
741             InformationDialog aInformationDialog(
742                 mxContext, xParentWindow,  maSaveAsURL, mbOpenNewDocument,
743                 nSourceSize, nDestSize, nEstimatedFileSize );
744             aInformationDialog.execute();
745             SetStatusValue( TK_OpenNewDocument, Any( mbOpenNewDocument ) );
746             DispatchStatus();
747         }
748 
749         if ( maSaveAsURL.getLength() )
750         {
751             if ( mbOpenNewDocument && xSelf.is() )
752             {
753                 Reference< awt::XWindow > xContainerWindow( xSelf->getContainerWindow() );
754                 xContainerWindow->setVisible( sal_True );
755             }
756             else
757             {
758                 Reference< XComponent > xComponent( mxModel, UNO_QUERY );
759                 xComponent->dispose();
760             }
761         }
762         if ( nSourceSize && nDestSize )
763         {
764             SetStatusValue( TK_FileSizeSource, Any( nSourceSize ) );
765             SetStatusValue( TK_FileSizeDestination, Any( nDestSize ) );
766             DispatchStatus();
767         }
768     }
769     else
770         bRet = sal_False;
771     return bRet;
772 }
773 
774