xref: /trunk/main/filter/source/pdf/pdffilter.cxx (revision 9e0fc027)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 
27 #include "pdffilter.hxx"
28 #include "pdfexport.hxx"
29 #include <vcl/svapp.hxx>
30 #include <vcl/window.hxx>
31 #include <svl/outstrm.hxx>
32 #include <svtools/FilterConfigItem.hxx>
33 
34 // -------------
35 // - PDFFilter -
36 // -------------
37 
PDFFilter(const Reference<XMultiServiceFactory> & rxMSF)38 PDFFilter::PDFFilter( const Reference< XMultiServiceFactory > &rxMSF ) :
39     mxMSF( rxMSF )
40 {
41 }
42 
43 // -----------------------------------------------------------------------------
44 
~PDFFilter()45 PDFFilter::~PDFFilter()
46 {
47 }
48 
49 // -----------------------------------------------------------------------------
50 
implExport(const Sequence<PropertyValue> & rDescriptor)51 sal_Bool PDFFilter::implExport( const Sequence< PropertyValue >& rDescriptor )
52 {
53     Reference< XOutputStream >  xOStm;
54     Sequence< PropertyValue >   aFilterData;
55 	sal_Int32					nLength = rDescriptor.getLength();
56 	const PropertyValue*		pValue = rDescriptor.getConstArray();
57     sal_Bool                    bRet = sal_False;
58 	Reference< task::XStatusIndicator > xStatusIndicator;
59 	Reference< task::XInteractionHandler > xIH;
60 
61 	for ( sal_Int32 i = 0 ; ( i < nLength ) && !xOStm.is(); ++i)
62 	{
63     	if( pValue[ i ].Name.equalsAscii( "OutputStream" ) )
64 			pValue[ i ].Value >>= xOStm;
65 		else if( pValue[ i ].Name.equalsAscii( "FilterData" ) )
66 		    pValue[ i ].Value >>= aFilterData;
67 		else if ( pValue[ i ].Name.equalsAscii( "StatusIndicator" ) )
68 			pValue[ i ].Value >>= xStatusIndicator;
69 		else if( pValue[i].Name.equalsAscii( "InteractionHandler" ) )
70 		    pValue[i].Value >>= xIH;
71 	}
72 
73 	/* we don't get FilterData if we are exporting directly
74 	 to pdf, but we have to use the last user settings (especially for the CompressMode) */
75 	if ( !aFilterData.getLength() )
76 	{
77 	    FilterConfigItem aCfgItem( String( RTL_CONSTASCII_USTRINGPARAM( "Office.Common/Filter/PDF/Export/" ) ) );
78 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "UseLosslessCompression" ) ), sal_False );
79 		aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "Quality" ) ), 90 );
80 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "ReduceImageResolution" ) ), sal_False );
81 		aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "MaxImageResolution" ) ), 300 );
82 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "UseTaggedPDF" ) ), sal_False );
83 		aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "SelectPdfVersion" ) ), 0 );
84 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "ExportNotes" ) ), sal_False );
85 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "ExportNotesPages" ) ), sal_False );
86 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "UseTransitionEffects" ) ), sal_True );
87 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "IsSkipEmptyPages" ) ), sal_False );
88 		aCfgItem.ReadBool( String( RTL_CONSTASCII_USTRINGPARAM( "ExportFormFields" ) ), sal_True );
89 		aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "FormsType" ) ), 0 );
90 		aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "HideViewerToolbar" ) ), sal_False );
91         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "HideViewerMenubar" ) ), sal_False );
92         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "HideViewerWindowControls" ) ), sal_False );
93         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "ResizeWindowToInitialPage" ) ), sal_False );
94         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "CenterWindow" ) ), sal_False );
95         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "OpenInFullScreenMode" ) ), sal_False );
96         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "DisplayPDFDocumentTitle" ) ), sal_True );
97         aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "InitialView" ) ), 0 );
98         aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "Magnification" ) ), 0 );
99         aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "PageLayout" ) ), 0 );
100         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "FirstPageOnLeft" ) ), sal_False );
101         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "IsAddStream" ) ), sal_False );
102 //
103 // the encryption is not available when exporting directly, since the encryption is off by default and the selection
104 // (encrypt or not) is not persistent; it's available through macro though,
105 // provided the correct property values are set, see help
106 //
107 // now, the relative link stuff
108         aCfgItem.ReadBool( OUString( RTL_CONSTASCII_USTRINGPARAM( "ExportLinksRelativeFsys" ) ), sal_False );
109         aCfgItem.ReadInt32( OUString( RTL_CONSTASCII_USTRINGPARAM( "PDFViewSelection" ) ), 0 );
110         aCfgItem.ReadBool( OUString( RTL_CONSTASCII_USTRINGPARAM( "ConvertOOoTargetToPDFTarget" ) ), sal_False );
111         aCfgItem.ReadBool( OUString( RTL_CONSTASCII_USTRINGPARAM( "ExportBookmarksToPDFDestination" ) ), sal_False );
112 
113         aCfgItem.ReadBool(  String( RTL_CONSTASCII_USTRINGPARAM( "ExportBookmarks" ) ), sal_True );
114         aCfgItem.ReadInt32( String( RTL_CONSTASCII_USTRINGPARAM( "OpenBookmarkLevels" ) ), -1 );
115 		aFilterData = aCfgItem.GetFilterData();
116 	}
117     if( mxSrcDoc.is() && xOStm.is() )
118     {
119         PDFExport       aExport( mxSrcDoc, xStatusIndicator, xIH, mxMSF );
120         ::utl::TempFile aTempFile;
121 
122         aTempFile.EnableKillingFile();
123         bRet = aExport.Export( aTempFile.GetURL(), aFilterData );
124 
125         if( bRet )
126         {
127             SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( aTempFile.GetURL(), STREAM_READ );
128 
129             if( pIStm )
130             {
131                 SvOutputStream aOStm( xOStm );
132 
133                 aOStm << *pIStm;
134                 bRet = ( aOStm.Tell() && ( aOStm.GetError() == ERRCODE_NONE ) );
135 
136                 delete pIStm;
137             }
138         }
139     }
140 
141     return bRet;
142 }
143 
144 // -----------------------------------------------------------------------------
145 class FocusWindowWaitCursor
146 {
147     Window*         m_pFocusWindow;
148     public:
FocusWindowWaitCursor()149     FocusWindowWaitCursor() :
150         m_pFocusWindow( Application::GetFocusWindow() )
151     {
152         if( m_pFocusWindow )
153         {
154             m_pFocusWindow->AddEventListener( LINK( this, FocusWindowWaitCursor, DestroyedLink ) );
155             m_pFocusWindow->EnterWait();
156         }
157     }
~FocusWindowWaitCursor()158     ~FocusWindowWaitCursor()
159     {
160         if( m_pFocusWindow )
161         {
162             m_pFocusWindow->LeaveWait();
163             m_pFocusWindow->RemoveEventListener( LINK( this, FocusWindowWaitCursor, DestroyedLink ) );
164         }
165     }
166 
167     DECL_LINK( DestroyedLink, VclWindowEvent* );
168 };
169 
IMPL_LINK(FocusWindowWaitCursor,DestroyedLink,VclWindowEvent *,pEvent)170 IMPL_LINK( FocusWindowWaitCursor, DestroyedLink, VclWindowEvent*, pEvent )
171 {
172     if( pEvent->GetId() == VCLEVENT_OBJECT_DYING )
173         m_pFocusWindow = NULL;
174     return 0;
175 }
176 
filter(const Sequence<PropertyValue> & rDescriptor)177 sal_Bool SAL_CALL PDFFilter::filter( const Sequence< PropertyValue >& rDescriptor )
178 	throw (RuntimeException)
179 {
180     FocusWindowWaitCursor aCur;
181 
182     const sal_Bool bRet = implExport( rDescriptor );
183 
184 	return bRet;
185 }
186 
187 // -----------------------------------------------------------------------------
188 
cancel()189 void SAL_CALL PDFFilter::cancel( ) throw (RuntimeException)
190 {
191 }
192 
193 // -----------------------------------------------------------------------------
194 
setSourceDocument(const Reference<XComponent> & xDoc)195 void SAL_CALL PDFFilter::setSourceDocument( const Reference< XComponent >& xDoc )
196 	throw (IllegalArgumentException, RuntimeException)
197 {
198 	mxSrcDoc = xDoc;
199 }
200 
201 // -----------------------------------------------------------------------------
202 
initialize(const::com::sun::star::uno::Sequence<::com::sun::star::uno::Any> &)203 void SAL_CALL PDFFilter::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& )
204 	throw (Exception, RuntimeException)
205 {
206 }
207 
208 // -----------------------------------------------------------------------------
209 
PDFFilter_getImplementationName()210 OUString PDFFilter_getImplementationName ()
211 	throw (RuntimeException)
212 {
213 	return OUString ( RTL_CONSTASCII_USTRINGPARAM ( "com.sun.star.comp.PDF.PDFFilter" ) );
214 }
215 
216 // -----------------------------------------------------------------------------
217 
218 #define SERVICE_NAME "com.sun.star.document.PDFFilter"
219 
PDFFilter_supportsService(const OUString & ServiceName)220 sal_Bool SAL_CALL PDFFilter_supportsService( const OUString& ServiceName )
221 	throw (RuntimeException)
222 {
223     return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
224 }
225 
226 // -----------------------------------------------------------------------------
227 
PDFFilter_getSupportedServiceNames()228 Sequence< OUString > SAL_CALL PDFFilter_getSupportedServiceNames(  ) throw (RuntimeException)
229 {
230 	Sequence < OUString > aRet(1);
231     OUString* pArray = aRet.getArray();
232     pArray[0] =  OUString ( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
233     return aRet;
234 }
235 
236 #undef SERVICE_NAME
237 
238 // -----------------------------------------------------------------------------
239 
PDFFilter_createInstance(const Reference<XMultiServiceFactory> & rSMgr)240 Reference< XInterface > SAL_CALL PDFFilter_createInstance( const Reference< XMultiServiceFactory > & rSMgr) throw( Exception )
241 {
242 	return (cppu::OWeakObject*) new PDFFilter( rSMgr );
243 }
244 
245 // -----------------------------------------------------------------------------
246 
getImplementationName()247 OUString SAL_CALL PDFFilter::getImplementationName()
248 	throw (RuntimeException)
249 {
250 	return PDFFilter_getImplementationName();
251 }
252 
253 // -----------------------------------------------------------------------------
254 
supportsService(const OUString & rServiceName)255 sal_Bool SAL_CALL PDFFilter::supportsService( const OUString& rServiceName )
256 	throw (RuntimeException)
257 {
258     return PDFFilter_supportsService( rServiceName );
259 }
260 
261 // -----------------------------------------------------------------------------
262 
getSupportedServiceNames()263 ::com::sun::star::uno::Sequence< OUString > SAL_CALL PDFFilter::getSupportedServiceNames(  ) throw (RuntimeException)
264 {
265     return PDFFilter_getSupportedServiceNames();
266 }
267