Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "vbahelper/vbadocumentbase.hxx"
21 : : #include "vbahelper/helperdecl.hxx"
22 : :
23 : : #include <com/sun/star/lang/DisposedException.hpp>
24 : : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
25 : : #include <com/sun/star/util/XModifiable.hpp>
26 : : #include <com/sun/star/util/XProtectable.hpp>
27 : : #include <com/sun/star/util/XCloseable.hpp>
28 : : #include <com/sun/star/util/URLTransformer.hpp>
29 : : #include <com/sun/star/util/XURLTransformer.hpp>
30 : : #include <com/sun/star/frame/XStorable.hpp>
31 : : #include <com/sun/star/frame/XFrame.hpp>
32 : : #include <com/sun/star/frame/XTitle.hpp>
33 : : #include <com/sun/star/document/XEmbeddedScripts.hpp> //Michael E. Bohn
34 : : #include <com/sun/star/beans/XPropertySet.hpp>
35 : : #include <ooo/vba/XApplicationBase.hpp>
36 : :
37 : : #include <cppuhelper/exc_hlp.hxx>
38 : : #include <comphelper/unwrapargs.hxx>
39 : : #include <tools/urlobj.hxx>
40 : : #include <osl/file.hxx>
41 : :
42 : : using namespace ::com::sun::star;
43 : : using namespace ::ooo::vba;
44 : :
45 [ # # ]: 0 : VbaDocumentBase::VbaDocumentBase( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext) :VbaDocumentBase_BASE( xParent, xContext ), mxModel(NULL)
46 : : {
47 : 0 : }
48 : :
49 : 0 : VbaDocumentBase::VbaDocumentBase( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< frame::XModel > xModel ) : VbaDocumentBase_BASE( xParent, xContext ), mxModel( xModel )
50 : : {
51 : 0 : }
52 : :
53 : 4 : VbaDocumentBase::VbaDocumentBase( uno::Sequence< uno::Any> const & args,
54 [ + - ][ + - ]: 4 : uno::Reference< uno::XComponentContext> const & xContext ) : VbaDocumentBase_BASE( getXSomethingFromArgs< XHelperInterface >( args, 0 ), xContext ), mxModel( getXSomethingFromArgs< frame::XModel >( args, 1 ) )
55 : : {
56 : 4 : }
57 : :
58 : : ::rtl::OUString
59 : 0 : VbaDocumentBase::getName() throw (uno::RuntimeException)
60 : : {
61 [ # # ][ # # ]: 0 : rtl::OUString sName = getModel()->getURL();
62 [ # # ]: 0 : if ( !sName.isEmpty() )
63 : : {
64 : :
65 [ # # ][ # # ]: 0 : INetURLObject aURL( getModel()->getURL() );
[ # # ][ # # ]
66 [ # # ][ # # ]: 0 : ::osl::File::getSystemPathFromFileURL( aURL.GetLastName(), sName );
[ # # ]
67 : : }
68 : : else
69 : : {
70 [ # # ][ # # ]: 0 : uno::Reference< frame::XTitle > xTitle( getModel(), uno::UNO_QUERY_THROW );
71 [ # # ][ # # ]: 0 : sName = xTitle->getTitle();
72 : 0 : sName = sName.trim();
73 : : }
74 : 0 : return sName;
75 : : }
76 : : ::rtl::OUString
77 : 0 : VbaDocumentBase::getPath() throw (uno::RuntimeException)
78 : : {
79 [ # # ][ # # ]: 0 : INetURLObject aURL( getModel()->getURL() );
[ # # ][ # # ]
80 [ # # ]: 0 : rtl::OUString sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
81 : 0 : rtl::OUString sPath;
82 [ # # ]: 0 : if( !sURL.isEmpty() )
83 : : {
84 [ # # ]: 0 : sURL = sURL.copy( 0, sURL.getLength() - aURL.GetLastName().getLength() - 1 );
85 [ # # ]: 0 : ::osl::File::getSystemPathFromFileURL( sURL, sPath );
86 : : }
87 [ # # ]: 0 : return sPath;
88 : : }
89 : :
90 : : ::rtl::OUString
91 : 0 : VbaDocumentBase::getFullName() throw (uno::RuntimeException)
92 : : {
93 : 0 : rtl::OUString sPath = getName();
94 : : //::osl::File::getSystemPathFromFileURL( getModel()->getURL(), sPath );
95 : 0 : return sPath;
96 : : }
97 : :
98 : : void
99 : 0 : VbaDocumentBase::Close( const uno::Any &rSaveArg, const uno::Any &rFileArg,
100 : : const uno::Any &rRouteArg ) throw (uno::RuntimeException)
101 : : {
102 : 0 : sal_Bool bSaveChanges = sal_False;
103 : 0 : rtl::OUString aFileName;
104 : 0 : sal_Bool bRouteWorkbook = sal_True;
105 : :
106 : 0 : rSaveArg >>= bSaveChanges;
107 : 0 : sal_Bool bFileName = ( rFileArg >>= aFileName );
108 : 0 : rRouteArg >>= bRouteWorkbook;
109 [ # # ][ # # ]: 0 : uno::Reference< frame::XStorable > xStorable( getModel(), uno::UNO_QUERY_THROW );
110 [ # # ][ # # ]: 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
111 : :
112 [ # # ]: 0 : if( bSaveChanges )
113 : : {
114 [ # # ][ # # ]: 0 : if( xStorable->isReadonly() )
[ # # ]
115 : : {
116 : : throw uno::RuntimeException(::rtl::OUString(
117 : : RTL_CONSTASCII_USTRINGPARAM( "Unable to save to a read only file ") ),
118 [ # # ][ # # ]: 0 : uno::Reference< XInterface >() );
119 : : }
120 [ # # ]: 0 : if( bFileName )
121 [ # # ][ # # ]: 0 : xStorable->storeAsURL( aFileName, uno::Sequence< beans::PropertyValue >(0) );
[ # # ][ # # ]
122 : : else
123 [ # # ][ # # ]: 0 : xStorable->store();
124 : : }
125 : : else
126 [ # # ][ # # ]: 0 : xModifiable->setModified( false );
127 : :
128 : : // first try to close the document using UI dispatch functionality
129 : 0 : sal_Bool bUIClose = sal_False;
130 : : try
131 : : {
132 [ # # ][ # # ]: 0 : uno::Reference< frame::XController > xController( getModel()->getCurrentController(), uno::UNO_SET_THROW );
[ # # ][ # # ]
133 [ # # ][ # # ]: 0 : uno::Reference< frame::XDispatchProvider > xDispatchProvider( xController->getFrame(), uno::UNO_QUERY_THROW );
[ # # ]
134 : :
135 [ # # ][ # # ]: 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
[ # # ]
136 [ # # ]: 0 : uno::Reference< util::XURLTransformer > xURLTransformer( util::URLTransformer::create(mxContext) );
137 : :
138 : 0 : util::URL aURL;
139 [ # # ]: 0 : aURL.Complete = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:CloseDoc" ) );
140 [ # # ][ # # ]: 0 : xURLTransformer->parseStrict( aURL );
141 : :
142 : : uno::Reference< css::frame::XDispatch > xDispatch(
143 [ # # ]: 0 : xDispatchProvider->queryDispatch( aURL, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_self" ) ), 0 ),
144 [ # # ][ # # ]: 0 : uno::UNO_SET_THROW );
[ # # ]
145 [ # # ][ # # ]: 0 : xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
[ # # ][ # # ]
146 [ # # ]: 0 : bUIClose = sal_True;
147 : : }
148 [ # # ]: 0 : catch(const uno::Exception&)
149 : : {
150 : : }
151 : :
152 [ # # ]: 0 : if ( !bUIClose )
153 : : {
154 : : // if it is not possible to use UI dispatch, try to close the model directly
155 [ # # ][ # # ]: 0 : uno::Reference< util::XCloseable > xCloseable( getModel(), uno::UNO_QUERY );
[ # # ]
156 [ # # ]: 0 : if( xCloseable.is() )
157 : : {
158 : : // use close(boolean DeliverOwnership)
159 : :
160 : : // The boolean parameter DeliverOwnership tells objects vetoing the close process that they may
161 : : // assume ownership if they object the closure by throwing a CloseVetoException
162 : : // Here we give up ownership. To be on the safe side, catch possible veto exception anyway.
163 : : try
164 : : {
165 [ # # ][ # # ]: 0 : xCloseable->close(sal_True);
166 : : }
167 [ # # ]: 0 : catch(const util::CloseVetoException&)
168 : : {
169 : : //close is cancelled, nothing to do
170 : : }
171 : : }
172 : : // If close is not supported by this model - try to dispose it.
173 : : // But if the model disagree with a reset request for the modify state
174 : : // we shouldn't do so. Otherwhise some strange things can happen.
175 : : else
176 : : {
177 [ # # ][ # # ]: 0 : uno::Reference< lang::XComponent > xDisposable ( getModel(), uno::UNO_QUERY );
[ # # ]
178 [ # # ]: 0 : if ( xDisposable.is() )
179 : : {
180 : : // To be on the safe side, catch possible veto exception anyway.
181 : : try
182 : : {
183 [ # # ][ # # ]: 0 : xDisposable->dispose();
184 : : }
185 [ # # ]: 0 : catch(const uno::Exception&)
186 : : {
187 : : }
188 : 0 : }
189 : 0 : }
190 : 0 : }
191 : 0 : }
192 : :
193 : : void
194 : 0 : VbaDocumentBase::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
195 : : {
196 : 0 : rtl::OUString rPassword;
197 [ # # ][ # # ]: 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
198 : : OSL_TRACE("Workbook::Protect stub");
199 [ # # ]: 0 : if( aPassword >>= rPassword )
200 [ # # ][ # # ]: 0 : xProt->protect( rPassword );
201 : : else
202 [ # # ][ # # ]: 0 : xProt->protect( rtl::OUString() );
203 : 0 : }
204 : :
205 : : void
206 : 0 : VbaDocumentBase::Unprotect( const uno::Any &aPassword ) throw (uno::RuntimeException)
207 : : {
208 : 0 : rtl::OUString rPassword;
209 [ # # ][ # # ]: 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
210 [ # # ][ # # ]: 0 : if( !xProt->isProtected() )
[ # # ]
211 : : throw uno::RuntimeException(::rtl::OUString(
212 : : RTL_CONSTASCII_USTRINGPARAM( "File is already unprotected" ) ),
213 [ # # ][ # # ]: 0 : uno::Reference< XInterface >() );
214 : : else
215 : : {
216 [ # # ]: 0 : if( aPassword >>= rPassword )
217 [ # # ][ # # ]: 0 : xProt->unprotect( rPassword );
218 : : else
219 [ # # ][ # # ]: 0 : xProt->unprotect( rtl::OUString() );
220 : 0 : }
221 : 0 : }
222 : :
223 : : void
224 : 0 : VbaDocumentBase::setSaved( sal_Bool bSave ) throw (uno::RuntimeException)
225 : : {
226 [ # # ][ # # ]: 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
227 : : try
228 : : {
229 [ # # ][ # # ]: 0 : xModifiable->setModified( !bSave );
230 : : }
231 [ # # ]: 0 : catch (const lang::DisposedException&)
232 : : {
233 : : // impossibility to set the modified state on disposed document should not trigger an error
234 : : }
235 [ # # # ]: 0 : catch (const beans::PropertyVetoException&)
236 : : {
237 [ # # ]: 0 : uno::Any aCaught( ::cppu::getCaughtException() );
238 : : throw lang::WrappedTargetRuntimeException(
239 : : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Can't change modified state of model!" ) ),
240 : : uno::Reference< uno::XInterface >(),
241 [ # # # # ]: 0 : aCaught );
242 : 0 : }
243 : 0 : }
244 : :
245 : : sal_Bool
246 : 0 : VbaDocumentBase::getSaved() throw (uno::RuntimeException)
247 : : {
248 [ # # ][ # # ]: 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
249 [ # # ][ # # ]: 0 : return !xModifiable->isModified();
250 : : }
251 : :
252 : : void
253 : 0 : VbaDocumentBase::Save() throw (uno::RuntimeException)
254 : : {
255 [ # # ]: 0 : rtl::OUString url = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Save"));
256 [ # # ]: 0 : uno::Reference< frame::XModel > xModel = getModel();
257 [ # # ]: 0 : dispatchRequests(xModel,url);
258 : 0 : }
259 : :
260 : : void
261 : 0 : VbaDocumentBase::Activate() throw (uno::RuntimeException)
262 : : {
263 [ # # ][ # # ]: 0 : uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
[ # # ][ # # ]
[ # # ][ # # ]
264 [ # # ][ # # ]: 0 : xFrame->activate();
265 : 0 : }
266 : :
267 : : uno::Any SAL_CALL
268 : 0 : VbaDocumentBase::getVBProject() throw (uno::RuntimeException)
269 : : {
270 [ # # ]: 0 : if( !mxVBProject.is() ) try
271 : : {
272 [ # # ][ # # ]: 0 : uno::Reference< XApplicationBase > xApp( Application(), uno::UNO_QUERY_THROW );
273 [ # # ][ # # ]: 0 : uno::Reference< XInterface > xVBE( xApp->getVBE(), uno::UNO_QUERY_THROW );
[ # # ]
274 [ # # ]: 0 : uno::Sequence< uno::Any > aArgs( 2 );
275 [ # # ][ # # ]: 0 : aArgs[ 0 ] <<= xVBE; // the VBE
276 [ # # ][ # # ]: 0 : aArgs[ 1 ] <<= getModel(); // document model for script container access
[ # # ]
277 [ # # ][ # # ]: 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
[ # # ]
278 [ # # ]: 0 : mxVBProject = xServiceManager->createInstanceWithArgumentsAndContext(
279 [ # # ][ # # ]: 0 : "ooo.vba.vbide.VBProject", aArgs, mxContext );
[ # # ][ # # ]
280 : : }
281 : 0 : catch(const uno::Exception&)
282 : : {
283 : : }
284 : 0 : return uno::Any( mxVBProject );
285 : : }
286 : :
287 : : rtl::OUString
288 : 0 : VbaDocumentBase::getServiceImplName()
289 : : {
290 : 0 : return rtl::OUString( "VbaDocumentBase" );
291 : : }
292 : :
293 : : uno::Sequence< rtl::OUString >
294 : 0 : VbaDocumentBase::getServiceNames()
295 : : {
296 [ # # ][ # # ]: 0 : static uno::Sequence< rtl::OUString > aServiceNames;
[ # # ][ # # ]
297 [ # # ]: 0 : if ( aServiceNames.getLength() == 0 )
298 : : {
299 : 0 : aServiceNames.realloc( 1 );
300 : 0 : aServiceNames[ 0 ] = "ooo.vba.VbaDocumentBase";
301 : : }
302 : 0 : return aServiceNames;
303 : : }
304 : :
305 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|