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