xref: /AOO41X/main/sw/source/ui/vba/vbadocuments.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
30*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyVetoException.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/document/XTypeDetection.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/uri/XUriReference.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <sfx2/objsh.hxx>
51*cdf0e10cSrcweir #include <tools/urlobj.hxx>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include "vbaglobals.hxx"
54*cdf0e10cSrcweir #include "vbadocument.hxx"
55*cdf0e10cSrcweir #include "vbadocuments.hxx"
56*cdf0e10cSrcweir #include <vbahelper/vbahelper.hxx>
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir #include <hash_map>
59*cdf0e10cSrcweir #include <osl/file.hxx>
60*cdf0e10cSrcweir using namespace ::ooo::vba;
61*cdf0e10cSrcweir using namespace ::com::sun::star;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir static uno::Any
64*cdf0e10cSrcweir getDocument( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< text::XTextDocument > &xDoc, const uno::Any& aApplication )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir 	// FIXME: fine as long as SwVbaDocument is stateless ...
67*cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
68*cdf0e10cSrcweir 	if( !xModel.is() )
69*cdf0e10cSrcweir 		return uno::Any();
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	SwVbaDocument *pWb = new SwVbaDocument(  uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xModel );
72*cdf0e10cSrcweir 	return uno::Any( uno::Reference< word::XDocument > (pWb) );
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir class DocumentEnumImpl : public EnumerationHelperImpl
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 	uno::Any m_aApplication;
78*cdf0e10cSrcweir public:
79*cdf0e10cSrcweir 	DocumentEnumImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Any& aApplication ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_aApplication( aApplication ) {}
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
82*cdf0e10cSrcweir 	{
83*cdf0e10cSrcweir 		uno::Reference< text::XTextDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
84*cdf0e10cSrcweir 		return getDocument( m_xContext, xDoc, m_aApplication );
85*cdf0e10cSrcweir 	}
86*cdf0e10cSrcweir };
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir SwVbaDocuments::SwVbaDocuments( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : SwVbaDocuments_BASE( xParent, xContext, VbaDocumentsBase::WORD_DOCUMENT )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir // XEnumerationAccess
92*cdf0e10cSrcweir uno::Type
93*cdf0e10cSrcweir SwVbaDocuments::getElementType() throw (uno::RuntimeException)
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 	return word::XDocument::static_type(0);
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir uno::Reference< container::XEnumeration >
98*cdf0e10cSrcweir SwVbaDocuments::createEnumeration() throw (uno::RuntimeException)
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	// #FIXME its possible the DocumentEnumImpl here doens't reflect
101*cdf0e10cSrcweir 	// the state of this object ( although it should ) would be
102*cdf0e10cSrcweir 	// safer to create an enumeration based on this objects state
103*cdf0e10cSrcweir 	// rather than one effectively based of the desktop component
104*cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
105*cdf0e10cSrcweir 	return new DocumentEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration(), Application() );
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir uno::Any
109*cdf0e10cSrcweir SwVbaDocuments::createCollectionObject( const uno::Any& aSource )
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir 	uno::Reference< text::XTextDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
112*cdf0e10cSrcweir 	return getDocument( mxContext, xDoc, Application() );
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir uno::Any SAL_CALL
116*cdf0e10cSrcweir SwVbaDocuments::Add( const uno::Any& Template, const uno::Any& /*NewTemplate*/, const uno::Any& /*DocumentType*/, const uno::Any& /*Visible*/ ) throw (uno::RuntimeException)
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir     rtl::OUString sFileName;
119*cdf0e10cSrcweir     if( Template.hasValue() && ( Template >>= sFileName ) )
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         return  Open( sFileName, uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any(), uno::Any());
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir     uno::Reference <text::XTextDocument> xTextDoc( createDocument() , uno::UNO_QUERY_THROW );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     if( xTextDoc.is() )
126*cdf0e10cSrcweir         return getDocument( mxContext, xTextDoc, Application() );
127*cdf0e10cSrcweir     return uno::Any();
128*cdf0e10cSrcweir }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir // #TODO# #FIXME# can any of the unused params below be used?
131*cdf0e10cSrcweir void SAL_CALL
132*cdf0e10cSrcweir SwVbaDocuments::Close( const uno::Any& /*SaveChanges*/, const uno::Any& /*OriginalFormat*/, const uno::Any& /*RouteDocument*/ ) throw (uno::RuntimeException)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     closeDocuments();
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir // #TODO# #FIXME# can any of the unused params below be used?
138*cdf0e10cSrcweir uno::Any SAL_CALL
139*cdf0e10cSrcweir SwVbaDocuments::Open( const ::rtl::OUString& Filename, const uno::Any& /*ConfirmConversions*/, const uno::Any& ReadOnly, const uno::Any& /*AddToRecentFiles*/, const uno::Any& /*PasswordDocument*/, const uno::Any& /*PasswordTemplate*/, const uno::Any& /*Revert*/, const uno::Any& /*WritePasswordDocument*/, const uno::Any& /*WritePasswordTemplate*/, const uno::Any& /*Format*/, const uno::Any& /*Encoding*/, const uno::Any& /*Visible*/, const uno::Any& /*OpenAndRepair*/, const uno::Any& /*DocumentDirection*/, const uno::Any& /*NoEncodingDialog*/, const uno::Any& /*XMLTransform*/ ) throw (uno::RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir 	// we need to detect if this is a URL, if not then assume its a file path
142*cdf0e10cSrcweir     rtl::OUString aURL;
143*cdf0e10cSrcweir     INetURLObject aObj;
144*cdf0e10cSrcweir 	aObj.SetURL( Filename );
145*cdf0e10cSrcweir 	bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
146*cdf0e10cSrcweir 	if ( bIsURL )
147*cdf0e10cSrcweir 		aURL = Filename;
148*cdf0e10cSrcweir 	else
149*cdf0e10cSrcweir 		osl::FileBase::getFileURLFromSystemPath( Filename, aURL );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > sProps(0);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 	uno::Reference <text::XTextDocument> xSpreadDoc( openDocument( Filename, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
154*cdf0e10cSrcweir 	uno::Any aRet = getDocument( mxContext, xSpreadDoc, Application() );
155*cdf0e10cSrcweir 	uno::Reference< word::XDocument > xDocument( aRet, uno::UNO_QUERY );
156*cdf0e10cSrcweir 	if ( xDocument.is() )
157*cdf0e10cSrcweir 		xDocument->Activate();
158*cdf0e10cSrcweir 	return aRet;
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir rtl::OUString&
162*cdf0e10cSrcweir SwVbaDocuments::getServiceImplName()
163*cdf0e10cSrcweir {
164*cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaDocuments") );
165*cdf0e10cSrcweir 	return sImplName;
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir uno::Sequence<rtl::OUString>
169*cdf0e10cSrcweir SwVbaDocuments::getServiceNames()
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
172*cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
173*cdf0e10cSrcweir 	{
174*cdf0e10cSrcweir 		sNames.realloc( 1 );
175*cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Documents") );
176*cdf0e10cSrcweir 	}
177*cdf0e10cSrcweir 	return sNames;
178*cdf0e10cSrcweir }
179