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 #ifndef __FRAMEWORK_PATTERN_STORAGES_HXX_
25 #define __FRAMEWORK_PATTERN_STORAGES_HXX_
26 
27 //_______________________________________________
28 // own includes
29 
30 #include <services.h>
31 #include <general.h>
32 
33 //_______________________________________________
34 // interface includes
35 #include <com/sun/star/io/XOutputStream.hpp>
36 #include <com/sun/star/io/XSeekable.hpp>
37 #include <com/sun/star/embed/ElementModes.hpp>
38 #include <com/sun/star/embed/XStorage.hpp>
39 #include <com/sun/star/embed/XPackageStructureCreator.hpp>
40 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 
43 //_______________________________________________
44 // other includes
45 
46 //_______________________________________________
47 // namespaces
48 
49 #ifndef css
50 namespace css = ::com::sun::star;
51 #endif
52 
53 namespace framework{
54     namespace pattern{
55         namespace storages{
56 
57 //_______________________________________________
58 // definitions
59 
60 //-----------------------------------------------
createTempStorageBasedOnFolder(const::rtl::OUString & sFolder,const css::uno::Reference<css::lang::XMultiServiceFactory> & xSMGR,sal_Bool bReadOnly)61 css::uno::Reference< css::embed::XStorage > createTempStorageBasedOnFolder(const ::rtl::OUString&                                        sFolder  ,
62                                                                            const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR    ,
63                                                                                  sal_Bool                                                bReadOnly)
64 {
65     // error during opening the temp file isn't really a runtime error -> handle it gracefully
66     css::uno::Reference< css::io::XOutputStream > xTempFile(xSMGR->createInstance(SERVICENAME_TEMPFILE), css::uno::UNO_QUERY);
67     if (!xTempFile.is())
68         return css::uno::Reference< css::embed::XStorage >();
69 
70     // creation of needed resources is mandatory -> error = runtime error
71     css::uno::Reference< css::embed::XPackageStructureCreator > xPackageCreator(xSMGR->createInstance(SERVICENAME_PACKAGESTRUCTURECREATOR), css::uno::UNO_QUERY_THROW);
72     css::uno::Reference< css::lang::XSingleServiceFactory >     xStorageFactory(xSMGR->createInstance(SERVICENAME_STORAGEFACTORY)         , css::uno::UNO_QUERY_THROW);
73 
74     // create zip package
75     xPackageCreator->convertToPackage(sFolder, xTempFile);
76 
77     // seek it back - so it can be used in a defined way.
78     css::uno::Reference< css::io::XSeekable > xSeekable(xTempFile, css::uno::UNO_QUERY_THROW);
79     xSeekable->seek(0);
80 
81     // open the temp. zip package - using the right open mode
82     sal_Int32 nOpenMode = css::embed::ElementModes::ELEMENT_READWRITE;
83     if (bReadOnly)
84         nOpenMode = css::embed::ElementModes::ELEMENT_READ;
85 
86     css::uno::Sequence< css::uno::Any > lArgs(2);
87     lArgs[0] <<= xTempFile;
88     lArgs[1] <<= nOpenMode;
89 
90     css::uno::Reference< css::embed::XStorage > xStorage(xStorageFactory->createInstanceWithArguments(lArgs), css::uno::UNO_QUERY_THROW);
91     return xStorage;
92 }
93 
94         } // namespace storages
95     } // namespace pattern
96 } // namespace framework
97 
98 #endif // __FRAMEWORK_PATTERN_STORAGES_HXX_
99