1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "docxexportfilter.hxx"
29 #include "rtfexportfilter.hxx"
30 #include "rtfimportfilter.hxx"
31 #include "docxexport.hxx"
32 
33 #include <docsh.hxx>
34 #include <pam.hxx>
35 #include <unotxdoc.hxx>
36 
37 #include <cppuhelper/factory.hxx>
38 
39 using namespace ::comphelper;
40 using namespace ::com::sun::star;
41 using ::rtl::OUString;
42 
43 #define S( x ) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
44 
45 DocxExportFilter::DocxExportFilter( const uno::Reference< lang::XMultiServiceFactory >& rMSF )
46     : oox::core::XmlFilterBase( rMSF )
47 {
48 }
49 
50 bool DocxExportFilter::exportDocument()
51 {
52     OSL_TRACE(, "DocxExportFilter::exportDocument()\n" ); // DEBUG remove me
53 
54     // get SwDoc*
55     uno::Reference< uno::XInterface > xIfc( getModel(), uno::UNO_QUERY );
56     SwXTextDocument *pTxtDoc = dynamic_cast< SwXTextDocument * >( xIfc.get() );
57     if ( !pTxtDoc )
58         return false;
59 
60     SwDoc *pDoc = pTxtDoc->GetDocShell()->GetDoc();
61     if ( !pDoc )
62         return false;
63 
64     // get SwPaM*
65     // FIXME so far we get SwPaM for the entire document; probably we should
66     // be able to output just the selection as well - though no idea how to
67     // get the correct SwPaM* then...
68     SwPaM aPam( pDoc->GetNodes().GetEndOfContent() );
69     aPam.SetMark();
70     aPam.Move( fnMoveBackward, fnGoDoc );
71 
72     SwPaM *pCurPam = new SwPaM( *aPam.End(), *aPam.Start() );
73 
74     // export the document
75     // (in a separate block so that it's destructed before the commit)
76     {
77         DocxExport aExport( this, pDoc, pCurPam, &aPam );
78         aExport.ExportDocument( true ); // FIXME support exporting selection only
79     }
80 
81     commit();
82 
83     // delete the pCurPam
84     if ( pCurPam )
85     {
86         while ( pCurPam->GetNext() != pCurPam )
87             delete pCurPam->GetNext();
88         delete pCurPam;
89     }
90 
91     return true;
92 }
93 
94 //////////////////////////////////////////////////////////////////////////
95 // UNO stuff so that the filter is registered
96 //////////////////////////////////////////////////////////////////////////
97 
98 #define IMPL_NAME "com.sun.star.comp.Writer.DocxExport"
99 
100 OUString DocxExport_getImplementationName()
101 {
102     return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPL_NAME ) );
103 }
104 
105 OUString DocxExportFilter::implGetImplementationName() const
106 {
107     return DocxExport_getImplementationName();
108 }
109 
110 uno::Sequence< OUString > SAL_CALL DocxExport_getSupportedServiceNames() throw()
111 {
112     const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportFilter" ) );
113     const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
114     return aSeq;
115 }
116 
117 uno::Reference< uno::XInterface > SAL_CALL DocxExport_createInstance(const uno::Reference< lang::XMultiServiceFactory > & rSMgr ) throw( uno::Exception )
118 {
119     return (cppu::OWeakObject*) new DocxExportFilter( rSMgr );
120 }
121 
122 #ifdef __cplusplus
123 extern "C"
124 {
125 #endif
126 
127 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment( const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */ )
128 {
129     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
130 }
131 
132 // ------------------------
133 // - component_getFactory -
134 // ------------------------
135 
136 SAL_DLLPUBLIC_EXPORT void* SAL_CALL component_getFactory( const sal_Char* pImplName, void* pServiceManager, void* /* pRegistryKey */ )
137 {
138     OSL_TRACE("%s, pImplName is '%s'", OSL_THIS_FUNC, pImplName);
139     uno::Reference< lang::XSingleServiceFactory > xFactory;
140     void* pRet = 0;
141 
142     if ( rtl_str_compare( pImplName, IMPL_NAME ) == 0 )
143     {
144         const OUString aServiceName( OUString::createFromAscii( IMPL_NAME ) );
145 
146         xFactory = uno::Reference< lang::XSingleServiceFactory >( ::cppu::createSingleFactory(
147                     reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
148                     DocxExport_getImplementationName(),
149                     DocxExport_createInstance,
150                     DocxExport_getSupportedServiceNames() ) );
151     } else 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     } else if ( rtl_str_compare( pImplName, IMPL_NAME_RTFIMPORT ) == 0 ) {
160         const OUString aServiceName( OUString::createFromAscii( IMPL_NAME_RTFIMPORT ) );
161 
162         xFactory = uno::Reference< lang::XSingleServiceFactory >( ::cppu::createSingleFactory(
163                     reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
164                     RtfImport_getImplementationName(),
165                     RtfImport_createInstance,
166                     RtfImport_getSupportedServiceNames() ) );
167     }
168 
169     if ( xFactory.is() )
170     {
171         xFactory->acquire();
172         pRet = xFactory.get();
173     }
174 
175     return pRet;
176 }
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
183