1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 
27 #include "helpinterceptor.hxx"
28 #include "helpdispatch.hxx"
29 #include "newhelp.hxx"
30 #include <sfx2/sfxuno.hxx>
31 #include <tools/urlobj.hxx>
32 #include <tools/debug.hxx>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/frame/XNotifyingDispatch.hpp>
35 #include <cppuhelper/interfacecontainer.h>
36 #include <vcl/window.hxx>
37 #include <limits.h>
38 
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::frame;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::util;
43 using namespace ::com::sun::star::lang;
44 
45 extern void AppendConfigToken_Impl( String& rURL, sal_Bool bQuestionMark ); // sfxhelp.cxx
46 
47 // class HelpInterceptor_Impl --------------------------------------------
48 
HelpInterceptor_Impl()49 HelpInterceptor_Impl::HelpInterceptor_Impl() :
50 
51 	m_pHistory	( NULL ),
52 	m_nCurPos	( 0 )
53 
54 {
55 }
56 
57 // -----------------------------------------------------------------------
58 
~HelpInterceptor_Impl()59 HelpInterceptor_Impl::~HelpInterceptor_Impl()
60 {
61 	for ( sal_uInt16 i = 0; m_pHistory && i < m_pHistory->Count(); ++i )
62 		delete m_pHistory->GetObject(i);
63 	delete m_pHistory;
64 }
65 
66 // -----------------------------------------------------------------------
67 
addURL(const String & rURL)68 void HelpInterceptor_Impl::addURL( const String& rURL )
69 {
70   if ( !m_pHistory )
71 		m_pHistory = new HelpHistoryList_Impl;
72 	sal_uIntPtr nCount = m_pHistory->Count();
73 	if ( nCount && m_nCurPos < ( nCount - 1 ) )
74 	{
75 		for ( sal_uIntPtr i = nCount - 1; i > m_nCurPos; i-- )
76 			delete m_pHistory->Remove(i);
77 	}
78     Reference<XFrame> xFrame(m_xIntercepted, UNO_QUERY);
79     Reference<XController> xController;
80     if(xFrame.is())
81         xController = xFrame->getController();
82     Any aViewData;
83     if(xController.is() && m_pHistory->Count())
84 	{
85 		m_pHistory->GetObject(m_nCurPos)->aViewData = xController->getViewData();
86 	}
87 
88 	m_aCurrentURL = rURL;
89 	Any aEmptyViewData;
90     m_pHistory->Insert( new HelpHistoryEntry_Impl( rURL, aEmptyViewData ), LIST_APPEND );
91 	m_nCurPos = m_pHistory->Count() - 1;
92 // TODO ?
93 	if ( m_xListener.is() )
94 	{
95 		::com::sun::star::frame::FeatureStateEvent aEvent;
96 		URL aURL;
97 	    aURL.Complete = rURL;
98 		aEvent.FeatureURL = aURL;
99         aEvent.Source = (::com::sun::star::frame::XDispatch*)this;
100 		m_xListener->statusChanged( aEvent );
101 	}
102 
103 	m_pWindow->UpdateToolbox();
104 }
105 
106 // -----------------------------------------------------------------------
107 
setInterception(Reference<XFrame> xFrame)108 void HelpInterceptor_Impl::setInterception( Reference< XFrame > xFrame )
109 {
110 	m_xIntercepted = Reference< XDispatchProviderInterception>( xFrame, UNO_QUERY );
111 
112 	if ( m_xIntercepted.is() )
113 		m_xIntercepted->registerDispatchProviderInterceptor( (XDispatchProviderInterceptor*)this );
114 }
115 
116 // -----------------------------------------------------------------------
117 
SetStartURL(const String & rURL)118 void HelpInterceptor_Impl::SetStartURL( const String& rURL )
119 {
120 	DBG_ASSERT( !m_pHistory, "invalid history" );
121 	if ( !m_pHistory )
122 	{
123 		m_pHistory = new HelpHistoryList_Impl;
124         Any aEmptyViewData;
125         m_pHistory->Insert( new HelpHistoryEntry_Impl( rURL, aEmptyViewData ), ((sal_uIntPtr)0x0) );
126 		m_nCurPos = m_pHistory->Count() - 1;
127 
128 		m_pWindow->UpdateToolbox();
129 	}
130 	m_aCurrentURL = rURL;
131 }
132 
HasHistoryPred() const133 sal_Bool HelpInterceptor_Impl::HasHistoryPred() const
134 {
135 	return m_pHistory && ( m_nCurPos > 0 );
136 }
137 
HasHistorySucc() const138 sal_Bool HelpInterceptor_Impl::HasHistorySucc() const
139 {
140 	return m_pHistory && ( m_nCurPos < ( m_pHistory->Count() - 1 ) );
141 }
142 
143 
144 // -----------------------------------------------------------------------
145 // XDispatchProvider
146 
queryDispatch(const URL & aURL,const::rtl::OUString & aTargetFrameName,sal_Int32 nSearchFlags)147 Reference< XDispatch > SAL_CALL HelpInterceptor_Impl::queryDispatch(
148 
149 	const URL& aURL, const ::rtl::OUString& aTargetFrameName, sal_Int32 nSearchFlags )
150 
151 	throw( RuntimeException )
152 
153 {
154 	Reference< XDispatch > xResult;
155 	if ( m_xSlaveDispatcher.is() )
156 		xResult = m_xSlaveDispatcher->queryDispatch( aURL, aTargetFrameName, nSearchFlags );
157 
158 	// INetURLObject aObj( aURL.Complete );
159 	// sal_Bool bHelpURL = ( aObj.GetProtocol() == INET_PROT_VND_SUN_STAR_HELP );
160     sal_Bool bHelpURL = aURL.Complete.toAsciiLowerCase().match(rtl::OUString::createFromAscii("vnd.sun.star.help"),0);
161 
162 	if ( bHelpURL )
163 	{
164 		DBG_ASSERT( xResult.is(), "invalid dispatch" );
165 	    HelpDispatch_Impl* pHelpDispatch = new HelpDispatch_Impl( *this, xResult );
166     	xResult = Reference< XDispatch >( static_cast< ::cppu::OWeakObject* >(pHelpDispatch), UNO_QUERY );
167 	}
168 
169 	return xResult;
170 }
171 
172 // -----------------------------------------------------------------------
173 
queryDispatches(const Sequence<DispatchDescriptor> & aDescripts)174 Sequence < Reference < XDispatch > > SAL_CALL HelpInterceptor_Impl::queryDispatches(
175 
176 	const Sequence< DispatchDescriptor >& aDescripts )
177 
178 	throw( RuntimeException )
179 
180 {
181 	Sequence< Reference< XDispatch > > aReturn( aDescripts.getLength() );
182 	Reference< XDispatch >* pReturn = aReturn.getArray();
183 	const DispatchDescriptor* pDescripts = aDescripts.getConstArray();
184 	for ( sal_Int16 i = 0; i < aDescripts.getLength(); ++i, ++pReturn, ++pDescripts )
185 	{
186 		*pReturn = queryDispatch( pDescripts->FeatureURL, pDescripts->FrameName, pDescripts->SearchFlags );
187 	}
188 	return aReturn;
189 }
190 
191 // -----------------------------------------------------------------------
192 // XDispatchProviderInterceptor
193 
getSlaveDispatchProvider()194 Reference< XDispatchProvider > SAL_CALL HelpInterceptor_Impl::getSlaveDispatchProvider()
195 
196 	throw( RuntimeException )
197 
198 {
199 	return m_xSlaveDispatcher;
200 }
201 
202 // -----------------------------------------------------------------------
203 
setSlaveDispatchProvider(const Reference<XDispatchProvider> & xNewSlave)204 void SAL_CALL HelpInterceptor_Impl::setSlaveDispatchProvider( const Reference< XDispatchProvider >& xNewSlave )
205 
206 	throw( RuntimeException )
207 
208 {
209 	m_xSlaveDispatcher = xNewSlave;
210 }
211 
212 // -----------------------------------------------------------------------
213 
getMasterDispatchProvider()214 Reference< XDispatchProvider > SAL_CALL HelpInterceptor_Impl::getMasterDispatchProvider()
215 
216 	throw( RuntimeException )
217 
218 {
219 	return m_xMasterDispatcher;
220 }
221 
222 // -----------------------------------------------------------------------
223 
setMasterDispatchProvider(const Reference<XDispatchProvider> & xNewMaster)224 void SAL_CALL HelpInterceptor_Impl::setMasterDispatchProvider( const Reference< XDispatchProvider >& xNewMaster )
225 
226 	throw( RuntimeException )
227 
228 {
229 	m_xMasterDispatcher = xNewMaster;
230 }
231 
232 // -----------------------------------------------------------------------
233 // XInterceptorInfo
234 
getInterceptedURLs()235 Sequence< ::rtl::OUString > SAL_CALL HelpInterceptor_Impl::getInterceptedURLs()
236 
237 	throw( RuntimeException )
238 
239 {
240 	Sequence< ::rtl::OUString > aURLList( 1 );
241 	aURLList[0] = DEFINE_CONST_UNICODE("vnd.sun.star.help://*");
242 	return aURLList;;
243 }
244 
245 // -----------------------------------------------------------------------
246 // XDispatch
247 
dispatch(const URL & aURL,const Sequence<::com::sun::star::beans::PropertyValue> &)248 void SAL_CALL HelpInterceptor_Impl::dispatch(
249 	const URL& aURL, const Sequence< ::com::sun::star::beans::PropertyValue >& ) throw( RuntimeException )
250 {
251 	sal_Bool bBack = ( String( DEFINE_CONST_UNICODE(".uno:Backward") ) == String( aURL.Complete ) );
252 	if ( bBack || String( DEFINE_CONST_UNICODE(".uno:Forward") ) == String( aURL.Complete ) )
253 	{
254 		if ( m_pHistory )
255 		{
256 			if(m_pHistory->Count() > m_nCurPos)
257 			{
258 				Reference<XFrame> xFrame(m_xIntercepted, UNO_QUERY);
259                 Reference<XController> xController;
260                 if(xFrame.is())
261                     xController = xFrame->getController();
262 				if(xController.is())
263 				{
264 					m_pHistory->GetObject(m_nCurPos)->aViewData = xController->getViewData();
265 				}
266 			}
267 
268 			sal_uIntPtr nPos = ( bBack && m_nCurPos > 0 ) ? --m_nCurPos
269 													: ( !bBack && m_nCurPos < m_pHistory->Count() - 1 )
270 													? ++m_nCurPos
271 													: ULONG_MAX;
272 
273 			if ( nPos < ULONG_MAX )
274 			{
275 				HelpHistoryEntry_Impl* pEntry = m_pHistory->GetObject( nPos );
276 				if ( pEntry )
277                     m_pWindow->loadHelpContent(pEntry->aURL, sal_False); // false => dont add item to history again!
278 			}
279 
280 			m_pWindow->UpdateToolbox();
281 		}
282 	}
283 }
284 
285 // -----------------------------------------------------------------------
286 
addStatusListener(const Reference<XStatusListener> & xControl,const URL &)287 void SAL_CALL HelpInterceptor_Impl::addStatusListener(
288 	const Reference< XStatusListener >& xControl, const URL& ) throw( RuntimeException )
289 {
290 	DBG_ASSERT( !m_xListener.is(), "listener already exists" );
291 	m_xListener = xControl;
292 }
293 
294 // -----------------------------------------------------------------------
295 
removeStatusListener(const Reference<XStatusListener> &,const URL &)296 void SAL_CALL HelpInterceptor_Impl::removeStatusListener(
297 	const Reference< XStatusListener >&, const URL&) throw( RuntimeException )
298 {
299 	m_xListener = 0;
300 }
301 
302 // HelpListener_Impl -----------------------------------------------------
303 
HelpListener_Impl(HelpInterceptor_Impl * pInter)304 HelpListener_Impl::HelpListener_Impl( HelpInterceptor_Impl* pInter )
305 {
306 	pInterceptor = pInter;
307     pInterceptor->addStatusListener( this, ::com::sun::star::util::URL() );
308 }
309 
310 // -----------------------------------------------------------------------
311 
statusChanged(const::com::sun::star::frame::FeatureStateEvent & Event)312 void SAL_CALL HelpListener_Impl::statusChanged( const ::com::sun::star::frame::FeatureStateEvent& Event )
313 
314 	throw( ::com::sun::star::uno::RuntimeException )
315 
316 {
317 	INetURLObject aObj( Event.FeatureURL.Complete );
318 	aFactory = aObj.GetHost();
319 	aChangeLink.Call( this );
320 }
321 
322 // -----------------------------------------------------------------------
323 
disposing(const::com::sun::star::lang::EventObject &)324 void SAL_CALL HelpListener_Impl::disposing( const ::com::sun::star::lang::EventObject& )
325 
326 	throw( ::com::sun::star::uno::RuntimeException )
327 
328 {
329     pInterceptor->removeStatusListener( this, ::com::sun::star::util::URL() );
330     pInterceptor = NULL;
331 }
332 /*-- 05.09.2002 12:17:59---------------------------------------------------
333 
334   -----------------------------------------------------------------------*/
HelpStatusListener_Impl(Reference<XDispatch> aDispatch,URL & rURL)335 HelpStatusListener_Impl::HelpStatusListener_Impl(
336         Reference < XDispatch > aDispatch, URL& rURL)
337 {
338     aDispatch->addStatusListener(this, rURL);
339 }
340 /*-- 05.09.2002 12:17:59---------------------------------------------------
341 
342   -----------------------------------------------------------------------*/
~HelpStatusListener_Impl()343 HelpStatusListener_Impl::~HelpStatusListener_Impl()
344 {
345     if(xDispatch.is())
346         xDispatch->removeStatusListener(this, com::sun::star::util::URL());
347 }
348 /*-- 05.09.2002 12:17:59---------------------------------------------------
349 
350   -----------------------------------------------------------------------*/
statusChanged(const FeatureStateEvent & rEvent)351 void HelpStatusListener_Impl::statusChanged(
352     const FeatureStateEvent& rEvent ) throw( RuntimeException )
353 {
354     aStateEvent = rEvent;
355 }
356 /*-- 05.09.2002 12:18:00---------------------------------------------------
357 
358   -----------------------------------------------------------------------*/
disposing(const EventObject &)359 void HelpStatusListener_Impl::disposing( const EventObject& ) throw( RuntimeException )
360 {
361     xDispatch->removeStatusListener(this, com::sun::star::util::URL());
362     xDispatch = 0;
363 }
364