1*efeef26fSAndrew Rist /**************************************************************
2*efeef26fSAndrew Rist  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*efeef26fSAndrew Rist  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19*efeef26fSAndrew Rist  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "rtfimportfilter.hxx"
25cdf0e10cSrcweir #include "../rtf/swparrtf.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <docsh.hxx>
28cdf0e10cSrcweir #include <doc.hxx>
29cdf0e10cSrcweir #include <pam.hxx>
30cdf0e10cSrcweir #include <unotxdoc.hxx>
31cdf0e10cSrcweir #include <swerror.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
34cdf0e10cSrcweir #include <comphelper/mediadescriptor.hxx>
35cdf0e10cSrcweir #include <unotools/ucbstreamhelper.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
38cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::comphelper;
41cdf0e10cSrcweir using namespace ::com::sun::star;
42cdf0e10cSrcweir using ::rtl::OUString;
43cdf0e10cSrcweir using rtl::OUStringToOString;
44cdf0e10cSrcweir 
RtfImportFilter(const uno::Reference<lang::XMultiServiceFactory> & xMSF)45cdf0e10cSrcweir RtfImportFilter::RtfImportFilter( const uno::Reference< lang::XMultiServiceFactory >& xMSF)  :
46cdf0e10cSrcweir     m_xMSF( xMSF )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
~RtfImportFilter()50cdf0e10cSrcweir RtfImportFilter::~RtfImportFilter()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
filter(const uno::Sequence<beans::PropertyValue> & aDescriptor)54cdf0e10cSrcweir sal_Bool RtfImportFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
55cdf0e10cSrcweir     throw (uno::RuntimeException)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     OSL_TRACE("%s", OSL_THIS_FUNC);
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     MediaDescriptor aMediaDesc = aDescriptor;
60cdf0e10cSrcweir     ::uno::Reference< io::XInputStream > xInputStream =
61cdf0e10cSrcweir         aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INPUTSTREAM(), uno::Reference< io::XInputStream >() );
62cdf0e10cSrcweir     SvStream* pStream = utl::UcbStreamHelper::CreateStream( xInputStream, sal_True );
63cdf0e10cSrcweir     if (!pStream)
64cdf0e10cSrcweir         return sal_False;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     // get SwDoc*
67cdf0e10cSrcweir     uno::Reference< uno::XInterface > xIfc( m_xDstDoc, uno::UNO_QUERY );
68cdf0e10cSrcweir     SwXTextDocument *pTxtDoc = dynamic_cast< SwXTextDocument * >( xIfc.get() );
69cdf0e10cSrcweir     if (!pTxtDoc)
70cdf0e10cSrcweir         return sal_False;
71cdf0e10cSrcweir     SwDoc *pDoc = pTxtDoc->GetDocShell()->GetDoc();
72cdf0e10cSrcweir     if (!pDoc)
73cdf0e10cSrcweir         return sal_False;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     // get SwPaM*
76cdf0e10cSrcweir     // NEEDSWORK should we care about partial imports? For now we just import
77cdf0e10cSrcweir     // the whole document
78cdf0e10cSrcweir     SwPaM aPam( pDoc->GetNodes().GetEndOfContent() );
79cdf0e10cSrcweir     aPam.SetMark();
80cdf0e10cSrcweir     aPam.Move( fnMoveBackward, fnGoDoc );
81cdf0e10cSrcweir     SwPaM *pCurPam = new SwPaM( *aPam.End(), *aPam.Start() );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     String aURL;
84cdf0e10cSrcweir     OUString sTemp;
85cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < aDescriptor.getLength(); i++ )
86cdf0e10cSrcweir     {
87cdf0e10cSrcweir         if( aDescriptor[i].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
88cdf0e10cSrcweir         {
89cdf0e10cSrcweir             aDescriptor[i].Value >>= sTemp;
90cdf0e10cSrcweir             aURL = sTemp;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     RtfReader aReader;
95cdf0e10cSrcweir     sal_Bool bRet = aReader.Read(pStream, *pDoc, aURL, *pCurPam) == 0;
96cdf0e10cSrcweir     delete pStream;
97cdf0e10cSrcweir     return bRet;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 
cancel()101cdf0e10cSrcweir void RtfImportFilter::cancel(  ) throw (uno::RuntimeException)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)105cdf0e10cSrcweir void RtfImportFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
106cdf0e10cSrcweir     throw (lang::IllegalArgumentException, uno::RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     m_xDstDoc = xDoc;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
112cdf0e10cSrcweir // UNO helpers
113cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////
114cdf0e10cSrcweir 
RtfImport_getImplementationName()115cdf0e10cSrcweir OUString RtfImport_getImplementationName()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPL_NAME_RTFIMPORT ) );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
RtfImport_getSupportedServiceNames()120cdf0e10cSrcweir uno::Sequence< OUString > SAL_CALL RtfImport_getSupportedServiceNames() throw()
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ImportFilter" ) );
123cdf0e10cSrcweir     const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
124cdf0e10cSrcweir     return aSeq;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
RtfImport_createInstance(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)127cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL RtfImport_createInstance(const uno::Reference< lang::XMultiServiceFactory > & rSMgr ) throw( uno::Exception )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     return (cppu::OWeakObject*) new RtfImportFilter( rSMgr );
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /* vi:set shiftwidth=4 expandtab: */
133