xref: /aoo41x/main/vcl/source/helper/xconnection.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include "vcl/svapp.hxx"
32 
33 #include "xconnection.hxx"
34 #include "svdata.hxx"
35 #include "salinst.hxx"
36 
37 namespace {
38 
39 namespace css = com::sun::star;
40 
41 }
42 
43 namespace vcl
44 {
45     class SolarMutexReleaser
46     {
47         sal_uLong mnReleased;
48     public:
49         SolarMutexReleaser()
50         {
51             mnReleased = Application::ReleaseSolarMutex();
52         }
53 
54         ~SolarMutexReleaser()
55         {
56             if( mnReleased )
57                 Application::AcquireSolarMutex( mnReleased );
58         }
59     };
60 }
61 
62 using namespace rtl;
63 using namespace osl;
64 using namespace vcl;
65 using namespace com::sun::star::uno;
66 using namespace com::sun::star::awt;
67 
68 
69 DisplayConnection::DisplayConnection()
70 {
71 	SalInstance::ConnectionIdentifierType eType;
72 	int nBytes;
73 	void* pBytes = ImplGetSVData()->mpDefInst->GetConnectionIdentifier( eType, nBytes );
74 	switch( eType )
75 	{
76 		case SalInstance::AsciiCString:
77 			m_aAny <<= OUString::createFromAscii( (sal_Char*)pBytes );
78 			break;
79 		case SalInstance::Blob:
80 			m_aAny <<= Sequence< sal_Int8 >( (sal_Int8*)pBytes, nBytes );
81 			break;
82 	}
83 }
84 
85 DisplayConnection::~DisplayConnection()
86 {}
87 
88 void DisplayConnection::start()
89 {
90 	ImplSVData* pSVData = ImplGetSVData();
91 	pSVData->mpDefInst->SetEventCallback( this );
92 }
93 
94 void DisplayConnection::terminate()
95 {
96 	ImplSVData* pSVData = ImplGetSVData();
97 
98     if( pSVData )
99     {
100         pSVData->mpDefInst->SetEventCallback( NULL );
101     }
102 
103     SolarMutexReleaser aRel;
104 
105 	MutexGuard aGuard( m_aMutex );
106     Any aEvent;
107     std::list< css::uno::Reference< XEventHandler > > aLocalList( m_aHandlers );
108 	for( ::std::list< css::uno::Reference< XEventHandler > >::const_iterator it = aLocalList.begin(); it != aLocalList.end(); ++it )
109 		(*it)->handleEvent( aEvent );
110 }
111 
112 void SAL_CALL DisplayConnection::addEventHandler( const Any& /*window*/, const css::uno::Reference< XEventHandler >& handler, sal_Int32 /*eventMask*/ ) throw()
113 {
114 	MutexGuard aGuard( m_aMutex );
115 
116 	m_aHandlers.push_back( handler );
117 }
118 
119 void SAL_CALL DisplayConnection::removeEventHandler( const Any& /*window*/, const css::uno::Reference< XEventHandler >& handler ) throw()
120 {
121 	MutexGuard aGuard( m_aMutex );
122 
123 	m_aHandlers.remove( handler );
124 }
125 
126 void SAL_CALL DisplayConnection::addErrorHandler( const css::uno::Reference< XEventHandler >& handler ) throw()
127 {
128 	MutexGuard aGuard( m_aMutex );
129 
130 	m_aErrorHandlers.push_back( handler );
131 }
132 
133 void SAL_CALL DisplayConnection::removeErrorHandler( const css::uno::Reference< XEventHandler >& handler ) throw()
134 {
135 	MutexGuard aGuard( m_aMutex );
136 
137 	m_aErrorHandlers.remove( handler );
138 }
139 
140 Any SAL_CALL DisplayConnection::getIdentifier() throw()
141 {
142 	return m_aAny;
143 }
144 
145 bool DisplayConnection::dispatchEvent( void* pData, int nBytes )
146 {
147     SolarMutexReleaser aRel;
148 
149 	Sequence< sal_Int8 > aSeq( (sal_Int8*)pData, nBytes );
150 	Any aEvent;
151 	aEvent <<= aSeq;
152     ::std::list< css::uno::Reference< XEventHandler > > handlers;
153     {
154         MutexGuard aGuard( m_aMutex );
155         handlers = m_aHandlers;
156     }
157 	for( ::std::list< css::uno::Reference< XEventHandler > >::const_iterator it = handlers.begin(); it != handlers.end(); ++it )
158 		if( (*it)->handleEvent( aEvent ) )
159 			return true;
160 	return false;
161 }
162 
163 bool DisplayConnection::dispatchErrorEvent( void* pData, int nBytes )
164 {
165     SolarMutexReleaser aRel;
166 
167 	Sequence< sal_Int8 > aSeq( (sal_Int8*)pData, nBytes );
168 	Any aEvent;
169 	aEvent <<= aSeq;
170     ::std::list< css::uno::Reference< XEventHandler > > handlers;
171     {
172         MutexGuard aGuard( m_aMutex );
173         handlers = m_aErrorHandlers;
174     }
175 	for( ::std::list< css::uno::Reference< XEventHandler > >::const_iterator it = handlers.begin(); it != handlers.end(); ++it )
176 		if( (*it)->handleEvent( aEvent ) )
177 			return true;
178 
179 	return false;
180 }
181