LibreOffice Module sw (master) 1
docxforms.cxx
Go to the documentation of this file.
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#include <doc.hxx>
21#include <docsh.hxx>
22#include <com/sun/star/uno/Reference.hxx>
23#include <com/sun/star/frame/XModule.hpp>
24#include <com/sun/star/xforms/Model.hpp>
25#include <com/sun/star/xforms/XModel2.hpp>
26#include <com/sun/star/xforms/XFormsUIHelper1.hpp>
27#include <com/sun/star/xforms/XForms.hpp>
29#include <osl/diagnose.h>
30#include <com/sun/star/container/XIndexAccess.hpp>
31
32using namespace ::com::sun::star;
33
34using uno::Reference;
35using uno::UNO_QUERY;
36using uno::Any;
37using uno::Exception;
38using xforms::XModel2;
39using frame::XModule;
40using xforms::XFormsUIHelper1;
41using com::sun::star::container::XIndexAccess;
42
43
44bool SwDoc::isXForms() const
45{
46 return mxXForms.is();
47}
48
49void SwDoc::initXForms( bool bCreateDefaultModel )
50{
51 OSL_ENSURE( ! isXForms(), "please initialize only once" );
52
53 try
54 {
55 // create XForms components
56 mxXForms = xforms::XForms::create( comphelper::getProcessComponentContext() );
57
58 // change our module identifier, to be able to have a dedicated UI
59 Reference< XModule > xModule;
60 SwDocShell* pShell( GetDocShell() );
61 if ( pShell )
62 xModule.set(pShell->GetModel(), css::uno::UNO_QUERY);
63 OSL_ENSURE( xModule.is(), "SwDoc::initXForms: no XModule at the document!" );
64 if ( xModule.is() )
65 xModule->setIdentifier( "com.sun.star.xforms.XMLFormDocument" );
66
67 // create default model
68 if( bCreateDefaultModel && mxXForms.is() )
69 {
70 OUString sName("Model 1");
71 Reference<XModel2> xModel = xforms::Model::create( comphelper::getProcessComponentContext() );
72 xModel->setID( sName );
73 Reference<XFormsUIHelper1>( xModel, uno::UNO_QUERY_THROW )->newInstance(
74 "Instance 1",
75 OUString(), true );
76 xModel->initialize();
77 mxXForms->insertByName( sName, Any( xModel ) );
78 OSL_ENSURE( mxXForms->hasElements(), "can't create XForms model" );
79 }
80
81 OSL_ENSURE( isXForms(), "initialization failed" );
82 }
83 catch( const Exception& )
84 {
85 }
86}
87
88// #i113606#, to release the cyclic reference between XFormModel and bindings/submissions.
90{
91 // get XForms models
92 if( !mxXForms.is() )
93 return;
94
95 // iterate over all models
96 const uno::Sequence<OUString> aNames = mxXForms->getElementNames();
97 for( const OUString& rName : aNames )
98 {
99 Reference< xforms::XModel > xModel(
100 mxXForms->getByName( rName ), UNO_QUERY );
101
102 if( xModel.is() )
103 {
104 // ask model for bindings
105 Reference< XIndexAccess > xBindings(
106 xModel->getBindings(), UNO_QUERY );
107
108 // Then release them one by one
109 int nCount = xBindings->getCount();
110 for( int i = nCount-1; i >= 0; i-- )
111 {
112 xModel->getBindings()->remove(xBindings->getByIndex( i ));
113 }
114
115 // ask model for Submissions
116 Reference< XIndexAccess > xSubmissions(
117 xModel->getSubmissions(), UNO_QUERY );
118
119 // Then release them one by one
120 nCount = xSubmissions->getCount();
121 for( int i = nCount-1; i >= 0; i-- )
122 {
123 xModel->getSubmissions()->remove(xSubmissions->getByIndex( i ));
124 }
125 }
126 }
127}
128
129/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XModel3 > GetModel() const
css::uno::Reference< css::container::XNameContainer > mxXForms
Definition: doc.hxx:216
void initXForms(bool bCreateDefaultModel)
Definition: docxforms.cxx:49
bool isXForms() const
Definition: docxforms.cxx:44
void disposeXForms()
Definition: docxforms.cxx:89
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
int nCount
OUString sName
@ Exception
Reference< XComponentContext > getProcessComponentContext()
int i
Reference< XModel > xModel