xref: /trunk/main/ucb/source/ucp/gio/gio_seekable.cxx (revision 2f86921c)
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 #include <rtl/memory.h>
25 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
26 #include <ucbhelper/cancelcommandexecution.hxx>
27 #include <string.h>
28 
29 #include "gio_seekable.hxx"
30 #include "gio_content.hxx"
31 
32 using namespace com::sun::star;
33 
34 namespace gio
35 {
36 
Seekable(GSeekable * pStream)37 Seekable::Seekable(GSeekable *pStream) : mpStream(pStream)
38 {
39     if (!mpStream)
40         throw io::NotConnectedException();
41 }
42 
~Seekable(void)43 Seekable::~Seekable( void )
44 {
45 }
46 
truncate(void)47 void SAL_CALL Seekable::truncate( void )
48     throw( io::IOException, uno::RuntimeException )
49 {
50     if (!mpStream)
51         throw io::NotConnectedException();
52 
53     if (!g_seekable_can_truncate(mpStream))
54         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Truncate unsupported")),
55             static_cast< cppu::OWeakObject * >(this));
56 
57     GError *pError=NULL;
58     if (!g_seekable_truncate(mpStream, 0, NULL, &pError))
59         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
60 }
61 
seek(sal_Int64 location)62 void SAL_CALL Seekable::seek( sal_Int64 location )
63     throw( lang::IllegalArgumentException, io::IOException, uno::RuntimeException )
64 {
65     if (!mpStream)
66         throw io::NotConnectedException();
67 
68     if (!g_seekable_can_seek(mpStream))
69         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
70             static_cast< cppu::OWeakObject * >(this));
71 
72     GError *pError=NULL;
73     if (!g_seekable_seek(mpStream, location, G_SEEK_SET, NULL, &pError))
74         convertToException(pError, static_cast< cppu::OWeakObject * >(this));
75 }
76 
getPosition()77 sal_Int64 SAL_CALL Seekable::getPosition() throw( io::IOException, uno::RuntimeException )
78 {
79     if (!mpStream)
80         throw io::NotConnectedException();
81 
82     return g_seekable_tell(mpStream);
83 }
84 
getLength()85 sal_Int64 SAL_CALL Seekable::getLength() throw( io::IOException, uno::RuntimeException )
86 {
87     if (!mpStream)
88         throw io::NotConnectedException();
89 
90     bool bOk = false;
91     sal_uInt64 nSize = 0;
92 
93     GFileInfo* pInfo = G_IS_FILE_INPUT_STREAM(mpStream)
94         ? g_file_input_stream_query_info(G_FILE_INPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL)
95         : g_file_output_stream_query_info(G_FILE_OUTPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL);
96 
97     if (pInfo)
98     {
99         if (g_file_info_has_attribute(pInfo, G_FILE_ATTRIBUTE_STANDARD_SIZE))
100         {
101             nSize = g_file_info_get_size(pInfo);
102             bOk = true;
103         }
104         g_object_unref(pInfo);
105     }
106 
107     if (!bOk)
108     {
109         GError *pError=NULL;
110         sal_Int64 nCurr = getPosition();
111         if (!g_seekable_seek(mpStream, 0, G_SEEK_END, NULL, &pError))
112             convertToException(pError, static_cast< cppu::OWeakObject * >(this));
113         nSize = getPosition();
114         seek(nCurr);
115         bOk = true;
116     }
117 
118     if (!bOk)
119         throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Getting size unsupported")),
120             static_cast< cppu::OWeakObject * >(this));
121 
122     return nSize;
123 }
124 
queryInterface(const uno::Type & type)125 uno::Any Seekable::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
126 {
127     uno::Any aRet = ::cppu::queryInterface ( type,
128         static_cast< XSeekable * >( this ) );
129 
130     if (!aRet.hasValue() && g_seekable_can_truncate(mpStream))
131         aRet = ::cppu::queryInterface ( type, static_cast< XTruncate * >( this ) );
132 
133     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
134 }
135 
136 }
137