1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski #ifndef FORMS_MODULE_IMPLEMENTATION_INCLUDE_CONTEXT
23*b1cdbd2cSJim Jagielski     #error "not to be included directly! use 'foo_module.hxx instead'!"
24*b1cdbd2cSJim Jagielski #endif
25*b1cdbd2cSJim Jagielski 
26*b1cdbd2cSJim Jagielski #ifndef FORMS_MODULE_NAMESPACE
27*b1cdbd2cSJim Jagielski     #error "set FORMS_MODULE_NAMESPACE to your namespace identifier!"
28*b1cdbd2cSJim Jagielski #endif
29*b1cdbd2cSJim Jagielski 
30*b1cdbd2cSJim Jagielski #include <comphelper/sequence.hxx>
31*b1cdbd2cSJim Jagielski 
32*b1cdbd2cSJim Jagielski //.........................................................................
33*b1cdbd2cSJim Jagielski namespace FORMS_MODULE_NAMESPACE
34*b1cdbd2cSJim Jagielski {
35*b1cdbd2cSJim Jagielski //.........................................................................
36*b1cdbd2cSJim Jagielski 
37*b1cdbd2cSJim Jagielski 	using namespace ::com::sun::star::uno;
38*b1cdbd2cSJim Jagielski 	using namespace ::com::sun::star::lang;
39*b1cdbd2cSJim Jagielski 	using namespace ::com::sun::star::registry;
40*b1cdbd2cSJim Jagielski 	using namespace ::comphelper;
41*b1cdbd2cSJim Jagielski 	using namespace ::cppu;
42*b1cdbd2cSJim Jagielski 
43*b1cdbd2cSJim Jagielski 	//=========================================================================
44*b1cdbd2cSJim Jagielski 	//= OFormsModule
45*b1cdbd2cSJim Jagielski 	//=========================================================================
46*b1cdbd2cSJim Jagielski 
47*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------------
48*b1cdbd2cSJim Jagielski 	//- registration helper
49*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------------
50*b1cdbd2cSJim Jagielski 
51*b1cdbd2cSJim Jagielski 	Sequence< ::rtl::OUString >*				OFormsModule::s_pImplementationNames = NULL;
52*b1cdbd2cSJim Jagielski 	Sequence< Sequence< ::rtl::OUString > >*	OFormsModule::s_pSupportedServices = NULL;
53*b1cdbd2cSJim Jagielski 	Sequence< sal_Int64 >*						OFormsModule::s_pCreationFunctionPointers = NULL;
54*b1cdbd2cSJim Jagielski 	Sequence< sal_Int64 >*						OFormsModule::s_pFactoryFunctionPointers = NULL;
55*b1cdbd2cSJim Jagielski 
56*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------------
registerComponent(const::rtl::OUString & _rImplementationName,const Sequence<::rtl::OUString> & _rServiceNames,ComponentInstantiation _pCreateFunction,FactoryInstantiation _pFactoryFunction)57*b1cdbd2cSJim Jagielski 	void OFormsModule::registerComponent(
58*b1cdbd2cSJim Jagielski 		const ::rtl::OUString& _rImplementationName,
59*b1cdbd2cSJim Jagielski 		const Sequence< ::rtl::OUString >& _rServiceNames,
60*b1cdbd2cSJim Jagielski 		ComponentInstantiation _pCreateFunction,
61*b1cdbd2cSJim Jagielski 		FactoryInstantiation _pFactoryFunction)
62*b1cdbd2cSJim Jagielski 	{
63*b1cdbd2cSJim Jagielski 		if (!s_pImplementationNames)
64*b1cdbd2cSJim Jagielski 		{
65*b1cdbd2cSJim Jagielski 			OSL_ENSURE(!s_pSupportedServices && !s_pCreationFunctionPointers && !s_pFactoryFunctionPointers,
66*b1cdbd2cSJim Jagielski 				"OFormsModule::registerComponent : inconsistent state (the pointers (1)) !");
67*b1cdbd2cSJim Jagielski 			s_pImplementationNames = new Sequence< ::rtl::OUString >;
68*b1cdbd2cSJim Jagielski 			s_pSupportedServices = new Sequence< Sequence< ::rtl::OUString > >;
69*b1cdbd2cSJim Jagielski 			s_pCreationFunctionPointers = new Sequence< sal_Int64 >;
70*b1cdbd2cSJim Jagielski 			s_pFactoryFunctionPointers = new Sequence< sal_Int64 >;
71*b1cdbd2cSJim Jagielski 		}
72*b1cdbd2cSJim Jagielski 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
73*b1cdbd2cSJim Jagielski 			"OFormsModule::registerComponent : inconsistent state (the pointers (2)) !");
74*b1cdbd2cSJim Jagielski 
75*b1cdbd2cSJim Jagielski 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
76*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
77*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
78*b1cdbd2cSJim Jagielski 			"OFormsModule::registerComponent : inconsistent state !");
79*b1cdbd2cSJim Jagielski 
80*b1cdbd2cSJim Jagielski 		sal_Int32 nOldLen = s_pImplementationNames->getLength();
81*b1cdbd2cSJim Jagielski 		s_pImplementationNames->realloc(nOldLen + 1);
82*b1cdbd2cSJim Jagielski 		s_pSupportedServices->realloc(nOldLen + 1);
83*b1cdbd2cSJim Jagielski 		s_pCreationFunctionPointers->realloc(nOldLen + 1);
84*b1cdbd2cSJim Jagielski 		s_pFactoryFunctionPointers->realloc(nOldLen + 1);
85*b1cdbd2cSJim Jagielski 
86*b1cdbd2cSJim Jagielski 		s_pImplementationNames->getArray()[nOldLen] = _rImplementationName;
87*b1cdbd2cSJim Jagielski 		s_pSupportedServices->getArray()[nOldLen] = _rServiceNames;
88*b1cdbd2cSJim Jagielski 		s_pCreationFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pCreateFunction);
89*b1cdbd2cSJim Jagielski 		s_pFactoryFunctionPointers->getArray()[nOldLen] = reinterpret_cast<sal_Int64>(_pFactoryFunction);
90*b1cdbd2cSJim Jagielski 	}
91*b1cdbd2cSJim Jagielski 
92*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------------
revokeComponent(const::rtl::OUString & _rImplementationName)93*b1cdbd2cSJim Jagielski 	void OFormsModule::revokeComponent(const ::rtl::OUString& _rImplementationName)
94*b1cdbd2cSJim Jagielski 	{
95*b1cdbd2cSJim Jagielski 		if (!s_pImplementationNames)
96*b1cdbd2cSJim Jagielski 		{
97*b1cdbd2cSJim Jagielski 			OSL_ASSERT("OFormsModule::revokeComponent : have no class infos ! Are you sure called this method at the right time ?");
98*b1cdbd2cSJim Jagielski 			return;
99*b1cdbd2cSJim Jagielski 		}
100*b1cdbd2cSJim Jagielski 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
101*b1cdbd2cSJim Jagielski 			"OFormsModule::revokeComponent : inconsistent state (the pointers) !");
102*b1cdbd2cSJim Jagielski 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
103*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
104*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
105*b1cdbd2cSJim Jagielski 			"OFormsModule::revokeComponent : inconsistent state !");
106*b1cdbd2cSJim Jagielski 
107*b1cdbd2cSJim Jagielski 		sal_Int32 nLen = s_pImplementationNames->getLength();
108*b1cdbd2cSJim Jagielski 		const ::rtl::OUString* pImplNames = s_pImplementationNames->getConstArray();
109*b1cdbd2cSJim Jagielski 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplNames)
110*b1cdbd2cSJim Jagielski 		{
111*b1cdbd2cSJim Jagielski 			if (pImplNames->equals(_rImplementationName))
112*b1cdbd2cSJim Jagielski 			{
113*b1cdbd2cSJim Jagielski 				removeElementAt(*s_pImplementationNames, i);
114*b1cdbd2cSJim Jagielski 				removeElementAt(*s_pSupportedServices, i);
115*b1cdbd2cSJim Jagielski 				removeElementAt(*s_pCreationFunctionPointers, i);
116*b1cdbd2cSJim Jagielski 				removeElementAt(*s_pFactoryFunctionPointers, i);
117*b1cdbd2cSJim Jagielski 				break;
118*b1cdbd2cSJim Jagielski 			}
119*b1cdbd2cSJim Jagielski 		}
120*b1cdbd2cSJim Jagielski 
121*b1cdbd2cSJim Jagielski 		if (s_pImplementationNames->getLength() == 0)
122*b1cdbd2cSJim Jagielski 		{
123*b1cdbd2cSJim Jagielski 			delete s_pImplementationNames; s_pImplementationNames = NULL;
124*b1cdbd2cSJim Jagielski 			delete s_pSupportedServices; s_pSupportedServices = NULL;
125*b1cdbd2cSJim Jagielski 			delete s_pCreationFunctionPointers; s_pCreationFunctionPointers = NULL;
126*b1cdbd2cSJim Jagielski 			delete s_pFactoryFunctionPointers; s_pFactoryFunctionPointers = NULL;
127*b1cdbd2cSJim Jagielski 		}
128*b1cdbd2cSJim Jagielski 	}
129*b1cdbd2cSJim Jagielski 
130*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const Reference<XMultiServiceFactory> & _rxServiceManager)131*b1cdbd2cSJim Jagielski 	Reference< XInterface > OFormsModule::getComponentFactory(
132*b1cdbd2cSJim Jagielski 		const ::rtl::OUString& _rImplementationName,
133*b1cdbd2cSJim Jagielski 		const Reference< XMultiServiceFactory >& _rxServiceManager)
134*b1cdbd2cSJim Jagielski 	{
135*b1cdbd2cSJim Jagielski 		OSL_ENSURE(_rxServiceManager.is(), "OFormsModule::getComponentFactory : invalid argument (service manager) !");
136*b1cdbd2cSJim Jagielski 		OSL_ENSURE(_rImplementationName.getLength(), "OFormsModule::getComponentFactory : invalid argument (implementation name) !");
137*b1cdbd2cSJim Jagielski 
138*b1cdbd2cSJim Jagielski 		if (!s_pImplementationNames)
139*b1cdbd2cSJim Jagielski 		{
140*b1cdbd2cSJim Jagielski 			OSL_ASSERT("OFormsModule::getComponentFactory : have no class infos ! Are you sure called this method at the right time ?");
141*b1cdbd2cSJim Jagielski 			return NULL;
142*b1cdbd2cSJim Jagielski 		}
143*b1cdbd2cSJim Jagielski 		OSL_ENSURE(s_pImplementationNames && s_pSupportedServices && s_pCreationFunctionPointers && s_pFactoryFunctionPointers,
144*b1cdbd2cSJim Jagielski 			"OFormsModule::getComponentFactory : inconsistent state (the pointers) !");
145*b1cdbd2cSJim Jagielski 		OSL_ENSURE(	(s_pImplementationNames->getLength() == s_pSupportedServices->getLength())
146*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pCreationFunctionPointers->getLength())
147*b1cdbd2cSJim Jagielski 					&&	(s_pImplementationNames->getLength() == s_pFactoryFunctionPointers->getLength()),
148*b1cdbd2cSJim Jagielski 			"OFormsModule::getComponentFactory : inconsistent state !");
149*b1cdbd2cSJim Jagielski 
150*b1cdbd2cSJim Jagielski 
151*b1cdbd2cSJim Jagielski 		Reference< XInterface > xReturn;
152*b1cdbd2cSJim Jagielski 
153*b1cdbd2cSJim Jagielski 
154*b1cdbd2cSJim Jagielski 		sal_Int32 nLen = s_pImplementationNames->getLength();
155*b1cdbd2cSJim Jagielski 		const ::rtl::OUString* pImplName = s_pImplementationNames->getConstArray();
156*b1cdbd2cSJim Jagielski 		const Sequence< ::rtl::OUString >* pServices = s_pSupportedServices->getConstArray();
157*b1cdbd2cSJim Jagielski 		const sal_Int64* pComponentFunction = s_pCreationFunctionPointers->getConstArray();
158*b1cdbd2cSJim Jagielski 		const sal_Int64* pFactoryFunction = s_pFactoryFunctionPointers->getConstArray();
159*b1cdbd2cSJim Jagielski 
160*b1cdbd2cSJim Jagielski 		for (sal_Int32 i=0; i<nLen; ++i, ++pImplName, ++pServices, ++pComponentFunction, ++pFactoryFunction)
161*b1cdbd2cSJim Jagielski 		{
162*b1cdbd2cSJim Jagielski 			if (pImplName->equals(_rImplementationName))
163*b1cdbd2cSJim Jagielski 			{
164*b1cdbd2cSJim Jagielski 				const FactoryInstantiation FactoryInstantiationFunction = reinterpret_cast<const FactoryInstantiation>(*pFactoryFunction);
165*b1cdbd2cSJim Jagielski 				const ComponentInstantiation ComponentInstantiationFunction = reinterpret_cast<const ComponentInstantiation>(*pComponentFunction);
166*b1cdbd2cSJim Jagielski 
167*b1cdbd2cSJim Jagielski 				xReturn = FactoryInstantiationFunction( _rxServiceManager, *pImplName, ComponentInstantiationFunction, *pServices, NULL);
168*b1cdbd2cSJim Jagielski 				if (xReturn.is())
169*b1cdbd2cSJim Jagielski 				{
170*b1cdbd2cSJim Jagielski 					xReturn->acquire();
171*b1cdbd2cSJim Jagielski 					return xReturn.get();
172*b1cdbd2cSJim Jagielski 				}
173*b1cdbd2cSJim Jagielski 			}
174*b1cdbd2cSJim Jagielski 		}
175*b1cdbd2cSJim Jagielski 
176*b1cdbd2cSJim Jagielski 		return NULL;
177*b1cdbd2cSJim Jagielski 	}
178*b1cdbd2cSJim Jagielski 
179*b1cdbd2cSJim Jagielski //.........................................................................
180*b1cdbd2cSJim Jagielski }   // namespace FORMS_MODULE_NAMESPACE
181*b1cdbd2cSJim Jagielski //.........................................................................
182*b1cdbd2cSJim Jagielski 
183