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