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