Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "pushbuttonnavigation.hxx"
21 : #include <com/sun/star/form/FormButtonType.hpp>
22 : #include <com/sun/star/beans/XPropertyState.hpp>
23 : #include "formstrings.hxx"
24 : #include <comphelper/extract.hxx>
25 : #include <comphelper/property.hxx>
26 : #include <osl/diagnose.h>
27 : #include <tools/diagnose_ex.h>
28 :
29 :
30 : namespace pcr
31 : {
32 :
33 :
34 : using namespace ::com::sun::star::uno;
35 : using namespace ::com::sun::star::beans;
36 : using namespace ::com::sun::star::form;
37 :
38 :
39 : namespace
40 : {
41 : static const sal_Int32 s_nFirstVirtualButtonType = 1 + (sal_Int32)FormButtonType_URL;
42 :
43 : static const sal_Char* pNavigationURLs[] =
44 : {
45 : ".uno:FormController/moveToFirst",
46 : ".uno:FormController/moveToPrev",
47 : ".uno:FormController/moveToNext",
48 : ".uno:FormController/moveToLast",
49 : ".uno:FormController/saveRecord",
50 : ".uno:FormController/undoRecord",
51 : ".uno:FormController/moveToNew",
52 : ".uno:FormController/deleteRecord",
53 : ".uno:FormController/refreshForm",
54 : NULL
55 : };
56 :
57 0 : static sal_Int32 lcl_getNavigationURLIndex( const OUString& _rNavURL )
58 : {
59 0 : const sal_Char** pLookup = pNavigationURLs;
60 0 : while ( *pLookup )
61 : {
62 0 : if ( _rNavURL.equalsAscii( *pLookup ) )
63 0 : return pLookup - pNavigationURLs;
64 0 : ++pLookup;
65 : }
66 0 : return -1;
67 : }
68 :
69 0 : static const sal_Char* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex )
70 : {
71 0 : const sal_Char** pLookup = pNavigationURLs;
72 0 : while ( _nButtonTypeIndex-- && *pLookup++ )
73 : ;
74 : OSL_ENSURE( *pLookup, "lcl_getNavigationURL: invalid index!" );
75 0 : return *pLookup;
76 : }
77 : }
78 :
79 :
80 : //= PushButtonNavigation
81 :
82 :
83 0 : PushButtonNavigation::PushButtonNavigation( const Reference< XPropertySet >& _rxControlModel )
84 : :m_xControlModel( _rxControlModel )
85 0 : ,m_bIsPushButton( false )
86 : {
87 : OSL_ENSURE( m_xControlModel.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
88 :
89 : try
90 : {
91 0 : m_bIsPushButton = ::comphelper::hasProperty( PROPERTY_BUTTONTYPE, m_xControlModel );
92 : }
93 0 : catch( const Exception& )
94 : {
95 : OSL_FAIL( "PushButtonNavigation::PushButtonNavigation: caught an exception!" );
96 : }
97 0 : }
98 :
99 :
100 0 : sal_Int32 PushButtonNavigation::implGetCurrentButtonType() const
101 : {
102 0 : sal_Int32 nButtonType = FormButtonType_PUSH;
103 0 : if ( !m_xControlModel.is() )
104 0 : return nButtonType;
105 0 : OSL_VERIFY( ::cppu::enum2int( nButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
106 :
107 0 : if ( nButtonType == FormButtonType_URL )
108 : {
109 : // there's a chance that this is a "virtual" button type
110 : // (which are realized by special URLs)
111 0 : OUString sTargetURL;
112 0 : m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL ) >>= sTargetURL;
113 :
114 0 : sal_Int32 nNavigationURLIndex = lcl_getNavigationURLIndex( sTargetURL );
115 0 : if ( nNavigationURLIndex >= 0)
116 : // it actually *is* a virtual button type
117 0 : nButtonType = s_nFirstVirtualButtonType + nNavigationURLIndex;
118 : }
119 0 : return nButtonType;
120 : }
121 :
122 :
123 0 : Any PushButtonNavigation::getCurrentButtonType() const
124 : {
125 : OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
126 0 : Any aReturn;
127 :
128 : try
129 : {
130 0 : aReturn <<= implGetCurrentButtonType();
131 : }
132 0 : catch( const Exception& )
133 : {
134 : OSL_FAIL( "PushButtonNavigation::getCurrentButtonType: caught an exception!" );
135 : }
136 0 : return aReturn;
137 : }
138 :
139 :
140 0 : void PushButtonNavigation::setCurrentButtonType( const Any& _rValue ) const
141 : {
142 : OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
143 0 : if ( !m_xControlModel.is() )
144 0 : return;
145 :
146 : try
147 : {
148 0 : sal_Int32 nButtonType = FormButtonType_PUSH;
149 0 : OSL_VERIFY( ::cppu::enum2int( nButtonType, _rValue ) );
150 0 : OUString sTargetURL;
151 :
152 0 : bool bIsVirtualButtonType = nButtonType >= s_nFirstVirtualButtonType;
153 0 : if ( bIsVirtualButtonType )
154 : {
155 0 : const sal_Char* pURL = lcl_getNavigationURL( nButtonType - s_nFirstVirtualButtonType );
156 0 : sTargetURL = OUString::createFromAscii( pURL );
157 :
158 0 : nButtonType = FormButtonType_URL;
159 : }
160 :
161 0 : m_xControlModel->setPropertyValue( PROPERTY_BUTTONTYPE, makeAny( static_cast< FormButtonType >( nButtonType ) ) );
162 0 : m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, makeAny( sTargetURL ) );
163 : }
164 0 : catch( const Exception& )
165 : {
166 : OSL_FAIL( "PushButtonNavigation::setCurrentButtonType: caught an exception!" );
167 : }
168 : }
169 :
170 :
171 0 : PropertyState PushButtonNavigation::getCurrentButtonTypeState( ) const
172 : {
173 : OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
174 0 : PropertyState eState = PropertyState_DIRECT_VALUE;
175 :
176 : try
177 : {
178 0 : Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
179 0 : if ( xStateAccess.is() )
180 : {
181 : // let's see what the model says about the ButtonType property
182 0 : eState = xStateAccess->getPropertyState( PROPERTY_BUTTONTYPE );
183 0 : if ( eState == PropertyState_DIRECT_VALUE )
184 : {
185 0 : sal_Int32 nRealButtonType = FormButtonType_PUSH;
186 0 : OSL_VERIFY( ::cppu::enum2int( nRealButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
187 : // perhaps it's one of the virtual button types?
188 0 : if ( FormButtonType_URL == nRealButtonType )
189 : {
190 : // yes, it is -> rely on the state of the URL property
191 0 : eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
192 : }
193 : }
194 0 : }
195 : }
196 0 : catch( const Exception& )
197 : {
198 : OSL_FAIL( "PushButtonNavigation::getCurrentButtonTypeState: caught an exception!" );
199 : }
200 :
201 0 : return eState;
202 : }
203 :
204 :
205 0 : Any PushButtonNavigation::getCurrentTargetURL() const
206 : {
207 0 : Any aReturn;
208 0 : if ( !m_xControlModel.is() )
209 0 : return aReturn;
210 :
211 : try
212 : {
213 0 : aReturn = m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL );
214 0 : if ( m_bIsPushButton )
215 : {
216 0 : sal_Int32 nCurrentButtonType = implGetCurrentButtonType();
217 0 : bool bIsVirtualButtonType = nCurrentButtonType >= s_nFirstVirtualButtonType;
218 0 : if ( bIsVirtualButtonType )
219 : {
220 : // pretend (to the user) that there's no URL set - since
221 : // virtual button types imply a special (technical) URL which
222 : // the user should not see
223 0 : aReturn <<= OUString();
224 : }
225 : }
226 : }
227 0 : catch( const Exception& )
228 : {
229 : OSL_FAIL( "PushButtonNavigation::getCurrentTargetURL: caught an exception!" );
230 : }
231 0 : return aReturn;
232 : }
233 :
234 :
235 0 : void PushButtonNavigation::setCurrentTargetURL( const Any& _rValue ) const
236 : {
237 0 : if ( !m_xControlModel.is() )
238 0 : return;
239 :
240 : try
241 : {
242 0 : m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, _rValue );
243 : }
244 0 : catch( const Exception& )
245 : {
246 : OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
247 : }
248 : }
249 :
250 :
251 0 : PropertyState PushButtonNavigation::getCurrentTargetURLState( ) const
252 : {
253 0 : PropertyState eState = PropertyState_DIRECT_VALUE;
254 :
255 : try
256 : {
257 0 : Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
258 0 : if ( xStateAccess.is() )
259 : {
260 0 : eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
261 0 : }
262 : }
263 0 : catch( const Exception& )
264 : {
265 : OSL_FAIL( "PushButtonNavigation::setCurrentTargetURL: caught an exception!" );
266 : }
267 :
268 0 : return eState;
269 : }
270 :
271 :
272 0 : bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
273 : {
274 0 : sal_Int32 nButtonType( FormButtonType_PUSH );
275 : try
276 : {
277 0 : nButtonType = implGetCurrentButtonType();
278 : }
279 0 : catch( const Exception& )
280 : {
281 : DBG_UNHANDLED_EXCEPTION();
282 : }
283 0 : return nButtonType == FormButtonType_URL;
284 : }
285 :
286 :
287 0 : bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
288 : {
289 0 : OUString sTargetURL;
290 0 : OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL );
291 0 : return !sTargetURL.isEmpty();
292 : }
293 :
294 :
295 : } // namespace pcr
296 :
297 :
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|