tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / extensions / source / propctrlr / pushbuttonnavigation.cxx
blob130cf322d15df174ddf1d18fffd11dbb0806e059
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "pushbuttonnavigation.hxx"
21 #include <com/sun/star/beans/XPropertyState.hpp>
22 #include "formstrings.hxx"
23 #include <comphelper/extract.hxx>
24 #include <comphelper/property.hxx>
25 #include <o3tl/string_view.hxx>
26 #include <osl/diagnose.h>
27 #include <comphelper/diagnose_ex.hxx>
30 namespace pcr
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::form;
39 namespace
41 const sal_Int32 s_nFirstVirtualButtonType = 1 + sal_Int32(FormButtonType_URL);
43 const char* pNavigationURLs[] =
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
57 sal_Int32 lcl_getNavigationURLIndex( std::u16string_view _rNavURL )
59 const char** pLookup = pNavigationURLs;
60 while ( *pLookup )
62 if ( o3tl::equalsAscii( _rNavURL, *pLookup ) )
63 return pLookup - pNavigationURLs;
64 ++pLookup;
66 return -1;
69 const char* lcl_getNavigationURL( sal_Int32 _nButtonTypeIndex )
71 const char** pLookup = pNavigationURLs;
72 while ( _nButtonTypeIndex-- && *pLookup++ )
74 OSL_ENSURE( *pLookup, "lcl_getNavigationURL: invalid index!" );
75 return *pLookup;
80 //= PushButtonNavigation
83 PushButtonNavigation::PushButtonNavigation( const Reference< XPropertySet >& _rxControlModel )
84 :m_xControlModel( _rxControlModel )
85 ,m_bIsPushButton( false )
87 OSL_ENSURE( m_xControlModel.is(), "PushButtonNavigation::PushButtonNavigation: invalid control model!" );
89 try
91 m_bIsPushButton = ::comphelper::hasProperty( PROPERTY_BUTTONTYPE, m_xControlModel );
93 catch( const Exception& )
95 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::PushButtonNavigation" );
100 FormButtonType PushButtonNavigation::implGetCurrentButtonType() const
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 ) ) );
107 if ( nButtonType == sal_Int32(FormButtonType_URL) )
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;
114 sal_Int32 nNavigationURLIndex = lcl_getNavigationURLIndex( sTargetURL );
115 if ( nNavigationURLIndex >= 0)
116 // it actually *is* a virtual button type
117 nButtonType = s_nFirstVirtualButtonType + nNavigationURLIndex;
119 return static_cast<FormButtonType>(nButtonType);
123 Any PushButtonNavigation::getCurrentButtonType() const
125 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonType: not expected to be called for forms!" );
126 Any aReturn;
130 aReturn <<= implGetCurrentButtonType();
132 catch( const Exception& )
134 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonType" );
136 return aReturn;
140 void PushButtonNavigation::setCurrentButtonType( const Any& _rValue ) const
142 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::setCurrentButtonType: not expected to be called for forms!" );
143 if ( !m_xControlModel.is() )
144 return;
148 sal_Int32 nButtonType = sal_Int32(FormButtonType_PUSH);
149 OSL_VERIFY( ::cppu::enum2int( nButtonType, _rValue ) );
150 OUString sTargetURL;
152 bool bIsVirtualButtonType = nButtonType >= s_nFirstVirtualButtonType;
153 if ( bIsVirtualButtonType )
155 const char* pURL = lcl_getNavigationURL( nButtonType - s_nFirstVirtualButtonType );
156 sTargetURL = OUString::createFromAscii( pURL );
158 nButtonType = sal_Int32(FormButtonType_URL);
161 m_xControlModel->setPropertyValue( PROPERTY_BUTTONTYPE, Any( static_cast< FormButtonType >( nButtonType ) ) );
162 m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, Any( sTargetURL ) );
164 catch( const Exception& )
166 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentButtonType" );
171 PropertyState PushButtonNavigation::getCurrentButtonTypeState( ) const
173 OSL_ENSURE( m_bIsPushButton, "PushButtonNavigation::getCurrentButtonTypeState: not expected to be called for forms!" );
174 PropertyState eState = PropertyState_DIRECT_VALUE;
178 Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
179 if ( xStateAccess.is() )
181 // let's see what the model says about the ButtonType property
182 eState = xStateAccess->getPropertyState( PROPERTY_BUTTONTYPE );
183 if ( eState == PropertyState_DIRECT_VALUE )
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 )
190 // yes, it is -> rely on the state of the URL property
191 eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
196 catch( const Exception& )
198 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentButtonTypeState" );
201 return eState;
205 Any PushButtonNavigation::getCurrentTargetURL() const
207 Any aReturn;
208 if ( !m_xControlModel.is() )
209 return aReturn;
213 aReturn = m_xControlModel->getPropertyValue( PROPERTY_TARGET_URL );
214 if ( m_bIsPushButton )
216 FormButtonType nCurrentButtonType = implGetCurrentButtonType();
217 bool bIsVirtualButtonType = nCurrentButtonType >= FormButtonType(s_nFirstVirtualButtonType);
218 if ( bIsVirtualButtonType )
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();
227 catch( const Exception& )
229 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::getCurrentTargetURL" );
231 return aReturn;
235 void PushButtonNavigation::setCurrentTargetURL( const Any& _rValue ) const
237 if ( !m_xControlModel.is() )
238 return;
242 m_xControlModel->setPropertyValue( PROPERTY_TARGET_URL, _rValue );
244 catch( const Exception& )
246 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
251 PropertyState PushButtonNavigation::getCurrentTargetURLState( ) const
253 PropertyState eState = PropertyState_DIRECT_VALUE;
257 Reference< XPropertyState > xStateAccess( m_xControlModel, UNO_QUERY );
258 if ( xStateAccess.is() )
260 eState = xStateAccess->getPropertyState( PROPERTY_TARGET_URL );
263 catch( const Exception& )
265 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PushButtonNavigation::setCurrentTargetURL" );
268 return eState;
272 bool PushButtonNavigation::currentButtonTypeIsOpenURL() const
274 FormButtonType nButtonType( FormButtonType_PUSH );
277 nButtonType = implGetCurrentButtonType();
279 catch( const Exception& )
281 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
283 return nButtonType == FormButtonType_URL;
287 bool PushButtonNavigation::hasNonEmptyCurrentTargetURL() const
289 OUString sTargetURL;
290 OSL_VERIFY( getCurrentTargetURL() >>= sTargetURL );
291 return !sTargetURL.isEmpty();
295 } // namespace pcr
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */