1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2f86921cSAndrew Rist * distributed with this work for additional information
6*2f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist * with the License. You may obtain a copy of the License at
10*2f86921cSAndrew Rist *
11*2f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*2f86921cSAndrew Rist *
13*2f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist * KIND, either express or implied. See the License for the
17*2f86921cSAndrew Rist * specific language governing permissions and limitations
18*2f86921cSAndrew Rist * under the License.
19*2f86921cSAndrew Rist *
20*2f86921cSAndrew Rist *************************************************************/
21*2f86921cSAndrew Rist
22*2f86921cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <rtl/memory.h>
25cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
26cdf0e10cSrcweir #include <ucbhelper/cancelcommandexecution.hxx>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "gio_seekable.hxx"
30cdf0e10cSrcweir #include "gio_content.hxx"
31cdf0e10cSrcweir
32cdf0e10cSrcweir using namespace com::sun::star;
33cdf0e10cSrcweir
34cdf0e10cSrcweir namespace gio
35cdf0e10cSrcweir {
36cdf0e10cSrcweir
Seekable(GSeekable * pStream)37cdf0e10cSrcweir Seekable::Seekable(GSeekable *pStream) : mpStream(pStream)
38cdf0e10cSrcweir {
39cdf0e10cSrcweir if (!mpStream)
40cdf0e10cSrcweir throw io::NotConnectedException();
41cdf0e10cSrcweir }
42cdf0e10cSrcweir
~Seekable(void)43cdf0e10cSrcweir Seekable::~Seekable( void )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir
truncate(void)47cdf0e10cSrcweir void SAL_CALL Seekable::truncate( void )
48cdf0e10cSrcweir throw( io::IOException, uno::RuntimeException )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir if (!mpStream)
51cdf0e10cSrcweir throw io::NotConnectedException();
52cdf0e10cSrcweir
53cdf0e10cSrcweir if (!g_seekable_can_truncate(mpStream))
54cdf0e10cSrcweir throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Truncate unsupported")),
55cdf0e10cSrcweir static_cast< cppu::OWeakObject * >(this));
56cdf0e10cSrcweir
57cdf0e10cSrcweir GError *pError=NULL;
58cdf0e10cSrcweir if (!g_seekable_truncate(mpStream, 0, NULL, &pError))
59cdf0e10cSrcweir convertToException(pError, static_cast< cppu::OWeakObject * >(this));
60cdf0e10cSrcweir }
61cdf0e10cSrcweir
seek(sal_Int64 location)62cdf0e10cSrcweir void SAL_CALL Seekable::seek( sal_Int64 location )
63cdf0e10cSrcweir throw( lang::IllegalArgumentException, io::IOException, uno::RuntimeException )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir if (!mpStream)
66cdf0e10cSrcweir throw io::NotConnectedException();
67cdf0e10cSrcweir
68cdf0e10cSrcweir if (!g_seekable_can_seek(mpStream))
69cdf0e10cSrcweir throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Seek unsupported")),
70cdf0e10cSrcweir static_cast< cppu::OWeakObject * >(this));
71cdf0e10cSrcweir
72cdf0e10cSrcweir GError *pError=NULL;
73cdf0e10cSrcweir if (!g_seekable_seek(mpStream, location, G_SEEK_SET, NULL, &pError))
74cdf0e10cSrcweir convertToException(pError, static_cast< cppu::OWeakObject * >(this));
75cdf0e10cSrcweir }
76cdf0e10cSrcweir
getPosition()77cdf0e10cSrcweir sal_Int64 SAL_CALL Seekable::getPosition() throw( io::IOException, uno::RuntimeException )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir if (!mpStream)
80cdf0e10cSrcweir throw io::NotConnectedException();
81cdf0e10cSrcweir
82cdf0e10cSrcweir return g_seekable_tell(mpStream);
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
getLength()85cdf0e10cSrcweir sal_Int64 SAL_CALL Seekable::getLength() throw( io::IOException, uno::RuntimeException )
86cdf0e10cSrcweir {
87cdf0e10cSrcweir if (!mpStream)
88cdf0e10cSrcweir throw io::NotConnectedException();
89cdf0e10cSrcweir
90cdf0e10cSrcweir bool bOk = false;
91cdf0e10cSrcweir sal_uInt64 nSize = 0;
92cdf0e10cSrcweir
93cdf0e10cSrcweir GFileInfo* pInfo = G_IS_FILE_INPUT_STREAM(mpStream)
94cdf0e10cSrcweir ? g_file_input_stream_query_info(G_FILE_INPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL)
95cdf0e10cSrcweir : g_file_output_stream_query_info(G_FILE_OUTPUT_STREAM(mpStream), const_cast<char*>(G_FILE_ATTRIBUTE_STANDARD_SIZE), NULL, NULL);
96cdf0e10cSrcweir
97cdf0e10cSrcweir if (pInfo)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir if (g_file_info_has_attribute(pInfo, G_FILE_ATTRIBUTE_STANDARD_SIZE))
100cdf0e10cSrcweir {
101cdf0e10cSrcweir nSize = g_file_info_get_size(pInfo);
102cdf0e10cSrcweir bOk = true;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir g_object_unref(pInfo);
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir if (!bOk)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir GError *pError=NULL;
110cdf0e10cSrcweir sal_Int64 nCurr = getPosition();
111cdf0e10cSrcweir if (!g_seekable_seek(mpStream, 0, G_SEEK_END, NULL, &pError))
112cdf0e10cSrcweir convertToException(pError, static_cast< cppu::OWeakObject * >(this));
113cdf0e10cSrcweir nSize = getPosition();
114cdf0e10cSrcweir seek(nCurr);
115cdf0e10cSrcweir bOk = true;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir if (!bOk)
119cdf0e10cSrcweir throw io::IOException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Getting size unsupported")),
120cdf0e10cSrcweir static_cast< cppu::OWeakObject * >(this));
121cdf0e10cSrcweir
122cdf0e10cSrcweir return nSize;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
queryInterface(const uno::Type & type)125cdf0e10cSrcweir uno::Any Seekable::queryInterface( const uno::Type &type ) throw( uno::RuntimeException )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir uno::Any aRet = ::cppu::queryInterface ( type,
128cdf0e10cSrcweir static_cast< XSeekable * >( this ) );
129cdf0e10cSrcweir
130cdf0e10cSrcweir if (!aRet.hasValue() && g_seekable_can_truncate(mpStream))
131cdf0e10cSrcweir aRet = ::cppu::queryInterface ( type, static_cast< XTruncate * >( this ) );
132cdf0e10cSrcweir
133cdf0e10cSrcweir return aRet.hasValue() ? aRet : OWeakObject::queryInterface( type );
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
136cdf0e10cSrcweir }
137