xref: /trunk/main/sw/source/filter/ww8/rtfimportfilter.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 "rtfimportfilter.hxx"
25 #include "../rtf/swparrtf.hxx"
26 
27 #include <docsh.hxx>
28 #include <doc.hxx>
29 #include <pam.hxx>
30 #include <unotxdoc.hxx>
31 #include <swerror.h>
32 
33 #include <cppuhelper/factory.hxx>
34 #include <comphelper/mediadescriptor.hxx>
35 #include <unotools/ucbstreamhelper.hxx>
36 
37 #include <com/sun/star/frame/XDesktop.hpp>
38 #include <com/sun/star/frame/XComponentLoader.hpp>
39 
40 using namespace ::comphelper;
41 using namespace ::com::sun::star;
42 using ::rtl::OUString;
43 using rtl::OUStringToOString;
44 
RtfImportFilter(const uno::Reference<lang::XMultiServiceFactory> & xMSF)45 RtfImportFilter::RtfImportFilter( const uno::Reference< lang::XMultiServiceFactory >& xMSF)  :
46     m_xMSF( xMSF )
47 {
48 }
49 
~RtfImportFilter()50 RtfImportFilter::~RtfImportFilter()
51 {
52 }
53 
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)54 sal_Bool RtfImportFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
55 {
56     OSL_TRACE("%s", OSL_THIS_FUNC);
57 
58     MediaDescriptor aMediaDesc = aDescriptor;
59     ::uno::Reference< io::XInputStream > xInputStream =
60         aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INPUTSTREAM(), uno::Reference< io::XInputStream >() );
61     SvStream* pStream = utl::UcbStreamHelper::CreateStream( xInputStream, sal_True );
62     if (!pStream)
63         return sal_False;
64 
65     // get SwDoc*
66     uno::Reference< uno::XInterface > xIfc( m_xDstDoc, uno::UNO_QUERY );
67     SwXTextDocument *pTxtDoc = dynamic_cast< SwXTextDocument * >( xIfc.get() );
68     if (!pTxtDoc)
69         return sal_False;
70     SwDoc *pDoc = pTxtDoc->GetDocShell()->GetDoc();
71     if (!pDoc)
72         return sal_False;
73 
74     // get SwPaM*
75     // NEEDSWORK should we care about partial imports? For now we just import
76     // the whole document
77     SwPaM aPam( pDoc->GetNodes().GetEndOfContent() );
78     aPam.SetMark();
79     aPam.Move( fnMoveBackward, fnGoDoc );
80     SwPaM *pCurPam = new SwPaM( *aPam.End(), *aPam.Start() );
81 
82     String aURL;
83     OUString sTemp;
84     for ( sal_Int32 i = 0; i < aDescriptor.getLength(); i++ )
85     {
86         if( aDescriptor[i].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
87         {
88             aDescriptor[i].Value >>= sTemp;
89             aURL = sTemp;
90         }
91     }
92 
93     RtfReader aReader;
94     sal_Bool bRet = aReader.Read(pStream, *pDoc, aURL, *pCurPam) == 0;
95     delete pStream;
96     return bRet;
97 }
98 
99 
cancel()100 void RtfImportFilter::cancel(  )
101 {
102 }
103 
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)104 void RtfImportFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
105 {
106     m_xDstDoc = xDoc;
107 }
108 
109 //////////////////////////////////////////////////////////////////////////
110 // UNO helpers
111 //////////////////////////////////////////////////////////////////////////
112 
RtfImport_getImplementationName()113 OUString RtfImport_getImplementationName()
114 {
115     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPL_NAME_RTFIMPORT ) );
116 }
117 
RtfImport_getSupportedServiceNames()118 uno::Sequence< OUString > SAL_CALL RtfImport_getSupportedServiceNames() throw()
119 {
120     const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ImportFilter" ) );
121     const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
122     return aSeq;
123 }
124 
RtfImport_createInstance(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)125 uno::Reference< uno::XInterface > SAL_CALL RtfImport_createInstance(const uno::Reference< lang::XMultiServiceFactory > & rSMgr )
126 {
127     return (cppu::OWeakObject*) new RtfImportFilter( rSMgr );
128 }
129 
130 /* vi:set shiftwidth=4 expandtab: */
131