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 #ifndef RPT_GEOMETRYHANDLER_HXX
25 #define RPT_GEOMETRYHANDLER_HXX
26 
27 #include "sal/config.h"
28 #include "com/sun/star/uno/XComponentContext.hpp"
29 #include "cppuhelper/compbase3.hxx"
30 #include "cppuhelper/basemutex.hxx"
31 #include "com/sun/star/inspection/XPropertyHandler.hpp"
32 #include "com/sun/star/script/XTypeConverter.hpp"
33 #include "com/sun/star/beans/XPropertySet.hpp"
34 #include "com/sun/star/lang/XServiceInfo.hpp"
35 #include "com/sun/star/report/XReportComponent.hpp"
36 #include "com/sun/star/report/XFunction.hpp"
37 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
38 #include <com/sun/star/sdbc/XRowSet.hpp>
39 #include <memory>
40 #include <comphelper/stl_types.hxx>
41 #include <comphelper/listenernotification.hxx>
42 #include "metadata.hxx"
43 
44 //........................................................................
45 namespace rptui
46 {
47 //........................................................................
48 
49     struct DefaultFunction
50     {
51         com::sun::star::beans::Optional< ::rtl::OUString>   m_sInitialFormula;
52         ::rtl::OUString                                     m_sName;
53         ::rtl::OUString                                     m_sSearchString;
54         ::rtl::OUString                                     m_sFormula;
55         ::sal_Bool                                          m_bPreEvaluated;
56         ::sal_Bool                                          m_bDeepTraversing;
57 
getNamerptui::DefaultFunction58         inline ::rtl::OUString getName() const { return m_sName; }
59     } ;
60 
61     class OPropertyInfoService;
62     typedef ::std::pair< ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunction>, ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunctionsSupplier> > TFunctionPair;
63     typedef ::std::multimap< ::rtl::OUString,TFunctionPair, ::comphelper::UStringMixLess > TFunctions;
64     typedef ::comphelper::OSimpleListenerContainer  <   ::com::sun::star::beans::XPropertyChangeListener
65                                                     ,   ::com::sun::star::beans::PropertyChangeEvent
66                                                     >   PropertyChangeListeners;
67     typedef ::cppu::WeakComponentImplHelper3<   ::com::sun::star::inspection::XPropertyHandler
68                                             ,   ::com::sun::star::beans::XPropertyChangeListener
69                                             ,   ::com::sun::star::lang::XServiceInfo> GeometryHandler_Base;
70 
71     class GeometryHandler:
72         private ::cppu::BaseMutex,
73         public GeometryHandler_Base
74     {
75         /** sets the counter function at the data field.
76         *   If the counter function doesn't exist it will be created.
77         */
78         void impl_setCounterFunction_throw();
79 
80         /** executes a dialog for chosing a filter criterion for a database report
81             @param _out_rSelectedClause
82                 the filter or order clause as chosen by the user
83             @precond
84                 we're really inspecting a database form (well, a RowSet at least)
85             @return
86                 <TRUE/> if and only if the user successfully chose a clause
87         */
88         bool impl_dialogFilter_nothrow( ::rtl::OUString& _out_rSelectedClause, ::osl::ClearableMutexGuard& _rClearBeforeDialog ) const;
89 
90         /** returns the data field type depending on the data field of the report control
91         *
92         * \param _sDataField if the data field is not empty it will be used as data field, otherwise the data field will be used.
93         * \return the data field type
94         */
95         sal_uInt32 impl_getDataFieldType_throw(const ::rtl::OUString& _sDataField = ::rtl::OUString()) const;
96 
97         ::com::sun::star::uno::Any getConstantValue(sal_Bool bToControlValue,sal_uInt16 nResId,const ::com::sun::star::uno::Any& _aValue,const ::rtl::OUString& _sConstantName,const ::rtl::OUString & PropertyName );
98         ::com::sun::star::beans::Property getProperty(const ::rtl::OUString & PropertyName);
99         void implCreateListLikeControl(
100                 const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& _rxControlFactory
101                 ,::com::sun::star::inspection::LineDescriptor & out_Descriptor
102                 ,sal_uInt16 _nResId
103                 ,sal_Bool _bReadOnlyControl
104                 ,sal_Bool _bTrueIfListBoxFalseIfComboBox
105             );
106         void implCreateListLikeControl(
107                 const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& _rxControlFactory
108                 ,::com::sun::star::inspection::LineDescriptor & out_Descriptor
109                 ,const ::std::vector< ::rtl::OUString>& _aEntries
110                 ,sal_Bool _bReadOnlyControl
111                 ,sal_Bool _bTrueIfListBoxFalseIfComboBox
112             );
113         void checkPosAndSize(   const ::com::sun::star::awt::Point& _aNewPos,
114                                 const ::com::sun::star::awt::Size& _aSize);
115 
116         ::rtl::OUString impl_convertToFormula( const ::com::sun::star::uno::Any& _rControlValue );
117 
118         void impl_initFieldList_nothrow( ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rFieldNames ) const;
119 
120         /** Creates the function defined by the function template
121         *
122         * \param _sFunctionName the function name
123         * \param _sDataField the data field
124         * \param _aFunction the function template
125         */
126         void impl_createFunction(const ::rtl::OUString& _sFunctionName,const ::rtl::OUString& _sDataField,const DefaultFunction& _aFunction);
127 
128         /** check whether the given function name is a countr function.
129         *
130         * \param _sQuotedFunctionName the quoted function name to check
131         * \param _Out_sScope the scope of the function
132         * \return When true it is a counter functions otherwise false.
133         */
134         bool impl_isCounterFunction_throw(const ::rtl::OUString& _sQuotedFunctionName,::rtl::OUString& _Out_sScope) const;
135 
136         /** clear the own properties like function and scope and send a notification
137         *
138         * \param _aGuard
139         * \param _sOldFunctionName
140         * \param _sOldScope
141         * \param _nOldDataFieldType
142         */
143        void resetOwnProperties(::osl::ResettableMutexGuard& _aGuard,const ::rtl::OUString& _sOldFunctionName,const ::rtl::OUString& _sOldScope,const sal_uInt32 _nOldDataFieldType);
144 
145         /** checks whether the name is a field or a parameter
146         *
147         * \param _sName the name to check
148         * \return true when it is a field or parameter otherwise false
149         */
150         bool impl_isDataField(const ::rtl::OUString& _sName) const;
151 
152         /**return all formula in a semicolon separated list
153         *
154         * \param _rList the localized function names
155         */
156         void impl_fillFormulaList_nothrow(::std::vector< ::rtl::OUString >& _out_rList) const;
157 
158         /** return all group names in a semicolon separated list starting with the group where this control is contained in.
159         *
160         * \param _rList fills the list with all scope names.
161         */
162         void impl_fillScopeList_nothrow(::std::vector< ::rtl::OUString >& _out_rList) const;
163 
164         /** return all supported output formats of the report definition
165         *
166         * \param _rList fills the list with all mime types
167         */
168         void impl_fillMimeTypes_nothrow(::std::vector< ::rtl::OUString >& _out_rList) const;
169 
170         /** return the one supported output formats of the report definition
171         *
172         * \param _sMimetype the mimetype
173         */
174         ::rtl::OUString impl_ConvertMimeTypeToUI_nothrow(const ::rtl::OUString& _sMimetype) const;
175 
176         /** return the MimeType for the given UI Name
177         *
178         * \param _sUIName the doc ui name
179         */
180         ::rtl::OUString impl_ConvertUIToMimeType_nothrow(const ::rtl::OUString& _sUIName) const;
181 
182         /** get the functions supplier for the set scope, default is the surrounding group.
183         *
184         * \param _rsNamePostFix the name post fix which canbe used when the scope as name part is needed
185         * \return the function supplier
186         */
187         ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunctionsSupplier> fillScope_throw(::rtl::OUString& _rsNamePostFix);
188 
189         /** checks if the given function is a default function we know.
190         *
191         * \param _sQuotedFunction the quoted function name
192         * \param _Out_rDataField the data field which is used in the function
193         * \param _xFunctionsSupplier the function supplier to search or empty if not used
194         * \param _bSet If set to sal_True than the m_sDefaultFunction and m_sScope vars will be set if successful.
195         * \return sal_True with known otherwise sal_False
196         */
197         sal_Bool isDefaultFunction(const ::rtl::OUString& _sQuotedFunction
198                                     ,::rtl::OUString& _Out_rDataField
199                                     ,const ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunctionsSupplier>& _xFunctionsSupplier = ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunctionsSupplier>()
200                                     ,bool _bSet = false) const;
201 
202         /** checks if the given function is a default function we know.
203         *
204         * \param _xFunction
205         * \param _rDataField
206         * \param _rsDefaultFunctionName
207         * \return
208         */
209         sal_Bool impl_isDefaultFunction_nothrow( const ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunction>& _xFunction
210                                             ,::rtl::OUString& _rDataField
211                                             ,::rtl::OUString& _rsDefaultFunctionName) const;
212 
213         /** fills the member m_aDefaultFunctions
214         *
215         */
216         void loadDefaultFunctions();
217 
218         /** creates a default functionof the _sFunction for the data field _sDataField
219         *   The new function will only be created if it didn't exist.
220         *
221         * \param _aGuard        Will be cleared, when a new function was created.
222         * \param _sFunction     The name of the function.
223         * \param _sDataField    The name of the data field.
224         */
225         void createDefaultFunction(::osl::ResettableMutexGuard& _aGuard ,const ::rtl::OUString& _sFunction,const ::rtl::OUString& _sDataField);
226 
227         void removeFunction();
228 
229         class OBlocker
230         {
231             bool& m_bIn;
232         public:
OBlocker(bool & _bIn)233             OBlocker(bool& _bIn) : m_bIn(_bIn){ m_bIn = true; }
~OBlocker()234             ~OBlocker() { m_bIn = false; }
235         };
236 
237 
238         // XEventListener
239 		virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& Source) throw( ::com::sun::star::uno::RuntimeException );
240         // XPropertyChangeListener
241 		virtual void SAL_CALL propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw(::com::sun::star::uno::RuntimeException);
242 
243     public:
244         // XServiceInfo - static versions
245 		static ::rtl::OUString getImplementationName_Static(  ) throw(::com::sun::star::uno::RuntimeException);
246 		static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_static(  ) throw(::com::sun::star::uno::RuntimeException);
247 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > SAL_CALL
248 						create(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >&);
249 
250     public:
251         explicit GeometryHandler(::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > const & context);
252 
253         // XServiceInfo
254 		virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException);
255 		virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);
256 		virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException);
257 
258         // ::com::sun::star::lang::XComponent:
259         virtual void SAL_CALL addEventListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > & xListener)   throw (::com::sun::star::uno::RuntimeException);
260         virtual void SAL_CALL removeEventListener(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > & aListener) throw (::com::sun::star::uno::RuntimeException);
261 
262         // ::com::sun::star::inspection::XPropertyHandler:
263         virtual void SAL_CALL inspect(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > & Component) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::NullPointerException);
264         virtual ::com::sun::star::uno::Any SAL_CALL getPropertyValue(const ::rtl::OUString & PropertyName) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
265         virtual void SAL_CALL setPropertyValue(const ::rtl::OUString & PropertyName, const ::com::sun::star::uno::Any & Value) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
266         virtual ::com::sun::star::beans::PropertyState SAL_CALL getPropertyState(const ::rtl::OUString & PropertyName) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
267         virtual ::com::sun::star::inspection::LineDescriptor SAL_CALL describePropertyLine(const ::rtl::OUString& PropertyName, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyControlFactory >& ControlFactory ) throw (::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException, ::com::sun::star::uno::RuntimeException);
268         virtual ::com::sun::star::uno::Any SAL_CALL convertToPropertyValue(const ::rtl::OUString & PropertyName, const ::com::sun::star::uno::Any & ControlValue) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
269         virtual ::com::sun::star::uno::Any SAL_CALL convertToControlValue(const ::rtl::OUString & PropertyName, const ::com::sun::star::uno::Any & PropertyValue, const ::com::sun::star::uno::Type & ControlValueType) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
270         virtual void SAL_CALL addPropertyChangeListener(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener > & Listener) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::NullPointerException);
271         virtual void SAL_CALL removePropertyChangeListener(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener > & _rxListener) throw (::com::sun::star::uno::RuntimeException);
272         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > SAL_CALL getSupportedProperties() throw (::com::sun::star::uno::RuntimeException);
273         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupersededProperties() throw (::com::sun::star::uno::RuntimeException);
274         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getActuatingProperties() throw (::com::sun::star::uno::RuntimeException);
275         virtual ::sal_Bool SAL_CALL isComposable(const ::rtl::OUString & PropertyName) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException);
276         virtual ::com::sun::star::inspection::InteractiveSelectionResult SAL_CALL onInteractivePropertySelection(const ::rtl::OUString & PropertyName, ::sal_Bool Primary, ::com::sun::star::uno::Any & out_Data, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI > & InspectorUI) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::lang::NullPointerException);
277         virtual void SAL_CALL actuatingPropertyChanged(const ::rtl::OUString & ActuatingPropertyName, const ::com::sun::star::uno::Any & NewValue, const ::com::sun::star::uno::Any & OldValue, const ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XObjectInspectorUI > & InspectorUI, ::sal_Bool FirstTimeInit) throw (::com::sun::star::uno::RuntimeException, ::com::sun::star::lang::NullPointerException);
278         virtual ::sal_Bool SAL_CALL suspend(::sal_Bool Suspend) throw (::com::sun::star::uno::RuntimeException);
279 
280     protected:
281         virtual ~GeometryHandler();
282     private:
283         GeometryHandler(GeometryHandler &); // not defined
284         void operator =(GeometryHandler &); // not defined
285 
286         // overload WeakComponentImplHelperBase::disposing()
287         // This function is called upon disposing the component,
288         // if your component needs special work when it becomes
289         // disposed, do it here.
290         virtual void SAL_CALL disposing();
291 
292         PropertyChangeListeners                                                             m_aPropertyListeners;
293         ::com::sun::star::uno::Sequence< ::rtl::OUString >                                  m_aFieldNames;
294         ::com::sun::star::uno::Sequence< ::rtl::OUString >                                  m_aParamNames;
295         TFunctions                                                                          m_aFunctionNames;
296         ::std::vector< DefaultFunction >                                                    m_aDefaultFunctions;
297         DefaultFunction                                                                     m_aCounterFunction;
298         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >        m_xContext;
299         mutable ::com::sun::star::uno::Reference< ::com::sun::star::report::XFunction>      m_xFunction;
300         ::com::sun::star::uno::Reference< ::com::sun::star::inspection::XPropertyHandler >  m_xFormComponentHandler; /// delegatee
301         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >           m_xReportComponent; /// inspectee
302         mutable ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRowSet >         m_xRowSet;
303         /// type converter, needed on various occasions
304         ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter >        m_xTypeConverter;
305         /// access to property meta data
306         ::std::auto_ptr< OPropertyInfoService >                                             m_pInfoService;
307         mutable ::rtl::OUString                                                             m_sDefaultFunction;
308         mutable ::rtl::OUString                                                             m_sScope;
309         sal_uInt32                                                                          m_nDataFieldType;
310         mutable bool                                                                        m_bNewFunction;
311         bool                                                                                m_bIn;
312     };
313 //........................................................................
314 } // namespace rptui
315 //........................................................................
316 
317 #endif // RPT_GeometryHandler_HXX
318