1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "intercept.hxx"
#include "dbastrings.hrc"
#include <com/sun/star/embed/EmbedStates.hpp>
#include <com/sun/star/document/XEventBroadcaster.hpp>
#include <com/sun/star/util/XModifiable.hpp>
#include <cppuhelper/weak.hxx>
#include <comphelper/types.hxx>
#include <tools/debug.hxx>
#include <tools/diagnose_ex.h>
#include <boost/scoped_ptr.hpp>
namespace dbaccess
{
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::ucb;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::io;
using namespace ::com::sun::star::embed;
using namespace ::com::sun::star::container;
using namespace ::comphelper;
using namespace ::cppu;
#define DISPATCH_SAVEAS 0
#define DISPATCH_SAVE 1
#define DISPATCH_CLOSEDOC 2
#define DISPATCH_CLOSEWIN 3
#define DISPATCH_CLOSEFRAME 4
#define DISPATCH_RELOAD 5
// the OSL_ENSURE in CTOR has to be changed too, when adding new defines
void SAL_CALL OInterceptor::dispose()
throw( RuntimeException )
{
EventObject aEvt( *this );
osl::MutexGuard aGuard(m_aMutex);
if ( m_pDisposeEventListeners && m_pDisposeEventListeners->getLength() )
m_pDisposeEventListeners->disposeAndClear( aEvt );
if ( m_pStatCL )
m_pStatCL->disposeAndClear( aEvt );
m_xSlaveDispatchProvider.clear();
m_xMasterDispatchProvider.clear();
m_pContentHolder = NULL;
}
OInterceptor::OInterceptor( ODocumentDefinition* _pContentHolder )
:m_pContentHolder( _pContentHolder )
,m_aInterceptedURL(7)
,m_pDisposeEventListeners(0)
,m_pStatCL(0)
{
OSL_ENSURE(DISPATCH_RELOAD < m_aInterceptedURL.getLength(),"Illegal size.");
m_aInterceptedURL[DISPATCH_SAVEAS] = ".uno:SaveAs";
m_aInterceptedURL[DISPATCH_SAVE] = ".uno:Save";
m_aInterceptedURL[DISPATCH_CLOSEDOC] = ".uno:CloseDoc";
m_aInterceptedURL[DISPATCH_CLOSEWIN] = ".uno:CloseWin";
m_aInterceptedURL[DISPATCH_CLOSEFRAME] = ".uno:CloseFrame";
m_aInterceptedURL[DISPATCH_RELOAD] = ".uno:Reload";
}
OInterceptor::~OInterceptor()
{
if( m_pDisposeEventListeners )
delete m_pDisposeEventListeners;
if(m_pStatCL)
delete m_pStatCL;
}
struct DispatchHelper
{
URL aURL;
Sequence<PropertyValue > aArguments;
};
//XDispatch
void SAL_CALL OInterceptor::dispatch( const URL& _URL,const Sequence<PropertyValue >& Arguments ) throw (RuntimeException, std::exception)
{
::osl::MutexGuard aGuard( m_aMutex );
if ( !m_pContentHolder )
return;
if ( _URL.Complete == m_aInterceptedURL[ DISPATCH_SAVE ] )
{
m_pContentHolder->save( false );
return;
}
if ( _URL.Complete == m_aInterceptedURL[ DISPATCH_RELOAD ] )
{
ODocumentDefinition::fillReportData(
m_pContentHolder->getContext(),
m_pContentHolder->getComponent(),
m_pContentHolder->getConnection()
);
return;
}
if( _URL.Complete == m_aInterceptedURL[ DISPATCH_SAVEAS ] )
{
if ( m_pContentHolder->isNewReport() )
{
m_pContentHolder->saveAs();
}
else if ( m_xSlaveDispatchProvider.is() )
{
Sequence< PropertyValue > aNewArgs = Arguments;
sal_Int32 nInd = 0;
while( nInd < aNewArgs.getLength() )
{
if ( aNewArgs[nInd].Name == "SaveTo" )
{
aNewArgs[nInd].Value <<= sal_True;
break;
}
nInd++;
}
if ( nInd == aNewArgs.getLength() )
{
aNewArgs.realloc( nInd + 1 );
aNewArgs[nInd].Name = "SaveTo";
aNewArgs[nInd].Value <<= sal_True;
}
Reference< XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(_URL, "_self", 0 );
if ( xDispatch.is() )
xDispatch->dispatch( _URL, aNewArgs );
}
return;
}
if ( _URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEDOC ]
|| _URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEWIN ]
|| _URL.Complete == m_aInterceptedURL[ DISPATCH_CLOSEFRAME ]
)
{
DispatchHelper* pHelper = new DispatchHelper;
pHelper->aArguments = Arguments;
pHelper->aURL = _URL;
Application::PostUserEvent( LINK( this, OInterceptor, OnDispatch ), reinterpret_cast< void* >( pHelper ) );
return;
}
}
IMPL_LINK( OInterceptor, OnDispatch, void*, _pDispatcher )
{
boost::scoped_ptr<DispatchHelper> pHelper( reinterpret_cast< DispatchHelper* >( _pDispatcher ) );
try
{
if ( m_pContentHolder && m_pContentHolder->prepareClose() && m_xSlaveDispatchProvider.is() )
{
Reference< XDispatch > xDispatch = m_xSlaveDispatchProvider->queryDispatch(pHelper->aURL, "_self", 0 );
if ( xDispatch.is() )
{
Reference< ::com::sun::star::document::XEventBroadcaster> xEvtB(m_pContentHolder->getComponent(),UNO_QUERY);
if ( xEvtB.is() )
xEvtB->removeEventListener(this);
Reference< XInterface > xKeepContentHolderAlive( *m_pContentHolder );
xDispatch->dispatch( pHelper->aURL,pHelper->aArguments);
}
}
}
catch ( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
return 0L;
}
void SAL_CALL OInterceptor::addStatusListener(
const Reference<
XStatusListener >& Control,
const URL& _URL )
throw (
RuntimeException, std::exception
)
{
if(!Control.is())
return;
if ( m_pContentHolder && _URL.Complete == m_aInterceptedURL[DISPATCH_SAVEAS] )
{ // SaveAs
if ( !m_pContentHolder->isNewReport() )
{
FeatureStateEvent aStateEvent;
aStateEvent.FeatureURL.Complete = m_aInterceptedURL[DISPATCH_SAVEAS];
aStateEvent.FeatureDescriptor = "SaveCopyTo";
aStateEvent.IsEnabled = sal_True;
aStateEvent.Requery = sal_False;
aStateEvent.State <<= OUString("($3)");
Control->statusChanged(aStateEvent);
}
{
osl::MutexGuard aGuard(m_aMutex);
if(!m_pStatCL)
m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
}
m_pStatCL->addInterface(_URL.Complete,Control);
}
else if ( m_pContentHolder && _URL.Complete == m_aInterceptedURL[DISPATCH_SAVE] )
{ // Save
FeatureStateEvent aStateEvent;
aStateEvent.FeatureURL.Complete = m_aInterceptedURL[DISPATCH_SAVE];
aStateEvent.FeatureDescriptor = "Update";
aStateEvent.IsEnabled = m_pContentHolder != NULL && m_pContentHolder->isModified();
aStateEvent.Requery = sal_False;
Control->statusChanged(aStateEvent);
{
osl::MutexGuard aGuard(m_aMutex);
if(!m_pStatCL)
m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
}
m_pStatCL->addInterface(_URL.Complete,Control);
Reference< ::com::sun::star::document::XEventBroadcaster> xEvtB(m_pContentHolder->getComponent(),UNO_QUERY);
if ( xEvtB.is() )
xEvtB->addEventListener(this);
}
else
{
sal_Int32 i = 2;
if(_URL.Complete == m_aInterceptedURL[i] ||
_URL.Complete == m_aInterceptedURL[++i] ||
_URL.Complete == m_aInterceptedURL[++i] ||
_URL.Complete == m_aInterceptedURL[i = DISPATCH_RELOAD] )
{ // Close and return
FeatureStateEvent aStateEvent;
aStateEvent.FeatureURL.Complete = m_aInterceptedURL[i];
aStateEvent.FeatureDescriptor = "Close and Return";
aStateEvent.IsEnabled = sal_True;
aStateEvent.Requery = sal_False;
Control->statusChanged(aStateEvent);
{
osl::MutexGuard aGuard(m_aMutex);
if(!m_pStatCL)
m_pStatCL = new PropertyChangeListenerContainer(m_aMutex);
}
m_pStatCL->addInterface(_URL.Complete,Control);
return;
}
}
}
void SAL_CALL OInterceptor::removeStatusListener(
const Reference<
XStatusListener >& Control,
const URL& _URL )
throw (
RuntimeException, std::exception
)
{
if(!(Control.is() && m_pStatCL))
return;
else
{
m_pStatCL->removeInterface(_URL.Complete,Control);
return;
}
}
//XInterceptorInfo
Sequence< OUString > SAL_CALL OInterceptor::getInterceptedURLs( ) throw ( RuntimeException, std::exception )
{
// now implemented as update
return m_aInterceptedURL;
}
// XDispatchProvider
Reference< XDispatch > SAL_CALL OInterceptor::queryDispatch( const URL& _URL,const OUString& TargetFrameName,sal_Int32 SearchFlags )
throw (RuntimeException, std::exception)
{
osl::MutexGuard aGuard(m_aMutex);
const OUString* pIter = m_aInterceptedURL.getConstArray();
const OUString* pEnd = pIter + m_aInterceptedURL.getLength();
for(;pIter != pEnd;++pIter)
{
if ( _URL.Complete == *pIter )
return (XDispatch*)this;
}
if(m_xSlaveDispatchProvider.is())
return m_xSlaveDispatchProvider->queryDispatch(_URL,TargetFrameName,SearchFlags);
else
return Reference<XDispatch>();
}
Sequence< Reference< XDispatch > > SAL_CALL OInterceptor::queryDispatches( const Sequence<DispatchDescriptor >& Requests ) throw ( RuntimeException, std::exception )
{
Sequence< Reference< XDispatch > > aRet;
osl::MutexGuard aGuard(m_aMutex);
if(m_xSlaveDispatchProvider.is())
aRet = m_xSlaveDispatchProvider->queryDispatches(Requests);
else
aRet.realloc(Requests.getLength());
for(sal_Int32 i = 0; i < Requests.getLength(); ++i)
{
const OUString* pIter = m_aInterceptedURL.getConstArray();
const OUString* pEnd = pIter + m_aInterceptedURL.getLength();
for(;pIter != pEnd;++pIter)
{
if ( Requests[i].FeatureURL.Complete == *pIter )
{
aRet[i] = (XDispatch*) this;
break;
}
}
}
return aRet;
}
//XDispatchProviderInterceptor
Reference< XDispatchProvider > SAL_CALL OInterceptor::getSlaveDispatchProvider( ) throw ( RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
return m_xSlaveDispatchProvider;
}
void SAL_CALL
OInterceptor::setSlaveDispatchProvider( const Reference< XDispatchProvider >& NewDispatchProvider )
throw ( RuntimeException, std::exception )
{
osl::MutexGuard aGuard(m_aMutex);
m_xSlaveDispatchProvider = NewDispatchProvider;
}
Reference< XDispatchProvider > SAL_CALL OInterceptor::getMasterDispatchProvider( )
throw (
RuntimeException, std::exception
)
{
osl::MutexGuard aGuard(m_aMutex);
return m_xMasterDispatchProvider;
}
void SAL_CALL OInterceptor::setMasterDispatchProvider(
const Reference< XDispatchProvider >& NewSupplier )
throw (
RuntimeException, std::exception
)
{
osl::MutexGuard aGuard(m_aMutex);
m_xMasterDispatchProvider = NewSupplier;
}
void SAL_CALL OInterceptor::notifyEvent( const ::com::sun::star::document::EventObject& Event ) throw (::com::sun::star::uno::RuntimeException, std::exception)
{
osl::ResettableMutexGuard _rGuard(m_aMutex);
if ( m_pStatCL && Event.EventName == "OnModifyChanged" )
{
OInterfaceContainerHelper* pListener = m_pStatCL->getContainer(m_aInterceptedURL[DISPATCH_SAVE]);
if ( pListener )
{
FeatureStateEvent aEvt;
aEvt.FeatureURL.Complete = m_aInterceptedURL[DISPATCH_SAVE];
aEvt.FeatureDescriptor = "Update";
Reference<XModifiable> xModel(Event.Source,UNO_QUERY);
aEvt.IsEnabled = xModel.is() && xModel->isModified();
aEvt.Requery = sal_False;
NOTIFY_LISTERNERS((*pListener),XStatusListener,statusChanged)
}
}
}
void SAL_CALL OInterceptor::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
{
}
} // namespace dbaccess
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|