Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #ifdef _MSC_VER
31 : : #pragma warning (disable:4190)
32 : : #endif
33 : : #include <com/sun/star/graphic/GraphicProvider.hpp>
34 : : #include <com/sun/star/graphic/XGraphicProvider.hpp>
35 : : #include <com/sun/star/graphic/GraphicType.hpp>
36 : : #include <com/sun/star/ucb/SimpleFileAccess.hpp>
37 : : #include <com/sun/star/ucb/XSimpleFileAccess2.hpp>
38 : :
39 : : #include <unotools/localfilehelper.hxx>
40 : : #include <tools/errinf.hxx>
41 : : #include <vcl/msgbox.hxx>
42 : : #include <vcl/metaact.hxx>
43 : : #include <vcl/virdev.hxx>
44 : : #include <sfx2/docfile.hxx>
45 : : #include <sfx2/docfilt.hxx>
46 : : #include <sfx2/frame.hxx>
47 : : #include <svx/svdograf.hxx>
48 : : #include <svx/svdpagv.hxx>
49 : :
50 : : #include "../../ui/inc/strings.hrc"
51 : : #include "../../ui/inc/DrawViewShell.hxx"
52 : : #include "../../ui/inc/DrawDocShell.hxx"
53 : : #include "../../ui/inc/ClientView.hxx"
54 : : #include "../../ui/inc/FrameView.hxx"
55 : :
56 : : #include "comphelper/anytostring.hxx"
57 : : #include "cppuhelper/exc_hlp.hxx"
58 : :
59 : : // --
60 : : #include <comphelper/processfactory.hxx>
61 : : #include <unotools/pathoptions.hxx>
62 : : #include <sfx2/filedlghelper.hxx>
63 : : #include <svtools/filter.hxx>
64 : : #include <svx/xoutbmp.hxx>
65 : :
66 : : // --
67 : :
68 : : #include "sdpage.hxx"
69 : : #include "drawdoc.hxx"
70 : : #include "sdresid.hxx"
71 : : #include "sdgrffilter.hxx"
72 : : #include "../../ui/inc/ViewShellBase.hxx"
73 : : #include <com/sun/star/uno/Sequence.h>
74 : : #include <com/sun/star/beans/PropertyValue.hpp>
75 : : #include <com/sun/star/beans/PropertyValues.hpp>
76 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
77 : : #include <com/sun/star/lang/XComponent.hpp>
78 : : #include <com/sun/star/document/XFilter.hpp>
79 : : #include <com/sun/star/document/XExporter.hpp>
80 : : #include <com/sun/star/view/XSelectionSupplier.hpp>
81 : : #include <com/sun/star/drawing/XDrawView.hpp>
82 : : #include "../../ui/inc/DrawController.hxx"
83 : : #include <cppuhelper/implbase2.hxx>
84 : : #include <com/sun/star/drawing/XShape.hpp>
85 : : #include <com/sun/star/task/XInteractionHandler.hpp>
86 : : #include <com/sun/star/task/XInteractionRequest.hpp>
87 : : #include <com/sun/star/drawing/GraphicFilterRequest.hpp>
88 : :
89 : : using namespace ::com::sun::star;
90 : : using namespace ::com::sun::star::uno;
91 : : using namespace ::com::sun::star::lang;
92 : : using namespace ::com::sun::star::beans;
93 : : using namespace ::com::sun::star::graphic;
94 : : using namespace ::com::sun::star::io;
95 : : using namespace ::com::sun::star::ucb;
96 : : using namespace com::sun::star::ui::dialogs;
97 : : using rtl::OUString;
98 : : using namespace ::sfx2;
99 : :
100 : :
101 : : // -----------------------------------------------------------------------------
102 : :
103 : : class SdGRFFilter_ImplInteractionHdl : public ::cppu::WeakImplHelper1< com::sun::star::task::XInteractionHandler >
104 : : {
105 : : com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > m_xInter;
106 : : sal_uInt16 nFilterError;
107 : :
108 : : public:
109 : :
110 : 0 : SdGRFFilter_ImplInteractionHdl( com::sun::star::uno::Reference< com::sun::star::task::XInteractionHandler > xInteraction ) :
111 : : m_xInter( xInteraction ),
112 : 0 : nFilterError( GRFILTER_OK )
113 : 0 : {}
114 : :
115 : : ~SdGRFFilter_ImplInteractionHdl();
116 : :
117 : 0 : sal_uInt16 GetErrorCode() const { return nFilterError; };
118 : :
119 : : virtual void SAL_CALL handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& )
120 : : throw( com::sun::star::uno::RuntimeException );
121 : : };
122 : :
123 : 0 : SdGRFFilter_ImplInteractionHdl::~SdGRFFilter_ImplInteractionHdl()
124 : : {
125 [ # # ]: 0 : }
126 : :
127 : 0 : void SdGRFFilter_ImplInteractionHdl::handle( const com::sun::star::uno::Reference< com::sun::star::task::XInteractionRequest >& xRequest )
128 : : throw( com::sun::star::uno::RuntimeException )
129 : : {
130 [ # # ]: 0 : if( !m_xInter.is() )
131 : 0 : return;
132 : :
133 [ # # ]: 0 : com::sun::star::drawing::GraphicFilterRequest aErr;
134 [ # # ][ # # ]: 0 : if ( xRequest->getRequest() >>= aErr )
[ # # ][ # # ]
135 : 0 : nFilterError = (sal_uInt16)aErr.ErrCode;
136 : : else
137 [ # # ][ # # ]: 0 : m_xInter->handle( xRequest );
[ # # ]
138 : : }
139 : :
140 : :
141 : : // ---------------
142 : : // - SdPPTFilter -
143 : : // ---------------
144 : :
145 : 0 : SdGRFFilter::SdGRFFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell ) :
146 : 0 : SdFilter( rMedium, rDocShell, sal_True )
147 : : {
148 : 0 : }
149 : :
150 : : // -----------------------------------------------------------------------------
151 : :
152 : 0 : SdGRFFilter::~SdGRFFilter()
153 : : {
154 [ # # ]: 0 : }
155 : :
156 : : // -----------------------------------------------------------------------------
157 : :
158 : 0 : void SdGRFFilter::HandleGraphicFilterError( sal_uInt16 nFilterError, sal_uLong nStreamError )
159 : : {
160 : : sal_uInt16 nId;
161 : :
162 [ # # # # : 0 : switch( nFilterError )
# # # ]
163 : : {
164 : : case GRFILTER_OPENERROR:
165 : 0 : nId = STR_IMPORT_GRFILTER_OPENERROR;
166 : 0 : break;
167 : : case GRFILTER_IOERROR:
168 : 0 : nId = STR_IMPORT_GRFILTER_IOERROR;
169 : 0 : break;
170 : : case GRFILTER_FORMATERROR:
171 : 0 : nId = STR_IMPORT_GRFILTER_FORMATERROR;
172 : 0 : break;
173 : : case GRFILTER_VERSIONERROR:
174 : 0 : nId = STR_IMPORT_GRFILTER_VERSIONERROR;
175 : 0 : break;
176 : : case GRFILTER_TOOBIG:
177 : 0 : nId = STR_IMPORT_GRFILTER_TOOBIG;
178 : 0 : break;
179 : : case 0 :
180 : 0 : nId = 0;
181 : 0 : break;
182 : :
183 : : default:
184 : : case GRFILTER_FILTERERROR:
185 : 0 : nId = STR_IMPORT_GRFILTER_FILTERERROR;
186 : 0 : break;
187 : : }
188 : :
189 [ # # ]: 0 : if( ERRCODE_NONE != nStreamError )
190 : 0 : ErrorHandler::HandleError( nStreamError );
191 [ # # ]: 0 : else if( STR_IMPORT_GRFILTER_IOERROR == nId )
192 : 0 : ErrorHandler::HandleError( ERRCODE_IO_GENERAL );
193 : : else
194 : : {
195 [ # # ][ # # ]: 0 : ErrorBox aErrorBox( NULL, WB_OK, String( SdResId( nId ) ) );
[ # # ][ # # ]
196 [ # # ][ # # ]: 0 : aErrorBox.Execute();
197 : : }
198 : 0 : }
199 : :
200 : : // -----------------------------------------------------------------------------
201 : :
202 : 0 : sal_Bool SdGRFFilter::Import()
203 : : {
204 [ # # ]: 0 : Graphic aGraphic;
205 [ # # ][ # # ]: 0 : const String aFileName( mrMedium.GetURLObject().GetMainURL( INetURLObject::NO_DECODE ) );
[ # # ]
206 [ # # ]: 0 : GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
207 [ # # ][ # # ]: 0 : const sal_uInt16 nFilter = rGraphicFilter.GetImportFormatNumberForTypeName( mrMedium.GetFilter()->GetTypeName() );
208 : 0 : sal_Bool bRet = sal_False;
209 : :
210 [ # # ]: 0 : SvStream* pIStm = mrMedium.GetInStream();
211 [ # # ][ # # ]: 0 : sal_uInt16 nReturn = pIStm ? rGraphicFilter.ImportGraphic( aGraphic, aFileName, *pIStm, nFilter ) : 1;
212 : :
213 [ # # ]: 0 : if( nReturn )
214 [ # # ][ # # ]: 0 : HandleGraphicFilterError( nReturn, rGraphicFilter.GetLastError().nStreamError );
215 : : else
216 : : {
217 [ # # ][ # # ]: 0 : if( mrDocument.GetPageCount() == 0L )
218 [ # # ]: 0 : mrDocument.CreateFirstPages();
219 : :
220 [ # # ]: 0 : SdPage* pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
221 : 0 : Point aPos;
222 [ # # ]: 0 : Size aPagSize( pPage->GetSize() );
223 : : Size aGrfSize( OutputDevice::LogicToLogic( aGraphic.GetPrefSize(),
224 [ # # ][ # # ]: 0 : aGraphic.GetPrefMapMode(), MAP_100TH_MM ) );
[ # # ][ # # ]
[ # # ][ # # ]
225 : :
226 [ # # ][ # # ]: 0 : aPagSize.Width() -= pPage->GetLftBorder() + pPage->GetRgtBorder();
227 [ # # ][ # # ]: 0 : aPagSize.Height() -= pPage->GetUppBorder() + pPage->GetLwrBorder();
228 : :
229 : : // scale to fit page
230 [ # # # # : 0 : if ( ( ( aGrfSize.Height() > aPagSize.Height() ) || ( aGrfSize.Width() > aPagSize.Width() ) ) &&
# # ][ # # ]
[ # # ]
231 : 0 : aGrfSize.Height() && aPagSize.Height() )
232 : : {
233 : 0 : double fGrfWH = (double) aGrfSize.Width() / aGrfSize.Height();
234 : 0 : double fWinWH = (double) aPagSize.Width() / aPagSize.Height();
235 : :
236 : : // Grafik an Pagesize anpassen (skaliert)
237 [ # # ]: 0 : if( fGrfWH < fWinWH )
238 : : {
239 : 0 : aGrfSize.Width() = (long) ( aPagSize.Height() * fGrfWH );
240 : 0 : aGrfSize.Height() = aPagSize.Height();
241 : : }
242 [ # # ]: 0 : else if( fGrfWH > 0.F )
243 : : {
244 : 0 : aGrfSize.Width() = aPagSize.Width();
245 : 0 : aGrfSize.Height()= (long) ( aPagSize.Width() / fGrfWH );
246 : : }
247 : : }
248 : :
249 : : // Ausgaberechteck fuer Grafik setzen
250 [ # # ]: 0 : aPos.X() = ( ( aPagSize.Width() - aGrfSize.Width() ) >> 1 ) + pPage->GetLftBorder();
251 [ # # ]: 0 : aPos.Y() = ( ( aPagSize.Height() - aGrfSize.Height() ) >> 1 ) + pPage->GetUppBorder();
252 : :
253 [ # # ][ # # ]: 0 : pPage->InsertObject( new SdrGrafObj( aGraphic, Rectangle( aPos, aGrfSize ) ) );
[ # # ][ # # ]
254 : 0 : bRet = sal_True;
255 : : }
256 [ # # ][ # # ]: 0 : return bRet;
257 : : }
258 : :
259 : : // -----------------------------------------------------------------------------
260 : :
261 : 0 : sal_Bool SdGRFFilter::Export()
262 : : {
263 : : // SJ: todo: error handling, the GraphicExportFilter does not support proper errorhandling
264 : :
265 : 0 : sal_Bool bRet = sal_False;
266 : :
267 : : uno::Reference< lang::XMultiServiceFactory >
268 [ # # ]: 0 : xSMgr( ::comphelper::getProcessServiceFactory() );
269 : : uno::Reference< uno::XInterface > xComponent
270 [ # # ]: 0 : ( xSMgr->createInstance( "com.sun.star.drawing.GraphicExportFilter" ),
271 [ # # ][ # # ]: 0 : uno::UNO_QUERY );
272 [ # # ]: 0 : if ( xComponent.is() )
273 : : {
274 : : uno::Reference< document::XExporter > xExporter
275 [ # # ]: 0 : ( xComponent, uno::UNO_QUERY );
276 : : uno::Reference< document::XFilter > xFilter
277 [ # # ]: 0 : ( xComponent, uno::UNO_QUERY );
278 [ # # ][ # # ]: 0 : if ( xExporter.is() && xFilter.is() )
[ # # ]
279 : : {
280 : 0 : SdPage* pPage = NULL;
281 : : sd::DrawViewShell* pDrawViewShell = static_cast< ::sd::DrawViewShell* >
282 [ # # ][ # # ]: 0 : ( ( ( mrDocShell.GetViewShell() && mrDocShell.GetViewShell()->ISA(::sd::DrawViewShell ) ) ? mrDocShell.GetViewShell() : NULL ) );
[ # # ][ # # ]
283 : :
284 : 0 : PageKind ePageKind = PK_STANDARD;
285 [ # # ]: 0 : if( pDrawViewShell )
286 : : {
287 : 0 : ePageKind = pDrawViewShell->GetPageKind();
288 [ # # ]: 0 : if( PK_HANDOUT == ePageKind )
289 [ # # ]: 0 : pPage = mrDocument.GetSdPage( 0, PK_HANDOUT );
290 : : else
291 [ # # ]: 0 : pPage = pDrawViewShell->GetActualPage();
292 : : }
293 : : else
294 [ # # ]: 0 : pPage = mrDocument.GetSdPage( 0, PK_STANDARD );
295 : :
296 [ # # ]: 0 : if ( pPage )
297 : : {
298 : : // taking the 'correct' page number, seems that there might exist a better method to archive this
299 [ # # ][ # # ]: 0 : pPage = mrDocument.GetSdPage( pPage->GetPageNum() ? ( pPage->GetPageNum() - 1 ) >> 1 : 0, ePageKind );
[ # # ][ # # ]
300 [ # # ]: 0 : if ( pPage )
301 : : {
302 [ # # ][ # # ]: 0 : uno::Reference< lang::XComponent > xSource( pPage->getUnoPage(), uno::UNO_QUERY );
303 [ # # ]: 0 : SfxItemSet* pSet = mrMedium.GetItemSet();
304 [ # # ][ # # ]: 0 : if ( pSet && xSource.is() )
[ # # ]
305 : : {
306 [ # # ][ # # ]: 0 : const String aTypeName( mrMedium.GetFilter()->GetTypeName() );
307 [ # # ]: 0 : GraphicFilter &rGraphicFilter = GraphicFilter::GetGraphicFilter();
308 [ # # ]: 0 : const sal_uInt16 nFilter = rGraphicFilter.GetExportFormatNumberForTypeName( aTypeName );
309 [ # # ]: 0 : if ( nFilter != GRFILTER_FORMAT_NOTFOUND )
310 : : {
311 : 0 : uno::Reference< task::XInteractionHandler > mXInteractionHandler;
312 : :
313 [ # # ]: 0 : beans::PropertyValues aArgs;
314 [ # # ]: 0 : TransformItems( SID_SAVEASDOC, *pSet, aArgs );
315 : :
316 : 0 : rtl::OUString sInteractionHandler( "InteractionHandler" );
317 : 0 : rtl::OUString sFilterName( "FilterName" );
318 [ # # ][ # # ]: 0 : rtl::OUString sShortName( rGraphicFilter.GetExportFormatShortName( nFilter ) );
[ # # ]
319 : :
320 : 0 : sal_Bool bFilterNameFound = sal_False;
321 : : sal_Int32 i, nCount;
322 [ # # ]: 0 : for ( i = 0, nCount = aArgs.getLength(); i < nCount; i++ )
323 : : {
324 [ # # ]: 0 : rtl::OUString& rStr = aArgs[ i ].Name;
325 [ # # ]: 0 : if ( rStr == sFilterName )
326 : : {
327 : 0 : bFilterNameFound = sal_True;
328 [ # # ]: 0 : aArgs[ i ].Name = sFilterName;
329 [ # # ][ # # ]: 0 : aArgs[ i ].Value <<= sShortName;
330 : : }
331 [ # # ]: 0 : else if ( rStr == sInteractionHandler )
332 : : {
333 : 0 : uno::Reference< task::XInteractionHandler > xHdl;
334 [ # # ][ # # ]: 0 : if ( aArgs[ i ].Value >>= xHdl )
[ # # ]
335 : : {
336 [ # # ][ # # ]: 0 : mXInteractionHandler = new SdGRFFilter_ImplInteractionHdl( xHdl );
[ # # ]
337 [ # # ][ # # ]: 0 : aArgs[ i ].Value <<= mXInteractionHandler;
338 : 0 : }
339 : : }
340 : : }
341 [ # # ]: 0 : if ( !bFilterNameFound )
342 : : {
343 [ # # ]: 0 : aArgs.realloc( ++nCount );
344 [ # # ]: 0 : aArgs[ i ].Name = sFilterName;
345 [ # # ][ # # ]: 0 : aArgs[ i ].Value <<= sShortName;
346 : : }
347 : :
348 : : // take selection if needed
349 [ # # ]: 0 : if( ( SFX_ITEM_SET == pSet->GetItemState( SID_SELECTION ) )
[ # # # # ]
[ # # ][ # # ]
350 [ # # ]: 0 : && static_cast< const SfxBoolItem& >( pSet->Get( SID_SELECTION ) ).GetValue()
351 : : && pDrawViewShell )
352 : : {
353 : : uno::Reference< view::XSelectionSupplier > xSelectionSupplier(
354 [ # # ][ # # ]: 0 : pDrawViewShell->GetViewShellBase().GetController(), uno::UNO_QUERY );
[ # # ]
355 [ # # ]: 0 : if ( xSelectionSupplier.is() )
356 : : {
357 [ # # ][ # # ]: 0 : uno::Any aSelection( xSelectionSupplier->getSelection() );
358 : 0 : uno::Reference< lang::XComponent > xSelection;
359 [ # # ][ # # ]: 0 : if ( aSelection >>= xSelection )
360 [ # # ]: 0 : xSource = xSelection;
361 : 0 : }
362 : : }
363 [ # # ][ # # ]: 0 : xExporter->setSourceDocument( xSource );
364 [ # # ][ # # ]: 0 : bRet = xFilter->filter( aArgs );
365 [ # # ][ # # ]: 0 : if ( !bRet && mXInteractionHandler.is() )
[ # # ]
366 : : SdGRFFilter::HandleGraphicFilterError(
367 [ # # ][ # # ]: 0 : static_cast< SdGRFFilter_ImplInteractionHdl* >( mXInteractionHandler.get() )->GetErrorCode(),
368 [ # # # # ]: 0 : rGraphicFilter.GetLastError().nStreamError );
[ # # ]
369 [ # # ]: 0 : }
370 : 0 : }
371 : : }
372 : : }
373 : 0 : }
374 : : }
375 : 0 : return bRet;
376 : : }
377 : :
378 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|