1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "XFormsModelContext.hxx"
32*cdf0e10cSrcweir #include <vector>
33*cdf0e10cSrcweir #include <utility>
34*cdf0e10cSrcweir #include "xmloff/xformsimport.hxx"
35*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
36*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/form/binding/XValueBinding.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/form/binding/XBindableValue.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/form/binding/XListEntrySource.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/form/binding/XListEntrySink.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/form/submission/XSubmission.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
44*cdf0e10cSrcweir #include <rtl/ustring.hxx>
45*cdf0e10cSrcweir #include <xformsapi.hxx>
46*cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
47*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir using std::pair;
50*cdf0e10cSrcweir using com::sun::star::uno::Reference;
51*cdf0e10cSrcweir using com::sun::star::uno::Exception;
52*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY;
53*cdf0e10cSrcweir using com::sun::star::uno::UNO_QUERY_THROW;
54*cdf0e10cSrcweir using com::sun::star::uno::UNO_SET_THROW;
55*cdf0e10cSrcweir using com::sun::star::uno::Sequence;
56*cdf0e10cSrcweir using com::sun::star::beans::XPropertySet;
57*cdf0e10cSrcweir using com::sun::star::beans::XPropertySetInfo;
58*cdf0e10cSrcweir using com::sun::star::beans::PropertyValue;
59*cdf0e10cSrcweir using com::sun::star::frame::XModel;
60*cdf0e10cSrcweir using com::sun::star::container::XNameAccess;
61*cdf0e10cSrcweir using com::sun::star::form::binding::XValueBinding;
62*cdf0e10cSrcweir using com::sun::star::form::binding::XBindableValue;
63*cdf0e10cSrcweir using com::sun::star::form::binding::XListEntrySource;
64*cdf0e10cSrcweir using com::sun::star::form::binding::XListEntrySink;
65*cdf0e10cSrcweir using com::sun::star::form::submission::XSubmission;
66*cdf0e10cSrcweir using com::sun::star::form::submission::XSubmissionSupplier;
67*cdf0e10cSrcweir using rtl::OUString;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir SvXMLImportContext* createXFormsModelContext(
70*cdf0e10cSrcweir     SvXMLImport& rImport,
71*cdf0e10cSrcweir     sal_uInt16 nPrefix,
72*cdf0e10cSrcweir     const rtl::OUString& rLocalName )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     return new XFormsModelContext( rImport, nPrefix, rLocalName );
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir void bindXFormsValueBinding(
78*cdf0e10cSrcweir 	Reference<XModel> xModel,
79*cdf0e10cSrcweir     pair<Reference<XPropertySet>,OUString> aPair )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     Reference<XBindableValue> xBindable(
82*cdf0e10cSrcweir         aPair.first,
83*cdf0e10cSrcweir         UNO_QUERY );
84*cdf0e10cSrcweir     Reference<XValueBinding> xBinding(
85*cdf0e10cSrcweir         lcl_findXFormsBinding( xModel, aPair.second ),
86*cdf0e10cSrcweir         UNO_QUERY );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     if( xBindable.is() && xBinding.is() )
89*cdf0e10cSrcweir     {
90*cdf0e10cSrcweir         try
91*cdf0e10cSrcweir         {
92*cdf0e10cSrcweir             xBindable->setValueBinding( xBinding );
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir         catch( const Exception& )
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             // ignore problems during binding
97*cdf0e10cSrcweir             // TODO: call XML error handling
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir void bindXFormsListBinding(
103*cdf0e10cSrcweir 	Reference<XModel> xModel,
104*cdf0e10cSrcweir     ::pair<Reference<XPropertySet>,OUString> aPair )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     Reference<XListEntrySink> xListEntrySink(
107*cdf0e10cSrcweir         aPair.first,
108*cdf0e10cSrcweir         UNO_QUERY );
109*cdf0e10cSrcweir     Reference<XListEntrySource> xListEntrySource(
110*cdf0e10cSrcweir         lcl_findXFormsBinding( xModel, aPair.second ),
111*cdf0e10cSrcweir         UNO_QUERY );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     if( xListEntrySink.is() && xListEntrySource.is() )
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         try
116*cdf0e10cSrcweir         {
117*cdf0e10cSrcweir             xListEntrySink->setListEntrySource( xListEntrySource );
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         catch( const Exception& )
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             // ignore problems during binding
122*cdf0e10cSrcweir             // TODO: call XML error handling
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void bindXFormsSubmission(
128*cdf0e10cSrcweir     Reference<XModel> xModel,
129*cdf0e10cSrcweir     pair<Reference<XPropertySet>,OUString> aPair )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir     Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY );
132*cdf0e10cSrcweir     Reference<XSubmission> xSubmission(
133*cdf0e10cSrcweir         lcl_findXFormsSubmission( xModel, aPair.second ),
134*cdf0e10cSrcweir         UNO_QUERY );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     if( xSubmissionSupp.is() && xSubmission.is() )
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         try
139*cdf0e10cSrcweir         {
140*cdf0e10cSrcweir             xSubmissionSupp->setSubmission( xSubmission );
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         catch( const Exception& )
143*cdf0e10cSrcweir         {
144*cdf0e10cSrcweir             // ignore problems during binding
145*cdf0e10cSrcweir             // TODO: call XML error handling
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings )
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" );
153*cdf0e10cSrcweir     if ( !_rXForms.is() )
154*cdf0e10cSrcweir         return;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     ::comphelper::NamedValueCollection aSettings( _rSettings );
157*cdf0e10cSrcweir     Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY );
158*cdf0e10cSrcweir     if ( !xModelSettings.is() )
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         OSL_ENSURE( false, "applyXFormsSettings: wrong type for the XFormModels settings!" );
161*cdf0e10cSrcweir         return;
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     try
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aSettingsForModels( xModelSettings->getElementNames() );
167*cdf0e10cSrcweir         for (   const ::rtl::OUString* pModelName = aSettingsForModels.getConstArray();
168*cdf0e10cSrcweir                 pModelName != aSettingsForModels.getConstArray() + aSettingsForModels.getLength();
169*cdf0e10cSrcweir                 ++pModelName
170*cdf0e10cSrcweir             )
171*cdf0e10cSrcweir         {
172*cdf0e10cSrcweir             // the settings for this particular model
173*cdf0e10cSrcweir             Sequence< PropertyValue > aModelSettings;
174*cdf0e10cSrcweir             OSL_VERIFY( xModelSettings->getByName( *pModelName ) >>= aModelSettings );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir             // the model itself
177*cdf0e10cSrcweir             if ( !_rXForms->hasByName( *pModelName ) )
178*cdf0e10cSrcweir             {
179*cdf0e10cSrcweir                 OSL_ENSURE( false, "applyXFormsSettings: have settings for a non-existent XForms model!" );
180*cdf0e10cSrcweir                 continue;
181*cdf0e10cSrcweir             }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             // propagate the settings, being tolerant by omitting properties which are not supported
184*cdf0e10cSrcweir             Reference< XPropertySet > xModelProps( _rXForms->getByName( *pModelName ), UNO_QUERY_THROW );
185*cdf0e10cSrcweir             Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir             for (   const PropertyValue* pSetting = aModelSettings.getConstArray();
188*cdf0e10cSrcweir                     pSetting != aModelSettings.getConstArray() + aModelSettings.getLength();
189*cdf0e10cSrcweir                     ++pSetting
190*cdf0e10cSrcweir                 )
191*cdf0e10cSrcweir             {
192*cdf0e10cSrcweir                 if ( !xModelPSI->hasPropertyByName( pSetting->Name ) )
193*cdf0e10cSrcweir                 {
194*cdf0e10cSrcweir                     OSL_ENSURE( false, "applyXFormsSettings: non-existent model property!" );
195*cdf0e10cSrcweir                     continue;
196*cdf0e10cSrcweir                 }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir                 xModelProps->setPropertyValue( pSetting->Name, pSetting->Value );
199*cdf0e10cSrcweir             }
200*cdf0e10cSrcweir         }
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir     catch( const Exception& )
203*cdf0e10cSrcweir     {
204*cdf0e10cSrcweir     	DBG_UNHANDLED_EXCEPTION();
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir }
207