1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir #include "pushbuttonnavigation.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/form/FormButtonType.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyState.hpp>
33*cdf0e10cSrcweir #include "formstrings.hxx"
34*cdf0e10cSrcweir #include <comphelper/extract.hxx>
35*cdf0e10cSrcweir #include <comphelper/property.hxx>
36*cdf0e10cSrcweir #include <osl/diagnose.h>
37*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //............................................................................
40*cdf0e10cSrcweir namespace pcr
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir //............................................................................
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
45*cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
46*cdf0e10cSrcweir     using namespace ::com::sun::star::form;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir     //------------------------------------------------------------------------
49*cdf0e10cSrcweir     namespace
50*cdf0e10cSrcweir     {
51*cdf0e10cSrcweir         static const sal_Int32 s_nFirstVirtualButtonType = 1 + (sal_Int32)FormButtonType_URL;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir         static const sal_Char* pNavigationURLs[] =
54*cdf0e10cSrcweir         {
55*cdf0e10cSrcweir 	        ".uno:FormController/moveToFirst",
56*cdf0e10cSrcweir 	        ".uno:FormController/moveToPrev",
57*cdf0e10cSrcweir 	        ".uno:FormController/moveToNext",
58*cdf0e10cSrcweir 	        ".uno:FormController/moveToLast",
59*cdf0e10cSrcweir 	        ".uno:FormController/saveRecord",
60*cdf0e10cSrcweir 	        ".uno:FormController/undoRecord",
61*cdf0e10cSrcweir 	        ".uno:FormController/moveToNew",
62*cdf0e10cSrcweir 	        ".uno:FormController/deleteRecord",
63*cdf0e10cSrcweir 	        ".uno:FormController/refreshForm",
64*cdf0e10cSrcweir             NULL
65*cdf0e10cSrcweir         };
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir         static sal_Int32 lcl_getNavigationURLIndex( const ::rtl::OUString& _rNavURL )
68*cdf0e10cSrcweir         {
69*cdf0e10cSrcweir             const sal_Char** pLookup = pNavigationURLs;
70*cdf0e10cSrcweir             while ( *pLookup )
71*cdf0e10cSrcweir             {
72*cdf0e10cSrcweir                 if ( _rNavURL.equalsAscii( *pLookup ) )
73*cdf0e10cSrcweir                     return pLookup - pNavigationURLs;
74*cdf0e10cSrcweir                 ++pLookup;
75*cdf0e10cSrcweir             }
76*cdf0e10cSrcweir             return -1;
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         static const sal_Char* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex )
80*cdf0e10cSrcweir         {
81*cdf0e10cSrcweir             const sal_Char** pLookup = pNavigationURLs;
82*cdf0e10cSrcweir             while ( _nButtonTypeIndex-- && *pLookup++ )
83*cdf0e10cSrcweir                 ;
84*cdf0e10cSrcweir             OSL_ENSURE( *pLookup, "lcl_getNavigationURL: invalid index!" );
85*cdf0e10cSrcweir             return *pLookup;
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     //========================================================================
90*cdf0e10cSrcweir     //= PushButtonNavigation
91*cdf0e10cSrcweir     //========================================================================
92*cdf0e10cSrcweir     //------------------------------------------------------------------------
93*cdf0e10cSrcweir     PushButtonNavigation::PushButtonNavigation( const Reference< XPropertySet >& _rxControlModel )
94*cdf0e10cSrcweir         :m_xControlModel( _rxControlModel )
95*cdf0e10cSrcweir         ,m_bIsPushButton( sal_False )
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         OSL_ENSURE( m_xControlModel.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir         try
100*cdf0e10cSrcweir         {
101*cdf0e10cSrcweir 			m_bIsPushButton = ::comphelper::hasProperty( PROPERTY_BUTTONTYPE, m_xControlModel );
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir         catch( const Exception& )
104*cdf0e10cSrcweir         {
105*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::PushButtonNavigation: caught an exception!" );
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     //------------------------------------------------------------------------
110*cdf0e10cSrcweir     sal_Int32 PushButtonNavigation::implGetCurrentButtonType() const SAL_THROW((Exception))
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         sal_Int32 nButtonType = FormButtonType_PUSH;
113*cdf0e10cSrcweir         if ( !m_xControlModel.is() )
114*cdf0e10cSrcweir             return nButtonType;
115*cdf0e10cSrcweir         OSL_VERIFY( ::cppu::enum2int( nButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         if ( nButtonType == FormButtonType_URL )
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             // there's a chance that this is a "virtual" button type
120*cdf0e10cSrcweir             // (which are realized by special URLs)
121*cdf0e10cSrcweir             ::rtl::OUString sTargetURL;
122*cdf0e10cSrcweir             m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL ) >>= sTargetURL;
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir             sal_Int32 nNavigationURLIndex = lcl_getNavigationURLIndex( sTargetURL );
125*cdf0e10cSrcweir             if ( nNavigationURLIndex >= 0)
126*cdf0e10cSrcweir                 // it actually *is* a virtual button type
127*cdf0e10cSrcweir                 nButtonType = s_nFirstVirtualButtonType + nNavigationURLIndex;
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir         return nButtonType;
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     //------------------------------------------------------------------------
133*cdf0e10cSrcweir     Any PushButtonNavigation::getCurrentButtonType() const SAL_THROW(())
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
136*cdf0e10cSrcweir         Any aReturn;
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         try
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             aReturn <<= implGetCurrentButtonType();
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         catch( const Exception& )
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::getCurrentButtonType: caught an exception!" );
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir         return aReturn;
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     //------------------------------------------------------------------------
150*cdf0e10cSrcweir     void PushButtonNavigation::setCurrentButtonType( const Any& _rValue ) const SAL_THROW(())
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
153*cdf0e10cSrcweir         if ( !m_xControlModel.is() )
154*cdf0e10cSrcweir             return;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir         try
157*cdf0e10cSrcweir         {
158*cdf0e10cSrcweir             sal_Int32 nButtonType = FormButtonType_PUSH;
159*cdf0e10cSrcweir             OSL_VERIFY( ::cppu::enum2int( nButtonType, _rValue ) );
160*cdf0e10cSrcweir             ::rtl::OUString sTargetURL;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir             bool bIsVirtualButtonType = nButtonType >= s_nFirstVirtualButtonType;
163*cdf0e10cSrcweir             if ( bIsVirtualButtonType )
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 const sal_Char* pURL = lcl_getNavigationURL( nButtonType - s_nFirstVirtualButtonType );
166*cdf0e10cSrcweir                 sTargetURL = ::rtl::OUString::createFromAscii( pURL );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir                 nButtonType = FormButtonType_URL;
169*cdf0e10cSrcweir             }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir             m_xControlModel->setPropertyValue( PROPERTY_BUTTONTYPE, makeAny( static_cast< FormButtonType >( nButtonType ) ) );
172*cdf0e10cSrcweir             m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, makeAny( sTargetURL ) );
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir         catch( const Exception& )
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::setCurrentButtonType: caught an exception!" );
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     //------------------------------------------------------------------------
181*cdf0e10cSrcweir     PropertyState PushButtonNavigation::getCurrentButtonTypeState( ) const SAL_THROW(())
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
184*cdf0e10cSrcweir         PropertyState eState = PropertyState_DIRECT_VALUE;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         try
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
189*cdf0e10cSrcweir             if ( xStateAccess.is() )
190*cdf0e10cSrcweir             {
191*cdf0e10cSrcweir                 // let's see what the model says about the ButtonType property
192*cdf0e10cSrcweir                 eState = xStateAccess->getPropertyState( PROPERTY_BUTTONTYPE );
193*cdf0e10cSrcweir                 if ( eState == PropertyState_DIRECT_VALUE )
194*cdf0e10cSrcweir                 {
195*cdf0e10cSrcweir                     sal_Int32 nRealButtonType = FormButtonType_PUSH;
196*cdf0e10cSrcweir                     OSL_VERIFY( ::cppu::enum2int( nRealButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
197*cdf0e10cSrcweir                     // perhaps it's one of the virtual button types?
198*cdf0e10cSrcweir                     if ( FormButtonType_URL == nRealButtonType )
199*cdf0e10cSrcweir                     {
200*cdf0e10cSrcweir                         // yes, it is -> rely on the state of the URL property
201*cdf0e10cSrcweir                         eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
202*cdf0e10cSrcweir                     }
203*cdf0e10cSrcweir                 }
204*cdf0e10cSrcweir             }
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir         catch( const Exception& )
207*cdf0e10cSrcweir         {
208*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::getCurrentButtonTypeState: caught an exception!" );
209*cdf0e10cSrcweir         }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         return eState;
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     //------------------------------------------------------------------------
215*cdf0e10cSrcweir     Any PushButtonNavigation::getCurrentTargetURL() const SAL_THROW(())
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         Any aReturn;
218*cdf0e10cSrcweir         if ( !m_xControlModel.is() )
219*cdf0e10cSrcweir             return aReturn;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         try
222*cdf0e10cSrcweir         {
223*cdf0e10cSrcweir             aReturn = m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL );
224*cdf0e10cSrcweir             if ( m_bIsPushButton )
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir                 sal_Int32 nCurrentButtonType = implGetCurrentButtonType();
227*cdf0e10cSrcweir                 bool bIsVirtualButtonType = nCurrentButtonType >= s_nFirstVirtualButtonType;
228*cdf0e10cSrcweir                 if ( bIsVirtualButtonType )
229*cdf0e10cSrcweir                 {
230*cdf0e10cSrcweir                     // pretend (to the user) that there's no URL set - since
231*cdf0e10cSrcweir                     // virtual button types imply a special (technical) URL which
232*cdf0e10cSrcweir                     // the user should not see
233*cdf0e10cSrcweir                     aReturn <<= ::rtl::OUString();
234*cdf0e10cSrcweir                 }
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir         catch( const Exception& )
238*cdf0e10cSrcweir         {
239*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::getCurrentTargetURL: caught an exception!" );
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir         return aReturn;
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir     //------------------------------------------------------------------------
245*cdf0e10cSrcweir     void PushButtonNavigation::setCurrentTargetURL( const Any& _rValue ) const SAL_THROW(())
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         if ( !m_xControlModel.is() )
248*cdf0e10cSrcweir             return;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir         try
251*cdf0e10cSrcweir         {
252*cdf0e10cSrcweir             m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, _rValue );
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir         catch( const Exception& )
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     //------------------------------------------------------------------------
261*cdf0e10cSrcweir     PropertyState PushButtonNavigation::getCurrentTargetURLState( ) const SAL_THROW(())
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         PropertyState eState = PropertyState_DIRECT_VALUE;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         try
266*cdf0e10cSrcweir         {
267*cdf0e10cSrcweir             Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
268*cdf0e10cSrcweir             if ( xStateAccess.is() )
269*cdf0e10cSrcweir             {
270*cdf0e10cSrcweir                 eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
271*cdf0e10cSrcweir             }
272*cdf0e10cSrcweir         }
273*cdf0e10cSrcweir         catch( const Exception& )
274*cdf0e10cSrcweir         {
275*cdf0e10cSrcweir             OSL_ENSURE( sal_False, "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
276*cdf0e10cSrcweir         }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         return eState;
279*cdf0e10cSrcweir     }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     //------------------------------------------------------------------------
282*cdf0e10cSrcweir     bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         sal_Int32 nButtonType( FormButtonType_PUSH );
285*cdf0e10cSrcweir         try
286*cdf0e10cSrcweir         {
287*cdf0e10cSrcweir             nButtonType = implGetCurrentButtonType();
288*cdf0e10cSrcweir         }
289*cdf0e10cSrcweir         catch( const Exception& )
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir         	DBG_UNHANDLED_EXCEPTION();
292*cdf0e10cSrcweir         }
293*cdf0e10cSrcweir         return nButtonType == FormButtonType_URL;
294*cdf0e10cSrcweir     }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     //------------------------------------------------------------------------
297*cdf0e10cSrcweir     bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
298*cdf0e10cSrcweir     {
299*cdf0e10cSrcweir         ::rtl::OUString sTargetURL;
300*cdf0e10cSrcweir         OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL );
301*cdf0e10cSrcweir         return sTargetURL.getLength() != 0;
302*cdf0e10cSrcweir     }
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir //............................................................................
305*cdf0e10cSrcweir }   // namespace pcr
306*cdf0e10cSrcweir //............................................................................
307