LibreOffice Module extensions (master) 1
pushbuttonnavigation.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/beans/XPropertyState.hpp>
22#include "formstrings.hxx"
25#include <o3tl/string_view.hxx>
26#include <osl/diagnose.h>
28
29
30namespace 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 const sal_Int32 s_nFirstVirtualButtonType = 1 + sal_Int32(FormButtonType_URL);
42
43 const 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 nullptr
55 };
56
57 sal_Int32 lcl_getNavigationURLIndex( std::u16string_view _rNavURL )
58 {
59 const char** pLookup = pNavigationURLs;
60 while ( *pLookup )
61 {
62 if ( o3tl::equalsAscii( _rNavURL, *pLookup ) )
63 return pLookup - pNavigationURLs;
64 ++pLookup;
65 }
66 return -1;
67 }
68
69 const char* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex )
70 {
71 const char** pLookup = pNavigationURLs;
72 while ( _nButtonTypeIndex-- && *pLookup++ )
73 ;
74 OSL_ENSURE( *pLookup, "lcl_getNavigationURL: invalid index!" );
75 return *pLookup;
76 }
77 }
78
79
80 //= PushButtonNavigation
81
82
83 PushButtonNavigation::PushButtonNavigation( const Reference< XPropertySet >& _rxControlModel )
84 :m_xControlModel( _rxControlModel )
85 ,m_bIsPushButton( false )
86 {
87 OSL_ENSURE( m_xControlModel.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
88
89 try
90 {
91 m_bIsPushButton = ::comphelper::hasProperty( PROPERTY_BUTTONTYPE, m_xControlModel );
92 }
93 catch( const Exception& )
94 {
95 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::PushButtonNavigation" );
96 }
97 }
98
99
101 {
102 sal_Int32 nButtonType = sal_Int32(FormButtonType_PUSH);
103 if ( !m_xControlModel.is() )
104 return static_cast<FormButtonType>(nButtonType);
105 OSL_VERIFY( ::cppu::enum2int( nButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
106
107 if ( nButtonType == sal_Int32(FormButtonType_URL) )
108 {
109 // there's a chance that this is a "virtual" button type
110 // (which are realized by special URLs)
111 OUString sTargetURL;
112 m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL ) >>= sTargetURL;
113
114 sal_Int32 nNavigationURLIndex = lcl_getNavigationURLIndex( sTargetURL );
115 if ( nNavigationURLIndex >= 0)
116 // it actually *is* a virtual button type
117 nButtonType = s_nFirstVirtualButtonType + nNavigationURLIndex;
118 }
119 return static_cast<FormButtonType>(nButtonType);
120 }
121
122
124 {
125 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
126 Any aReturn;
127
128 try
129 {
130 aReturn <<= implGetCurrentButtonType();
131 }
132 catch( const Exception& )
133 {
134 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonType" );
135 }
136 return aReturn;
137 }
138
139
140 void PushButtonNavigation::setCurrentButtonType( const Any& _rValue ) const
141 {
142 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
143 if ( !m_xControlModel.is() )
144 return;
145
146 try
147 {
148 sal_Int32 nButtonType = sal_Int32(FormButtonType_PUSH);
149 OSL_VERIFY( ::cppu::enum2int( nButtonType, _rValue ) );
150 OUString sTargetURL;
151
152 bool bIsVirtualButtonType = nButtonType >= s_nFirstVirtualButtonType;
153 if ( bIsVirtualButtonType )
154 {
155 const char* pURL = lcl_getNavigationURL( nButtonType - s_nFirstVirtualButtonType );
156 sTargetURL = OUString::createFromAscii( pURL );
157
158 nButtonType = sal_Int32(FormButtonType_URL);
159 }
160
161 m_xControlModel->setPropertyValue( PROPERTY_BUTTONTYPE, Any( static_cast< FormButtonType >( nButtonType ) ) );
162 m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, Any( sTargetURL ) );
163 }
164 catch( const Exception& )
165 {
166 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentButtonType" );
167 }
168 }
169
170
172 {
173 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
174 PropertyState eState = PropertyState_DIRECT_VALUE;
175
176 try
177 {
178 Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
179 if ( xStateAccess.is() )
180 {
181 // let's see what the model says about the ButtonType property
182 eState = xStateAccess->getPropertyState( PROPERTY_BUTTONTYPE );
183 if ( eState == PropertyState_DIRECT_VALUE )
184 {
185 sal_Int32 nRealButtonType = sal_Int32(FormButtonType_PUSH);
186 OSL_VERIFY( ::cppu::enum2int( nRealButtonType, m_xControlModel->getPropertyValue( PROPERTY_BUTTONTYPE ) ) );
187 // perhaps it's one of the virtual button types?
188 if ( sal_Int32(FormButtonType_URL) == nRealButtonType )
189 {
190 // yes, it is -> rely on the state of the URL property
191 eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
192 }
193 }
194 }
195 }
196 catch( const Exception& )
197 {
198 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonTypeState" );
199 }
200
201 return eState;
202 }
203
204
206 {
207 Any aReturn;
208 if ( !m_xControlModel.is() )
209 return aReturn;
210
211 try
212 {
213 aReturn = m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL );
214 if ( m_bIsPushButton )
215 {
216 FormButtonType nCurrentButtonType = implGetCurrentButtonType();
217 bool bIsVirtualButtonType = nCurrentButtonType >= FormButtonType(s_nFirstVirtualButtonType);
218 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 aReturn <<= OUString();
224 }
225 }
226 }
227 catch( const Exception& )
228 {
229 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentTargetURL" );
230 }
231 return aReturn;
232 }
233
234
235 void PushButtonNavigation::setCurrentTargetURL( const Any& _rValue ) const
236 {
237 if ( !m_xControlModel.is() )
238 return;
239
240 try
241 {
242 m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, _rValue );
243 }
244 catch( const Exception& )
245 {
246 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
247 }
248 }
249
250
252 {
253 PropertyState eState = PropertyState_DIRECT_VALUE;
254
255 try
256 {
257 Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
258 if ( xStateAccess.is() )
259 {
260 eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
261 }
262 }
263 catch( const Exception& )
264 {
265 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
266 }
267
268 return eState;
269 }
270
271
273 {
274 FormButtonType nButtonType( FormButtonType_PUSH );
275 try
276 {
277 nButtonType = implGetCurrentButtonType();
278 }
279 catch( const Exception& )
280 {
281 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
282 }
283 return nButtonType == FormButtonType_URL;
284 }
285
286
288 {
289 OUString sTargetURL;
290 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL );
291 return !sTargetURL.isEmpty();
292 }
293
294
295} // namespace pcr
296
297
298/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool currentButtonTypeIsOpenURL() const
determines whether the current button type is FormButtonType_URL
css::form::FormButtonType implGetCurrentButtonType() const
void setCurrentButtonType(const css::uno::Any &_rValue) const
sets the current value of the "ButtonType" property, taking into account the "virtual" button types s...
void setCurrentTargetURL(const css::uno::Any &_rValue) const
sets the current value of the "TargetURL" property, taking into account that some URLs are special va...
css::beans::PropertyState getCurrentButtonTypeState() const
retrieves the current state of the "ButtonType" property, taking into account the "virtual" button ty...
css::uno::Reference< css::beans::XPropertySet > m_xControlModel
css::uno::Any getCurrentButtonType() const
returns the current value of the "ButtonType" property, taking into account the "virtual" button type...
css::uno::Any getCurrentTargetURL() const
returns the current value of the "TargetURL" property, taking into account that some URLs are special...
PushButtonNavigation(const css::uno::Reference< css::beans::XPropertySet > &_rxControlModel)
ctor
css::beans::PropertyState getCurrentTargetURLState() const
retrieves the current state of the "TargetURL" property, taking into account that some URLs are speci...
bool hasNonEmptyCurrentTargetURL() const
determines whether the TargetURL property does currently denote a non-empty string
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
constexpr OUStringLiteral PROPERTY_TARGET_URL
Definition: formstrings.hxx:51
constexpr OUStringLiteral PROPERTY_BUTTONTYPE
Definition: formstrings.hxx:58
@ Exception
bool equalsAscii(std::u16string_view s1, std::string_view s2)
a property handler for any virtual string properties
Definition: browserline.cxx:39