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 #ifndef _UNTOOLS_UCBLOCKBYTES_HXX
28 #define _UNTOOLS_UCBLOCKBYTES_HXX
29 
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <com/sun/star/uno/Sequence.hxx>
32 #include <com/sun/star/ucb/XContent.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include "unotools/unotoolsdllapi.h"
35 
36 #include <vos/thread.hxx>
37 #include <vos/conditn.hxx>
38 #include <vos/mutex.hxx>
39 #include <tools/stream.hxx>
40 #include <tools/link.hxx>
41 #include <tools/errcode.hxx>
42 #include <tools/datetime.hxx>
43 
44 namespace com
45 {
46 	namespace sun
47 	{
48 		namespace star
49 		{
50 			namespace task
51 			{
52 				class XInteractionHandler;
53 			}
54 			namespace io
55 			{
56 				class XStream;
57 				class XInputStream;
58 				class XOutputStream;
59 				class XSeekable;
60 			}
61 			namespace ucb
62 			{
63 				class XContent;
64 			}
65 			namespace beans
66 			{
67 				struct PropertyValue;
68 			}
69 		}
70 	}
71 }
72 
73 namespace utl
74 {
75 SV_DECL_REF( UcbLockBytes )
76 
77 class UcbLockBytesHandler : public SvRefBase
78 {
79     sal_Bool        m_bActive;
80 public:
81     enum LoadHandlerItem
82     {
83         DATA_AVAILABLE,
84         DONE,
85         CANCEL
86     };
87 
88                     UcbLockBytesHandler()
89                         : m_bActive( sal_True )
90                     {}
91 
92     virtual void    Handle( LoadHandlerItem nWhich, UcbLockBytesRef xLockBytes ) = 0;
93     void            Activate( sal_Bool bActivate = sal_True ) { m_bActive = bActivate; }
94     sal_Bool        IsActive() const { return m_bActive; }
95 };
96 
97 SV_DECL_IMPL_REF( UcbLockBytesHandler )
98 
99 #define NS_UNO ::com::sun::star::uno
100 #define NS_IO ::com::sun::star::io
101 #define NS_UCB ::com::sun::star::ucb
102 #define NS_BEANS ::com::sun::star::beans
103 #define NS_TASK ::com::sun::star::task
104 
105 class UNOTOOLS_DLLPUBLIC UcbLockBytes : public virtual SvLockBytes
106 {
107 	vos::OCondition			m_aInitialized;
108 	vos::OCondition			m_aTerminated;
109 	vos::OMutex				m_aMutex;
110 
111     String                  m_aContentType;
112     String                  m_aRealURL;
113     DateTime                m_aExpireDate;
114 
115     NS_UNO::Reference < NS_IO::XInputStream >  m_xInputStream;
116     NS_UNO::Reference < NS_IO::XOutputStream > m_xOutputStream;
117     NS_UNO::Reference < NS_IO::XSeekable >     m_xSeekable;
118 	void*					m_pCommandThread; // is alive only for compatibility reasons
119     UcbLockBytesHandlerRef  m_xHandler;
120 
121 	sal_uInt32              m_nRead;
122 	sal_uInt32              m_nSize;
123     ErrCode                 m_nError;
124 
125     sal_Bool                m_bTerminated : 1;
126     sal_Bool                m_bDontClose : 1;
127     sal_Bool                m_bStreamValid : 1;
128 
129 	DECL_LINK(				DataAvailHdl, void * );
130 
131                             UcbLockBytes( UcbLockBytesHandler* pHandler=NULL );
132 protected:
133 	virtual                 ~UcbLockBytes (void);
134 
135 public:
136 							// properties: Referer, PostMimeType
137     static UcbLockBytesRef  CreateLockBytes( const NS_UNO::Reference < NS_UCB::XContent >& xContent,
138                                             const ::rtl::OUString& rReferer,
139                                             const ::rtl::OUString& rMediaType,
140                                             const NS_UNO::Reference < NS_IO::XInputStream >& xPostData,
141                                             const NS_UNO::Reference < NS_TASK::XInteractionHandler >& xInter,
142 											UcbLockBytesHandler* pHandler=0 );
143 
144     static UcbLockBytesRef  CreateLockBytes( const NS_UNO::Reference < NS_UCB::XContent >& xContent,
145 											const NS_UNO::Sequence < NS_BEANS::PropertyValue >& rProps,
146 											StreamMode eMode,
147                                             const NS_UNO::Reference < NS_TASK::XInteractionHandler >& xInter,
148 											UcbLockBytesHandler* pHandler=0 );
149 
150     static UcbLockBytesRef  CreateInputLockBytes( const NS_UNO::Reference < NS_IO::XInputStream >& xContent );
151     static UcbLockBytesRef  CreateLockBytes( const NS_UNO::Reference < NS_IO::XStream >& xContent );
152 
153 	// SvLockBytes
154 	virtual void            SetSynchronMode (sal_Bool bSynchron);
155 	virtual ErrCode         ReadAt ( sal_uLong nPos, void *pBuffer, sal_uLong nCount, sal_uLong *pRead) const;
156 	virtual ErrCode         WriteAt ( sal_uLong, const void*, sal_uLong, sal_uLong *pWritten);
157 	virtual ErrCode         Flush (void) const;
158 	virtual ErrCode         SetSize (sal_uLong);
159 	virtual ErrCode         Stat ( SvLockBytesStat *pStat, SvLockBytesStatFlag) const;
160 
161     void                    SetError( ErrCode nError )
162                             { m_nError = nError; }
163 
164     ErrCode                 GetError() const
165                             { return m_nError; }
166 
167     void                    Cancel(); // is alive only for compatibility reasons
168 
169     // the following properties are available when and after the first DataAvailable callback has been executed
170     String                  GetContentType() const;
171     String                  GetRealURL() const;
172     DateTime                GetExpireDate() const;
173 
174     // calling this method delegates the responsibility to call closeinput to the caller!
175     NS_UNO::Reference < NS_IO::XInputStream > getInputStream();
176     NS_UNO::Reference < NS_IO::XStream > getStream();
177 
178 #if _SOLAR__PRIVATE
179     sal_Bool                setInputStream_Impl( const NS_UNO::Reference < NS_IO::XInputStream > &rxInputStream,
180 												 sal_Bool bSetXSeekable = sal_True );
181     sal_Bool                setStream_Impl( const NS_UNO::Reference < NS_IO::XStream > &rxStream );
182     void                    terminate_Impl (void);
183 
184     NS_UNO::Reference < NS_IO::XInputStream > getInputStream_Impl() const
185                             {
186                                 vos::OGuard aGuard( SAL_CONST_CAST(UcbLockBytes*, this)->m_aMutex );
187                                 return m_xInputStream;
188                             }
189 
190     NS_UNO::Reference < NS_IO::XOutputStream > getOutputStream_Impl() const
191                             {
192                                 vos::OGuard aGuard( SAL_CONST_CAST(UcbLockBytes*, this)->m_aMutex );
193                                 return m_xOutputStream;
194                             }
195 
196     NS_UNO::Reference < NS_IO::XSeekable > getSeekable_Impl() const
197                             {
198                                 vos::OGuard aGuard( SAL_CONST_CAST(UcbLockBytes*, this)->m_aMutex );
199                                 return m_xSeekable;
200                             }
201 
202     sal_Bool                hasInputStream_Impl() const
203                             {
204                                 vos::OGuard aGuard( SAL_CONST_CAST(UcbLockBytes*, this)->m_aMutex );
205                                 return m_xInputStream.is();
206                             }
207 
208     void                    setDontClose_Impl()
209                             { m_bDontClose = sal_True; }
210 
211     void                    SetContentType_Impl( const String& rType ) { m_aContentType = rType; }
212     void                    SetRealURL_Impl( const String& rURL )  { m_aRealURL = rURL; }
213     void                    SetExpireDate_Impl( const DateTime& rDateTime )  { m_aExpireDate = rDateTime; }
214     void                    SetStreamValid_Impl();
215 #endif
216 };
217 
218 SV_IMPL_REF( UcbLockBytes );
219 
220 }
221 
222 #endif
223