| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include "XFormsModelContext.hxx" |
| 22 |
#include <utility> |
| 23 |
#include <xmloff/xformsimport.hxx> |
| 24 |
#include <com/sun/star/uno/Reference.hxx> |
| 25 |
#include <com/sun/star/beans/XPropertySet.hpp> |
| 26 |
#include <com/sun/star/form/binding/XValueBinding.hpp> |
| 27 |
#include <com/sun/star/form/binding/XBindableValue.hpp> |
| 28 |
#include <com/sun/star/form/binding/XListEntrySource.hpp> |
| 29 |
#include <com/sun/star/form/binding/XListEntrySink.hpp> |
| 30 |
#include <com/sun/star/form/submission/XSubmission.hpp> |
| 31 |
#include <com/sun/star/form/submission/XSubmissionSupplier.hpp> |
| 32 |
#include <com/sun/star/container/XNameAccess.hpp> |
| 33 |
#include <rtl/ustring.hxx> |
| 34 |
#include "xformsapi.hxx" |
| 35 |
#include <comphelper/namedvaluecollection.hxx> |
| 36 |
#include <comphelper/diagnose_ex.hxx> |
| 37 |
|
| 38 |
using std::pair; |
| 39 |
using com::sun::star::uno::Reference; |
| 40 |
using com::sun::star::uno::Exception; |
| 41 |
using com::sun::star::uno::UNO_QUERY; |
| 42 |
using com::sun::star::uno::UNO_QUERY_THROW; |
| 43 |
using com::sun::star::uno::UNO_SET_THROW; |
| 44 |
using com::sun::star::uno::Sequence; |
| 45 |
using com::sun::star::beans::XPropertySet; |
| 46 |
using com::sun::star::beans::XPropertySetInfo; |
| 47 |
using com::sun::star::beans::PropertyValue; |
| 48 |
using com::sun::star::frame::XModel; |
| 49 |
using com::sun::star::container::XNameAccess; |
| 50 |
using com::sun::star::form::binding::XValueBinding; |
| 51 |
using com::sun::star::form::binding::XBindableValue; |
| 52 |
using com::sun::star::form::binding::XListEntrySource; |
| 53 |
using com::sun::star::form::binding::XListEntrySink; |
| 54 |
using com::sun::star::form::submission::XSubmission; |
| 55 |
using com::sun::star::form::submission::XSubmissionSupplier; |
| 56 |
|
| 57 |
SvXMLImportContext* createXFormsModelContext( |
| 58 |
SvXMLImport& rImport ) |
| 59 |
{ |
| 60 |
return new XFormsModelContext( rImport ); |
| 61 |
} |
| 62 |
|
| 63 |
void bindXFormsValueBinding(Reference<XModel> const& xModel, |
| 64 |
const pair<Reference<XPropertySet>, OUString>& aPair) |
| 65 |
{ |
| 66 |
Reference<XBindableValue> xBindable( |
| 67 |
aPair.first, |
| 68 |
UNO_QUERY ); |
| 69 |
Reference<XValueBinding> xBinding( |
| 70 |
xforms_findXFormsBinding( xModel, aPair.second ), |
| 71 |
UNO_QUERY ); |
| 72 |
|
| 73 |
if( xBindable.is() && xBinding.is() ) |
| 74 |
{ |
| 75 |
try |
| 76 |
{ |
| 77 |
xBindable->setValueBinding( xBinding ); |
| 78 |
} |
| 79 |
catch( const Exception& ) |
| 80 |
{ |
| 81 |
// ignore problems during binding |
| 82 |
// TODO: call XML error handling |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
void bindXFormsListBinding(Reference<XModel> const& xModel, |
| 88 |
const ::pair<Reference<XPropertySet>, OUString>& aPair) |
| 89 |
{ |
| 90 |
Reference<XListEntrySink> xListEntrySink( |
| 91 |
aPair.first, |
| 92 |
UNO_QUERY ); |
| 93 |
Reference<XListEntrySource> xListEntrySource( |
| 94 |
xforms_findXFormsBinding( xModel, aPair.second ), |
| 95 |
UNO_QUERY ); |
| 96 |
|
| 97 |
if( xListEntrySink.is() && xListEntrySource.is() ) |
| 98 |
{ |
| 99 |
try |
| 100 |
{ |
| 101 |
xListEntrySink->setListEntrySource( xListEntrySource ); |
| 102 |
} |
| 103 |
catch( const Exception& ) |
| 104 |
{ |
| 105 |
// ignore problems during binding |
| 106 |
// TODO: call XML error handling |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void bindXFormsSubmission(Reference<XModel> const& xModel, |
| 112 |
const pair<Reference<XPropertySet>, OUString>& aPair) |
| 113 |
{ |
| 114 |
Reference<XSubmissionSupplier> xSubmissionSupp( aPair.first, UNO_QUERY ); |
| 115 |
Reference<XSubmission> xSubmission( |
| 116 |
xforms_findXFormsSubmission( xModel, aPair.second ), |
| 117 |
UNO_QUERY ); |
| 118 |
|
| 119 |
if( xSubmissionSupp.is() && xSubmission.is() ) |
| 120 |
{ |
| 121 |
try |
| 122 |
{ |
| 123 |
xSubmissionSupp->setSubmission( xSubmission ); |
| 124 |
} |
| 125 |
catch( const Exception& ) |
| 126 |
{ |
| 127 |
// ignore problems during binding |
| 128 |
// TODO: call XML error handling |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void applyXFormsSettings( const Reference< XNameAccess >& _rXForms, const Sequence< PropertyValue >& _rSettings ) |
| 134 |
{ |
| 135 |
OSL_PRECOND( _rXForms.is(), "applyXFormsSettings: invalid XForms container!" ); |
| 136 |
if ( !_rXForms.is() ) |
| 137 |
return; |
| 138 |
|
| 139 |
Reference< XNameAccess > xModelSettings( ::comphelper::NamedValueCollection::get( _rSettings, u"XFormModels" ), UNO_QUERY ); |
| 140 |
if ( !xModelSettings.is() ) |
| 141 |
{ |
| 142 |
OSL_FAIL( "applyXFormsSettings: wrong type for the XFormModels settings!" ); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
try |
| 147 |
{ |
| 148 |
const Sequence< OUString > aSettingsForModels( xModelSettings->getElementNames() ); |
| 149 |
for ( auto const & modelName : aSettingsForModels ) |
| 150 |
{ |
| 151 |
// the settings for this particular model |
| 152 |
Sequence< PropertyValue > aModelSettings; |
| 153 |
OSL_VERIFY( xModelSettings->getByName( modelName ) >>= aModelSettings ); |
| 154 |
|
| 155 |
// the model itself |
| 156 |
if ( !_rXForms->hasByName( modelName ) ) |
| 157 |
{ |
| 158 |
OSL_FAIL( "applyXFormsSettings: have settings for a non-existent XForms model!" ); |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
// propagate the settings, being tolerant by omitting properties which are not supported |
| 163 |
Reference< XPropertySet > xModelProps( _rXForms->getByName( modelName ), UNO_QUERY_THROW ); |
| 164 |
Reference< XPropertySetInfo > xModelPSI( xModelProps->getPropertySetInfo(), UNO_SET_THROW ); |
| 165 |
|
| 166 |
for (auto const& setting : aModelSettings) |
| 167 |
{ |
| 168 |
if ( !xModelPSI->hasPropertyByName( setting.Name ) ) |
| 169 |
{ |
| 170 |
OSL_FAIL( "applyXFormsSettings: non-existent model property!" ); |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
xModelProps->setPropertyValue( setting.Name, setting.Value ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
catch( const Exception& ) |
| 179 |
{ |
| 180 |
DBG_UNHANDLED_EXCEPTION("xmloff"); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |