1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _MODEL_HELPER_HXX
24 #define _MODEL_HELPER_HXX
25 
26 //
27 // some helper definitions that must be available for model.cxx and
28 // model_ui.cxx
29 //
30 
31 #include "namedcollection.hxx"
32 #include "binding.hxx"
33 #include "submission.hxx"
34 #include "unohelper.hxx"
35 
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <com/sun/star/lang/XUnoTunnel.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 
42 namespace xforms
43 {
44     class Model;
45 }
46 
47 //
48 // BindingCollection
49 //
50 
51 namespace xforms
52 {
53 
54 class BindingCollection : public NamedCollection<com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> >
55 {
56     Model* mpModel;
57 
58 public:
BindingCollection(Model * pModel)59     BindingCollection( Model* pModel ) : mpModel( pModel ) {}
~BindingCollection()60     virtual ~BindingCollection() {}
61 
isValid(const T & t) const62     virtual bool isValid( const T& t ) const
63     {
64         return Binding::getBinding( t ) != NULL;
65     }
66 
67 protected:
_insert(const T & t)68     virtual void _insert( const T& t )
69     {
70         OSL_ENSURE( Binding::getBinding( t ) != NULL, "invalid item?" );
71         Binding::getBinding( t )->_setModel( Binding::Model_t( mpModel ) );
72     }
73 
_remove(const T & t)74     virtual void _remove( const T& t )
75     {
76         OSL_ENSURE( Binding::getBinding( t ) != NULL, "invalid item?" );
77         Binding::getBinding( t )->_setModel( Binding::Model_t() );
78     }
79 };
80 
81 
82 
83 //
84 // SubmissionCollection
85 //
86 
87 class SubmissionCollection : public NamedCollection<com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> >
88 {
89     Model* mpModel;
90 
91 public:
SubmissionCollection(Model * pModel)92     SubmissionCollection( Model* pModel ) : mpModel( pModel ) {}
~SubmissionCollection()93     virtual ~SubmissionCollection() {}
94 
95 public:
isValid(const T & t) const96     virtual bool isValid( const T& t ) const
97     {
98         return Submission::getSubmission( t ) != NULL;
99     }
100 
101 protected:
_insert(const T & t)102     virtual void _insert( const T& t )
103     {
104         OSL_ENSURE( Submission::getSubmission( t ) != NULL, "invalid item?" );
105         Submission::getSubmission( t )->setModel( com::sun::star::uno::Reference<com::sun::star::xforms::XModel>( mpModel ) );
106     }
107 
_remove(const T & t)108     virtual void _remove( const T& t )
109     {
110         OSL_ENSURE( Submission::getSubmission( t ) != NULL, "invalid item?" );
111         Submission::getSubmission( t )->setModel( com::sun::star::uno::Reference<com::sun::star::xforms::XModel>( ) );
112     }
113 };
114 
115 
116 //
117 // InstanceCollection
118 //
119 
120 class InstanceCollection : public Collection<com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> >
121 {
122 public:
isValid(const T & t) const123     virtual bool isValid( const T& t ) const
124     {
125         const com::sun::star::beans::PropertyValue* pValues = t.getConstArray();
126         rtl::OUString sInstance( OUSTRING("Instance") );
127         sal_Bool bFound = sal_False;
128         for( sal_Int32 i = 0; ( ! bFound ) && ( i < t.getLength() ); i++ )
129         {
130             bFound |= ( pValues[i].Name == sInstance );
131         }
132         return bFound ? true : false;
133     }
134 };
135 
136 
137 //
138 // helper functions
139 //
140 
141 sal_Int32 lcl_findInstance( const InstanceCollection*,
142                             const rtl::OUString& );
143 
144 
145 // get values from Sequence<PropertyValue> describing an Instance
146 void getInstanceData(
147     const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>&,
148     rtl::OUString* pID,
149     com::sun::star::uno::Reference<com::sun::star::xml::dom::XDocument>*,
150     rtl::OUString* pURL,
151     bool* pURLOnce );
152 
153 // set values on Sequence<PropertyValue> for an Instance
154 void setInstanceData(
155     com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>&,
156     const rtl::OUString* pID,
157     const com::sun::star::uno::Reference<com::sun::star::xml::dom::XDocument>*,
158     const rtl::OUString* pURL,
159     const bool* pURLOnce );
160 
161 } // namespace xforms
162 
163 #endif
164