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