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     throw (uno::RuntimeException)
56 {
57     OSL_TRACE("%s", OSL_THIS_FUNC);
58 
59     MediaDescriptor aMediaDesc = aDescriptor;
60     ::uno::Reference< io::XInputStream > xInputStream =
61         aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_INPUTSTREAM(), uno::Reference< io::XInputStream >() );
62     SvStream* pStream = utl::UcbStreamHelper::CreateStream( xInputStream, sal_True );
63     if (!pStream)
64         return sal_False;
65 
66     // get SwDoc*
67     uno::Reference< uno::XInterface > xIfc( m_xDstDoc, uno::UNO_QUERY );
68     SwXTextDocument *pTxtDoc = dynamic_cast< SwXTextDocument * >( xIfc.get() );
69     if (!pTxtDoc)
70         return sal_False;
71     SwDoc *pDoc = pTxtDoc->GetDocShell()->GetDoc();
72     if (!pDoc)
73         return sal_False;
74 
75     // get SwPaM*
76     // NEEDSWORK should we care about partial imports? For now we just import
77     // the whole document
78     SwPaM aPam( pDoc->GetNodes().GetEndOfContent() );
79     aPam.SetMark();
80     aPam.Move( fnMoveBackward, fnGoDoc );
81     SwPaM *pCurPam = new SwPaM( *aPam.End(), *aPam.Start() );
82 
83     String aURL;
84     OUString sTemp;
85     for ( sal_Int32 i = 0; i < aDescriptor.getLength(); i++ )
86     {
87         if( aDescriptor[i].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
88         {
89             aDescriptor[i].Value >>= sTemp;
90             aURL = sTemp;
91         }
92     }
93 
94     RtfReader aReader;
95     sal_Bool bRet = aReader.Read(pStream, *pDoc, aURL, *pCurPam) == 0;
96     delete pStream;
97     return bRet;
98 }
99 
100 
cancel()101 void RtfImportFilter::cancel(  ) throw (uno::RuntimeException)
102 {
103 }
104 
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)105 void RtfImportFilter::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
106     throw (lang::IllegalArgumentException, uno::RuntimeException)
107 {
108     m_xDstDoc = xDoc;
109 }
110 
111 //////////////////////////////////////////////////////////////////////////
112 // UNO helpers
113 //////////////////////////////////////////////////////////////////////////
114 
RtfImport_getImplementationName()115 OUString RtfImport_getImplementationName()
116 {
117     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPL_NAME_RTFIMPORT ) );
118 }
119 
RtfImport_getSupportedServiceNames()120 uno::Sequence< OUString > SAL_CALL RtfImport_getSupportedServiceNames() throw()
121 {
122     const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ImportFilter" ) );
123     const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
124     return aSeq;
125 }
126 
RtfImport_createInstance(const uno::Reference<lang::XMultiServiceFactory> & rSMgr)127 uno::Reference< uno::XInterface > SAL_CALL RtfImport_createInstance(const uno::Reference< lang::XMultiServiceFactory > & rSMgr ) throw( uno::Exception )
128 {
129     return (cppu::OWeakObject*) new RtfImportFilter( rSMgr );
130 }
131 
132 /* vi:set shiftwidth=4 expandtab: */
133