Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <cstdio>
22 :
23 : #include <comphelper/servicedecl.hxx>
24 : #include <uno/environment.h>
25 : #include <com/sun/star/drawing/XDrawPage.hpp>
26 : #include <com/sun/star/drawing/XDrawView.hpp>
27 : #include <com/sun/star/frame/XDesktop.hpp>
28 : #include <com/sun/star/frame/XController.hpp>
29 : #include <com/sun/star/view/XSelectionSupplier.hpp>
30 : #include <com/sun/star/drawing/XDrawSubController.hpp>
31 : #include <com/sun/star/container/XNamed.hpp>
32 : #include <com/sun/star/uno/XComponentContext.hpp>
33 : #include <com/sun/star/drawing/framework/XControllerManager.hpp>
34 : #include <com/sun/star/drawing/framework/XConfigurationController.hpp>
35 : #include <com/sun/star/drawing/framework/XConfiguration.hpp>
36 : #include <com/sun/star/drawing/framework/AnchorBindingMode.hpp>
37 : #include <com/sun/star/drawing/framework/XResourceId.hpp>
38 : #include <com/sun/star/drawing/framework/XResource.hpp>
39 : #include <com/sun/star/drawing/framework/XView.hpp>
40 : #include <com/sun/star/drawing/framework/ResourceId.hpp>
41 : #include <comphelper/processfactory.hxx>
42 :
43 : #include <osl/mutex.hxx>
44 :
45 : #include "svgfilter.hxx"
46 :
47 : using ::rtl::OUString;
48 : using namespace ::com::sun::star;
49 :
50 : // -------------
51 : // - SVGFilter -
52 : // -------------
53 :
54 0 : SVGFilter::SVGFilter( const Reference< XComponentContext >& rxCtx ) :
55 0 : mxMSF( rxCtx->getServiceManager(),
56 : uno::UNO_QUERY_THROW ),
57 : mpSVGDoc( NULL ),
58 : mpSVGExport( NULL ),
59 : mpSVGFontExport( NULL ),
60 : mpSVGWriter( NULL ),
61 : mpDefaultSdrPage( NULL ),
62 : mpSdrModel( NULL ),
63 : mbPresentation( sal_False ),
64 : mbExportAll( sal_False ),
65 0 : mpObjects( NULL )
66 :
67 : {
68 0 : }
69 :
70 : // -----------------------------------------------------------------------------
71 :
72 0 : SVGFilter::~SVGFilter()
73 : {
74 : DBG_ASSERT( mpSVGDoc == NULL, "mpSVGDoc not destroyed" );
75 : DBG_ASSERT( mpSVGExport == NULL, "mpSVGExport not destroyed" );
76 : DBG_ASSERT( mpSVGFontExport == NULL, "mpSVGFontExport not destroyed" );
77 : DBG_ASSERT( mpSVGWriter == NULL, "mpSVGWriter not destroyed" );
78 : DBG_ASSERT( mpObjects == NULL, "mpObjects not destroyed" );
79 0 : }
80 :
81 : // -----------------------------------------------------------------------------
82 :
83 0 : sal_Bool SAL_CALL SVGFilter::filter( const Sequence< PropertyValue >& rDescriptor )
84 : throw (RuntimeException)
85 : {
86 0 : SolarMutexGuard aGuard;
87 0 : Window* pFocusWindow = Application::GetFocusWindow();
88 : sal_Bool bRet;
89 :
90 0 : if( pFocusWindow )
91 0 : pFocusWindow->EnterWait();
92 :
93 0 : if( mxDstDoc.is() )
94 0 : bRet = implImport( rDescriptor );
95 : #ifndef DISABLE_EXPORT
96 0 : else if( mxSrcDoc.is() )
97 : {
98 0 : if( !mbExportAll && !mSelectedPages.hasElements() )
99 : {
100 0 : uno::Reference< frame::XDesktop > xDesktop(mxMSF->createInstance( "com.sun.star.frame.Desktop" ),
101 0 : uno::UNO_QUERY_THROW);
102 0 : uno::Reference< frame::XFrame > xFrame(xDesktop->getCurrentFrame(),
103 0 : uno::UNO_QUERY_THROW);
104 0 : uno::Reference<frame::XController > xController(xFrame->getController(),
105 0 : uno::UNO_QUERY_THROW);
106 : uno::Reference<drawing::XDrawView > xDrawView(xController,
107 0 : uno::UNO_QUERY_THROW);
108 : uno::Reference<drawing::framework::XControllerManager> xManager(xController,
109 0 : uno::UNO_QUERY_THROW);
110 0 : uno::Reference<drawing::framework::XConfigurationController> xConfigController(xManager->getConfigurationController());
111 :
112 : // which view configuration are we in?
113 : //
114 : // * traverse Impress resources to find slide preview pane, grab selection from there
115 : // * otherwise, fallback to current slide
116 : //
117 : uno::Sequence<uno::Reference<drawing::framework::XResourceId> > aResIds(
118 0 : xConfigController->getCurrentConfiguration()->getResources(
119 : uno::Reference<drawing::framework::XResourceId>(),
120 : "",
121 0 : drawing::framework::AnchorBindingMode_INDIRECT));
122 :
123 0 : for( sal_Int32 i=0; i<aResIds.getLength(); ++i )
124 : {
125 : // can we somehow obtain the slidesorter from the Impress framework?
126 0 : if( aResIds[i]->getResourceURL() == "private:resource/view/SlideSorter" )
127 : {
128 : // got it, grab current selection from there
129 : uno::Reference<drawing::framework::XResource> xRes(
130 0 : xConfigController->getResource(aResIds[i]));
131 :
132 : uno::Reference< view::XSelectionSupplier > xSelectionSupplier(
133 : xRes,
134 0 : uno::UNO_QUERY );
135 0 : if( xSelectionSupplier.is() )
136 : {
137 0 : uno::Any aSelection = xSelectionSupplier->getSelection();
138 0 : if( aSelection.hasValue() )
139 : {
140 0 : ObjectSequence aSelectedPageSequence;
141 0 : aSelection >>= aSelectedPageSequence;
142 0 : mSelectedPages.realloc( aSelectedPageSequence.getLength() );
143 0 : for( sal_Int32 j=0; j<mSelectedPages.getLength(); ++j )
144 : {
145 0 : uno::Reference< drawing::XDrawPage > xDrawPage( aSelectedPageSequence[j],
146 0 : uno::UNO_QUERY );
147 0 : mSelectedPages[j] = xDrawPage;
148 0 : }
149 :
150 : // and stop looping. it is likely not getting better
151 0 : break;
152 0 : }
153 0 : }
154 : }
155 : }
156 :
157 0 : if( !mSelectedPages.hasElements() )
158 : {
159 : // apparently failed to glean selection - fallback to current page
160 0 : mSelectedPages.realloc( 1 );
161 0 : mSelectedPages[0] = xDrawView->getCurrentPage();
162 0 : }
163 : }
164 :
165 : /*
166 : * Export all slides, or requested "PagePos"
167 : */
168 0 : if( !mSelectedPages.hasElements() )
169 : {
170 0 : sal_Int32 nLength = rDescriptor.getLength();
171 0 : const PropertyValue* pValue = rDescriptor.getConstArray();
172 0 : sal_Int32 nPageToExport = -1;
173 :
174 0 : for ( sal_Int32 i = 0 ; i < nLength; ++i)
175 : {
176 0 : if ( pValue[ i ].Name == "PagePos" )
177 : {
178 0 : pValue[ i ].Value >>= nPageToExport;
179 : }
180 : }
181 :
182 0 : uno::Reference< drawing::XMasterPagesSupplier > xMasterPagesSupplier( mxSrcDoc, uno::UNO_QUERY );
183 0 : uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( mxSrcDoc, uno::UNO_QUERY );
184 :
185 0 : if( xMasterPagesSupplier.is() && xDrawPagesSupplier.is() )
186 : {
187 0 : uno::Reference< drawing::XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), uno::UNO_QUERY );
188 0 : uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY );
189 0 : if( xMasterPages.is() && xMasterPages->getCount() &&
190 0 : xDrawPages.is() && xDrawPages->getCount() )
191 : {
192 0 : sal_Int32 nDPCount = xDrawPages->getCount();
193 :
194 0 : mSelectedPages.realloc( nPageToExport != -1 ? 1 : nDPCount );
195 : sal_Int32 i;
196 0 : for( i = 0; i < nDPCount; ++i )
197 : {
198 0 : if( nPageToExport != -1 && nPageToExport == i )
199 : {
200 0 : uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), uno::UNO_QUERY );
201 0 : mSelectedPages[0] = xDrawPage;
202 : }
203 : else
204 : {
205 0 : uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), uno::UNO_QUERY );
206 0 : mSelectedPages[i] = xDrawPage;
207 : }
208 : }
209 0 : }
210 0 : }
211 : }
212 :
213 : /*
214 : * We get all master page that are targeted by at least one draw page.
215 : * The master page are put in an unordered set.
216 : */
217 0 : ObjectSet aMasterPageTargetSet;
218 0 : for( sal_Int32 i = 0; i < mSelectedPages.getLength(); ++i )
219 : {
220 0 : uno::Reference< drawing::XMasterPageTarget > xMasterPageTarget( mSelectedPages[i], uno::UNO_QUERY );
221 0 : if( xMasterPageTarget.is() )
222 : {
223 0 : aMasterPageTargetSet.insert( xMasterPageTarget->getMasterPage() );
224 : }
225 0 : }
226 : // Later we move them to a uno::Sequence so we can get them by index
227 0 : mMasterPageTargets.realloc( aMasterPageTargetSet.size() );
228 0 : ObjectSet::const_iterator aElem = aMasterPageTargetSet.begin();
229 0 : for( sal_Int32 i = 0; aElem != aMasterPageTargetSet.end(); ++aElem, ++i)
230 : {
231 0 : uno::Reference< drawing::XDrawPage > xMasterPage( *aElem, uno::UNO_QUERY );
232 0 : mMasterPageTargets[i] = xMasterPage;
233 0 : }
234 :
235 0 : bRet = implExport( rDescriptor );
236 : }
237 : #endif
238 : else
239 0 : bRet = sal_False;
240 :
241 0 : if( pFocusWindow )
242 0 : pFocusWindow->LeaveWait();
243 :
244 0 : return bRet;
245 : }
246 :
247 : // -----------------------------------------------------------------------------
248 :
249 0 : void SAL_CALL SVGFilter::cancel( ) throw (RuntimeException)
250 : {
251 0 : }
252 :
253 : // -----------------------------------------------------------------------------
254 :
255 0 : void SAL_CALL SVGFilter::setSourceDocument( const Reference< XComponent >& xDoc )
256 : throw (IllegalArgumentException, RuntimeException)
257 : {
258 0 : mxSrcDoc = xDoc;
259 0 : }
260 :
261 : // -----------------------------------------------------------------------------
262 :
263 0 : void SAL_CALL SVGFilter::setTargetDocument( const Reference< XComponent >& xDoc )
264 : throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException)
265 : {
266 0 : mxDstDoc = xDoc;
267 0 : }
268 :
269 : // -----------------------------------------------------------------------------
270 :
271 0 : rtl::OUString SAL_CALL SVGFilter::detect( Sequence< PropertyValue >& io_rDescriptor ) throw (RuntimeException)
272 : {
273 0 : uno::Reference< io::XInputStream > xInput;
274 :
275 0 : const beans::PropertyValue* pAttribs = io_rDescriptor.getConstArray();
276 0 : const sal_Int32 nAttribs = io_rDescriptor.getLength();
277 0 : for( sal_Int32 i = 0; i < nAttribs; i++ )
278 : {
279 0 : if ( pAttribs[i].Name == "InputStream" )
280 0 : pAttribs[i].Value >>= xInput;
281 : }
282 :
283 0 : if( !xInput.is() )
284 0 : return rtl::OUString();
285 :
286 0 : uno::Reference< io::XSeekable > xSeek( xInput, uno::UNO_QUERY );
287 0 : if( xSeek.is() )
288 0 : xSeek->seek( 0 );
289 :
290 : // read the first 1024 bytes & check a few magic string
291 : // constants (heuristically)
292 0 : const sal_Int32 nLookAhead = 1024;
293 0 : uno::Sequence< sal_Int8 > aBuf( nLookAhead );
294 0 : const sal_uInt64 nBytes=xInput->readBytes(aBuf, nLookAhead);
295 0 : const sal_Int8* const pBuf=aBuf.getConstArray();
296 :
297 0 : sal_Int8 aMagic1[] = {'<', 's', 'v', 'g'};
298 0 : if( std::search(pBuf, pBuf+nBytes,
299 0 : aMagic1, aMagic1+sizeof(aMagic1)/sizeof(*aMagic1)) != pBuf+nBytes )
300 0 : return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("svg_Scalable_Vector_Graphics") );
301 :
302 0 : sal_Int8 aMagic2[] = {'D', 'O', 'C', 'T', 'Y', 'P', 'E', ' ', 's', 'v', 'g'};
303 0 : if( std::search(pBuf, pBuf+nBytes,
304 0 : aMagic2, aMagic2+sizeof(aMagic2)/sizeof(*aMagic2)) != pBuf+nBytes )
305 0 : return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("svg_Scalable_Vector_Graphics") );
306 :
307 0 : return rtl::OUString();
308 : }
309 :
310 : // -----------------------------------------------------------------------------
311 :
312 : namespace sdecl = comphelper::service_decl;
313 0 : sdecl::class_<SVGFilter> serviceImpl;
314 0 : const sdecl::ServiceDecl svgFilter(
315 : serviceImpl,
316 : "com.sun.star.comp.Draw.SVGFilter",
317 : "com.sun.star.document.ImportFilter;"
318 : "com.sun.star.document.ExportFilter;"
319 : "com.sun.star.document.ExtendedTypeDetection" );
320 :
321 : // The C shared lib entry points
322 0 : extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL svgfilter_component_getFactory(
323 : sal_Char const* pImplName,
324 : ::com::sun::star::lang::XMultiServiceFactory* pServiceManager,
325 : ::com::sun::star::registry::XRegistryKey* pRegistryKey )
326 : {
327 : return component_getFactoryHelper( pImplName, pServiceManager,
328 0 : pRegistryKey, svgFilter );
329 0 : }
330 :
331 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|