1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _MODEL_HELPER_HXX
28 #define _MODEL_HELPER_HXX
29 
30 //
31 // some helper definitions that must be available for model.cxx and
32 // model_ui.cxx
33 //
34 
35 #include "namedcollection.hxx"
36 #include "binding.hxx"
37 #include "submission.hxx"
38 #include "unohelper.hxx"
39 
40 #include <com/sun/star/uno/Reference.hxx>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 
46 namespace xforms
47 {
48     class Model;
49 }
50 
51 //
52 // BindingCollection
53 //
54 
55 namespace xforms
56 {
57 
58 class BindingCollection : public NamedCollection<com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> >
59 {
60     Model* mpModel;
61 
62 public:
63     BindingCollection( Model* pModel ) : mpModel( pModel ) {}
64     virtual ~BindingCollection() {}
65 
66     virtual bool isValid( const T& t ) const
67     {
68         return Binding::getBinding( t ) != NULL;
69     }
70 
71 protected:
72     virtual void _insert( const T& t )
73     {
74         OSL_ENSURE( Binding::getBinding( t ) != NULL, "invalid item?" );
75         Binding::getBinding( t )->_setModel( Binding::Model_t( mpModel ) );
76     }
77 
78     virtual void _remove( const T& t )
79     {
80         OSL_ENSURE( Binding::getBinding( t ) != NULL, "invalid item?" );
81         Binding::getBinding( t )->_setModel( Binding::Model_t() );
82     }
83 };
84 
85 
86 
87 //
88 // SubmissionCollection
89 //
90 
91 class SubmissionCollection : public NamedCollection<com::sun::star::uno::Reference<com::sun::star::beans::XPropertySet> >
92 {
93     Model* mpModel;
94 
95 public:
96     SubmissionCollection( Model* pModel ) : mpModel( pModel ) {}
97     virtual ~SubmissionCollection() {}
98 
99 public:
100     virtual bool isValid( const T& t ) const
101     {
102         return Submission::getSubmission( t ) != NULL;
103     }
104 
105 protected:
106     virtual void _insert( const T& t )
107     {
108         OSL_ENSURE( Submission::getSubmission( t ) != NULL, "invalid item?" );
109         Submission::getSubmission( t )->setModel( com::sun::star::uno::Reference<com::sun::star::xforms::XModel>( mpModel ) );
110     }
111 
112     virtual void _remove( const T& t )
113     {
114         OSL_ENSURE( Submission::getSubmission( t ) != NULL, "invalid item?" );
115         Submission::getSubmission( t )->setModel( com::sun::star::uno::Reference<com::sun::star::xforms::XModel>( ) );
116     }
117 };
118 
119 
120 //
121 // InstanceCollection
122 //
123 
124 class InstanceCollection : public Collection<com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue> >
125 {
126 public:
127     virtual bool isValid( const T& t ) const
128     {
129         const com::sun::star::beans::PropertyValue* pValues = t.getConstArray();
130         rtl::OUString sInstance( OUSTRING("Instance") );
131         sal_Bool bFound = sal_False;
132         for( sal_Int32 i = 0; ( ! bFound ) && ( i < t.getLength() ); i++ )
133         {
134             bFound |= ( pValues[i].Name == sInstance );
135         }
136         return bFound ? true : false;
137     }
138 };
139 
140 
141 //
142 // helper functions
143 //
144 
145 sal_Int32 lcl_findInstance( const InstanceCollection*,
146                             const rtl::OUString& );
147 
148 
149 // get values from Sequence<PropertyValue> describing an Instance
150 void getInstanceData(
151     const com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>&,
152     rtl::OUString* pID,
153     com::sun::star::uno::Reference<com::sun::star::xml::dom::XDocument>*,
154     rtl::OUString* pURL,
155     bool* pURLOnce );
156 
157 // set values on Sequence<PropertyValue> for an Instance
158 void setInstanceData(
159     com::sun::star::uno::Sequence<com::sun::star::beans::PropertyValue>&,
160     const rtl::OUString* pID,
161     const com::sun::star::uno::Reference<com::sun::star::xml::dom::XDocument>*,
162     const rtl::OUString* pURL,
163     const bool* pURLOnce );
164 
165 } // namespace xforms
166 
167 #endif
168