1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 #include <comphelper/processfactory.hxx>
29 #include <osl/diagnose.h>
30 #include <com/sun/star/container/XIndexAccess.hpp>
32 using namespace ::com::sun::star
;
38 using xforms::XModel2
;
40 using xforms::XFormsUIHelper1
;
41 using com::sun::star::container::XIndexAccess
;
44 bool SwDoc::isXForms() const
49 void SwDoc::initXForms( bool bCreateDefaultModel
)
51 OSL_ENSURE( ! isXForms(), "please initialize only once" );
55 // create XForms components
56 mxXForms
= xforms::XForms::create( comphelper::getProcessComponentContext() );
58 // change our module identifier, to be able to have a dedicated UI
59 Reference
< XModule
> xModule
;
60 SwDocShell
* pShell( GetDocShell() );
62 xModule
.set(pShell
->GetModel(), css::uno::UNO_QUERY
);
63 OSL_ENSURE( xModule
.is(), "SwDoc::initXForms: no XModule at the document!" );
65 xModule
->setIdentifier( "com.sun.star.xforms.XMLFormDocument" );
67 // create default model
68 if( bCreateDefaultModel
&& mxXForms
.is() )
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(
77 mxXForms
->insertByName( sName
, Any( xModel
) );
78 OSL_ENSURE( mxXForms
->hasElements(), "can't create XForms model" );
81 OSL_ENSURE( isXForms(), "initialization failed" );
83 catch( const Exception
& )
88 // #i113606#, to release the cyclic reference between XFormModel and bindings/submissions.
89 void SwDoc::disposeXForms( )
95 // iterate over all models
96 const uno::Sequence
<OUString
> aNames
= mxXForms
->getElementNames();
97 for( const OUString
& rName
: aNames
)
99 Reference
< xforms::XModel
> xModel(
100 mxXForms
->getByName( rName
), UNO_QUERY
);
104 // ask model for bindings
105 Reference
< XIndexAccess
> xBindings(
106 xModel
->getBindings(), UNO_QUERY
);
108 // Then release them one by one
109 int nCount
= xBindings
->getCount();
110 for( int i
= nCount
-1; i
>= 0; i
-- )
112 xModel
->getBindings()->remove(xBindings
->getByIndex( i
));
115 // ask model for Submissions
116 Reference
< XIndexAccess
> xSubmissions(
117 xModel
->getSubmissions(), UNO_QUERY
);
119 // Then release them one by one
120 nCount
= xSubmissions
->getCount();
121 for( int i
= nCount
-1; i
>= 0; i
-- )
123 xModel
->getSubmissions()->remove(xSubmissions
->getByIndex( i
));
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */