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