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