Update ooo320-m1
[ooovba.git] / desktop / source / deployment / registry / dp_backend.cxx
blobf37a9d5bfe839f037983b35cb847e1a53bf66b6b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dp_backend.cxx,v $
10 * $Revision: 1.20 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "dp_backend.h"
35 #include "dp_ucb.h"
36 #include "rtl/uri.hxx"
37 #include "cppuhelper/exc_hlp.hxx"
38 #include "comphelper/servicedecl.hxx"
39 #include "comphelper/unwrapargs.hxx"
40 #include "ucbhelper/content.hxx"
41 #include "com/sun/star/lang/WrappedTargetRuntimeException.hpp"
42 #include "com/sun/star/beans/StringPair.hpp"
45 using namespace ::dp_misc;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::ucb;
49 using ::rtl::OUString;
51 namespace dp_registry {
52 namespace backend {
54 //______________________________________________________________________________
55 PackageRegistryBackend::~PackageRegistryBackend()
59 //______________________________________________________________________________
60 void PackageRegistryBackend::disposing( lang::EventObject const & event )
61 throw (RuntimeException)
63 Reference<deployment::XPackage> xPackage(
64 event.Source, UNO_QUERY_THROW );
65 OUString url( xPackage->getURL() );
66 ::osl::MutexGuard guard( getMutex() );
67 if ( m_bound.erase( url ) != 1 )
69 OSL_ASSERT( false );
73 //______________________________________________________________________________
74 PackageRegistryBackend::PackageRegistryBackend(
75 Sequence<Any> const & args,
76 Reference<XComponentContext> const & xContext )
77 : t_BackendBase( getMutex() ),
78 m_xComponentContext( xContext ),
79 m_eContext( CONTEXT_UNKNOWN ),
80 m_readOnly( false )
82 boost::optional<OUString> cachePath;
83 boost::optional<bool> readOnly;
84 comphelper::unwrapArgs( args, m_context, cachePath, readOnly );
85 if (cachePath)
86 m_cachePath = *cachePath;
87 if (readOnly)
88 m_readOnly = *readOnly;
90 if (m_context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("user") ))
91 m_eContext = CONTEXT_USER;
92 else if (m_context.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("shared") ))
93 m_eContext = CONTEXT_SHARED;
94 else if (m_context.matchIgnoreAsciiCaseAsciiL(
95 RTL_CONSTASCII_STRINGPARAM("vnd.sun.star.tdoc:/") ))
96 m_eContext = CONTEXT_DOCUMENT;
97 else
98 m_eContext = CONTEXT_UNKNOWN;
101 //______________________________________________________________________________
102 void PackageRegistryBackend::check()
104 ::osl::MutexGuard guard( getMutex() );
105 if (rBHelper.bInDispose || rBHelper.bDisposed) {
106 throw lang::DisposedException(
107 OUSTR("PackageRegistryBackend instance has already been disposed!"),
108 static_cast<OWeakObject *>(this) );
112 //______________________________________________________________________________
113 void PackageRegistryBackend::disposing()
115 try {
116 m_xComponentContext.clear();
117 WeakComponentImplHelperBase::disposing();
119 catch (RuntimeException &) {
120 throw;
122 catch (Exception &) {
123 Any exc( ::cppu::getCaughtException() );
124 throw lang::WrappedTargetRuntimeException(
125 OUSTR("caught unexpected exception while disposing!"),
126 static_cast<OWeakObject *>(this), exc );
130 // XPackageRegistry
131 //______________________________________________________________________________
132 Reference<deployment::XPackage> PackageRegistryBackend::bindPackage(
133 OUString const & url, OUString const & mediaType,
134 Reference<XCommandEnvironment> const & xCmdEnv )
135 throw (deployment::DeploymentException, CommandFailedException,
136 lang::IllegalArgumentException, RuntimeException)
138 ::osl::ResettableMutexGuard guard( getMutex() );
139 check();
140 t_string2weakref::const_iterator const iFind( m_bound.find( url ) );
141 if (iFind != m_bound.end()) {
142 Reference<deployment::XPackage> xPackage( iFind->second );
143 if (xPackage.is())
144 return xPackage;
146 guard.clear();
148 Reference<deployment::XPackage> xNewPackage;
149 try {
150 xNewPackage = bindPackage_( url, mediaType, xCmdEnv );
152 catch (RuntimeException &) {
153 throw;
155 catch (lang::IllegalArgumentException &) {
156 throw;
158 catch (CommandFailedException &) {
159 throw;
161 catch (deployment::DeploymentException &) {
162 throw;
164 catch (Exception &) {
165 Any exc( ::cppu::getCaughtException() );
166 throw deployment::DeploymentException(
167 OUSTR("Error binding package: ") + url,
168 static_cast<OWeakObject *>(this), exc );
171 guard.reset();
172 ::std::pair< t_string2weakref::iterator, bool > insertion(
173 m_bound.insert( t_string2weakref::value_type( url, xNewPackage ) ) );
174 if (insertion.second)
175 { // first insertion
176 OSL_ASSERT( Reference<XInterface>(insertion.first->second)
177 == xNewPackage );
179 else
180 { // found existing entry
181 Reference<deployment::XPackage> xPackage( insertion.first->second );
182 if (xPackage.is())
183 return xPackage;
184 insertion.first->second = xNewPackage;
186 guard.clear();
187 xNewPackage->addEventListener( this ); // listen for disposing events
188 return xNewPackage;
191 //##############################################################################
193 //______________________________________________________________________________
194 Package::~Package()
198 //______________________________________________________________________________
199 Package::Package( ::rtl::Reference<PackageRegistryBackend> const & myBackend,
200 OUString const & url,
201 OUString const & name,
202 OUString const & displayName,
203 Reference<deployment::XPackageTypeInfo> const & xPackageType )
204 : t_PackageBase( getMutex() ),
205 m_myBackend( myBackend ),
206 m_url( url ),
207 m_name( name ),
208 m_displayName( displayName ),
209 m_xPackageType( xPackageType )
213 //______________________________________________________________________________
214 void Package::disposing()
216 m_myBackend.clear();
217 WeakComponentImplHelperBase::disposing();
220 //______________________________________________________________________________
221 void Package::check() const
223 ::osl::MutexGuard guard( getMutex() );
224 if (rBHelper.bInDispose || rBHelper.bDisposed) {
225 throw lang::DisposedException(
226 OUSTR("Package instance has already been disposed!"),
227 static_cast<OWeakObject *>(const_cast<Package *>(this)));
231 // XComponent
232 //______________________________________________________________________________
233 void Package::dispose() throw (RuntimeException)
235 check();
236 WeakComponentImplHelperBase::dispose();
239 //______________________________________________________________________________
240 void Package::addEventListener(
241 Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
243 check();
244 WeakComponentImplHelperBase::addEventListener( xListener );
247 //______________________________________________________________________________
248 void Package::removeEventListener(
249 Reference<lang::XEventListener> const & xListener ) throw (RuntimeException)
251 check();
252 WeakComponentImplHelperBase::removeEventListener( xListener );
255 // XModifyBroadcaster
256 //______________________________________________________________________________
257 void Package::addModifyListener(
258 Reference<util::XModifyListener> const & xListener )
259 throw (RuntimeException)
261 check();
262 rBHelper.addListener( ::getCppuType( &xListener ), xListener );
265 //______________________________________________________________________________
266 void Package::removeModifyListener(
267 Reference<util::XModifyListener> const & xListener )
268 throw (RuntimeException)
270 check();
271 rBHelper.removeListener( ::getCppuType( &xListener ), xListener );
274 //______________________________________________________________________________
275 void Package::checkAborted(
276 ::rtl::Reference<AbortChannel> const & abortChannel )
278 if (abortChannel.is() && abortChannel->isAborted()) {
279 throw CommandAbortedException(
280 OUSTR("abort!"), static_cast<OWeakObject *>(this) );
284 // XPackage
285 //______________________________________________________________________________
286 Reference<task::XAbortChannel> Package::createAbortChannel()
287 throw (RuntimeException)
289 check();
290 return new AbortChannel;
293 //______________________________________________________________________________
294 sal_Bool Package::isBundle() throw (RuntimeException)
296 return false; // default
299 //______________________________________________________________________________
300 ::sal_Bool Package::checkPrerequisites(
301 const css::uno::Reference< css::task::XAbortChannel >&,
302 const css::uno::Reference< css::ucb::XCommandEnvironment >&,
303 sal_Bool, ::rtl::OUString const &)
304 throw (css::deployment::DeploymentException,
305 css::ucb::CommandFailedException,
306 css::ucb::CommandAbortedException,
307 css::uno::RuntimeException)
309 return true;
312 //______________________________________________________________________________
313 ::sal_Bool Package::checkDependencies(
314 const css::uno::Reference< css::ucb::XCommandEnvironment >& )
315 throw (css::deployment::DeploymentException,
316 css::ucb::CommandFailedException,
317 css::uno::RuntimeException)
319 return true;
323 //______________________________________________________________________________
324 Sequence< Reference<deployment::XPackage> > Package::getBundle(
325 Reference<task::XAbortChannel> const &,
326 Reference<XCommandEnvironment> const & )
327 throw (deployment::DeploymentException,
328 CommandFailedException, CommandAbortedException,
329 lang::IllegalArgumentException, RuntimeException)
331 return Sequence< Reference<deployment::XPackage> >();
334 //______________________________________________________________________________
335 OUString Package::getName() throw (RuntimeException)
337 return m_name;
340 beans::Optional<OUString> Package::getIdentifier() throw (RuntimeException)
342 return beans::Optional<OUString>();
345 //______________________________________________________________________________
346 OUString Package::getVersion() throw (RuntimeException)
348 return OUString();
351 //______________________________________________________________________________
352 OUString Package::getURL() throw (RuntimeException)
354 return m_url;
357 //______________________________________________________________________________
358 OUString Package::getDisplayName() throw (RuntimeException)
360 return m_displayName;
363 //______________________________________________________________________________
364 OUString Package::getDescription() throw (RuntimeException)
366 return OUString();
369 //______________________________________________________________________________
370 Sequence<OUString> Package::getUpdateInformationURLs() throw (RuntimeException)
372 return Sequence<OUString>();
375 //______________________________________________________________________________
376 css::beans::StringPair Package::getPublisherInfo() throw (RuntimeException)
378 css::beans::StringPair aEmptyPair;
379 return aEmptyPair;
382 //______________________________________________________________________________
383 uno::Reference< css::graphic::XGraphic > Package::getIcon( sal_Bool /*bHighContrast*/ ) throw ( RuntimeException )
385 uno::Reference< css::graphic::XGraphic > aEmpty;
386 return aEmpty;
389 //______________________________________________________________________________
390 Reference<deployment::XPackageTypeInfo> Package::getPackageType()
391 throw (RuntimeException)
393 return m_xPackageType;
396 //______________________________________________________________________________
397 void Package::exportTo(
398 OUString const & destFolderURL, OUString const & newTitle,
399 sal_Int32 nameClashAction, Reference<XCommandEnvironment> const & xCmdEnv )
400 throw (CommandFailedException, CommandAbortedException, RuntimeException)
402 ::ucbhelper::Content destFolder( destFolderURL, xCmdEnv );
403 ::ucbhelper::Content sourceContent( getURL(), xCmdEnv );
404 if (! destFolder.transferContent(
405 sourceContent, ::ucbhelper::InsertOperation_COPY,
406 newTitle, nameClashAction ))
407 throw RuntimeException( OUSTR("UCB transferContent() failed!"), 0 );
410 //______________________________________________________________________________
411 void Package::fireModified()
413 ::cppu::OInterfaceContainerHelper * container = rBHelper.getContainer(
414 ::getCppuType( static_cast<Reference<
415 util::XModifyListener> const *>(0) ) );
416 if (container != 0) {
417 Sequence< Reference<XInterface> > elements(
418 container->getElements() );
419 lang::EventObject evt( static_cast<OWeakObject *>(this) );
420 for ( sal_Int32 pos = 0; pos < elements.getLength(); ++pos )
422 Reference<util::XModifyListener> xListener(
423 elements[ pos ], UNO_QUERY );
424 if (xListener.is())
425 xListener->modified( evt );
430 // XPackage
431 //______________________________________________________________________________
432 beans::Optional< beans::Ambiguous<sal_Bool> > Package::isRegistered(
433 Reference<task::XAbortChannel> const & xAbortChannel,
434 Reference<XCommandEnvironment> const & xCmdEnv )
435 throw (deployment::DeploymentException,
436 CommandFailedException, CommandAbortedException, RuntimeException)
438 try {
439 ::osl::ResettableMutexGuard guard( getMutex() );
440 return isRegistered_( guard,
441 AbortChannel::get(xAbortChannel),
442 xCmdEnv );
444 catch (RuntimeException &) {
445 throw;
447 catch (CommandFailedException &) {
448 throw;
450 catch (CommandAbortedException &) {
451 throw;
453 catch (deployment::DeploymentException &) {
454 throw;
456 catch (Exception &) {
457 Any exc( ::cppu::getCaughtException() );
458 throw deployment::DeploymentException(
459 OUSTR("unexpected exception occured!"),
460 static_cast<OWeakObject *>(this), exc );
464 //______________________________________________________________________________
465 void Package::processPackage_impl(
466 bool doRegisterPackage,
467 Reference<task::XAbortChannel> const & xAbortChannel,
468 Reference<XCommandEnvironment> const & xCmdEnv )
470 check();
471 bool action = false;
473 try {
474 try {
475 ::osl::ResettableMutexGuard guard( getMutex() );
476 beans::Optional< beans::Ambiguous<sal_Bool> > option(
477 isRegistered_( guard, AbortChannel::get(xAbortChannel),
478 xCmdEnv ) );
479 action = (option.IsPresent &&
480 (option.Value.IsAmbiguous ||
481 (doRegisterPackage ? !option.Value.Value
482 : option.Value.Value)));
483 if (action) {
484 OUString displayName( getDisplayName() );
485 ProgressLevel progress(
486 xCmdEnv,
487 (doRegisterPackage
488 ? PackageRegistryBackend::StrRegisteringPackage::get()
489 : PackageRegistryBackend::StrRevokingPackage::get())
490 + displayName );
491 processPackage_( guard,
492 doRegisterPackage,
493 AbortChannel::get(xAbortChannel),
494 xCmdEnv );
497 catch (RuntimeException &) {
498 OSL_ENSURE( 0, "### unexpected RuntimeException!" );
499 throw;
501 catch (CommandFailedException &) {
502 throw;
504 catch (CommandAbortedException &) {
505 throw;
507 catch (deployment::DeploymentException &) {
508 throw;
510 catch (Exception &) {
511 Any exc( ::cppu::getCaughtException() );
512 throw deployment::DeploymentException(
513 (doRegisterPackage
514 ? getResourceString(RID_STR_ERROR_WHILE_REGISTERING)
515 : getResourceString(RID_STR_ERROR_WHILE_REVOKING))
516 + getDisplayName(), static_cast<OWeakObject *>(this), exc );
519 catch (...) {
520 if (action)
521 fireModified();
522 throw;
524 if (action)
525 fireModified();
528 //______________________________________________________________________________
529 void Package::registerPackage(
530 Reference<task::XAbortChannel> const & xAbortChannel,
531 Reference<XCommandEnvironment> const & xCmdEnv )
532 throw (deployment::DeploymentException,
533 CommandFailedException, CommandAbortedException,
534 lang::IllegalArgumentException, RuntimeException)
536 processPackage_impl( true /* register */, xAbortChannel, xCmdEnv );
539 //______________________________________________________________________________
540 void Package::revokePackage(
541 Reference<task::XAbortChannel> const & xAbortChannel,
542 Reference<XCommandEnvironment> const & xCmdEnv )
543 throw (deployment::DeploymentException,
544 CommandFailedException, CommandAbortedException,
545 lang::IllegalArgumentException, RuntimeException)
547 processPackage_impl( false /* revoke */, xAbortChannel, xCmdEnv );
550 //##############################################################################
552 //______________________________________________________________________________
553 Package::TypeInfo::~TypeInfo()
557 // XPackageTypeInfo
558 //______________________________________________________________________________
559 OUString Package::TypeInfo::getMediaType() throw (RuntimeException)
561 return m_mediaType;
564 //______________________________________________________________________________
565 OUString Package::TypeInfo::getDescription() throw (RuntimeException)
567 return getShortDescription();
570 //______________________________________________________________________________
571 OUString Package::TypeInfo::getShortDescription() throw (RuntimeException)
573 return m_shortDescr;
576 //______________________________________________________________________________
577 OUString Package::TypeInfo::getFileFilter() throw (RuntimeException)
579 return m_fileFilter;
582 //______________________________________________________________________________
583 Any Package::TypeInfo::getIcon( sal_Bool highContrast, sal_Bool smallIcon )
584 throw (RuntimeException)
586 if (! smallIcon)
587 return Any();
588 const sal_uInt16 nIconId = (highContrast ? m_smallIcon_HC : m_smallIcon);
589 return Any( &nIconId, getCppuType( static_cast<sal_uInt16 const *>(0) ) );