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 #include <rtfexportfilter.hxx>
25 #include <rtfexport.hxx>
26 #include <rtfimportfilter.hxx>
27 
28 #include <docsh.hxx>
29 #include <doc.hxx>
30 #include <pam.hxx>
31 #include <unotxdoc.hxx>
32 
33 #include <cppuhelper/factory.hxx>
34 #include <comphelper/mediadescriptor.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 
37 using namespace ::comphelper;
38 using namespace ::com::sun::star;
39 using ::rtl::OUString;
40 
RtfExportFilter(const uno::Reference<lang::XMultiServiceFactory> & xMSF)41 RtfExportFilter::RtfExportFilter( const uno::Reference< lang::XMultiServiceFactory >& xMSF)  :
42     m_xMSF( xMSF )
43 {
44 }
45 
~RtfExportFilter()46 RtfExportFilter::~RtfExportFilter()
47 {
48 }
49 
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)50 sal_Bool RtfExportFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
51     throw (uno::RuntimeException)
52 {
53     OSL_TRACE("%s", OSL_THIS_FUNC);
54 
55     MediaDescriptor aMediaDesc = aDescriptor;
56     ::uno::Reference< io::XStream > xStream =
57         aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_STREAMFOROUTPUT(), uno::Reference< io::XStream >() );
58     m_pStream = utl::UcbStreamHelper::CreateStream( xStream, sal_True );
59     m_aWriter.SetStream(m_pStream);
60 
61     // get SwDoc*
62     uno::Reference< uno::XInterface > xIfc( m_xSrcDoc, uno::UNO_QUERY );
63     SwXTextDocument *pTxtDoc = dynamic_cast< SwXTextDocument * >( xIfc.get() );
64     if ( !pTxtDoc ) {
65         return sal_False;
66     }
67 
68     SwDoc *pDoc = pTxtDoc->GetDocShell()->GetDoc();
69     if ( !pDoc ) {
70         return sal_False;
71     }
72 
73     // get SwPaM*
74     // we get SwPaM for the entire document; copy&paste is handled internally, not via UNO
75     SwPaM aPam( pDoc->GetNodes().GetEndOfContent() );
76     aPam.SetMark();
77     aPam.Move( fnMoveBackward, fnGoDoc );
78 
79     SwPaM *pCurPam = new SwPaM( *aPam.End(), *aPam.Start() );
80 
81     // export the document
82     // (in a separate block so that it's destructed before the commit)
83     {
84         RtfExport aExport( this, pDoc, pCurPam, &aPam, NULL );
85         aExport.ExportDocument( true );
86     }
87 
88     // delete the pCurPam
89     if ( pCurPam )
90     {
91         while ( pCurPam->GetNext() != pCurPam )
92             delete pCurPam->GetNext();
93         delete pCurPam;
94     }
95     delete m_pStream;
96 
97     return sal_True;
98 }
99 
100 
cancel()101 void RtfExportFilter::cancel(  ) throw (uno::RuntimeException)
102 {
103 }
104 
setSourceDocument(const uno::Reference<lang::XComponent> & xDoc)105 void RtfExportFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
106     throw (lang::IllegalArgumentException, uno::RuntimeException)
107 {
108     m_xSrcDoc = xDoc;
109 }
110 
111 //////////////////////////////////////////////////////////////////////////
112 // UNO helpers
113 //////////////////////////////////////////////////////////////////////////
114 
RtfExport_getImplementationName()115 OUString RtfExport_getImplementationName()
116 {
117     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPL_NAME_RTFEXPORT ) );
118 }
119 
RtfExport_getSupportedServiceNames()120 uno::Sequence< OUString > SAL_CALL RtfExport_getSupportedServiceNames() throw()
121 {
122     const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportFilter" ) );
123     const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
124     return aSeq;
125 }
126 
RtfExport_createInstance(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)127 uno::Reference< uno::XInterface > SAL_CALL RtfExport_createInstance(const uno::Reference< lang::XMultiServiceFactory > & rSMgr ) throw( uno::Exception )
128 {
129     return (cppu::OWeakObject*) new RtfExportFilter( rSMgr );
130 }
131 #ifdef __cplusplus
132 extern "C"
133 {
134 #endif
135 
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)136 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
137 {
138     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
139 }
140 
141 // ------------------------
142 // - component_getFactory -
143 // ------------------------
144 
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)145 SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char* pImplName, void* pServiceManager, void* /* pRegistryKey */ )
146 {
147     OSL_TRACE("%s, pImplName is '%s'", OSL_THIS_FUNC, pImplName);
148     uno::Reference< lang::XSingleServiceFactory > xFactory;
149     void* pRet = 0;
150 
151     if ( rtl_str_compare( pImplName, IMPL_NAME_RTFEXPORT ) == 0 ) {
152         const OUString aServiceName( OUString::createFromAscii( IMPL_NAME_RTFEXPORT ) );
153 
154         xFactory = uno::Reference< lang::XSingleServiceFactory >( ::cppu::createSingleFactory(
155                     reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
156                     RtfExport_getImplementationName(),
157                     RtfExport_createInstance,
158                     RtfExport_getSupportedServiceNames() ) );
159     }
160     else if ( rtl_str_compare( pImplName, IMPL_NAME_RTFIMPORT ) == 0 ) {
161         const OUString aServiceName( OUString::createFromAscii( IMPL_NAME_RTFIMPORT ) );
162 
163         xFactory = uno::Reference< lang::XSingleServiceFactory >( ::cppu::createSingleFactory(
164                     reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
165                     RtfImport_getImplementationName(),
166                     RtfImport_createInstance,
167                     RtfImport_getSupportedServiceNames() ) );
168     }
169 
170     if ( xFactory.is() )
171     {
172         xFactory->acquire();
173         pRet = xFactory.get();
174     }
175 
176     return pRet;
177 }
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 /* vi:set shiftwidth=4 expandtab: */
184