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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 
31 #include "XFormsModelContext.hxx"
32 #include <vector>
33 #include <utility>
34 #include "xmloff/xformsimport.hxx"
35 #include <com/sun/star/uno/Reference.hxx>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <com/sun/star/form/binding/XValueBinding.hpp>
38 #include <com/sun/star/form/binding/XBindableValue.hpp>
39 #include <com/sun/star/form/binding/XListEntrySource.hpp>
40 #include <com/sun/star/form/binding/XListEntrySink.hpp>
41 #include <com/sun/star/form/submission/XSubmission.hpp>
42 #include <com/sun/star/form/submission/XSubmissionSupplier.hpp>
43 #include <com/sun/star/container/XNameAccess.hpp>
44 #include <rtl/ustring.hxx>
45 #include <xformsapi.hxx>
46 #include <comphelper/namedvaluecollection.hxx>
47 #include <tools/diagnose_ex.h>
48 
49 using std::pair;
50 using com::sun::star::uno::Reference;
51 using com::sun::star::uno::Exception;
52 using com::sun::star::uno::UNO_QUERY;
53 using com::sun::star::uno::UNO_QUERY_THROW;
54 using com::sun::star::uno::UNO_SET_THROW;
55 using com::sun::star::uno::Sequence;
56 using com::sun::star::beans::XPropertySet;
57 using com::sun::star::beans::XPropertySetInfo;
58 using com::sun::star::beans::PropertyValue;
59 using com::sun::star::frame::XModel;
60 using com::sun::star::container::XNameAccess;
61 using com::sun::star::form::binding::XValueBinding;
62 using com::sun::star::form::binding::XBindableValue;
63 using com::sun::star::form::binding::XListEntrySource;
64 using com::sun::star::form::binding::XListEntrySink;
65 using com::sun::star::form::submission::XSubmission;
66 using com::sun::star::form::submission::XSubmissionSupplier;
67 using rtl::OUString;
68 
69 SvXMLImportContext* createXFormsModelContext(
70     SvXMLImport& rImport,
71     sal_uInt16 nPrefix,
72     const rtl::OUString& rLocalName )
73 {
74     return new XFormsModelContext( rImport, nPrefix, rLocalName );
75 }
76 
77 void bindXFormsValueBinding(
78 	Reference<XModel> xModel,
79     pair<Reference<XPropertySet>,OUString> aPair )
80 {
81     Reference<XBindableValue> xBindable(
82         aPair.first,
83         UNO_QUERY );
84     Reference<XValueBinding> xBinding(
85         lcl_findXFormsBinding( xModel, aPair.second ),
86         UNO_QUERY );
87 
88     if( xBindable.is() && xBinding.is() )
89     {
90         try
91         {
92             xBindable->setValueBinding( xBinding );
93         }
94         catch( const Exception& )
95         {
96             // ignore problems during binding
97             // TODO: call XML error handling
98         }
99     }
100 }
101 
102 void bindXFormsListBinding(
103 	Reference<XModel> xModel,
104     ::pair<Reference<XPropertySet>,OUString> aPair )
105 {
106     Reference<XListEntrySink> xListEntrySink(
107         aPair.first,
108         UNO_QUERY );
109     Reference<XListEntrySource> xListEntrySource(
110         lcl_findXFormsBinding( xModel, aPair.second ),
111         UNO_QUERY );
112 
113     if( xListEntrySink.is() && xListEntrySource.is() )
114     {
115         try
116         {
117             xListEntrySink->setListEntrySource( xListEntrySource );
118         }
119         catch( const Exception& )
120         {
121             // ignore problems during binding
122             // TODO: call XML error handling
123         }
124     }
125 }
126 
127 void bindXFormsSubmission(
128     Reference<XModel> xModel,
129     pair<Reference<XPropertySet>,OUString> aPair )
130 {
131     Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY );
132     Reference<XSubmission> xSubmission(
133         lcl_findXFormsSubmission( xModel, aPair.second ),
134         UNO_QUERY );
135 
136     if( xSubmissionSupp.is() && xSubmission.is() )
137     {
138         try
139         {
140             xSubmissionSupp->setSubmission( xSubmission );
141         }
142         catch( const Exception& )
143         {
144             // ignore problems during binding
145             // TODO: call XML error handling
146         }
147     }
148 }
149 
150 void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings )
151 {
152     OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" );
153     if ( !_rXForms.is() )
154         return;
155 
156     ::comphelper::NamedValueCollection aSettings( _rSettings );
157     Reference< XNameAccess > xModelSettings( aSettings.get( "XFormModels" ), UNO_QUERY );
158     if ( !xModelSettings.is() )
159     {
160         OSL_ENSURE( false, "applyXFormsSettings: wrong type for the XFormModels settings!" );
161         return;
162     }
163 
164     try
165     {
166         Sequence< ::rtl::OUString > aSettingsForModels( xModelSettings->getElementNames() );
167         for (   const ::rtl::OUString* pModelName = aSettingsForModels.getConstArray();
168                 pModelName != aSettingsForModels.getConstArray() + aSettingsForModels.getLength();
169                 ++pModelName
170             )
171         {
172             // the settings for this particular model
173             Sequence< PropertyValue > aModelSettings;
174             OSL_VERIFY( xModelSettings->getByName( *pModelName ) >>= aModelSettings );
175 
176             // the model itself
177             if ( !_rXForms->hasByName( *pModelName ) )
178             {
179                 OSL_ENSURE( false, "applyXFormsSettings: have settings for a non-existent XForms model!" );
180                 continue;
181             }
182 
183             // propagate the settings, being tolerant by omitting properties which are not supported
184             Reference< XPropertySet > xModelProps( _rXForms->getByName( *pModelName ), UNO_QUERY_THROW );
185             Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW );
186 
187             for (   const PropertyValue* pSetting = aModelSettings.getConstArray();
188                     pSetting != aModelSettings.getConstArray() + aModelSettings.getLength();
189                     ++pSetting
190                 )
191             {
192                 if ( !xModelPSI->hasPropertyByName( pSetting->Name ) )
193                 {
194                     OSL_ENSURE( false, "applyXFormsSettings: non-existent model property!" );
195                     continue;
196                 }
197 
198                 xModelProps->setPropertyValue( pSetting->Name, pSetting->Value );
199             }
200         }
201     }
202     catch( const Exception& )
203     {
204     	DBG_UNHANDLED_EXCEPTION();
205     }
206 }
207