xref: /trunk/main/svl/source/config/itemholder2.cxx (revision 40df464e)
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_svl.hxx"
26 
27 #include "itemholder2.hxx"
28 
29 //-----------------------------------------------
30 // includes
31 #include <comphelper/processfactory.hxx>
32 #include <com/sun/star/lang/XComponent.hpp>
33 
34 #include <svl/cjkoptions.hxx>
35 #include <svl/ctloptions.hxx>
36 #include <svl/languageoptions.hxx>
37 #include <unotools/options.hxx>
38 
39 #include <tools/debug.hxx>
40 
41 //-----------------------------------------------
42 // namespaces
43 
44 namespace css = ::com::sun::star;
45 
46 //-----------------------------------------------
47 // declarations
48 
49 //-----------------------------------------------
ItemHolder2()50 ItemHolder2::ItemHolder2()
51     : ItemHolderMutexBase()
52 {
53     try
54     {
55         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
56         css::uno::Reference< css::lang::XComponent > xCfg(
57             xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
58             css::uno::UNO_QUERY);
59         if (xCfg.is())
60             xCfg->addEventListener(static_cast< css::lang::XEventListener* >(this));
61     }
62 // #i37892  got errorhandling from   ConfigManager::GetConfigurationProvider()
63     catch(css::uno::RuntimeException& rREx)
64     {
65         throw rREx;
66     }
67 #ifdef DBG_UTIL
68     catch(css::uno::Exception& rEx)
69 	{
70         static sal_Bool bMessage = sal_True;
71         if(bMessage)
72         {
73             bMessage = sal_False;
74             ::rtl::OString sMsg("CreateInstance with arguments exception: ");
75             sMsg += ::rtl::OString(rEx.Message.getStr(),
76                         rEx.Message.getLength(),
77                         RTL_TEXTENCODING_ASCII_US);
78             DBG_ERROR(sMsg.getStr());
79         }
80 	}
81 #else
82 	catch(css::uno::Exception&){}
83 #endif
84 }
85 
86 //-----------------------------------------------
~ItemHolder2()87 ItemHolder2::~ItemHolder2()
88 {
89     impl_releaseAllItems();
90 }
91 
92 //-----------------------------------------------
holdConfigItem(EItem eItem)93 void ItemHolder2::holdConfigItem(EItem eItem)
94 {
95     static ItemHolder2* pHolder = new ItemHolder2();
96     pHolder->impl_addItem(eItem);
97 }
98 
99 //-----------------------------------------------
disposing(const css::lang::EventObject &)100 void SAL_CALL ItemHolder2::disposing(const css::lang::EventObject&)
101     throw(css::uno::RuntimeException)
102 {
103     impl_releaseAllItems();
104 }
105 
106 //-----------------------------------------------
impl_addItem(EItem eItem)107 void ItemHolder2::impl_addItem(EItem eItem)
108 {
109     ::osl::ResettableMutexGuard aLock(m_aLock);
110 
111     TItems::const_iterator pIt;
112     for (  pIt  = m_lItems.begin();
113            pIt != m_lItems.end()  ;
114          ++pIt                    )
115     {
116         const TItemInfo& rInfo = *pIt;
117         if (rInfo.eItem == eItem)
118             return;
119     }
120 
121     TItemInfo aNewItem;
122     aNewItem.eItem = eItem;
123     impl_newItem(aNewItem);
124     if (aNewItem.pItem)
125         m_lItems.push_back(aNewItem);
126 }
127 
128 //-----------------------------------------------
impl_releaseAllItems()129 void ItemHolder2::impl_releaseAllItems()
130 {
131     ::osl::ResettableMutexGuard aLock(m_aLock);
132 
133     TItems::iterator pIt;
134     for (  pIt  = m_lItems.begin();
135            pIt != m_lItems.end()  ;
136          ++pIt                    )
137     {
138         TItemInfo& rInfo = *pIt;
139         impl_deleteItem(rInfo);
140     }
141     m_lItems.clear();
142 }
143 
144 //-----------------------------------------------
impl_newItem(TItemInfo & rItem)145 void ItemHolder2::impl_newItem(TItemInfo& rItem)
146 {
147     switch(rItem.eItem)
148     {
149         case E_CJKOPTIONS :
150             rItem.pItem = new SvtCJKOptions();
151             break;
152 
153         case E_CTLOPTIONS :
154             rItem.pItem = new SvtCTLOptions();
155             break;
156 
157         case E_LANGUAGEOPTIONS :
158 // capsulate CTL and CJL options !            rItem.pItem = new SvtLanguageOptions();
159             break;
160 
161         default:
162             OSL_ASSERT(false);
163             break;
164     }
165 }
166 
167 //-----------------------------------------------
impl_deleteItem(TItemInfo & rItem)168 void ItemHolder2::impl_deleteItem(TItemInfo& rItem)
169 {
170     if (rItem.pItem)
171     {
172         delete rItem.pItem;
173         rItem.pItem = 0;
174     }
175 }
176