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 INCLUDED_TDOC_STGELEMS_HXX 25 #define INCLUDED_TDOC_STGELEMS_HXX 26 27 #include <memory> 28 29 #include "osl/mutex.hxx" 30 #include "rtl/ref.hxx" 31 32 #include "cppuhelper/implbase2.hxx" 33 #include "cppuhelper/implbase5.hxx" 34 35 #include "com/sun/star/embed/XStorage.hpp" 36 #include "com/sun/star/embed/XTransactedObject.hpp" 37 #include "com/sun/star/io/XOutputStream.hpp" 38 #include "com/sun/star/io/XStream.hpp" 39 #include "com/sun/star/io/XTruncate.hpp" 40 #include "com/sun/star/lang/XComponent.hpp" 41 42 #include "tdoc_storage.hxx" 43 44 namespace tdoc_ucp { 45 46 struct MutexHolder 47 { 48 osl::Mutex m_aMutex; 49 }; 50 51 //======================================================================= 52 53 class ParentStorageHolder : public MutexHolder 54 { 55 public: 56 ParentStorageHolder( 57 const com::sun::star::uno::Reference< 58 com::sun::star::embed::XStorage > & xParentStorage, 59 const rtl::OUString & rUri ); 60 isParentARootStorage() const61 bool isParentARootStorage() const 62 { return m_bParentIsRootStorage; } 63 com::sun::star::uno::Reference< com::sun::star::embed::XStorage > getParentStorage() const64 getParentStorage() const 65 { return m_xParentStorage; } setParentStorage(const com::sun::star::uno::Reference<com::sun::star::embed::XStorage> & xStg)66 void setParentStorage( const com::sun::star::uno::Reference< 67 com::sun::star::embed::XStorage > & xStg ) 68 { osl::MutexGuard aGuard( m_aMutex ); m_xParentStorage = xStg; } 69 70 private: 71 com::sun::star::uno::Reference< 72 com::sun::star::embed::XStorage > m_xParentStorage; 73 bool m_bParentIsRootStorage; 74 }; 75 76 //======================================================================= 77 78 typedef 79 cppu::WeakImplHelper2< 80 com::sun::star::embed::XStorage, 81 com::sun::star::embed::XTransactedObject > StorageUNOBase; 82 83 class Storage : public StorageUNOBase, public ParentStorageHolder 84 { 85 public: 86 Storage( 87 const com::sun::star::uno::Reference< 88 com::sun::star::lang::XMultiServiceFactory > & xSMgr, 89 const rtl::Reference< StorageElementFactory > & xFactory, 90 const rtl::OUString & rUri, 91 const com::sun::star::uno::Reference< 92 com::sun::star::embed::XStorage > & xParentStorage, 93 const com::sun::star::uno::Reference< 94 com::sun::star::embed::XStorage > & xStorageToWrap ); 95 virtual ~Storage(); 96 isDocumentStorage() const97 bool isDocumentStorage() const { return m_bIsDocumentStorage; } 98 99 // XInterface 100 virtual com::sun::star::uno::Any SAL_CALL queryInterface( 101 const com::sun::star::uno::Type& aType ); 102 virtual void SAL_CALL acquire() 103 throw (); 104 virtual void SAL_CALL release() 105 throw (); 106 107 // XTypeProvider (implemnented by base, but needs to be overridden for 108 // delegating to aggregate) 109 virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL 110 getTypes(); 111 virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL 112 getImplementationId(); 113 114 // XComponent ( one of XStorage bases ) 115 virtual void SAL_CALL 116 dispose(); 117 virtual void SAL_CALL 118 addEventListener( const com::sun::star::uno::Reference< 119 com::sun::star::lang::XEventListener > & xListener ); 120 virtual void SAL_CALL 121 removeEventListener( const com::sun::star::uno::Reference< 122 com::sun::star::lang::XEventListener >& aListener ); 123 124 // XNameAccess ( one of XStorage bases ) 125 virtual com::sun::star::uno::Any SAL_CALL 126 getByName( const rtl::OUString& aName ); 127 virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL 128 getElementNames(); 129 virtual sal_Bool SAL_CALL 130 hasByName( const rtl::OUString& aName ); 131 132 // XElementAccess (base of XNameAccess) 133 virtual com::sun::star::uno::Type SAL_CALL 134 getElementType(); 135 virtual sal_Bool SAL_CALL 136 hasElements(); 137 138 // XStorage 139 virtual void SAL_CALL 140 copyToStorage( const com::sun::star::uno::Reference< 141 com::sun::star::embed::XStorage >& xDest ); 142 virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL 143 openStreamElement( const ::rtl::OUString& aStreamName, 144 sal_Int32 nOpenMode ); 145 virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL 146 openEncryptedStreamElement( const ::rtl::OUString& aStreamName, 147 sal_Int32 nOpenMode, 148 const ::rtl::OUString& aPassword ); 149 virtual com::sun::star::uno::Reference< com::sun::star::embed::XStorage > SAL_CALL 150 openStorageElement( const ::rtl::OUString& aStorName, 151 sal_Int32 nOpenMode ); 152 virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL 153 cloneStreamElement( const ::rtl::OUString& aStreamName ); 154 virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL 155 cloneEncryptedStreamElement( const ::rtl::OUString& aStreamName, 156 const ::rtl::OUString& aPassword ); 157 virtual void SAL_CALL 158 copyLastCommitTo( const com::sun::star::uno::Reference< 159 com::sun::star::embed::XStorage >& xTargetStorage ); 160 virtual void SAL_CALL 161 copyStorageElementLastCommitTo( const ::rtl::OUString& aStorName, 162 const com::sun::star::uno::Reference< 163 com::sun::star::embed::XStorage > & 164 xTargetStorage ); 165 virtual sal_Bool SAL_CALL 166 isStreamElement( const ::rtl::OUString& aElementName ); 167 virtual sal_Bool SAL_CALL 168 isStorageElement( const ::rtl::OUString& aElementName ); 169 virtual void SAL_CALL 170 removeElement( const ::rtl::OUString& aElementName ); 171 virtual void SAL_CALL 172 renameElement( const ::rtl::OUString& aEleName, 173 const ::rtl::OUString& aNewName ); 174 virtual void SAL_CALL 175 copyElementTo( const ::rtl::OUString& aElementName, 176 const com::sun::star::uno::Reference< 177 com::sun::star::embed::XStorage >& xDest, 178 const ::rtl::OUString& aNewName ); 179 virtual void SAL_CALL 180 moveElementTo( const ::rtl::OUString& aElementName, 181 const com::sun::star::uno::Reference< 182 com::sun::star::embed::XStorage >& xDest, 183 const ::rtl::OUString& rNewName ); 184 185 // XTransactedObject 186 virtual void SAL_CALL commit(); 187 virtual void SAL_CALL revert(); 188 189 private: 190 Storage( const rtl::Reference< Storage > & rFactory ); // n.i. 191 192 rtl::Reference< StorageElementFactory > m_xFactory; 193 com::sun::star::uno::Reference< 194 com::sun::star::uno::XAggregation > m_xAggProxy; 195 com::sun::star::uno::Reference< 196 com::sun::star::embed::XStorage > m_xWrappedStorage; 197 com::sun::star::uno::Reference< 198 com::sun::star::embed::XTransactedObject > m_xWrappedTransObj; 199 com::sun::star::uno::Reference< 200 com::sun::star::lang::XComponent > m_xWrappedComponent; 201 com::sun::star::uno::Reference< 202 com::sun::star::lang::XTypeProvider > m_xWrappedTypeProv; 203 bool m_bIsDocumentStorage; 204 205 StorageElementFactory::StorageMap::iterator m_aContainerIt; 206 207 friend class StorageElementFactory; 208 friend class std::auto_ptr< Storage >; 209 }; 210 211 //======================================================================= 212 213 typedef 214 cppu::WeakImplHelper2< 215 com::sun::star::io::XOutputStream, 216 com::sun::star::lang::XComponent > OutputStreamUNOBase; 217 218 class OutputStream : public OutputStreamUNOBase, public ParentStorageHolder 219 { 220 public: 221 OutputStream( 222 const com::sun::star::uno::Reference< 223 com::sun::star::lang::XMultiServiceFactory > & xSMgr, 224 const rtl::OUString & rUri, 225 const com::sun::star::uno::Reference< 226 com::sun::star::embed::XStorage > & xParentStorage, 227 const com::sun::star::uno::Reference< 228 com::sun::star::io::XOutputStream > & xStreamToWrap ); 229 virtual ~OutputStream(); 230 231 // XInterface 232 virtual com::sun::star::uno::Any SAL_CALL 233 queryInterface( const com::sun::star::uno::Type& aType ); 234 235 // XTypeProvider (implemnented by base, but needs to be overridden for 236 // delegating to aggregate) 237 virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL 238 getTypes(); 239 virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL 240 getImplementationId(); 241 242 // XOutputStream 243 virtual void SAL_CALL 244 writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ); 245 virtual void SAL_CALL 246 flush( ); 247 // Note: We need to intercept this one. 248 virtual void SAL_CALL 249 closeOutput( ); 250 251 // XComponent 252 // Note: We need to intercept this one. 253 virtual void SAL_CALL 254 dispose(); 255 virtual void SAL_CALL 256 addEventListener( const com::sun::star::uno::Reference< 257 com::sun::star::lang::XEventListener >& xListener ); 258 virtual void SAL_CALL 259 removeEventListener( const com::sun::star::uno::Reference< 260 com::sun::star::lang::XEventListener >& aListener ); 261 262 private: 263 com::sun::star::uno::Reference< 264 com::sun::star::uno::XAggregation > m_xAggProxy; 265 com::sun::star::uno::Reference< 266 com::sun::star::io::XOutputStream > m_xWrappedStream; 267 com::sun::star::uno::Reference< 268 com::sun::star::lang::XComponent > m_xWrappedComponent; 269 com::sun::star::uno::Reference< 270 com::sun::star::lang::XTypeProvider > m_xWrappedTypeProv; 271 }; 272 273 //======================================================================= 274 275 typedef cppu::WeakImplHelper5< com::sun::star::io::XStream, 276 com::sun::star::io::XOutputStream, 277 com::sun::star::io::XTruncate, 278 com::sun::star::io::XInputStream, 279 com::sun::star::lang::XComponent > 280 StreamUNOBase; 281 282 class Stream : public StreamUNOBase, public ParentStorageHolder 283 { 284 public: 285 Stream( 286 const com::sun::star::uno::Reference< 287 com::sun::star::lang::XMultiServiceFactory > & xSMgr, 288 const rtl::OUString & rUri, 289 const com::sun::star::uno::Reference< 290 com::sun::star::embed::XStorage > & xParentStorage, 291 const com::sun::star::uno::Reference< 292 com::sun::star::io::XStream > & xStreamToWrap ); 293 294 virtual ~Stream(); 295 296 // XInterface 297 virtual com::sun::star::uno::Any SAL_CALL 298 queryInterface( const com::sun::star::uno::Type& aType ); 299 300 // XTypeProvider (implemnented by base, but needs to be overridden for 301 // delegating to aggregate) 302 virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL 303 getTypes(); 304 virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL 305 getImplementationId(); 306 307 // XStream 308 virtual com::sun::star::uno::Reference< 309 com::sun::star::io::XInputStream > SAL_CALL 310 getInputStream(); 311 312 virtual com::sun::star::uno::Reference< 313 com::sun::star::io::XOutputStream > SAL_CALL 314 getOutputStream(); 315 316 // XOutputStream 317 virtual void SAL_CALL 318 writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData ); 319 320 virtual void SAL_CALL 321 flush(); 322 323 virtual void SAL_CALL 324 closeOutput(); 325 326 // XTruncate 327 virtual void SAL_CALL 328 truncate(); 329 330 // XInputStream 331 virtual sal_Int32 SAL_CALL 332 readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData, 333 sal_Int32 nBytesToRead ); 334 335 virtual sal_Int32 SAL_CALL 336 readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData, 337 sal_Int32 nMaxBytesToRead ); 338 339 virtual void SAL_CALL 340 skipBytes( sal_Int32 nBytesToSkip ); 341 342 virtual sal_Int32 SAL_CALL 343 available(); 344 345 virtual void SAL_CALL 346 closeInput(); 347 348 // XComponent 349 // Note: We need to intercept this one. 350 virtual void SAL_CALL 351 dispose(); 352 virtual void SAL_CALL 353 addEventListener( const com::sun::star::uno::Reference< 354 com::sun::star::lang::XEventListener >& xListener ); 355 virtual void SAL_CALL 356 removeEventListener( const com::sun::star::uno::Reference< 357 com::sun::star::lang::XEventListener >& aListener ); 358 359 private: 360 void commitChanges(); 361 362 com::sun::star::uno::Reference< 363 com::sun::star::uno::XAggregation > m_xAggProxy; 364 com::sun::star::uno::Reference< 365 com::sun::star::io::XStream > m_xWrappedStream; 366 com::sun::star::uno::Reference< 367 com::sun::star::io::XOutputStream > m_xWrappedOutputStream; 368 com::sun::star::uno::Reference< 369 com::sun::star::io::XTruncate > m_xWrappedTruncate; 370 com::sun::star::uno::Reference< 371 com::sun::star::io::XInputStream > m_xWrappedInputStream; 372 com::sun::star::uno::Reference< 373 com::sun::star::lang::XComponent > m_xWrappedComponent; 374 com::sun::star::uno::Reference< 375 com::sun::star::lang::XTypeProvider > m_xWrappedTypeProv; 376 }; 377 378 } // namespace tdoc_ucp 379 380 #endif /* !INCLUDED_TDOC_STGELEMS_HXX */ 381