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, uno::Reference< frame::XModel > xModel ) : VbaDocumentBase_BASE( xParent, xContext ), mxModel( xModel )
46 : {
47 0 : }
48 :
49 0 : VbaDocumentBase::VbaDocumentBase( uno::Sequence< uno::Any> const & args,
50 0 : uno::Reference< uno::XComponentContext> const & xContext ) : VbaDocumentBase_BASE( getXSomethingFromArgs< XHelperInterface >( args, 0 ), xContext ), mxModel( getXSomethingFromArgs< frame::XModel >( args, 1 ) )
51 : {
52 0 : }
53 :
54 : OUString
55 0 : VbaDocumentBase::getName() throw (uno::RuntimeException)
56 : {
57 0 : OUString sName = getModel()->getURL();
58 0 : if ( !sName.isEmpty() )
59 : {
60 :
61 0 : INetURLObject aURL( getModel()->getURL() );
62 0 : ::osl::File::getSystemPathFromFileURL( aURL.GetLastName(), sName );
63 : }
64 : else
65 : {
66 0 : uno::Reference< frame::XTitle > xTitle( getModel(), uno::UNO_QUERY_THROW );
67 0 : sName = xTitle->getTitle();
68 0 : sName = sName.trim();
69 : }
70 0 : return sName;
71 : }
72 : OUString
73 0 : VbaDocumentBase::getPath() throw (uno::RuntimeException)
74 : {
75 0 : INetURLObject aURL( getModel()->getURL() );
76 0 : OUString sURL = aURL.GetMainURL( INetURLObject::DECODE_TO_IURI );
77 0 : OUString sPath;
78 0 : if( !sURL.isEmpty() )
79 : {
80 0 : sURL = sURL.copy( 0, sURL.getLength() - aURL.GetLastName().getLength() - 1 );
81 0 : ::osl::File::getSystemPathFromFileURL( sURL, sPath );
82 : }
83 0 : return sPath;
84 : }
85 :
86 : OUString
87 0 : VbaDocumentBase::getFullName() throw (uno::RuntimeException)
88 : {
89 0 : OUString sPath = getName();
90 : //::osl::File::getSystemPathFromFileURL( getModel()->getURL(), sPath );
91 0 : return sPath;
92 : }
93 :
94 : void
95 0 : VbaDocumentBase::Close( const uno::Any &rSaveArg, const uno::Any &rFileArg,
96 : const uno::Any &rRouteArg ) throw (uno::RuntimeException)
97 : {
98 0 : sal_Bool bSaveChanges = sal_False;
99 0 : OUString aFileName;
100 0 : sal_Bool bRouteWorkbook = sal_True;
101 :
102 0 : rSaveArg >>= bSaveChanges;
103 0 : sal_Bool bFileName = ( rFileArg >>= aFileName );
104 0 : rRouteArg >>= bRouteWorkbook;
105 0 : uno::Reference< frame::XStorable > xStorable( getModel(), uno::UNO_QUERY_THROW );
106 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
107 :
108 0 : if( bSaveChanges )
109 : {
110 0 : if( xStorable->isReadonly() )
111 : {
112 0 : throw uno::RuntimeException("Unable to save to a read only file ", uno::Reference< XInterface >() );
113 : }
114 0 : if( bFileName )
115 0 : xStorable->storeAsURL( aFileName, uno::Sequence< beans::PropertyValue >(0) );
116 : else
117 0 : xStorable->store();
118 : }
119 : else
120 0 : xModifiable->setModified( false );
121 :
122 : // first try to close the document using UI dispatch functionality
123 0 : sal_Bool bUIClose = sal_False;
124 : try
125 : {
126 0 : uno::Reference< frame::XController > xController( getModel()->getCurrentController(), uno::UNO_SET_THROW );
127 0 : uno::Reference< frame::XDispatchProvider > xDispatchProvider( xController->getFrame(), uno::UNO_QUERY_THROW );
128 :
129 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
130 0 : uno::Reference< util::XURLTransformer > xURLTransformer( util::URLTransformer::create(mxContext) );
131 :
132 0 : util::URL aURL;
133 0 : aURL.Complete = ".uno:CloseDoc";
134 0 : xURLTransformer->parseStrict( aURL );
135 :
136 : uno::Reference< css::frame::XDispatch > xDispatch(
137 0 : xDispatchProvider->queryDispatch( aURL, "_self" , 0 ),
138 0 : uno::UNO_SET_THROW );
139 0 : xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
140 0 : bUIClose = sal_True;
141 : }
142 0 : catch(const uno::Exception&)
143 : {
144 : }
145 :
146 0 : if ( !bUIClose )
147 : {
148 : // if it is not possible to use UI dispatch, try to close the model directly
149 0 : bool bCloseable = false;
150 0 : uno::Reference< frame::XModel > xModel = getModel();
151 : try
152 : {
153 0 : uno::Reference< util::XCloseable > xCloseable( xModel, uno::UNO_QUERY );
154 :
155 : // use close(boolean DeliverOwnership)
156 : // The boolean parameter DeliverOwnership tells objects vetoing the close
157 : // process that they may assume ownership if they object the closure by
158 : // throwing a CloseVetoException. Here we give up ownership. To be on the
159 : // safe side, catch possible veto exception anyway.
160 0 : if ( xCloseable.is() )
161 : {
162 0 : bCloseable = true;
163 0 : xCloseable->close(sal_True);
164 0 : }
165 : }
166 0 : catch (const uno::Exception &)
167 : {
168 : // vetoed
169 : }
170 0 : if (!bCloseable)
171 : {
172 : try {
173 : // If close is not supported by this model - try to dispose it.
174 : // But if the model disagree with a reset request for the modify state
175 : // we shouldn't do so. Otherwhise some strange things can happen.
176 0 : uno::Reference< lang::XComponent > xDisposable ( xModel, uno::UNO_QUERY_THROW );
177 0 : xDisposable->dispose();
178 : }
179 0 : catch(const uno::Exception&)
180 : {
181 : }
182 0 : }
183 0 : }
184 0 : }
185 :
186 : void
187 0 : VbaDocumentBase::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
188 : {
189 0 : OUString rPassword;
190 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
191 : OSL_TRACE("Workbook::Protect stub");
192 0 : if( aPassword >>= rPassword )
193 0 : xProt->protect( rPassword );
194 : else
195 0 : xProt->protect( OUString() );
196 0 : }
197 :
198 : void
199 0 : VbaDocumentBase::Unprotect( const uno::Any &aPassword ) throw (uno::RuntimeException)
200 : {
201 0 : OUString rPassword;
202 0 : uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
203 0 : if( !xProt->isProtected() )
204 0 : throw uno::RuntimeException("File is already unprotected", uno::Reference< XInterface >() );
205 : else
206 : {
207 0 : if( aPassword >>= rPassword )
208 0 : xProt->unprotect( rPassword );
209 : else
210 0 : xProt->unprotect( OUString() );
211 0 : }
212 0 : }
213 :
214 : void
215 0 : VbaDocumentBase::setSaved( sal_Bool bSave ) throw (uno::RuntimeException)
216 : {
217 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
218 : try
219 : {
220 0 : xModifiable->setModified( !bSave );
221 : }
222 0 : catch (const lang::DisposedException&)
223 : {
224 : // impossibility to set the modified state on disposed document should not trigger an error
225 : }
226 0 : catch (const beans::PropertyVetoException&)
227 : {
228 0 : uno::Any aCaught( ::cppu::getCaughtException() );
229 : throw lang::WrappedTargetRuntimeException(
230 : "Can't change modified state of model!",
231 : uno::Reference< uno::XInterface >(),
232 0 : aCaught );
233 0 : }
234 0 : }
235 :
236 : sal_Bool
237 0 : VbaDocumentBase::getSaved() throw (uno::RuntimeException)
238 : {
239 0 : uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
240 0 : return !xModifiable->isModified();
241 : }
242 :
243 : void
244 0 : VbaDocumentBase::Save() throw (uno::RuntimeException)
245 : {
246 0 : OUString url(".uno:Save");
247 0 : uno::Reference< frame::XModel > xModel = getModel();
248 0 : dispatchRequests(xModel,url);
249 0 : }
250 :
251 : void
252 0 : VbaDocumentBase::Activate() throw (uno::RuntimeException)
253 : {
254 0 : uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
255 0 : xFrame->activate();
256 0 : }
257 :
258 : uno::Any SAL_CALL
259 0 : VbaDocumentBase::getVBProject() throw (uno::RuntimeException)
260 : {
261 0 : if( !mxVBProject.is() ) try
262 : {
263 0 : uno::Reference< XApplicationBase > xApp( Application(), uno::UNO_QUERY_THROW );
264 0 : uno::Reference< XInterface > xVBE( xApp->getVBE(), uno::UNO_QUERY_THROW );
265 0 : uno::Sequence< uno::Any > aArgs( 2 );
266 0 : aArgs[ 0 ] <<= xVBE; // the VBE
267 0 : aArgs[ 1 ] <<= getModel(); // document model for script container access
268 0 : uno::Reference< lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_SET_THROW );
269 0 : mxVBProject = xServiceManager->createInstanceWithArgumentsAndContext(
270 0 : "ooo.vba.vbide.VBProject", aArgs, mxContext );
271 : }
272 0 : catch(const uno::Exception&)
273 : {
274 : }
275 0 : return uno::Any( mxVBProject );
276 : }
277 :
278 : OUString
279 0 : VbaDocumentBase::getServiceImplName()
280 : {
281 0 : return OUString( "VbaDocumentBase" );
282 : }
283 :
284 : uno::Sequence< OUString >
285 0 : VbaDocumentBase::getServiceNames()
286 : {
287 0 : static uno::Sequence< OUString > aServiceNames;
288 0 : if ( aServiceNames.getLength() == 0 )
289 : {
290 0 : aServiceNames.realloc( 1 );
291 0 : aServiceNames[ 0 ] = "ooo.vba.VbaDocumentBase";
292 : }
293 0 : return aServiceNames;
294 : }
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|