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 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include "itemholder2.hxx"
32 
33 //-----------------------------------------------
34 // includes
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/lang/XComponent.hpp>
37 
38 #include <svtools/accessibilityoptions.hxx>
39 #include <svtools/apearcfg.hxx>
40 #include <svtools/menuoptions.hxx>
41 #include <svtools/colorcfg.hxx>
42 #include <svtools/fontsubstconfig.hxx>
43 #include <svtools/helpopt.hxx>
44 #include <svtools/printoptions.hxx>
45 #include <unotools/options.hxx>
46 #include <svtools/miscopt.hxx>
47 
48 
49 #include <tools/debug.hxx>
50 
51 //-----------------------------------------------
52 // namespaces
53 
54 namespace css = ::com::sun::star;
55 
56 //-----------------------------------------------
57 // declarations
58 
59 //-----------------------------------------------
60 ItemHolder2::ItemHolder2()
61     : ItemHolderMutexBase()
62 {
63     try
64     {
65         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = ::comphelper::getProcessServiceFactory();
66         css::uno::Reference< css::lang::XComponent > xCfg(
67             xSMGR->createInstance(::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider")),
68             css::uno::UNO_QUERY);
69         if (xCfg.is())
70             xCfg->addEventListener(static_cast< css::lang::XEventListener* >(this));
71     }
72 // #i37892  got errorhandling from   ConfigManager::GetConfigurationProvider()
73     catch(css::uno::RuntimeException& rREx)
74     {
75         throw rREx;
76     }
77 #ifdef DBG_UTIL
78     catch(css::uno::Exception& rEx)
79 	{
80         static sal_Bool bMessage = sal_True;
81         if(bMessage)
82         {
83             bMessage = sal_False;
84             ::rtl::OString sMsg("CreateInstance with arguments exception: ");
85             sMsg += ::rtl::OString(rEx.Message.getStr(),
86                         rEx.Message.getLength(),
87                         RTL_TEXTENCODING_ASCII_US);
88             DBG_ERROR(sMsg.getStr());
89         }
90 	}
91 #else
92 	catch(css::uno::Exception&){}
93 #endif
94 }
95 
96 //-----------------------------------------------
97 ItemHolder2::~ItemHolder2()
98 {
99     impl_releaseAllItems();
100 }
101 
102 //-----------------------------------------------
103 void ItemHolder2::holdConfigItem(EItem eItem)
104 {
105     static ItemHolder2* pHolder = new ItemHolder2();
106     pHolder->impl_addItem(eItem);
107 }
108 
109 //-----------------------------------------------
110 void SAL_CALL ItemHolder2::disposing(const css::lang::EventObject&)
111     throw(css::uno::RuntimeException)
112 {
113     impl_releaseAllItems();
114 }
115 
116 //-----------------------------------------------
117 void ItemHolder2::impl_addItem(EItem eItem)
118 {
119     ::osl::ResettableMutexGuard aLock(m_aLock);
120 
121     TItems::const_iterator pIt;
122     for (  pIt  = m_lItems.begin();
123            pIt != m_lItems.end()  ;
124          ++pIt                    )
125     {
126         const TItemInfo& rInfo = *pIt;
127         if (rInfo.eItem == eItem)
128             return;
129     }
130 
131     TItemInfo aNewItem;
132     aNewItem.eItem = eItem;
133     impl_newItem(aNewItem);
134     if (aNewItem.pItem)
135         m_lItems.push_back(aNewItem);
136 }
137 
138 //-----------------------------------------------
139 void ItemHolder2::impl_releaseAllItems()
140 {
141     ::osl::ResettableMutexGuard aLock(m_aLock);
142 
143     TItems::iterator pIt;
144     for (  pIt  = m_lItems.begin();
145            pIt != m_lItems.end()  ;
146          ++pIt                    )
147     {
148         TItemInfo& rInfo = *pIt;
149         impl_deleteItem(rInfo);
150     }
151     m_lItems.clear();
152 }
153 
154 //-----------------------------------------------
155 void ItemHolder2::impl_newItem(TItemInfo& rItem)
156 {
157     switch(rItem.eItem)
158     {
159         case E_ACCESSIBILITYOPTIONS :
160             rItem.pItem = new SvtAccessibilityOptions();
161             break;
162 
163         case E_APEARCFG :
164 // no ref count            rItem.pItem = new SvtTabAppearanceCfg();
165             break;
166 
167         case E_COLORCFG :
168             rItem.pItem = new ::svtools::ColorConfig();
169             break;
170 
171         case E_FONTSUBSTCONFIG :
172 // no ref count            rItem.pItem = new SvtFontSubstConfig();
173             break;
174 
175         case E_HELPOPTIONS :
176             rItem.pItem = new SvtHelpOptions();
177             break;
178 
179         case E_MENUOPTIONS :
180             rItem.pItem = new SvtMenuOptions();
181             break;
182 
183         case E_PRINTOPTIONS :
184             rItem.pItem = new SvtPrinterOptions();
185             break;
186 
187         case E_PRINTFILEOPTIONS :
188             rItem.pItem = new SvtPrintFileOptions();
189             break;
190 
191         case E_MISCOPTIONS :
192             rItem.pItem = new SvtMiscOptions();
193             break;
194 
195         default:
196             OSL_ASSERT(false);
197             break;
198     }
199 }
200 
201 //-----------------------------------------------
202 void ItemHolder2::impl_deleteItem(TItemInfo& rItem)
203 {
204     if (rItem.pItem)
205     {
206         delete rItem.pItem;
207         rItem.pItem = 0;
208     }
209 }
210