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 <tools/diagnose_ex.h>
30 #include <com/sun/star/container/XIndexAccess.hpp>
31 
32 using namespace ::com::sun::star;
33 
34 using uno::Reference;
35 using uno::UNO_QUERY;
36 using uno::makeAny;
37 using uno::Exception;
38 using xforms::XModel2;
39 using frame::XModule;
40 using xforms::XFormsUIHelper1;
41 using com::sun::star::container::XIndexAccess;
42 
43 
44 bool SwDoc::isXForms() const
45 {
46  return mxXForms.is();
47 }
48 
49 void 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, makeAny( 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  {
94  // iterate over all models
95  uno::Sequence<OUString> aNames = mxXForms->getElementNames();
96  const OUString* pNames = aNames.getConstArray();
97  sal_Int32 nNames = aNames.getLength();
98  for( sal_Int32 n = 0; (n < nNames); n++ )
99  {
100  Reference< xforms::XModel > xModel(
101  mxXForms->getByName( pNames[n] ), UNO_QUERY );
102 
103  if( xModel.is() )
104  {
105  // ask model for bindings
106  Reference< XIndexAccess > xBindings(
107  xModel->getBindings(), UNO_QUERY );
108 
109  // Then release them one by one
110  int nCount = xBindings->getCount();
111  for( int i = nCount-1; i >= 0; i-- )
112  {
113  xModel->getBindings()->remove(xBindings->getByIndex( i ));
114  }
115 
116  // ask model for Submissions
117  Reference< XIndexAccess > xSubmissions(
118  xModel->getSubmissions(), UNO_QUERY );
119 
120  // Then release them one by one
121  nCount = xSubmissions->getCount();
122  for( int i = nCount-1; i >= 0; i-- )
123  {
124  xModel->getSubmissions()->remove(xSubmissions->getByIndex( i ));
125  }
126  }
127  }
128  }
129 }
130 
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
css::uno::Reference< css::frame::XModel > GetModel() const
css::uno::Reference< css::container::XNameContainer > mxXForms
Definition: doc.hxx:205
void initXForms(bool bCreateDefaultModel)
Definition: docxforms.cxx:49
OUString sName
int i
bool isXForms() const
Definition: docxforms.cxx:44
void disposeXForms()
Definition: docxforms.cxx:89
Reference< XComponentContext > getProcessComponentContext()
Reference< XModel > xModel
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)