Hi Carsten and all,
A colleague of mine who develops extensions, met a problem he cannot
explain. And I'd like to ask for help on his behalf.
As in the source file in attachment, he was trying to implement a
::com::sun::star::task::Job component and make it respond to OnFocus
event, when the current document is activated. In a case where user
switches from an OOo window A to another OOo window B, OnFocus should
call com.sun.star.task.XJob:execute(). However, he found that the XFrame
returned by xDesktop->getCurrentFrame() (Line 186) seems to be the frame
of A's rather than B's as he expected. He would like to know if this is
a right behavior of OOo and the solution to achieve his goal.
Could you please help to check his code and share your suggestions?
Thanks and Best Regards,
Felix.
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: LoadDocFinish.cxx,v $
*
* $Revision: 1.6.106.2 $
*
* last change: $Author: obo $ $Date: 2008/02/01 10:26:26 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "LoadDocFinish.hxx"
#include <comphelper/sequenceashashmap.hxx>
#ifndef _CPPUHELPER_FACTORY_HXX_
#include <cppuhelper/factory.hxx>
#endif
#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
#include <comphelper/processfactory.hxx>
#endif
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <configurationaccess.hxx>
#include <com/sun/star/frame/XDesktop.hpp>
using namespace rtl;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::task;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::frame;
const char* LoadDocFinish::interfaces[] =
{
"com.sun.star.task.XJob",
NULL,
};
const char* LoadDocFinish::implementationName =
"com.sun.star.comp.LoadDocFinish";
const char* LoadDocFinish::serviceName = "com.sun.star.task.Job";
namespace
{
struct Action
{
OUString Name;
sal_Bool Enable;
};
Action Actions[]={
{OUString( RTL_CONSTASCII_USTRINGPARAM( "Cut")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM( "Print")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM( "Copy")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM( "Paste")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM(
"SelectAll")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM(
"PrintPagePreView")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM(
"PreviewPrintOptions")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM(
"PrintDefault")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM(
"ExportDirectToPDF")),sal_False},
{OUString( RTL_CONSTASCII_USTRINGPARAM( "EditDoc")),sal_False}
};
}
OUString LoadDocFinish::static_GetImplementationName()
{
return OUString( RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.comp.LoadDocFinish"));
}
Sequence< OUString > LoadDocFinish::static_GetSupportedServiceNames()
{
Sequence < OUString > aNames(1);
aNames[0] = OUString(RTL_CONSTASCII_USTRINGPARAM (
"com.sun.star.task.Job" ) );
return aNames;
}
// XServiceInfo
::rtl::OUString SAL_CALL LoadDocFinish::getImplementationName()
throw ( RuntimeException )
{
return static_GetImplementationName();
}
sal_Bool SAL_CALL LoadDocFinish::static_supportsService(rtl::OUString const &
rServiceName)
{
return rServiceName == getSupportedServiceNames()[0];
}
sal_Bool SAL_CALL LoadDocFinish::supportsService( const ::rtl::OUString&
rServiceName )
throw ( RuntimeException )
{
return static_supportsService ( rServiceName );
}
Sequence< ::rtl::OUString > SAL_CALL LoadDocFinish::getSupportedServiceNames()
throw ( RuntimeException )
{
return static_GetSupportedServiceNames();
}
Reference< XInterface > SAL_CALL LoadDocFinish::CreateInstance(
const Reference< XMultiServiceFactory >& rSMgr )
{
static osl::Mutex aMutex;
osl::MutexGuard guard( aMutex );
return (XComponent*) ( new LoadDocFinish( rSMgr ) );
}
LoadDocFinish::LoadDocFinish( const Reference< XMultiServiceFactory >& xFactory
) :
m_aListeners( m_aMutex ),
m_xServiceManager( xFactory )
{
}
LoadDocFinish::~LoadDocFinish()
{
}
// XComponent
void SAL_CALL LoadDocFinish::dispose() throw ( RuntimeException )
{
EventObject aObject;
aObject.Source = (XComponent*)this;
m_aListeners.disposeAndClear( aObject );
}
void SAL_CALL LoadDocFinish::addEventListener( const Reference< XEventListener
> & aListener) throw ( RuntimeException )
{
m_aListeners.addInterface( aListener );
}
void SAL_CALL LoadDocFinish::removeEventListener( const Reference<
XEventListener > & aListener ) throw ( RuntimeException )
{
m_aListeners.removeInterface( aListener );
}
// XJob
void SAL_CALL LoadDocFinish::executeAsync( const Sequence<
::com::sun::star::beans::NamedValue >& Arguments, const Reference< XJobListener
>& Listener )
throw (IllegalArgumentException, RuntimeException)
{
//OUString str = Actions[0].Name;
Reference< XMultiServiceFactory > xConfigProvider = Reference<
XMultiServiceFactory > ( m_xServiceManager->createInstance( OUString(
RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider"
) ) ), UNO_QUERY );
PropertyValue propertyValue;
propertyValue.Name = OUString::createFromAscii( "nodepath" );
propertyValue.Value <<= OUString::createFromAscii(
"/org.openoffice.Office.Commands/Execute/Disabled" );
Sequence < Any > lParams( 1 );
lParams[0] <<= propertyValue;
Reference< XNameAccess > xAccess = Reference< XNameAccess > (
xConfigProvider->createInstanceWithArguments( OUString(
RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.configuration.ConfigurationUpdateAccess" ) ), lParams ),
UNO_QUERY );
Reference < XNameContainer > xNameContainer = Reference <
XNameContainer > ( xAccess, UNO_QUERY );
sal_Int32 nSize = sizeof(Actions)/sizeof(Actions[0]);
// Enable all commands
for ( int i = 0; i < nSize; i++ )
{
try {
xNameContainer->removeByName( Actions[ i ].Name);
}
// Ignore if there are no item to delete
catch ( ... ) {}
}
//////////////////////////////////////////////////////////////////////////
Reference<XDesktop> xDesktop(::comphelper::getProcessServiceFactory()
->createInstance(OUString::createFromAscii("com.sun.star.frame.Desktop")),UNO_QUERY);
Reference<XFrame> xFrame(xDesktop->getCurrentFrame());
if (!xFrame.is())
{
Listener->jobFinished(this,Any());
return;
}
Reference<XController> xController(xFrame->getController(),UNO_QUERY);
Reference<XModel> xModel(xController->getModel(),UNO_QUERY);
Sequence<PropertyValue> aArgsList(xModel->getArgs());
const PropertyValue* pProgArray = aArgsList.getConstArray();
long nPropSize = aArgsList.getLength();
OUString aPassword;
for (sal_Int32 i=0;i<nPropSize;i++)
{
const PropertyValue& rProp = pProgArray[i];
OUString aProgName = rProp.Name;
if (aProgName.equalsAscii("Password"))
{
rProp.Value >>= aPassword;
break;
}
}
//////////////////////////////////////////////////////////////////////////
//OUString nPkCount = configurationaccess::getPKCount();
if (aPassword.getLength())
{
// Disable required commands
for ( int i = 0; i < nSize; i++ )
{
if ( !Actions[ i ].Enable )
{
Reference < XSingleServiceFactory >
xSetElementFactory = Reference < XSingleServiceFactory > ( xAccess, UNO_QUERY
);
Sequence < Any > aArgs( 0 );
Reference< XInterface > xNewElement =
xSetElementFactory->createInstanceWithArguments( aArgs );
Any aNewElement;
aNewElement <<= xNewElement;
Reference< XPropertySet > xPropertySet =
Reference< XPropertySet > ( xNewElement, UNO_QUERY );
Any aCommandURL;
aCommandURL <<= Actions[ i ].Name;
xPropertySet->setPropertyValue(
OUString::createFromAscii( "Command" ), aCommandURL );
xNameContainer->insertByName( Actions[ i
].Name, aNewElement );
}
}
}
// Save changes
Reference < XChangesBatch > xFlush = Reference < XChangesBatch > (
xAccess, UNO_QUERY );
xFlush->commitChanges();
Listener->jobFinished(this,Any());
//return Any();
//return makeAny( (sal_Bool)fsw.Execute() );
}
// XJobExecutor
void SAL_CALL LoadDocFinish::trigger(const OUString&)
throw ( RuntimeException )
{
// trigger wizard with override, so it gets started regardless of
// configuration
/*Sequence<NamedValue> seq(1);
seq[0] = NamedValue(
OUString::createFromAscii("Override"),
makeAny(sal_True));
execute(seq);*/
}
Reference < XSingleServiceFactory > LoadDocFinish::createServiceFactory(
Reference < XMultiServiceFactory > const & rServiceFactory )
{
return cppu::createSingleFactory (rServiceFactory,
static_GetImplementationName(),
CreateInstance,
static_GetSupportedServiceNames()
);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]