1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <vbahelper/vbadocumentbase.hxx>
23 #include <com/sun/star/beans/PropertyVetoException.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
26 #include <com/sun/star/util/XModifiable.hpp>
27 #include <com/sun/star/util/XProtectable.hpp>
28 #include <com/sun/star/util/XCloseable.hpp>
29 #include <com/sun/star/util/URLTransformer.hpp>
30 #include <com/sun/star/util/XURLTransformer.hpp>
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
32 #include <com/sun/star/frame/XStorable.hpp>
33 #include <com/sun/star/frame/XFrame.hpp>
34 #include <com/sun/star/frame/XTitle.hpp>
35 #include <ooo/vba/XApplicationBase.hpp>
37 #include <comphelper/automationinvokedzone.hxx>
38 #include <cppuhelper/exc_hlp.hxx>
39 #include <tools/urlobj.hxx>
40 #include <osl/file.hxx>
41 #include <sal/log.hxx>
43 using namespace ::com::sun::star
;
44 using namespace ::ooo::vba
;
46 VbaDocumentBase::VbaDocumentBase( const uno::Reference
< ov::XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, uno::Reference
< frame::XModel
> xModel
) : VbaDocumentBase_BASE( xParent
, xContext
), mxModel(std::move( xModel
))
50 VbaDocumentBase::VbaDocumentBase( uno::Sequence
< uno::Any
> const & args
,
51 uno::Reference
< uno::XComponentContext
> const & xContext
) : VbaDocumentBase_BASE( getXSomethingFromArgs
< XHelperInterface
>( args
, 0 ), xContext
), mxModel( getXSomethingFromArgs
< frame::XModel
>( args
, 1 ) )
56 VbaDocumentBase::getName()
58 OUString sName
= VbaDocumentBase::getNameFromModel( getModel() );
59 SAL_INFO("vbahelper", "VbaDocumentBase::getName: '" << sName
<< "'");
64 OUString
VbaDocumentBase::getNameFromModel( const uno::Reference
< frame::XModel
>& xModel
)
66 OUString sName
= xModel
.is() ? xModel
->getURL() : OUString();
67 if ( !sName
.isEmpty() )
70 INetURLObject
aURL( xModel
->getURL() );
71 ::osl::File::getSystemPathFromFileURL( aURL
.GetLastName(), sName
);
75 uno::Reference
< frame::XTitle
> xTitle( xModel
, uno::UNO_QUERY_THROW
);
76 sName
= xTitle
->getTitle();
82 VbaDocumentBase::getPath()
84 INetURLObject
aURL( getModel()->getURL() );
85 OUString sURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
);
89 sURL
= sURL
.copy( 0, sURL
.getLength() - aURL
.GetLastName().getLength() - 1 );
90 ::osl::File::getSystemPathFromFileURL( sURL
, sPath
);
92 SAL_INFO("vbahelper", "VbaDocumentBase::getPath: '" << sPath
<< "'");
98 VbaDocumentBase::getFullName()
100 // In the Automation case, follow the specs.
101 if (comphelper::Automation::AutomationInvokedZone::isActive())
103 // We know that Automation is relevant only on Windows, so hardcode "\\".
104 OUString sPath
= getPath() + "\\" + getName();
105 SAL_INFO("vbahelper", "VbaDocumentBase::getFullName: '" << sPath
<< "'");
109 OUString sPath
= getName();
110 //::osl::File::getSystemPathFromFileURL( getModel()->getURL(), sPath );
111 SAL_INFO("vbahelper", "VbaDocumentBase::getFullName: '" << sPath
<< "'");
116 VbaDocumentBase::Close( const uno::Any
&rSaveArg
, const uno::Any
&rFileArg
,
117 const uno::Any
&rRouteArg
)
119 bool bSaveChanges
= false;
121 bool bRouteWorkbook
= true;
123 rSaveArg
>>= bSaveChanges
;
124 bool bFileName
= ( rFileArg
>>= aFileName
);
125 rRouteArg
>>= bRouteWorkbook
;
126 uno::Reference
< frame::XStorable
> xStorable( getModel(), uno::UNO_QUERY_THROW
);
127 uno::Reference
< util::XModifiable
> xModifiable( getModel(), uno::UNO_QUERY_THROW
);
131 if( xStorable
->isReadonly() )
133 throw uno::RuntimeException("Unable to save to a read only file " );
136 xStorable
->storeAsURL( aFileName
, uno::Sequence
< beans::PropertyValue
>(0) );
141 xModifiable
->setModified( false );
143 // first try to close the document using UI dispatch functionality
144 bool bUIClose
= false;
147 uno::Reference
< frame::XController
> xController( getModel()->getCurrentController(), uno::UNO_SET_THROW
);
148 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider( xController
->getFrame(), uno::UNO_QUERY_THROW
);
150 uno::Reference
< lang::XMultiComponentFactory
> xServiceManager( mxContext
->getServiceManager(), uno::UNO_SET_THROW
);
151 uno::Reference
< util::XURLTransformer
> xURLTransformer( util::URLTransformer::create(mxContext
) );
154 aURL
.Complete
= ".uno:CloseDoc";
155 xURLTransformer
->parseStrict( aURL
);
157 uno::Reference
< css::frame::XDispatch
> xDispatch(
158 xDispatchProvider
->queryDispatch( aURL
, "_self" , 0 ),
159 uno::UNO_SET_THROW
);
160 xDispatch
->dispatch( aURL
, uno::Sequence
< beans::PropertyValue
>() );
163 catch(const uno::Exception
&)
170 // if it is not possible to use UI dispatch, try to close the model directly
171 bool bCloseable
= false;
172 uno::Reference
< frame::XModel
> xModel
= getModel();
175 uno::Reference
< util::XCloseable
> xCloseable( xModel
, uno::UNO_QUERY
);
177 // use close(boolean DeliverOwnership)
178 // The boolean parameter DeliverOwnership tells objects vetoing the close
179 // process that they may assume ownership if they object the closure by
180 // throwing a CloseVetoException. Here we give up ownership. To be on the
181 // safe side, catch possible veto exception anyway.
182 if ( xCloseable
.is() )
185 xCloseable
->close(true);
188 catch (const uno::Exception
&)
196 // If close is not supported by this model - try to dispose it.
197 // But if the model disagree with a reset request for the modify state
198 // we shouldn't do so. Otherwise some strange things can happen.
199 uno::Reference
< lang::XComponent
> xDisposable ( xModel
, uno::UNO_QUERY_THROW
);
200 xDisposable
->dispose();
202 catch(const uno::Exception
&)
208 VbaDocumentBase::Protect( const uno::Any
&aPassword
)
211 uno::Reference
< util::XProtectable
> xProt( getModel(), uno::UNO_QUERY_THROW
);
212 SAL_INFO("vbahelper", "Workbook::Protect stub");
213 if( aPassword
>>= rPassword
)
214 xProt
->protect( rPassword
);
216 xProt
->protect( OUString() );
220 VbaDocumentBase::Unprotect( const uno::Any
&aPassword
)
223 uno::Reference
< util::XProtectable
> xProt( getModel(), uno::UNO_QUERY_THROW
);
224 if( !xProt
->isProtected() )
225 throw uno::RuntimeException("File is already unprotected" );
226 if( aPassword
>>= rPassword
)
227 xProt
->unprotect( rPassword
);
229 xProt
->unprotect( OUString() );
233 VbaDocumentBase::setSaved( sal_Bool bSave
)
235 uno::Reference
< util::XModifiable
> xModifiable( getModel(), uno::UNO_QUERY_THROW
);
238 xModifiable
->setModified( !bSave
);
240 catch (const lang::DisposedException
&)
242 // impossibility to set the modified state on disposed document should not trigger an error
244 catch (const beans::PropertyVetoException
&)
246 uno::Any
aCaught( ::cppu::getCaughtException() );
247 throw lang::WrappedTargetRuntimeException(
248 "Can't change modified state of model!",
249 uno::Reference
< uno::XInterface
>(),
255 VbaDocumentBase::getSaved()
257 uno::Reference
< util::XModifiable
> xModifiable( getModel(), uno::UNO_QUERY_THROW
);
258 return !xModifiable
->isModified();
262 VbaDocumentBase::Save()
264 uno::Reference
< frame::XModel
> xModel
= getModel();
265 dispatchRequests(xModel
,".uno:Save");
269 VbaDocumentBase::Activate()
271 uno::Reference
< frame::XFrame
> xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_SET_THROW
);
276 VbaDocumentBase::getVBProject()
278 if( !mxVBProject
.is() ) try
280 uno::Reference
< XApplicationBase
> xApp( Application(), uno::UNO_QUERY_THROW
);
281 uno::Reference
< XInterface
> xVBE( xApp
->getVBE(), uno::UNO_QUERY_THROW
);
282 uno::Sequence
< uno::Any
> aArgs
{ uno::Any(xVBE
), // the VBE
283 uno::Any(getModel()) }; // document model for script container access
284 uno::Reference
< lang::XMultiComponentFactory
> xServiceManager( mxContext
->getServiceManager(), uno::UNO_SET_THROW
);
285 mxVBProject
= xServiceManager
->createInstanceWithArgumentsAndContext(
286 "ooo.vba.vbide.VBProject", aArgs
, mxContext
);
288 catch(const uno::Exception
&)
291 return uno::Any( mxVBProject
);
295 VbaDocumentBase::getServiceImplName()
297 return "VbaDocumentBase";
300 uno::Sequence
< OUString
>
301 VbaDocumentBase::getServiceNames()
303 static uno::Sequence
< OUString
> const aServiceNames
305 "ooo.vba.VbaDocumentBase"
307 return aServiceNames
;
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */