xref: /aoo41x/main/sd/source/filter/grf/sdgrffilter.cxx (revision 79aad27f)
1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sd.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #ifdef _MSC_VER
28cdf0e10cSrcweir #pragma warning (disable:4190)
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <com/sun/star/graphic/XGraphicProvider.hpp>
31cdf0e10cSrcweir #include <com/sun/star/graphic/GraphicType.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
35cdf0e10cSrcweir #include <tools/errinf.hxx>
36cdf0e10cSrcweir #include <vcl/msgbox.hxx>
37cdf0e10cSrcweir #include <vcl/metaact.hxx>
38cdf0e10cSrcweir #include <vcl/virdev.hxx>
39cdf0e10cSrcweir #include <sfx2/docfile.hxx>
40cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
41cdf0e10cSrcweir #include <sfx2/frame.hxx>
42cdf0e10cSrcweir #include <svx/svdograf.hxx>
43cdf0e10cSrcweir #include <svx/svdpagv.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "../../ui/inc/strings.hrc"
46cdf0e10cSrcweir #include "../../ui/inc/DrawViewShell.hxx"
47cdf0e10cSrcweir #include "../../ui/inc/DrawDocShell.hxx"
48cdf0e10cSrcweir #include "../../ui/inc/ClientView.hxx"
49cdf0e10cSrcweir #include "../../ui/inc/FrameView.hxx"
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #include "comphelper/anytostring.hxx"
52cdf0e10cSrcweir #include "cppuhelper/exc_hlp.hxx"
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // --
55cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
56cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
57cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
58cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
59cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
60cdf0e10cSrcweir #include <sfx2/filedlghelper.hxx>
61cdf0e10cSrcweir #include <tools/urlobj.hxx>
62cdf0e10cSrcweir #include <svtools/filter.hxx>
63cdf0e10cSrcweir #include <svx/xoutbmp.hxx>
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // --
66cdf0e10cSrcweir 
67cdf0e10cSrcweir #include "sdpage.hxx"
68cdf0e10cSrcweir #include "drawdoc.hxx"
69cdf0e10cSrcweir #include "sdresid.hxx"
70cdf0e10cSrcweir #include "sdgrffilter.hxx"
71cdf0e10cSrcweir #include "../../ui/inc/ViewShellBase.hxx"
72cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h>
73cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
74cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValues.hpp>
75cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
76cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
77cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
78cdf0e10cSrcweir #include <com/sun/star/document/XExporter.hpp>
79cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
80cdf0e10cSrcweir #include <com/sun/star/view/XSelectionSupplier.hpp>
81cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawView.hpp>
82cdf0e10cSrcweir #include "../../ui/inc/DrawController.hxx"
83cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
84cdf0e10cSrcweir #include <com/sun/star/drawing/XShape.hpp>
85cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
86cdf0e10cSrcweir #include <com/sun/star/task/XInteractionRequest.hpp>
87cdf0e10cSrcweir #include <com/sun/star/drawing/GraphicFilterRequest.hpp>
88cdf0e10cSrcweir 
89cdf0e10cSrcweir using namespace ::com::sun::star;
90cdf0e10cSrcweir using namespace ::com::sun::star::uno;
91cdf0e10cSrcweir using namespace ::com::sun::star::lang;
92cdf0e10cSrcweir using namespace ::com::sun::star::beans;
93cdf0e10cSrcweir using namespace ::com::sun::star::graphic;
94cdf0e10cSrcweir using namespace ::com::sun::star::io;
95cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
96cdf0e10cSrcweir using namespace com::sun::star::ui::dialogs;
97cdf0e10cSrcweir using rtl::OUString;
98cdf0e10cSrcweir using namespace ::sfx2;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
101cdf0e10cSrcweir // -----------------------------------------------------------------------------
102cdf0e10cSrcweir 
103cdf0e10cSrcweir class SdGRFFilter_ImplInteractionHdl : public ::cppu::WeakImplHelper1< com::sun::star::task::XInteractionHandler >
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > m_xInter;
106cdf0e10cSrcweir 	sal_uInt16 nFilterError;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	public:
109cdf0e10cSrcweir 
SdGRFFilter_ImplInteractionHdl(com::sun::star::uno::Reference<com::sun::star::task::XInteractionHandler> xInteraction)110cdf0e10cSrcweir     SdGRFFilter_ImplInteractionHdl( com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > xInteraction ) :
111cdf0e10cSrcweir 		m_xInter( xInteraction ),
112cdf0e10cSrcweir 		nFilterError( GRFILTER_OK )
113cdf0e10cSrcweir         {}
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     ~SdGRFFilter_ImplInteractionHdl();
116cdf0e10cSrcweir 
GetErrorCode() const117cdf0e10cSrcweir 	sal_uInt16 GetErrorCode() const { return nFilterError; };
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	virtual void SAL_CALL	handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& )
120cdf0e10cSrcweir 								throw( com::sun::star::uno::RuntimeException );
121cdf0e10cSrcweir };
122cdf0e10cSrcweir 
~SdGRFFilter_ImplInteractionHdl()123cdf0e10cSrcweir SdGRFFilter_ImplInteractionHdl::~SdGRFFilter_ImplInteractionHdl()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
handle(const com::sun::star::uno::Reference<com::sun::star::task::XInteractionRequest> & xRequest)127cdf0e10cSrcweir void SdGRFFilter_ImplInteractionHdl::handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& xRequest )
128cdf0e10cSrcweir         throw( com::sun::star::uno::RuntimeException )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	if( !m_xInter.is() )
131cdf0e10cSrcweir 		return;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	com::sun::star::drawing::GraphicFilterRequest aErr;
134cdf0e10cSrcweir     if ( xRequest->getRequest() >>= aErr )
135cdf0e10cSrcweir 		nFilterError = (sal_uInt16)aErr.ErrCode;
136cdf0e10cSrcweir 	else
137cdf0e10cSrcweir         m_xInter->handle( xRequest );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir // ---------------
142cdf0e10cSrcweir // - SdPPTFilter -
143cdf0e10cSrcweir // ---------------
144cdf0e10cSrcweir 
SdGRFFilter(SfxMedium & rMedium,::sd::DrawDocShell & rDocShell)145cdf0e10cSrcweir SdGRFFilter::SdGRFFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell ) :
146cdf0e10cSrcweir 	SdFilter( rMedium, rDocShell, sal_True )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir // -----------------------------------------------------------------------------
151cdf0e10cSrcweir 
~SdGRFFilter()152cdf0e10cSrcweir SdGRFFilter::~SdGRFFilter()
153cdf0e10cSrcweir {
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir // -----------------------------------------------------------------------------
157cdf0e10cSrcweir 
HandleGraphicFilterError(sal_uInt16 nFilterError,sal_uLong nStreamError)158cdf0e10cSrcweir void SdGRFFilter::HandleGraphicFilterError( sal_uInt16 nFilterError, sal_uLong nStreamError )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	sal_uInt16 nId;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	switch( nFilterError )
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		case GRFILTER_OPENERROR:
165cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_OPENERROR;
166cdf0e10cSrcweir 			break;
167cdf0e10cSrcweir 		case GRFILTER_IOERROR:
168cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_IOERROR;
169cdf0e10cSrcweir 			break;
170cdf0e10cSrcweir 		case GRFILTER_FORMATERROR:
171cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_FORMATERROR;
172cdf0e10cSrcweir 			break;
173cdf0e10cSrcweir 		case GRFILTER_VERSIONERROR:
174cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_VERSIONERROR;
175cdf0e10cSrcweir 			break;
176cdf0e10cSrcweir 		case GRFILTER_TOOBIG:
177cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_TOOBIG;
178cdf0e10cSrcweir 			break;
179cdf0e10cSrcweir 		case 0 :
180cdf0e10cSrcweir 			nId = 0;
181cdf0e10cSrcweir 			break;
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 		default:
184cdf0e10cSrcweir 		case GRFILTER_FILTERERROR:
185cdf0e10cSrcweir 			nId = STR_IMPORT_GRFILTER_FILTERERROR;
186cdf0e10cSrcweir 			break;
187cdf0e10cSrcweir 	}
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 	if( ERRCODE_NONE != nStreamError )
190cdf0e10cSrcweir 		ErrorHandler::HandleError( nStreamError );
191cdf0e10cSrcweir 	else if( STR_IMPORT_GRFILTER_IOERROR == nId )
192cdf0e10cSrcweir 		ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
193cdf0e10cSrcweir 	else
194cdf0e10cSrcweir 	{
195cdf0e10cSrcweir 		ErrorBox aErrorBox( NULL, WB_OK, String( SdResId( nId ) ) );
196cdf0e10cSrcweir 		aErrorBox.Execute();
197cdf0e10cSrcweir 	}
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // -----------------------------------------------------------------------------
201cdf0e10cSrcweir 
Import()202cdf0e10cSrcweir sal_Bool SdGRFFilter::Import()
203cdf0e10cSrcweir {
204cdf0e10cSrcweir 	Graphic         aGraphic;
205cdf0e10cSrcweir 	const String    aFileName( mrMedium.GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) );
206cdf0e10cSrcweir 	GraphicFilter*  pGraphicFilter = GraphicFilter::GetGraphicFilter();
207cdf0e10cSrcweir 	const sal_uInt16    nFilter = pGraphicFilter->GetImportFormatNumberForTypeName( mrMedium.GetFilter()->GetTypeName() );
208cdf0e10cSrcweir 	sal_Bool		bRet = sal_False;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	// ggf. Filterdialog ausfuehren
211cdf0e10cSrcweir 	if ( !pGraphicFilter->HasImportDialog( nFilter ) || pGraphicFilter->DoImportDialog( NULL, nFilter ) )
212cdf0e10cSrcweir 	{
213cdf0e10cSrcweir 		SvStream*		pIStm = mrMedium.GetInStream();
214cdf0e10cSrcweir 		sal_uInt16			nReturn = pIStm ? pGraphicFilter->ImportGraphic( aGraphic, aFileName, *pIStm, nFilter ) : 1;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 		if( nReturn )
217cdf0e10cSrcweir 			HandleGraphicFilterError( nReturn, pGraphicFilter->GetLastError().nStreamError );
218cdf0e10cSrcweir 		else
219cdf0e10cSrcweir 		{
220cdf0e10cSrcweir 			if( mrDocument.GetPageCount() == 0L )
221cdf0e10cSrcweir 				mrDocument.CreateFirstPages();
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 			SdPage*		pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
224cdf0e10cSrcweir 			Point		aPos;
225cdf0e10cSrcweir 			Size		aPagSize( pPage->GetSize() );
226cdf0e10cSrcweir 			Size		aGrfSize( OutputDevice::LogicToLogic( aGraphic.GetPrefSize(),
227cdf0e10cSrcweir 								  aGraphic.GetPrefMapMode(), MAP_100TH_MM ) );
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 			aPagSize.Width() -= pPage->GetLftBorder() + pPage->GetRgtBorder();
230cdf0e10cSrcweir 			aPagSize.Height() -= pPage->GetUppBorder() + pPage->GetLwrBorder();
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			// scale to fit page
233cdf0e10cSrcweir 			if ( ( ( aGrfSize.Height() > aPagSize.Height() ) || ( aGrfSize.Width() > aPagSize.Width() ) ) &&
234cdf0e10cSrcweir 				 aGrfSize.Height() && aPagSize.Height() )
235cdf0e10cSrcweir 			{
236cdf0e10cSrcweir 				double fGrfWH =	(double) aGrfSize.Width() / aGrfSize.Height();
237cdf0e10cSrcweir 				double fWinWH =	(double) aPagSize.Width() / aPagSize.Height();
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 				// Grafik an Pagesize anpassen (skaliert)
240cdf0e10cSrcweir 				if( fGrfWH < fWinWH )
241cdf0e10cSrcweir 				{
242cdf0e10cSrcweir 					aGrfSize.Width() = (long) ( aPagSize.Height() * fGrfWH );
243cdf0e10cSrcweir 					aGrfSize.Height() = aPagSize.Height();
244cdf0e10cSrcweir 				}
245cdf0e10cSrcweir 				else if( fGrfWH > 0.F )
246cdf0e10cSrcweir 				{
247cdf0e10cSrcweir 					aGrfSize.Width() = aPagSize.Width();
248cdf0e10cSrcweir 					aGrfSize.Height()= (long) ( aPagSize.Width() / fGrfWH );
249cdf0e10cSrcweir 				}
250cdf0e10cSrcweir 			}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 			// Ausgaberechteck fuer Grafik setzen
253cdf0e10cSrcweir 			aPos.X() = ( ( aPagSize.Width() - aGrfSize.Width() ) >> 1 ) + pPage->GetLftBorder();
254cdf0e10cSrcweir 			aPos.Y() = ( ( aPagSize.Height() - aGrfSize.Height() ) >> 1 )  + pPage->GetUppBorder();
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 			pPage->InsertObject( new SdrGrafObj( aGraphic, Rectangle( aPos, aGrfSize ) ) );
257cdf0e10cSrcweir 			bRet = sal_True;
258cdf0e10cSrcweir 		}
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 	return bRet;
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir // -----------------------------------------------------------------------------
264cdf0e10cSrcweir 
Export()265cdf0e10cSrcweir sal_Bool SdGRFFilter::Export()
266cdf0e10cSrcweir {
267cdf0e10cSrcweir 	// SJ: todo: error handling, the GraphicExportFilter does not support proper errorhandling
268cdf0e10cSrcweir 
269cdf0e10cSrcweir     sal_Bool bRet = sal_False;
270cdf0e10cSrcweir 
271cdf0e10cSrcweir  	uno::Reference< lang::XMultiServiceFactory >
272cdf0e10cSrcweir         xSMgr( ::comphelper::getProcessServiceFactory() );
273cdf0e10cSrcweir     uno::Reference< uno::XInterface > xComponent
274cdf0e10cSrcweir         ( xSMgr->createInstance( rtl::OUString::createFromAscii( "com.sun.star.drawing.GraphicExportFilter" ) ),
275cdf0e10cSrcweir             uno::UNO_QUERY );
276cdf0e10cSrcweir 	if ( xComponent.is() )
277cdf0e10cSrcweir 	{
278cdf0e10cSrcweir 		uno::Reference< document::XExporter > xExporter
279cdf0e10cSrcweir 			( xComponent, uno::UNO_QUERY );
280cdf0e10cSrcweir 		uno::Reference< document::XFilter > xFilter
281cdf0e10cSrcweir 			( xComponent, uno::UNO_QUERY );
282cdf0e10cSrcweir 		if ( xExporter.is() && xFilter.is() )
283cdf0e10cSrcweir 		{
284cdf0e10cSrcweir 			SdPage*	pPage = NULL;
285cdf0e10cSrcweir 			sd::DrawViewShell*	pDrawViewShell = static_cast< ::sd::DrawViewShell* >
286cdf0e10cSrcweir 				( ( ( mrDocShell.GetViewShell() && mrDocShell.GetViewShell()->ISA(::sd::DrawViewShell ) ) ? mrDocShell.GetViewShell() : NULL ) );
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 			PageKind ePageKind = PK_STANDARD;
289cdf0e10cSrcweir 			if( pDrawViewShell )
290cdf0e10cSrcweir 			{
291cdf0e10cSrcweir 				ePageKind = pDrawViewShell->GetPageKind();
292cdf0e10cSrcweir 				if( PK_HANDOUT == ePageKind )
293cdf0e10cSrcweir 					pPage = mrDocument.GetSdPage( 0, PK_HANDOUT );
294cdf0e10cSrcweir 				else
295cdf0e10cSrcweir 					pPage = pDrawViewShell->GetActualPage();
296cdf0e10cSrcweir 			}
297cdf0e10cSrcweir 			else
298cdf0e10cSrcweir 				pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 			if ( pPage )
301cdf0e10cSrcweir 			{
302cdf0e10cSrcweir 				// taking the 'correct' page number, seems that there might exist a better method to archive this
303cdf0e10cSrcweir 				pPage = mrDocument.GetSdPage( pPage->GetPageNum() ? ( pPage->GetPageNum() - 1 ) >> 1 : 0, ePageKind );
304cdf0e10cSrcweir 				if ( pPage )
305cdf0e10cSrcweir 				{
306cdf0e10cSrcweir 					uno::Reference< lang::XComponent > xSource( pPage->getUnoPage(), uno::UNO_QUERY );
307cdf0e10cSrcweir 					SfxItemSet* pSet = mrMedium.GetItemSet();
308cdf0e10cSrcweir                 	GraphicFilter*  pGraphicFilter = GraphicFilter::GetGraphicFilter();
309cdf0e10cSrcweir 					if ( pSet && pGraphicFilter && xSource.is() )
310cdf0e10cSrcweir 					{
311cdf0e10cSrcweir 						const String aTypeName( mrMedium.GetFilter()->GetTypeName() );
312cdf0e10cSrcweir 						const sal_uInt16 nFilter = pGraphicFilter->GetExportFormatNumberForTypeName( aTypeName );
313cdf0e10cSrcweir 						if ( nFilter != GRFILTER_FORMAT_NOTFOUND )
314cdf0e10cSrcweir 						{
315cdf0e10cSrcweir 							uno::Reference< task::XInteractionHandler > mXInteractionHandler;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir 							beans::PropertyValues aArgs;
318cdf0e10cSrcweir 							TransformItems( SID_SAVEASDOC, *pSet, aArgs );
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 							rtl::OUString sInteractionHandler( RTL_CONSTASCII_USTRINGPARAM( "InteractionHandler" ) );
321cdf0e10cSrcweir 							rtl::OUString sFilterName( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ) );
322cdf0e10cSrcweir 							rtl::OUString sShortName( pGraphicFilter->GetExportFormatShortName( nFilter ) );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 							sal_Bool	bFilterNameFound = sal_False;
325cdf0e10cSrcweir 							sal_Int32	i, nCount;
326cdf0e10cSrcweir 							for ( i = 0, nCount = aArgs.getLength(); i < nCount; i++ )
327cdf0e10cSrcweir 							{
328cdf0e10cSrcweir 								rtl::OUString& rStr = aArgs[ i ].Name;
329cdf0e10cSrcweir 								if ( rStr == sFilterName )
330cdf0e10cSrcweir 								{
331cdf0e10cSrcweir 									bFilterNameFound = sal_True;
332cdf0e10cSrcweir 									aArgs[ i ].Name = sFilterName;
333cdf0e10cSrcweir 									aArgs[ i ].Value <<= sShortName;
334cdf0e10cSrcweir 								}
335cdf0e10cSrcweir 								else if ( rStr == sInteractionHandler )
336cdf0e10cSrcweir 								{
337cdf0e10cSrcweir 									uno::Reference< task::XInteractionHandler > xHdl;
338cdf0e10cSrcweir 									if ( aArgs[ i ].Value >>= xHdl )
339cdf0e10cSrcweir 									{
340cdf0e10cSrcweir 										mXInteractionHandler = new SdGRFFilter_ImplInteractionHdl( xHdl );
341cdf0e10cSrcweir 										aArgs[ i ].Value <<= mXInteractionHandler;
342cdf0e10cSrcweir 									}
343cdf0e10cSrcweir 								}
344cdf0e10cSrcweir 							}
345cdf0e10cSrcweir 							if ( !bFilterNameFound )
346cdf0e10cSrcweir 							{
347cdf0e10cSrcweir 								aArgs.realloc( ++nCount );
348cdf0e10cSrcweir 								aArgs[ i ].Name = sFilterName;
349cdf0e10cSrcweir 								aArgs[ i ].Value <<= sShortName;
350cdf0e10cSrcweir 							}
351cdf0e10cSrcweir 
352cdf0e10cSrcweir 							// take selection if needed
353cdf0e10cSrcweir 							if( ( SFX_ITEM_SET == pSet->GetItemState( SID_SELECTION ) )
354cdf0e10cSrcweir 								&& static_cast< const SfxBoolItem& >( pSet->Get( SID_SELECTION ) ).GetValue()
355cdf0e10cSrcweir 								&& pDrawViewShell )
356cdf0e10cSrcweir 							{
357cdf0e10cSrcweir 								uno::Reference< view::XSelectionSupplier > xSelectionSupplier(
358cdf0e10cSrcweir 									pDrawViewShell->GetViewShellBase().GetController(), uno::UNO_QUERY );
359cdf0e10cSrcweir 								if ( xSelectionSupplier.is() )
360cdf0e10cSrcweir 								{
361cdf0e10cSrcweir 									uno::Any aSelection( xSelectionSupplier->getSelection() );
362cdf0e10cSrcweir 									uno::Reference< lang::XComponent > xSelection;
363cdf0e10cSrcweir 									if ( aSelection >>= xSelection )
364cdf0e10cSrcweir 										xSource = xSelection;
365cdf0e10cSrcweir 								}
366cdf0e10cSrcweir 							}
367cdf0e10cSrcweir 							xExporter->setSourceDocument( xSource );
368cdf0e10cSrcweir 							bRet = xFilter->filter( aArgs );
369cdf0e10cSrcweir 							if ( !bRet && mXInteractionHandler.is() )
370cdf0e10cSrcweir 								SdGRFFilter::HandleGraphicFilterError(
371cdf0e10cSrcweir 									static_cast< SdGRFFilter_ImplInteractionHdl* >( mXInteractionHandler.get() )->GetErrorCode(),
372cdf0e10cSrcweir 													pGraphicFilter->GetLastError().nStreamError );
373cdf0e10cSrcweir 						}
374cdf0e10cSrcweir  					}
375cdf0e10cSrcweir 				}
376cdf0e10cSrcweir 			}
377cdf0e10cSrcweir 		}
378cdf0e10cSrcweir 	}
379cdf0e10cSrcweir     return bRet;
380cdf0e10cSrcweir }
381cdf0e10cSrcweir 
SaveGraphic(const::com::sun::star::uno::Reference<::com::sun::star::drawing::XShape> & xShape)382cdf0e10cSrcweir void SdGRFFilter::SaveGraphic( const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XShape >& xShape )
383cdf0e10cSrcweir {
384cdf0e10cSrcweir 	try
385cdf0e10cSrcweir 	{
386cdf0e10cSrcweir 		Reference< XMultiServiceFactory > xSM( ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
387cdf0e10cSrcweir 
388cdf0e10cSrcweir 		Reference< XGraphicProvider > xProvider( xSM->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicProvider" ) ) ), UNO_QUERY_THROW );
389cdf0e10cSrcweir 		Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW );
390cdf0e10cSrcweir 
391cdf0e10cSrcweir 		// detect mime type of graphic
392cdf0e10cSrcweir 		OUString aMimeType;
393cdf0e10cSrcweir         OUString sGraphicURL;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 		// first try to detect from graphic object
396cdf0e10cSrcweir 		Reference< XPropertySet > xGraphicSet( xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ) ), UNO_QUERY_THROW );
397cdf0e10cSrcweir         xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicURL" ) ) ) >>= sGraphicURL;
398cdf0e10cSrcweir 
399cdf0e10cSrcweir         bool bIsLinked = (sGraphicURL.getLength() != 0) && (sGraphicURL.compareToAscii( RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.GraphicObject:") ) != 0);
400cdf0e10cSrcweir 
401cdf0e10cSrcweir         if( !bIsLinked )
402cdf0e10cSrcweir        		xGraphicSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) ) ) >>= aMimeType;
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 		if( bIsLinked || aMimeType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "image/x-vclgraphic" ) ) || !aMimeType.getLength() )
405cdf0e10cSrcweir 		{
406cdf0e10cSrcweir 			// this failed, try to detect it from graphic stream and URL
407cdf0e10cSrcweir 			OUString aURL( sGraphicURL );
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 			if( aURL.getLength() == 0 )
410cdf0e10cSrcweir 				xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicStreamURL" ) ) ) >>= aURL;
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 			{
413cdf0e10cSrcweir 				Reference< XInputStream > xGraphStream( xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicStream" ) ) ), UNO_QUERY );
414cdf0e10cSrcweir 				PropertyValues aDesc(2);
415cdf0e10cSrcweir 				aDesc[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
416cdf0e10cSrcweir 				aDesc[0].Value <<= aURL;
417cdf0e10cSrcweir 				aDesc[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "InputStream" ) );
418cdf0e10cSrcweir 				aDesc[1].Value <<= xGraphStream;
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 				Reference< XPropertySet > xDescSet( xProvider->queryGraphicDescriptor( aDesc ), UNO_QUERY_THROW );
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 				xDescSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) ) ) >>= aMimeType;
423cdf0e10cSrcweir 			}
424cdf0e10cSrcweir 		}
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 		if( aMimeType.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "image/x-vclgraphic" ) ) || !aMimeType.getLength() )
427cdf0e10cSrcweir 		{
428cdf0e10cSrcweir 			// this also failed, now set a mimetype that fits graphic best
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 			// gif for animated pixel
431cdf0e10cSrcweir 			// png for non animated pixel
432cdf0e10cSrcweir 			// svm for vector format
433cdf0e10cSrcweir 			sal_Int8 nGraphicType = 0;
434cdf0e10cSrcweir 			xGraphicSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicType" ) ) ) >>= nGraphicType;
435cdf0e10cSrcweir 			switch( nGraphicType )
436cdf0e10cSrcweir 			{
437cdf0e10cSrcweir 			case ::com::sun::star::graphic::GraphicType::VECTOR:
438cdf0e10cSrcweir 				aMimeType = OUString::createFromAscii( "image/x-svm" );
439cdf0e10cSrcweir 				break;
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 			case ::com::sun::star::graphic::GraphicType::PIXEL:
442cdf0e10cSrcweir 				{
443cdf0e10cSrcweir 					sal_Bool bAnimated = sal_False;
444cdf0e10cSrcweir 					xGraphicSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Animated" ) ) ) >>= bAnimated;
445cdf0e10cSrcweir 
446cdf0e10cSrcweir 					if( bAnimated )
447cdf0e10cSrcweir 					{
448cdf0e10cSrcweir 						aMimeType = OUString::createFromAscii( "image/gif" );
449cdf0e10cSrcweir 						break;
450cdf0e10cSrcweir 					}
451cdf0e10cSrcweir 				}
452cdf0e10cSrcweir 				// Fallthrough!
453cdf0e10cSrcweir //			case ::com::sun::star::graphic::GraphicType::EMPTY:
454cdf0e10cSrcweir 			default:
455cdf0e10cSrcweir 				aMimeType = OUString::createFromAscii( "image/png" );
456cdf0e10cSrcweir 				break;
457cdf0e10cSrcweir 			}
458cdf0e10cSrcweir 		}
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 		// init dialog
461cdf0e10cSrcweir 		SvtPathOptions aPathOpt;
462cdf0e10cSrcweir 		String sGrfPath( aPathOpt.GetGraphicPath() );
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 		FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
465cdf0e10cSrcweir 		Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
466cdf0e10cSrcweir 
467cdf0e10cSrcweir 		String aTitle( SdResId( STR_TITLE_SAVE_AS_PICTURE ) );
468cdf0e10cSrcweir 		aDlgHelper.SetTitle( aTitle );
469cdf0e10cSrcweir 
470cdf0e10cSrcweir 		INetURLObject aPath;
471cdf0e10cSrcweir 		aPath.SetSmartURL( sGrfPath);
472cdf0e10cSrcweir 	    xFP->setDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
473cdf0e10cSrcweir 
474cdf0e10cSrcweir 		// populate filter dialog filter list and select default filter to match graphic mime type
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 		GraphicFilter& rGF = *GraphicFilter::GetGraphicFilter();
477cdf0e10cSrcweir 		Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
478cdf0e10cSrcweir 		OUString aDefaultFormatName;
479cdf0e10cSrcweir 		sal_uInt16 nCount = rGF.GetExportFormatCount();
480cdf0e10cSrcweir 
481cdf0e10cSrcweir 		std::map< OUString, OUString > aMimeTypeMap;
482cdf0e10cSrcweir 
483cdf0e10cSrcweir 		for ( sal_uInt16 i = 0; i < nCount; i++ )
484cdf0e10cSrcweir 		{
485cdf0e10cSrcweir 			const OUString aExportFormatName( rGF.GetExportFormatName( i ) );
486cdf0e10cSrcweir 			const OUString aFilterMimeType( rGF.GetExportFormatMediaType( i ) );
487cdf0e10cSrcweir 			xFltMgr->appendFilter( aExportFormatName, rGF.GetExportWildcard( i ) );
488cdf0e10cSrcweir 			aMimeTypeMap[ aExportFormatName ] = aFilterMimeType;
489cdf0e10cSrcweir 			if( aMimeType == aFilterMimeType )
490cdf0e10cSrcweir 				aDefaultFormatName = aExportFormatName;
491cdf0e10cSrcweir 		}
492cdf0e10cSrcweir 
493cdf0e10cSrcweir 		if( aDefaultFormatName.getLength() == 0 )
494cdf0e10cSrcweir 		{
495cdf0e10cSrcweir 			nCount = rGF.GetImportFormatCount();
496cdf0e10cSrcweir 			for( sal_uInt16 i = 0; i < nCount; i++ )
497cdf0e10cSrcweir 			{
498cdf0e10cSrcweir 				const OUString aFilterMimeType( rGF.GetImportFormatMediaType( i ) );
499cdf0e10cSrcweir 				if( aMimeType == aFilterMimeType )
500cdf0e10cSrcweir 				{
501cdf0e10cSrcweir 					aDefaultFormatName = rGF.GetImportFormatName( i );
502cdf0e10cSrcweir 					xFltMgr->appendFilter( aDefaultFormatName,  rGF.GetImportWildcard( i ) );
503cdf0e10cSrcweir 					aMimeTypeMap[ aDefaultFormatName ] = aFilterMimeType;
504cdf0e10cSrcweir 					break;
505cdf0e10cSrcweir 				}
506cdf0e10cSrcweir 			}
507cdf0e10cSrcweir 		}
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 		if( aDefaultFormatName.getLength() == 0 )
510cdf0e10cSrcweir 			aDefaultFormatName = OUString( RTL_CONSTASCII_USTRINGPARAM( "PNG - Portable Network Graphic" ) );
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 		xFltMgr->setCurrentFilter( aDefaultFormatName );
513cdf0e10cSrcweir 
514cdf0e10cSrcweir 		// execute dialog
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 		if( aDlgHelper.Execute() == ERRCODE_NONE )
517cdf0e10cSrcweir 		{
518cdf0e10cSrcweir 			OUString sPath( xFP->getFiles().getConstArray()[0] );
519cdf0e10cSrcweir 			aPath.SetSmartURL( sPath);
520cdf0e10cSrcweir 			sGrfPath = aPath.GetPath();
521cdf0e10cSrcweir 
522cdf0e10cSrcweir 			OUString aExportMimeType( aMimeTypeMap[xFltMgr->getCurrentFilter()] );
523cdf0e10cSrcweir 
524cdf0e10cSrcweir 			Reference< XInputStream > xGraphStream;
525cdf0e10cSrcweir 			if( aMimeType == aExportMimeType )
526cdf0e10cSrcweir 				xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "GraphicStream" ) ) ) >>= xGraphStream;
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 			if( xGraphStream.is() )
529cdf0e10cSrcweir 			{
530cdf0e10cSrcweir 				Reference< XSimpleFileAccess2 > xFileAccess( xSM->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ucb.SimpleFileAccess" ) ) ), UNO_QUERY_THROW );
531cdf0e10cSrcweir 				xFileAccess->writeFile( sPath, xGraphStream );
532cdf0e10cSrcweir 			}
533cdf0e10cSrcweir 			else
534cdf0e10cSrcweir 			{
535cdf0e10cSrcweir 				PropertyValues aDesc(2);
536cdf0e10cSrcweir 				aDesc[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "URL" ) );
537cdf0e10cSrcweir 				aDesc[0].Value <<= sPath;
538cdf0e10cSrcweir 				aDesc[1].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "MimeType" ) );
539cdf0e10cSrcweir 				aDesc[1].Value <<= aExportMimeType;
540cdf0e10cSrcweir 				Reference< XGraphic > xGraphic( xShapeSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic" ) ) ), UNO_QUERY_THROW );
541cdf0e10cSrcweir 				xProvider->storeGraphic( xGraphic, aDesc );
542cdf0e10cSrcweir 			}
543cdf0e10cSrcweir 		}
544cdf0e10cSrcweir 	}
545cdf0e10cSrcweir 	catch( Exception& )
546cdf0e10cSrcweir 	{
547cdf0e10cSrcweir 		DBG_ERROR(
548cdf0e10cSrcweir 			(rtl::OString("SdGRFFilter::SaveGraphic(), "
549cdf0e10cSrcweir 					"exception caught: ") +
550cdf0e10cSrcweir 			rtl::OUStringToOString(
551cdf0e10cSrcweir 				comphelper::anyToString( cppu::getCaughtException() ),
552cdf0e10cSrcweir 				RTL_TEXTENCODING_UTF8 )).getStr() );
553cdf0e10cSrcweir 	}
554cdf0e10cSrcweir }
555