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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_unotools.hxx"
26 #include <unotools/componentresmodule.hxx>
27 
28 /** === begin UNO includes === **/
29 /** === end UNO includes === **/
30 #include <tools/resmgr.hxx>
31 #include <osl/diagnose.h>
32 
33 //........................................................................
34 namespace utl
35 {
36 //........................................................................
37 
38 	//====================================================================
39 	//= OComponentResModuleImpl
40 	//====================================================================
41     /** PIMPL-class for OComponentResourceModule
42 
43         not threadsafe!
44     */
45     class OComponentResModuleImpl
46     {
47     private:
48 		ResMgr*		    m_pRessources;
49 		bool            m_bInitialized;
50         ::rtl::OString  m_sResFilePrefix;
51 
52     public:
OComponentResModuleImpl(const::rtl::OString & _rResFilePrefix)53         OComponentResModuleImpl( const ::rtl::OString& _rResFilePrefix )
54             :m_pRessources( NULL )
55             ,m_bInitialized( false )
56             ,m_sResFilePrefix( _rResFilePrefix )
57         {
58         }
59 
~OComponentResModuleImpl()60         ~OComponentResModuleImpl()
61         {
62             freeResManager();
63         }
64 
65         /** releases our resource manager
66         */
67         void freeResManager();
68 
69         /** retrieves our resource manager
70         */
71         ResMgr*	getResManager();
72 
73     private:
74         OComponentResModuleImpl();                                              // never implemented
75         OComponentResModuleImpl( const OComponentResModuleImpl& );              // never implemented
76         OComponentResModuleImpl& operator=( const OComponentResModuleImpl& );   // never implemented
77     };
78 
79 	//--------------------------------------------------------------------
freeResManager()80     void OComponentResModuleImpl::freeResManager()
81     {
82 		delete m_pRessources, m_pRessources = NULL;
83         m_bInitialized = false;
84     }
85 
86 	//--------------------------------------------------------------------
getResManager()87     ResMgr*	OComponentResModuleImpl::getResManager()
88     {
89 		if ( !m_pRessources && !m_bInitialized )
90 		{
91 			// create a manager with a fixed prefix
92 			ByteString aMgrName = m_sResFilePrefix;
93 
94 			m_pRessources = ResMgr::CreateResMgr( aMgrName.GetBuffer() );
95 			OSL_ENSURE( m_pRessources,
96 					( ByteString( "OModuleImpl::getResManager: could not create the resource manager (file name: " )
97 				+=	aMgrName
98 				+=	ByteString( ")!" ) ).GetBuffer() );
99 
100 			m_bInitialized = sal_True;
101 		}
102 		return m_pRessources;
103     }
104 
105 	//====================================================================
106 	//= OComponentResourceModule
107 	//====================================================================
108 	//--------------------------------------------------------------------
OComponentResourceModule(const::rtl::OString & _rResFilePrefix)109     OComponentResourceModule::OComponentResourceModule( const ::rtl::OString& _rResFilePrefix )
110         :BaseClass()
111         ,m_pImpl( new OComponentResModuleImpl( _rResFilePrefix ) )
112     {
113     }
114 
115 	//--------------------------------------------------------------------
~OComponentResourceModule()116     OComponentResourceModule::~OComponentResourceModule()
117     {
118     }
119 
120 	//-------------------------------------------------------------------------
getResManager()121 	ResMgr*	OComponentResourceModule::getResManager()
122 	{
123         ::osl::MutexGuard aGuard( m_aMutex );
124 		return m_pImpl->getResManager();
125 	}
126 
127 	//--------------------------------------------------------------------------
onFirstClient()128     void OComponentResourceModule::onFirstClient()
129     {
130         BaseClass::onFirstClient();
131     }
132 
133 	//--------------------------------------------------------------------------
onLastClient()134     void OComponentResourceModule::onLastClient()
135     {
136         m_pImpl->freeResManager();
137         BaseClass::onLastClient();
138     }
139 
140 //........................................................................
141 } // namespace utl
142 //........................................................................
143