1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*63bba73cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*63bba73cSAndrew Rist  * distributed with this work for additional information
6*63bba73cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*63bba73cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist  * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*63bba73cSAndrew Rist  *
11*63bba73cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist  *
13*63bba73cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist  * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*63bba73cSAndrew Rist  * specific language governing permissions and limitations
18*63bba73cSAndrew Rist  * under the License.
19*63bba73cSAndrew Rist  *
20*63bba73cSAndrew Rist  *************************************************************/
21*63bba73cSAndrew Rist 
22*63bba73cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "XFormsModelContext.hxx"
28cdf0e10cSrcweir #include <vector>
29cdf0e10cSrcweir #include <utility>
30cdf0e10cSrcweir #include "xmloff/xformsimport.hxx"
31cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
32cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/form/binding/XValueBinding.hpp>
34cdf0e10cSrcweir #include <com/sun/star/form/binding/XBindableValue.hpp>
35cdf0e10cSrcweir #include <com/sun/star/form/binding/XListEntrySource.hpp>
36cdf0e10cSrcweir #include <com/sun/star/form/binding/XListEntrySink.hpp>
37cdf0e10cSrcweir #include <com/sun/star/form/submission/XSubmission.hpp>
38cdf0e10cSrcweir #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
39cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
40cdf0e10cSrcweir #include <rtl/ustring.hxx>
41cdf0e10cSrcweir #include <xformsapi.hxx>
42cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
43cdf0e10cSrcweir #include <tools/diagnose_ex.h>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using std::pair;
46cdf0e10cSrcweir using com::sun::star::uno::Reference;
47cdf0e10cSrcweir using com::sun::star::uno::Exception;
48cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
49cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
50cdf0e10cSrcweir using com::sun::star::uno::UNO_SET_THROW;
51cdf0e10cSrcweir using com::sun::star::uno::Sequence;
52cdf0e10cSrcweir using com::sun::star::beans::XPropertySet;
53cdf0e10cSrcweir using com::sun::star::beans::XPropertySetInfo;
54cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
55cdf0e10cSrcweir using com::sun::star::frame::XModel;
56cdf0e10cSrcweir using com::sun::star::container::XNameAccess;
57cdf0e10cSrcweir using com::sun::star::form::binding::XValueBinding;
58cdf0e10cSrcweir using com::sun::star::form::binding::XBindableValue;
59cdf0e10cSrcweir using com::sun::star::form::binding::XListEntrySource;
60cdf0e10cSrcweir using com::sun::star::form::binding::XListEntrySink;
61cdf0e10cSrcweir using com::sun::star::form::submission::XSubmission;
62cdf0e10cSrcweir using com::sun::star::form::submission::XSubmissionSupplier;
63cdf0e10cSrcweir using rtl::OUString;
64cdf0e10cSrcweir 
createXFormsModelContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const rtl::OUString & rLocalName)65cdf0e10cSrcweir SvXMLImportContext* createXFormsModelContext(
66cdf0e10cSrcweir     SvXMLImport& rImport,
67cdf0e10cSrcweir     sal_uInt16 nPrefix,
68cdf0e10cSrcweir     const rtl::OUString& rLocalName )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     return new XFormsModelContext( rImport, nPrefix, rLocalName );
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
bindXFormsValueBinding(Reference<XModel> xModel,pair<Reference<XPropertySet>,OUString> aPair)73cdf0e10cSrcweir void bindXFormsValueBinding(
74cdf0e10cSrcweir 	Reference<XModel> xModel,
75cdf0e10cSrcweir     pair<Reference<XPropertySet>,OUString> aPair )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     Reference<XBindableValue> xBindable(
78cdf0e10cSrcweir         aPair.first,
79cdf0e10cSrcweir         UNO_QUERY );
80cdf0e10cSrcweir     Reference<XValueBinding> xBinding(
81cdf0e10cSrcweir         lcl_findXFormsBinding( xModel, aPair.second ),
82cdf0e10cSrcweir         UNO_QUERY );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     if( xBindable.is() && xBinding.is() )
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         try
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             xBindable->setValueBinding( xBinding );
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         catch( const Exception& )
91cdf0e10cSrcweir         {
92cdf0e10cSrcweir             // ignore problems during binding
93cdf0e10cSrcweir             // TODO: call XML error handling
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
bindXFormsListBinding(Reference<XModel> xModel,::pair<Reference<XPropertySet>,OUString> aPair)98cdf0e10cSrcweir void bindXFormsListBinding(
99cdf0e10cSrcweir 	Reference<XModel> xModel,
100cdf0e10cSrcweir     ::pair<Reference<XPropertySet>,OUString> aPair )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     Reference<XListEntrySink> xListEntrySink(
103cdf0e10cSrcweir         aPair.first,
104cdf0e10cSrcweir         UNO_QUERY );
105cdf0e10cSrcweir     Reference<XListEntrySource> xListEntrySource(
106cdf0e10cSrcweir         lcl_findXFormsBinding( xModel, aPair.second ),
107cdf0e10cSrcweir         UNO_QUERY );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     if( xListEntrySink.is() && xListEntrySource.is() )
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         try
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir             xListEntrySink->setListEntrySource( xListEntrySource );
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir         catch( const Exception& )
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             // ignore problems during binding
118cdf0e10cSrcweir             // TODO: call XML error handling
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
bindXFormsSubmission(Reference<XModel> xModel,pair<Reference<XPropertySet>,OUString> aPair)123cdf0e10cSrcweir void bindXFormsSubmission(
124cdf0e10cSrcweir     Reference<XModel> xModel,
125cdf0e10cSrcweir     pair<Reference<XPropertySet>,OUString> aPair )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY );
128cdf0e10cSrcweir     Reference<XSubmission> xSubmission(
129cdf0e10cSrcweir         lcl_findXFormsSubmission( xModel, aPair.second ),
130cdf0e10cSrcweir         UNO_QUERY );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     if( xSubmissionSupp.is() && xSubmission.is() )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         try
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             xSubmissionSupp->setSubmission( xSubmission );
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir         catch( const Exception& )
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             // ignore problems during binding
141cdf0e10cSrcweir             // TODO: call XML error handling
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
applyXFormsSettings(const Reference<XNameAccess> & _rXForms,const Sequence<PropertyValue> & _rSettings)146cdf0e10cSrcweir void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir     OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" );
149cdf0e10cSrcweir     if ( !_rXForms.is() )
150cdf0e10cSrcweir         return;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     ::comphelper::NamedValueCollection aSettings( _rSettings );
153cdf0e10cSrcweir     Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY );
154cdf0e10cSrcweir     if ( !xModelSettings.is() )
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         OSL_ENSURE( false, "applyXFormsSettings: wrong type for the XFormModels settings!" );
157cdf0e10cSrcweir         return;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     try
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         Sequence< ::rtl::OUString > aSettingsForModels( xModelSettings->getElementNames() );
163cdf0e10cSrcweir         for (   const ::rtl::OUString* pModelName = aSettingsForModels.getConstArray();
164cdf0e10cSrcweir                 pModelName != aSettingsForModels.getConstArray() + aSettingsForModels.getLength();
165cdf0e10cSrcweir                 ++pModelName
166cdf0e10cSrcweir             )
167cdf0e10cSrcweir         {
168cdf0e10cSrcweir             // the settings for this particular model
169cdf0e10cSrcweir             Sequence< PropertyValue > aModelSettings;
170cdf0e10cSrcweir             OSL_VERIFY( xModelSettings->getByName( *pModelName ) >>= aModelSettings );
171cdf0e10cSrcweir 
172cdf0e10cSrcweir             // the model itself
173cdf0e10cSrcweir             if ( !_rXForms->hasByName( *pModelName ) )
174cdf0e10cSrcweir             {
175cdf0e10cSrcweir                 OSL_ENSURE( false, "applyXFormsSettings: have settings for a non-existent XForms model!" );
176cdf0e10cSrcweir                 continue;
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             // propagate the settings, being tolerant by omitting properties which are not supported
180cdf0e10cSrcweir             Reference< XPropertySet > xModelProps( _rXForms->getByName( *pModelName ), UNO_QUERY_THROW );
181cdf0e10cSrcweir             Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             for (   const PropertyValue* pSetting = aModelSettings.getConstArray();
184cdf0e10cSrcweir                     pSetting != aModelSettings.getConstArray() + aModelSettings.getLength();
185cdf0e10cSrcweir                     ++pSetting
186cdf0e10cSrcweir                 )
187cdf0e10cSrcweir             {
188cdf0e10cSrcweir                 if ( !xModelPSI->hasPropertyByName( pSetting->Name ) )
189cdf0e10cSrcweir                 {
190cdf0e10cSrcweir                     OSL_ENSURE( false, "applyXFormsSettings: non-existent model property!" );
191cdf0e10cSrcweir                     continue;
192cdf0e10cSrcweir                 }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir                 xModelProps->setPropertyValue( pSetting->Name, pSetting->Value );
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir     catch( const Exception& )
199cdf0e10cSrcweir     {
200cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir }
203