xref: /AOO41X/main/framework/source/accelerators/presethandler.cxx (revision 6d739b60ff8f4ed2134ae1442e284f9da90334b4)
1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir #include <accelerators/presethandler.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_______________________________________________
29cdf0e10cSrcweir // own includes
30cdf0e10cSrcweir #include <classes/fwkresid.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "classes/resource.hrc"
33cdf0e10cSrcweir #include <threadhelp/readguard.hxx>
34cdf0e10cSrcweir #include <threadhelp/writeguard.hxx>
35cdf0e10cSrcweir #include <services.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //_______________________________________________
38cdf0e10cSrcweir // interface includes
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #ifndef __COM_SUN_STAR_CONFIGURATION_CORRUPTEDUICONFIGURATIONEXCEPTION_HPP_
41cdf0e10cSrcweir #include <com/sun/star/configuration/CorruptedUIConfigurationException.hpp>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #ifndef __COM_SUN_STAR_CONTAINER_NOSUCHELEMENTEXCEPTION_HPP_
45cdf0e10cSrcweir #include <com/sun/star/container/NoSuchElementException.hpp>
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir #ifndef __COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
49cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #ifndef __COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
53cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
54cdf0e10cSrcweir #endif
55cdf0e10cSrcweir 
56cdf0e10cSrcweir #ifndef __COM_SUN_STAR_EMBED_ELEMENTMODES_HPP_
57cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #ifndef __COM_SUN_STAR_EMBED_XTRANSACTEDOBJECT_HPP_
61cdf0e10cSrcweir #include <com/sun/star/embed/XTransactedObject.hpp>
62cdf0e10cSrcweir #endif
63cdf0e10cSrcweir 
64cdf0e10cSrcweir #ifndef __COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
65cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
66cdf0e10cSrcweir #endif
67cdf0e10cSrcweir 
68cdf0e10cSrcweir //_______________________________________________
69cdf0e10cSrcweir // other includes
70cdf0e10cSrcweir #include <vcl/svapp.hxx>
71cdf0e10cSrcweir 
72cdf0e10cSrcweir #ifndef _RTL_USTRBUF_HXX
73cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
74cdf0e10cSrcweir #endif
75cdf0e10cSrcweir 
76cdf0e10cSrcweir //_______________________________________________
77cdf0e10cSrcweir // const
78cdf0e10cSrcweir 
79cdf0e10cSrcweir #define SUBSTORAGE_GLOBAL       DECLARE_ASCII("global" )
80cdf0e10cSrcweir #define SUBSTORAGE_MODULES      DECLARE_ASCII("modules")
81cdf0e10cSrcweir 
82cdf0e10cSrcweir #define BASEPATH_SHARE_LAYER    DECLARE_ASCII("UIConfig"  )
83cdf0e10cSrcweir #define BASEPATH_USER_LAYER     DECLARE_ASCII("UserConfig")
84cdf0e10cSrcweir 
85cdf0e10cSrcweir #define RELPATH_SHARE_LAYER     DECLARE_ASCII("soffice.cfg")
86cdf0e10cSrcweir #define RELPATH_USER_LAYER      DECLARE_ASCII("soffice.cfg")
87cdf0e10cSrcweir // #define RELPATH_SHARE_LAYER     DECLARE_ASCII("soffice.cfg/uiconfig.zip")
88cdf0e10cSrcweir // #define RELPATH_USER_LAYER      DECLARE_ASCII("soffice.cfg/uiconfig.zip")
89cdf0e10cSrcweir 
90cdf0e10cSrcweir #define FILE_EXTENSION          DECLARE_ASCII(".xml")
91cdf0e10cSrcweir 
92cdf0e10cSrcweir #define PATH_SEPERATOR          DECLARE_ASCII("/")
93cdf0e10cSrcweir 
94cdf0e10cSrcweir static const ::sal_Int32 ID_CORRUPT_UICONFIG_SHARE   = 1;
95cdf0e10cSrcweir static const ::sal_Int32 ID_CORRUPT_UICONFIG_USER    = 2;
96cdf0e10cSrcweir static const ::sal_Int32 ID_CORRUPT_UICONFIG_GENERAL = 3;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //_______________________________________________
99cdf0e10cSrcweir // namespace
100cdf0e10cSrcweir 
101cdf0e10cSrcweir namespace framework
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //-----------------------------------------------
PRESET_DEFAULT()105cdf0e10cSrcweir ::rtl::OUString PresetHandler::PRESET_DEFAULT()
106cdf0e10cSrcweir {
107cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("default");
108cdf0e10cSrcweir     return RSTYPE;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir //-----------------------------------------------
TARGET_CURRENT()112cdf0e10cSrcweir ::rtl::OUString PresetHandler::TARGET_CURRENT()
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("current");
115cdf0e10cSrcweir     return RSTYPE;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir //-----------------------------------------------
RESOURCETYPE_MENUBAR()119cdf0e10cSrcweir ::rtl::OUString PresetHandler::RESOURCETYPE_MENUBAR()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("menubar");
122cdf0e10cSrcweir     return RSTYPE;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir //-----------------------------------------------
RESOURCETYPE_TOOLBAR()126cdf0e10cSrcweir ::rtl::OUString PresetHandler::RESOURCETYPE_TOOLBAR()
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("toolbar");
129cdf0e10cSrcweir     return RSTYPE;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir //-----------------------------------------------
RESOURCETYPE_ACCELERATOR()133cdf0e10cSrcweir ::rtl::OUString PresetHandler::RESOURCETYPE_ACCELERATOR()
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("accelerator");
136cdf0e10cSrcweir     return RSTYPE;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir //-----------------------------------------------
RESOURCETYPE_STATUSBAR()140cdf0e10cSrcweir ::rtl::OUString PresetHandler::RESOURCETYPE_STATUSBAR()
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     static ::rtl::OUString RSTYPE = DECLARE_ASCII("statusbar");
143cdf0e10cSrcweir     return RSTYPE;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir //-----------------------------------------------
PresetHandler(const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR)147cdf0e10cSrcweir PresetHandler::PresetHandler(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
148cdf0e10cSrcweir     : ThreadHelpBase     (&Application::GetSolarMutex()        )
149cdf0e10cSrcweir     , m_xSMGR            (xSMGR                                )
150cdf0e10cSrcweir     , m_aSharedStorages  (                                     )
151cdf0e10cSrcweir     , m_lDocumentStorages(xSMGR                                )
152cdf0e10cSrcweir     , m_aLocale          (::comphelper::Locale::X_NOTRANSLATE())
153cdf0e10cSrcweir {
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir //-----------------------------------------------
PresetHandler(const PresetHandler & rCopy)157cdf0e10cSrcweir PresetHandler::PresetHandler(const PresetHandler& rCopy)
158cdf0e10cSrcweir     : ThreadHelpBase     (&Application::GetSolarMutex()        )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     m_xSMGR                 = rCopy.m_xSMGR;
161cdf0e10cSrcweir     m_eConfigType           = rCopy.m_eConfigType;
162cdf0e10cSrcweir     m_sResourceType         = rCopy.m_sResourceType;
163cdf0e10cSrcweir     m_sModule               = rCopy.m_sModule;
164cdf0e10cSrcweir     m_aSharedStorages       = rCopy.m_aSharedStorages;
165cdf0e10cSrcweir     m_xWorkingStorageShare  = rCopy.m_xWorkingStorageShare;
166cdf0e10cSrcweir     m_xWorkingStorageNoLang = rCopy.m_xWorkingStorageNoLang;
167cdf0e10cSrcweir     m_xWorkingStorageUser   = rCopy.m_xWorkingStorageUser;
168cdf0e10cSrcweir     m_lPresets              = rCopy.m_lPresets;
169cdf0e10cSrcweir     m_lTargets              = rCopy.m_lTargets;
170cdf0e10cSrcweir     m_aLocale               = rCopy.m_aLocale;
171cdf0e10cSrcweir     m_lDocumentStorages     = rCopy.m_lDocumentStorages;
172cdf0e10cSrcweir     m_sRelPathShare         = rCopy.m_sRelPathShare;
173cdf0e10cSrcweir     m_sRelPathNoLang        = rCopy.m_sRelPathNoLang;
174cdf0e10cSrcweir     m_sRelPathUser          = rCopy.m_sRelPathUser;
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir //-----------------------------------------------
~PresetHandler()178cdf0e10cSrcweir PresetHandler::~PresetHandler()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir     m_xWorkingStorageShare.clear();
181cdf0e10cSrcweir     m_xWorkingStorageNoLang.clear();
182cdf0e10cSrcweir     m_xWorkingStorageUser.clear();
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     /* #i46497#
185cdf0e10cSrcweir         Dont call forgetCachedStorages() here for shared storages.
186cdf0e10cSrcweir         Because we opened different sub storages by using openPath().
187cdf0e10cSrcweir         And every already open path was reused and referenced (means it's
188cdf0e10cSrcweir         ref count was increased!)
189cdf0e10cSrcweir         So now we have to release our ref counts to these shared storages
190cdf0e10cSrcweir         only ... and not to free all used storages.
191cdf0e10cSrcweir         Otherwise we will disconnect all other open configuration access
192cdf0e10cSrcweir         objects which base on these storages.
193cdf0e10cSrcweir      */
194cdf0e10cSrcweir     m_aSharedStorages->m_lStoragesShare.closePath(m_sRelPathShare);
195cdf0e10cSrcweir     m_aSharedStorages->m_lStoragesUser.closePath (m_sRelPathUser );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir     /* On the other side closePath() is not needed for our special handled
198cdf0e10cSrcweir        document storage. Because it's not shared with others ... so we can
199cdf0e10cSrcweir        free it.
200cdf0e10cSrcweir      */
201cdf0e10cSrcweir     m_lDocumentStorages.forgetCachedStorages();
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir //-----------------------------------------------
forgetCachedStorages()205cdf0e10cSrcweir void PresetHandler::forgetCachedStorages()
206cdf0e10cSrcweir {
207cdf0e10cSrcweir     // SAFE -> ----------------------------------
208cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     if (m_eConfigType == E_DOCUMENT)
211cdf0e10cSrcweir     {
212cdf0e10cSrcweir         m_xWorkingStorageShare.clear();
213cdf0e10cSrcweir         m_xWorkingStorageNoLang.clear();
214cdf0e10cSrcweir         m_xWorkingStorageUser.clear();
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     m_lDocumentStorages.forgetCachedStorages();
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     aWriteLock.unlock();
220cdf0e10cSrcweir     // <- SAFE ----------------------------------
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir //-----------------------------------------------
lcl_getLocalizedMessage(::sal_Int32 nID)224cdf0e10cSrcweir ::rtl::OUString lcl_getLocalizedMessage(::sal_Int32 nID)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     ::rtl::OUString sMessage = ::rtl::OUString::createFromAscii("Unknown error.");
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     switch(nID)
229cdf0e10cSrcweir     {
230cdf0e10cSrcweir         case ID_CORRUPT_UICONFIG_SHARE :
231cdf0e10cSrcweir                 sMessage = ::rtl::OUString( String( FwkResId( STR_CORRUPT_UICFG_SHARE )));
232cdf0e10cSrcweir                 break;
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         case ID_CORRUPT_UICONFIG_USER :
235cdf0e10cSrcweir                 sMessage = ::rtl::OUString( String( FwkResId( STR_CORRUPT_UICFG_USER )));
236cdf0e10cSrcweir                 break;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         case ID_CORRUPT_UICONFIG_GENERAL :
239cdf0e10cSrcweir                 sMessage = ::rtl::OUString( String( FwkResId( STR_CORRUPT_UICFG_GENERAL )));
240cdf0e10cSrcweir                 break;
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir     return sMessage;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir //-----------------------------------------------
getOrCreateRootStorageShare()247cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getOrCreateRootStorageShare()
248cdf0e10cSrcweir {
249cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xRoot = m_aSharedStorages->m_lStoragesShare.getRootStorage();
250cdf0e10cSrcweir     if (xRoot.is())
251cdf0e10cSrcweir         return xRoot;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     // SAFE -> ----------------------------------
254cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
255cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
256cdf0e10cSrcweir     aReadLock.unlock();
257cdf0e10cSrcweir     // <- SAFE ----------------------------------
258cdf0e10cSrcweir 
259cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xPathSettings(
260cdf0e10cSrcweir         xSMGR->createInstance(SERVICENAME_PATHSETTINGS),
261cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     ::rtl::OUString sShareLayer;
264cdf0e10cSrcweir     xPathSettings->getPropertyValue(BASEPATH_SHARE_LAYER) >>= sShareLayer;
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     // "UIConfig" is a "multi path" ... use first part only here!
267cdf0e10cSrcweir     sal_Int32 nPos = sShareLayer.indexOf(';');
268cdf0e10cSrcweir     if (nPos > 0)
269cdf0e10cSrcweir         sShareLayer = sShareLayer.copy(0, nPos);
270cdf0e10cSrcweir 
271cdf0e10cSrcweir     // Note: May be an user uses URLs without a final slash! Check it ...
272cdf0e10cSrcweir     nPos = sShareLayer.lastIndexOf('/');
273cdf0e10cSrcweir     if (nPos != sShareLayer.getLength()-1)
274cdf0e10cSrcweir         sShareLayer += ::rtl::OUString::createFromAscii("/");
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     sShareLayer += RELPATH_SHARE_LAYER; // folder
277cdf0e10cSrcweir     /*
278cdf0e10cSrcweir     // TODO remove me!
279cdf0e10cSrcweir     // Attention: This is temp. workaround ... We create a temp. storage file
280cdf0e10cSrcweir     // based of a sytem directory. This must be used so, till the storage implementation
281cdf0e10cSrcweir     // can work on directories too.
282cdf0e10cSrcweir     */
283cdf0e10cSrcweir     css::uno::Sequence< css::uno::Any > lArgs(2);
284cdf0e10cSrcweir     lArgs[0] <<= sShareLayer;
285cdf0e10cSrcweir     lArgs[1] <<= css::embed::ElementModes::READ | css::embed::ElementModes::NOCREATE;
286cdf0e10cSrcweir 
287cdf0e10cSrcweir     css::uno::Reference< css::lang::XSingleServiceFactory > xStorageFactory(xSMGR->createInstance(SERVICENAME_FILESYSTEMSTORAGEFACTORY)  , css::uno::UNO_QUERY_THROW);
288cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage >             xStorage;
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     try
291cdf0e10cSrcweir     {
292cdf0e10cSrcweir         xStorage = css::uno::Reference< css::embed::XStorage >(xStorageFactory->createInstanceWithArguments(lArgs), css::uno::UNO_QUERY_THROW);
293cdf0e10cSrcweir     }
294cdf0e10cSrcweir     catch(const css::uno::Exception& ex)
295cdf0e10cSrcweir     {
296cdf0e10cSrcweir         throw css::configuration::CorruptedUIConfigurationException(
297cdf0e10cSrcweir             lcl_getLocalizedMessage(ID_CORRUPT_UICONFIG_SHARE),
298cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >(),
299cdf0e10cSrcweir             ex.Message);
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir     m_aSharedStorages->m_lStoragesShare.setRootStorage(xStorage);
303cdf0e10cSrcweir 
304cdf0e10cSrcweir     return xStorage;
305cdf0e10cSrcweir }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir //-----------------------------------------------
getOrCreateRootStorageUser()308cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getOrCreateRootStorageUser()
309cdf0e10cSrcweir {
310cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xRoot = m_aSharedStorages->m_lStoragesUser.getRootStorage();
311cdf0e10cSrcweir     if (xRoot.is())
312cdf0e10cSrcweir         return xRoot;
313cdf0e10cSrcweir 
314cdf0e10cSrcweir     // SAFE -> ----------------------------------
315cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
316cdf0e10cSrcweir     css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
317cdf0e10cSrcweir     aReadLock.unlock();
318cdf0e10cSrcweir     // <- SAFE ----------------------------------
319cdf0e10cSrcweir 
320cdf0e10cSrcweir     css::uno::Reference< css::beans::XPropertySet > xPathSettings(
321cdf0e10cSrcweir         xSMGR->createInstance(SERVICENAME_PATHSETTINGS),
322cdf0e10cSrcweir         css::uno::UNO_QUERY_THROW);
323cdf0e10cSrcweir 
324cdf0e10cSrcweir     ::rtl::OUString sUserLayer;
325cdf0e10cSrcweir     xPathSettings->getPropertyValue(BASEPATH_USER_LAYER) >>= sUserLayer ;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     // Note: May be an user uses URLs without a final slash! Check it ...
328cdf0e10cSrcweir     sal_Int32 nPos = sUserLayer.lastIndexOf('/');
329cdf0e10cSrcweir     if (nPos != sUserLayer.getLength()-1)
330cdf0e10cSrcweir         sUserLayer += ::rtl::OUString::createFromAscii("/");
331cdf0e10cSrcweir 
332cdf0e10cSrcweir     sUserLayer  += RELPATH_USER_LAYER; // storage file
333cdf0e10cSrcweir 
334cdf0e10cSrcweir     css::uno::Sequence< css::uno::Any > lArgs(2);
335cdf0e10cSrcweir     lArgs[0] <<= sUserLayer;
336cdf0e10cSrcweir     lArgs[1] <<= css::embed::ElementModes::READWRITE;
337cdf0e10cSrcweir 
338cdf0e10cSrcweir     css::uno::Reference< css::lang::XSingleServiceFactory > xStorageFactory(xSMGR->createInstance(SERVICENAME_FILESYSTEMSTORAGEFACTORY)  , css::uno::UNO_QUERY_THROW);
339cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage >             xStorage;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir     try
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         xStorage = css::uno::Reference< css::embed::XStorage >(xStorageFactory->createInstanceWithArguments(lArgs), css::uno::UNO_QUERY_THROW);
344cdf0e10cSrcweir     }
345cdf0e10cSrcweir     catch(const css::uno::Exception& ex)
346cdf0e10cSrcweir     {
347cdf0e10cSrcweir         throw css::configuration::CorruptedUIConfigurationException(
348cdf0e10cSrcweir             lcl_getLocalizedMessage(ID_CORRUPT_UICONFIG_USER),
349cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >(),
350cdf0e10cSrcweir             ex.Message);
351cdf0e10cSrcweir     }
352cdf0e10cSrcweir 
353cdf0e10cSrcweir     m_aSharedStorages->m_lStoragesUser.setRootStorage(xStorage);
354cdf0e10cSrcweir 
355cdf0e10cSrcweir     return xStorage;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir //-----------------------------------------------
getWorkingStorageShare()359cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getWorkingStorageShare()
360cdf0e10cSrcweir {
361cdf0e10cSrcweir     // SAFE -> ----------------------------------
362cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
363cdf0e10cSrcweir     return m_xWorkingStorageShare;
364cdf0e10cSrcweir     // <- SAFE ----------------------------------
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir //-----------------------------------------------
getWorkingStorageUser()368cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getWorkingStorageUser()
369cdf0e10cSrcweir {
370cdf0e10cSrcweir     // SAFE -> ----------------------------------
371cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
372cdf0e10cSrcweir     return m_xWorkingStorageUser;
373cdf0e10cSrcweir     // <- SAFE ----------------------------------
374cdf0e10cSrcweir }
375cdf0e10cSrcweir 
376cdf0e10cSrcweir //-----------------------------------------------
getParentStorageShare(const css::uno::Reference<css::embed::XStorage> &)377cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getParentStorageShare(const css::uno::Reference< css::embed::XStorage >& /*xChild*/)
378cdf0e10cSrcweir {
379cdf0e10cSrcweir     // SAFE -> ----------------------------------
380cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
381cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorking = m_xWorkingStorageShare;
382cdf0e10cSrcweir     aReadLock.unlock();
383cdf0e10cSrcweir     // <- SAFE ----------------------------------
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     return m_aSharedStorages->m_lStoragesShare.getParentStorage(xWorking);
386cdf0e10cSrcweir }
387cdf0e10cSrcweir 
388cdf0e10cSrcweir //-----------------------------------------------
getParentStorageUser(const css::uno::Reference<css::embed::XStorage> &)389cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::getParentStorageUser(const css::uno::Reference< css::embed::XStorage >& /*xChild*/)
390cdf0e10cSrcweir {
391cdf0e10cSrcweir     // SAFE -> ----------------------------------
392cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
393cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorking = m_xWorkingStorageUser;
394cdf0e10cSrcweir     aReadLock.unlock();
395cdf0e10cSrcweir     // <- SAFE ----------------------------------
396cdf0e10cSrcweir 
397cdf0e10cSrcweir     return m_aSharedStorages->m_lStoragesUser.getParentStorage(xWorking);
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir //-----------------------------------------------
connectToResource(PresetHandler::EConfigType eConfigType,const::rtl::OUString & sResource,const::rtl::OUString & sModule,const css::uno::Reference<css::embed::XStorage> & xDocumentRoot,const::comphelper::Locale & aLocale)401cdf0e10cSrcweir void PresetHandler::connectToResource(      PresetHandler::EConfigType                   eConfigType  ,
402cdf0e10cSrcweir                                       const ::rtl::OUString&                             sResource    ,
403cdf0e10cSrcweir                                       const ::rtl::OUString&                             sModule      ,
404cdf0e10cSrcweir                                       const css::uno::Reference< css::embed::XStorage >& xDocumentRoot,
405cdf0e10cSrcweir                                       const ::comphelper::Locale&                        aLocale      )
406cdf0e10cSrcweir {
407cdf0e10cSrcweir     // TODO free all current open storages!
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     // SAFE -> ----------------------------------
410cdf0e10cSrcweir     WriteGuard aWriteLock(m_aLock);
411cdf0e10cSrcweir 
412cdf0e10cSrcweir     m_eConfigType   = eConfigType  ;
413cdf0e10cSrcweir     m_sResourceType = sResource    ;
414cdf0e10cSrcweir     m_sModule       = sModule      ;
415cdf0e10cSrcweir     m_aLocale       = aLocale      ;
416cdf0e10cSrcweir 
417cdf0e10cSrcweir     aWriteLock.unlock();
418cdf0e10cSrcweir     // <- SAFE ----------------------------------
419cdf0e10cSrcweir 
420cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xShare;
421cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xNoLang;
422cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xUser;
423cdf0e10cSrcweir 
424cdf0e10cSrcweir     // special case for documents
425cdf0e10cSrcweir     // use outside root storage, if we run in E_DOCUMENT mode!
426cdf0e10cSrcweir     if (eConfigType == E_DOCUMENT)
427cdf0e10cSrcweir     {
428cdf0e10cSrcweir         if (!xDocumentRoot.is())
429cdf0e10cSrcweir             throw css::uno::RuntimeException(
430cdf0e10cSrcweir                     ::rtl::OUString::createFromAscii("There is valid root storage, where the UI configuration can work on."),
431cdf0e10cSrcweir                     css::uno::Reference< css::uno::XInterface >());
432cdf0e10cSrcweir         m_lDocumentStorages.setRootStorage(xDocumentRoot);
433cdf0e10cSrcweir         xShare = xDocumentRoot;
434cdf0e10cSrcweir         xUser  = xDocumentRoot;
435cdf0e10cSrcweir     }
436cdf0e10cSrcweir     else
437cdf0e10cSrcweir     {
438cdf0e10cSrcweir         xShare = getOrCreateRootStorageShare();
439cdf0e10cSrcweir         xUser  = getOrCreateRootStorageUser();
440cdf0e10cSrcweir     }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir     // #...#
443cdf0e10cSrcweir     try
444cdf0e10cSrcweir     {
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     // a) inside share layer we should not create any new structures ... We jave to use
447cdf0e10cSrcweir     //    existing ones only!
448cdf0e10cSrcweir     // b) inside user layer we can (SOFT mode!) but sometimes we shouldnt (HARD mode!)
449cdf0e10cSrcweir     //    create new empty structures. We should preferr using of any existing structure.
450cdf0e10cSrcweir     sal_Int32 eShareMode = (css::embed::ElementModes::READ      | css::embed::ElementModes::NOCREATE);
451cdf0e10cSrcweir     sal_Int32 eUserMode  = (css::embed::ElementModes::READWRITE                                     );
452cdf0e10cSrcweir 
453cdf0e10cSrcweir     ::rtl::OUStringBuffer sRelPathBuf(1024);
454cdf0e10cSrcweir     ::rtl::OUString       sRelPathShare;
455cdf0e10cSrcweir     ::rtl::OUString       sRelPathNoLang;
456cdf0e10cSrcweir     ::rtl::OUString       sRelPathUser;
457cdf0e10cSrcweir     switch(eConfigType)
458cdf0e10cSrcweir     {
459cdf0e10cSrcweir         case E_GLOBAL :
460cdf0e10cSrcweir         {
461cdf0e10cSrcweir             sRelPathBuf.append(SUBSTORAGE_GLOBAL);
462cdf0e10cSrcweir             sRelPathBuf.append(PATH_SEPERATOR   );
463cdf0e10cSrcweir             sRelPathBuf.append(sResource        );
464cdf0e10cSrcweir             sRelPathShare = sRelPathBuf.makeStringAndClear();
465cdf0e10cSrcweir             sRelPathUser  = sRelPathShare;
466cdf0e10cSrcweir 
467cdf0e10cSrcweir             xShare = impl_openPathIgnoringErrors(sRelPathShare, eShareMode, sal_True );
468cdf0e10cSrcweir             xUser  = impl_openPathIgnoringErrors(sRelPathUser , eUserMode , sal_False);
469cdf0e10cSrcweir         }
470cdf0e10cSrcweir         break;
471cdf0e10cSrcweir 
472cdf0e10cSrcweir         case E_MODULES :
473cdf0e10cSrcweir         {
474cdf0e10cSrcweir             sRelPathBuf.append(SUBSTORAGE_MODULES);
475cdf0e10cSrcweir             sRelPathBuf.append(PATH_SEPERATOR    );
476cdf0e10cSrcweir             sRelPathBuf.append(sModule           );
477cdf0e10cSrcweir             sRelPathBuf.append(PATH_SEPERATOR    );
478cdf0e10cSrcweir             sRelPathBuf.append(sResource         );
479cdf0e10cSrcweir             sRelPathShare = sRelPathBuf.makeStringAndClear();
480cdf0e10cSrcweir             sRelPathUser  = sRelPathShare;
481cdf0e10cSrcweir 
482cdf0e10cSrcweir             xShare = impl_openPathIgnoringErrors(sRelPathShare, eShareMode, sal_True );
483cdf0e10cSrcweir             xUser  = impl_openPathIgnoringErrors(sRelPathUser , eUserMode , sal_False);
484cdf0e10cSrcweir         }
485cdf0e10cSrcweir         break;
486cdf0e10cSrcweir 
487cdf0e10cSrcweir         case E_DOCUMENT :
488cdf0e10cSrcweir         {
489cdf0e10cSrcweir             // A document does not have a share layer in real.
490cdf0e10cSrcweir             // It has one layer only, and this one should be opened READ_WRITE.
491cdf0e10cSrcweir             // So we open the user layer here only and set the share layer equals to it .-)
492cdf0e10cSrcweir 
493cdf0e10cSrcweir             sRelPathBuf.append(sResource);
494cdf0e10cSrcweir             sRelPathUser  = sRelPathBuf.makeStringAndClear();
495cdf0e10cSrcweir             sRelPathShare = sRelPathUser;
496cdf0e10cSrcweir 
497cdf0e10cSrcweir             try
498cdf0e10cSrcweir             {
499cdf0e10cSrcweir                 xUser  = m_lDocumentStorages.openPath(sRelPathUser , eUserMode );
500cdf0e10cSrcweir                 xShare = xUser;
501cdf0e10cSrcweir             }
502cdf0e10cSrcweir             catch(const css::uno::RuntimeException& exRun)
503cdf0e10cSrcweir                 { throw exRun; }
504cdf0e10cSrcweir             catch(const css::uno::Exception&)
505cdf0e10cSrcweir                 { xShare.clear(); xUser.clear(); }
506cdf0e10cSrcweir         }
507cdf0e10cSrcweir         break;
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir     // Non-localized global share
511cdf0e10cSrcweir     xNoLang = xShare;
512cdf0e10cSrcweir     sRelPathNoLang = sRelPathShare;
513cdf0e10cSrcweir 
514cdf0e10cSrcweir     if (
515cdf0e10cSrcweir         (aLocale     != ::comphelper::Locale::X_NOTRANSLATE()) && // localized level?
516cdf0e10cSrcweir         (eConfigType != E_DOCUMENT                           )    // no localization in document mode!
517cdf0e10cSrcweir        )
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         // First try to find the right localized set inside share layer.
520cdf0e10cSrcweir         // Fallbacks are allowed there.
521cdf0e10cSrcweir         ::comphelper::Locale aShareLocale       = aLocale      ;
522cdf0e10cSrcweir         ::rtl::OUString      sLocalizedSharePath(sRelPathShare);
523cdf0e10cSrcweir         sal_Bool             bAllowFallbacks    = sal_True     ;
524cdf0e10cSrcweir         xShare = impl_openLocalizedPathIgnoringErrors(sLocalizedSharePath, eShareMode, sal_True , aShareLocale, bAllowFallbacks);
525cdf0e10cSrcweir 
526cdf0e10cSrcweir         // The try to locate the right sub dir inside user layer ... without using fallbacks!
527cdf0e10cSrcweir         // Normaly the corresponding sub dir should be created matching the specified locale.
528cdf0e10cSrcweir         // Because we allow creation of storages inside user layer by default.
529cdf0e10cSrcweir         ::comphelper::Locale aUserLocale        = aLocale    ;
530cdf0e10cSrcweir         ::rtl::OUString      sLocalizedUserPath(sRelPathUser);
531cdf0e10cSrcweir                              bAllowFallbacks    = sal_False  ;
532cdf0e10cSrcweir         xUser = impl_openLocalizedPathIgnoringErrors(sLocalizedUserPath, eUserMode , sal_False, aUserLocale, bAllowFallbacks);
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         sRelPathShare = sLocalizedSharePath;
535cdf0e10cSrcweir         sRelPathUser  = sLocalizedUserPath ;
536cdf0e10cSrcweir     }
537cdf0e10cSrcweir 
538cdf0e10cSrcweir     // read content of level 3 (presets, targets)
539cdf0e10cSrcweir           css::uno::Reference< css::container::XNameAccess > xAccess ;
540cdf0e10cSrcweir           css::uno::Sequence< ::rtl::OUString >              lNames  ;
541cdf0e10cSrcweir     const ::rtl::OUString*                                   pNames  ;
542cdf0e10cSrcweir           sal_Int32                                          c       ;
543cdf0e10cSrcweir           sal_Int32                                          i       ;
544cdf0e10cSrcweir           OUStringList                                       lPresets;
545cdf0e10cSrcweir           OUStringList                                       lTargets;
546cdf0e10cSrcweir 
547cdf0e10cSrcweir     // read preset names of share layer
548cdf0e10cSrcweir     xAccess = css::uno::Reference< css::container::XNameAccess >(xShare, css::uno::UNO_QUERY);
549cdf0e10cSrcweir     if (xAccess.is())
550cdf0e10cSrcweir     {
551cdf0e10cSrcweir         lNames  = xAccess->getElementNames();
552cdf0e10cSrcweir         pNames  = lNames.getConstArray();
553cdf0e10cSrcweir         c       = lNames.getLength();
554cdf0e10cSrcweir 
555cdf0e10cSrcweir         for (i=0; i<c; ++i)
556cdf0e10cSrcweir         {
557cdf0e10cSrcweir             ::rtl::OUString sTemp = pNames[i];
558cdf0e10cSrcweir             sal_Int32       nPos  = sTemp.indexOf(FILE_EXTENSION);
559cdf0e10cSrcweir             if (nPos > -1)
560cdf0e10cSrcweir                 sTemp = sTemp.copy(0,nPos);
561cdf0e10cSrcweir             lPresets.push_back(sTemp);
562cdf0e10cSrcweir         }
563cdf0e10cSrcweir     }
564cdf0e10cSrcweir 
565cdf0e10cSrcweir     // read preset names of user layer
566cdf0e10cSrcweir     xAccess = css::uno::Reference< css::container::XNameAccess >(xUser, css::uno::UNO_QUERY);
567cdf0e10cSrcweir     if (xAccess.is())
568cdf0e10cSrcweir     {
569cdf0e10cSrcweir         lNames  = xAccess->getElementNames();
570cdf0e10cSrcweir         pNames  = lNames.getConstArray();
571cdf0e10cSrcweir         c       = lNames.getLength();
572cdf0e10cSrcweir 
573cdf0e10cSrcweir         for (i=0; i<c; ++i)
574cdf0e10cSrcweir         {
575cdf0e10cSrcweir             ::rtl::OUString sTemp = pNames[i];
576cdf0e10cSrcweir             sal_Int32       nPos  = sTemp.indexOf(FILE_EXTENSION);
577cdf0e10cSrcweir             if (nPos > -1)
578cdf0e10cSrcweir                 sTemp = sTemp.copy(0,nPos);
579cdf0e10cSrcweir             lTargets.push_back(sTemp);
580cdf0e10cSrcweir         }
581cdf0e10cSrcweir     }
582cdf0e10cSrcweir 
583cdf0e10cSrcweir     // SAFE -> ----------------------------------
584cdf0e10cSrcweir     aWriteLock.lock();
585cdf0e10cSrcweir 
586cdf0e10cSrcweir     m_xWorkingStorageShare = xShare  ;
587cdf0e10cSrcweir     m_xWorkingStorageNoLang= xNoLang;
588cdf0e10cSrcweir     m_xWorkingStorageUser  = xUser   ;
589cdf0e10cSrcweir     m_lPresets             = lPresets;
590cdf0e10cSrcweir     m_lTargets             = lTargets;
591cdf0e10cSrcweir     m_sRelPathShare        = sRelPathShare;
592cdf0e10cSrcweir     m_sRelPathNoLang       = sRelPathNoLang;
593cdf0e10cSrcweir     m_sRelPathUser         = sRelPathUser;
594cdf0e10cSrcweir 
595cdf0e10cSrcweir     aWriteLock.unlock();
596cdf0e10cSrcweir     // <- SAFE ----------------------------------
597cdf0e10cSrcweir 
598cdf0e10cSrcweir     }
599cdf0e10cSrcweir     catch(const css::uno::Exception& ex)
600cdf0e10cSrcweir     {
601cdf0e10cSrcweir         throw css::configuration::CorruptedUIConfigurationException(
602cdf0e10cSrcweir             lcl_getLocalizedMessage(ID_CORRUPT_UICONFIG_GENERAL),
603cdf0e10cSrcweir             css::uno::Reference< css::uno::XInterface >(),
604cdf0e10cSrcweir             ex.Message);
605cdf0e10cSrcweir     }
606cdf0e10cSrcweir }
607cdf0e10cSrcweir 
608cdf0e10cSrcweir //-----------------------------------------------
copyPresetToTarget(const::rtl::OUString & sPreset,const::rtl::OUString & sTarget)609cdf0e10cSrcweir void PresetHandler::copyPresetToTarget(const ::rtl::OUString& sPreset,
610cdf0e10cSrcweir                                        const ::rtl::OUString& sTarget)
611cdf0e10cSrcweir {
612cdf0e10cSrcweir     // dont check our preset list, if element exists
613cdf0e10cSrcweir     // We try to open it and forward all errors to the user!
614cdf0e10cSrcweir 
615cdf0e10cSrcweir     // SAFE -> ----------------------------------
616cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
617cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorkingShare = m_xWorkingStorageShare;
618cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorkingNoLang= m_xWorkingStorageNoLang;
619cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorkingUser  = m_xWorkingStorageUser ;
620cdf0e10cSrcweir     aReadLock.unlock();
621cdf0e10cSrcweir     // <- SAFE ----------------------------------
622cdf0e10cSrcweir 
623cdf0e10cSrcweir     // e.g. module without any config data ?!
624cdf0e10cSrcweir     if (
625cdf0e10cSrcweir         (!xWorkingShare.is()) ||
626cdf0e10cSrcweir         (!xWorkingUser.is() )
627cdf0e10cSrcweir        )
628cdf0e10cSrcweir     {
629cdf0e10cSrcweir        return;
630cdf0e10cSrcweir     }
631cdf0e10cSrcweir 
632cdf0e10cSrcweir     ::rtl::OUString sPresetFile(sPreset);
633cdf0e10cSrcweir     sPresetFile += FILE_EXTENSION;
634cdf0e10cSrcweir 
635cdf0e10cSrcweir     ::rtl::OUString sTargetFile(sTarget);
636cdf0e10cSrcweir     sTargetFile += FILE_EXTENSION;
637cdf0e10cSrcweir 
638cdf0e10cSrcweir     // remove existing elements before you try to copy the preset to that location ...
639cdf0e10cSrcweir     // Otherwise w will get an ElementExistException inside copyElementTo()!
640cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > xCheckingUser(xWorkingUser, css::uno::UNO_QUERY_THROW);
641cdf0e10cSrcweir     if (xCheckingUser->hasByName(sTargetFile))
642cdf0e10cSrcweir         xWorkingUser->removeElement(sTargetFile);
643cdf0e10cSrcweir 
644cdf0e10cSrcweir     xWorkingShare->copyElementTo(sPresetFile, xWorkingUser, sTargetFile);
645cdf0e10cSrcweir 
646cdf0e10cSrcweir     // If our storages work in transacted mode, we have
647cdf0e10cSrcweir     // to commit all changes from bottom to top!
648cdf0e10cSrcweir     commitUserChanges();
649cdf0e10cSrcweir }
650cdf0e10cSrcweir 
651cdf0e10cSrcweir //-----------------------------------------------
openPreset(const::rtl::OUString & sPreset,sal_Bool bUseNoLangGlobal)652cdf0e10cSrcweir css::uno::Reference< css::io::XStream > PresetHandler::openPreset(const ::rtl::OUString& sPreset,
653cdf0e10cSrcweir                                                                   sal_Bool bUseNoLangGlobal)
654cdf0e10cSrcweir {
655cdf0e10cSrcweir     // SAFE -> ----------------------------------
656cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
657cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xFolder = bUseNoLangGlobal? m_xWorkingStorageNoLang: m_xWorkingStorageShare;
658cdf0e10cSrcweir     aReadLock.unlock();
659cdf0e10cSrcweir     // <- SAFE ----------------------------------
660cdf0e10cSrcweir 
661cdf0e10cSrcweir     // e.g. module without any config data ?!
662cdf0e10cSrcweir     if (!xFolder.is())
663cdf0e10cSrcweir        return css::uno::Reference< css::io::XStream >();
664cdf0e10cSrcweir 
665cdf0e10cSrcweir     ::rtl::OUString sFile(sPreset);
666cdf0e10cSrcweir     sFile += FILE_EXTENSION;
667cdf0e10cSrcweir 
668cdf0e10cSrcweir     // inform user about errors (use original exceptions!)
669cdf0e10cSrcweir     css::uno::Reference< css::io::XStream > xStream = xFolder->openStreamElement(sFile, css::embed::ElementModes::READ);
670cdf0e10cSrcweir     return xStream;
671cdf0e10cSrcweir }
672cdf0e10cSrcweir 
673cdf0e10cSrcweir //-----------------------------------------------
openTarget(const::rtl::OUString & sTarget,sal_Bool bCreateIfMissing)674cdf0e10cSrcweir css::uno::Reference< css::io::XStream > PresetHandler::openTarget(const ::rtl::OUString& sTarget         ,
675cdf0e10cSrcweir                                                                         sal_Bool         bCreateIfMissing)
676cdf0e10cSrcweir {
677cdf0e10cSrcweir     // SAFE -> ----------------------------------
678cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
679cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xFolder = m_xWorkingStorageUser;
680cdf0e10cSrcweir     aReadLock.unlock();
681cdf0e10cSrcweir     // <- SAFE ----------------------------------
682cdf0e10cSrcweir 
683cdf0e10cSrcweir     // e.g. module without any config data ?!
684cdf0e10cSrcweir     if (!xFolder.is())
685cdf0e10cSrcweir        return css::uno::Reference< css::io::XStream >();
686cdf0e10cSrcweir 
687cdf0e10cSrcweir     ::rtl::OUString sFile(sTarget);
688cdf0e10cSrcweir     sFile += FILE_EXTENSION;
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     sal_Int32 nOpenMode = css::embed::ElementModes::READWRITE;
691cdf0e10cSrcweir     if (!bCreateIfMissing)
692cdf0e10cSrcweir         nOpenMode |= css::embed::ElementModes::NOCREATE;
693cdf0e10cSrcweir 
694cdf0e10cSrcweir     // try it in read/write mode first and ignore errors.
695cdf0e10cSrcweir     css::uno::Reference< css::io::XStream > xStream;
696cdf0e10cSrcweir     try
697cdf0e10cSrcweir     {
698cdf0e10cSrcweir         xStream = xFolder->openStreamElement(sFile, nOpenMode);
699cdf0e10cSrcweir         return xStream;
700cdf0e10cSrcweir     }
701cdf0e10cSrcweir     catch(const css::uno::RuntimeException&)
702cdf0e10cSrcweir         { throw; }
703cdf0e10cSrcweir     catch(const css::uno::Exception&)
704cdf0e10cSrcweir         { xStream.clear(); }
705cdf0e10cSrcweir 
706cdf0e10cSrcweir     // try it readonly if it failed before.
707cdf0e10cSrcweir     // inform user about errors (use original exceptions!)
708cdf0e10cSrcweir     nOpenMode &= ~css::embed::ElementModes::WRITE;
709cdf0e10cSrcweir     xStream    = xFolder->openStreamElement(sFile, nOpenMode);
710cdf0e10cSrcweir 
711cdf0e10cSrcweir     return xStream;
712cdf0e10cSrcweir }
713cdf0e10cSrcweir 
714cdf0e10cSrcweir //-----------------------------------------------
commitUserChanges()715cdf0e10cSrcweir void PresetHandler::commitUserChanges()
716cdf0e10cSrcweir {
717cdf0e10cSrcweir     // SAFE -> ----------------------------------
718cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
719cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xWorking = m_xWorkingStorageUser;
720cdf0e10cSrcweir     EConfigType                                 eCfgType = m_eConfigType;
721cdf0e10cSrcweir     aReadLock.unlock();
722cdf0e10cSrcweir     // <- SAFE ----------------------------------
723cdf0e10cSrcweir 
724cdf0e10cSrcweir     // e.g. module without any config data ?!
725cdf0e10cSrcweir     if (!xWorking.is())
726cdf0e10cSrcweir        return;
727cdf0e10cSrcweir 
728cdf0e10cSrcweir     ::rtl::OUString sPath;
729cdf0e10cSrcweir 
730cdf0e10cSrcweir     switch(eCfgType)
731cdf0e10cSrcweir     {
732cdf0e10cSrcweir         case E_GLOBAL :
733cdf0e10cSrcweir         case E_MODULES :
734cdf0e10cSrcweir         {
735cdf0e10cSrcweir             sPath = m_aSharedStorages->m_lStoragesUser.getPathOfStorage(xWorking);
736cdf0e10cSrcweir             m_aSharedStorages->m_lStoragesUser.commitPath(sPath);
737cdf0e10cSrcweir             m_aSharedStorages->m_lStoragesUser.notifyPath(sPath);
738cdf0e10cSrcweir         }
739cdf0e10cSrcweir         break;
740cdf0e10cSrcweir 
741cdf0e10cSrcweir         case E_DOCUMENT :
742cdf0e10cSrcweir         {
743cdf0e10cSrcweir             sPath = m_lDocumentStorages.getPathOfStorage(xWorking);
744cdf0e10cSrcweir             m_lDocumentStorages.commitPath(sPath);
745cdf0e10cSrcweir             m_lDocumentStorages.notifyPath(sPath);
746cdf0e10cSrcweir         }
747cdf0e10cSrcweir         break;
748cdf0e10cSrcweir     }
749cdf0e10cSrcweir }
750cdf0e10cSrcweir 
751cdf0e10cSrcweir //-----------------------------------------------
addStorageListener(IStorageListener * pListener)752cdf0e10cSrcweir void PresetHandler::addStorageListener(IStorageListener* pListener)
753cdf0e10cSrcweir {
754cdf0e10cSrcweir     // SAFE -> ----------------------------------
755cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
756cdf0e10cSrcweir     ::rtl::OUString sRelPath = m_sRelPathUser; // use user path ... because we dont work directly on the share layer!
757cdf0e10cSrcweir     EConfigType     eCfgType = m_eConfigType;
758cdf0e10cSrcweir     aReadLock.unlock();
759cdf0e10cSrcweir     // <- SAFE ----------------------------------
760cdf0e10cSrcweir 
761cdf0e10cSrcweir     if (!sRelPath.getLength())
762cdf0e10cSrcweir         return;
763cdf0e10cSrcweir 
764cdf0e10cSrcweir     switch(eCfgType)
765cdf0e10cSrcweir     {
766cdf0e10cSrcweir         case E_GLOBAL :
767cdf0e10cSrcweir         case E_MODULES :
768cdf0e10cSrcweir         {
769cdf0e10cSrcweir             m_aSharedStorages->m_lStoragesUser.addStorageListener(pListener, sRelPath);
770cdf0e10cSrcweir         }
771cdf0e10cSrcweir         break;
772cdf0e10cSrcweir 
773cdf0e10cSrcweir         case E_DOCUMENT :
774cdf0e10cSrcweir         {
775cdf0e10cSrcweir             m_lDocumentStorages.addStorageListener(pListener, sRelPath);
776cdf0e10cSrcweir         }
777cdf0e10cSrcweir         break;
778cdf0e10cSrcweir     }
779cdf0e10cSrcweir }
780cdf0e10cSrcweir 
781cdf0e10cSrcweir //-----------------------------------------------
removeStorageListener(IStorageListener * pListener)782cdf0e10cSrcweir void PresetHandler::removeStorageListener(IStorageListener* pListener)
783cdf0e10cSrcweir {
784cdf0e10cSrcweir     // SAFE -> ----------------------------------
785cdf0e10cSrcweir     ReadGuard aReadLock(m_aLock);
786cdf0e10cSrcweir     ::rtl::OUString sRelPath = m_sRelPathUser; // use user path ... because we dont work directly on the share layer!
787cdf0e10cSrcweir     EConfigType     eCfgType = m_eConfigType;
788cdf0e10cSrcweir     aReadLock.unlock();
789cdf0e10cSrcweir     // <- SAFE ----------------------------------
790cdf0e10cSrcweir 
791cdf0e10cSrcweir     if (!sRelPath.getLength())
792cdf0e10cSrcweir         return;
793cdf0e10cSrcweir 
794cdf0e10cSrcweir     switch(eCfgType)
795cdf0e10cSrcweir     {
796cdf0e10cSrcweir         case E_GLOBAL :
797cdf0e10cSrcweir         case E_MODULES :
798cdf0e10cSrcweir         {
799cdf0e10cSrcweir             m_aSharedStorages->m_lStoragesUser.removeStorageListener(pListener, sRelPath);
800cdf0e10cSrcweir         }
801cdf0e10cSrcweir         break;
802cdf0e10cSrcweir 
803cdf0e10cSrcweir         case E_DOCUMENT :
804cdf0e10cSrcweir         {
805cdf0e10cSrcweir             m_lDocumentStorages.removeStorageListener(pListener, sRelPath);
806cdf0e10cSrcweir         }
807cdf0e10cSrcweir         break;
808cdf0e10cSrcweir     }
809cdf0e10cSrcweir }
810cdf0e10cSrcweir 
811cdf0e10cSrcweir //-----------------------------------------------
impl_openPathIgnoringErrors(const::rtl::OUString & sPath,sal_Int32 eMode,sal_Bool bShare)812cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::impl_openPathIgnoringErrors(const ::rtl::OUString& sPath ,
813cdf0e10cSrcweir                                                                                              sal_Int32        eMode ,
814cdf0e10cSrcweir                                                                                              sal_Bool         bShare)
815cdf0e10cSrcweir {
816cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xPath;
817cdf0e10cSrcweir     try
818cdf0e10cSrcweir     {
819cdf0e10cSrcweir         if (bShare)
820cdf0e10cSrcweir             xPath = m_aSharedStorages->m_lStoragesShare.openPath(sPath, eMode);
821cdf0e10cSrcweir         else
822cdf0e10cSrcweir             xPath = m_aSharedStorages->m_lStoragesUser.openPath(sPath, eMode);
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir     catch(const css::uno::RuntimeException& exRun)
825cdf0e10cSrcweir         { throw exRun; }
826cdf0e10cSrcweir     catch(const css::uno::Exception&)
827cdf0e10cSrcweir         { xPath.clear(); }
828cdf0e10cSrcweir     return xPath;
829cdf0e10cSrcweir }
830cdf0e10cSrcweir 
831cdf0e10cSrcweir //-----------------------------------------------
impl_findMatchingLocalizedValue(const::std::vector<::rtl::OUString> & lLocalizedValues,::comphelper::Locale & aLocale,sal_Bool bAllowFallbacks)832cdf0e10cSrcweir ::std::vector< ::rtl::OUString >::const_iterator PresetHandler::impl_findMatchingLocalizedValue(const ::std::vector< ::rtl::OUString >& lLocalizedValues,
833cdf0e10cSrcweir                                                                                                       ::comphelper::Locale&             aLocale         ,
834cdf0e10cSrcweir                                                                                                       sal_Bool                          bAllowFallbacks )
835cdf0e10cSrcweir {
836cdf0e10cSrcweir     ::std::vector< ::rtl::OUString >::const_iterator pFound = lLocalizedValues.end();
837cdf0e10cSrcweir     if (bAllowFallbacks)
838cdf0e10cSrcweir     {
839cdf0e10cSrcweir         pFound = ::comphelper::Locale::getFallback(lLocalizedValues, aLocale.toISO());
840cdf0e10cSrcweir     }
841cdf0e10cSrcweir     else
842cdf0e10cSrcweir     {
843cdf0e10cSrcweir         for (  pFound  = lLocalizedValues.begin();
844cdf0e10cSrcweir                pFound != lLocalizedValues.end()  ;
845cdf0e10cSrcweir              ++pFound                            )
846cdf0e10cSrcweir         {
847cdf0e10cSrcweir             const ::rtl::OUString&     sCheckISO   = *pFound;
848cdf0e10cSrcweir                   ::comphelper::Locale aCheckLocale(sCheckISO);
849cdf0e10cSrcweir             if (aCheckLocale.equals(aLocale))
850cdf0e10cSrcweir                 break;
851cdf0e10cSrcweir         }
852cdf0e10cSrcweir     }
853cdf0e10cSrcweir 
854cdf0e10cSrcweir     // if we found a valid locale ... take it over to our in/out parameter aLocale
855cdf0e10cSrcweir     if (pFound != lLocalizedValues.end())
856cdf0e10cSrcweir     {
857cdf0e10cSrcweir         const ::rtl::OUString& sISOLocale = *pFound;
858cdf0e10cSrcweir         aLocale.fromISO(sISOLocale);
859cdf0e10cSrcweir     }
860cdf0e10cSrcweir 
861cdf0e10cSrcweir     return pFound;
862cdf0e10cSrcweir }
863cdf0e10cSrcweir 
864cdf0e10cSrcweir //-----------------------------------------------
impl_openLocalizedPathIgnoringErrors(::rtl::OUString & sPath,sal_Int32 eMode,sal_Bool bShare,::comphelper::Locale & aLocale,sal_Bool bAllowFallback)865cdf0e10cSrcweir css::uno::Reference< css::embed::XStorage > PresetHandler::impl_openLocalizedPathIgnoringErrors(::rtl::OUString&      sPath         ,
866cdf0e10cSrcweir                                                                                                 sal_Int32             eMode         ,
867cdf0e10cSrcweir                                                                                                 sal_Bool              bShare        ,
868cdf0e10cSrcweir                                                                                                 ::comphelper::Locale& aLocale       ,
869cdf0e10cSrcweir                                                                                                 sal_Bool              bAllowFallback)
870cdf0e10cSrcweir {
871cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage >      xPath         = impl_openPathIgnoringErrors(sPath, eMode, bShare);
872cdf0e10cSrcweir     ::std::vector< ::rtl::OUString >                 lSubFolders   = impl_getSubFolderNames(xPath);
873cdf0e10cSrcweir     ::std::vector< ::rtl::OUString >::const_iterator pLocaleFolder = impl_findMatchingLocalizedValue(lSubFolders, aLocale, bAllowFallback);
874cdf0e10cSrcweir 
875cdf0e10cSrcweir     // no fallback ... creation not allowed => no storage
876cdf0e10cSrcweir     if (
877cdf0e10cSrcweir         (pLocaleFolder == lSubFolders.end()                                                ) &&
878cdf0e10cSrcweir         ((eMode & css::embed::ElementModes::NOCREATE) == css::embed::ElementModes::NOCREATE)
879cdf0e10cSrcweir        )
880cdf0e10cSrcweir         return css::uno::Reference< css::embed::XStorage >();
881cdf0e10cSrcweir 
882cdf0e10cSrcweir     // it doesnt matter, if there is a locale fallback or not
883cdf0e10cSrcweir     // If creation of storages is allowed, we do it anyway.
884cdf0e10cSrcweir     // Otherwhise we have no acc config at all, which can make other trouble.
885cdf0e10cSrcweir     ::rtl::OUString sLocalizedPath;
886cdf0e10cSrcweir     sLocalizedPath  = sPath;
887cdf0e10cSrcweir     sLocalizedPath += PATH_SEPERATOR;
888cdf0e10cSrcweir     if (pLocaleFolder != lSubFolders.end())
889cdf0e10cSrcweir         sLocalizedPath += *pLocaleFolder;
890cdf0e10cSrcweir     else
891cdf0e10cSrcweir         sLocalizedPath += aLocale.toISO();
892cdf0e10cSrcweir 
893cdf0e10cSrcweir     css::uno::Reference< css::embed::XStorage > xLocalePath = impl_openPathIgnoringErrors(sLocalizedPath, eMode, bShare);
894cdf0e10cSrcweir 
895cdf0e10cSrcweir     if (xLocalePath.is())
896cdf0e10cSrcweir         sPath = sLocalizedPath;
897cdf0e10cSrcweir     else
898cdf0e10cSrcweir         sPath = ::rtl::OUString();
899cdf0e10cSrcweir 
900cdf0e10cSrcweir     return xLocalePath;
901cdf0e10cSrcweir }
902cdf0e10cSrcweir 
903cdf0e10cSrcweir //-----------------------------------------------
impl_getSubFolderNames(const css::uno::Reference<css::embed::XStorage> & xFolder)904cdf0e10cSrcweir ::std::vector< ::rtl::OUString > PresetHandler::impl_getSubFolderNames(const css::uno::Reference< css::embed::XStorage >& xFolder)
905cdf0e10cSrcweir {
906cdf0e10cSrcweir     css::uno::Reference< css::container::XNameAccess > xAccess(xFolder, css::uno::UNO_QUERY);
907cdf0e10cSrcweir     if (!xAccess.is())
908cdf0e10cSrcweir         return ::std::vector< ::rtl::OUString >();
909cdf0e10cSrcweir 
910cdf0e10cSrcweir           ::std::vector< ::rtl::OUString >      lSubFolders;
911cdf0e10cSrcweir     const css::uno::Sequence< ::rtl::OUString > lNames = xAccess->getElementNames();
912cdf0e10cSrcweir     const ::rtl::OUString*                      pNames = lNames.getConstArray();
913cdf0e10cSrcweir           sal_Int32                             c      = lNames.getLength();
914cdf0e10cSrcweir           sal_Int32                             i      = 0;
915cdf0e10cSrcweir 
916cdf0e10cSrcweir     for (i=0; i<c; ++i)
917cdf0e10cSrcweir     {
918cdf0e10cSrcweir         try
919cdf0e10cSrcweir         {
920cdf0e10cSrcweir             if (xFolder->isStorageElement(pNames[i]))
921cdf0e10cSrcweir                 lSubFolders.push_back(pNames[i]);
922cdf0e10cSrcweir         }
923cdf0e10cSrcweir         catch(const css::uno::RuntimeException& exRun)
924cdf0e10cSrcweir             { throw exRun; }
925cdf0e10cSrcweir         catch(const css::uno::Exception&)
926cdf0e10cSrcweir             {}
927cdf0e10cSrcweir     }
928cdf0e10cSrcweir 
929cdf0e10cSrcweir     return lSubFolders;
930cdf0e10cSrcweir }
931cdf0e10cSrcweir 
932cdf0e10cSrcweir //-----------------------------------------------
933cdf0e10cSrcweir } // namespace framework
934