1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_OOX_HELPER_STORAGEBASE_HXX
21 #define INCLUDED_OOX_HELPER_STORAGEBASE_HXX
22 
23 #include <functional>
24 #include <memory>
25 #include <vector>
26 
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <oox/dllapi.h>
29 #include <oox/helper/refmap.hxx>
30 #include <rtl/ustring.hxx>
31 
32 namespace com { namespace sun { namespace star {
33     namespace embed { class XStorage; }
34     namespace io { class XInputStream; }
35     namespace io { class XOutputStream; }
36     namespace io { class XStream; }
37 } } }
38 
39 namespace oox {
40 
41 
42 class StorageBase;
43 typedef std::shared_ptr< StorageBase > StorageRef;
44 
45 /** Base class for storage access implementations.
46 
47     Derived classes will be used to encapsulate storage access implementations
48     for ZIP storages containing XML streams, and OLE storages containing binary
49     data streams.
50  */
51 class OOX_DLLPUBLIC StorageBase
52 {
53 public:
54     explicit            StorageBase(
55                             const css::uno::Reference< css::io::XInputStream >& rxInStream,
56                             bool bBaseStreamAccess );
57 
58     explicit            StorageBase(
59                             const css::uno::Reference< css::io::XStream >& rxOutStream,
60                             bool bBaseStreamAccess );
61 
62     virtual             ~StorageBase();
63 
64     /** Returns true, if the object represents a valid storage. */
65     bool                isStorage() const;
66 
67     /** Returns true, if the object represents the root storage. */
68     bool                isRootStorage() const;
69 
70     /** Returns true, if the storage operates in read-only mode (based on an
71         input stream). */
isReadOnly() const72     bool                isReadOnly() const { return mbReadOnly;}
73 
74     /** Returns the com.sun.star.embed.XStorage interface of the current storage. */
75     css::uno::Reference< css::embed::XStorage >
76                         getXStorage() const;
77 
78     /** Returns the element name of this storage. */
getName() const79     const OUString& getName() const { return maStorageName;}
80 
81     /** Returns the full path of this storage. */
82     OUString     getPath() const;
83 
84     /** Fills the passed vector with the names of all direct elements of this
85         storage. */
86     void                getElementNames( ::std::vector< OUString >& orElementNames ) const;
87 
88     /** Opens and returns the specified sub storage from the storage.
89 
90         @param rStorageName
91             The name of the embedded storage. The name may contain slashes to
92             open storages from embedded substorages.
93         @param bCreateMissing
94             True = create missing sub storages (for export filters). Must be
95             false for storages based on input streams.
96      */
97     StorageRef          openSubStorage( const OUString& rStorageName, bool bCreateMissing );
98 
99     /** Opens and returns the specified input stream from the storage.
100 
101         @param rStreamName
102             The name of the embedded storage stream. The name may contain
103             slashes to open streams from embedded substorages. If base stream
104             access has been enabled in the constructor, the base stream can be
105             accessed by passing an empty string as stream name.
106      */
107     css::uno::Reference< css::io::XInputStream >
108                         openInputStream( const OUString& rStreamName );
109 
110     /** Opens and returns the specified output stream from the storage.
111 
112         @param rStreamName
113             The name of the embedded storage stream. The name may contain
114             slashes to create and open streams in embedded substorages. If base
115             stream access has been enabled in the constructor, the base stream
116             can be accessed by passing an empty string as stream name.
117      */
118     css::uno::Reference< css::io::XOutputStream >
119                         openOutputStream( const OUString& rStreamName );
120 
121     /** Copies the specified element from this storage to the passed
122          destination storage.
123 
124         @param rElementName
125             The name of the embedded storage or stream. The name may contain
126             slashes to specify an element in an embedded substorage. In this
127             case, the element will be copied to the same substorage in the
128             destination storage.
129      */
130     void                copyToStorage( StorageBase& rDestStrg, const OUString& rElementName );
131 
132     /** Copies all streams of this storage and of all substorages to the passed
133         destination. */
134     void                copyStorageToStorage( StorageBase& rDestStrg );
135 
136     /** Commits the changes to the storage and all substorages. */
137     void                commit();
138 
139 protected:
140     /** Special constructor for sub storage objects. */
141     explicit            StorageBase( const StorageBase& rParentStorage, const OUString& rStorageName, bool bReadOnly );
142 
143 private:
144                         StorageBase( const StorageBase& ) = delete;
145     StorageBase&        operator=( const StorageBase& ) = delete;
146 
147     /** Returns true, if the object represents a valid storage. */
148     virtual bool        implIsStorage() const = 0;
149 
150     /** Returns the com.sun.star.embed.XStorage interface of the current storage. */
151     virtual css::uno::Reference< css::embed::XStorage >
152                         implGetXStorage() const = 0;
153 
154     /** Returns the names of all elements of this storage. */
155     virtual void        implGetElementNames( ::std::vector< OUString >& orElementNames ) const = 0;
156 
157     /** Implementation of opening a storage element. */
158     virtual StorageRef  implOpenSubStorage( const OUString& rElementName, bool bCreate ) = 0;
159 
160     /** Implementation of opening an input stream element. */
161     virtual css::uno::Reference< css::io::XInputStream >
162                         implOpenInputStream( const OUString& rElementName ) = 0;
163 
164     /** Implementation of opening an output stream element. */
165     virtual css::uno::Reference< css::io::XOutputStream >
166                         implOpenOutputStream( const OUString& rElementName ) = 0;
167 
168     /** Commits the current storage. */
169     virtual void        implCommit() const = 0;
170 
171     /** Helper that opens and caches the specified direct substorage. */
172     StorageRef          getSubStorage( const OUString& rElementName, bool bCreateMissing );
173 
174 private:
175     RefMap< OUString, StorageBase >
176                         maSubStorages;      ///< Map of direct sub storages.
177     css::uno::Reference< css::io::XInputStream >
178                         mxInStream;         ///< Cached base input stream (to keep it alive).
179     css::uno::Reference< css::io::XStream >
180                         mxOutStream;        ///< Cached base output stream (to keep it alive).
181     OUString const      maParentPath;       ///< Full path of parent storage.
182     OUString const      maStorageName;      ///< Name of this storage, if it is a substorage.
183     bool const          mbBaseStreamAccess; ///< True = access base streams with empty stream name.
184     bool const          mbReadOnly;         ///< True = storage opened read-only (based on input stream).
185 };
186 
187 
188 } // namespace oox
189 
190 #endif
191 
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
193