xref: /aoo41x/main/sw/source/ui/vba/vbadocuments.cxx (revision efeef26f)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
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 #include <comphelper/processfactory.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
26cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
29cdf0e10cSrcweir #include <com/sun/star/text/XTextDocument.hpp>
30cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
31cdf0e10cSrcweir #include <com/sun/star/frame/XComponentLoader.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
34cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
35cdf0e10cSrcweir #include <com/sun/star/frame/FrameSearchFlag.hpp>
36cdf0e10cSrcweir #include <com/sun/star/util/XModifiable.hpp>
37cdf0e10cSrcweir #include <com/sun/star/frame/XStorable.hpp>
38cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyVetoException.hpp>
40cdf0e10cSrcweir #include <com/sun/star/util/XCloseable.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
42cdf0e10cSrcweir #include <com/sun/star/document/XTypeDetection.hpp>
43cdf0e10cSrcweir #include <com/sun/star/uri/XUriReference.hpp>
44cdf0e10cSrcweir #include <com/sun/star/uri/XUriReferenceFactory.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <sfx2/objsh.hxx>
47cdf0e10cSrcweir #include <tools/urlobj.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include "vbaglobals.hxx"
50cdf0e10cSrcweir #include "vbadocument.hxx"
51cdf0e10cSrcweir #include "vbadocuments.hxx"
52cdf0e10cSrcweir #include <vbahelper/vbahelper.hxx>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #include <hash_map>
55cdf0e10cSrcweir #include <osl/file.hxx>
56cdf0e10cSrcweir using namespace ::ooo::vba;
57cdf0e10cSrcweir using namespace ::com::sun::star;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir static uno::Any
getDocument(uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<text::XTextDocument> & xDoc,const uno::Any & aApplication)60cdf0e10cSrcweir getDocument( uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< text::XTextDocument > &xDoc, const uno::Any& aApplication )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	// FIXME: fine as long as SwVbaDocument is stateless ...
63cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
64cdf0e10cSrcweir 	if( !xModel.is() )
65cdf0e10cSrcweir 		return uno::Any();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	SwVbaDocument *pWb = new SwVbaDocument(  uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xModel );
68cdf0e10cSrcweir 	return uno::Any( uno::Reference< word::XDocument > (pWb) );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir class DocumentEnumImpl : public EnumerationHelperImpl
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	uno::Any m_aApplication;
74cdf0e10cSrcweir public:
DocumentEnumImpl(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration,const uno::Any & aApplication)75cdf0e10cSrcweir 	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 ) {}
76cdf0e10cSrcweir 
nextElement()77cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		uno::Reference< text::XTextDocument > xDoc( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
80cdf0e10cSrcweir 		return getDocument( m_xContext, xDoc, m_aApplication );
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir };
83cdf0e10cSrcweir 
SwVbaDocuments(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext)84cdf0e10cSrcweir SwVbaDocuments::SwVbaDocuments( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : SwVbaDocuments_BASE( xParent, xContext, VbaDocumentsBase::WORD_DOCUMENT )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir }
87cdf0e10cSrcweir // XEnumerationAccess
88cdf0e10cSrcweir uno::Type
getElementType()89cdf0e10cSrcweir SwVbaDocuments::getElementType() throw (uno::RuntimeException)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir 	return word::XDocument::static_type(0);
92cdf0e10cSrcweir }
93cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()94cdf0e10cSrcweir SwVbaDocuments::createEnumeration() throw (uno::RuntimeException)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	// #FIXME its possible the DocumentEnumImpl here doens't reflect
97cdf0e10cSrcweir 	// the state of this object ( although it should ) would be
98cdf0e10cSrcweir 	// safer to create an enumeration based on this objects state
99cdf0e10cSrcweir 	// rather than one effectively based of the desktop component
100cdf0e10cSrcweir     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
101cdf0e10cSrcweir 	return new DocumentEnumImpl( mxParent, mxContext, xEnumerationAccess->createEnumeration(), Application() );
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir uno::Any
createCollectionObject(const uno::Any & aSource)105cdf0e10cSrcweir SwVbaDocuments::createCollectionObject( const uno::Any& aSource )
106cdf0e10cSrcweir {
107cdf0e10cSrcweir 	uno::Reference< text::XTextDocument > xDoc( aSource, uno::UNO_QUERY_THROW );
108cdf0e10cSrcweir 	return getDocument( mxContext, xDoc, Application() );
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir uno::Any SAL_CALL
Add(const uno::Any & Template,const uno::Any &,const uno::Any &,const uno::Any &)112cdf0e10cSrcweir SwVbaDocuments::Add( const uno::Any& Template, const uno::Any& /*NewTemplate*/, const uno::Any& /*DocumentType*/, const uno::Any& /*Visible*/ ) throw (uno::RuntimeException)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     rtl::OUString sFileName;
115cdf0e10cSrcweir     if( Template.hasValue() && ( Template >>= sFileName ) )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         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());
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir     uno::Reference <text::XTextDocument> xTextDoc( createDocument() , uno::UNO_QUERY_THROW );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     if( xTextDoc.is() )
122cdf0e10cSrcweir         return getDocument( mxContext, xTextDoc, Application() );
123cdf0e10cSrcweir     return uno::Any();
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir // #TODO# #FIXME# can any of the unused params below be used?
127cdf0e10cSrcweir void SAL_CALL
Close(const uno::Any &,const uno::Any &,const uno::Any &)128cdf0e10cSrcweir SwVbaDocuments::Close( const uno::Any& /*SaveChanges*/, const uno::Any& /*OriginalFormat*/, const uno::Any& /*RouteDocument*/ ) throw (uno::RuntimeException)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     closeDocuments();
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir // #TODO# #FIXME# can any of the unused params below be used?
134cdf0e10cSrcweir uno::Any SAL_CALL
Open(const::rtl::OUString & Filename,const uno::Any &,const uno::Any & ReadOnly,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)135cdf0e10cSrcweir 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)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	// we need to detect if this is a URL, if not then assume its a file path
138cdf0e10cSrcweir     rtl::OUString aURL;
139cdf0e10cSrcweir     INetURLObject aObj;
140cdf0e10cSrcweir 	aObj.SetURL( Filename );
141cdf0e10cSrcweir 	bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
142cdf0e10cSrcweir 	if ( bIsURL )
143cdf0e10cSrcweir 		aURL = Filename;
144cdf0e10cSrcweir 	else
145cdf0e10cSrcweir 		osl::FileBase::getFileURLFromSystemPath( Filename, aURL );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	uno::Sequence< beans::PropertyValue > sProps(0);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	uno::Reference <text::XTextDocument> xSpreadDoc( openDocument( Filename, ReadOnly, sProps ), uno::UNO_QUERY_THROW );
150cdf0e10cSrcweir 	uno::Any aRet = getDocument( mxContext, xSpreadDoc, Application() );
151cdf0e10cSrcweir 	uno::Reference< word::XDocument > xDocument( aRet, uno::UNO_QUERY );
152cdf0e10cSrcweir 	if ( xDocument.is() )
153cdf0e10cSrcweir 		xDocument->Activate();
154cdf0e10cSrcweir 	return aRet;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir rtl::OUString&
getServiceImplName()158cdf0e10cSrcweir SwVbaDocuments::getServiceImplName()
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaDocuments") );
161cdf0e10cSrcweir 	return sImplName;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir uno::Sequence<rtl::OUString>
getServiceNames()165cdf0e10cSrcweir SwVbaDocuments::getServiceNames()
166cdf0e10cSrcweir {
167cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > sNames;
168cdf0e10cSrcweir 	if ( sNames.getLength() == 0 )
169cdf0e10cSrcweir 	{
170cdf0e10cSrcweir 		sNames.realloc( 1 );
171cdf0e10cSrcweir 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Documents") );
172cdf0e10cSrcweir 	}
173cdf0e10cSrcweir 	return sNames;
174cdf0e10cSrcweir }
175